Skip to content
Snippets Groups Projects
Commit a3588b29 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify compare

parent f618f68f
No related branches found
No related tags found
No related merge requests found
......@@ -14,66 +14,69 @@
 
namespace alib {
 
template<typename T>
template < typename T >
class base {
public:
virtual ~base() noexcept {}
virtual ~base ( ) noexcept { }
 
virtual long long selfTypeId() const = 0;
virtual long long selfTypeId ( ) const = 0;
 
virtual T* clone() const = 0;
virtual T * clone ( ) const = 0;
 
virtual T* plunder() && = 0;
virtual T * plunder ( ) && = 0;
 
bool operator!=(const T& other) const {
return this->compare(other) != 0;
bool operator !=( const T & other ) const {
return this->compare ( other ) != 0;
}
 
bool operator==(const T & other) const {
return this->compare(other) == 0;
bool operator ==( const T & other ) const {
return this->compare ( other ) == 0;
}
 
bool operator<(const T & other) const {
return this->compare(other) < 0;
bool operator <( const T & other ) const {
return this->compare ( other ) < 0;
}
 
bool operator<=(const T & other) const {
return this->compare(other) <= 0;
bool operator <=( const T & other ) const {
return this->compare ( other ) <= 0;
}
 
bool operator>(const T & other) const {
return this->compare(other) > 0;
bool operator >( const T & other ) const {
return this->compare ( other ) > 0;
}
 
bool operator>=(const T & other) const {
return this->compare(other) >= 0;
bool operator >=( const T & other ) const {
return this->compare ( other ) >= 0;
}
 
virtual int compare(const T & other) const = 0;
virtual int compare ( const T & other ) const = 0;
 
template<typename R, typename S> //TODO what if someone want to control the ordering?
static int compare(const R& first, const S& second) {
if(first.selfTypeId() < second.selfTypeId())
template < typename R >
// TODO what if someone want to control the ordering?
static int compare ( const R & first, const T & second ) {
if ( first.selfTypeId ( ) < second.selfTypeId ( ) )
return -1;
else if(first.selfTypeId() > second.selfTypeId())
else if ( first.selfTypeId ( ) > second.selfTypeId ( ) )
return 1;
else
return first.compare((const R&) second);
return first.compare ( ( const R & ) second );
}
 
friend std::ostream& operator<<(std::ostream& os, const T & instance) {
friend std::ostream & operator <<( std::ostream & os, const T & instance ) {
instance >> os;
return os;
}
 
virtual void operator>>(std::ostream&) const = 0;
virtual void operator >>( std::ostream & ) const = 0;
 
virtual explicit operator std::string () const = 0;
virtual explicit operator std::string ( ) const = 0;
 
template<typename R>
static long long typeId(const R&) {
return (long long) &base::typeId<R>;
template < typename R >
static long long typeId ( const R & ) {
return ( long long ) & base::typeId < R >;
}
};
 
} /* namespace alib */
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment