MIRA
Array.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
47 #ifndef _MIRA_ARRAY_H_
48 #define _MIRA_ARRAY_H_
49 
53 
57 
58 #include <utils/Foreach.h>
59 
60 namespace mira {
61 
63 
65 namespace serialization {
66 
68 
80 template<typename T>
81 struct PlainArray
82 {
83  typedef T value_type;
84 
85  PlainArray(T* iData, uint32 iSize) : data(iData), size(iSize) {}
86 
87  PlainArray(const PlainArray& other) : data(other.data), size(other.size) {}
88 
89  PlainArray operator=(const PlainArray& other) {
90  if(size!=other.size)
91  MIRA_THROW(XIO, "Cannot assign array with different size");
92  // copy each item
93  for(uint32 i=0; i<size; ++i)
94  data[i]=other.data[i];
95  return *this;
96  }
97 
98  template<typename Reflector>
99  void reflect(Reflector& r)
100  {
101  typedef PlainArray<T> PlainArray;
102  MIRA_REFLECT_CALL(Reflector, r, "PlainArray ReflectCollectionItems",
104  }
105 
106  uint32 getSizeInBytes() const {
107  return size * sizeof(value_type);
108  }
109 
110  bool empty() const { return size==0; }
111 
112  T* begin() { return data; }
113  T* end() { return data+size; }
114 
115  T* data;
116  const uint32 size;
117 };
118 
126 template<typename T>
127 struct Array
128 {
129  Array(T* iData, uint32 iSize) : array(iData, iSize) {}
130 
131  template<typename Reflector>
132  void reflectRead(Reflector& r)
133  {
134  uint32 count = array.size;
135  typedef Array<T> Array;
136  MIRA_REFLECT_CALL(Reflector, r, "Array ReflectCollectionCount",
138 
139  // delegate to array
140  r.delegate(array);
141  }
142 
143  template<typename Reflector>
144  void reflectWrite(Reflector& r)
145  {
146  uint32 count;
147  typedef Array<T> Array;
148  MIRA_REFLECT_CALL(Reflector, r, "Array ReflectCollectionCount",
150 
151  if(array.size!=count)
152  MIRA_THROW(XIO, "Cannot reflect the collection with " << count <<
153  " items into an array of size " << array.size);
154 
155  // delegate to array
156  r.delegate(array);
157  }
158 
160 
161  bool empty() const { return array.empty(); }
162 
163  T* begin() { return array.begin(); }
164  T* end() { return array.end(); }
165 
166  PlainArray<T> array;
167 };
168 
170 
171 } // namespace
172 
173 // specify the internal marker traits for PlainArray and Array:
174 template <typename T>
175 class IsObjectTrackable<serialization::PlainArray<T>> : public std::false_type {};
176 
177 template <typename T, typename SerializerTag>
178 class IsTransparentSerializable<serialization::PlainArray<T>,SerializerTag> : public std::true_type {};
179 
180 template <typename T>
181 class IsObjectTrackable<serialization::Array<T>> : public std::false_type {};
182 
183 template <typename T>
184 class IsCollection<serialization::Array<T>> : public std::true_type {};
185 
187 
189 
190 } // namespace
191 
192 #endif
Macro for iterating over all elements in a container.
void reflectWrite(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:581
json_spirit::mArray Array
A representation of an array (vector) in JSON.
Definition: JSON.h:190
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
#define MIRA_SPLIT_REFLECT_MEMBER
Macro that insert a class member reflect() method just splitting reflection into a reflectRead() and ...
Definition: SplitReflect.h:238
Provides type trait that indicates whether a type should be serialized "transparently".
Provides MIRA_SPLIT_REFLECT macros.
Provides type trait that indicates whether pointer/object tracking should be enabled for a certain ty...
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:81
ReflectCollectionCount and ReflectCollectionItems to be specialized for certain Reflectors.
Contains the base interface of all Reflectors, Serializers, etc.
static void reflect(Reflector &r, uint32 &ioCount)
Definition: ReflectCollection.h:71
#define MIRA_REFLECT_CALL(ReflectorType, reflector, context, COMMAND)
Whenever a reflection function calls another function that is independently maintained, the call should be marked to the reflector.
Definition: ReflectorMacros.h:115
void reflect(Reflector &r, LogRecord &record)
Non-intrusive reflector for LogRecord.
Definition: LoggingCore.h:136
Provides type trait that indicates whether a type is a collection.
static void reflect(Reflector &r, Container &c)
Definition: ReflectCollection.h:89
void reflectRead(Reflector &r, Buffer< T, Allocator > &c)
Specialization of the non-intrusive reflect for Buffer.
Definition: Buffer.h:565