DataSourceArgsCommand.hpp

00001 /***************************************************************************
00002   tag: FMTC  do nov 2 13:06:11 CET 2006  DataSourceArgsCommand.hpp
00003 
00004                         DataSourceArgsCommand.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_DATASOURCE_ARGS_COMMAND_HPP
00040 #define ORO_DATASOURCE_ARGS_COMMAND_HPP
00041 
00042 #include "CommandFunctors.hpp"
00043 #include "CommandProcessor.hpp"
00044 #include "CommandDSFunctors.hpp"
00045 #include "DispatchInterface.hpp"
00046 
00047 namespace RTT
00048 {
00049     namespace detail
00050     {
00055         template<class CommandT,
00056                  class CommandF = detail::Functor<CommandT> >
00057         class DataSourceArgsCommand
00058             : public DispatchInterface
00059         {
00060             CommandF mcom;
00061             mutable CommandF mcon;
00062             CommandProcessor* mcp;
00063             bool minvoked, maccept, mvalid, mexec, minvert;
00064         public:
00065             typedef boost::function_traits<CommandT> traits;
00066             typedef CommandT Signature;
00067             typedef bool result_type;
00068 
00069             DataSourceArgsCommand(boost::function<CommandT> com, boost::function<CommandT> con, CommandProcessor* cp, bool inverted)
00070                 : mcom( com ), mcon( con ),
00071                   mcp( cp ),
00072                   minvoked(false), maccept(false),
00073                   mvalid(false), mexec(false), minvert(inverted)
00074             {
00075             }
00076 
00077             DataSourceArgsCommand(CommandF com, CommandF con, CommandProcessor* cp, bool inverted)
00078                 : mcom( com ), mcon( con ),
00079                   mcp( cp ),
00080                   minvoked(false), maccept(false),
00081                   mvalid(false), mexec(false), minvert(inverted)
00082             {
00083             }
00084 
00085             DataSourceArgsCommand<CommandT,CommandF>* create() const
00086             {
00087                 return clone();
00088             }
00089 
00090             template<class Arg1T>
00091             DataSourceArgsCommand<CommandT,CommandF>* create(DataSource<Arg1T>* a1) const
00092             {
00093                 DataSourceArgsCommand<CommandT,CommandF>* r =  this->clone();
00094                 r->mcom.setArguments(a1);
00095                 r->mcon.setArguments(a1);
00096                 return r;
00097             }
00098 
00099             template<class Arg1T, class Arg2T>
00100             DataSourceArgsCommand<CommandT,CommandF>* create(DataSource<Arg1T>* a1, DataSource<Arg2T>* a2) const
00101             {
00102                 DataSourceArgsCommand<CommandT,CommandF>* r =  this->clone();
00103                 r->mcom.setArguments(a1, a2);
00104                 r->mcon.setArguments(a1, a2);
00105                 return r;
00106             }
00107 
00108             template<class Arg1T, class Arg2T, class Arg3T>
00109             DataSourceArgsCommand<CommandT,CommandF>* create(DataSource<Arg1T>* a1, DataSource<Arg2T>* a2, DataSource<Arg3T>* a3) const
00110             {
00111                 DataSourceArgsCommand<CommandT,CommandF>* r =  this->clone();
00112                 r->mcom.setArguments(a1, a2, a3);
00113                 r->mcon.setArguments(a1, a2, a3);
00114                 return r;
00115             }
00116 
00117             template<class Arg1T, class Arg2T, class Arg3T, class Arg4T>
00118             DataSourceArgsCommand<CommandT,CommandF>* create(DataSource<Arg1T>* a1, DataSource<Arg2T>* a2, DataSource<Arg3T>* a3, DataSource<Arg4T>* a4) const
00119             {
00120                 DataSourceArgsCommand<CommandT,CommandF>* r =  this->clone();
00121                 r->mcom.setArguments(a1, a2, a3, a4);
00122                 r->mcon.setArguments(a1, a2, a3, a4);
00123                 return r;
00124             }
00125 
00126             bool ready() const {
00127                 return !minvoked;
00128             }
00129 
00130             virtual bool dispatch() {
00131                 if (minvoked)
00132                     return false;
00133                 minvoked = true;
00134                 this->readArguments();
00135                 return maccept = (mcp->process( this ) != 0);
00136             }
00137 
00138             virtual void readArguments() { mcom.readArguments(); }
00139 
00140             virtual bool execute() {
00141                 mvalid = mcom.execute();
00142                 mexec = true;
00143                 return mvalid;
00144             }
00145 
00146             virtual bool done() const {
00147                 if (mexec && mvalid )
00148                     return mcon.evaluate() != minvert;
00149                 return false;
00150             }
00151 
00152             virtual void reset() {
00153                 minvoked = (false);
00154                 maccept = (false);
00155                 mvalid = (false);
00156                 mexec = (false);
00157             }
00158 
00159             virtual bool sent() const {
00160                 return minvoked;
00161             }
00162 
00163             virtual bool accepted() const {
00164                 return maccept;
00165             }
00166 
00167             virtual bool executed() const {
00168                 return mexec;
00169             }
00170 
00171             virtual bool valid() const {
00172                 return mvalid;
00173             }
00174 
00175             virtual ConditionInterface* createCondition() const
00176             {
00177                 return new detail::ConditionFunctor<CommandT,CommandF>(mcon, minvert);
00178             }
00179 
00180             virtual DataSourceArgsCommand<CommandT,CommandF>* clone() const {
00181                 return new DataSourceArgsCommand<CommandT,CommandF>(*this);
00182             }
00183 
00184             virtual DataSourceArgsCommand<CommandT,CommandF>* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00185                 return new DataSourceArgsCommand<CommandT,CommandF>(CommandF(mcom.copy(alreadyCloned)),
00186                                                                     CommandF(mcon.copy(alreadyCloned)),mcp, minvert);
00187             }
00188 
00189         };
00190     }
00191 }
00192 
00193 #endif
Generated on Thu Dec 23 13:22:37 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3