Goby3  3.1.5a
2024.05.23
protobuf.h
Go to the documentation of this file.
1 // Copyright 2019-2022:
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_PROTOBUF_H
25 #define GOBY_MIDDLEWARE_MARSHALLING_PROTOBUF_H
26 
27 #include <mutex>
28 
29 #include <dccl/dynamic_protobuf_manager.h>
31 
33 
34 #include "interface.h"
35 
36 #if GOOGLE_PROTOBUF_VERSION < 3001000
37 #define ByteSizeLong ByteSize
38 #endif
39 
40 namespace goby
41 {
42 namespace middleware
43 {
45 template <typename DataType>
47  DataType, MarshallingScheme::PROTOBUF,
48  std::enable_if_t<!std::is_same<DataType, google::protobuf::Message>::value>>
49 {
51  static std::vector<char> serialize(const DataType& msg)
52  {
53  std::vector<char> bytes(msg.ByteSizeLong(), 0);
54  msg.SerializeToArray(bytes.data(), bytes.size());
55  return bytes;
56  }
57 
65  static std::string type_name(const DataType& d = DataType())
66  {
67  return DataType::descriptor()->full_name();
68  }
69 
71  template <typename CharIterator>
72  static std::shared_ptr<DataType> parse(CharIterator bytes_begin, CharIterator bytes_end,
73  CharIterator& actual_end,
74  const std::string& type = type_name())
75  {
76  auto msg = std::make_shared<DataType>();
77  msg->ParseFromArray(&*bytes_begin, bytes_end - bytes_begin);
78  actual_end = bytes_begin + msg->ByteSizeLong();
79  return msg;
80  }
81 };
82 
84 template <> struct SerializerParserHelper<google::protobuf::Message, MarshallingScheme::PROTOBUF>
85 {
87  static std::vector<char> serialize(const google::protobuf::Message& msg)
88  {
89  std::vector<char> bytes(msg.ByteSizeLong(), 0);
90  msg.SerializeToArray(bytes.data(), bytes.size());
91  return bytes;
92  }
93 
97  static std::string type_name(const google::protobuf::Message& d)
98  {
99  return d.GetDescriptor()->full_name();
100  }
101 
105  static std::string type_name(const google::protobuf::Descriptor* desc)
106  {
107  return desc->full_name();
108  }
109 
117  template <typename CharIterator>
118  static std::shared_ptr<google::protobuf::Message>
119  parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator& actual_end,
120  const std::string& type, bool user_pool_first = false)
121  {
122  std::shared_ptr<google::protobuf::Message> msg;
123 
124  {
125  static std::mutex dynamic_protobuf_manager_mutex;
126  std::lock_guard<std::mutex> lock(dynamic_protobuf_manager_mutex);
127  msg = dccl::DynamicProtobufManager::new_protobuf_message<
128  std::shared_ptr<google::protobuf::Message>>(type, user_pool_first);
129  }
130 
131  msg->ParseFromArray(&*bytes_begin, bytes_end - bytes_begin);
132  actual_end = bytes_begin + msg->ByteSizeLong();
133  return msg;
134  }
135 };
136 
137 namespace protobuf
138 {
139 namespace detail
140 {
141 // used to select between DCCL messages (with added DCCLParameters Enumeration)
142 // and normal Protobuf messages
143 // in the DCCL case, both "scheme_protobuf_or_dccl" functions are valid, but the one
144 // with "dccl_selector" as the argument is chosen because this doesn't require
145 // upcasting to "protobuf_selector"
146 // in the plain Protobuf case, just the "scheme_protobuf_or_dccl(protobuf_selector)" function
147 // is chosen by template resolution, so this one is used.
149 {
150 };
152 {
153 };
154 
155 template <typename T,
156  typename std::enable_if<std::is_enum<typename T::DCCLParameters>::value>::type* = nullptr>
158 {
160 }
161 
162 template <typename T> constexpr int scheme_protobuf_or_dccl(protobuf_selector)
163 {
165 }
166 } // namespace detail
167 } // namespace protobuf
168 
169 template <typename T, typename std::enable_if<
170  std::is_base_of<google::protobuf::Message, T>::value>::type* = nullptr>
171 constexpr int scheme()
172 {
173  return protobuf::detail::scheme_protobuf_or_dccl<T>(protobuf::detail::dccl_selector());
174 }
175 
176 } // namespace middleware
177 } // namespace goby
178 
179 #endif
goby::middleware::MarshallingScheme
Enumeration and helper functions for marshalling scheme identification.
Definition: interface.h:45
interface.h
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::protobuf::detail::protobuf_selector
Definition: protobuf.h:148
intervehicle.pb.h
goby::util::logger::mutex
std::recursive_mutex mutex
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::PROTOBUF, std::enable_if_t<!std::is_same< DataType, google::protobuf::Message >::value > >::serialize
static std::vector< char > serialize(const DataType &msg)
Serialize Protobuf message (standard Protobuf encoding)
Definition: protobuf.h:51
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::PROTOBUF >::serialize
static std::vector< char > serialize(const google::protobuf::Message &msg)
Serialize Protobuf message (standard Protobuf encoding)
Definition: protobuf.h:87
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::PROTOBUF, std::enable_if_t<!std::is_same< DataType, google::protobuf::Message >::value > >::type_name
static std::string type_name(const DataType &d=DataType())
Full protobuf Message name, including package (if one is defined).
Definition: protobuf.h:65
detail
detail namespace with internal helper functions
Definition: json.hpp:246
goby::middleware::MarshallingScheme::PROTOBUF
@ PROTOBUF
Definition: interface.h:53
goby::util::logger_lock::lock
@ lock
Definition: flex_ostreambuf.h:62
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::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::PROTOBUF >::type_name
static std::string type_name(const google::protobuf::Message &d)
Full protobuf name from message instantiation, including package (if one is defined).
Definition: protobuf.h:97
message.h
goby::middleware::protobuf::detail::dccl_selector
Definition: protobuf.h:151
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::PROTOBUF >::parse
static std::shared_ptr< google::protobuf::Message > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type, bool user_pool_first=false)
Parse Protobuf message (using standard Protobuf decoding) given the Protobuf type name and assuming t...
Definition: protobuf.h:119
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::PROTOBUF, std::enable_if_t<!std::is_same< DataType, google::protobuf::Message >::value > >::parse
static std::shared_ptr< DataType > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Parse Protobuf message (using standard Protobuf decoding)
Definition: protobuf.h:72
jwt::json::type
type
Generic JSON types used in JWTs.
Definition: jwt.h:2071
goby::msg
extern ::google::protobuf::internal::ExtensionIdentifier< ::google::protobuf::MessageOptions, ::google::protobuf::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition: option_extensions.pb.h:1327
google::protobuf::Message
Definition: message.h:189
google::protobuf::Message::GetDescriptor
const Descriptor * GetDescriptor() const
Definition: message.h:336
goby::middleware::MarshallingScheme::DCCL
@ DCCL
Definition: interface.h:54
goby::middleware::scheme
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition: cstr.h:65
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::PROTOBUF >::type_name
static std::string type_name(const google::protobuf::Descriptor *desc)
Full protobuf name from descriptor, including package (if one is defined).
Definition: protobuf.h:105
goby::middleware::protobuf::detail::scheme_protobuf_or_dccl
constexpr int scheme_protobuf_or_dccl(dccl_selector)
Definition: protobuf.h:157
google
Definition: dccl.h:57
detail::enable_if_t
typename std::enable_if< B, T >::type enable_if_t
Definition: json.hpp:3079