Orocos Real-Time Toolkit  2.8.3
carray.hpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:55:18 CEST 2010 carray.hpp
3 
4  carray.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_CARRAY_HPP_
40 #define ORO_CARRAY_HPP_
41 
42 #include <boost/version.hpp>
43 #if BOOST_VERSION >= 106400
44 // The class name has been changed from boost::serialization::array<T> to array_wrapper<T> in Boost 1.61,
45 // but the header has only be renamed in Boost 1.64. Starting from Boost 1.65 array.hpp includes array_wrapper.hpp,
46 // but with 1.64 compilation fails if array_wrapper.hpp is not included.
47 # include <boost/serialization/array_wrapper.hpp>
48 #else
49 # include <boost/serialization/array.hpp>
50 #endif
51 #include <boost/array.hpp>
52 
53 namespace RTT
54 {
55  namespace types {
56 
76  template<class T>
77  class carray
78  {
79  public:
80  typedef T value_type;
81 
88  carray(value_type* t, std::size_t s) :
89  m_t( s ? t : 0),
90  m_element_count(s)
91  {}
92 
99  carray() : m_t(0), m_element_count(0) {}
100 
107 #if BOOST_VERSION >= 106100
108  carray( boost::serialization::array_wrapper<T> const& orig)
109 #else
110  carray( boost::serialization::array<T> const& orig)
111 #endif
112  : m_t( orig.address() ), m_element_count( orig.count() ) {
113  if (m_element_count == 0)
114  m_t = 0;
115  }
116 
123  template<std::size_t N>
124  carray( boost::array<T,N> & orig)
125  : m_t( orig.c_array() ), m_element_count( N ) {
126  if (m_element_count == 0)
127  m_t = 0;
128  }
129 
133  void init(value_type* t, std::size_t s) {
134  m_t = s ? t : 0;
135  m_element_count = s;
136  }
137 
142  value_type* address() const
143  {
144  return m_t;
145  }
146 
150  std::size_t count() const
151  {
152  return m_element_count;
153  }
154 
161  template <class OtherT>
162  const carray<T>& operator=( const carray<OtherT>& orig ) {
163  if (&orig != this)
164  for(std::size_t i = 0; i != orig.count() && i != count(); ++i)
165  m_t[i] = orig.address()[i];
166  return *this;
167  }
168 
176  template <class OtherT>
177 #if BOOST_VERSION >= 106100
178  const carray<T>& operator=( boost::serialization::array_wrapper<OtherT> const& orig ) {
179 #else
180  const carray<T>& operator=( boost::serialization::array<OtherT> const& orig ) {
181 #endif
182  if (orig.address() != m_t)
183  for(std::size_t i = 0; i != orig.count() && i != count(); ++i)
184  m_t[i] = orig.address()[i];
185  return *this;
186  }
187 
195  template <class OtherT, std::size_t OtherN>
196  const carray<T>& operator=( const boost::array<OtherT,OtherN>& orig ) {
197  if (orig.data() != m_t)
198  for(std::size_t i = 0; i != orig.size() && i != count(); ++i)
199  m_t[i] = orig[i];
200  return *this;
201  }
202 
203  private:
204  value_type* m_t;
205  std::size_t m_element_count;
206  };
207  }
208 }
209 
210 #endif /* ORO_CARRAY_HPP_ */
const carray< T > & operator=(boost::serialization::array< OtherT > const &orig)
Assignment only copies max(this->count(), orig.count()) elements from orig to this object...
Definition: carray.hpp:180
carray(boost::serialization::array< T > const &orig)
We are constructible from boost::serialization::array<T> Makes a shallow copy in order to keep the re...
Definition: carray.hpp:110
void init(value_type *t, std::size_t s)
(Re-)initialize this carray to a new address and size.
Definition: carray.hpp:133
value_type * address() const
The address of the first element of the array.
Definition: carray.hpp:142
Wraps a C array such that we can return a C array from a DataSource.
Definition: carray.hpp:77
std::size_t count() const
Definition: carray.hpp:150
carray()
Creates an empty carray.
Definition: carray.hpp:99
const carray< T > & operator=(const carray< OtherT > &orig)
Assignment only copies max(this->count(), orig.count()) elements from orig to this object...
Definition: carray.hpp:162
carray(boost::array< T, N > &orig)
We are constructible from boost::array<T,N> Makes a shallow copy in order to keep the reference to th...
Definition: carray.hpp:124
carray(value_type *t, std::size_t s)
Create an C array wrapper.
Definition: carray.hpp:88
Contains TaskContext, Activity, OperationCaller, Operation, Property, InputPort, OutputPort, Attribute.
Definition: Activity.cpp:51
const carray< T > & operator=(const boost::array< OtherT, OtherN > &orig)
Assignment only copies max(this->count(), orig.size()) elements from orig to this object...
Definition: carray.hpp:196