Goby3  3.1.5a
2024.05.23
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 
36 namespace goby
37 {
38 namespace middleware
39 {
40 //
41 // MarshallingScheme
42 //
43 
46 {
49  {
52  CSTR = 0,
53  PROTOBUF = 1,
54  DCCL = 2,
55  // CAPTN_PROTO = 3,
56  // MSGPACK = 4,
58  MAVLINK = 6,
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 
97 template <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 
147 template <typename DataType, typename Transporter> constexpr int transporter_scheme()
148 {
150  return Transporter::template scheme<typename primitive_type<DataType>::type>();
151 }
152 
167 template <typename DataType,
168  typename std::enable_if<std::is_same<DataType, void>::value>::type* = nullptr>
169 constexpr 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
goby::middleware::MarshallingScheme
Enumeration and helper functions for marshalling scheme identification.
Definition: interface.h:45
goby::middleware::SerializerParserHelper::parse
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
goby::middleware::MarshallingScheme::CXX_OBJECT
@ CXX_OBJECT
Definition: interface.h:57
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::SerializerParserHelper
Class for parsing and serializing a given marshalling scheme. Must be specialized for a particular sc...
Definition: interface.h:97
goby::middleware::SerializerParserHelper::serialize
static std::vector< char > serialize(const DataType &)
Given data, produce a vector of bytes.
Definition: interface.h:100
goby::util::e
constexpr T e
Definition: constants.h:35
goby::middleware::MarshallingScheme::PROTOBUF
@ PROTOBUF
Definition: interface.h:53
goby::middleware::detail::primitive_type
Definition: primitive_type.h:36
goby::middleware::MarshallingScheme::ALL_SCHEMES
@ ALL_SCHEMES
Definition: interface.h:50
goby::middleware::MarshallingScheme::NULL_SCHEME
@ NULL_SCHEME
Definition: interface.h:51
goby::middleware::MarshallingScheme::MarshallingSchemeEnum
MarshallingSchemeEnum
Marshalilng schemes implemented in Goby.
Definition: interface.h:48
goby::middleware::MarshallingScheme::from_string
static int from_string(const std::string &s)
Convert from a string to a marshalling scheme id.
Definition: interface.h:77
goby::middleware::SerializerParserHelper::type_name
static std::string type_name()
The marshalling scheme specific string name for this type.
Definition: interface.h:107
goby::middleware::MarshallingScheme::to_string
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
to_string
NLOHMANN_BASIC_JSON_TPL_DECLARATION std::string to_string(const NLOHMANN_BASIC_JSON_TPL &j)
user-defined to_string function for JSON values
Definition: json.hpp:24301
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:2071
goby::middleware::MarshallingScheme::JSON
@ JSON
Definition: interface.h:59
goby::middleware::MarshallingScheme::DCCL
@ DCCL
Definition: interface.h:54
goby::middleware::transporter_scheme
constexpr int transporter_scheme()
Helper function for calling a particular transporter's scheme method.
Definition: interface.h:147
goby::middleware::scheme
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition: cstr.h:65
primitive_type.h
goby::middleware::MarshallingScheme::MAVLINK
@ MAVLINK
Definition: interface.h:58
goby::middleware::SerializerParserHelper::type_name
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
goby::middleware::MarshallingScheme::CSTR
@ CSTR
Definition: interface.h:52