RSB  0.9.6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ProtocolBufferConverter.h
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of RSB.
4  *
5  * Copyright (C) 2011 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/shared_ptr.hpp>
32 
33 #include <rsc/runtime/TypeStringTools.h>
34 
35 #include "Converter.h"
36 #include "rsb/rsbexports.h"
37 
38 namespace rsb {
39 namespace converter {
40 
47 template<typename ProtocolBuffer>
48 class ProtocolBufferConverter: public Converter<std::string> {
49 public:
51  virtual
53 
54  std::string
55  serialize(const AnnotatedData& data, std::string& wire);
56 
58  deserialize(const std::string& wireType, const std::string& wire);
59 
60 private:
61 
62  std::string typeNameToProtoName(const std::string& type_name) {
63  bool skip = false;
64  #ifdef WIN32
65  if (type_name.size() >= 6 && type_name.substr(0, 6) == "class ") {
66  skip = true;
67  }
68  #endif
69  std::string result = ".";
70  bool colon = false;
71  for (std::string::const_iterator it
72  = type_name.begin() + (skip ? 6 : 0);
73  it != type_name.end(); ++it) {
74  // Consume two (hopefully adjacent) ':', emit one '.'
75  if (*it == ':') {
76  if (colon) {
77  colon = false;
78  } else {
79  result.push_back('.');
80  colon = true;
81  }
82  } else {
83  result.push_back(*it);
84  }
85  }
86  return result;
87  }
88 
89  std::string typeNameToWireSchema(const std::string& type_name) {
90  return typeNameToProtoName(type_name);
91  }
92 
93 };
94 
95 // Implementation
96 
97 template<typename ProtocolBuffer>
99  Converter<std::string> (rsc::runtime::typeName<ProtocolBuffer>(),
100  typeNameToWireSchema(rsc::runtime::typeName<
101  ProtocolBuffer>())) {
102 }
103 
104 template<typename ProtocolBuffer>
106 }
107 
108 template<typename ProtocolBuffer>
110  const AnnotatedData& data, std::string& wireData) {
111  assert(data.first == getDataType());
112 
113  boost::shared_ptr<ProtocolBuffer> s = boost::static_pointer_cast<
114  ProtocolBuffer>(data.second);
115  s->SerializeToString(&wireData);
116  return getWireSchema();
117 }
118 
119 template<typename ProtocolBuffer>
121  const std::string& wireSchema, const std::string& wireData) {
122  assert(wireSchema == getWireSchema());
123 
124  boost::shared_ptr<ProtocolBuffer> result(new ProtocolBuffer());
125  result->ParseFromString(wireData);
126  return std::make_pair(getDataType(), result);
127 }
128 
129 }
130 }
std::pair< std::string, boost::shared_ptr< void > > AnnotatedData
A combination of data type and the actual data.
Definition: Event.h:256
std::string typeNameToProtoName(const std::string &type_name)
A generic converter for data types based on Protocol Buffer messages.
std::string typeNameToWireSchema(const std::string &type_name)
AnnotatedData deserialize(const std::string &wireType, 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.