Advertisement
Guest User

Phil Nash

a guest
Nov 8th, 2008
578
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1.     enum ComparisonResult
  2.     {
  3.         less = -1,
  4.         equal = 0,
  5.         greater = 1
  6.     };
  7.        
  8.     template<typename T>
  9.     class CompareChain : std::binary_function<const T&, const T&, bool>
  10.     {
  11.         typedef boost::function<ComparisonResult( const T& lhs, const T& rhs )> CompareFunctor;
  12.     public:
  13.         CompareChain( const CompareFunctor& f1 )
  14.         {
  15.             functions.push_back( f1 );
  16.         }
  17.        
  18.         CompareChain( const CompareFunctor& f1, const CompareFunctor& f2 )
  19.         {
  20.             functions.push_back( f1 );
  21.             functions.push_back( f2 );
  22.         }
  23.        
  24.         CompareChain( const CompareFunctor& f1, const CompareFunctor& f2, const CompareFunctor& f3 )
  25.         {
  26.             functions.push_back( f1 );
  27.             functions.push_back( f2 );
  28.             functions.push_back( f3 );
  29.         }
  30.        
  31.         bool operator()( const T& lhs, const T& rhs ) const
  32.         {
  33.             BOOST_FOREACH( CompareFunctor f, functions )           
  34.             {
  35.                 switch( f( lhs, rhs ) )
  36.                 {
  37.                     case less:
  38.                         return true;
  39.                     case greater:
  40.                         return false;
  41.                 }
  42.             }
  43.             return false;
  44.         }
  45.     private:
  46.         std::vector<CompareFunctor> functions;
  47.     };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement