• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • Directories
  • File List
  • File Members

functional.hpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2010 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
00024 */
00030 #ifndef __CLAW_FUNCTIONAL_HPP__
00031 #define __CLAW_FUNCTIONAL_HPP__
00032 
00033 #include <utility>
00034 #include <functional>
00035 
00036 namespace claw
00037 {
00038   /*-------------------------------------------------------------------------*/
00043   template <class T1, class T2>
00044   class first: public std::unary_function< std::pair<T1, T2>, T1& >  
00045   {
00046   public:
00047     T1& operator()( std::pair<T1, T2>& p ) const 
00048     {
00049       return p.first;
00050     } // operator() const
00051   }; // class first
00052 
00053   /*-------------------------------------------------------------------------*/
00058   template <class T1, class T2>
00059   class const_first:
00060     public std::unary_function<const std::pair<T1, T2>, const T1&>
00061   {
00062   public:
00063     const T1& operator()( const std::pair<T1, T2>& p ) const 
00064     {
00065       return p.first;
00066     } // operator()
00067 
00068   }; // class const_first
00069 
00070   /*-------------------------------------------------------------------------*/
00077   template<class Pair>
00078   class pair_first:
00079     public first<typename Pair::first_type, typename Pair::second_type>
00080   {
00081     // nothing
00082   }; // class pair_first
00083 
00084   /*-------------------------------------------------------------------------*/
00091   template<class Pair>
00092   class const_pair_first:
00093     public const_first<typename Pair::first_type, typename Pair::second_type>
00094   {
00095     // nothing
00096   }; // class const_pair_first
00097 
00098   /*-------------------------------------------------------------------------*/
00103   template <class T1, class T2>
00104   class second: public std::unary_function< std::pair<T1, T2>, T2& >  
00105   {
00106   public:
00107     T2& operator()( std::pair<T1, T2>& p ) const 
00108     {
00109       return p.second;
00110     } // operator() const
00111   }; // class second
00112 
00113   /*-------------------------------------------------------------------------*/
00118   template <class T1, class T2>
00119   class const_second:
00120     public std::unary_function< const std::pair<T1, T2>, const T2& >  
00121   {
00122   public:
00123     const T2& operator()( const std::pair<T1, T2>& p ) const 
00124     {
00125       return p.second;
00126     } // operator()
00127 
00128   }; // class const_second
00129 
00130   /*-------------------------------------------------------------------------*/
00137   template< class Pair >
00138   class pair_second: public second< typename Pair::first_type,
00139                                      typename Pair::second_type >
00140   {
00141     // nothing
00142   }; // class pair_second
00143 
00144   /*-------------------------------------------------------------------------*/
00151   template<class Pair>
00152   class const_pair_second:
00153     public const_second<typename Pair::first_type, typename Pair::second_type>
00154   {
00155   public:
00156     const_pair_second() {}
00157 
00158     template<typename F, typename S>
00159     const_pair_second( const second<F, S>& )
00160       {}
00161 
00162   }; // class const_pair_second
00163 
00164   /*-------------------------------------------------------------------------*/
00175   template<class T>
00176   class unary_true: public std::unary_function<T, bool>
00177   {
00178   public:
00179     bool operator()( const T& t ) const { return true; }
00180   }; // class unary_true
00181 
00182   /*-------------------------------------------------------------------------*/
00194   template<class T, class U>
00195   class binary_true: public std::binary_function<T, U, bool>
00196   {
00197   public:
00198     bool operator()( const T& t, const U& u ) const
00199     {
00200       return true;
00201     } // operator()
00202   }; // class binary_true
00203 
00204   /*-------------------------------------------------------------------------*/
00216   template<typename F1, typename F2>
00217   class unary_compose
00218    : public std::unary_function< typename F2::argument_type,
00219                                   typename F1::result_type >
00220   {
00221   public:
00222     unary_compose() {}
00223 
00231     template<typename G1, typename G2>
00232     unary_compose( const unary_compose<G1, G2>& that ) { }
00233       
00237     typename F1::result_type
00238     operator()( typename F2::argument_type& a ) const
00239     {
00240       return F1()( F2()(a) );
00241     }
00242   }; // class unary_compose
00243 
00244   /*-------------------------------------------------------------------------*/
00254   template<typename T>
00255   class delete_function: public std::unary_function<T, void>
00256   {
00257   public:
00258     void operator()( const T& a ) const
00259     {
00260       delete a;
00261     }
00262   }; // class delete_function
00263 
00264   /*-------------------------------------------------------------------------*/
00274   template<typename T>
00275   class clone: public std::unary_function<T*, T*>
00276   {
00277   public:
00278     T* operator()( const T* a ) const
00279     {
00280       return new T(*a);
00281     }
00282   }; // class clone
00283 
00284   /*-------------------------------------------------------------------------*/
00293   template<typename T>
00294   class dereference:
00295     public std::unary_function<T*, T&>
00296   {
00297   public:
00298     T& operator()( T* a ) const
00299     {
00300       return *a;
00301     }
00302 
00303   }; // class dereference
00304 
00305   /*-------------------------------------------------------------------------*/
00314   template<typename T>
00315   class const_dereference:
00316     public std::unary_function<const T*, const T&>
00317   {
00318   public:
00319     const_dereference() { }
00320     const_dereference( const dereference<T>& ) { }
00321     const_dereference( const const_dereference<T>& ) { }
00322 
00323     const T& operator()( const T* a ) const
00324     {
00325       return *a;
00326     }
00327 
00328   }; // class const_dereference
00329 } // namespace claw
00330 
00331 #endif // __CLAW_FUNCTIONAL_HPP__

Generated on Fri Dec 24 2010 13:18:31 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.7.2