Orocos Real-Time Toolkit  2.9.0
CmdFunction.hpp
Go to the documentation of this file.
1 #ifndef ORO_CMD_FUNCTION_HPP
2 #define ORO_CMD_FUNCTION_HPP
3 
4 #include "../SendStatus.hpp"
5 #include "../base/ActionInterface.hpp"
6 #include "../base/DisposableInterface.hpp"
7 #include "ConditionInterface.hpp"
8 #include "../internal/DataSources.hpp"
9 #include "ProgramInterface.hpp"
10 #include "../internal/DataSource.hpp"
11 #include "../ExecutionEngine.hpp"
12 #include <boost/shared_ptr.hpp>
13 #include <boost/bind.hpp>
14 
15 #include <iostream>
16 
17 namespace RTT
18 { namespace scripting {
19  using namespace detail;
20  using namespace std;
21 
27  : public internal::DataSource<SendStatus>
28  {
29  base::ActionInterface* minit;
30  ExecutionEngine* mrunner;
31  ExecutionEngine* mcaller;
32  boost::shared_ptr<ProgramInterface> _foo;
33  mutable SendStatus ss;
34  mutable bool isqueued;
35  mutable bool maccept;
36 
37  public:
46  boost::shared_ptr<ProgramInterface> foo,
48  )
49  : minit(init_com),
50  mrunner(p), mcaller(caller),
51  _foo( foo ), ss(SendFailure), isqueued(false), maccept(false)
52  {
53  }
54 
56  this->reset();
57  delete minit;
58  }
59 
60  virtual SendStatus get() const {
61  try {
62  // this is asyn behaviour :
63  if (isqueued == false ) {
64  isqueued = true;
65  ss = SendNotReady;
66  // is called before runFunction is executed.
67  minit->readArguments();
68  maccept = minit->execute() && mrunner->runFunction( _foo.get() );
69  // we ignore the ret value of start(). It could have been auto-started during loading() of the function.
70  if ( _foo->needsStart() ) { // _foo might be auto-started in runFunction()
71  _foo->start();
72  }
73  if ( ! maccept ) {
74  return ss = SendFailure;
75  }
76  }
77  if ( _foo->inError() ) {
78  return ss = CollectFailure;
79  }
80  if ( _foo->isStopped() ) {
81  return ss = SendSuccess;
82  }
83  } catch (...) {
84  cout << "CmdFunction threw an exception" <<endl;
85  }
86  return ss;
87  }
88 
89  virtual SendStatus value() const {
90  return ss;
91  }
92 
93  virtual SendStatus const& rvalue() const {
94  return ss;
95  }
96 
97  virtual bool evaluate() const {
98  // return true if ready to be read
99  return get() != SendNotReady;
100  }
101 
102  virtual void reset() {
103  if (_foo->isLoaded()) mrunner->removeFunction( _foo.get() );
104  maccept = false;
105  isqueued = false;
106  ss = SendFailure;
107  }
108 
110  {
111  return new CmdFunction( minit->clone(), _foo, mrunner, mcaller );
112  }
113 
114  CmdFunction* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const
115  {
116  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
117  if ( alreadyCloned[this] == 0 ) {
118  boost::shared_ptr<ProgramInterface> fcpy( _foo->copy(alreadyCloned) );
119  alreadyCloned[this] = new CmdFunction( minit->copy(alreadyCloned), fcpy , mrunner, mcaller);
120  }
121 
122  assert ( dynamic_cast<CmdFunction*>( alreadyCloned[this] ) == static_cast<CmdFunction*>( alreadyCloned[this] ) );
123  return static_cast<CmdFunction*>( alreadyCloned[this] );
124  }
125 
126  };
127 
132  : public ConditionInterface
133  {
135  public:
136 
138  collectds(ds)
139  {
140  }
141 
142  void reset() {}
143 
144  bool evaluate()
145  {
146  // We may not call collectds->evaluate() inhere, because this actively tries a collect,
147  // and if it would succeed, we would proceed immediately, without giving the vertex node
148  // a chance to do something with the return value of the cmd.
149  return collectds->rvalue() != SendNotReady;
150  }
151 
152  virtual CmdCollectCondition* clone() const
153  {
154  return new CmdCollectCondition( collectds );
155  }
157  std::map<
158  const base::DataSourceBase*,
159  base::DataSourceBase*>& alreadyCloned) const
160  {
161  return new CmdCollectCondition ( collectds->copy(alreadyCloned) );
162  }
163  };
164 }}
165 
166 #endif
DataSource is a base class representing a generic way to read data of type T.
Definition: DataSource.hpp:94
void reset()
Some conditions need to be reset at some points.
virtual CmdCollectCondition * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
When copying an Orocos program, we want identical internal::DataSource&#39;s to be mapped to identical Da...
virtual SendStatus const & rvalue() const
Get a const reference to the value of this DataSource.
Definition: CmdFunction.hpp:93
CmdFunction * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
virtual bool removeFunction(base::ExecutableInterface *f)
Remove a running function added with runFunction.
The base class for all internal data representations.
virtual CmdCollectCondition * clone() const
The Clone Software Pattern.
This interface represents the concept of a condition which can be evaluated and return true or false...
virtual void reset()
Reset the data to initial values.
STL namespace.
CmdFunction * clone() const
Return a shallow clone of this DataSource.
bool evaluate()
Evaluate the Condition and return the outcome.
#define RTT_SCRIPTING_API
basic_ostreams & endl(basic_ostreams &s)
Flush and newline.
Definition: rtstreams.cpp:110
An execution engine serialises (executes one after the other) the execution of all commands...
SendStatus
Returns the status of a send() or collect() invocation.
Definition: SendStatus.hpp:53
virtual DataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const =0
Create a deep copy of this internal::DataSource, unless it is already cloned.
printstream cout
Console Output.
Definition: rtstreams.cpp:45
virtual SendStatus value() const
Return the result of the last evaluate() function.
Definition: CmdFunction.hpp:89
Returned when the result of the send() could not be collected.
Definition: SendStatus.hpp:55
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
virtual bool evaluate() const
Force an evaluation of the DataSourceBase.
Definition: CmdFunction.hpp:97
DataSource< SendStatus >::shared_ptr collectds
Returned when the send() succeeded, but the operation has not yet been executed by the receiving comp...
Definition: SendStatus.hpp:57
A DataSource which sends a FunctionFraph for execution in a ExecutionEngine.
Definition: CmdFunction.hpp:26
Returned when the send() failed to deliver the operation call to the receiving component.
Definition: SendStatus.hpp:56
CmdFunction(base::ActionInterface *init_com, boost::shared_ptr< ProgramInterface > foo, ExecutionEngine *p, ExecutionEngine *caller)
Create a Command to send a function to a ExecutionEngine.
Definition: CmdFunction.hpp:45
CmdCollectCondition(DataSource< SendStatus >::shared_ptr ds)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
virtual bool runFunction(base::ExecutableInterface *f)
Run a given function in step() or loop().
A DataSource that collects the result of a CmdFunction.