Orocos Real-Time Toolkit  2.9.0
DataSources.inl
Go to the documentation of this file.
1 #ifndef ORO_CORELIB_DATASOURCES_INL
2 #define ORO_CORELIB_DATASOURCES_INL
3 
4 #include "DataSources.hpp"
5 #include "DataSource.inl"
6 
7 namespace RTT
8 { namespace internal {
9 
10  template<typename T>
12 
13  template<typename T>
15  : mdata( data )
16  {
17  }
18 
22  template<>
24 
25  template<typename T>
27  : mdata()
28  {
29  }
30 
31  template<typename T>
33  {
34  mdata = t;
35  }
36 
40  template<>
42 
43  template<typename T>
45  {
46  return new ValueDataSource<T>( mdata );
47  }
48 
49  template<typename T>
50  ValueDataSource<T>* ValueDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
51  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
52  if ( replace[this] != 0 ) {
53  assert ( dynamic_cast<ValueDataSource<T>*>( replace[this] ) == static_cast<ValueDataSource<T>*>( replace[this] ) );
54  return static_cast<ValueDataSource<T>*>( replace[this] );
55  }
56  // Other pieces in the code rely on insertion in the map :
57  replace[this] = const_cast<ValueDataSource<T>*>(this);
58  // return this instead of a copy.
59  return const_cast<ValueDataSource<T>*>(this);
60  }
61 
62  template<typename T>
64 
65  template<typename T>
67  : mdata( value )
68  {
69  }
70 
71  template<typename T>
73  {
74  return new ConstantDataSource<T>(mdata);
75  }
76 
77  template<typename T>
78  ConstantDataSource<T>* ConstantDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
79  // no copy needed, share this with all instances.
80  return const_cast<ConstantDataSource<T>*>(this);
81  }
82 
83  template<typename T>
85 
86  template<typename T>
88  : mref( ref )
89  {
90  }
91 
92  template<typename T>
94  {
95  return new ConstReferenceDataSource<T>(mref);
96  }
97 
98  template<typename T>
99  ConstReferenceDataSource<T>* ConstReferenceDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
100  return const_cast<ConstReferenceDataSource<T>*>(this); // no copy needed, data is outside.
101  }
102 
103  template<typename T>
105 
106  template<typename T>
108  : mptr( &ref )
109  {
110  }
111  template<typename T>
113  {
114  *mptr = t;
115  }
116 
117  template<typename T>
119  {
120  return new ReferenceDataSource<T>(*mptr);
121  }
122 
123  template<typename T>
124  ReferenceDataSource<T>* ReferenceDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& alreadyCloned ) const {
125  return const_cast<ReferenceDataSource<T>*>(this); // no copy needed, data is outside.
126  }
127 
128  template<typename T>
130 
131  template<typename T>
133  : mdata(size ? new typename T::value_type[size] : 0 ), marray(mdata,size)
134  {
135  }
136 
137  template<typename T>
139  : mdata( oarray.count() ? new typename T::value_type[oarray.count()] : 0 ), marray(mdata, oarray.count())
140  {
141  marray = oarray; // deep copy!
142  }
143 
144  template<typename T>
145  void ArrayDataSource<T>::newArray( std::size_t size )
146  {
147  delete[] mdata;
148  mdata = size ? new typename T::value_type[size] : 0;
149  for(std::size_t i=0; i!= size; ++i) mdata[i] = typename T::value_type();
150  marray.init(mdata,size);
151  }
152 
153  template<typename T>
155  {
156  // makes a deep copy !
157  marray = t;
158  }
159 
160  template<typename T>
162  {
163  ArrayDataSource<T>* ret = new ArrayDataSource<T>( marray.count() );
164  ret->set( marray );
165  return ret;
166  }
167 
168  template<typename T>
169  ArrayDataSource<T>* ArrayDataSource<T>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
170  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
171  if ( replace[this] != 0 ) {
172  assert ( dynamic_cast<ArrayDataSource<T>*>( replace[this] ) == static_cast<ArrayDataSource<T>*>( replace[this] ) );
173  return static_cast<ArrayDataSource<T>*>( replace[this] );
174  }
175  // Other pieces in the code rely on insertion in the map :
176  replace[this] = const_cast<ArrayDataSource<T>*>(this);
177  // return this instead of a copy.
178  return const_cast<ArrayDataSource<T>*>(this);
179  }
180 
181  template< typename BoundT>
182  UnboundDataSource<BoundT>::UnboundDataSource( typename BoundT::result_t data )
183  : BoundT( data )
184  {
185  }
186 
187  template< typename BoundT>
189  {
190  }
191 
192  template< typename BoundT>
193  UnboundDataSource<BoundT>* UnboundDataSource<BoundT>::copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace) const {
194  if ( replace[this] != 0 )
195  return static_cast<UnboundDataSource<BoundT>*>(replace[this]);
196  replace[this] = new UnboundDataSource<BoundT>( this->get() );
197  return static_cast<UnboundDataSource<BoundT>*>(replace[this]);
198  }
199  }
200 }
201 
202 #endif
DataSource< T >::value_t mdata
Definition: DataSources.hpp:64
AssignableDataSource< T >::reference_t set()
Get a reference to the value of this DataSource.
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
virtual ValueDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replace) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
Definition: DataSources.inl:50
virtual ConstReferenceDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
Definition: DataSources.inl:99
void newArray(std::size_t size)
Clears the array of this data source and creates a new one.
virtual ConstantDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
Definition: DataSources.inl:78
DataSource< T >::result_t value() const
Return the result of the last evaluate() function.
Definition: DataSources.hpp:83
ConstReferenceDataSource(typename DataSource< T >::const_reference_t ref)
Definition: DataSources.inl:87
A DataSource which is used to manipulate a reference to an external value.
virtual ReferenceDataSource< T > * clone() const
Return a shallow clone of this DataSource.
A DataSource which is used to read a const reference to an external value.
virtual ConstReferenceDataSource< T > * clone() const
Return a shallow clone of this DataSource.
Definition: DataSources.inl:93
ArrayDataSource(std::size_t size=0)
Create and allocate an Array of a fixed size.
ReferenceDataSource(typename AssignableDataSource< T >::reference_t ref)
void set(typename AssignableDataSource< T >::param_t t)
AssignableDataSource< T >::reference_t set()
Get a reference to the value of this DataSource.
Definition: DataSources.hpp:90
virtual ReferenceDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
virtual ArrayDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replace) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
A special DataSource only to be used for if you understand the copy()/clone() semantics very well...
A DataSource which holds a constant value and returns it in its get() method.
~ArrayDataSource()
Use shared_ptr.
A DataSource that holds a fixed size array, using the types::carray class.
virtual ConstantDataSource< T > * clone() const
Return a shallow clone of this DataSource.
Definition: DataSources.inl:72
virtual ValueDataSource< T > * clone() const
Return a shallow clone of this DataSource.
Definition: DataSources.inl:44
AssignableDataSource< T >::reference_t set()
Get a reference to the value of this DataSource.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:52
void ref() const
Increase the reference count by one.
Definition: DataSource.cpp:80
A simple, yet very useful DataSource, which keeps a value, and returns it in its get() method...
Definition: DataSources.hpp:60
~ValueDataSource()
Use shared_ptr.
Definition: DataSources.inl:11
details::GetConstRef< T >::type const_reference_t
Definition: DataSource.hpp:107
virtual ArrayDataSource< T > * clone() const
Return a shallow clone of this DataSource.
virtual UnboundDataSource< BoundType > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replace) const
~ReferenceDataSource()
Use shared_ptr.
~ConstantDataSource()
Use shared_ptr.
Definition: DataSources.inl:63
boost::call_traits< value_t >::param_type param_t
Definition: DataSource.hpp:192