Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
interface.h
Go to the documentation of this file.
1// Copyright 2016-2023:
2// GobySoft, LLC (2013-)
3// Community contributors (see AUTHORS file)
4// File authors:
5// Toby Schneider <toby@gobysoft.org>
6//
7//
8// This file is part of the Goby Underwater Autonomy Project Libraries
9// ("The Goby Libraries").
10//
11// The Goby Libraries are free software: you can redistribute them and/or modify
12// them under the terms of the GNU Lesser General Public License as published by
13// the Free Software Foundation, either version 2.1 of the License, or
14// (at your option) any later version.
15//
16// The Goby Libraries are distributed in the hope that they will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU Lesser General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public License
22// along with Goby. If not, see <http://www.gnu.org/licenses/>.
23
24#ifndef GOBY_MIDDLEWARE_MARSHALLING_INTERFACE_H
25#define GOBY_MIDDLEWARE_MARSHALLING_INTERFACE_H
26
27#include <map> // for map
28#include <memory> // for share...
29#include <string> // for string
30#include <type_traits> // for is_void
31#include <utility> // for pair
32#include <vector> // for vector
33
35
36namespace goby
37{
38namespace middleware
39{
40//
41// MarshallingScheme
42//
43
46{
49 {
52 CSTR = 0,
54 DCCL = 2,
55 // CAPTN_PROTO = 3,
56 // MSGPACK = 4,
59 JSON = 7,
60 // BOOST_SERIALIZATION = 8 (currently in Netsim)
61 };
62
67 static std::string to_string(int e)
68 {
69 auto it = e2s.find(e);
70 return it != e2s.end() ? it->second : std::to_string(e);
71 }
72
77 static int from_string(const std::string& s)
78 {
79 auto it = s2e.find(s);
80 return it != s2e.end() ? it->second : std::stoi(s);
81 }
82
83 private:
84 static const std::map<int, std::string> e2s;
85 static const std::map<std::string, int> s2e;
86};
87
88//
89// SerializerParserHelper
90//
91
97template <typename DataType, int scheme, class Enable = void> struct SerializerParserHelper
98{
100 static std::vector<char> serialize(const DataType& /*msg*/)
101 {
102 static_assert(std::is_void<Enable>::value, "SerializerParserHelper must be specialized");
103 return std::vector<char>();
104 }
105
107 static std::string type_name()
108 {
109 static_assert(std::is_void<Enable>::value, "SerializerParserHelper must be specialized");
110 return std::string();
111 }
112
114 static std::string type_name(const DataType& /*d*/)
115 {
116 static_assert(std::is_void<Enable>::value, "SerializerParserHelper must be specialized");
117 return std::string();
118 }
119
128 template <typename CharIterator>
129 static std::shared_ptr<DataType> parse(CharIterator bytes_begin, CharIterator bytes_end,
130 CharIterator& actual_end,
131 const std::string& type = type_name())
132 {
133 static_assert(std::is_void<Enable>::value, "SerializerParserHelper must be specialized");
134 return std::shared_ptr<DataType>();
135 }
136};
137
138//
139// scheme
140//
141
147template <typename DataType, typename Transporter> constexpr int transporter_scheme()
148{
150 return Transporter::template scheme<typename primitive_type<DataType>::type>();
151}
152
167template <typename DataType,
168 typename std::enable_if<std::is_same<DataType, void>::value>::type* = nullptr>
169constexpr int scheme()
170{
171 static_assert(std::is_same<DataType, void>::value, "Null scheme instantiated");
173}
174
175} // namespace middleware
176} // namespace goby
177
178#endif
constexpr int transporter_scheme()
Helper function for calling a particular transporter's scheme method.
Definition interface.h:147
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition cstr.h:65
The global namespace for the Goby project.
Enumeration and helper functions for marshalling scheme identification.
Definition interface.h:46
static int from_string(const std::string &s)
Convert from a string to a marshalling scheme id.
Definition interface.h:77
MarshallingSchemeEnum
Marshalilng schemes implemented in Goby.
Definition interface.h:49
static std::string to_string(int e)
Convert a known marshalling scheme to a human-readable string or an unknown scheme to the string repr...
Definition interface.h:67
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Definition interface.h:98
static std::string type_name()
The marshalling scheme specific string name for this type.
Definition interface.h:107
static std::vector< char > serialize(const DataType &)
Given data, produce a vector of bytes.
Definition interface.h:100
static std::shared_ptr< DataType > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Given a beginning and end iterator to bytes, parse the data and return it.
Definition interface.h:129
static std::string type_name(const DataType &)
The marshalling scheme specific string name for this type, given a instantiation of the type (useful ...
Definition interface.h:114