Orocos Real-Time Toolkit  2.9.0
TryCommand.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Peter Soetens Mon Jun 26 13:25:57 CEST 2006 TryCommand.cxx
3 
4  TryCommand.cxx - description
5  -------------------
6  begin : Mon June 26 2006
7  copyright : (C) 2006 Peter Soetens
8  email : peter.soetens@fmtc.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 #include <Logger.hpp>
40 #include "TryCommand.hpp"
41 #include "../internal/DataSources.hpp"
42 
43 namespace RTT
44 {
45 
46  using namespace detail;
47 
50  :_result( storage == 0 ? new UnboundDataSource< ValueDataSource<bool> >(true) : storage ),
51  c(command) {}
52 
54  delete c;
55  }
57  //Logger::In in("TryCommand");
58  //Logger::log() <<Logger::RealTime << "execute()"<<Logger::endl;
59  if (_result->get() == false) // has thrown in readArguments
60  return false;
61  try {
62  c->execute();
63  } catch( ... ) {
64  _result->set( false );
65  return true;
66  }
67  _result->set( true );
68  return true;
69  }
71  c->reset();
72  _result->set(true);
73  }
74 
75  bool TryCommand::valid() const {
76  // ok to check conditions if command is valid or it failed.
77  // we assume here that c behaves as a DispatchAction:
78 
79  return _result->get() == false || c->valid();
80  }
81 
83  try {
84  c->readArguments();
85  } catch( ... ) {
86  _result->set( false );
87  return;
88  }
89  _result->set( true );
90  }
91 
93  return c;
94  }
95 
97  return _result;
98  }
99 
101  return new TryCommand( c->clone(),
102  _result );
103  }
104 
105  TryCommand* TryCommand::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
106  return new TryCommand( c->copy( alreadyCloned ),
107  _result->copy(alreadyCloned));
108  }
109 
110 
112  :c(ec), _invert(invert) {}
113 
115  // do not delete !
116  }
117 
119  // by default true means reject
120  return _invert != c->get();
121  }
122 
124  return new TryCommandResult( c, _invert ); // do not clone c !
125  }
126 
127  ConditionInterface* TryCommandResult::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
128  return new TryCommandResult( c->copy(alreadyCloned), _invert );
129  }
130 
132  :_cache( cache == 0 ? new UnboundDataSource<ValueDataSource<bool> >(false) : cache ),
133  _ds(ds) {}
134 
136  }
137 
139  }
140 
142  _ds->evaluate();
143  _cache->set( _ds->rvalue() );
144  return true;
145  }
146 
148  _cache->set(false);
149  _ds->reset();
150  }
151 
153  return _cache;
154  }
155 
157  return new EvalCommand( _ds,
158  _cache );
159  }
160 
161  ActionInterface* EvalCommand::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
162  return new EvalCommand( _ds->copy( alreadyCloned ),
163  _cache->copy(alreadyCloned) );
164  }
165 
167  :c(ec) {}
168 
170  // do not delete !
171  }
172 
174  return c->get();
175  }
176 
178  return new EvalCommandResult( c ); // do not clone c !
179  }
180 
181  ConditionInterface* EvalCommandResult::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
182  return new EvalCommandResult( c->copy( alreadyCloned ) );
183  }
184 
185 }
DataSource is a base class representing a generic way to read data of type T.
Definition: DataSource.hpp:94
ConditionInterface * clone() const
The Clone Software Pattern.
Definition: TryCommand.cpp:123
virtual result_t get() const =0
Return the data as type T.
base::ActionInterface * theCommand() const
Definition: TryCommand.cpp:92
EvalCommand(internal::DataSource< bool >::shared_ptr ds, internal::AssignableDataSource< bool >::shared_ptr cache=0)
Definition: TryCommand.cpp:131
virtual void reset()
Reset this action.
bool execute()
Execute the functionality of this action.
Definition: TryCommand.cpp:56
EvalCommandResult(internal::DataSource< bool >::shared_ptr ec)
Definition: TryCommand.cpp:166
This interface represents the concept of a condition which can be evaluated and return true or false...
ConditionInterface * 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...
Definition: TryCommand.cpp:127
virtual void set(param_t t)=0
Set this DataSource with a value.
virtual void readArguments()=0
This is invoked some time before execute() at a time when the action may read its function arguments...
virtual bool valid() const
Inspect if this action was executed and valid.
void readArguments()
This is invoked some time before execute() at a time when the action may read its function arguments...
Definition: TryCommand.cpp:82
TryCommandResult(internal::DataSource< bool >::shared_ptr ec, bool invert)
Pass TryCommand::result() to the first parameter of the command you want to check.
Definition: TryCommand.cpp:111
void reset()
Reset this action.
Definition: TryCommand.cpp:147
bool evaluate()
Evaluate the Condition and return the outcome.
Definition: TryCommand.cpp:118
TryCommand * clone() const
The Clone Software Pattern.
Definition: TryCommand.cpp:100
virtual void reset()
Reset the data to initial values.
Definition: DataSource.cpp:87
base::ActionInterface * clone() const
The Clone Software Pattern.
Definition: TryCommand.cpp:156
bool evaluate()
Evaluate the Condition and return the outcome.
Definition: TryCommand.cpp:173
A command which tries another command and stores the result in a internal::DataSource<bool>.
Definition: TryCommand.hpp:59
bool execute()
Execute the functionality of this action.
Definition: TryCommand.cpp:141
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.
virtual ActionInterface * clone() const =0
The Clone Software Pattern.
void readArguments()
This is invoked some time before execute() at a time when the action may read its function arguments...
Definition: TryCommand.cpp:138
virtual const_reference_t rvalue() const =0
Get a const reference to the value of this DataSource.
virtual bool execute()=0
Execute the functionality of this action.
Based on the software pattern &#39;command&#39;, this interface allows execution of action objects...
ConditionInterface * clone() const
The Clone Software Pattern.
Definition: TryCommand.cpp:177
internal::AssignableDataSource< bool >::shared_ptr result()
Definition: TryCommand.cpp:96
TryCommand(base::ActionInterface *command, internal::AssignableDataSource< bool >::shared_ptr storage=0)
Try a command.
Definition: TryCommand.cpp:48
bool valid() const
Inspect if this action was executed and valid.
Definition: TryCommand.cpp:75
A special DataSource only to be used for if you understand the copy()/clone() semantics very well...
internal::AssignableDataSource< bool >::shared_ptr cache()
Definition: TryCommand.cpp:152
A DataSource which has set() methods.
Definition: DataSource.hpp:184
void reset()
Reset this action.
Definition: TryCommand.cpp:70
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...
Definition: TryCommand.cpp:161
TryCommand * 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...
Definition: TryCommand.cpp:105
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
virtual bool evaluate() const
Force an evaluation of the DataSourceBase.
Definition: DataSource.inl:52
ConditionInterface * 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...
Definition: TryCommand.cpp:181
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.
virtual ActionInterface * copy(std::map< const DataSourceBase *, DataSourceBase * > &alreadyCloned) const
When copying an Orocos program, we want identical internal::DataSource&#39;s to be mapped to identical Da...