CommandDSFunctors.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:10 CET 2006  CommandDSFunctors.hpp
00003 
00004                         CommandDSFunctors.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_COMMAND_DS_FUNCTORS_HPP
00040 #define ORO_COMMAND_DS_FUNCTORS_HPP
00041 
00042 #include <boost/type_traits.hpp>
00043 #include <boost/shared_ptr.hpp>
00044 #include <boost/weak_ptr.hpp>
00045 #include <boost/function.hpp>
00046 #include "DataSource.hpp"
00047 
00048 namespace RTT
00049 {
00050     namespace detail {
00051 
00052         template<typename FunctionT>
00053         class FunctorDS0
00054         {
00055         public:
00056             typedef boost::function<FunctionT> FunctionImpl;
00057             typedef FunctionT Function;
00058             typedef typename boost::remove_pointer<typename FunctionImpl::arg1_type>::type CompT;
00059             FunctionImpl fun;
00060             typename DataSource<boost::weak_ptr<CompT> >::shared_ptr ds;
00061             typedef boost::weak_ptr<CompT> CompW;
00062             typedef boost::shared_ptr<CompT> CompS;
00063 
00064             FunctorDS0( DataSource<CompW>* c, FunctionImpl f )
00065                 : fun( f ), ds(c)
00066             {}
00067 
00068             void setArguments( DataSourceBase* = 0, DataSourceBase* = 0, DataSourceBase* = 0, DataSourceBase* = 0  ) {}
00069             void readArguments(){}
00070 
00071             bool execute()
00072             {
00073                 // the Component pointer is stored in a DataSource
00074                 CompS c = ds->get().lock();
00075                 if (c) {
00076                     CompT* ct = c.get();
00077                     return fun( ct );
00078                 } else {
00079                     return false; // destroyed.
00080                 }
00081             }
00082 
00083             bool evaluate()
00084             {
00085                 // logical XOR :
00086                 boost::shared_ptr<CompT> c = ds->get().lock();
00087                 if (c){
00088                     CompT* ct = c.get();
00089                     return fun( ct );
00090                 } else
00091                     return false;
00092             }
00093 
00094             FunctorDS0<FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00095             {
00096                 return FunctorDS0( ds->copy(alreadyCloned), fun );
00097             }
00098         };
00099 
00104         template<typename FunctionT>
00105         class FunctorDS1
00106         {
00107         public:
00108             typedef FunctionT Function;
00109             typedef boost::function<FunctionT> FunctionImpl;
00110             // arg1_type is the component type.
00111             typedef typename boost::remove_pointer<typename FunctionImpl::arg1_type>::type CompT;
00112             typedef typename FunctionImpl::arg2_type Arg2T;
00113             FunctionImpl fun;
00114             typename DataSource<Arg2T>::shared_ptr aa;
00115             typename DataSource<boost::weak_ptr<CompT> >::shared_ptr ds;
00116 
00117             FunctorDS1( DataSource<boost::weak_ptr<CompT> >* c, FunctionImpl f, DataSource<Arg2T>* a = 0)
00118                 : fun( f ), aa( a ), ds(c)
00119             {
00120             }
00121 
00122             void setArguments( DataSource<Arg2T>* a, DataSourceBase* = 0, DataSourceBase* = 0, DataSourceBase* = 0  )
00123             {
00124                 aa = a;
00125             }
00126 
00127             void readArguments()
00128             {
00129                 aa->evaluate();
00130             }
00131 
00132             bool execute()
00133             {
00134                 boost::shared_ptr<CompT> c =  ds->get().lock();
00135                 if (c){
00136                     CompT* ct = c.get();
00137                     Arg2T a = aa->value();
00138                     bool r= fun( ct, a );
00139                     return r;
00140                 } else
00141                     return false;
00142             }
00143 
00144             bool evaluate()
00145             {
00146                 Arg2T a =  aa->value();
00147                 boost::shared_ptr<CompT> c = ds->get().lock();
00148                 // logical XOR :
00149                 if (c) {
00150                     CompT* ct = c.get();
00151                     bool r= fun( ct, a );
00152                     aa->updated();
00153                     return r;
00154                 } else
00155                     return false;
00156             }
00157 
00158             FunctorDS1<FunctionT> copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00159             {
00160                 return FunctorDS1( ds->copy(alreadyCloned), fun, aa->copy( alreadyCloned ) );
00161             }
00162         };
00163 
00164     }
00165 }
00166 
00167 #endif
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3