MIRA
BinarySerializerCodec.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_BINARYSERIALIZERCODEC_H_
48 #define _MIRA_BINARYSERIALIZERCODEC_H_
49 
50 #ifndef Q_MOC_RUN
51 #include <boost/shared_ptr.hpp>
52 #endif
53 
54 #include <factory/Factory.h>
55 #include <platform/Typename.h>
56 #include <utils/Buffer.h>
57 #include <utils/TypedVoidPtr.h>
58 
59 namespace mira {
60 
62 
65 
67 typedef boost::shared_ptr<BinarySerializerCodec> BinarySerializerCodecPtr;
68 
70 
114 {
116 public:
117 
121  struct Fourcc
122  {
124  Fourcc(const std::string& iFourcc="NULL")
125  {
126  std::size_t l = std::min<std::size_t>(iFourcc.size(),4);
127  for(std::size_t i=0; i<l; ++i)
128  fourcc[i] = iFourcc[i];
129  for(std::size_t i=l; i<4; ++i)
130  fourcc[i] = '\0';
131  }
132 
134  Fourcc(uint32 val) : value(val) {}
135 
136  template<typename Reflector>
137  void reflect(Reflector& r) {
138  r.member("Value", value, "The 32bit value of the fourcc code");
139  }
140 
142  static Fourcc null() {
143  return Fourcc("NULL");
144  }
145 
146  bool operator==(Fourcc other) const {
147  return value==other.value;
148  }
149 
150  bool operator!=(Fourcc other) const {
151  return !operator==(other);
152  }
153 
154  bool operator<(Fourcc other) const {
155  return value < other.value;
156  }
157 
159  std::string string() const {
160  return std::string(fourcc,4);
161  }
162 
163  public:
164  union {
165  char fourcc[4];
166  uint32 value; // as 32bit value
167  };
168  };
169 
170  static_assert(sizeof(Fourcc)==4, "Fourcc must have a size of 4 bytes");
171 
172 public:
173 
175 
176  template<typename Reflector>
177  void reflect(Reflector& r) {
178  r.itemName(this->getClass().getName());
179  }
180 
181 public:
182 
184  virtual Fourcc getFourcc() const {
185  std::string s = this->getClass().getMetaInfo("FOURCC");
186  return Fourcc(s);
187  }
188 
195  virtual TypeId getSupportedTypeId() const = 0;
196 
197 public:
198 
200  template <typename T>
201  Buffer<uint8> encode(const T& object) {
202  // these methods pass the typed objects through the untyped
203  // encode methods below.
204  return std::move(encodeBuffer(&object));
205  }
206 
208  template <typename T>
209  void decode(const Buffer<uint8>& data, T& ioObject) {
210  decodeBuffer(data, &ioObject);
211  }
212 
213 protected:
214 
223  virtual Buffer<uint8> encodeBuffer(TypedVoidConstPtr objectPtr) = 0;
224 
233  virtual void decodeBuffer(const Buffer<uint8>& data, TypedVoidPtr ioObjectPtr) = 0;
234 
235 
236 public:
237 
243  template <typename T>
245 
246  BinarySerializerCodecPtr codec = createCodec(typeId<T>(), fourcc);
247  if(!codec)
248  MIRA_THROW(XInvalidParameter, "No codec found for fourcc '" <<
249  fourcc.string() << "' and type '" << typeName<T>() <<"'");
250  return codec;
251  }
252 
253 private:
254 
255  static BinarySerializerCodecPtr createCodec(TypeId type, Fourcc fourcc);
256 
257 };
258 
260 
261 } // namespace
262 
263 #endif
void reflect(Reflector &r)
Definition: BinarySerializerCodec.h:177
static BinarySerializerCodecPtr createCodec(Fourcc fourcc)
Creates a codec with the specified FOURCC code that is suitable for encoding/decoding the specified t...
Definition: BinarySerializerCodec.h:244
virtual ~BinarySerializerCodec()
Definition: BinarySerializerCodec.h:174
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
Abstract base class for codecs that can be used with BinarySerializer and BinaryDeserializer.
Definition: BinarySerializerCodec.h:113
Same as TypedVoidPtr but const.
Definition: TypedVoidPtr.h:171
Get compiler and platform independent typenames.
uint32 value
Definition: BinarySerializerCodec.h:166
Type safe handling of typed void pointers.
#define MIRA_THROW(ex, msg)
Macro for throwing an exception.
Definition: Exception.h:82
Buffer< uint8 > encode(const T &object)
Encodes the given object and returns the buffer with the encoded data.
Definition: BinarySerializerCodec.h:201
#define MIRA_ABSTRACT_OBJECT(classIdentifier)
Use this MACRO instead of MIRA_OBJECT to declare the class as abstract.
Definition: FactoryMacros.h:235
Class that allows to maintain type-safety when passing void pointers.
Definition: TypedVoidPtr.h:101
$Header file containing base classes to enable class creation using a class factory$ ...
A four-character code that is used to identify data formats and codecs.
Definition: BinarySerializerCodec.h:121
PropertyHint type(const std::string &t)
Sets the attribute "type" to the specified value.
Definition: PropertyHint.h:295
Fourcc(const std::string &iFourcc="NULL")
Constructs Fourcc from string.
Definition: BinarySerializerCodec.h:124
The object class acts as a generic base class for classes which should be used with the classFactory...
Definition: Object.h:144
bool operator!=(Fourcc other) const
Definition: BinarySerializerCodec.h:150
bool operator==(const ImgIteratorBase &a, const ImgIteratorBase &b)
Definition: ImgIterator.h:225
Fourcc(uint32 val)
Constructs Fourcc from 32bit value.
Definition: BinarySerializerCodec.h:134
void decode(const Buffer< uint8 > &data, T &ioObject)
Decodes the given buffer into the object.
Definition: BinarySerializerCodec.h:209
bool operator==(Fourcc other) const
Definition: BinarySerializerCodec.h:146
int TypeId
The type of the integral TypeId, that can be retrieved by typeId<T>()
Definition: TypeId.h:64
bool operator<(Fourcc other) const
Definition: BinarySerializerCodec.h:154
std::string string() const
Returns the fourcc as human readable string.
Definition: BinarySerializerCodec.h:159
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
Generic buffer class that can be used as a replacement for std::vector.
static Fourcc null()
Returns the &#39;NULL&#39; fourcc.
Definition: BinarySerializerCodec.h:142
virtual Fourcc getFourcc() const
Returns the Fourcc code information.
Definition: BinarySerializerCodec.h:184
void reflect(Reflector &r)
Definition: BinarySerializerCodec.h:137
boost::shared_ptr< BinarySerializerCodec > BinarySerializerCodecPtr
Shared pointer of BinarySerializerCodec.
Definition: BinarySerializerCodec.h:64