Orocos Real-Time Toolkit  2.6.0
DataSources.inl
00001 #ifndef ORO_CORELIB_DATASOURCES_INL
00002 #define ORO_CORELIB_DATASOURCES_INL
00003 
00004 #include "DataSources.hpp"
00005 #include "DataSource.inl"
00006 
00007 namespace RTT
00008 { namespace internal {
00009 
00010     template<typename T>
00011     ValueDataSource<T>::~ValueDataSource() {}
00012 
00013     template<typename T>
00014     ValueDataSource<T>::ValueDataSource( T data )
00015         : mdata( data )
00016     {
00017     }
00018 
00019     template<typename T>
00020     ValueDataSource<T>::ValueDataSource( )
00021         : mdata()
00022     {
00023     }
00024 
00025     template<typename T>
00026     void ValueDataSource<T>::set( typename AssignableDataSource<T>::param_t t )
00027     {
00028         mdata = t;
00029     }
00030 
00031     template<typename T>
00032     ValueDataSource<T>* ValueDataSource<T>::clone() const
00033     {
00034         return new ValueDataSource<T>( mdata );
00035     }
00036 
00037     template<typename T>
00038     ValueDataSource<T>* ValueDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
00039         // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
00040         if ( replace[this] != 0 ) {
00041             assert ( dynamic_cast<ValueDataSource<T>*>( replace[this] ) == static_cast<ValueDataSource<T>*>( replace[this] ) );
00042             return static_cast<ValueDataSource<T>*>( replace[this] );
00043         }
00044         // Other pieces in the code rely on insertion in the map :
00045         replace[this] = const_cast<ValueDataSource<T>*>(this);
00046         // return this instead of a copy.
00047         return const_cast<ValueDataSource<T>*>(this);
00048     }
00049 
00050     template<typename T>
00051     ConstantDataSource<T>::~ConstantDataSource() {}
00052 
00053     template<typename T>
00054     ConstantDataSource<T>::ConstantDataSource( T value )
00055         : mdata( value )
00056     {
00057     }
00058 
00059     template<typename T>
00060     ConstantDataSource<T>* ConstantDataSource<T>::clone() const
00061     {
00062         return new ConstantDataSource<T>(mdata);
00063     }
00064 
00065     template<typename T>
00066     ConstantDataSource<T>* ConstantDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00067         // no copy needed, share this with all instances.
00068         return const_cast<ConstantDataSource<T>*>(this);
00069     }
00070 
00071     template<typename T>
00072     ConstReferenceDataSource<T>::~ConstReferenceDataSource() {}
00073 
00074     template<typename T>
00075     ConstReferenceDataSource<T>::ConstReferenceDataSource( typename DataSource<T>::const_reference_t ref )
00076         : mref( ref )
00077     {
00078     }
00079 
00080     template<typename T>
00081     ConstReferenceDataSource<T>* ConstReferenceDataSource<T>::clone() const
00082     {
00083         return new ConstReferenceDataSource<T>(mref);
00084     }
00085 
00086     template<typename T>
00087     ConstReferenceDataSource<T>* ConstReferenceDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00088         return const_cast<ConstReferenceDataSource<T>*>(this); // no copy needed, data is outside.
00089     }
00090 
00091     template<typename T>
00092     ReferenceDataSource<T>::~ReferenceDataSource() {}
00093 
00094     template<typename T>
00095     ReferenceDataSource<T>::ReferenceDataSource( typename AssignableDataSource<T>::reference_t ref )
00096         : mptr( &ref )
00097     {
00098     }
00099     template<typename T>
00100     void ReferenceDataSource<T>::set( typename AssignableDataSource<T>::param_t t )
00101     {
00102         *mptr = t;
00103     }
00104 
00105     template<typename T>
00106     ReferenceDataSource<T>* ReferenceDataSource<T>::clone() const
00107     {
00108         return new ReferenceDataSource<T>(*mptr);
00109     }
00110 
00111     template<typename T>
00112     ReferenceDataSource<T>* ReferenceDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
00113         return const_cast<ReferenceDataSource<T>*>(this); // no copy needed, data is outside.
00114     }
00115 
00116     template<typename T>
00117     ArrayDataSource<T>::~ArrayDataSource() { delete[] mdata; }
00118 
00119     template<typename T>
00120     ArrayDataSource<T>::ArrayDataSource( std::size_t size )
00121         : mdata(size ? new typename T::value_type[size] : 0 ), marray(mdata,size)
00122     {
00123     }
00124 
00125     template<typename T>
00126     ArrayDataSource<T>::ArrayDataSource( T const& oarray )
00127         : mdata( oarray.count() ? new typename T::value_type[oarray.count()] : 0 ), marray(mdata, oarray.count())
00128     {
00129         marray = oarray; // deep copy!
00130     }
00131 
00132     template<typename T>
00133     void ArrayDataSource<T>::newArray( std::size_t size )
00134     {
00135         delete[] mdata;
00136         mdata = size ? new typename T::value_type[size] : 0;
00137         for(std::size_t i=0; i!= size; ++i) mdata[i] = typename T::value_type();
00138         marray.init(mdata,size);
00139     }
00140 
00141     template<typename T>
00142     void ArrayDataSource<T>::set( typename AssignableDataSource<T>::param_t t )
00143     {
00144         // makes a deep copy !
00145         marray = t;
00146     }
00147 
00148     template<typename T>
00149     ArrayDataSource<T>* ArrayDataSource<T>::clone() const
00150     {
00151         ArrayDataSource<T>* ret = new ArrayDataSource<T>( marray.count() );
00152         ret->set( marray );
00153         return ret;
00154     }
00155 
00156     template<typename T>
00157     ArrayDataSource<T>* ArrayDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
00158         // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
00159         if ( replace[this] != 0 ) {
00160             assert ( dynamic_cast<ArrayDataSource<T>*>( replace[this] ) == static_cast<ArrayDataSource<T>*>( replace[this] ) );
00161             return static_cast<ArrayDataSource<T>*>( replace[this] );
00162         }
00163         // Other pieces in the code rely on insertion in the map :
00164         replace[this] = const_cast<ArrayDataSource<T>*>(this);
00165         // return this instead of a copy.
00166         return const_cast<ArrayDataSource<T>*>(this);
00167     }
00168 
00169         template< typename BoundT>
00170         UnboundDataSource<BoundT>::UnboundDataSource( typename BoundT::result_t data )
00171             : BoundT( data )
00172         {
00173         }
00174 
00175         template< typename BoundT>
00176         UnboundDataSource<BoundT>::UnboundDataSource( )
00177         {
00178         }
00179 
00180         template< typename BoundT>
00181         UnboundDataSource<BoundT>* UnboundDataSource<BoundT>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace) const {
00182             if ( replace[this] != 0 )
00183                 return static_cast<UnboundDataSource<BoundT>*>(replace[this]);
00184             replace[this] = new UnboundDataSource<BoundT>( this->get() );
00185             return static_cast<UnboundDataSource<BoundT>*>(replace[this]);
00186         }
00187     }
00188 }
00189 
00190 #endif