Goby3 3.5.1
2026.06.04
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// Copilot <198982749+Copilot@users.noreply.github.com>
7//
8//
9// This file is part of the Goby Underwater Autonomy Project Libraries
10// ("The Goby Libraries").
11//
12// The Goby Libraries are free software: you can redistribute them and/or modify
13// them under the terms of the GNU Lesser General Public License as published by
14// the Free Software Foundation, either version 2.1 of the License, or
15// (at your option) any later version.
16//
17// The Goby Libraries are distributed in the hope that they will be useful,
18// but WITHOUT ANY WARRANTY; without even the implied warranty of
19// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20// GNU Lesser General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public License
23// along with Goby. If not, see <http://www.gnu.org/licenses/>.
24
25#ifndef GOBY_MIDDLEWARE_MARSHALLING_PROTOBUF_H
26#define GOBY_MIDDLEWARE_MARSHALLING_PROTOBUF_H
27
28#include <mutex>
29
30#include <dccl/dynamic_protobuf_manager.h>
32
34
35#include "interface.h"
36
37#if GOOGLE_PROTOBUF_VERSION < 3001000
38#define ByteSizeLong ByteSize
39#endif
40
41namespace goby
42{
43namespace middleware
44{
46template <typename DataType>
48 DataType, MarshallingScheme::PROTOBUF,
49 std::enable_if_t<!std::is_same<DataType, google::protobuf::Message>::value>>
50{
52 static std::vector<char> serialize(const DataType& msg)
53 {
54 std::vector<char> bytes(msg.ByteSizeLong(), 0);
55 msg.SerializeToArray(bytes.data(), bytes.size());
56 return bytes;
57 }
58
66 static std::string type_name(const DataType& d = DataType())
67 {
68 return DataType::descriptor()->full_name();
69 }
70
72 template <typename CharIterator>
73 static std::shared_ptr<DataType> parse(CharIterator bytes_begin, CharIterator bytes_end,
74 CharIterator& actual_end,
75 const std::string& type = type_name())
76 {
77 auto msg = std::make_shared<DataType>();
78 msg->ParseFromArray(&*bytes_begin, bytes_end - bytes_begin);
79 actual_end = bytes_begin + msg->ByteSizeLong();
80 return msg;
81 }
82};
83
85template <> struct SerializerParserHelper<google::protobuf::Message, MarshallingScheme::PROTOBUF>
86{
88 static std::vector<char> serialize(const google::protobuf::Message& msg)
89 {
90 std::vector<char> bytes(msg.ByteSizeLong(), 0);
91 msg.SerializeToArray(bytes.data(), bytes.size());
92 return bytes;
93 }
94
98 static std::string type_name(const google::protobuf::Message& d)
99 {
100 return d.GetDescriptor()->full_name();
101 }
102
106 static std::string type_name(const google::protobuf::Descriptor* desc)
107 {
108 return desc->full_name();
109 }
110
118 template <typename CharIterator>
119 static std::shared_ptr<google::protobuf::Message>
120 parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator& actual_end,
121 const std::string& type, bool user_pool_first = false)
122 {
123 std::shared_ptr<google::protobuf::Message> msg;
124
125 {
126 static std::mutex dynamic_protobuf_manager_mutex;
127 std::lock_guard<std::mutex> lock(dynamic_protobuf_manager_mutex);
128 // Use explicit raw pointer + reset for scan-build ownership tracking
129 msg.reset(
130 dccl::DynamicProtobufManager::new_protobuf_message<google::protobuf::Message*>(
131 type, user_pool_first));
132 }
133
134 msg->ParseFromArray(&*bytes_begin, bytes_end - bytes_begin);
135 actual_end = bytes_begin + msg->ByteSizeLong();
136 return msg;
137 }
138};
139
140namespace protobuf
141{
142namespace detail
143{
144// used to select between DCCL messages (with added DCCLParameters Enumeration)
145// and normal Protobuf messages
146// in the DCCL case, both "scheme_protobuf_or_dccl" functions are valid, but the one
147// with "dccl_selector" as the argument is chosen because this doesn't require
148// upcasting to "protobuf_selector"
149// in the plain Protobuf case, just the "scheme_protobuf_or_dccl(protobuf_selector)" function
150// is chosen by template resolution, so this one is used.
152{
153};
157
158template <typename T,
159 typename std::enable_if<std::is_enum<typename T::DCCLParameters>::value>::type* = nullptr>
164
165template <typename T> constexpr int scheme_protobuf_or_dccl(protobuf_selector)
166{
168}
169} // namespace detail
170} // namespace protobuf
171
172template <typename T, typename std::enable_if<
173 std::is_base_of<google::protobuf::Message, T>::value>::type* = nullptr>
174constexpr int scheme()
175{
176 return protobuf::detail::scheme_protobuf_or_dccl<T>(protobuf::detail::dccl_selector());
177}
178
179} // namespace middleware
180} // namespace goby
181
182#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:160
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:73
static std::string type_name(const DataType &d=DataType())
Full protobuf Message name, including package (if one is defined).
Definition protobuf.h:66
static std::string type_name(const google::protobuf::Descriptor *desc)
Full protobuf name from descriptor, including package (if one is defined).
Definition protobuf.h:106
static std::vector< char > serialize(const google::protobuf::Message &msg)
Serialize Protobuf message (standard Protobuf encoding)
Definition protobuf.h:88
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:120
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:98
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