CommandExecFunction.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Tue Dec 21 22:43:07 CET 2004  CommandExecFunction.hpp
00003 
00004                         CommandExecFunction.hpp -  description
00005                            -------------------
00006     begin                : Tue December 21 2004
00007     copyright            : (C) 2004 Peter Soetens
00008     email                : peter.soetens@mech.kuleuven.ac.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 COMMAND_EXEC_FUNCTION_HPP
00040 #define COMMAND_EXEC_FUNCTION_HPP
00041 
00042 #include "ConditionInterface.hpp"
00043 #include "CommandInterface.hpp"
00044 #include "DataSources.hpp"
00045 #include "ProgramInterface.hpp"
00046 #include "ProgramProcessor.hpp"
00047 #include "DispatchInterface.hpp"
00048 #include "DataSource.hpp"
00049 #include <boost/shared_ptr.hpp>
00050 
00051 namespace RTT
00052 {
00056     class RTT_API ConditionExecFunction
00057         : public ConditionInterface
00058     {
00059         DataSource<ProgramInterface*>::shared_ptr _v;
00060     public:
00061         ConditionExecFunction( DataSource<ProgramInterface*>* v)
00062             : _v( v )
00063         {}
00064 
00065         bool evaluate()
00066         {
00067             return _v->get()->isStopped();
00068         }
00069 
00070         ConditionInterface* clone() const
00071         {
00072             return new ConditionExecFunction( _v.get() );
00073         }
00074 
00075         ConditionInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00076         {
00077             // after *all* the copying is done, _v will be set to the correct function
00078             // by the Command's copy.
00079             return new ConditionExecFunction( _v->copy( alreadyCloned ) );
00080         }
00081 
00082     };
00083 
00089     class RTT_API CommandExecFunction
00090         : public DispatchInterface
00091     {
00092         CommandInterface* minit;
00093         ProgramProcessor* _proc;
00094         AssignableDataSource<ProgramInterface*>::shared_ptr _v;
00095         boost::shared_ptr<ProgramInterface> _foo;
00096         bool isqueued;
00097         AssignableDataSource<bool>::shared_ptr maccept;
00098     public:
00107         CommandExecFunction( CommandInterface* init_com, boost::shared_ptr<ProgramInterface> foo, ProgramProcessor* p, AssignableDataSource<ProgramInterface*>* v = 0 , AssignableDataSource<bool>* a = 0 );
00108 
00109         ~CommandExecFunction();
00110 
00111         void readArguments()
00112         {
00113             minit->readArguments();
00114         }
00115 
00116         bool ready() const {
00117             return !isqueued;
00118         }
00119 
00120         bool dispatch()
00121         {
00122             return execute();
00123         }
00124 
00125         bool execute()
00126         {
00127             // this is asyn behaviour :
00128             if (isqueued == false ) {
00129                 isqueued = true;
00130                 maccept->set( minit->execute() && _proc->runFunction( _foo.get() ) );
00131                 return maccept->get();
00132             }
00133             // if it was queued already return if it is
00134             // in error or not.
00135             return maccept->get() && ! _foo->inError();
00136         }
00137 
00138         void reset()
00139         {
00140             // reset the program, so that it is valid to be re-queued again
00141             _foo->reset();
00142             minit->reset();
00143             isqueued = false;
00144             //remove any old left-overs.
00145             _foo->stop();
00146         }
00147 
00148         virtual bool sent() const {
00149             return isqueued;
00150         }
00151 
00152         virtual bool accepted() const {
00153             return maccept->get();
00154         }
00155 
00156         virtual bool executed() const {
00157             return isqueued;
00158         }
00159 
00160         virtual bool valid() const {
00161             return maccept->get() && ! _foo->inError();
00162         }
00163 
00164         virtual bool done() const {
00165             return maccept->get() && _v->get()->isStopped();
00166         }
00167 
00171         ConditionInterface* createCondition() const
00172         {
00173             return new ConditionExecFunction( _v.get() );
00174         }
00175 
00176         DispatchInterface* clone() const
00177         {
00178             // _v is shared_ptr, so don't clone.
00179             return new CommandExecFunction( minit->clone(), _foo, _proc, _v.get(), maccept.get() );
00180         }
00181 
00182         DispatchInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const
00183         {
00184             // this may seem strange, but :
00185             // make a copy of foo (a function), make a copy of _v (a datasource), store pointer to new foo in _v !
00186             boost::shared_ptr<ProgramInterface> fcpy( _foo->copy(alreadyCloned) );
00187             AssignableDataSource<ProgramInterface*>* vcpy = _v->copy(alreadyCloned);
00188             vcpy->set( fcpy.get() ); // since we own _foo, we may manipulate the copy of _v
00189             AssignableDataSource<bool>* acpy = maccept->copy(alreadyCloned);
00190             return new CommandExecFunction( minit->copy(alreadyCloned), fcpy , _proc, vcpy, acpy );
00191         }
00192 
00193     };
00194 
00195 }
00196 
00197 #endif

Generated on Tue Jul 13 11:03:22 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.1