Method.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_TASK_METHOD_HPP
00040 #define ORO_TASK_METHOD_HPP
00041
00042 #include <boost/function.hpp>
00043 #include <string>
00044 #include "UnMember.hpp"
00045 #include "MethodBase.hpp"
00046 #include "LocalMethod.hpp"
00047 #include "ActionInterface.hpp"
00048 #include "Logger.hpp"
00049
00050 namespace RTT
00051 {
00070 template<class FunctionT>
00071 class Method
00072 : public detail::InvokerSignature<boost::function_traits<FunctionT>::arity,
00073 FunctionT,
00074 boost::shared_ptr< detail::MethodBase<FunctionT> > >
00075 {
00076 std::string mname;
00077 typedef detail::InvokerSignature<boost::function_traits<FunctionT>::arity,
00078 FunctionT,
00079 boost::shared_ptr< detail::MethodBase<FunctionT> > > Base;
00080 public:
00081 typedef FunctionT Signature;
00082 typedef typename boost::function_traits<Signature>::result_type result_type;
00083 typedef boost::function_traits<Signature> traits;
00084 typedef boost::shared_ptr< detail::MethodBase<FunctionT> > MethodBasePtr;
00085
00091 Method()
00092 : Base(), mname()
00093 {}
00094
00100 Method(std::string name)
00101 : Base(), mname(name)
00102 {}
00103
00109 Method(const Method& m)
00110 : Base(m.impl),
00111 mname(m.mname)
00112 {}
00113
00121 Method& operator=(const Method& m)
00122 {
00123 if ( this == &m )
00124 return *this;
00125 mname = m.mname;
00126 this->impl = m.impl;
00127 return *this;
00128 }
00129
00136 Method(boost::shared_ptr<ActionInterface> implementation)
00137 : Base( boost::dynamic_pointer_cast< detail::MethodBase<Signature> >(implementation) ),
00138 mname()
00139 {
00140 if ( !this->impl && implementation ) {
00141 log(Error) << "Tried to construct Method from incompatible type."<< endlog();
00142 }
00143 }
00144
00153 Method& operator=(boost::shared_ptr<ActionInterface> implementation)
00154 {
00155 if (this->impl && this->impl == implementation)
00156 return *this;
00157 this->impl = boost::dynamic_pointer_cast< detail::MethodBase<Signature> >(implementation);
00158 if ( !this->impl && implementation ) {
00159 log(Error) << "Tried to assign Method '"<<mname<<"' from incompatible type."<< endlog();
00160 }
00161 return *this;
00162 }
00163
00172 template<class M, class ObjectType>
00173 Method(std::string name, M meth, ObjectType object)
00174 : Base( MethodBasePtr(new detail::LocalMethod<Signature>(meth, object) ) ),
00175 mname(name)
00176 {}
00177
00184 template<class M>
00185 Method(std::string name, M meth)
00186 : Base( MethodBasePtr(new detail::LocalMethod<Signature>(meth) ) ),
00187 mname(name)
00188 {}
00189
00193 ~Method()
00194 {
00195 }
00196
00202 bool ready() const {
00203 return this->impl;
00204 }
00205
00206
00210 const std::string& getName() const {return mname;}
00211
00215 const MethodBasePtr getMethodImpl() const {
00216 return this->impl;
00217 }
00218
00222 void setMethodImpl( MethodBasePtr new_impl) const {
00223 this->impl = new_impl;
00224 }
00225
00235 template<class M, class ObjectType>
00236 void setMethod(M meth, ObjectType object)
00237 {
00238 this->impl = MethodBasePtr(new detail::LocalMethod<Signature>(meth, object) );
00239 }
00240
00250 template<class M>
00251 void setMethod(M meth)
00252 {
00253 this->impl = MethodBasePtr(new detail::LocalMethod<Signature>(meth) );
00254 }
00255 };
00256
00264 template<class F, class O>
00265 Method< typename detail::UnMember<F>::type > method(std::string name, F method, O object) {
00266 return Method< typename detail::UnMember<F>::type >(name, method, object);
00267 }
00268
00275 template<class F>
00276 Method<F> method(std::string name, F method) {
00277 return Method<F>(name, method);
00278 }
00285 template<class F>
00286 Method< typename detail::ArgMember<F>::type > method_ds(std::string name, F method) {
00287 return Method< typename detail::ArgMember<F>::type >(name, method);
00288 }
00289 }
00290
00291 #endif