Orocos Real-Time Toolkit  2.6.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 
00177         ExecutionEngine* getOwnerExecutionEngine() const;
00178 
00187         virtual bool addService( shared_ptr obj );
00188 
00193         virtual void removeService( std::string const& service_name );
00194 
00199         Service::shared_ptr provides();
00200 
00206         Service::shared_ptr provides(const std::string& service_name);
00212         shared_ptr getService(const std::string& service_name);
00213 
00217         bool hasService(const std::string& service_name);
00218 
00222         void clear();
00223 
00228         std::vector<std::string> getOperationNames() const;
00229 
00234         bool hasOperation(const std::string& name) const;
00235 
00248         bool addLocalOperation( base::OperationBase& op );
00249 
00256         boost::shared_ptr<base::DisposableInterface> getLocalOperation( std::string name );
00257 
00268         OperationInterfacePart* getOperation( std::string name );
00269 
00273         void removeOperation( const std::string& name );
00274 
00291         bool setOperationThread(std::string const& name, ExecutionThread et);
00292 
00302         template<class Signature>
00303         Operation<Signature>& addOperation( Operation<Signature>& op )
00304         {
00305             if ( this->addLocalOperation( op ) == false )
00306                 return op;
00307             this->add( op.getName(), new internal::OperationInterfacePartFused<Signature>( &op ) );
00308             return op;
00309         }
00310 
00319         template<class Signature>
00320         Operation<Signature>& addSynchronousOperation( Operation<Signature>& op )
00321         {
00322             if ( this->addLocalOperation( op ) == false )
00323                 return op;
00324             this->add( op.getName(), new internal::SynchronousOperationInterfacePartFused<Signature>( &op ) );
00325             return op;
00326         }
00327 
00341         template<class Func, class Service>
00342         Operation< typename internal::GetSignature<Func>::Signature >&
00343         addOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
00344         {
00345             typedef typename internal::GetSignature<Func>::Signature Signature;
00346             Operation<Signature>* op = new Operation<Signature>(name, func, serv, et, this->getOwnerExecutionEngine() );
00347             ownedoperations.push_back(op);
00348             return addOperation( *op );
00349         }
00350 
00362         template<class Func>
00363         Operation< Func >&
00364         addOperation( const std::string name, Func* func, ExecutionThread et = ClientThread )
00365         {
00366             typedef Func Signature;
00367             boost::function<Signature> bfunc = func;
00368             Operation<Signature>* op = new Operation<Signature>(name, bfunc, et, this->getOwnerExecutionEngine() );
00369             ownedoperations.push_back(op);
00370             return addOperation( *op );
00371         }
00372 
00385         template<class Func, class Service>
00386         Operation< typename internal::GetSignature<Func>::Signature >&
00387         addSynchronousOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
00388         {
00389             typedef typename internal::GetSignature<Func>::Signature Signature;
00390             Operation<Signature>* op = new Operation<Signature>(name, func, serv, et, this->getOwnerExecutionEngine() );
00391             ownedoperations.push_back(op);
00392             return addSynchronousOperation( *op );
00393         }
00394 
00400         template<class Func,class ObjT>
00401         Operation< typename internal::GetSignatureDS<Func>::Signature>& addOperationDS( const std::string& name, Func func, internal::DataSource< boost::shared_ptr<ObjT> >* sp,
00402                 ExecutionThread et = ClientThread)
00403         {
00404             typedef typename internal::GetSignatureDS<Func>::Signature SignatureDS;    // function signature with normal object pointer
00405             Operation<SignatureDS>* op = new Operation<SignatureDS>(name, boost::function<SignatureDS>(func), et, this->getOwnerExecutionEngine() );
00406             ownedoperations.push_back(op);
00407             return addOperationDS( sp, *op );
00408         }
00409 
00415         template<class Signature,class ObjT>
00416         Operation<Signature>& addOperationDS( internal::DataSource< boost::shared_ptr<ObjT> >* sp, Operation<Signature>& op)
00417         {
00418             if ( this->addLocalOperation( op ) == false ) {
00419                 assert(false);
00420                 return op; // should never be reached.
00421             }
00422             this->add( op.getName(), new internal::OperationInterfacePartFusedDS<Signature,ObjT>( sp, &op) );
00423             return op;
00424         }
00425 
00435         base::DataSourceBase::shared_ptr getOperation( std::string name,
00436                                    const std::vector<base::DataSourceBase::shared_ptr>& args, ExecutionEngine* caller) const
00437         {
00438             return this->produce(name, args, caller);
00439         }
00440 
00449         internal::OperationCallerC create(std::string name, ExecutionEngine* caller);
00450 
00454         bool resetOperation(std::string name, base::OperationBase* impl);
00455     protected:
00456         typedef std::map< std::string, shared_ptr > Services;
00458         Services services;
00459 
00460         bool testOperation(base::OperationBase& op);
00461         typedef std::map<std::string,base::OperationBase* > SimpleOperations;
00462         typedef std::vector<base::OperationBase*> OperationList;
00463         SimpleOperations simpleoperations;
00464         OperationList ownedoperations;
00465         std::string mname;
00466         std::string mdescription;
00467         TaskContext* mowner;
00468         shared_ptr parent;
00469     };
00470 }
00471 
00472 
00473 #endif