LocalCommand.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:10 CET 2006  LocalCommand.hpp
00003 
00004                         LocalCommand.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_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()) ) // if invoked and not ready.
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()) ) // if invoked and not ready.
00100                     return false;
00101                 this->reset();
00102                 // bind types from Storage<Function>
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()) ) // if invoked and not ready.
00111                     return false;
00112                 this->reset();
00113                 // bind types from Storage<Function>
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()) ) // if invoked and not ready.
00122                     return false;
00123                 this->reset();
00124                 // bind types from Storage<Function>
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()) ) // if invoked and not ready.
00133                     return false;
00134                 this->reset();
00135                 // bind types from Storage<Function>
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() ) // if invoked and not ready.
00221                     return false;
00222                 this->reset();
00223                 // race: maccepted may become true only after process+step ran.
00224                 this->maccept = (this->mcp->process( this ) != 0);
00225                 this->minvoked = true;
00226                 return this->maccept;
00227             }
00228 
00229             virtual bool execute() {
00230                 // do not allow to execute twice
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                 // LocalCommands are not used by the Parser, so this method is actually
00270                 // not used within Orocos.
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
Generated on Thu Dec 23 13:22:38 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3