Go to the documentation of this file.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
00039 #ifndef ORO_ARRAYPARTDATASOURCE_HPP_
00040 #define ORO_ARRAYPARTDATASOURCE_HPP_
00041
00042 #include "DataSource.hpp"
00043 #include "NA.hpp"
00044
00045 namespace RTT
00046 {
00047 namespace internal
00048 {
00057 template<typename T>
00058 class ArrayPartDataSource
00059 : public AssignableDataSource<T>
00060 {
00061
00062 typename AssignableDataSource<T>::value_t* mref;
00063
00064 DataSource<unsigned int>::shared_ptr mindex;
00065
00066 base::DataSourceBase::shared_ptr mparent;
00067
00068 unsigned int mmax;
00069 public:
00070 ~ArrayPartDataSource() {}
00071
00072 typedef boost::intrusive_ptr<ArrayPartDataSource<T> > shared_ptr;
00073
00081 ArrayPartDataSource( typename AssignableDataSource<T>::reference_t ref,
00082 DataSource<unsigned int>::shared_ptr index,
00083 base::DataSourceBase::shared_ptr parent, unsigned int max )
00084 : mref(&ref), mindex(index), mparent(parent), mmax(max)
00085 {
00086 }
00087
00088 typename DataSource<T>::result_t get() const
00089 {
00090 unsigned int i = mindex->get();
00091 if (i >= mmax)
00092 return internal::NA<T>::na();
00093 return mref[ i ];
00094 }
00095
00096 typename DataSource<T>::result_t value() const
00097 {
00098 unsigned int i = mindex->get();
00099 if (i >= mmax)
00100 return internal::NA<T>::na();
00101 return mref[ i ];
00102 }
00103
00104 void set( typename AssignableDataSource<T>::param_t t )
00105 {
00106 unsigned int i = mindex->get();
00107 if (i >= mmax)
00108 return;
00109 mref[ i ] = t;
00110 updated();
00111 }
00112
00113 typename AssignableDataSource<T>::reference_t set()
00114 {
00115 unsigned int i = mindex->get();
00116 if (i >= mmax)
00117 return internal::NA<typename AssignableDataSource<T>::reference_t>::na();
00118 return mref[ i ];
00119 }
00120
00121 typename AssignableDataSource<T>::const_reference_t rvalue() const
00122 {
00123 unsigned int i = mindex->get();
00124 if (i >= mmax)
00125 return internal::NA<typename AssignableDataSource<T>::const_reference_t>::na();
00126 return mref[ i ];
00127 }
00128
00129 void updated() {
00130 if (mparent) mparent->updated();
00131 }
00132
00133 virtual ArrayPartDataSource<T>* clone() const {
00134 return new ArrayPartDataSource<T>( *mref, mindex, mparent, mmax);
00135 }
00136
00137 virtual ArrayPartDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
00138
00139 if ( replace[this] != 0 ) {
00140 assert ( dynamic_cast<ArrayPartDataSource<T>*>( replace[this] ) == static_cast<ArrayPartDataSource<T>*>( replace[this] ) );
00141 return static_cast<ArrayPartDataSource<T>*>( replace[this] );
00142 }
00143
00144
00145 assert( mparent->getRawPointer() != 0 && "Can't copy part of rvalue datasource.");
00146 if ( mparent->getRawPointer() == 0 )
00147 throw std::runtime_error("PartDataSource.hpp: Can't copy part of rvalue datasource.");
00148 base::DataSourceBase::shared_ptr mparent_copy = mparent->copy(replace);
00149
00150 int offset = reinterpret_cast<unsigned char*>( mref ) - reinterpret_cast<unsigned char*>(mparent->getRawPointer());
00151
00152 typename AssignableDataSource<T>::value_t* mref_copy = reinterpret_cast<typename AssignableDataSource<T>::value_t*>( reinterpret_cast<unsigned char*>(mparent_copy->getRawPointer()) + offset );
00153
00154 replace[this] = new ArrayPartDataSource<T>(*mref_copy, mindex->copy(replace), mparent_copy, mmax);
00155
00156 return static_cast<ArrayPartDataSource<T>*>(replace[this]);
00157
00158 }
00159 };
00160
00161 }
00162 }
00163
00164 #endif