CommandExecFunction.hpp
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 #ifndef COMMAND_EXEC_FUNCTION_HPP
00040 #define COMMAND_EXEC_FUNCTION_HPP
00041
00042 #include "ConditionInterface.hpp"
00043 #include "CommandInterface.hpp"
00044 #include "DataSources.hpp"
00045 #include "ProgramInterface.hpp"
00046 #include "ProgramProcessor.hpp"
00047 #include "DispatchInterface.hpp"
00048 #include "DataSource.hpp"
00049 #include <boost/shared_ptr.hpp>
00050
00051 namespace RTT
00052 {
00056 class ConditionExecFunction
00057 : public ConditionInterface
00058 {
00059 DataSource<ProgramInterface*>::shared_ptr _v;
00060 public:
00061 ConditionExecFunction( DataSource<ProgramInterface*>* v)
00062 : _v( v )
00063 {}
00064
00065 bool evaluate()
00066 {
00067 return _v->get()->isStopped();
00068 }
00069
00070 ConditionInterface* clone() const
00071 {
00072 return new ConditionExecFunction( _v.get() );
00073 }
00074
00075 ConditionInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00076 {
00077
00078
00079 return new ConditionExecFunction( _v->copy( alreadyCloned ) );
00080 }
00081
00082 };
00083
00089 class CommandExecFunction
00090 : public DispatchInterface
00091 {
00092 CommandInterface* minit;
00093 ProgramProcessor* _proc;
00094 AssignableDataSource<ProgramInterface*>::shared_ptr _v;
00095 boost::shared_ptr<ProgramInterface> _foo;
00096 bool isqueued;
00097 AssignableDataSource<bool>::shared_ptr maccept;
00098 public:
00107 CommandExecFunction( CommandInterface* init_com, boost::shared_ptr<ProgramInterface> foo, ProgramProcessor* p, AssignableDataSource<ProgramInterface*>* v = 0 , AssignableDataSource<bool>* a = 0 )
00108 : minit(init_com),
00109 _proc(p),
00110 _v( v==0 ? new detail::UnboundDataSource< ValueDataSource<ProgramInterface*> >(foo.get()) : v ),
00111 _foo( foo ), isqueued(false), maccept( a ? a : new detail::UnboundDataSource<ValueDataSource<bool> >(false) )
00112 {
00113 }
00114
00115 ~CommandExecFunction() {
00116 if ( _foo->isRunning() )
00117 log(Critical) << "Destroying Function running in ProgramProcessor !" << endlog();
00118 this->reset();
00119 }
00120
00121 void readArguments()
00122 {
00123 minit->readArguments();
00124 }
00125
00126 bool ready() const {
00127 return !isqueued;
00128 }
00129
00130 bool dispatch()
00131 {
00132 return execute();
00133 }
00134
00135 bool execute()
00136 {
00137
00138 if (isqueued == false ) {
00139 isqueued = true;
00140 maccept->set( minit->execute() && _proc->runFunction( _foo.get() ) );
00141 return maccept->get();
00142 }
00143
00144
00145 return maccept->get() && ! _foo->inError();
00146 }
00147
00148 void reset()
00149 {
00150
00151 _foo->reset();
00152 minit->reset();
00153 isqueued = false;
00154
00155 _foo->stop();
00156 }
00157
00158 virtual bool sent() const {
00159 return isqueued;
00160 }
00161
00162 virtual bool accepted() const {
00163 return maccept->get();
00164 }
00165
00166 virtual bool executed() const {
00167 return isqueued;
00168 }
00169
00170 virtual bool valid() const {
00171 return maccept->get();
00172 }
00173
00174 virtual bool done() const {
00175 return maccept->get() && _v->get()->isStopped();
00176 }
00177
00181 ConditionInterface* createCondition() const
00182 {
00183 return new ConditionExecFunction( _v.get() );
00184 }
00185
00186 DispatchInterface* clone() const
00187 {
00188
00189 return new CommandExecFunction( minit->clone(), _foo, _proc, _v.get(), maccept.get() );
00190 }
00191
00192 DispatchInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00193 {
00194
00195
00196 boost::shared_ptr<ProgramInterface> fcpy( _foo->copy(alreadyCloned) );
00197 AssignableDataSource<ProgramInterface*>* vcpy = _v->copy(alreadyCloned);
00198 vcpy->set( fcpy.get() );
00199 AssignableDataSource<bool>* acpy = maccept->copy(alreadyCloned);
00200 return new CommandExecFunction( minit->copy(alreadyCloned), fcpy , _proc, vcpy, acpy );
00201 }
00202
00203 };
00204
00205 }
00206
00207 #endif