Orocos Real-Time Toolkit  2.8.3
ArrayPartDataSource.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 ArrayPartDataSource.hpp
3 
4  ArrayPartDataSource.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_ARRAYPARTDATASOURCE_HPP_
40 #define ORO_ARRAYPARTDATASOURCE_HPP_
41 
42 #include "DataSource.hpp"
43 #include "NA.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
62  typename AssignableDataSource<T>::value_t* mref;
63  // [] index
65  // parent data source, for updating after set().
67  // safety: all indexes must be smaller than this.
68  unsigned int mmax;
69  public:
71 
72  typedef boost::intrusive_ptr<ArrayPartDataSource<T> > shared_ptr;
73 
83  base::DataSourceBase::shared_ptr parent, unsigned int max )
84  : mref(&ref), mindex(index), mparent(parent), mmax(max)
85  {
86  }
87 
88  typename DataSource<T>::result_t get() const
89  {
90  unsigned int i = mindex->get();
91  if (i >= mmax)
92  return internal::NA<T>::na();
93  return mref[ i ];
94  }
95 
96  typename DataSource<T>::result_t value() const
97  {
98  unsigned int i = mindex->get();
99  if (i >= mmax)
100  return internal::NA<T>::na();
101  return mref[ i ];
102  }
103 
104  void set( typename AssignableDataSource<T>::param_t t )
105  {
106  unsigned int i = mindex->get();
107  if (i >= mmax)
108  return;
109  mref[ i ] = t;
110  updated();
111  }
112 
114  {
115  unsigned int i = mindex->get();
116  if (i >= mmax)
118  return mref[ i ];
119  }
120 
122  {
123  unsigned int i = mindex->get();
124  if (i >= mmax)
126  return mref[ i ];
127  }
128 
129  void updated() {
130  if (mparent) mparent->updated();
131  }
132 
133  virtual ArrayPartDataSource<T>* clone() const {
134  return new ArrayPartDataSource<T>( *mref, mindex, mparent, mmax);
135  }
136 
137  virtual ArrayPartDataSource<T>* copy( std::map<const base::DataSourceBase*, base::DataSourceBase*>& replace ) const {
138  // if somehow a copy exists, return the copy, otherwise return this (see Attribute copy)
139  if ( replace[this] != 0 ) {
140  assert ( dynamic_cast<ArrayPartDataSource<T>*>( replace[this] ) == static_cast<ArrayPartDataSource<T>*>( replace[this] ) );
141  return static_cast<ArrayPartDataSource<T>*>( replace[this] );
142  }
143 
144  // Both this and mparent are copied, but the part must also copy reference mref from the new parent !
145  assert( mparent->getRawPointer() != 0 && "Can't copy part of rvalue datasource.");
146  if ( mparent->getRawPointer() == 0 )
147  throw std::runtime_error("PartDataSource.hpp: Can't copy part of rvalue datasource.");
148  base::DataSourceBase::shared_ptr mparent_copy = mparent->copy(replace);
149  // calculate the pointer offset in parent:
150  int offset = reinterpret_cast<unsigned char*>( mref ) - reinterpret_cast<unsigned char*>(mparent->getRawPointer());
151  // get the pointer to the new parent member:
152  typename AssignableDataSource<T>::value_t* mref_copy = reinterpret_cast<typename AssignableDataSource<T>::value_t*>( reinterpret_cast<unsigned char*>(mparent_copy->getRawPointer()) + offset );
153 
154  replace[this] = new ArrayPartDataSource<T>(*mref_copy, mindex->copy(replace), mparent_copy, mmax);
155  // returns copy
156  return static_cast<ArrayPartDataSource<T>*>(replace[this]);
157 
158  }
159  };
160 
161  }
162 }
163 
164 #endif /* ORO_ARRAYPARTDATASOURCE_HPP_ */
DataSource is a base class representing a generic way to read data of type T.
Definition: DataSource.hpp:94
virtual result_t get() const =0
Return the data as type T.
ArrayPartDataSource(typename AssignableDataSource< T >::reference_t ref, DataSource< unsigned int >::shared_ptr index, base::DataSourceBase::shared_ptr parent, unsigned int max)
The part is constructed from a reference to an existing array of elements and an index for accessing ...
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...
static T na()
Definition: NA.hpp:57
AssignableDataSource< T >::const_reference_t rvalue() const
Get a const reference to the value of this DataSource.
boost::intrusive_ptr< ArrayPartDataSource< T > > shared_ptr
DataSource< T >::result_t value() const
Return the result of the last evaluate() function.
A DataSource which is used to manipulate a reference to a part of a data source holding a C-style arr...
virtual DataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &alreadyCloned) const =0
Create a deep copy of this internal::DataSource, unless it is already cloned.
virtual ArrayPartDataSource< T > * copy(std::map< const base::DataSourceBase *, base::DataSourceBase * > &replace) const
Create a deep copy of this internal::DataSource, unless it is already cloned.
virtual ArrayPartDataSource< T > * clone() const
Return a shallow clone of this DataSource.
This class is used to return a &#39;default&#39; value when no value is available (&#39;Not Available&#39;).
Definition: NA.hpp:53
A DataSource which has set() methods.
Definition: DataSource.hpp:184
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
void ref() const
Increase the reference count by one.
Definition: DataSource.cpp:80
boost::call_traits< value_t >::param_type param_t
Definition: DataSource.hpp:192