Goby3 3.6.1
2026.09.15
Loading...
Searching...
No Matches
protobuf.h
Go to the documentation of this file.
1// Copyright 2019-2026:
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
40namespace goby
41{
42namespace middleware
43{
45template <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
84template <> 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 // Use explicit raw pointer + reset for scan-build ownership tracking
128 msg.reset(
129 dccl::DynamicProtobufManager::new_protobuf_message<google::protobuf::Message*>(
130 type, user_pool_first));
131 }
132
133 msg->ParseFromArray(&*bytes_begin, bytes_end - bytes_begin);
134 actual_end = bytes_begin + msg->ByteSizeLong();
135 return msg;
136 }
137};
138
139namespace protobuf
140{
141namespace detail
142{
143// used to select between DCCL messages (with added DCCLParameters Enumeration)
144// and normal Protobuf messages
145// in the DCCL case, both "scheme_protobuf_or_dccl" functions are valid, but the one
146// with "dccl_selector" as the argument is chosen because this doesn't require
147// upcasting to "protobuf_selector"
148// in the plain Protobuf case, just the "scheme_protobuf_or_dccl(protobuf_selector)" function
149// is chosen by template resolution, so this one is used.
151{
152};
156
157template <typename T,
158 typename std::enable_if<std::is_enum<typename T::DCCLParameters>::value>::type* = nullptr>
163
164template <typename T> constexpr int scheme_protobuf_or_dccl(protobuf_selector)
165{
167}
168} // namespace detail
169} // namespace protobuf
170
171template <typename T, typename std::enable_if<
172 std::is_base_of<google::protobuf::Message, T>::value>::type* = nullptr>
173constexpr int scheme()
174{
175 return protobuf::detail::scheme_protobuf_or_dccl<T>(protobuf::detail::dccl_selector());
176}
177
178} // namespace middleware
179} // namespace goby
180
181#endif
const Descriptor * GetDescriptor() const
Definition message.h:357
detail namespace with internal helper functions
Definition json.hpp:263
constexpr int scheme_protobuf_or_dccl(dccl_selector)
Definition protobuf.h:159
constexpr int scheme()
Placeholder to provide an interface for the scheme() function family.
Definition cstr.h:65
The global namespace for the Goby project.
extern ::PROTOBUF_NAMESPACE_ID::internal::ExtensionIdentifier< ::PROTOBUF_NAMESPACE_ID::MessageOptions, ::PROTOBUF_NAMESPACE_ID::internal::MessageTypeTraits< ::goby::GobyMessageOptions >, 11, false > msg
Definition dccl.h:58
Enumeration and helper functions for marshalling scheme identification.
Definition interface.h:46
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
static std::string type_name(const DataType &d=DataType())
Full protobuf Message name, including package (if one is defined).
Definition protobuf.h:65
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
static std::vector< char > serialize(const google::protobuf::Message &msg)
Serialize Protobuf message (standard Protobuf encoding)
Definition protobuf.h:87
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
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
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