Function.h

00001 
00002 // The Loki Library
00003 // Copyright (c) 2005 Peter Kümmel
00004 // Permission to use, copy, modify, distribute and sell this software for any 
00005 //     purpose is hereby granted without fee, provided that the above copyright 
00006 //     notice appear in all copies and that both that copyright notice and this 
00007 //     permission notice appear in supporting documentation.
00008 // The author makes no representations about the 
00009 //     suitability of this software for any purpose. It is provided "as is" 
00010 //     without express or implied warranty.
00012 #ifndef LOKI_FUNCTION_INC_
00013 #define LOKI_FUNCTION_INC_
00014 
00015 // $Id: Function.h 750 2006-10-17 19:50:02Z syntheticpp $
00016 
00017 
00018 #define LOKI_ENABLE_FUNCTION
00019 
00020 #include <loki/Functor.h>
00021 #include <loki/Sequence.h>
00022 
00023 namespace Loki
00024 {
00025 
00041     
00042     template<class R = void()>
00043     struct Function;
00044 
00045 
00046     template<class R>
00047     struct Function<R()> : public Functor<R>
00048     {
00049         typedef Functor<R> FBase;
00050 
00051         Function() : FBase() {}
00052 
00053         Function(const Function& func) : FBase() 
00054         {
00055             if( !func.empty()) 
00056                 FBase::operator=(func);
00057         }
00058                 
00059         // test on emptiness
00060         template<class R2> 
00061         Function(Function<R2()> func) : FBase() 
00062         {
00063             if(!func.empty())
00064                 FBase::operator=(func);
00065         }
00066         
00067         // clear  by '= 0'
00068         Function(const int i) : FBase()
00069         { 
00070             if(i==0)
00071                 FBase::clear();
00072             else
00073                 throw std::runtime_error("Loki::Function(const int i): i!=0");
00074         }
00075         
00076         template<class Func>
00077         Function(Func func) : FBase(func) {}
00078 
00079         template<class Host, class Func>
00080         Function(const Host& host, const Func& func) : FBase(host,func) {}
00081 
00082     };
00083 
00084 
00086 // macros for the repetitions
00088 
00089 #define LOKI_FUNCTION_BODY                          \
00090                                                     \
00091         Function() : FBase() {}                     \
00092                                                     \
00093         Function(const Function& func) : FBase()    \
00094         {                                           \
00095             if( !func.empty())                      \
00096                 FBase::operator=(func);             \
00097         }                                           \
00098                                                     \
00099         Function(const int i) : FBase()             \
00100         {                                           \
00101             if(i==0)                                \
00102                 FBase::clear();                     \
00103             else                                    \
00104                 throw std::runtime_error(           \
00105             "Loki::Function(const int i): i!=0");   \
00106         }                                           \
00107                                                     \
00108         template<class Func>                        \
00109         Function(Func func) : FBase(func) {}        \
00110                                                     \
00111         template<class Host, class Func>            \
00112         Function(const Host& host, const Func& func): FBase(host,func) {}
00113 
00114         
00115 #define LOKI_FUNCTION_R2_CTOR_BODY          \
00116                                             \
00117         : FBase()                           \
00118         {                                   \
00119             if(!func.empty())               \
00120                 FBase::operator=(func);     \
00121         }
00122 
00123 
00125 // repetitions
00127 
00128     template<>
00129     struct Function<>
00130         : public Loki::Functor<>
00131     {
00132         typedef Functor<> FBase;
00133         
00134         template<class R2>
00135         Function(Function<R2()> func) 
00136             LOKI_FUNCTION_R2_CTOR_BODY
00137 
00138         LOKI_FUNCTION_BODY // if compilation breaks here then 
00139                            // Function.h was not included before
00140                            // Functor.h, check your include order
00141                            // or define LOKI_ENABLE_FUNCTION 
00142     };
00143 
00144     template<class R,class P01>
00145     struct Function<R(P01)> 
00146         : public Loki::Functor<R, Seq<P01> >
00147     {
00148         typedef Functor<R, Seq<P01> > FBase;
00149         
00150         template<class R2,class Q01>
00151         Function(Function<R2(Q01)> func) 
00152             LOKI_FUNCTION_R2_CTOR_BODY
00153 
00154         LOKI_FUNCTION_BODY
00155     };
00156 
00157     template<class R,class P01,class P02>
00158     struct Function<R(P01,P02)> 
00159         : public Functor<R, Seq<P01,P02> >
00160     {
00161         typedef Functor<R, Seq<P01,P02> > FBase;
00162 
00163         template<class R2,class Q01, class Q02>
00164         Function(Function<R2(Q01,Q02)> func) 
00165             LOKI_FUNCTION_R2_CTOR_BODY
00166 
00167         LOKI_FUNCTION_BODY
00168     };
00169 
00170     template<class R,class P01,class P02, class P03>
00171     struct Function<R(P01,P02,P03)> 
00172         : public Functor<R, Seq<P01,P02,P03> >
00173     {
00174         typedef Functor<R, Seq<P01,P02,P03> > FBase;
00175 
00176         template<class R2,class Q01, class Q02,class Q03>
00177         Function(Function<R2(Q01,Q02,Q03)> func) 
00178             LOKI_FUNCTION_R2_CTOR_BODY
00179 
00180         LOKI_FUNCTION_BODY
00181     };
00182 
00183     template<class R,class P01,class P02, class P03,class P04>
00184     struct Function<R(P01,P02,P03,P04)> 
00185         : public Functor<R, Seq<P01,P02,P03,P04> >
00186     {
00187         typedef Functor<R, Seq<P01,P02,P03,P04> > FBase;
00188 
00189         template<class R2,class Q01,class Q02, class Q03,class Q04>
00190         Function(Function<R2(Q01,Q02,Q03,Q04)> func) 
00191             LOKI_FUNCTION_R2_CTOR_BODY
00192 
00193         LOKI_FUNCTION_BODY
00194     };
00195 
00196     template<class R,class P01,class P02, class P03,class P04,class P05>
00197     struct Function<R(P01,P02,P03,P04,P05)> 
00198         : public Functor<R, Seq<P01,P02,P03,P04,P05> >
00199     {
00200         typedef Functor<R, Seq<P01,P02,P03,P04,P05> > FBase;
00201 
00202         template<class R2,class Q01,class Q02, class Q03,class Q04,class Q05>
00203         Function(Function<R2(Q01,Q02,Q03,Q04,Q05)> func) 
00204             LOKI_FUNCTION_R2_CTOR_BODY
00205 
00206         LOKI_FUNCTION_BODY
00207     };
00208 
00209     template<class R,    class P01,class P02, class P03,class P04,class P05,
00210                         class P06>
00211     struct Function<R(P01,P02,P03,P04,P05,P06)> 
00212         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06> >
00213     {
00214         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06> > FBase;
00215         
00216         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00217                             class Q06>
00218         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06)> func) 
00219             LOKI_FUNCTION_R2_CTOR_BODY
00220 
00221         LOKI_FUNCTION_BODY
00222     };
00223 
00224     template<class R,    class P01,class P02, class P03,class P04,class P05,
00225                         class P06,class P07>
00226     struct Function<R(P01,P02,P03,P04,P05,P06,P07)> 
00227         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07> >
00228     {
00229         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07> > FBase;
00230 
00231         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00232                             class Q06,class Q07>
00233         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07)> func) 
00234             LOKI_FUNCTION_R2_CTOR_BODY
00235 
00236         LOKI_FUNCTION_BODY
00237     };
00238 
00239     template<class R,    class P01,class P02, class P03,class P04,class P05,
00240                         class P06,class P07, class P08>
00241     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08)> 
00242         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08> >
00243     {
00244         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08> > FBase;
00245         
00246         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00247                             class Q06,class Q07, class Q08>
00248         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08)> func) 
00249             LOKI_FUNCTION_R2_CTOR_BODY
00250 
00251         LOKI_FUNCTION_BODY
00252     };
00253 
00254     template<class R,    class P01,class P02, class P03,class P04,class P05,
00255                         class P06,class P07, class P08,class P09>
00256     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09)> 
00257         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09> >
00258     {
00259         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09    > > FBase;
00260         
00261         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00262                             class Q06,class Q07, class Q08,class Q09>
00263         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09)> func) 
00264             LOKI_FUNCTION_R2_CTOR_BODY
00265 
00266         LOKI_FUNCTION_BODY
00267     };
00268 
00269     template<class R,    class P01,class P02, class P03,class P04,class P05,
00270                         class P06,class P07, class P08,class P09,class P10>
00271     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10)> 
00272         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10> >
00273     {
00274         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10> > FBase;
00275         
00276         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00277                             class Q06,class Q07, class Q08,class Q09,class Q10>
00278         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10)> func) 
00279             LOKI_FUNCTION_R2_CTOR_BODY
00280 
00281         LOKI_FUNCTION_BODY
00282     };
00283 
00284     template<class R,    class P01,class P02, class P03,class P04,class P05,
00285                         class P06,class P07, class P08,class P09,class P10,
00286                         class P11>
00287     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11)> 
00288             : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11> >
00289     {
00290         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11> >FBase;
00291         
00292         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00293                             class Q06,class Q07, class Q08,class Q09,class Q10,
00294                             class Q11>
00295         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11)> func) 
00296             LOKI_FUNCTION_R2_CTOR_BODY
00297 
00298         LOKI_FUNCTION_BODY
00299     };
00300 
00301     template<class R,    class P01,class P02, class P03,class P04,class P05,
00302                         class P06,class P07, class P08,class P09,class P10,
00303                         class P11,class P12>
00304     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12)> 
00305         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12> >
00306     {
00307         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12> > FBase;
00308         
00309         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00310                             class Q06,class Q07, class Q08,class Q09,class Q10,
00311                             class Q11,class Q12>
00312         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11)> func) 
00313             LOKI_FUNCTION_R2_CTOR_BODY
00314 
00315         LOKI_FUNCTION_BODY
00316     };
00317 
00318     template<class R,    class P01,class P02, class P03,class P04,class P05,
00319                         class P06,class P07, class P08,class P09,class P10,
00320                         class P11,class P12, class P13>
00321     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13)> 
00322         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13> >
00323     {
00324         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13> > FBase;
00325         
00326         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00327                             class Q06,class Q07, class Q08,class Q09,class Q10,
00328                             class Q11,class Q12, class Q13>
00329         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13)> func) 
00330             LOKI_FUNCTION_R2_CTOR_BODY
00331 
00332         LOKI_FUNCTION_BODY
00333     };
00334 
00335     template<class R,    class P01,class P02, class P03,class P04,class P05,
00336                         class P06,class P07, class P08,class P09,class P10,
00337                         class P11,class P12, class P13,class P14>
00338     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14)> 
00339         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14> >
00340     {
00341         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14> > FBase;
00342         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00343                             class Q06,class Q07, class Q08,class Q09,class Q10,
00344                             class Q11,class Q12, class Q13,class Q14>
00345         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13,Q14)> func) 
00346             LOKI_FUNCTION_R2_CTOR_BODY
00347 
00348         LOKI_FUNCTION_BODY
00349     };
00350 
00351     template<class R,    class P01,class P02, class P03,class P04,class P05,
00352                         class P06,class P07, class P08,class P09,class P10,
00353                         class P11,class P12, class P13,class P14,class P15>
00354     struct Function<R(P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15)> 
00355         : public Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15> >
00356     {
00357         typedef Functor<R, Seq<P01,P02,P03,P04,P05,P06,P07,P08,P09,P10,P11,P12,P13,P14,P15> > FBase;
00358 
00359         template<class R2,    class Q01,class Q02, class Q03,class Q04,class Q05,
00360                             class Q06,class Q07, class Q08,class Q09,class Q10,
00361                             class Q11,class Q12, class Q13,class Q14,class Q15>
00362         Function(Function<R2(Q01,Q02,Q03,Q04,Q05,Q06,Q07,Q08,Q09,Q10,Q11,Q12,Q13,Q14,Q15)> func) 
00363             LOKI_FUNCTION_R2_CTOR_BODY
00364 
00365         LOKI_FUNCTION_BODY
00366     };
00367 
00368 }// namespace Loki
00369 
00370 #endif // end file guardian
00371 

Generated on Sun Feb 25 16:52:22 2007 for Loki by  doxygen 1.5.1-p1