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 }
ConditionInterface * clone() const
Definition: TryCommand.cpp:123
virtual result_t get() const =0
internal::DataSource< bool >::shared_ptr c
Definition: TryCommand.hpp:99
base::ActionInterface * c
Definition: TryCommand.hpp:65
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
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
Definition: TryCommand.cpp:127
virtual void set(param_t t)=0
virtual void readArguments()=0
virtual bool valid() const
TryCommandResult(internal::DataSource< bool >::shared_ptr ec, bool invert)
Definition: TryCommand.cpp:111
bool evaluate()
Evaluate the Condition and return the outcome.
Definition: TryCommand.cpp:118
TryCommand * clone() const
Definition: TryCommand.cpp:100
base::ActionInterface * clone() const
Definition: TryCommand.cpp:156
bool evaluate()
Evaluate the Condition and return the outcome.
Definition: TryCommand.cpp:173
virtual DataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const =0
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...
internal::DataSource< bool >::shared_ptr c
Definition: TryCommand.hpp:160
ConditionInterface * clone() const
Definition: TryCommand.cpp:177
internal::AssignableDataSource< bool >::shared_ptr result()
Definition: TryCommand.cpp:96
internal::AssignableDataSource< bool >::shared_ptr _result
Definition: TryCommand.hpp:64
TryCommand(base::ActionInterface *command, internal::AssignableDataSource< bool >::shared_ptr storage=0)
Definition: TryCommand.cpp:48
internal::AssignableDataSource< bool >::shared_ptr cache()
Definition: TryCommand.cpp:152
base::ActionInterface * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Definition: TryCommand.cpp:161
internal::AssignableDataSource< bool >::shared_ptr _cache
Definition: TryCommand.hpp:131
TryCommand * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Definition: TryCommand.cpp:105
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:53
ConditionInterface * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Definition: TryCommand.cpp:181
internal::DataSource< bool >::shared_ptr _ds
Definition: TryCommand.hpp:133
virtual AssignableDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const =0
virtual bool evaluate() const
virtual ActionInterface * copy(std::map< const DataSourceBase *, DataSourceBase * > &alreadyCloned) const


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:44