Orocos Real-Time Toolkit  2.6.0
TryCommand.cpp
00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:57 CEST 2006  TryCommand.cxx
00003 
00004                         TryCommand.cxx -  description
00005                            -------------------
00006     begin                : Mon June 26 2006
00007     copyright            : (C) 2006 Peter Soetens
00008     email                : peter.soetens@fmtc.be
00009 
00010  ***************************************************************************
00011  *   This library is free software; you can redistribute it and/or         *
00012  *   modify it under the terms of the GNU General Public                   *
00013  *   License as published by the Free Software Foundation;                 *
00014  *   version 2 of the License.                                             *
00015  *                                                                         *
00016  *   As a special exception, you may use this file as part of a free       *
00017  *   software library without restriction.  Specifically, if other files   *
00018  *   instantiate templates or use macros or inline functions from this     *
00019  *   file, or you compile this file and link it with other files to        *
00020  *   produce an executable, this file does not by itself cause the         *
00021  *   resulting executable to be covered by the GNU General Public          *
00022  *   License.  This exception does not however invalidate any other        *
00023  *   reasons why the executable file might be covered by the GNU General   *
00024  *   Public License.                                                       *
00025  *                                                                         *
00026  *   This library is distributed in the hope that it will be useful,       *
00027  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00028  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
00029  *   Lesser General Public License for more details.                       *
00030  *                                                                         *
00031  *   You should have received a copy of the GNU General Public             *
00032  *   License along with this library; if not, write to the Free Software   *
00033  *   Foundation, Inc., 59 Temple Place,                                    *
00034  *   Suite 330, Boston, MA  02111-1307  USA                                *
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 /*=0*/)
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             //Logger::In in("TryCommand");
00058             //Logger::log() <<Logger::RealTime << "execute()"<<Logger::endl;
00059             if (_result->get() == false) // has thrown in readArguments
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             // ok to check conditions if command is valid or it failed.
00077             // we assume here that c behaves as a DispatchAction:
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             // do not delete !
00116         }
00117 
00118         bool TryCommandResult::evaluate() {
00119             // by default true means reject
00120             return  _invert != c->get();
00121         }
00122 
00123         ConditionInterface* TryCommandResult::clone() const {
00124             return new TryCommandResult( c, _invert ); // do not clone c !
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 /*=0*/)
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             // do not delete !
00171         }
00172 
00173         bool EvalCommandResult::evaluate() {
00174             return c->get();
00175         }
00176 
00177         ConditionInterface* EvalCommandResult::clone() const {
00178             return new EvalCommandResult( c ); // do not clone c !
00179         }
00180 
00181         ConditionInterface* EvalCommandResult::copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00182             return new EvalCommandResult( c->copy( alreadyCloned ) );
00183         }
00184 
00185 }