RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
RosettaConverter.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSB project.
4  *
5  * Copyright (C) 2012 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 <boost/cstdint.hpp>
30 #include <boost/format.hpp>
31 
32 #include <rosetta/api.h>
33 
34 #include "Converter.h"
35 #include "rsb/rsbexports.h"
36 
37 namespace rsb {
38 namespace converter {
39 
48 template <typename Mechanism,
49  typename DataType,
50  typename WireSchema>
51 class RosettaConverter : public Converter<std::string> {
52 public:
54  virtual ~RosettaConverter();
55 
56  std::string serialize(const AnnotatedData& data, std::string& wire);
57 
58  AnnotatedData deserialize(const std::string& wireType, const std::string& wire);
59 };
60 
61 // Implementation
62 
63 template <typename Mechanism,
64  typename DataType,
65  typename WireSchema>
67  Converter<std::string>(rsc::runtime::typeName<DataType>(),
68  boost::str(boost::format("rosetta<%1%,%2%>")
69  % Mechanism::name()
70  % WireSchema::name())) {
71 }
72 
73 template <typename Mechanism,
74  typename DataType,
75  typename WireSchema>
77 }
78 
79 template <typename Mechanism,
80  typename DataType,
81  typename WireSchema>
83  std::string& wireData) {
84  assert(data.first == getDataType());
85 
86  boost::shared_ptr<DataType> object
87  = boost::static_pointer_cast<DataType>(data.second);
88  boost::uint64_t size = rosetta::packedSize<Mechanism, WireSchema>(*object);
89  wireData.resize(size);
90 
91  std::vector<unsigned char> temp(size); /* TODO(jmoringe, 2012-04-25): temp */
92  rosetta::pack<Mechanism, WireSchema>(*object, temp, 0, size);
93  memcpy((void*) &wireData[0], (void*) &temp[0], size);
94 
95  return getWireSchema();
96 }
97 
98 template <typename Mechanism,
99  typename DataType,
100  typename WireSchema>
103  const std::string& wireData) {
104  assert(wireSchema == getWireSchema());
105 
106  boost::shared_ptr<DataType> result(new DataType());
107 
108  std::vector<unsigned char> data(wireData.size()); /* TODO(jmoringe, 2012-04-25): temp */
109  memcpy((void*) &data[0], (void*) &wireData[0], wireData.size());
110  rosetta::unpack<Mechanism, WireSchema>(data, *result, 0, wireData.size());
111 
112  return std::make_pair(getDataType(), result);
113 }
114 
115 }
116 }
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:256
std::string serialize(const AnnotatedData &data, std::string &wire)
Serialized the given domain object to the wire.
AnnotatedData deserialize(const std::string &wireType, const std::string &wire)
Deserializes a domain object from a wire type.