CommandDSFunctors.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
00074 CompS c = ds->get().lock();
00075 if (c) {
00076 CompT* ct = c.get();
00077 return fun( ct );
00078 } else {
00079 return false;
00080 }
00081 }
00082
00083 bool evaluate()
00084 {
00085
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
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
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