ArrayPartDataSource.hpp
Go to the documentation of this file.
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  ArrayPartDataSource.hpp
00003 
00004                         ArrayPartDataSource.hpp -  description
00005                            -------------------
00006     begin                : Tue September 07 2010
00007     copyright            : (C) 2010 The SourceWorks
00008     email                : peter@thesourceworks.com
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_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             // a reference to a value_t
00062             typename AssignableDataSource<T>::value_t* mref;
00063             // [] index
00064             DataSource<unsigned int>::shared_ptr mindex;
00065             // parent data source, for updating after set().
00066             base::DataSourceBase::shared_ptr mparent;
00067             // safety: all indexes must be smaller than this.
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                 // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
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                 // Both this and mparent are copied, but the part must also copy reference mref from the new parent !
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                 // calculate the pointer offset in parent:
00150                 int offset = reinterpret_cast<unsigned char*>( mref ) - reinterpret_cast<unsigned char*>(mparent->getRawPointer());
00151                 // get the pointer to the new parent member:
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                 // returns copy
00156                 return static_cast<ArrayPartDataSource<T>*>(replace[this]);
00157 
00158             }
00159         };
00160 
00161     }
00162 }
00163 
00164 #endif /* ORO_ARRAYPARTDATASOURCE_HPP_ */


rtt
Author(s): RTT Developers
autogenerated on Sat Jun 8 2019 18:46:05