Orocos Real-Time Toolkit  2.8.3
CallFunction.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 CallFunction.hpp
3 
4  CallFunction.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
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  * 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_CALL_FUNCTION_HPP
40 #define ORO_CALL_FUNCTION_HPP
41 
42 #include "../base/ActionInterface.hpp"
43 #include "../base/DisposableInterface.hpp"
44 #include "../internal/DataSources.hpp"
45 #include "ProgramInterface.hpp"
46 #include "../internal/DataSource.hpp"
47 #include "../ExecutionEngine.hpp"
48 #include <boost/shared_ptr.hpp>
49 #include <boost/bind.hpp>
50 
51 namespace RTT
52 { namespace scripting {
53  using namespace detail;
54 
63  : public base::ActionInterface
64  {
65  base::ActionInterface* minit;
66  ExecutionEngine* mrunner;
67  ExecutionEngine* mcaller;
76  boost::shared_ptr<ProgramInterface> _foo;
77  bool isqueued;
78  bool maccept;
79 
80  bool fooDone() {
81  return _foo->inError() || _foo->isStopped();
82  }
83  public:
93  boost::shared_ptr<ProgramInterface> foo,
94  ExecutionEngine* p, ExecutionEngine* caller,
97  : minit(init_com),
98  mrunner(p), mcaller(caller),
99  _v( v==0 ? new internal::UnboundDataSource< internal::ValueDataSource<ProgramInterface*> >(foo.get()) : v ),
100  _foo( foo ), isqueued(false), maccept(false)
101  {
102  }
103 
105  this->reset();
106  }
107 
108  virtual bool execute() {
109  // this is asyn behaviour :
110  if (isqueued == false ) {
111  isqueued = true;
112  maccept = minit->execute() && mrunner->runFunction( _foo.get() );
113  // we ignore the ret value of start(). It could have been auto-started during loading() of the function.
114  if ( _foo->needsStart() ) // _foo might be auto-started in runFunction()
115  _foo->start();
116  if ( maccept ) {
117  // block for the result: foo stopped or in error
118  //mcaller->waitForFunctions(boost::bind(&CallFunction::fooDone,this) );
119  mrunner->waitForFunctions(boost::bind(&CallFunction::fooDone,this) );
120  if ( _foo->inError() ) {
121  throw false;
122  }
123  return true;
124  }
125  return false;
126  }
127  return true;
128  }
129 
130  virtual void reset() {
131  if (_foo->isLoaded()) mrunner->removeFunction( _foo.get() );
132  maccept = false;
133  isqueued = false;
134  }
135 
136  virtual bool valid() const {
137  return maccept;
138  }
139 
140  virtual void readArguments() {
141  // is called before runFunction is executed.
142  minit->readArguments();
143  }
144 
146  {
147  // _v is shared_ptr, so don't clone.
148  return new CallFunction( minit->clone(), _foo, mrunner, mcaller, _v.get() );
149  }
150 
151  base::ActionInterface* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const
152  {
153  // this may seem strange, but :
154  // make a copy of foo (a function), make a copy of _v (a datasource), store pointer to new foo in _v !
155  boost::shared_ptr<ProgramInterface> fcpy( _foo->copy(alreadyCloned) );
156  internal::AssignableDataSource<ProgramInterface*>* vcpy = _v->copy(alreadyCloned);
157  vcpy->set( fcpy.get() ); // since we own _foo, we may manipulate the copy of _v
158  return new CallFunction( minit->copy(alreadyCloned), fcpy , mrunner, mcaller, vcpy );
159  }
160 
161  };
162 
163 }}
164 
165 #endif
base::ActionInterface * clone() const
The Clone Software Pattern.
virtual result_t get() const =0
Return the data as type T.
virtual bool removeFunction(base::ExecutableInterface *f)
Remove a running function added with runFunction.
virtual bool execute()
Execute the functionality of this action.
#define RTT_SCRIPTING_API
void waitForFunctions(const boost::function< bool(void)> &pred)
Call this if you wish to block on a function completing in the Execution Engine.
An execution engine serialises (executes one after the other) the execution of all commands...
virtual bool valid() const
Inspect if this action was executed and valid.
virtual void readArguments()
This is invoked some time before execute() at a time when the action may read its function arguments...
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
base::ActionInterface * 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...
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Use this type to store a pointer to an AssignableDataSource.
Definition: DataSource.hpp:198
A special DataSource only to be used for if you understand the copy()/clone() semantics very well...
A DataSource which has set() methods.
Definition: DataSource.hpp:184
virtual void reset()
Reset this action.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
An action which calls a FunctionFraph for execution in a ExecutionEngine.
virtual bool runFunction(base::ExecutableInterface *f)
Run a given function in step() or loop().
A Program represents a collection of instructions that can be stepwise executed.
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
virtual AssignableDataSource< 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.
CallFunction(base::ActionInterface *init_com, boost::shared_ptr< ProgramInterface > foo, ExecutionEngine *p, ExecutionEngine *caller, internal::AssignableDataSource< ProgramInterface * > *v=0, internal::AssignableDataSource< bool > *a=0)
Create a Command to send a function to a ExecutionEngine.