RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
BoolConverter.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is a part of the RSB project
4  *
5  * Copyright (C) 2010 by Johannes Wienke <jwienke at techfak dot uni-bielefeld dot de>
6  * Copyright (C) 2013 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
7  *
8  * This file may be licensed under the terms of the
9  * GNU Lesser General Public License Version 3 (the ``LGPL''),
10  * or (at your option) any later version.
11  *
12  * Software distributed under the License is distributed
13  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
14  * express or implied. See the LGPL for the specific language
15  * governing rights and limitations.
16  *
17  * You should have received a copy of the LGPL along with this
18  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
19  * or write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21  *
22  * The development of this software was supported by:
23  * CoR-Lab, Research Institute for Cognition and Robotics
24  * Bielefeld University
25  *
26  * ============================================================ */
27 
28 #include "BoolConverter.h"
29 
30 #include <sstream>
31 
32 #include "SerializationException.h"
33 
34 using namespace std;
35 
36 namespace rsb {
37 namespace converter {
38 
39 const string BoolConverter::WIRE_SCHEMA = "bool";
40 
41 BoolConverter::BoolConverter() :
42  Converter<string> (WIRE_SCHEMA, RSB_TYPE_TAG(bool)) {
43 }
44 
46 }
47 
48 string BoolConverter::serialize(const AnnotatedData& data, string& wire) {
49  assert(data.first == this->getDataType());
50 
51  boost::shared_ptr<bool> s
52  = boost::static_pointer_cast<bool>(data.second);
53  wire.resize(1);
54  wire[0] = ((*s) ? 1 : 0);
55  return WIRE_SCHEMA;
56 }
57 
58 AnnotatedData BoolConverter::deserialize(const std::string& wireSchema,
59  const string& wire) {
60  assert(wireSchema == WIRE_SCHEMA);
61 
62  if (wire.size() == 1 && wire[0] == 0) {
63  return make_pair(getDataType(), boost::shared_ptr<bool>(new bool(false)));
64  } else if (wire.size() == 1 && wire[0] == 1) {
65  return make_pair(getDataType(), boost::shared_ptr<bool>(new bool(true)));
66  } else {
67  throw runtime_error("Invalid encoding for bool.");
68  }
69 }
70 
71 }
72 }
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:256
static const std::string WIRE_SCHEMA
Definition: BoolConverter.h:55
#define RSB_TYPE_TAG(T)
Definition: Converter.h:45
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.
std::string serialize(const AnnotatedData &data, std::string &wire)
Serialized the given domain object to the wire.