Invoker.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:05:58 CET 2006  Invoker.hpp
00003 
00004                         Invoker.hpp -  description
00005                            -------------------
00006     begin                : do november 02 2006
00007     copyright            : (C) 2006 FMTC
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
00035  *                                                                         *
00036  ***************************************************************************/
00037 
00038 
00039 #ifndef ORO_INVOKER_BASE_HPP
00040 #define ORO_INVOKER_BASE_HPP
00041 
00042 #include <boost/function.hpp>
00043 #include "boost/function_types/function_type.hpp"
00044 #include "boost/function_types/function_type_arity.hpp"
00045 #include "NA.hpp"
00046 
00047 namespace RTT
00048 {
00049     namespace detail
00050     {
00055         template<int, class F>
00056         struct InvokerBaseImpl;
00057 
00058         template<class F>
00059         struct InvokerBaseImpl<0,F>
00060         {
00061             typedef typename boost::function_traits<F>::result_type result_type;
00062             virtual ~InvokerBaseImpl() {}
00063             virtual result_type operator()() = 0;
00064         };
00065 
00066         template<class F>
00067         struct InvokerBaseImpl<1,F>
00068         {
00069             typedef typename boost::function_traits<F>::result_type result_type;
00070             typedef typename boost::function<F>::arg1_type arg1_type;
00071             virtual ~InvokerBaseImpl() {}
00072             virtual result_type operator()(arg1_type a1) = 0;
00073         };
00074 
00075         template<class F>
00076         struct InvokerBaseImpl<2,F>
00077         {
00078             typedef typename boost::function_traits<F>::result_type result_type;
00079             typedef typename boost::function<F>::arg1_type arg1_type;
00080             typedef typename boost::function<F>::arg2_type arg2_type;
00081             virtual ~InvokerBaseImpl() {}
00082             virtual result_type operator()(arg1_type a1, arg2_type a2) = 0;
00083         };
00084 
00085         template<class F>
00086         struct InvokerBaseImpl<3,F>
00087         {
00088             typedef typename boost::function_traits<F>::result_type result_type;
00089             typedef typename boost::function<F>::arg1_type arg1_type;
00090             typedef typename boost::function<F>::arg2_type arg2_type;
00091             typedef typename boost::function<F>::arg3_type arg3_type;
00092             virtual ~InvokerBaseImpl() {}
00093             virtual result_type operator()(arg1_type a1, arg2_type a2, arg3_type a3) = 0;
00094         };
00095 
00096         template<class F>
00097         struct InvokerBaseImpl<4,F>
00098         {
00099             typedef typename boost::function_traits<F>::result_type result_type;
00100             typedef typename boost::function<F>::arg1_type arg1_type;
00101             typedef typename boost::function<F>::arg2_type arg2_type;
00102             typedef typename boost::function<F>::arg3_type arg3_type;
00103             typedef typename boost::function<F>::arg4_type arg4_type;
00104             virtual ~InvokerBaseImpl() {}
00105             virtual result_type operator()(arg1_type a1, arg2_type a2, arg3_type a3, arg4_type a4) = 0;
00106         };
00107 
00108         template<class F>
00109         struct InvokerBase
00110             : public InvokerBaseImpl<boost::function_traits<F>::arity, F>
00111         {};
00112 
00113         template<int, class F, class BaseImpl>
00114         struct InvokerImpl;
00115 
00116         template<class F, class BaseImpl>
00117         struct InvokerImpl<0,F,BaseImpl>
00118             : public BaseImpl
00119         {
00120             typedef typename boost::function_traits<F>::result_type result_type;
00124             result_type operator()()
00125             {
00126                 return BaseImpl::invoke();
00127             }
00128         };
00129 
00130         template<class F, class BaseImpl>
00131         struct InvokerImpl<1,F,BaseImpl>
00132             : public BaseImpl
00133         {
00134             typedef typename boost::function_traits<F>::result_type result_type;
00135             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00139             result_type operator()(arg1_type a1)
00140             {
00141                 return BaseImpl::template invoke<arg1_type>( a1 );
00142             }
00143         };
00144 
00145         template<class F, class BaseImpl>
00146         struct InvokerImpl<2,F,BaseImpl>
00147             : public BaseImpl
00148         {
00149             typedef typename boost::function_traits<F>::result_type result_type;
00150             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00151             typedef typename boost::function_traits<F>::arg2_type arg2_type;
00152 
00156             result_type operator()(arg1_type t1, arg2_type t2)
00157             {
00158                 return BaseImpl::template invoke<arg1_type, arg2_type>(t1, t2);
00159             }
00160 
00161         };
00162 
00163         template<class F, class BaseImpl>
00164         struct InvokerImpl<3,F,BaseImpl>
00165             : public BaseImpl
00166         {
00167             typedef typename boost::function_traits<F>::result_type result_type;
00168             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00169             typedef typename boost::function_traits<F>::arg2_type arg2_type;
00170             typedef typename boost::function_traits<F>::arg3_type arg3_type;
00171 
00175             result_type operator()(arg1_type t1, arg2_type t2, arg3_type t3)
00176             {
00177                 return BaseImpl::template invoke<arg1_type, arg2_type, arg3_type>(t1, t2, t3);
00178             }
00179 
00180         };
00181 
00182         template<class F, class BaseImpl>
00183         struct InvokerImpl<4,F,BaseImpl>
00184             : public BaseImpl
00185         {
00186             typedef typename boost::function_traits<F>::result_type result_type;
00187             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00188             typedef typename boost::function_traits<F>::arg2_type arg2_type;
00189             typedef typename boost::function_traits<F>::arg3_type arg3_type;
00190             typedef typename boost::function_traits<F>::arg4_type arg4_type;
00191 
00195             result_type operator()(arg1_type t1, arg2_type t2, arg3_type t3, arg4_type t4)
00196             {
00197                 return BaseImpl::template invoke<arg1_type, arg2_type, arg3_type, arg4_type>(t1, t2, t3, t4);
00198             }
00199 
00200         };
00201 
00207         template<class F, class BaseImpl>
00208         struct Invoker
00209             : public InvokerImpl<boost::function_traits<F>::arity, F, BaseImpl>
00210         {};
00211 
00212 
00219         template<int, class Signature, class ToInvoke>
00220         struct InvokerSignature;
00221 
00222         template<class F, class ToInvoke>
00223         struct InvokerSignature<0,F,ToInvoke>
00224         {
00225             typedef typename boost::function_traits<F>::result_type result_type;
00226 
00227             InvokerSignature() : impl() {}
00228             InvokerSignature(ToInvoke implementation) : impl(implementation) {}
00229             ~InvokerSignature() {}
00230 
00234             result_type operator()()
00235             {
00236                 if (impl)
00237                     return (*impl)();
00238                 return NA<result_type>::na();
00239             }
00240         protected:
00241             ToInvoke impl;
00242         };
00243 
00244         template<class F, class ToInvoke>
00245         struct InvokerSignature<1,F,ToInvoke>
00246         {
00247             typedef typename boost::function_traits<F>::result_type result_type;
00248             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00249 
00250             InvokerSignature() : impl() {}
00251             InvokerSignature(ToInvoke implementation) : impl(implementation) {}
00252             ~InvokerSignature() {}
00253 
00257             result_type operator()(arg1_type a1)
00258             {
00259                 if (impl)
00260                     return (*impl)( a1 );
00261                 return NA<result_type>::na();
00262             }
00263         protected:
00264             ToInvoke impl;
00265         };
00266 
00267         template<class F, class ToInvoke>
00268         struct InvokerSignature<2,F,ToInvoke>
00269         {
00270             typedef typename boost::function_traits<F>::result_type result_type;
00271             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00272             typedef typename boost::function_traits<F>::arg2_type arg2_type;
00273 
00274             InvokerSignature() : impl() {}
00275             InvokerSignature(ToInvoke implementation) : impl(implementation) {}
00276             ~InvokerSignature() {}
00277 
00281             result_type operator()(arg1_type t1, arg2_type t2)
00282             {
00283                 if (impl)
00284                     return (*impl)(t1, t2);
00285                 return NA<result_type>::na();
00286             }
00287 
00288         protected:
00289             ToInvoke impl;
00290         };
00291 
00292         template<class F, class ToInvoke>
00293         struct InvokerSignature<3,F,ToInvoke>
00294         {
00295             typedef typename boost::function_traits<F>::result_type result_type;
00296             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00297             typedef typename boost::function_traits<F>::arg2_type arg2_type;
00298             typedef typename boost::function_traits<F>::arg3_type arg3_type;
00299 
00300             InvokerSignature() : impl() {}
00301             InvokerSignature(ToInvoke implementation) : impl(implementation) {}
00302             ~InvokerSignature() { }
00303 
00307             result_type operator()(arg1_type t1, arg2_type t2, arg3_type t3)
00308             {
00309                 if (impl)
00310                     return (*impl)(t1, t2, t3);
00311                 return NA<result_type>::na();
00312             }
00313 
00314         protected:
00315             ToInvoke impl;
00316         };
00317 
00318         template<class F, class ToInvoke>
00319         struct InvokerSignature<4,F,ToInvoke>
00320         {
00321             typedef typename boost::function_traits<F>::result_type result_type;
00322             typedef typename boost::function_traits<F>::arg1_type arg1_type;
00323             typedef typename boost::function_traits<F>::arg2_type arg2_type;
00324             typedef typename boost::function_traits<F>::arg3_type arg3_type;
00325             typedef typename boost::function_traits<F>::arg4_type arg4_type;
00326 
00327             InvokerSignature() : impl() {}
00328             InvokerSignature(ToInvoke implementation) : impl(implementation) {}
00329             ~InvokerSignature() { }
00330 
00334             result_type operator()(arg1_type t1, arg2_type t2, arg3_type t3, arg4_type t4)
00335             {
00336                 if (impl)
00337                     return (*impl)(t1, t2, t3, t4);
00338                 return NA<result_type>::na();
00339             }
00340 
00341         protected:
00342             ToInvoke impl;
00343         };
00344 
00345     }
00346 }
00347 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3