Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

compose.h

Go to the documentation of this file.
00001 /***
00002  * Copyright (c) 2005, SkeTo Project
00003  * All rights reserved.
00004  */
00011 #ifndef __COMPOSE_H__
00012 #define __COMPOSE_H__
00013 
00014 #include "functions.h"
00015 
00016 namespace skeleton {
00017   template< typename F, typename G >
00018   class composite_unary_function
00019     : public skeleton::unary_function< typename G::argument_type,
00020                        typename F::result_type >
00021   {
00022     F f;
00023     G g;
00024   public:
00025     composite_unary_function( const F &f_, const G &g_ ) : f( f_ ), g( g_ )
00026     { }
00027     typename F::result_type
00028     operator()( typename G::argument_type x ) const
00029     {
00030       return f( g( x ) );
00031     }
00032   };
00033   
00034   template< typename F, typename G >
00035   class composite_unary_ptr_function
00036     : public skeleton::unary_ptr_function< typename F::first_argument_type,
00037                        typename G::second_argument_type >
00038   {
00039     F f;
00040     G g;
00041   public:
00042     composite_unary_ptr_function( const F &f_, const G &g_ ) : f( f_ ), g( g_ )
00043     { }
00044     void
00045     operator()( typename F::first_argument_type        *r,
00046         const typename G::second_argument_type *x ) const
00047     {
00048       typename G::first_argument_type a;
00049       g( &a, x );
00050       f( r, &a );
00051     }
00052   };
00053   
00054   template< typename F, typename G >
00055   class composite_binary_function
00056     : public skeleton::binary_function< typename G::first_argument_type,
00057                     typename G::second_argument_type,
00058                     typename F::result_type >
00059   {
00060     F f;
00061     G g;
00062   public:
00063     composite_binary_function( const F &f_, const G &g_ ) : f( f_ ), g( g_ )
00064     {}
00065     typename F::result_type
00066     operator()( typename G::first_argument_type  x,
00067         typename G::second_argument_type y ) const
00068     {
00069       return f( g( x, y ) );
00070     }
00071   };
00072   
00073   template< typename F, typename G >
00074   class composite_binary_ptr_function
00075     : public skeleton::binary_function< typename F::first_argument_type,
00076                     typename G::second_argument_type,
00077                     typename G::third_argument_type >
00078   {
00079     F f;
00080     G g;
00081   public:
00082     composite_binary_ptr_function( const F &f_, const G &g_ ) : f( f_ ), g( g_ )
00083     {}
00084     void
00085     operator()( typename F::first_argument_type        *r,
00086         const typename G::second_argument_type *x,
00087         const typename G::third_argument_type  *y ) const
00088     {
00089       typename F::first_argument_type a;
00090       g( &a, x, y );
00091       f( r, &a );
00092     }
00093   };
00094   
00095   template< typename F, typename G, typename H >
00096   class composite_binary_two_function
00097     : public skeleton::binary_function< typename G::argument_type,
00098                     typename H::argument_type,
00099                     typename F::result_type >
00100   {
00101     F f;
00102     G g;
00103     H h;
00104   public:
00105     composite_binary_two_function( const F &f_, const G &g_, const H &h_ ) : f( f_ ), g( g_ ), h( h_ )
00106     {}
00107     typename F::result_type
00108     operator()( typename G::argument_type x,
00109         typename H::argument_type y ) const
00110     {
00111       return f( g( x ), h( y ) );
00112     }
00113   };
00114   
00115   template< typename F, typename G, typename H >
00116   class composite_binary_two_ptr_function
00117     : public skeleton::binary_function< typename F::first_argument_type,
00118                     typename G::second_argument_type,
00119                     typename H::second_argument_type >
00120   {
00121     F f;
00122     G g;
00123     H h;
00124   public:
00125     composite_binary_two_ptr_function( const F &f_, const G &g_, const H &h_ ) : f( f_ ), g( g_ ), h( h_ )
00126     {}
00127     void
00128     operator()( typename F::first_argument_type        *r,
00129         const typename G::second_argument_type *x,
00130         const typename H::second_argument_type *y ) const
00131     {
00132       typename G::first_argument_type a;
00133       typename H::first_argument_type b;
00134       g( &a, x );
00135       h( &b, y );
00136       f( r, &a, &b );
00137     }
00138   };
00139   
00140   template< typename F, typename G >
00141   inline skeleton::composite_unary_function< F, G >
00142   compose_unary_functions( const F &f, const G &g )
00143   {
00144     return skeleton::composite_unary_function< F, G >( f, g );
00145   }
00146   
00147   template< typename F, typename G >
00148   inline skeleton::composite_unary_ptr_function< F, G >
00149   compose_unary_ptr_functions( const F &f, const G &g )
00150   {
00151     return skeleton::composite_unary_ptr_function< F, G >( f, g );
00152   }
00153   
00154   template< typename F, typename G >
00155   inline skeleton::composite_binary_function< F, G >
00156   compose_binary_functions( const F &f, const G &g )
00157   {
00158     return skeleton::composite_binary_function< F, G >( f, g );
00159   }
00160   
00161   template< typename F, typename G >
00162   inline skeleton::composite_binary_ptr_function< F, G >
00163   compose_binary_ptr_functions( const F &f, const G &g )
00164   {
00165     return skeleton::composite_binary_ptr_function< F, G >( f, g );
00166   }
00167   
00168   template< typename F, typename G, typename H >
00169   inline skeleton::composite_binary_two_function< F, G, H >
00170   compose_binary_two_functions( const F &f, const G &g, const H &h )
00171   {
00172     return skeleton::composite_binary_two_function< F, G, H >( f, g, h );
00173   }
00174   
00175   template< typename F, typename G, typename H >
00176   inline skeleton::composite_binary_two_ptr_function< F, G, H >
00177   compose_binary_two_ptr_functions( const F &f, const G &g, const H &h )
00178   {
00179     return skeleton::composite_binary_two_ptr_function< F, G, H >( f, g, h );
00180   }
00181 }
00182 
00183 #endif // __COMPOSE_H__

Generated on Wed Jan 18 22:19:30 2006 for SkeTo -- Skeleton Library in Tokyo by  doxygen 1.4.4