AssignCommand.hpp
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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