Method.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:07 CET 2006  Method.hpp
00003 
00004                         Method.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_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
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3