AssignCommand.hpp

00001 /***************************************************************************
00002   tag: Peter Soetens  Mon Jun 26 13:25:56 CEST 2006  AssignCommand.hpp
00003 
00004                         AssignCommand.hpp -  description
00005                            -------------------
00006     begin                : Mon June 26 2006
00007     copyright            : (C) 2006 Peter Soetens
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 #include "DataSource.hpp"
00039 
00040 #ifndef ORO_ASSIGNCOMMAND_HPP
00041 #define ORO_ASSIGNCOMMAND_HPP
00042 
00043 #include "CommandInterface.hpp"
00044 
00045 namespace RTT
00046 {
00047     namespace detail {
00048 
00059         template<typename T, typename S = T>
00060         class AssignCommand
00061             : public CommandInterface
00062         {
00063         public:
00064             typedef typename AssignableDataSource<T>::shared_ptr LHSSource;
00065             typedef typename DataSource<S>::const_ptr RHSSource;
00066         private:
00067             LHSSource lhs;
00068             RHSSource rhs;
00069         public:
00073             AssignCommand( LHSSource l, RHSSource r )
00074                 : lhs( l ), rhs( r )
00075             {
00076             }
00077 
00078             void readArguments() {
00079                 rhs->evaluate();
00080             }
00081 
00082             bool execute()
00083             {
00084                 lhs->set( rhs->value() );
00085                 return true;
00086             }
00087 
00088             virtual CommandInterface* clone() const
00089             {
00090                 return new AssignCommand( lhs.get(), rhs.get() );
00091             }
00092 
00093             virtual CommandInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00094                 return new AssignCommand( lhs->copy( alreadyCloned ), rhs->copy( alreadyCloned ) );
00095             }
00096         };
00097 
00106         template<typename T, typename APred, typename S = T>
00107         class AssignContainerCommand
00108             : public CommandInterface
00109         {
00110             typedef typename AssignableDataSource<T>::shared_ptr LHSSource;
00111             LHSSource lhs;
00112             typedef typename DataSource<S>::const_ptr RHSSource;
00113             RHSSource rhs;
00114         public:
00115             AssignContainerCommand( LHSSource l, RHSSource r )
00116                 : lhs( l ), rhs( r )
00117             {
00118             }
00119 
00120             void readArguments() {
00121                 rhs->evaluate();
00122             }
00123 
00124             bool execute()
00125             {
00126                 if ( APred()( lhs->get(), rhs->value()) == false )
00127                     return false;
00128                 lhs->set( rhs->value() );
00129                 return true;
00130             }
00131 
00132             virtual CommandInterface* clone() const
00133             {
00134                 return new AssignContainerCommand( lhs.get(), rhs.get() );
00135             }
00136 
00137             virtual CommandInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00138                 return new AssignContainerCommand( lhs->copy( alreadyCloned ), rhs->copy( alreadyCloned ) );
00139             }
00140         };
00141 
00150         template<typename T, typename Index, typename SetType, typename Pred>
00151         class AssignIndexCommand
00152             : public CommandInterface
00153         {
00154             typedef typename DataSource<Index>::shared_ptr IndexSource;
00155             IndexSource i;
00156             typedef typename AssignableDataSource<T>::shared_ptr LHSSource;
00157             LHSSource lhs;
00158             typedef typename DataSource<SetType>::shared_ptr RHSSource;
00159             RHSSource rhs;
00160         public:
00161             AssignIndexCommand( LHSSource l, IndexSource index, RHSSource r)
00162                 : i(index),lhs( l ), rhs( r )
00163             {
00164             }
00165 
00166             void readArguments() {
00167                 rhs->evaluate();
00168             }
00169 
00170             bool execute()
00171             {
00172                 Index ind = i->get();
00173                 if ( Pred()( lhs->get(), ind) && &(lhs->set()) != 0 ) {
00174                     lhs->set()[ ind ] = rhs->value();
00175                     lhs->updated();
00176                     return true;
00177                 }
00178                 return false;
00179             }
00180 
00181             virtual CommandInterface* clone() const
00182             {
00183                 return new AssignIndexCommand( lhs.get(), i.get(), rhs.get() );
00184             }
00185 
00186             virtual CommandInterface* copy( std::map<const DataSourceBase*, DataSourceBase*>& alreadyCloned ) const {
00187                 return new AssignIndexCommand( lhs->copy( alreadyCloned ), i->copy( alreadyCloned ), rhs->copy( alreadyCloned ) );
00188             }
00189         };
00190     }
00191 }
00192 
00193 #endif
00194 
Generated on Thu Dec 23 13:22:36 2010 for Orocos Real-Time Toolkit by  doxygen 1.6.3