RSB  0.7.0
 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 
31 #include <rosetta/api.h>
32 
33 #include "Converter.h"
34 #include "rsb/rsbexports.h"
35 
36 namespace rsb {
37 namespace converter {
38 
46 template <typename Mechanism,
47  typename Type>
48 class RosettaConverter : public Converter<std::string> {
49 public:
51  virtual ~RosettaConverter();
52 
53  std::string
54  serialize(const AnnotatedData& data, std::string& wire);
55 
57  deserialize(const std::string& wireType, const std::string& wire);
58 };
59 
60 // Implementation
61 
62 template <typename Mechanism,
63  typename Type>
65  Converter<std::string> (rsc::runtime::typeName<Type>(),
66  rsc::runtime::typeName<Mechanism>()
67  + ":" + rsc::runtime::typeName<Type>()) {
68 }
69 
70 template <typename Mechanism,
71  typename Type>
73 }
74 
75 template <typename Mechanism,
76  typename Type>
77 std::string
79  std::string& wireData) {
80  assert(data.first == getDataType());
81 
82  boost::shared_ptr<Type> object = boost::static_pointer_cast<Type>(data.second);
83  boost::uint64_t size = rosetta::packedSize<Mechanism>(*object);
84  wireData.resize(size);
85 
86  std::vector<unsigned char> temp(size); /* TODO(jmoringe, 2012-04-25): temp */
87  rosetta::pack<Mechanism>(*object, temp, 0, size);
88  memcpy((void*) &wireData[0], (void*) &temp[0], size);
89 
90  return getWireSchema();
91 }
92 
93 template <typename Mechanism,
94  typename Type>
96 RosettaConverter<Mechanism, Type>::deserialize(const std::string& wireSchema,
97  const std::string& wireData) {
98  assert(wireSchema == getWireSchema());
99 
100  boost::shared_ptr<Type> result(new Type());
101 
102  std::vector<unsigned char> data(wireData.size()); /* TODO(jmoringe, 2012-04-25): temp */
103  memcpy((void*) &data[0], (void*) &wireData[0], wireData.size());
104  rosetta::unpack<Mechanism>(data, *result, 0, wireData.size());
105 
106  return std::make_pair(getDataType(), result);
107 }
108 
109 }
110 }