RSB  0.17.0
FloatingPointConverter.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 #include <limits>
31 #include <stdexcept>
32 
33 #include <boost/shared_ptr.hpp>
34 
35 #include "Converter.h"
36 
37 #include "rsb/rsbexports.h"
38 
39 namespace rsb {
40 namespace converter {
41 
47 template <typename T>
48 class FloatingPointConverter: public Converter<std::string> {
49 public:
50  FloatingPointConverter(const std::string& wireSchema)
51  : Converter<std::string>(wireSchema, RSB_TYPE_TAG(T)) {
52  if (!std::numeric_limits<T>::is_iec559) {
53  throw std::runtime_error("This converter only works on platforms"
54  " that represent " + getDataType() +
55  " values in IEC 559 format which does not"
56  " seem to the case on this platform"
57  " according to"
58  " std::numeric_limits<>::is_iec559");
59  }
60  }
61 
63  }
64 
65  std::string serialize(const AnnotatedData& data, std::string& wire) {
66  assert(data.first == this->getDataType());
67 
68  // Just grab the bytes of *data.second and put them into wire.
69  boost::shared_ptr<T> number = boost::static_pointer_cast<T>(data.second);
70  wire.resize(sizeof(T));
71  for (unsigned int i = 0; i < sizeof(T); ++i) {
72  wire[i] = ((char*) number.get())[i];
73  }
74  return getWireSchema();
75  }
76 
77  AnnotatedData deserialize(const std::string& wireSchema,
78  const std::string& wire) {
79  assert(wireSchema == getWireSchema());
80  assert(wire.size() == sizeof(T));
81 
82  // Write bytes from wire into a floating point number.
83  boost::shared_ptr<T> number(new T(0));
84  for (unsigned int i = 0; i < sizeof(T); ++i) {
85  ((char*) number.get())[i] = wire[i];
86  }
87  return make_pair(getDataType(), number);
88  }
89 
90  typedef boost::shared_ptr< FloatingPointConverter<T> > Ptr;
91 };
92 
98 class RSB_EXPORT FloatConverter: public FloatingPointConverter<float> {
99 public:
100  FloatConverter();
101 };
102 
108 class RSB_EXPORT DoubleConverter: public FloatingPointConverter<double> {
109 public:
110  DoubleConverter();
111 };
112 
113 }
114 }
FloatingPointConverter(const std::string &wireSchema)
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
Base class for floating point type converters.
STL namespace.
virtual std::string getWireSchema() const
Returns the name of the wire schema this converter can (de)serialize from/to.
Definition: Converter.h:97
std::string serialize(const AnnotatedData &data, std::string &wire)
Serialized the given domain object to the wire.
AnnotatedData deserialize(const std::string &wireSchema, const std::string &wire)
Deserializes a domain object from a wire type.
Converter for float type.
virtual std::string getDataType() const
Returns the name of the data type this converter is applicable for.
Definition: Converter.h:86
boost::shared_ptr< FloatingPointConverter< T > > Ptr