Service.hpp
Go to the documentation of this file.
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 && BOOST_VERSION < 105300
00062 #include <boost/smart_ptr/enable_shared_from_this2.hpp>
00063 #endif
00064 #if BOOST_VERSION >= 105300
00065 #include <boost/smart_ptr/enable_shared_from_raw.hpp>
00066 #endif
00067 
00068 namespace RTT
00069 {
00070     namespace internal {
00071 #if BOOST_VERSION >= 104000
00072 #if BOOST_VERSION < 105300
00073         typedef  boost::enable_shared_from_this2<Service> shared_from_raw;
00074 #else
00075         typedef  boost::enable_shared_from_raw shared_from_raw;
00076 #endif
00077 #else
00078         typedef  boost::enable_shared_from_this<Service> shared_from_raw;
00079 #endif
00080     }
00096     class RTT_API Service
00097         : public OperationInterface,
00098           public ConfigurationInterface,
00099           public DataFlowInterface,
00100           public internal::shared_from_raw
00101     {
00102     public:
00103         typedef OperationInterface Factory;
00104         typedef boost::shared_ptr<Service> shared_ptr;
00105         typedef boost::shared_ptr<const Service> shared_constptr;
00106         typedef std::vector<std::string> ProviderNames;
00107 
00108 #if BOOST_VERSION >= 105300
00109         Service::shared_ptr shared_from_this() { return boost::shared_from_raw(this); }
00110         Service::shared_constptr shared_from_this() const { return boost::shared_from_raw(this); }
00111 #endif
00112 
00123         static Service::shared_ptr Create(const std::string& name, TaskContext* owner = 0);
00124 
00135         Service(const std::string& name, TaskContext* owner = 0);
00136 
00137         virtual ~Service();
00138 
00142         const std::string& getName() const { return mname; }
00143 
00147         const std::string& doc() const { return mdescription; }
00148 
00152         void doc(const std::string& description) { mdescription = description; }
00153 
00157         void setName(const std::string& name) { mname = name;}
00158 
00163         void setOwner(TaskContext* new_owner);
00164 
00169         void setParent(shared_ptr new_parent);
00170 
00174         shared_ptr getParent() const { return parent; }
00175 
00180         virtual ProviderNames getProviderNames() const;
00181 
00188         TaskContext* getOwner() const { return mowner; }
00189 
00194         ExecutionEngine* getOwnerExecutionEngine() const;
00195 
00207         virtual bool addService( shared_ptr obj );
00208 
00218         virtual void removeService( std::string const& service_name );
00219 
00226         Service::shared_ptr provides();
00227 
00237         Service::shared_ptr provides(const std::string& service_name);
00238 
00248         shared_ptr getService(const std::string& service_name);
00249 
00255         bool hasService(const std::string& service_name);
00256 
00263         void clear();
00264 
00269         std::vector<std::string> getOperationNames() const;
00270 
00275         bool hasOperation(const std::string& name) const;
00276 
00289         bool addLocalOperation( base::OperationBase& op );
00290 
00297         boost::shared_ptr<base::DisposableInterface> getLocalOperation( std::string name );
00298 
00309         OperationInterfacePart* getOperation( std::string name );
00310 
00314         void removeOperation( const std::string& name );
00315 
00332         bool setOperationThread(std::string const& name, ExecutionThread et);
00333 
00343         template<class Signature>
00344         Operation<Signature>& addOperation( Operation<Signature>& op )
00345         {
00346             if ( this->addLocalOperation( op ) == false )
00347                 return op;
00348             this->add( op.getName(), new internal::OperationInterfacePartFused<Signature>( &op ) );
00349             return op;
00350         }
00351 
00352 #ifdef ORO_SIGNALLING_OPERATIONS
00353 
00362         template<class Signature>
00363         Operation<Signature>& addEventOperation( Operation<Signature>& op )
00364         {
00365             op.signals();
00366             if ( this->addLocalOperation( op ) == false )
00367                 return op;
00368             this->add( op.getName(), new internal::EventOperationInterfacePartFused<Signature>( &op ) );
00369             return op;
00370         }
00371 #endif
00372 
00381         template<class Signature>
00382         Operation<Signature>& addSynchronousOperation( Operation<Signature>& op )
00383         {
00384             if ( this->addLocalOperation( op ) == false )
00385                 return op;
00386             this->add( op.getName(), new internal::SynchronousOperationInterfacePartFused<Signature>( &op ) );
00387             return op;
00388         }
00389 
00403         template<class Func, class Class>
00404         Operation< typename internal::GetSignature<Func>::Signature >&
00405         addOperation( const std::string name, Func func, Class* obj, ExecutionThread et = ClientThread )
00406         {
00407             typedef typename internal::GetSignature<Func>::Signature Signature;
00408             Operation<Signature>* op = new Operation<Signature>(name, func, obj, et, this->getOwnerExecutionEngine() );
00409             ownedoperations.push_back(op);
00410             return addOperation( *op );
00411         }
00412 
00424         template<class Func>
00425         Operation< Func >&
00426         addOperation( const std::string name, Func* func, ExecutionThread et = ClientThread )
00427         {
00428             typedef Func Signature;
00429             boost::function<Signature> bfunc = func;
00430             Operation<Signature>* op = new Operation<Signature>(name, bfunc, et, this->getOwnerExecutionEngine() );
00431             ownedoperations.push_back(op);
00432             return addOperation( *op );
00433         }
00434 
00435 #ifdef ORO_SIGNALLING_OPERATIONS
00436 
00447         template<class Func>
00448         Operation< Func >&
00449         addEventOperation( const std::string name, Func* func, ExecutionThread et = ClientThread )
00450         {
00451             typedef Func Signature;
00452             boost::function<Signature> bfunc = func;
00453             Operation<Signature>* op = new Operation<Signature>(name, bfunc, et, this->getOwnerExecutionEngine() );
00454             ownedoperations.push_back(op);
00455             return addEventOperation( *op );
00456         }
00457 
00470         template<class Func, class Class>
00471         Operation< typename internal::GetSignature<Func>::Signature >&
00472         addEventOperation( const std::string name, Func func, Class* obj, ExecutionThread et = ClientThread )
00473         {
00474             typedef typename internal::GetSignature<Func>::Signature Signature;
00475             Operation<Signature>* op = new Operation<Signature>(name, func, obj, et, this->getOwnerExecutionEngine() );
00476             ownedoperations.push_back(op);
00477             return addEventOperation( *op );
00478         }
00479 #endif
00480 
00493         template<class Func, class Class>
00494         Operation< typename internal::GetSignature<Func>::Signature >&
00495         addSynchronousOperation( const std::string name, Func func, Class* obj, ExecutionThread et = ClientThread )
00496         {
00497             typedef typename internal::GetSignature<Func>::Signature Signature;
00498             Operation<Signature>* op = new Operation<Signature>(name, func, obj, et, this->getOwnerExecutionEngine() );
00499             ownedoperations.push_back(op);
00500             return addSynchronousOperation( *op );
00501         }
00502 
00508         template<class Func,class ObjT>
00509         Operation< typename internal::GetSignatureDS<Func>::Signature>& addOperationDS( const std::string& name, Func func, internal::DataSource< boost::shared_ptr<ObjT> >* sp,
00510                 ExecutionThread et = ClientThread)
00511         {
00512             typedef typename internal::GetSignatureDS<Func>::Signature SignatureDS;    // function signature with normal object pointer
00513             Operation<SignatureDS>* op = new Operation<SignatureDS>(name, boost::function<SignatureDS>(func), et, this->getOwnerExecutionEngine() );
00514             ownedoperations.push_back(op);
00515             return addOperationDS( sp, *op );
00516         }
00517 
00523         template<class Signature,class ObjT>
00524         Operation<Signature>& addOperationDS( internal::DataSource< boost::shared_ptr<ObjT> >* sp, Operation<Signature>& op)
00525         {
00526             if ( this->addLocalOperation( op ) == false ) {
00527                 assert(false);
00528                 return op; // should never be reached.
00529             }
00530             this->add( op.getName(), new internal::OperationInterfacePartFusedDS<Signature,ObjT>( sp, &op) );
00531             return op;
00532         }
00533 
00543         base::DataSourceBase::shared_ptr getOperation( std::string name,
00544                                    const std::vector<base::DataSourceBase::shared_ptr>& args, ExecutionEngine* caller) const
00545         {
00546             return this->produce(name, args, caller);
00547         }
00548 
00557         internal::OperationCallerC create(std::string name, ExecutionEngine* caller);
00558 
00562         bool resetOperation(std::string name, base::OperationBase* impl);
00563     protected:
00564         typedef std::map< std::string, shared_ptr > Services;
00566         Services services;
00567 
00568         bool testOperation(base::OperationBase& op);
00569         typedef std::map<std::string,base::OperationBase* > SimpleOperations;
00570         typedef std::vector<base::OperationBase*> OperationList;
00571         SimpleOperations simpleoperations;
00572         OperationList ownedoperations;
00573         std::string mname;
00574         std::string mdescription;
00575         TaskContext* mowner;
00576         shared_ptr parent;
00577     };
00578 }
00579 
00580 
00581 #endif


rtt
Author(s): RTT Developers
autogenerated on Wed Aug 26 2015 16:15:53