compose.hh

00001 // Copyright (C) 2001, 2004  EPITA Research and Development Laboratory
00002 //
00003 // This file is part of the Olena Library.  This library is free
00004 // software; you can redistribute it and/or modify it under the terms
00005 // of the GNU General Public License version 2 as published by the
00006 // Free Software Foundation.
00007 //
00008 // This library is distributed in the hope that it will be useful,
00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00011 // General Public License for more details.
00012 //
00013 // You should have received a copy of the GNU General Public License
00014 // along with this library; see the file COPYING.  If not, write to
00015 // the Free Software Foundation, 59 Temple Place - Suite 330, Boston,
00016 // MA 02111-1307, USA.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software library without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to
00022 // produce an executable, this file does not by itself cause the
00023 // resulting executable to be covered by the GNU General Public
00024 // License.  This exception does not however invalidate any other
00025 // reasons why the executable file might be covered by the GNU General
00026 // Public License.
00027 
00028 #ifndef OLENA_CORE_COMPOSE_HH
00029 # define OLENA_CORE_COMPOSE_HH
00030 
00031 # include <functional>
00032 
00033 namespace oln {
00034 
00035   namespace internal {
00036 
00043     template< class F1, class F2 >
00044     struct compose_uu_ :
00045       public std::unary_function <typename F2::argument_type,
00046                                   typename F1::result_type>
00047     {
00048       typedef compose_uu_ self_type;
00049 
00050       typename self_type::result_type
00051       operator()(typename self_type::argument_type arg) const 
00052       {
00053         return f1_(f2_(arg));
00054       }
00055 
00056       compose_uu_(const F1& f1, const F2& f2) : f1_(f1), f2_(f2) {}
00057       
00058     private:
00059       
00060       const F1 f1_;
00061       const F2 f2_;
00062     
00063     };
00064 
00072     template< class F1, class F2 >
00073     struct compose_ub_ :
00074       public std::binary_function <typename F2::first_argument_type,
00075                                    typename F2::second_argument_type,
00076                                    typename F1::result_type>
00077     {
00078       typedef compose_ub_ self_type;
00079 
00080       typename self_type::result_type
00081       operator()(typename self_type::first_argument_type arg1,
00082                  typename self_type::second_argument_type arg2) const 
00083       {
00084         return f1_(f2_(arg1, arg2));
00085       }
00086 
00087       compose_ub_(const F1& f1, const F2& f2) : f1_(f1), f2_(f2) {}
00088       
00089     private:
00090     
00091       const F1 f1_;
00092       const F2 f2_;
00093     
00094     };
00095     
00102     template< class F1, class F2 >
00103     struct compose_bu_ :
00104       public std::binary_function <typename F2::argument_type,
00105                                    typename F2::argument_type,
00106                                    typename F1::result_type>
00107     {
00108       typedef compose_bu_ self_type;
00109 
00110       typename self_type::result_type
00111       operator()(typename self_type::first_argument_type arg1,
00112                  typename self_type::second_argument_type arg2) const 
00113       {
00114         return f1_(f2_(arg1), f2_(arg2));
00115       }
00116 
00117       compose_bu_(const F1& f1, const F2& f2) : f1_(f1), f2_(f2) {}
00118 
00119     private:
00120     
00121       const F1 f1_;
00122       const F2 f2_;
00123     
00124     };
00125 
00126   }
00127 
00128   
00130   template<class UF1, class UF2>
00131   internal::compose_uu_<UF1, UF2>
00132   compose_uu(const UF1& f1, const UF2& f2)
00133   {
00134     return internal::compose_uu_<UF1, UF2>(f1, f2);
00135   }
00136 
00138   template<class UF1, class BF2>
00139   internal::compose_ub_<UF1, BF2>
00140   compose_ub(const UF1& f1, const BF2& f2)
00141   {
00142     return internal::compose_ub_<UF1, BF2>(f1, f2);
00143   }
00144 
00146   template<class BF1, class UF2>
00147   internal::compose_bu_<BF1, UF2>
00148   compose_bu(const BF1& f1, const UF2& f2)
00149   {
00150     return internal::compose_bu_<BF1, UF2>(f1, f2);
00151   }
00152 
00158   template<class T>
00159   struct f_identity : std::unary_function<T, T>
00160   {
00161     T 
00162     operator()(T t) const
00163     {
00164       return t;
00165     }
00166   };
00167 
00168 } // end of oln
00169 
00170 #endif // OLENA_CORE_COMPOSE_HH

Generated on Thu Apr 15 20:13:07 2004 for Olena by doxygen 1.3.6-20040222