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 @@ ...@@ -14,66 +14,69 @@
   
namespace alib { namespace alib {
   
template<typename T> template < typename T >
class base { class base {
public: 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 { bool operator !=( const T & other ) const {
return this->compare(other) != 0; return this->compare ( other ) != 0;
} }
   
bool operator==(const T & other) const { bool operator ==( const T & other ) const {
return this->compare(other) == 0; return this->compare ( other ) == 0;
} }
   
bool operator<(const T & other) const { bool operator <( const T & other ) const {
return this->compare(other) < 0; return this->compare ( other ) < 0;
} }
   
bool operator<=(const T & other) const { bool operator <=( const T & other ) const {
return this->compare(other) <= 0; return this->compare ( other ) <= 0;
} }
   
bool operator>(const T & other) const { bool operator >( const T & other ) const {
return this->compare(other) > 0; return this->compare ( other ) > 0;
} }
   
bool operator>=(const T & other) const { bool operator >=( const T & other ) const {
return this->compare(other) >= 0; 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? template < typename R >
static int compare(const R& first, const S& second) {
if(first.selfTypeId() < second.selfTypeId()) // 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; return -1;
else if(first.selfTypeId() > second.selfTypeId()) else if ( first.selfTypeId ( ) > second.selfTypeId ( ) )
return 1; return 1;
else 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; instance >> os;
return 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> template < typename R >
static long long typeId(const R&) { static long long typeId ( const R & ) {
return (long long) &base::typeId<R>; return ( long long ) & base::typeId < R >;
} }
}; };
   
} /* namespace alib */ } /* 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