Orocos Real-Time Toolkit  2.9.0
TaskContext.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Tue Dec 21 22:43:08 CET 2004 TaskContext.hpp
3 
4  TaskContext.hpp - description
5  -------------------
6  begin : Tue December 21 2004
7  copyright : (C) 2004 Peter Soetens
8  email : peter.soetens@mech.kuleuven.ac.be
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_TASK_CONTEXT_HPP
40 #define ORO_TASK_CONTEXT_HPP
41 
42 
43 #include "rtt-config.h"
44 #include "Service.hpp"
45 #include "ServiceRequester.hpp"
46 #include "DataFlowInterface.hpp"
47 #include "ExecutionEngine.hpp"
48 #include "base/TaskCore.hpp"
49 #include <boost/make_shared.hpp>
50 
51 #include <string>
52 #include <map>
53 
54 namespace RTT
55 {
94  : public base::TaskCore
95  {
96  public:
100  typedef std::vector< std::string > PeerList;
101 
110  TaskContext( const std::string& name, TaskState initial_state = Stopped );
111 
112  virtual ~TaskContext();
113 
117  virtual const std::string& getName() const { return tcservice->getName(); }
118 
133  bool setActivity( base::ActivityInterface* new_act );
134 
140  base::ActivityInterface* getActivity();
141 
161  template<typename T>
162  T* getActivity() { return dynamic_cast<T*>(getActivity()); }
163 
169  virtual void clear();
170 
179  virtual bool ready();
180 
181  virtual bool start();
182  virtual bool stop();
183 
198  virtual bool addPeer( TaskContext* peer, std::string alias = "" );
199 
203  virtual void removePeer( const std::string& name );
204 
208  virtual void removePeer( TaskContext* peer );
209 
213  virtual bool connectPeers( TaskContext* peer );
214 
220  virtual void disconnect();
221 
225  virtual void disconnectPeers( const std::string& name );
226 
231  virtual PeerList getPeerList() const;
232 
236  virtual bool hasPeer( const std::string& peer_name ) const;
237 
242  virtual TaskContext* getPeer(const std::string& peer_name ) const;
257  Service::shared_ptr provides() { return tcservice; }
258 
264  Service::shared_ptr provides(const std::string& service_name) { return tcservice->provides(service_name); }
265 
270  ServiceRequester::shared_ptr requires() { return tcrequests; }
271 
276  ServiceRequester::shared_ptr requires(const std::string& service_name) {
277  return tcrequests->requires(service_name);
278  }
279 
283  virtual bool connectServices( TaskContext* peer);
284 
297  template<class ServiceType>
298  boost::shared_ptr<ServiceType> getProvider(const std::string& name) {
299  if (!prepareProvide(name)) return boost::shared_ptr<ServiceType>();
300  LocalServices::iterator it = localservs.find(name);
301  if ( it != localservs.end() ) {
302  return boost::dynamic_pointer_cast<ServiceType>(it->second);
303  }
304  boost::shared_ptr<ServiceType> st = boost::make_shared<ServiceType>(this);
305  st->connectTo( provides(name) );
306  localservs[name] = st;
307  return st;
308  }
309 
315  bool loadService(const std::string& service_name);
316 
335  template<class Signature>
337  {
338  return tcservice->addOperation(op);
339  }
340 
345  template<class Func, class Service>
347  addOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
348  {
349  return tcservice->addOperation(name,func, serv, et);
350  }
351 
356  template<class Signature>
358  addOperation( const std::string name, Signature* func, ExecutionThread et = ClientThread )
359  {
360  return tcservice->addOperation(name, func, et);
361  }
362 
373  {
374  return tcservice->getOperation(name);
375  }
376 
380  OperationInterface* operations() { return this->provides().get(); }
381 
399  template<class T>
400  bool addAttribute( const std::string& name, T& attr) {
401  return tcservice->addAttribute(name, attr);
402  }
403 
412  template<class T>
413  bool addConstant( const std::string& name, const T& attr) {
414  return tcservice->addConstant(name, attr);
415  }
416 
427  {
428  return tcservice->addAttribute(a);
429  }
430 
439  base::AttributeBase* getAttribute( const std::string& name ) const
440  {
441  return tcservice->getAttribute(name);
442  }
443 
447  ConfigurationInterface* attributes() { return this->provides().get(); }
448 
467  template<class T>
468  Property<T>& addProperty( const std::string& name, T& attr) {
469  return tcservice->addProperty(name, attr);
470  }
471 
477  return tcservice->addProperty(pb);
478  }
479 
487  base::PropertyBase* getProperty(const std::string& name) const
488  {
489  return tcservice->getProperty(name);
490  }
491 
495  PropertyBag* properties() { return this->provides()->properties(); }
496 
512  base::PortInterface& addPort(const std::string& name, base::PortInterface& port) {
513  port.setName(name);
514  return ports()->addPort(port);
515  }
516 
523  return ports()->addPort(port);
524  }
525 
526  typedef boost::function<void(base::PortInterface*)> SlotFunction;
536  base::InputPortInterface& addEventPort(const std::string& name, base::InputPortInterface& port, SlotFunction callback = SlotFunction() ) {
537  port.setName(name);
538  return ports()->addEventPort(port,callback);
539  }
540 
549  base::InputPortInterface& addEventPort(base::InputPortInterface& port, SlotFunction callback = SlotFunction() ) {
550  return ports()->addEventPort(port,callback);
551  }
552 
558  base::PortInterface* getPort(const std::string& name) const {
559  return ports()->getPort(name);
560  }
561 
562 
567  return tcservice.get();
568  }
569 
573  const DataFlowInterface* ports() const {
574  return tcservice.get();
575  }
576 
580  virtual bool connectPorts( TaskContext* peer );
583  protected:
591  void forceActivity( base::ActivityInterface* new_act);
592 
606  virtual bool dataOnPortHook( base::PortInterface* port );
607 
619  virtual void dataOnPortCallback( base::PortInterface* port );
620 
621  // Required to invoke dataOnPortCallback() from the ExecutionEngine.
622  friend class ExecutionEngine;
623 
624  private:
625 
626  typedef std::map< std::string, TaskContext* > PeerMap;
627  typedef std::vector< TaskContext* > Users;
629  PeerMap _task_map;
631  Users musers;
632 
637  void addUser(TaskContext* user);
638 
643  void removeUser(TaskContext* user);
644 
648  void setup();
649 
650  friend class DataFlowInterface;
651  typedef std::map<base::PortInterface*, SlotFunction > UserCallbacks;
652  UserCallbacks user_callbacks;
653 
658  void dataOnPort(base::PortInterface* port);
659 
663  void setDataOnPortCallback(base::InputPortInterface* port, SlotFunction callback);
664 
668  void removeDataOnPortCallback(base::PortInterface* port);
669 
677  bool prepareProvide(const std::string& name);
678 
679  typedef std::map<std::string, boost::shared_ptr<ServiceRequester> > LocalServices;
680  LocalServices localservs;
681 
682  Service::shared_ptr tcservice;
683  ServiceRequester::shared_ptr tcrequests;
684  os::Mutex mportlock;
685 
686  // non copyable
688 
694  };
695 
702 
710 }
711 
712 #endif
The base class of the InputPort.
Property< T > & addProperty(const std::string &name, T &attr)
Adds a variable of any type as a property to the attribute interface.
The Interface of a TaskContext which exposes its data-flow ports.
The minimal Orocos task.
Definition: TaskCore.hpp:54
const DataFlowInterface * ports() const
Get the Data flow ports of this task.
base::PortInterface * getPort(const std::string &name) const
Get a port of this Component.
Service::shared_ptr provides()
Returns this Service.
ConfigurationInterface * attributes()
Returns the attributes of this TaskContext as an ConfigurationInterface.
base::InputPortInterface & addEventPort(const std::string &name, base::InputPortInterface &port, SlotFunction callback=SlotFunction())
Name and add an Event triggering Port to the interface of this task and add a Service with the same n...
The operation ties a C or C++ function into a component interface.
Base class for all properties.
Operation< Signature > & addOperation(const std::string name, Signature *func, ExecutionThread et=ClientThread)
Adds a C function as an operation.
bool addProperty(base::PropertyBase &pb)
Add an base::PropertyBase as a property.
OperationInterface * operations()
Returns the operations of this TaskContext as an OperationInterface.
bool addConstant(const std::string &name, const T &attr)
Adds a variable of any type as read-only attribute to the attribute interface.
boost::shared_ptr< ServiceRequester > shared_ptr
Operation< typename internal::GetSignature< Func >::Signature > & addOperation(const std::string name, Func func, Service *serv, ExecutionThread et=ClientThread)
Adds a C++ function as an operation.
#define RTT_API
Definition: rtt-config.h:97
A container for holding references to properties.
Definition: PropertyBag.hpp:96
bool addAttribute(base::AttributeBase &a)
Add an base::AttributeBase which remains owned by the user.
base::InputPortInterface & addEventPort(base::InputPortInterface &port, SlotFunction callback=SlotFunction())
Add an Event triggering Port to the interface of this task and add a Service with the same name of th...
An execution engine serialises (executes one after the other) the execution of all commands...
boost::shared_ptr< Service > shared_ptr
Definition: Service.hpp:101
This class allows storage and retrieval of operations, ports, attributes and properties provided by a...
Definition: Service.hpp:93
ServiceRequester::shared_ptr requires()
Returns the object that manages which methods this Task requires to be implemented by another task...
boost::function< void(base::PortInterface *)> SlotFunction
Name and add a Port to the interface of this task and add a Service with the same name of the port...
DataFlowInterface * ports()
Get the Data flow ports of this task.
An attribute is a minimalistic, named placeholder for data.
boost::shared_ptr< ActivityInterface > shared_ptr
Service::shared_ptr provides(const std::string &service_name)
Returns a sub-Service which resorts under this Service.
Interface to start/stop and query a Activity.
base::PortInterface & addPort(const std::string &name, base::PortInterface &port)
Name and add a Port to the interface of this task and add a Service with the same name of the port...
A property represents a named value of any type with a description.
Definition: Property.hpp:76
base::AttributeBase * getAttribute(const std::string &name) const
Adds a variable of any type as read/write attribute to the attribute interface.
std::vector< std::string > PeerList
A list of Peer TaskContext names.
bool setName(const std::string &name)
Change the name of this unconnected Port.
A class for keeping track of Attribute, Constant and Property objects of a TaskContext.
TaskState
Describes the different states a component can have.
Definition: TaskCore.hpp:99
This class defines the interface for creating operation objects without using C++ templates...
bool addAttribute(const std::string &name, T &attr)
Adds a variable of any type as read/write attribute to the attribute interface.
T * getActivity()
Get a pointer to the activity running this component.
PropertyBag * properties()
Returns the properties of this TaskContext as a PropertyBag.
OperationInterfacePart * getOperation(std::string name)
Get a previously added operation for use in a C++ OperationCaller object.
base::PropertyBase * getProperty(const std::string &name) const
Get a Property with name name.
The TaskContext is the C++ representation of an Orocos component.
Definition: TaskContext.hpp:93
An object oriented wrapper around a non recursive mutex.
Definition: Mutex.hpp:92
bool connectPorts(TaskContext *A, TaskContext *B)
Connect the Data Flow Ports of A and B in both directions, by matching port names.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
The base class of every data flow port.
Operation< Signature > & addOperation(Operation< Signature > &op)
Add an operation object to the interface.
Holds all exported operations of a component and is able to produce callers for these operations...
base::PortInterface & addPort(base::PortInterface &port)
Add a Port to the interface of this task and add a Service with the same name of the port...
bool connectPeers(TaskContext *A, TaskContext *B)
Set up the Execution Flow (who knows who) between A and B in both directions.
virtual const std::string & getName() const
Returns the name of this TaskContext.
ExecutionThread
Users can choose if an operation&#39;s function is executed in the component&#39;s thread (OwnThread) or in t...
ServiceRequester::shared_ptr requires(const std::string &service_name)
Returns the object that manages which methods this Task requires to be implemented by another service...
boost::shared_ptr< ServiceType > getProvider(const std::string &name)
Use this method to be able to make OperationCaller calls to services provided by this component...