RSB  0.17.0
IntegerConverter.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project
4  *
5  * Copyright (C) 2016 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #pragma once
28 
29 #include <string>
30 
31 #include <boost/cstdint.hpp>
32 #include <boost/shared_ptr.hpp>
33 
34 #include "Converter.h"
35 
36 #include "rsb/rsbexports.h"
37 
38 namespace rsb {
39 namespace converter {
40 
46 template <typename T>
47 class IntegerConverter: public Converter<std::string> {
48 public:
49  IntegerConverter(const std::string& wireSchema)
50  : Converter<std::string>(wireSchema, RSB_TYPE_TAG(T)) {
51  }
52 
53  virtual ~IntegerConverter() {
54  }
55 
56  std::string serialize(const AnnotatedData& data, std::string& wire) {
57  assert(data.first == this->getDataType());
58 
59  // Just grab the bytes of *data.second and put them into wire.
60  boost::shared_ptr<T> number = boost::static_pointer_cast<T>(data.second);
61  wire.resize(sizeof(T));
62  for (unsigned int i = 0; i < sizeof(T); ++i) {
63  wire[i] = (unsigned char) (*number >> (i * 8ull));
64  }
65  return getWireSchema();
66  }
67 
68  AnnotatedData deserialize(const std::string& wireSchema,
69  const std::string& wire) {
70  assert(wireSchema == getWireSchema());
71  assert(wire.size() == sizeof(T));
72 
73  boost::shared_ptr<T> number(new T(0));
74  for (unsigned int i = 0; i < sizeof(T); ++i) {
75  *number |= ((T) ((unsigned char) wire[i]) << (i * 8ull));
76  }
77  return make_pair(getDataType(), number);
78  }
79 
80  typedef boost::shared_ptr< IntegerConverter<T> > Ptr;
81 };
82 
88 class RSB_EXPORT Uint32Converter: public IntegerConverter<boost::uint32_t> {
89 public:
91 };
92 
98 class RSB_EXPORT Uint64Converter: public IntegerConverter<boost::uint64_t> {
99 public:
100  Uint64Converter();
101 };
102 
108 class RSB_EXPORT Int32Converter: public IntegerConverter<boost::int32_t> {
109 public:
110  Int32Converter();
111 };
112 
118 class RSB_EXPORT Int64Converter: public IntegerConverter<boost::int64_t> {
119 public:
120  Int64Converter();
121 };
122 
123 }
124 }
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:269
#define RSB_TYPE_TAG(T)
Definition: Converter.h:45
STL namespace.
Converter for boost::uint64_t.
boost::shared_ptr< IntegerConverter< T > > Ptr
virtual std::string getWireSchema() const
Returns the name of the wire schema this converter can (de)serialize from/to.
Definition: Converter.h:97
IntegerConverter(const std::string &wireSchema)
std::string serialize(const AnnotatedData &data, std::string &wire)
Serialized the given domain object to the wire.
Converter for boost::int32_t.
virtual std::string getDataType() const
Returns the name of the data type this converter is applicable for.
Definition: Converter.h:86
AnnotatedData deserialize(const std::string &wireSchema, const std::string &wire)
Deserializes a domain object from a wire type.
Converter for boost::uint32_t.
Base class for integer type converters.
Converter for boost::int64_t.