Orocos Real-Time Toolkit  2.8.3
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 
121  TaskContext(const std::string& name, ExecutionEngine* parent, TaskState initial_state = Stopped );
122 
123  virtual ~TaskContext();
124 
128  virtual const std::string& getName() const { return tcservice->getName(); }
129 
144  bool setActivity( base::ActivityInterface* new_act );
145 
151  base::ActivityInterface* getActivity();
152 
172  template<typename T>
173  T* getActivity() { return dynamic_cast<T*>(getActivity()); }
174 
180  virtual void clear();
181 
190  virtual bool ready();
191 
192  virtual bool start();
193  virtual bool stop();
194 
209  virtual bool addPeer( TaskContext* peer, std::string alias = "" );
210 
214  virtual void removePeer( const std::string& name );
215 
219  virtual void removePeer( TaskContext* peer );
220 
224  virtual bool connectPeers( TaskContext* peer );
225 
231  virtual void disconnect();
232 
236  virtual void disconnectPeers( const std::string& name );
237 
242  virtual PeerList getPeerList() const;
243 
247  virtual bool hasPeer( const std::string& peer_name ) const;
248 
253  virtual TaskContext* getPeer(const std::string& peer_name ) const;
268  Service::shared_ptr provides() { return tcservice; }
269 
275  Service::shared_ptr provides(const std::string& service_name) { return tcservice->provides(service_name); }
276 
281  ServiceRequester::shared_ptr requires() { return tcrequests; }
282 
287  ServiceRequester::shared_ptr requires(const std::string& service_name) {
288  return tcrequests->requires(service_name);
289  }
290 
294  virtual bool connectServices( TaskContext* peer);
295 
308  template<class ServiceType>
309  boost::shared_ptr<ServiceType> getProvider(const std::string& name) {
310  if (!prepareProvide(name)) return boost::shared_ptr<ServiceType>();
311  LocalServices::iterator it = localservs.find(name);
312  if ( it != localservs.end() ) {
313  return boost::dynamic_pointer_cast<ServiceType>(it->second);
314  }
315  boost::shared_ptr<ServiceType> st = boost::make_shared<ServiceType>(this);
316  st->connectTo( provides(name) );
317  localservs[name] = st;
318  return st;
319  }
320 
326  bool loadService(const std::string& service_name);
327 
346  template<class Signature>
348  {
349  return tcservice->addOperation(op);
350  }
351 
356  template<class Func, class Service>
358  addOperation( const std::string name, Func func, Service* serv, ExecutionThread et = ClientThread )
359  {
360  return tcservice->addOperation(name,func, serv, et);
361  }
362 
367  template<class Signature>
369  addOperation( const std::string name, Signature* func, ExecutionThread et = ClientThread )
370  {
371  return tcservice->addOperation(name, func, et);
372  }
373 
384  {
385  return tcservice->getOperation(name);
386  }
387 
391  OperationInterface* operations() { return this->provides().get(); }
392 
410  template<class T>
411  bool addAttribute( const std::string& name, T& attr) {
412  return tcservice->addAttribute(name, attr);
413  }
414 
423  template<class T>
424  bool addConstant( const std::string& name, const T& attr) {
425  return tcservice->addConstant(name, attr);
426  }
427 
438  {
439  return tcservice->addAttribute(a);
440  }
441 
450  base::AttributeBase* getAttribute( const std::string& name ) const
451  {
452  return tcservice->getAttribute(name);
453  }
454 
458  ConfigurationInterface* attributes() { return this->provides().get(); }
459 
478  template<class T>
479  Property<T>& addProperty( const std::string& name, T& attr) {
480  return tcservice->addProperty(name, attr);
481  }
482 
488  return tcservice->addProperty(pb);
489  }
490 
498  base::PropertyBase* getProperty(const std::string& name) const
499  {
500  return tcservice->getProperty(name);
501  }
502 
506  PropertyBag* properties() { return this->provides()->properties(); }
507 
523  base::PortInterface& addPort(const std::string& name, base::PortInterface& port) {
524  port.setName(name);
525  return ports()->addPort(port);
526  }
527 
534  return ports()->addPort(port);
535  }
536 
537  typedef boost::function<void(base::PortInterface*)> SlotFunction;
547  base::InputPortInterface& addEventPort(const std::string& name, base::InputPortInterface& port, SlotFunction callback = SlotFunction() ) {
548  port.setName(name);
549  return ports()->addEventPort(port,callback);
550  }
551 
560  base::InputPortInterface& addEventPort(base::InputPortInterface& port, SlotFunction callback = SlotFunction() ) {
561  return ports()->addEventPort(port,callback);
562  }
563 
569  base::PortInterface* getPort(const std::string& name) const {
570  return ports()->getPort(name);
571  }
572 
573 
578  return tcservice.get();
579  }
580 
584  const DataFlowInterface* ports() const {
585  return tcservice.get();
586  }
587 
591  virtual bool connectPorts( TaskContext* peer );
594  protected:
602  void forceActivity( base::ActivityInterface* new_act);
603 
617  virtual bool dataOnPortHook( base::PortInterface* port );
618 
619  private:
620 
621  typedef std::map< std::string, TaskContext* > PeerMap;
622  typedef std::vector< TaskContext* > Users;
624  PeerMap _task_map;
626  Users musers;
627 
632  void addUser(TaskContext* user);
633 
638  void removeUser(TaskContext* user);
639 
643  void setup();
644 
645  friend class DataFlowInterface;
647  typedef std::map<base::PortInterface*, SlotFunction > UserCallbacks;
648  UserCallbacks user_callbacks;
649 
654  void dataOnPort(base::PortInterface* port);
660  bool dataOnPortSize(unsigned int max);
664  void dataOnPortCallback(base::InputPortInterface* port, SlotFunction callback);
668  void dataOnPortRemoved(base::PortInterface* port);
669 
674  void prepareUpdateHook();
675 
683  bool prepareProvide(const std::string& name);
684 
685  typedef std::map<std::string, boost::shared_ptr<ServiceRequester> > LocalServices;
686  LocalServices localservs;
687 
688  Service::shared_ptr tcservice;
689  ServiceRequester::shared_ptr tcrequests;
690  os::Mutex mportlock;
691 
692  // non copyable
694 
700  };
701 
708 
716 }
717 
718 #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.
This object represents the default Multi-Writer, Single-Reader queue implementation used by Orocos ob...
Definition: MWSRQueue.hpp:66
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:82
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:51
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...