CommandFunctors.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:11 CET 2006  CommandFunctors.hpp
00003 
00004                         CommandFunctors.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_COMMANDFUNCTORS_HPP
00040 #define ORO_COMMANDFUNCTORS_HPP
00041 
00042 #include <boost/bind.hpp>
00043 #include <boost/function.hpp>
00044 #include <boost/type_traits/function_traits.hpp>
00045 
00046 #include "CommandInterface.hpp"
00047 #include "ConditionInterface.hpp"
00048 #include "DataSource.hpp"
00049 #include "DataSource.hpp"
00050 
00051 namespace RTT
00052 {
00053 
00054 
00055 
00056     namespace detail {
00057 
00058         template<int, class F>
00059         class FunctorImpl;
00060 
00065         template<typename FunctionT>
00066         class FunctorImpl<0, FunctionT>
00067         {
00068         public:
00069             FunctionT fun;
00070 
00071             FunctorImpl( FunctionT f)
00072                 : fun( f )
00073             {
00074             }
00075 
00076             void setArguments( DataSourceBase* = 0, DataSourceBase* = 0, DataSourceBase* = 0, DataSourceBase* = 0  )
00077             {
00078             }
00079 
00080             void readArguments()
00081             {
00082             }
00083 
00084             bool execute()
00085             {
00086                 return fun();
00087             }
00088 
00089             bool evaluate()
00090             {
00091                 return fun();
00092             }
00093 
00094             FunctorImpl<0,FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00095             {
00096                 return FunctorImpl( fun );
00097             }
00098         };
00099 
00104         template<typename FunctionT>
00105         class FunctorImpl<1, FunctionT>
00106         {
00107         public:
00108             typedef typename FunctionT::arg1_type Arg1T;
00109             FunctionT fun;
00110             typename DataSource<Arg1T>::shared_ptr aa;
00111 
00112             FunctorImpl( FunctionT f, DataSource<Arg1T>* a = 0 )
00113                 : fun( f ), aa(a)
00114             {
00115             }
00116 
00117             void setArguments( DataSource<Arg1T>* a, DataSourceBase* = 0, DataSourceBase* = 0, DataSourceBase* = 0  )
00118             {
00119                 aa = a;
00120             }
00121 
00122             void readArguments()
00123             {
00124                 aa->evaluate();
00125             }
00126 
00127             bool execute()
00128             {
00129                 Arg1T a = aa->value();
00130                 return fun( a );
00131             }
00132 
00133             bool evaluate()
00134             {
00135                 Arg1T a = aa->value();
00136                 bool r = fun( a );
00137                 aa->updated();
00138                 return r;
00139             }
00140 
00141             FunctorImpl<1,FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00142             {
00143                 return FunctorImpl( fun, aa->copy( alreadyCloned ) );
00144             }
00145         };
00146 
00147         template<typename FunctionT>
00148         class FunctorImpl<2, FunctionT>
00149         {
00150         public:
00151             typedef typename FunctionT::arg1_type Arg1T;
00152             typedef typename FunctionT::arg2_type Arg2T;
00153             FunctionT fun;
00154             typename DataSource<Arg1T>::shared_ptr aa;
00155             typename DataSource<Arg2T>::shared_ptr bb;
00156 
00157             FunctorImpl( FunctionT f, DataSource<Arg1T>* a = 0, DataSource<Arg2T>* b = 0  )
00158                 : fun( f ), aa(a), bb(b)
00159             {
00160             }
00161 
00162             void setArguments( DataSource<Arg1T>* a, DataSource<Arg2T>* b, DataSourceBase* = 0, DataSourceBase* = 0  )
00163             {
00164                 aa = a;
00165                 bb = b;
00166             }
00167 
00168             void readArguments()
00169             {
00170                 aa->evaluate();
00171                 bb->evaluate();
00172             }
00173 
00174             bool execute()
00175             {
00176                 Arg1T a = aa->value();
00177                 Arg2T b = bb->value();
00178                 return fun( a, b );
00179             }
00180 
00181             bool evaluate()
00182             {
00183                 Arg1T a = aa->value();
00184                 Arg2T b = bb->value();
00185                 bool r = fun( a, b);
00186                 aa->updated();
00187                 bb->updated();
00188                 return r;
00189             }
00190 
00191             FunctorImpl<2,FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00192             {
00193                 return FunctorImpl( fun, aa->copy( alreadyCloned ), bb->copy(alreadyCloned));
00194             }
00195         };
00196 
00197 
00198         template<typename FunctionT>
00199         class FunctorImpl<3, FunctionT>
00200         {
00201         public:
00202             typedef typename FunctionT::arg1_type Arg1T;
00203             typedef typename FunctionT::arg2_type Arg2T;
00204             typedef typename FunctionT::arg3_type Arg3T;
00205             FunctionT fun;
00206             typename DataSource<Arg1T>::shared_ptr aa;
00207             typename DataSource<Arg2T>::shared_ptr bb;
00208             typename DataSource<Arg3T>::shared_ptr cc;
00209 
00210             FunctorImpl( FunctionT f, DataSource<Arg1T>* a = 0, DataSource<Arg2T>* b = 0, DataSource<Arg3T>* c = 0)
00211                 : fun( f ), aa(a), bb(b), cc(c)
00212             {
00213             }
00214 
00215             void setArguments( DataSource<Arg1T>* a, DataSource<Arg2T>* b, DataSource<Arg3T>* c, DataSourceBase* = 0  )
00216             {
00217                 aa = a;
00218                 bb = b;
00219                 cc = c;
00220             }
00221 
00222             void readArguments()
00223             {
00224                 aa->evaluate();
00225                 bb->evaluate();
00226                 cc->evaluate();
00227             }
00228 
00229             bool execute()
00230             {
00231                 Arg1T a = aa->value();
00232                 Arg2T b = bb->value();
00233                 Arg3T c = cc->value();
00234                 return fun( a, b, c);
00235             }
00236 
00237             bool evaluate()
00238             {
00239                 Arg1T a = aa->value();
00240                 Arg2T b = bb->value();
00241                 Arg3T c = cc->value();
00242                 bool r = fun( a, b, c);
00243                 aa->updated();
00244                 bb->updated();
00245                 cc->updated();
00246                 return r;
00247             }
00248 
00249             FunctorImpl<3,FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00250             {
00251                 return FunctorImpl( fun, aa->copy( alreadyCloned ), bb->copy(alreadyCloned), cc->copy(alreadyCloned) );
00252             }
00253         };
00254 
00255 
00256         template<typename FunctionT>
00257         class FunctorImpl<4, FunctionT>
00258         {
00259         public:
00260             typedef typename FunctionT::arg1_type Arg1T;
00261             typedef typename FunctionT::arg2_type Arg2T;
00262             typedef typename FunctionT::arg3_type Arg3T;
00263             typedef typename FunctionT::arg4_type Arg4T;
00264             FunctionT fun;
00265             typename DataSource<Arg1T>::shared_ptr aa;
00266             typename DataSource<Arg2T>::shared_ptr bb;
00267             typename DataSource<Arg3T>::shared_ptr cc;
00268             typename DataSource<Arg4T>::shared_ptr dd;
00269 
00270             FunctorImpl( FunctionT f, DataSource<Arg1T>* a = 0, DataSource<Arg2T>* b = 0, DataSource<Arg3T>* c = 0, DataSource<Arg4T>* d = 0  )
00271                 : fun( f ), aa(a), bb(b), cc(c), dd(d)
00272             {
00273             }
00274 
00275             void setArguments( DataSource<Arg1T>* a, DataSource<Arg2T>* b, DataSource<Arg3T>* c, DataSource<Arg4T>* d  )
00276             {
00277                 aa = a;
00278                 bb = b;
00279                 cc = c;
00280                 dd = d;
00281             }
00282 
00283             void readArguments()
00284             {
00285                 aa->evaluate();
00286                 bb->evaluate();
00287                 cc->evaluate();
00288                 dd->evaluate();
00289             }
00290 
00291             bool execute()
00292             {
00293                 Arg1T a = aa->value();
00294                 Arg2T b = bb->value();
00295                 Arg3T c = cc->value();
00296                 Arg4T d = dd->value();
00297                 return fun( a, b, c, d );
00298             }
00299 
00300             bool evaluate()
00301             {
00302                 Arg1T a = aa->value();
00303                 Arg2T b = bb->value();
00304                 Arg3T c = cc->value();
00305                 Arg4T d = dd->value();
00306                 bool r = fun( a, b, c, d );
00307                 aa->updated();
00308                 bb->updated();
00309                 cc->updated();
00310                 dd->updated();
00311                 return r;
00312             }
00313 
00314             FunctorImpl<4,FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00315             {
00316                 return FunctorImpl( fun, aa->copy( alreadyCloned ), bb->copy(alreadyCloned), cc->copy(alreadyCloned), dd->copy(alreadyCloned) );
00317             }
00318         };
00319 
00328         template< class FunctionT>
00329         struct Functor
00330             : public FunctorImpl<boost::function_traits<FunctionT>::arity,boost::function<FunctionT> >
00331         {
00332             typedef FunctionT Function;
00333             typedef boost::function<FunctionT> FunctionImpl;
00334 
00335             Functor<FunctionT>(FunctionImpl impl)
00336                 : FunctorImpl<boost::function_traits<FunctionT>::arity,FunctionImpl>(impl)
00337             {}
00338 
00339             // Allow construction from base class.
00340             Functor<FunctionT>(FunctorImpl<boost::function_traits<FunctionT>::arity,FunctionImpl> impl)
00341                 : FunctorImpl<boost::function_traits<FunctionT>::arity,FunctionImpl>(impl)
00342             {}
00343         };
00344 
00345 
00351         template<typename SignatureT, typename FunctorT = Functor<SignatureT> >
00352         class CommandFunctor
00353             :public CommandInterface
00354         {
00355         public:
00356             typedef typename FunctorT::FunctionImpl Function;
00357             typedef SignatureT Signature;
00358             typedef FunctorT Functor;
00359 
00360             FunctorT com;
00361 
00365             CommandFunctor(Function impl)
00366                 : com(impl)
00367             {
00368             }
00369 
00370             CommandFunctor(FunctorT impl)
00371                 : com(impl)
00372             {
00373             }
00374 
00375             virtual void readArguments() { com.readArguments(); }
00376 
00377             virtual bool execute() { return com.execute(); }
00378 
00379             virtual CommandFunctor<Signature,FunctorT>* clone() const
00380             {
00381                 return new CommandFunctor( com );
00382             }
00383 
00384             virtual CommandFunctor<Signature,FunctorT>* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00385             {
00386                 return new CommandFunctor( FunctorT(com.copy(alreadyCloned)) );
00387             }
00388         };
00389 
00394         class CommandFunction
00395             :public CommandInterface
00396         {
00397         public:
00398             typedef boost::function<bool(void)> Function;
00399 
00400             Function com;
00401 
00405             CommandFunction(Function impl)
00406                 : com(impl)
00407             {
00408             }
00409 
00410             virtual void readArguments() { }
00411 
00412             virtual bool execute() { return com(); }
00413 
00414             virtual CommandFunction* clone() const
00415             {
00416                 return new CommandFunction( com );
00417             }
00418         };
00419 
00420 
00426         template<typename SignatureT, typename FunctorT = Functor<SignatureT> >
00427         class ConditionFunctor
00428             :public ConditionInterface
00429         {
00430         public:
00431             typedef typename FunctorT::FunctionImpl Function;
00432             typedef SignatureT Signature;
00433             typedef FunctorT Functor;
00434 
00435             FunctorT con;
00436             bool minvert;
00437 
00441             ConditionFunctor(Function impl, bool invert=false)
00442                 : con(impl), minvert(invert)
00443             {
00444             }
00445 
00446             ConditionFunctor(FunctorT impl, bool invert=false)
00447                 : con(impl), minvert(invert)
00448             {
00449             }
00450 
00451             virtual bool evaluate() { return con.evaluate() != minvert; }
00452 
00453             virtual ConditionFunctor<Signature,FunctorT>* clone() const
00454             {
00455                 return new ConditionFunctor( con, minvert );
00456             }
00457 
00458             virtual ConditionFunctor<Signature,FunctorT>* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00459             {
00460                 return new ConditionFunctor( FunctorT(con.copy(alreadyCloned)), minvert );
00461             }
00462         };
00463 
00468         class ConditionFunction
00469             :public ConditionInterface
00470         {
00471         public:
00472             typedef boost::function<bool(void)> Function;
00473 
00474             Function con;
00475             bool minvert;
00476 
00480             ConditionFunction(Function impl, bool invert=false)
00481                 : con(impl), minvert(invert)
00482             {
00483             }
00484 
00485             virtual bool evaluate() { return con() != minvert; }
00486 
00487             virtual ConditionFunction* clone() const
00488             {
00489                 return new ConditionFunction( con, minvert );
00490             }
00491         };
00492     }
00493 }
00494 
00495 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3