Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
dccl_serializer_parser.h
Go to the documentation of this file.
1// Copyright 2019-2021:
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_DETAIL_DCCL_SERIALIZER_PARSER_H
25#define GOBY_MIDDLEWARE_MARSHALLING_DETAIL_DCCL_SERIALIZER_PARSER_H
26
27#include <memory> // for unique_ptr
28#include <mutex> // for mutex, lock_guard
29#include <ostream> // for basic_ostream
30#include <set> // for set
31#include <string> // for string, operat...
32#include <unordered_map> // for unordered_map
33#include <utility> // for pair, make_pair
34
35#include <dccl/codec.h> // for Codec
36#include <dccl/dynamic_protobuf_manager.h> // for DynamicProtobu...
37
38#include "goby/middleware/protobuf/intervehicle.pb.h" // for DCCLForwardedData
39#include "goby/util/debug_logger/flex_ostream.h" // for operator<<
40
41namespace google
42{
43namespace protobuf
44{
45class Descriptor;
46} // namespace protobuf
47} // namespace google
48
49namespace goby
50{
51namespace middleware
52{
53namespace protobuf
54{
55class SerializerProtobufMetadata;
56} // namespace protobuf
57
58namespace detail
59{
62{
63 private:
64 static std::unique_ptr<dccl::Codec> codec_;
65
66 protected:
67 static std::mutex dccl_mutex_;
68
70 {
71 LoaderBase() = default;
72 virtual ~LoaderBase() = default;
73 };
74
75 template <typename DataType> struct Loader : public LoaderBase
76 {
77 Loader() { codec().load<DataType>(); }
78 ~Loader() override { codec().unload<DataType>(); }
79 };
80
81 struct LoaderDynamic : public LoaderBase
82 {
83 LoaderDynamic(const google::protobuf::Descriptor* desc) : desc_(desc)
84 {
85 codec().load(desc_);
86 }
87 ~LoaderDynamic() override { codec().unload(desc_); }
88
89 private:
90 const google::protobuf::Descriptor* desc_;
91 };
92
93 static std::unordered_map<const google::protobuf::Descriptor*, std::unique_ptr<LoaderBase>>
95
96 static std::set<std::string> loaded_proto_files_;
97
98 template <typename DataType> static void check_load()
99 {
100 const auto* desc = DataType::descriptor();
101 if (!loader_map_.count(desc))
102 loader_map_.insert(
103 std::make_pair(desc, std::unique_ptr<LoaderBase>(new Loader<DataType>())));
104 }
105
106 static void check_load(const google::protobuf::Descriptor* desc)
107 {
108 if (!loader_map_.count(desc))
109 loader_map_.insert(
110 std::make_pair(desc, std::unique_ptr<LoaderBase>(new LoaderDynamic(desc))));
111 }
112
113
114 static dccl::Codec& codec()
115 {
116 if (!codec_)
117 codec_ = std::make_unique<dccl::Codec>();
118 return *codec_;
119 }
120
121 static dccl::Codec& set_codec(dccl::Codec* new_codec)
122 {
123 codec_.reset(new_codec);
124 loader_map_.clear();
125 return *new_codec;
126 }
127
128 public:
131
132 constexpr static int INVALID_DCCL_ID{0};
133
134 template <typename CharIterator> static unsigned id(CharIterator begin, CharIterator end)
135 {
136 std::lock_guard<std::mutex> lock(dccl_mutex_);
137 return codec().id(begin, end);
138 }
139
140 static unsigned id(const std::string& full_name)
141 {
142 std::lock_guard<std::mutex> lock(dccl_mutex_);
143 auto* desc = dccl::DynamicProtobufManager::find_descriptor(full_name);
144 if (desc)
145 {
146 return codec().id(desc);
147 }
148 else
149 {
150 goby::glog.is_warn() && goby::glog << "No DCCL message found with name: " << full_name
151 << std::endl;
152 return 0;
153 }
154 }
155
158 unpack(const std::string& bytes);
159
160 static void load_library(const std::string& library)
161 {
162 std::lock_guard<std::mutex> lock(dccl_mutex_);
163 codec().load_library(library);
164 }
165
167 static void setup_dlog();
168};
169} // namespace detail
170
171} // namespace middleware
172} // namespace goby
173
174#endif
detail namespace with internal helper functions
Definition json.hpp:247
The global namespace for the Goby project.
util::FlexOstream glog
Access the Goby logger through this object.
Definition dccl.h:58
Wraps a dccl::Codec in a thread-safe way to make it usable by SerializerParserHelper.
static unsigned id(const std::string &full_name)
static void check_load(const google::protobuf::Descriptor *desc)
static dccl::Codec & set_codec(dccl::Codec *new_codec)
static void setup_dlog()
Enable dlog output to glog using same verbosity settings as glog.
static void load_metadata(const goby::middleware::protobuf::SerializerProtobufMetadata &meta)
static std::unordered_map< const google::protobuf::Descriptor *, std::unique_ptr< LoaderBase > > loader_map_
static goby::middleware::intervehicle::protobuf::DCCLForwardedData unpack(const std::string &bytes)
static unsigned id(CharIterator begin, CharIterator end)