DLibCommand.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:04 CET 2006  DLibCommand.hpp
00003 
00004                         DLibCommand.hpp -  description
00005                            -------------------
00006     begin                : do november 02 2006
00007     copyright            : (C) 2006 FMTC
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 #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
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3