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_TASK_COMMAND_HPP
00040 #define ORO_TASK_COMMAND_HPP
00041
00042 #include <string>
00043 #include <boost/static_assert.hpp>
00044 #include <boost/type_traits/is_same.hpp>
00045 #include "CommandBase.hpp"
00046 #include "LocalCommand.hpp"
00047 #include "UnMember.hpp"
00048
00049 namespace RTT
00050 {
00092 template<class CommandT>
00093 class Command
00094 : public detail::InvokerSignature<boost::function_traits<CommandT>::arity, CommandT, detail::CommandBase<CommandT>* >
00095 {
00096 protected:
00097 std::string mname;
00098 typedef detail::InvokerSignature<boost::function_traits<CommandT>::arity, CommandT, detail::CommandBase<CommandT>* > Base;
00099
00100 BOOST_STATIC_ASSERT(( boost::is_same<typename boost::function_traits<CommandT>::result_type,bool>::value ));
00101 public:
00102 typedef CommandT Signature;
00103
00107 typedef DispatchInterface::Status Status;
00108
00114 Command()
00115 : Base(0), mname()
00116 {}
00117
00124 Command(std::string name)
00125 : Base(0), mname(name)
00126 {}
00127
00131 Command(const Command& c)
00132 : Base( c.impl ? c.impl->cloneI() : 0), mname(c.mname)
00133 {
00134 }
00135
00139 Command& operator=(const Command& c)
00140 {
00141 if ( &c == this )
00142 return *this;
00143 this->mname = c.mname;
00144 delete this->impl;
00145 this->impl = 0;
00146 if ( c.impl )
00147 this->impl = c.impl->cloneI();
00148 return *this;
00149 }
00150
00162 template<class CommandF, class ConditionF, class ObjectT>
00163 Command(std::string name, CommandF com, ConditionF con, ObjectT t, bool invert = false)
00164 : Base( new detail::LocalCommand<CommandT>(com,con,t, invert)),
00165 mname(name)
00166 {
00167 }
00168
00182 template<class CommandF, class ConditionF, class ObjectT>
00183 Command(std::string name, CommandF com, ConditionF con, ObjectT t, CommandProcessor* commandp, bool invert = false)
00184 : Base( new detail::LocalCommand<CommandT>(com,con,t,commandp, invert)),
00185 mname(name)
00186 {
00187 }
00188
00198 template<class CommandF, class ConditionF>
00199 Command(std::string name, CommandF com, ConditionF con, CommandProcessor* commandp, bool invert = false)
00200 : Base( new detail::LocalCommand<CommandT>(com,con,commandp, invert)),
00201 mname(name)
00202 {
00203 }
00204
00212 Command(DispatchInterface* implementation)
00213 : Base( dynamic_cast< detail::CommandBase<CommandT>* >(implementation) ),
00214 mname()
00215 {
00216
00217 if ( !this->impl && implementation) {
00218 log(Error) << "Tried to assign Command from incompatible type."<< endlog();
00219 delete implementation;
00220 }
00221 }
00222
00226 ~Command()
00227 {
00228 delete this->impl;
00229 }
00230
00238 Command& operator=(DispatchInterface* implementation)
00239 {
00240 if ( this->impl && this->impl == implementation)
00241 return *this;
00242 delete this->impl;
00243 this->impl = dynamic_cast< detail::CommandBase<CommandT>* >(implementation);
00244 if (this->impl == 0 && implementation) {
00245 log(Error) << "Tried to assign Command from incompatible type."<< endlog();
00246 delete implementation;
00247 }
00248 return *this;
00249 }
00250
00256 bool ready() const {
00257 return this->impl && this->impl->ready();
00258 }
00259
00264 bool done() const {
00265 if (!this->impl) return false;
00266 return this->impl->done();
00267 }
00268
00273 void reset() {
00274 if (!this->impl) return;
00275 return this->impl->reset();
00276 }
00277
00282 bool sent() const {
00283 if (!this->impl) return false;
00284 return this->impl->sent();
00285 }
00286
00292 bool accepted() const {
00293 if (!this->impl) return false;
00294 return this->impl->accepted();
00295 }
00296
00301 bool executed() const {
00302 if (!this->impl) return false;
00303 return this->impl->executed();
00304 }
00305
00310 bool valid() const {
00311 if (!this->impl) return false;
00312 return this->impl->valid();
00313 }
00314
00320 const std::string& getName() const {
00321 return mname;
00322 }
00323
00331 detail::CommandBase<CommandT>* getCommandImpl() const {
00332 return this->impl;
00333 }
00334
00340 void setCommandImpl(detail::CommandBase<CommandT>* new_impl) const {
00341 delete this->impl;
00342 return this->impl = new_impl;
00343 }
00344 };
00345
00362 template<class ComF, class ConF, class Object>
00363 Command< typename detail::UnMember<ComF>::type > command(std::string name, ComF command, ConF condition, Object object, bool invert = false) {
00364 return Command< typename detail::UnMember<ComF>::type >(name, command, condition, object, invert);
00365 }
00366
00382 template<class ComF, class ConF, class Object>
00383 Command< typename detail::UnMember<ComF>::type > command(std::string name, ComF command, ConF condition, Object object, CommandProcessor* cp, bool invert = false) {
00384 return Command< typename detail::UnMember<ComF>::type >(name, command, condition, object, cp, invert);
00385 }
00386
00400 template<class ComF, class ConF>
00401 Command< typename detail::UnMember<ComF>::type > command(std::string name, ComF command, ConF condition, CommandProcessor* cp, bool invert = false) {
00402 return Command< typename detail::UnMember<ComF>::type >(name, command, condition, cp, invert);
00403 }
00404
00405 }
00406 #endif