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  }
179 
180 public:
181 
183  virtual Fourcc getFourcc() const {
184  std::string s = this->getClass().getMetaInfo("FOURCC");
185  return Fourcc(s);
186  }
187 
194  virtual TypeId getSupportedTypeId() const = 0;
195 
196 public:
197 
199  template <typename T>
200  Buffer<uint8> encode(const T& object) {
201  // these methods pass the typed objects through the untyped
202  // encode methods below.
203  return std::move(encodeBuffer(&object));
204  }
205 
207  template <typename T>
208  void decode(const Buffer<uint8>& data, T& ioObject) {
209  decodeBuffer(data, &ioObject);
210  }
211 
212 protected:
213 
222  virtual Buffer<uint8> encodeBuffer(TypedVoidConstPtr objectPtr) = 0;
223 
232  virtual void decodeBuffer(const Buffer<uint8>& data, TypedVoidPtr ioObjectPtr) = 0;
233 
234 
235 public:
236 
242  template <typename T>
244 
245  BinarySerializerCodecPtr codec = createCodec(typeId<T>(), fourcc);
246  if(!codec)
247  MIRA_THROW(XInvalidParameter, "No codec found for fourcc '" <<
248  fourcc.string() << "' and type '" << typeName<T>() <<"'");
249  return codec;
250  }
251 
252 private:
253 
254  static BinarySerializerCodecPtr createCodec(TypeId type, Fourcc fourcc);
255 
256 };
257 
259 
260 } // namespace
261 
262 #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:243
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:81
Buffer< uint8 > encode(const T &object)
Encodes the given object and returns the buffer with the encoded data.
Definition: BinarySerializerCodec.h:200
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
#define MIRA_OBJECT(classIdentifier)
Use this MACRO if you like the factory to automatically extract the class name from the given identif...
Definition: FactoryMacros.h:183
void decode(const Buffer< uint8 > &data, T &ioObject)
Decodes the given buffer into the object.
Definition: BinarySerializerCodec.h:208
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:183
void reflect(Reflector &r)
Definition: BinarySerializerCodec.h:137
boost::shared_ptr< BinarySerializerCodec > BinarySerializerCodecPtr
Shared pointer of BinarySerializerCodec.
Definition: BinarySerializerCodec.h:64