Goby3  3.1.4
2024.02.22
dccl.h
Go to the documentation of this file.
1 // Copyright 2016-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_DCCL_H
25 #define GOBY_MIDDLEWARE_MARSHALLING_DCCL_H
26 
28 
29 #include "protobuf.h"
30 
31 namespace goby
32 {
33 namespace middleware
34 {
35 namespace protobuf
36 {
37 class InterVehicleSubscription;
38 } // namespace protobuf
39 
43 template <typename DataType>
46 {
47  public:
49  static std::vector<char> serialize(const DataType& msg)
50  {
51  std::lock_guard<std::mutex> lock(dccl_mutex_);
52  check_load<DataType>();
53  std::vector<char> bytes(codec().size(msg), 0);
54  codec().encode(bytes.data(), bytes.size(), msg);
55  return bytes;
56  }
57 
65  static std::string type_name(const DataType& d = DataType())
66  {
67  return DataType::descriptor()->full_name();
68  }
69 
73  template <typename CharIterator>
74  static std::shared_ptr<DataType> parse(CharIterator bytes_begin, CharIterator bytes_end,
75  CharIterator& actual_end,
76  const std::string& type = type_name())
77  {
78  std::lock_guard<std::mutex> lock(dccl_mutex_);
79  check_load<DataType>();
80  auto msg = std::make_shared<DataType>();
81  actual_end = codec().decode(bytes_begin, bytes_end, msg.get());
82  return msg;
83  }
84 
94  static unsigned id()
95  {
96  std::lock_guard<std::mutex> lock(dccl_mutex_);
97  check_load<DataType>();
98  return codec().template id<DataType>();
99  }
100 
101  static unsigned id(const google::protobuf::Message& d) { return id(); }
102 
103  private:
104 };
105 
107 template <>
110 {
111  public:
113  static std::vector<char> serialize(const google::protobuf::Message& msg)
114  {
115  std::lock_guard<std::mutex> lock(dccl_mutex_);
116  check_load(msg.GetDescriptor());
117  std::vector<char> bytes(codec().size(msg), 0);
118  codec().encode(bytes.data(), bytes.size(), msg);
119  return bytes;
120  }
121 
125  static std::string type_name(const google::protobuf::Message& d)
126  {
127  return type_name(d.GetDescriptor());
128  }
129 
133  static std::string type_name(const google::protobuf::Descriptor* desc)
134  {
135  return desc->full_name();
136  }
137 
145  template <typename CharIterator>
146  static std::shared_ptr<google::protobuf::Message>
147  parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator& actual_end,
148  const std::string& type, bool user_pool_first = false)
149  {
150  std::lock_guard<std::mutex> lock(dccl_mutex_);
151 
152  auto msg = dccl::DynamicProtobufManager::new_protobuf_message<
153  std::shared_ptr<google::protobuf::Message>>(type, user_pool_first);
154 
155  check_load(msg->GetDescriptor());
156  actual_end = codec().decode(bytes_begin, bytes_end, msg.get());
157  return msg;
158  }
159 
161  static unsigned id(const google::protobuf::Descriptor* desc)
162  {
163  std::lock_guard<std::mutex> lock(dccl_mutex_);
164  check_load(desc);
165  return codec().id(desc);
166  }
167 
169  static unsigned id(const google::protobuf::Message& d) { return id(d.GetDescriptor()); }
170 
171  private:
172 };
173 
174 } // namespace middleware
175 } // namespace goby
176 
177 #endif
goby::middleware::MarshallingScheme
Enumeration and helper functions for marshalling scheme identification.
Definition: interface.h:45
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::DCCL >::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 DCCL/Protobuf message (using DCCL decoding) given the Protobuf type name and assuming the messa...
Definition: dccl.h:147
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::DCCL >::id
static unsigned id(const google::protobuf::Message &d)
Returns the DCCL ID given an instantiated message.
Definition: dccl.h:169
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::DCCL >::serialize
static std::vector< char > serialize(const google::protobuf::Message &msg)
Serialize DCCL/Protobuf message (using DCCL encoding)
Definition: dccl.h:113
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
protobuf.h
goby::util::logger_lock::lock
@ lock
Definition: flex_ostreambuf.h:62
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::DCCL >::id
static unsigned id()
Returns the DCCL ID.
Definition: dccl.h:94
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::DCCL >::id
static unsigned id(const google::protobuf::Message &d)
Definition: dccl.h:101
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::DCCL >::parse
static std::shared_ptr< DataType > parse(CharIterator bytes_begin, CharIterator bytes_end, CharIterator &actual_end, const std::string &type=type_name())
Parse one DCCL message.
Definition: dccl.h:74
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< DataType, MarshallingScheme::DCCL >::type_name
static std::string type_name(const DataType &d=DataType())
Full protobuf Message name (identical to Protobuf specialization)
Definition: dccl.h:65
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
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::DCCL >::type_name
static std::string type_name(const google::protobuf::Descriptor *desc)
Full protobuf name from descriptor, including package (if one is defined).
Definition: dccl.h:133
google::protobuf::Message
Definition: message.h:189
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::DCCL >::id
static unsigned id(const google::protobuf::Descriptor *desc)
Returns the DCCL ID given a Protobuf Descriptor.
Definition: dccl.h:161
google::protobuf::Message::GetDescriptor
const Descriptor * GetDescriptor() const
Definition: message.h:336
goby::middleware::detail::DCCLSerializerParserHelperBase
Wraps a dccl::Codec in a thread-safe way to make it usable by SerializerParserHelper.
Definition: dccl_serializer_parser.h:61
goby::middleware::SerializerParserHelper< google::protobuf::Message, MarshallingScheme::DCCL >::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: dccl.h:125
goby::middleware::SerializerParserHelper< DataType, MarshallingScheme::DCCL >::serialize
static std::vector< char > serialize(const DataType &msg)
Serialize message using DCCL encoding.
Definition: dccl.h:49
goby::middleware::MarshallingScheme::DCCL
@ DCCL
Definition: interface.h:54
dccl_serializer_parser.h
google
Definition: dccl.h:57