Orocos Real-Time Toolkit  2.5.0
PartDataSource.hpp
00001 /***************************************************************************
00002   tag: The SourceWorks  Tue Sep 7 00:55:18 CEST 2010  PartDataSource.hpp
00003 
00004                         PartDataSource.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_PARTDATASOURCE_HPP_
00040 #define ORO_PARTDATASOURCE_HPP_
00041 
00042 #include "DataSource.hpp"
00043 #include "../types/carray.hpp"
00044 
00045 namespace RTT
00046 {
00047     namespace internal
00048     {
00061         template<typename T>
00062         class PartDataSource
00063         : public AssignableDataSource<T>
00064         {
00065             // a reference to a value_t
00066             typename AssignableDataSource<T>::reference_t mref;
00067             // parent data source, for updating after set().
00068             base::DataSourceBase::shared_ptr mparent;
00069         public:
00070             ~PartDataSource() {}
00071 
00072             typedef boost::intrusive_ptr<PartDataSource<T> > shared_ptr;
00073 
00080             PartDataSource( typename AssignableDataSource<T>::reference_t ref,
00081                             base::DataSourceBase::shared_ptr parent )
00082                 : mref(ref), mparent(parent)
00083             {
00084             }
00085 
00086             typename DataSource<T>::result_t get() const
00087             {
00088                 return mref;
00089             }
00090 
00091             typename DataSource<T>::result_t value() const
00092             {
00093                 return mref;
00094             }
00095 
00096             void set( typename AssignableDataSource<T>::param_t t )
00097             {
00098                 mref = t;
00099                 updated();
00100             }
00101 
00102             typename AssignableDataSource<T>::reference_t set()
00103             {
00104                 return mref;
00105             }
00106 
00107             typename AssignableDataSource<T>::const_reference_t rvalue() const
00108             {
00109                 return mref;
00110             }
00111 
00112             void updated() {
00113                 mparent->updated();
00114             }
00115 
00116             virtual PartDataSource<T>* clone() const {
00117                 return new PartDataSource<T>(mref, mparent);
00118             }
00119 
00120             virtual PartDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
00121                 // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
00122                 if ( replace[this] != 0 ) {
00123                     assert ( dynamic_cast<PartDataSource<T>*>( replace[this] ) == static_cast<PartDataSource<T>*>( replace[this] ) );
00124                     return static_cast<PartDataSource<T>*>( replace[this] );
00125                 }
00126                 // Other pieces in the code rely on insertion in the map :
00127                 replace[this] = new PartDataSource<T>(mref, mparent->copy(replace));
00128                 // return this instead of a copy.
00129                 return static_cast<PartDataSource<T>*>(replace[this]);
00130 
00131             }
00132         };
00133 
00134         template<typename T>
00135         class PartDataSource< types::carray<T> >
00136         : public AssignableDataSource< types::carray<T> >
00137         {
00138             // keeps ref to real array.
00139             types::carray<T> mref;
00140             // parent data source, for updating after set().
00141             base::DataSourceBase::shared_ptr mparent;
00142         public:
00143             ~PartDataSource() {}
00144 
00145             typedef boost::intrusive_ptr<PartDataSource<T> > shared_ptr;
00146 
00153             PartDataSource( types::carray<T> ref,
00154                             base::DataSourceBase::shared_ptr parent )
00155                 : mref(ref), mparent(parent)
00156             {
00157             }
00158 
00159             types::carray<T> get() const
00160             {
00161                 return mref;
00162             }
00163 
00164             types::carray<T> value() const
00165             {
00166                 return mref;
00167             }
00168 
00169             void set( typename AssignableDataSource< types::carray<T> >::param_t t )
00170             {
00171                 mref = t;
00172                 updated();
00173             }
00174 
00175             types::carray<T>& set()
00176             {
00177                 return mref;
00178             }
00179 
00180             types::carray<T> const& rvalue() const
00181             {
00182                 return mref;
00183             }
00184 
00185             void updated() {
00186                 mparent->updated();
00187             }
00188 
00189             virtual PartDataSource* clone() const {
00190                 return new PartDataSource(mref, mparent);
00191             }
00192 
00193             virtual PartDataSource* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
00194                 // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
00195                 if ( replace[this] != 0 ) {
00196                     assert ( dynamic_cast<PartDataSource*>( replace[this] ) == static_cast<PartDataSource*>( replace[this] ) );
00197                     return static_cast<PartDataSource*>( replace[this] );
00198                 }
00199                 // Other pieces in the code rely on insertion in the map :
00200                 replace[this] = new PartDataSource(mref, mparent->copy(replace));
00201                 // return this instead of a copy.
00202                 return static_cast<PartDataSource*>(replace[this]);
00203 
00204             }
00205         };
00206     }
00207 }
00208 
00209 
00210 #endif /* ORO_PARTDATASOURCE_HPP_ */