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 ORO_LOCAL_COMMAND_HPP
00040 #define ORO_LOCAL_COMMAND_HPP
00041
00042 #include <boost/function.hpp>
00043 #include <string>
00044 #include "DispatchInterface.hpp"
00045 #include "CommandProcessor.hpp"
00046 #include "CommandFunctors.hpp"
00047 #include "BindStorage.hpp"
00048 #include "Invoker.hpp"
00049 #include "CommandBase.hpp"
00050
00051 namespace RTT
00052 {
00053 namespace detail
00054 {
00061 template<class CommandT>
00062 class LocalCommandImpl
00063 : public CommandBase<CommandT>,
00064 protected detail::BindStorage<CommandT>
00065 {
00066 protected:
00067 CommandProcessor* mcp;
00068
00069 bool minvoked, maccept, mvalid, mexec, minvert;
00070 public:
00074 LocalCommandImpl()
00075 : mcp(0),
00076 minvoked(false), maccept(false),
00077 mvalid(false), mexec(false), minvert(false)
00078 {}
00079
00080 CommandProcessor* getCommandProcessor() const { return mcp; }
00081
00082 bool isInverted() const { return minvert; }
00083
00089 bool invoke() {
00090 if (!mcp ||(minvoked && !this->done()) )
00091 return false;
00092 this->reset();
00093 minvoked = true;
00094 return maccept = (mcp->process( this ) != 0) ;
00095 }
00096
00097 template<class T1>
00098 bool invoke( T1 a1 ) {
00099 if (!mcp ||(minvoked && !this->done()) )
00100 return false;
00101 this->reset();
00102
00103 this->store( a1 );
00104 minvoked = true;
00105 return maccept = (mcp->process( this ) != 0);
00106 }
00107
00108 template<class T1, class T2>
00109 bool invoke( T1 a1, T2 a2 ) {
00110 if (!mcp ||(minvoked && !this->done()) )
00111 return false;
00112 this->reset();
00113
00114 this->store( a1, a2 );
00115 minvoked = true;
00116 return maccept = (mcp->process( this ) != 0);
00117 }
00118
00119 template<class T1, class T2, class T3>
00120 bool invoke( T1 a1, T2 a2, T3 a3 ) {
00121 if (!mcp ||(minvoked && !this->done()) )
00122 return false;
00123 this->reset();
00124
00125 this->store( a1, a2, a3 );
00126 minvoked = true;
00127 return maccept = (mcp->process( this ) != 0);
00128 }
00129
00130 template<class T1, class T2, class T3, class T4>
00131 bool invoke( T1 a1, T2 a2, T3 a3, T4 a4 ) {
00132 if (!mcp ||(minvoked && !this->done()) )
00133 return false;
00134 this->reset();
00135
00136 this->store( a1, a2, a3, a4 );
00137 minvoked = true;
00138 return maccept = (mcp->process( this ) != 0);
00139 }
00140 };
00141
00142
00149 template<class CommandT>
00150 class LocalCommand
00151 : public Invoker<CommandT,LocalCommandImpl<CommandT> >
00152 {
00153 public:
00154 typedef CommandT Signature;
00155
00167 template<class CommandF, class ConditionF, class ObjectT>
00168 LocalCommand(CommandF com, ConditionF con, ObjectT t, bool invert = false)
00169 {
00170 this->setup(com,con,t);
00171 this->minvert = invert;
00172 this->mcp = t->engine()->commands();
00173 }
00174
00188 template<class CommandF, class ConditionF, class ObjectT>
00189 LocalCommand(CommandF com, ConditionF con, ObjectT t, CommandProcessor* commandp, bool invert = false)
00190 {
00191 this->setup(com,con,t);
00192 this->minvert = invert;
00193 this->mcp = commandp;
00194 }
00195
00205 template<class CommandF, class ConditionF>
00206 LocalCommand(CommandF com, ConditionF con, CommandProcessor* commandp, bool invert = false)
00207 {
00208 this->setup(com,con);
00209 this->minvert = invert;
00210 this->mcp = commandp;
00211 }
00212
00213 virtual void readArguments() {}
00214
00215 virtual bool ready() const {
00216 return ( !this->minvoked || this->done() );
00217 }
00218
00219 virtual bool dispatch() {
00220 if (this->minvoked && !this->done() )
00221 return false;
00222 this->reset();
00223
00224 this->maccept = (this->mcp->process( this ) != 0);
00225 this->minvoked = true;
00226 return this->maccept;
00227 }
00228
00229 virtual bool execute() {
00230
00231 if ( this->mexec )
00232 return false;
00233 this->mvalid = this->exec();
00234 this->mexec = true;
00235 return this->mvalid;
00236 }
00237
00238 virtual bool done() const {
00239 if (this->mexec && this->mvalid )
00240 return this->check() != this->minvert;
00241 return false;
00242 }
00243
00244 virtual void reset() {
00245 this->minvoked = (false);
00246 this->maccept = (false);
00247 this->mvalid = (false);
00248 this->mexec = (false);
00249 }
00250
00251 virtual bool sent() const {
00252 return this->minvoked;
00253 }
00254
00255 virtual bool accepted() const {
00256 return this->maccept;
00257 }
00258
00259 virtual bool executed() const {
00260 return this->mexec;
00261 }
00262
00263 virtual bool valid() const {
00264 return this->mvalid;
00265 }
00266
00267 virtual ConditionInterface* createCondition() const
00268 {
00269
00270
00271 return new detail::ConditionFunctor<bool(void)>( boost::bind<bool>( boost::mem_fn(&LocalCommand::done), this) );
00272 }
00273
00281 virtual DispatchInterface* clone() const {
00282 return new LocalCommand(*this);
00283 }
00284
00285 virtual CommandBase<CommandT>* cloneI() const {
00286 return new LocalCommand(*this);
00287 }
00288
00289 boost::function<CommandT> getCommandFunction() const {
00290 return this->command();
00291 }
00292
00293 boost::function<CommandT> getConditionFunction() const {
00294 return this->condition();
00295 }
00296
00297 };
00298 }
00299 }
00300 #endif