DLibCommand.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 ORO_DLIB_COMMAND_HPP
00040 #define ORO_DLIB_COMMAND_HPP
00041
00042 #include <boost/function.hpp>
00043 #include <boost/bind.hpp>
00044 #include <boost/mem_fn.hpp>
00045 #include <string>
00046 #include "../DispatchInterface.hpp"
00047 #include "../CommandFunctors.hpp"
00048 #include "../Invoker.hpp"
00049 #include "../CommandBase.hpp"
00050
00051 namespace RTT
00052 {
00058 namespace DLib
00059 {
00073 template<class CommandT, class ProtocolT>
00074 class DLibCommandImpl
00075 : public detail::CommandBase<CommandT>
00076 {
00077 protected:
00078 int id;
00079 ProtocolT* protocol;
00080 public:
00084 DLibCommandImpl()
00085 : id(0), protocol(0)
00086 {
00087 }
00088
00094 bool invoke() {
00095 if (id && (this->protocol->getCommandStatus(id) & ( DispatchInterface::Ready | DispatchInterface::Done) ) )
00096 return protocol->sendCommand(id);
00097 return false;
00098 }
00099
00100 template<class T1>
00101 bool invoke( T1 a1 ) {
00102 if (id && (this->protocol->getCommandStatus(id) & ( DispatchInterface::Ready | DispatchInterface::Done) ) )
00103 return protocol->sendCommand(id, a1);
00104 return false;
00105 }
00106
00107 template<class T1, class T2>
00108 bool invoke( T1 a1, T2 a2 ) {
00109 if (id && (this->protocol->getCommandStatus(id) & ( DispatchInterface::Ready | DispatchInterface::Done) ) )
00110 return protocol->sendCommand(id, a1, a2);
00111 return false;
00112 }
00113
00114 template<class T1, class T2, class T3>
00115 bool invoke( T1 a1, T2 a2, T3 a3 ) {
00116 if (id && (this->protocol->getCommandStatus(id) & ( DispatchInterface::Ready | DispatchInterface::Done) ) )
00117 return protocol->sendCommand(id, a1, a2, a3);
00118 return false;
00119 }
00120
00121 template<class T1, class T2, class T3, class T4>
00122 bool invoke( T1 a1, T2 a2, T3 a3, T4 a4 ) {
00123 if (id && (this->protocol->getCommandStatus(id) & ( DispatchInterface::Ready | DispatchInterface::Done) ) )
00124 return protocol->sendCommand(id, a1, a2, a3, a4);
00125 return false;
00126 }
00127 };
00128
00129
00138 template<class CommandT, class ProtocolT>
00139 class DLibCommand
00140 : public detail::Invoker<CommandT,DLibCommandImpl<CommandT,ProtocolT> >
00141 {
00142 public:
00143 typedef CommandT Signature;
00144
00151 DLibCommand(std::string component, std::string name, ProtocolT* _protocol)
00152 {
00153 this->protocol = _protocol;
00154 this->id = this->protocol->getCommandId(component,name);
00155 if (this->id == 0) {
00156 log(Error) << "Could not find Component '"<<component <<"' or Command '"<<name<<"' in that component."<<endlog();
00157 }
00158 }
00159
00160 virtual void readArguments() {}
00161
00162 virtual bool ready() const {
00163 unsigned int st = this->protocol->getCommandStatus(this->id);
00164 return st == DispatchInterface::Ready || st == DispatchInterface::Done;
00165 }
00166
00167 virtual bool dispatch() {
00168 return false;
00169 }
00170
00171 virtual bool execute() {
00172 return false;
00173 }
00174
00175 virtual bool done() const {
00176 return this->protocol->getCommandStatus(this->id) == DispatchInterface::Done;
00177 }
00178
00179 virtual void reset() {
00180 return this->protocol->resetCommand(this->id);
00181 }
00182
00183 virtual bool sent() const {
00184 return this->protocol->getCommandStatus(this->id) >= DispatchInterface::Sent;
00185 }
00186
00187 virtual bool accepted() const {
00188 return this->protocol->getCommandStatus(this->id) >= DispatchInterface::Accepted;
00189 }
00190
00191 virtual bool executed() const {
00192 return this->protocol->getCommandStatus(this->id) >= DispatchInterface::Executed;
00193 }
00194
00195 virtual bool valid() const {
00196 return this->protocol->getCommandStatus(this->id) >= DispatchInterface::Valid;
00197 }
00198
00199 virtual ConditionInterface* createCondition() const
00200 {
00201 return new detail::ConditionFunctor<bool(void)>( boost::bind<bool>( boost::mem_fn(&DLibCommand::done), this) );
00202 }
00203
00211 virtual DispatchInterface* clone() const {
00212 return new DLibCommand(*this);
00213 }
00214
00215 virtual detail::CommandBase<CommandT>* cloneI() const {
00216 return new DLibCommand(*this);
00217 }
00218 };
00219 }
00220 }
00221 #endif