Goby3  3.1.5a
2024.05.23
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 
41 namespace google
42 {
43 namespace protobuf
44 {
45 class Descriptor;
46 } // namespace protobuf
47 } // namespace google
48 
49 namespace goby
50 {
51 namespace middleware
52 {
53 namespace protobuf
54 {
55 class SerializerProtobufMetadata;
56 } // namespace protobuf
57 
58 namespace detail
59 {
62 {
63  private:
64  static std::unique_ptr<dccl::Codec> codec_;
65 
66  protected:
68 
69  struct LoaderBase
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:
129  DCCLSerializerParserHelperBase() = default;
130  virtual ~DCCLSerializerParserHelperBase() = default;
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
goby::middleware::detail::DCCLSerializerParserHelperBase::Loader::Loader
Loader()
Definition: dccl_serializer_parser.h:77
goby::middleware::detail::DCCLSerializerParserHelperBase::loaded_proto_files_
static std::set< std::string > loaded_proto_files_
Definition: dccl_serializer_parser.h:96
goby::middleware::detail::DCCLSerializerParserHelperBase::LoaderDynamic
Definition: dccl_serializer_parser.h:81
goby::middleware::detail::DCCLSerializerParserHelperBase::LoaderBase::~LoaderBase
virtual ~LoaderBase()=default
goby::middleware::detail::DCCLSerializerParserHelperBase::INVALID_DCCL_ID
constexpr static int INVALID_DCCL_ID
Definition: dccl_serializer_parser.h:132
goby::middleware::detail::DCCLSerializerParserHelperBase::LoaderBase
Definition: dccl_serializer_parser.h:69
goby::middleware::detail::DCCLSerializerParserHelperBase::load_library
static void load_library(const std::string &library)
Definition: dccl_serializer_parser.h:160
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::detail::DCCLSerializerParserHelperBase::check_load
static void check_load()
Definition: dccl_serializer_parser.h:98
goby::middleware::detail::DCCLSerializerParserHelperBase::Loader
Definition: dccl_serializer_parser.h:75
goby::util::FlexOstream::is_warn
bool is_warn()
Definition: flex_ostream.h:82
intervehicle.pb.h
goby::middleware::intervehicle::protobuf::DCCLForwardedData
Definition: intervehicle.pb.h:1343
goby::util::logger::mutex
std::recursive_mutex mutex
goby::middleware::detail::DCCLSerializerParserHelperBase::LoaderDynamic::~LoaderDynamic
~LoaderDynamic() override
Definition: dccl_serializer_parser.h:87
goby::middleware::detail::DCCLSerializerParserHelperBase::load_metadata
static void load_metadata(const goby::middleware::protobuf::SerializerProtobufMetadata &meta)
detail
detail namespace with internal helper functions
Definition: json.hpp:246
goby::middleware::detail::DCCLSerializerParserHelperBase::~DCCLSerializerParserHelperBase
virtual ~DCCLSerializerParserHelperBase()=default
goby::util::logger_lock::lock
@ lock
Definition: flex_ostreambuf.h:62
goby::middleware::detail::DCCLSerializerParserHelperBase::LoaderDynamic::LoaderDynamic
LoaderDynamic(const google::protobuf::Descriptor *desc)
Definition: dccl_serializer_parser.h:83
goby::middleware::detail::DCCLSerializerParserHelperBase::set_codec
static dccl::Codec & set_codec(dccl::Codec *new_codec)
Definition: dccl_serializer_parser.h:121
goby::middleware::detail::DCCLSerializerParserHelperBase::unpack
static goby::middleware::intervehicle::protobuf::DCCLForwardedData unpack(const std::string &bytes)
goby::middleware::detail::DCCLSerializerParserHelperBase::dccl_mutex_
static std::mutex dccl_mutex_
Definition: dccl_serializer_parser.h:67
goby::middleware::detail::DCCLSerializerParserHelperBase::codec
static dccl::Codec & codec()
Definition: dccl_serializer_parser.h:114
goby::middleware::detail::DCCLSerializerParserHelperBase::DCCLSerializerParserHelperBase
DCCLSerializerParserHelperBase()=default
goby::middleware::detail::DCCLSerializerParserHelperBase::setup_dlog
static void setup_dlog()
Enable dlog output to glog using same verbosity settings as glog.
flex_ostream.h
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::detail::DCCLSerializerParserHelperBase::id
static unsigned id(const std::string &full_name)
Definition: dccl_serializer_parser.h:140
goby::middleware::detail::DCCLSerializerParserHelperBase::loader_map_
static std::unordered_map< const google::protobuf::Descriptor *, std::unique_ptr< LoaderBase > > loader_map_
Definition: dccl_serializer_parser.h:94
goby::middleware::detail::DCCLSerializerParserHelperBase::Loader::~Loader
~Loader() override
Definition: dccl_serializer_parser.h:78
goby::middleware::protobuf::SerializerProtobufMetadata
Definition: serializer_transporter.pb.h:109
goby::middleware::detail::DCCLSerializerParserHelperBase::check_load
static void check_load(const google::protobuf::Descriptor *desc)
Definition: dccl_serializer_parser.h:106
goby::middleware::detail::DCCLSerializerParserHelperBase::id
static unsigned id(CharIterator begin, CharIterator end)
Definition: dccl_serializer_parser.h:134
goby::glog
util::FlexOstream glog
Access the Goby logger through this object.
google
Definition: dccl.h:57
goby::middleware::detail::DCCLSerializerParserHelperBase::LoaderBase::LoaderBase
LoaderBase()=default