Orocos Real-Time Toolkit  2.5.0
Service.hpp
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  Service.hpp
00003 
00004                         Service.hpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
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_SERVICE_PROVIDER_HPP
00040 #define ORO_SERVICE_PROVIDER_HPP
00041 
00042 #include "rtt-config.h"
00043 #include "OperationInterface.hpp"
00044 #include "DataFlowInterface.hpp"
00045 #include "internal/OperationInterfacePartFused.hpp"
00046 #include "internal/LocalOperationCaller.hpp"
00047 #include "internal/OperationCallerC.hpp"
00048 #include "internal/UnMember.hpp"
00049 #include "internal/GetSignature.hpp"
00050 
00051 #include "ConfigurationInterface.hpp"
00052 #include "Operation.hpp"
00053 #ifdef ORO_REMOTING
00054 #include "internal/RemoteOperationCaller.hpp"
00055 #endif
00056 #include <boost/shared_ptr.hpp>
00057 #include <boost/static_assert.hpp>
00058 #include <boost/type_traits/function_traits.hpp>
00059 #include <boost/function_types/components.hpp>
00060 #include <boost/enable_shared_from_this.hpp>
00061 #if BOOST_VERSION >= 104000
00062 #include <boost/smart_ptr/enable_shared_from_this2.hpp>
00063 #endif
00064 
00065 namespace RTT
00066 {
00081     class RTT_API Service
00082         : public OperationInterface,
00083           public ConfigurationInterface,
00084           public DataFlowInterface,
00085 #if BOOST_VERSION >= 104000
00086           public boost::enable_shared_from_this2<Service>
00087 #else
00088           public boost::enable_shared_from_this<Service>
00089 #endif
00090     {
00091     public:
00092         typedef OperationInterface Factory;
00093         typedef boost::shared_ptr<Service> shared_ptr;
00094         typedef std::vector<std::string> ProviderNames;
00095 
00106         static Service::shared_ptr Create(const std::string& name, TaskContext* owner = 0);
00107 
00118         Service(const std::string& name, TaskContext* owner = 0);
00119 
00120         virtual ~Service();
00121 
00125         const std::string& getName() const { return mname; }
00126 
00130         const std::string& doc() const { return mdescription; }
00131 
00135         void doc(const std::string& description) { mdescription = description; }
00136 
00140         void setName(const std::string& name) { mname = name;}
00141 
00146         void setOwner(TaskContext* new_owner);
00147 
00152         void setParent(shared_ptr new_parent);
00153 
00157         shared_ptr getParent() const { return parent; }
00158 
00163         virtual ProviderNames getProviderNames() const;
00164 
00171         TaskContext* getOwner() const { return mowner; }
00172 
00181         virtual bool addService( shared_ptr obj );
00182 
00187         virtual void removeService( std::string const& service_name );
00188 
00193         Service::shared_ptr provides();
00194 
00200         Service::shared_ptr provides(const std::string& service_name);
00206         shared_ptr getService(const std::string& service_name);
00207 
00211         bool hasService(const std::string& service_name);
00212 
00216         void clear();
00217 
00222         std::vector<std::string> getOperationNames() const;
00223 
00228         bool hasOperation(const std::string& name) const;
00229 
00242         bool addLocalOperation( base::OperationBase& op );
00243 
00250         boost::shared_ptr<base::DisposableInterface> getLocalOperation( std::string name );
00251 
00262         OperationInterfacePart* getOperation( std::string name );
00263 
00267         void removeOperation( const std::string& name );
00268 
00285         bool setOperationThread(std::string const& name, ExecutionThread et);
00286 
00296         template<class Signature>
00297         Operation<Signature>& addOperation( Operation<Signature>& op )
00298         {
00299             if ( this->addLocalOperation( op ) == false )
00300                 return op;
00301             this->add( op.getName(), new internal::OperationInterfacePartFused<Signature>( &op ) );
00302             return op;
00303         }
00304 
00313         template<class Signature>
00314         Operation<Signature>& addSynchronousOperation( Operation<Signature>& op )
00315         {
00316             if ( this->addLocalOperation( op ) == false )
00317                 return op;
00318             this->add( op.getName(), new internal::SynchronousOperationInterfacePartFused<Signature>( &op ) );
00319             return op;
00320         }
00321 
00335         template<class Func, class Service>
00336         Operation< typename internal::GetSignature<Func>::Signature >&
00337         addOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
00338         {
00339             typedef typename internal::GetSignature<Func>::Signature Signature;
00340             Operation<Signature>* op = new Operation<Signature>(name, func, serv, et);
00341             ownedoperations.push_back(op);
00342             return addOperation( *op );
00343         }
00344 
00356         template<class Func>
00357         Operation< Func >&
00358         addOperation( const std::string name, Func* func, ExecutionThread et = ClientThread )
00359         {
00360             typedef Func Signature;
00361             boost::function<Signature> bfunc = func;
00362             Operation<Signature>* op = new Operation<Signature>(name, bfunc, et);
00363             ownedoperations.push_back(op);
00364             return addOperation( *op );
00365         }
00366 
00379         template<class Func, class Service>
00380         Operation< typename internal::GetSignature<Func>::Signature >&
00381         addSynchronousOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
00382         {
00383             typedef typename internal::GetSignature<Func>::Signature Signature;
00384             Operation<Signature>* op = new Operation<Signature>(name, func, serv, et);
00385             ownedoperations.push_back(op);
00386             return addSynchronousOperation( *op );
00387         }
00388 
00394         template<class Func,class ObjT>
00395         Operation< typename internal::GetSignatureDS<Func>::Signature>& addOperationDS( const std::string& name, Func func, internal::DataSource< boost::shared_ptr<ObjT> >* sp,
00396                 ExecutionThread et = ClientThread)
00397         {
00398             typedef typename internal::GetSignatureDS<Func>::Signature SignatureDS;    // function signature with normal object pointer
00399             Operation<SignatureDS>* op = new Operation<SignatureDS>(name, boost::function<SignatureDS>(func), et);
00400             ownedoperations.push_back(op);
00401             return addOperationDS( sp, *op );
00402         }
00403 
00409         template<class Signature,class ObjT>
00410         Operation<Signature>& addOperationDS( internal::DataSource< boost::shared_ptr<ObjT> >* sp, Operation<Signature>& op)
00411         {
00412             if ( this->addLocalOperation( op ) == false ) {
00413                 assert(false);
00414                 return op; // should never be reached.
00415             }
00416             this->add( op.getName(), new internal::OperationInterfacePartFusedDS<Signature,ObjT>( sp, &op) );
00417             return op;
00418         }
00419 
00429         base::DataSourceBase::shared_ptr getOperation( std::string name,
00430                                    const std::vector<base::DataSourceBase::shared_ptr>& args, ExecutionEngine* caller) const
00431         {
00432             return this->produce(name, args, caller);
00433         }
00434 
00443         internal::OperationCallerC create(std::string name, ExecutionEngine* caller);
00444 
00448         bool resetOperation(std::string name, base::OperationBase* impl);
00449     protected:
00450         typedef std::map< std::string, shared_ptr > Services;
00452         Services services;
00453 
00454         bool testOperation(base::OperationBase& op);
00455         typedef std::map<std::string,base::OperationBase* > SimpleOperations;
00456         typedef std::vector<base::OperationBase*> OperationList;
00457         SimpleOperations simpleoperations;
00458         OperationList ownedoperations;
00459         std::string mname;
00460         std::string mdescription;
00461         TaskContext* mowner;
00462         shared_ptr parent;
00463     };
00464 }
00465 
00466 
00467 #endif