Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039 #include <Logger.hpp>
00040 #include "TryCommand.hpp"
00041 #include "../internal/DataSources.hpp"
00042
00043 namespace RTT
00044 {
00045
00046 using namespace detail;
00047
00048 TryCommand::TryCommand( ActionInterface* command,
00049 AssignableDataSource<bool>::shared_ptr storage )
00050 :_result( storage == 0 ? new UnboundDataSource< ValueDataSource<bool> >(true) : storage ),
00051 c(command) {}
00052
00053 TryCommand::~TryCommand() {
00054 delete c;
00055 }
00056 bool TryCommand::execute() {
00057
00058
00059 if (_result->get() == false)
00060 return false;
00061 try {
00062 c->execute();
00063 } catch( ... ) {
00064 _result->set( false );
00065 return true;
00066 }
00067 _result->set( true );
00068 return true;
00069 }
00070 void TryCommand::reset() {
00071 c->reset();
00072 _result->set(true);
00073 }
00074
00075 bool TryCommand::valid() const {
00076
00077
00078
00079 return _result->get() == false || c->valid();
00080 }
00081
00082 void TryCommand::readArguments() {
00083 try {
00084 c->readArguments();
00085 } catch( ... ) {
00086 _result->set( false );
00087 return;
00088 }
00089 _result->set( true );
00090 }
00091
00092 ActionInterface* TryCommand::theCommand() const {
00093 return c;
00094 }
00095
00096 AssignableDataSource<bool>::shared_ptr TryCommand::result() {
00097 return _result;
00098 }
00099
00100 TryCommand* TryCommand::clone() const {
00101 return new TryCommand( c->clone(),
00102 _result );
00103 }
00104
00105 TryCommand* TryCommand::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00106 return new TryCommand( c->copy( alreadyCloned ),
00107 _result->copy(alreadyCloned));
00108 }
00109
00110
00111 TryCommandResult::TryCommandResult( DataSource<bool>::shared_ptr ec, bool invert)
00112 :c(ec), _invert(invert) {}
00113
00114 TryCommandResult::~TryCommandResult() {
00115
00116 }
00117
00118 bool TryCommandResult::evaluate() {
00119
00120 return _invert != c->get();
00121 }
00122
00123 ConditionInterface* TryCommandResult::clone() const {
00124 return new TryCommandResult( c, _invert );
00125 }
00126
00127 ConditionInterface* TryCommandResult::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00128 return new TryCommandResult( c->copy(alreadyCloned), _invert );
00129 }
00130
00131 EvalCommand::EvalCommand( DataSource<bool>::shared_ptr ds, AssignableDataSource<bool>::shared_ptr cache )
00132 :_cache( cache == 0 ? new UnboundDataSource<ValueDataSource<bool> >(false) : cache ),
00133 _ds(ds) {}
00134
00135 EvalCommand::~EvalCommand() {
00136 }
00137
00138 void EvalCommand::readArguments() {
00139 _ds->evaluate();
00140 }
00141
00142 bool EvalCommand::execute() {
00143 _cache->set( _ds->value() );
00144 return true;
00145 }
00146
00147 void EvalCommand::reset() {
00148 _cache->set(false);
00149 _ds->reset();
00150 }
00151
00152 AssignableDataSource<bool>::shared_ptr EvalCommand::cache() {
00153 return _cache;
00154 }
00155
00156 ActionInterface* EvalCommand::clone() const {
00157 return new EvalCommand( _ds,
00158 _cache );
00159 }
00160
00161 ActionInterface* EvalCommand::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00162 return new EvalCommand( _ds->copy( alreadyCloned ),
00163 _cache->copy(alreadyCloned) );
00164 }
00165
00166 EvalCommandResult::EvalCommandResult( DataSource<bool>::shared_ptr ec)
00167 :c(ec) {}
00168
00169 EvalCommandResult::~EvalCommandResult() {
00170
00171 }
00172
00173 bool EvalCommandResult::evaluate() {
00174 return c->get();
00175 }
00176
00177 ConditionInterface* EvalCommandResult::clone() const {
00178 return new EvalCommandResult( c );
00179 }
00180
00181 ConditionInterface* EvalCommandResult::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00182 return new EvalCommandResult( c->copy( alreadyCloned ) );
00183 }
00184
00185 }