CmdFunction.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Sat Oct 25 00:52:59 2014 +0200 CmdFunction.hpp
3 
4  CmdFunction.hpp - description
5  -------------------
6  begin : Sat Oct 25 2014
7  copyright : (C) 2014 Peter Soetens
8  email : peter@thesourceworks.com
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  * 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_CMD_FUNCTION_HPP
40 #define ORO_CMD_FUNCTION_HPP
41 
42 #include "../SendStatus.hpp"
43 #include "../base/ActionInterface.hpp"
44 #include "../base/DisposableInterface.hpp"
45 #include "ConditionInterface.hpp"
46 #include "../internal/DataSources.hpp"
47 #include "ProgramInterface.hpp"
48 #include "../internal/DataSource.hpp"
49 #include "../ExecutionEngine.hpp"
50 #include <boost/shared_ptr.hpp>
51 #include <boost/bind.hpp>
52 
53 #include <iostream>
54 
55 namespace RTT
56 { namespace scripting {
57  using namespace detail;
58  using namespace std;
59 
64  class RTT_SCRIPTING_API CmdFunction
65  : public internal::DataSource<SendStatus>
66  {
70  boost::shared_ptr<ProgramInterface> _foo;
71  mutable SendStatus ss;
72  mutable bool isqueued;
73  mutable bool maccept;
74 
75  public:
84  boost::shared_ptr<ProgramInterface> foo,
86  )
87  : minit(init_com),
88  mrunner(p), mcaller(caller),
89  _foo( foo ), ss(SendFailure), isqueued(false), maccept(false)
90  {
91  }
92 
94  this->reset();
95  delete minit;
96  }
97 
98  virtual SendStatus get() const {
99  try {
100  // this is asyn behaviour :
101  if (isqueued == false ) {
102  isqueued = true;
103  ss = SendNotReady;
104  // is called before runFunction is executed.
105  minit->readArguments();
106  maccept = minit->execute() && mrunner->runFunction( _foo.get() );
107  // we ignore the ret value of start(). It could have been auto-started during loading() of the function.
108  if ( _foo->needsStart() ) { // _foo might be auto-started in runFunction()
109  _foo->start();
110  }
111  if ( ! maccept ) {
112  return ss = SendFailure;
113  }
114  }
115  if ( _foo->inError() ) {
116  return ss = CollectFailure;
117  }
118  if ( _foo->isStopped() ) {
119  return ss = SendSuccess;
120  }
121  } catch (...) {
122  cout << "CmdFunction threw an exception" <<endl;
123  }
124  return ss;
125  }
126 
127  virtual SendStatus value() const {
128  return ss;
129  }
130 
131  virtual SendStatus const& rvalue() const {
132  return ss;
133  }
134 
135  virtual bool evaluate() const {
136  // return true if ready to be read
137  return get() != SendNotReady;
138  }
139 
140  virtual void reset() {
141  if (_foo->isLoaded()) mrunner->removeFunction( _foo.get() );
142  maccept = false;
143  isqueued = false;
144  ss = SendFailure;
145  }
146 
148  {
149  return new CmdFunction( minit->clone(), _foo, mrunner, mcaller );
150  }
151 
152  CmdFunction* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const
153  {
154  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
155  if ( alreadyCloned[this] == 0 ) {
156  boost::shared_ptr<ProgramInterface> fcpy( _foo->copy(alreadyCloned) );
157  alreadyCloned[this] = new CmdFunction( minit->copy(alreadyCloned), fcpy , mrunner, mcaller);
158  }
159 
160  assert ( dynamic_cast<CmdFunction*>( alreadyCloned[this] ) == static_cast<CmdFunction*>( alreadyCloned[this] ) );
161  return static_cast<CmdFunction*>( alreadyCloned[this] );
162  }
163 
164  };
165 
169  struct RTT_SCRIPTING_API CmdCollectCondition
170  : public ConditionInterface
171  {
173  public:
174 
176  collectds(ds)
177  {
178  }
179 
180  void reset() {}
181 
182  bool evaluate()
183  {
184  // We may not call collectds->evaluate() inhere, because this actively tries a collect,
185  // and if it would succeed, we would proceed immediately, without giving the vertex node
186  // a chance to do something with the return value of the cmd.
187  return collectds->rvalue() != SendNotReady;
188  }
189 
190  virtual CmdCollectCondition* clone() const
191  {
192  return new CmdCollectCondition( collectds );
193  }
195  std::map<
196  const base::DataSourceBase*,
197  base::DataSourceBase*>& alreadyCloned) const
198  {
199  return new CmdCollectCondition ( collectds->copy(alreadyCloned) );
200  }
201  };
202 }}
203 
204 #endif
virtual CmdCollectCondition * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
virtual SendStatus const & rvalue() const
CmdFunction * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
virtual bool removeFunction(base::ExecutableInterface *f)
boost::shared_ptr< ProgramInterface > _foo
Definition: CmdFunction.hpp:70
The base class for all internal data representations.
virtual CmdCollectCondition * clone() const
This interface represents the concept of a condition which can be evaluated and return true or false...
Definition: mystd.hpp:163
CmdFunction * clone() const
bool evaluate()
Evaluate the Condition and return the outcome.
virtual void readArguments()=0
base::ActionInterface * minit
Definition: CmdFunction.hpp:67
SendStatus
Definition: SendStatus.hpp:53
virtual DataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const =0
printstream cout
Definition: rtstreams.cpp:45
virtual SendStatus value() const
virtual ActionInterface * clone() const =0
virtual const_reference_t rvalue() const =0
virtual bool execute()=0
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
virtual bool evaluate() const
basic_ostreams & endl(basic_ostreams &s)
Definition: rtstreams.cpp:110
DataSource< SendStatus >::shared_ptr collectds
CmdFunction(base::ActionInterface *init_com, boost::shared_ptr< ProgramInterface > foo, ExecutionEngine *p, ExecutionEngine *caller)
Definition: CmdFunction.hpp:83
ExecutionEngine * mrunner
Definition: CmdFunction.hpp:68
CmdCollectCondition(DataSource< SendStatus >::shared_ptr ds)
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
ExecutionEngine * mcaller
Definition: CmdFunction.hpp:69
virtual bool runFunction(base::ExecutableInterface *f)
void foo(double d)
virtual ActionInterface * copy(std::map< const DataSourceBase *, DataSourceBase * > &alreadyCloned) const


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:21