Orocos Real-Time Toolkit  2.5.0
ArrayPartDataSource.hpp
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     {
00060         template<typename T>
00061         class ArrayPartDataSource
00062         : public AssignableDataSource<T>
00063         {
00064             // a reference to a value_t
00065             typename AssignableDataSource<T>::value_t* mref;
00066             // [] index
00067             DataSource<unsigned int>::shared_ptr mindex;
00068             // parent data source, for updating after set().
00069             base::DataSourceBase::shared_ptr mparent;
00070             // safety: all indexes must be smaller than this.
00071             unsigned int mmax;
00072         public:
00073             ~ArrayPartDataSource() {}
00074 
00075             typedef boost::intrusive_ptr<ArrayPartDataSource<T> > shared_ptr;
00076 
00084             ArrayPartDataSource( typename AssignableDataSource<T>::reference_t ref,
00085                                     DataSource<unsigned int>::shared_ptr index,
00086                                     base::DataSourceBase::shared_ptr parent, unsigned int max )
00087                 : mref(&ref), mindex(index), mparent(parent), mmax(max)
00088             {
00089             }
00090 
00091             typename DataSource<T>::result_t get() const
00092             {
00093                 unsigned int i = mindex->get();
00094                 if (i >= mmax)
00095                     return internal::NA<T>::na();
00096                 return mref[ i ];
00097             }
00098 
00099             typename DataSource<T>::result_t value() const
00100             {
00101                 unsigned int i = mindex->get();
00102                 if (i >= mmax)
00103                     return internal::NA<T>::na();
00104                 return mref[ i ];
00105             }
00106 
00107             void set( typename AssignableDataSource<T>::param_t t )
00108             {
00109                 unsigned int i = mindex->get();
00110                 if (i >= mmax)
00111                     return;
00112                 mref[ i ] = t;
00113                 updated();
00114             }
00115 
00116             typename AssignableDataSource<T>::reference_t set()
00117             {
00118                 unsigned int i = mindex->get();
00119                 if (i >= mmax)
00120                     return internal::NA<typename AssignableDataSource<T>::reference_t>::na();
00121                 return mref[ i ];
00122             }
00123 
00124             typename AssignableDataSource<T>::const_reference_t rvalue() const
00125             {
00126                 unsigned int i = mindex->get();
00127                 if (i >= mmax)
00128                     return internal::NA<typename AssignableDataSource<T>::const_reference_t>::na();
00129                 return mref[ i ];
00130             }
00131 
00132             void updated() {
00133                 if (mparent) mparent->updated();
00134             }
00135 
00136             virtual ArrayPartDataSource<T>* clone() const {
00137                 return new ArrayPartDataSource<T>( *mref, mindex, mparent, mmax);
00138             }
00139 
00140             virtual ArrayPartDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
00141                 // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
00142                 if ( replace[this] != 0 ) {
00143                     assert ( dynamic_cast<ArrayPartDataSource<T>*>( replace[this] ) == static_cast<ArrayPartDataSource<T>*>( replace[this] ) );
00144                     return static_cast<ArrayPartDataSource<T>*>( replace[this] );
00145                 }
00146                 replace[this] = new ArrayPartDataSource<T>(*mref, mindex->copy(replace), mparent->copy(replace), mmax);
00147                 return static_cast<ArrayPartDataSource<T>*>(replace[this]);
00148 
00149             }
00150         };
00151 
00152     }
00153 }
00154 
00155 #endif /* ORO_PARTDATASOURCE_HPP_ */