Orocos Real-Time Toolkit  2.8.3
PartDataSource.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 PartDataSource.hpp
3 
4  PartDataSource.hpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * This library is free software; you can redistribute it and/or *
12  * modify it under the terms of the GNU General Public *
13  * License as published by the Free Software Foundation; *
14  * version 2 of the License. *
15  * *
16  * As a special exception, you may use this file as part of a free *
17  * software library without restriction. Specifically, if other files *
18  * instantiate templates or use macros or inline functions from this *
19  * file, or you compile this file and link it with other files to *
20  * produce an executable, this file does not by itself cause the *
21  * resulting executable to be covered by the GNU General Public *
22  * License. This exception does not however invalidate any other *
23  * reasons why the executable file might be covered by the GNU General *
24  * Public License. *
25  * *
26  * This library is distributed in the hope that it will be useful, *
27  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
29  * Lesser General Public License for more details. *
30  * *
31  * You should have received a copy of the GNU General Public *
32  * License along with this library; if not, write to the Free Software *
33  * Foundation, Inc., 59 Temple Place, *
34  * Suite 330, Boston, MA 02111-1307 USA *
35  * *
36  ***************************************************************************/
37 
38 
39 #ifndef ORO_PARTDATASOURCE_HPP_
40 #define ORO_PARTDATASOURCE_HPP_
41 
42 #include "DataSource.hpp"
43 #include "../types/carray.hpp"
44 
45 namespace RTT
46 {
47  namespace internal
48  {
57  template<typename T>
59  : public AssignableDataSource<T>
60  {
61  // a reference to a value_t
63  // parent data source, for updating after set().
65  public:
67 
68  typedef boost::intrusive_ptr<PartDataSource<T> > shared_ptr;
69 
78  : mref(ref), mparent(parent)
79  {
80  }
81 
82  typename DataSource<T>::result_t get() const
83  {
84  return mref;
85  }
86 
87  typename DataSource<T>::result_t value() const
88  {
89  return mref;
90  }
91 
92  void set( typename AssignableDataSource<T>::param_t t )
93  {
94  mref = t;
95  updated();
96  }
97 
99  {
100  return mref;
101  }
102 
104  {
105  return mref;
106  }
107 
108  void updated() {
109  mparent->updated();
110  }
111 
112  virtual PartDataSource<T>* clone() const {
113  return new PartDataSource<T>(mref, mparent);
114  }
115 
116  virtual PartDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
117  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
118  if ( replace[this] != 0 ) {
119  assert ( dynamic_cast<PartDataSource<T>*>( replace[this] ) == static_cast<PartDataSource<T>*>( replace[this] ) );
120  return static_cast<PartDataSource<T>*>( replace[this] );
121  }
122  // Both this and mparent are copied, but the part must also copy reference mref from the new parent !
123  assert( mparent->getRawPointer() != 0 && "Can't copy part of rvalue datasource.");
124  if ( mparent->getRawPointer() == 0 )
125  throw std::runtime_error("PartDataSource.hpp: Can't copy part of rvalue datasource.");
126  base::DataSourceBase::shared_ptr mparent_copy = mparent->copy(replace);
127  // calculate the pointer offset in parent:
128  int offset = reinterpret_cast<unsigned char*>( &mref ) - reinterpret_cast<unsigned char*>(mparent->getRawPointer());
129  // get the pointer to the new parent member:
130  typename AssignableDataSource<T>::value_t* mref_copy = reinterpret_cast<typename AssignableDataSource<T>::value_t*>( reinterpret_cast<unsigned char*>(mparent_copy->getRawPointer()) + offset );
131  replace[this] = new PartDataSource<T>( *mref_copy, mparent_copy );
132  // returns copy
133  return static_cast<PartDataSource<T>*>(replace[this]);
134 
135  }
136  };
137 
141  template<typename T>
142  class PartDataSource< types::carray<T> >
143  : public AssignableDataSource< types::carray<T> >
144  {
145  // keeps ref to real array.
146  types::carray<T> mref;
147  // parent data source, for updating after set().
149  public:
151 
152  typedef boost::intrusive_ptr<PartDataSource<T> > shared_ptr;
153 
162  : mref(ref), mparent(parent)
163  {
164  }
165 
166  types::carray<T> get() const
167  {
168  return mref;
169  }
170 
172  {
173  return mref;
174  }
175 
177  {
178  mref = t;
179  updated();
180  }
181 
183  {
184  return mref;
185  }
186 
187  types::carray<T> const& rvalue() const
188  {
189  return mref;
190  }
191 
192  void updated() {
193  mparent->updated();
194  }
195 
196  virtual PartDataSource* clone() const {
197  return new PartDataSource(mref, mparent);
198  }
199 
200  virtual PartDataSource* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
201  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
202  if ( replace[this] != 0 ) {
203  assert ( dynamic_cast<PartDataSource*>( replace[this] ) == static_cast<PartDataSource*>( replace[this] ) );
204  return static_cast<PartDataSource*>( replace[this] );
205  }
206  // Both this and mparent are copied, but the part must also copy reference mref from the new parent !
207  assert( mparent->getRawPointer() != 0 && "Can't copy part of rvalue datasource.");
208  if ( mparent->getRawPointer() == 0 )
209  throw std::runtime_error("PartDataSource.hpp: Can't copy part of rvalue datasource.");
210  base::DataSourceBase::shared_ptr mparent_copy = mparent->copy(replace);
211  // calculate the pointer offset in parent:
212  int offset = reinterpret_cast<unsigned char*>( mref.address() ) - reinterpret_cast<unsigned char*>(mparent->getRawPointer());
213  // get the pointer to the new parent member:
214  types::carray<T> mref_copy;
215  mref_copy.init(reinterpret_cast<T*>( reinterpret_cast<unsigned char*>(mparent_copy->getRawPointer()) + offset ), mref.count() );
216  replace[this] = new PartDataSource(mref_copy, mparent_copy);
217  // return copy.
218  return static_cast<PartDataSource*>(replace[this]);
219 
220  }
221  };
222  }
223 }
224 
225 
226 #endif /* ORO_PARTDATASOURCE_HPP_ */
A DataSource which is used to manipulate a reference to a part of a data source.
types::carray< T > value() const
Return the result of the last evaluate() function.
void init(value_type *t, std::size_t s)
(Re-)initialize this carray to a new address and size.
Definition: carray.hpp:133
virtual PartDataSource< T > * clone() const
Return a shallow clone of this DataSource.
boost::call_traits< value_t >::reference reference_t
Definition: DataSource.hpp:193
void updated()
In case the internal::DataSource returns a &#39;reference&#39; type, call this method to notify it that the d...
types::carray< T > const & rvalue() const
Get a const reference to the value of this DataSource.
Wraps a C array such that we can return a C array from a DataSource.
Definition: carray.hpp:77
virtual PartDataSource * clone() const
Return a shallow clone of this DataSource.
DataSource< T >::result_t value() const
Return the result of the last evaluate() function.
PartDataSource(typename AssignableDataSource< T >::reference_t ref, base::DataSourceBase::shared_ptr parent)
The part is constructed from a reference to a member variable of an existing struct.
boost::intrusive_ptr< PartDataSource< T > > shared_ptr
A DataSource which has set() methods.
Definition: DataSource.hpp:184
virtual PartDataSource * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replace) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
DataSource< T >::value_t value_t
Definition: DataSource.hpp:190
DataSource< T >::const_reference_t const_reference_t
Definition: DataSource.hpp:191
boost::intrusive_ptr< DataSourceBase > shared_ptr
Use this type to store a pointer to a DataSourceBase.
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
PartDataSource(types::carray< T > ref, base::DataSourceBase::shared_ptr parent)
The part is constructed from a reference to a member variable of an existing struct.
void ref() const
Increase the reference count by one.
Definition: DataSource.cpp:80
AssignableDataSource< T >::const_reference_t rvalue() const
Get a const reference to the value of this DataSource.
boost::intrusive_ptr< PartDataSource< T > > shared_ptr
boost::call_traits< value_t >::param_type param_t
Definition: DataSource.hpp:192
virtual PartDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replace) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
void updated()
In case the internal::DataSource returns a &#39;reference&#39; type, call this method to notify it that the d...