Goby3 3.2.3
2025.05.13
Loading...
Searching...
No Matches
hdf5_protobuf_values.h
Go to the documentation of this file.
1// Copyright 2016-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_LOG_HDF5_HDF5_PROTOBUF_VALUES_H
25#define GOBY_MIDDLEWARE_LOG_HDF5_HDF5_PROTOBUF_VALUES_H
26
27#include <google/protobuf/descriptor.h>
29
30#include "goby/util/binary.h"
31
32namespace goby
33{
34namespace middleware
35{
36namespace hdf5
37{
38struct PBMeta
39{
40 PBMeta(const google::protobuf::Reflection* r, const google::protobuf::FieldDescriptor* f,
42 : refl(r), field_desc(f), msg(m)
43 {
44 }
45
47 const google::protobuf::FieldDescriptor* field_desc;
49};
50
51template <typename T>
52void retrieve_default_value(T* val, const google::protobuf::FieldDescriptor* field_desc);
53
54template <typename T> void retrieve_empty_value(T* val);
55template <typename T> T retrieve_empty_value()
56{
57 T t;
59 return t;
60}
61
62template <typename T> void retrieve_single_value(T* val, PBMeta m)
63{
64 if (m.field_desc->is_optional() && !m.refl->HasField(m.msg, m.field_desc))
66 else
68}
69
70template <typename T> void retrieve_single_present_value(T* val, PBMeta meta);
71template <typename T> void retrieve_repeated_value(T* val, int index, PBMeta meta);
72
73template <>
74void retrieve_default_value(std::int32_t* val, const google::protobuf::FieldDescriptor* field_desc)
75{
76 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_INT32)
77 {
78 *val = field_desc->default_value_int32();
79 }
80 else if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_ENUM)
81 {
82 const google::protobuf::EnumValueDescriptor* enum_desc = field_desc->default_value_enum();
83 *val = enum_desc->number();
84 }
85}
86template <> void retrieve_empty_value(std::int32_t* val)
87{
88 *val = std::numeric_limits<std::int32_t>::max();
89}
90template <> void retrieve_single_present_value(std::int32_t* val, PBMeta m)
91{
92 if (m.field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_INT32)
93 {
94 *val = m.refl->GetInt32(m.msg, m.field_desc);
95 }
96 else if (m.field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_ENUM)
97 {
98 const google::protobuf::EnumValueDescriptor* enum_desc =
99 m.refl->GetEnum(m.msg, m.field_desc);
100 *val = enum_desc->number();
101 }
102}
103template <> void retrieve_repeated_value(std::int32_t* val, int index, PBMeta m)
104{
105 if (m.field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_INT32)
106 {
107 *val = m.refl->GetRepeatedInt32(m.msg, m.field_desc, index);
108 }
109 else if (m.field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_ENUM)
110 {
111 const google::protobuf::EnumValueDescriptor* enum_desc =
112 m.refl->GetRepeatedEnum(m.msg, m.field_desc, index);
113 *val = enum_desc->number();
114 }
115}
116
117template <>
118void retrieve_default_value(std::uint32_t* val, const google::protobuf::FieldDescriptor* field_desc)
119{
120 *val = field_desc->default_value_uint32();
121}
122template <> void retrieve_empty_value(std::uint32_t* val)
123{
124 *val = std::numeric_limits<std::uint32_t>::max();
125}
126template <> void retrieve_single_present_value(std::uint32_t* val, PBMeta m)
127{
128 *val = m.refl->GetUInt32(m.msg, m.field_desc);
129}
130template <> void retrieve_repeated_value(std::uint32_t* val, int index, PBMeta m)
131{
132 *val = m.refl->GetRepeatedUInt32(m.msg, m.field_desc, index);
133}
134
135template <>
136void retrieve_default_value(std::int64_t* val, const google::protobuf::FieldDescriptor* field_desc)
137{
138 *val = field_desc->default_value_int64();
139}
140template <> void retrieve_empty_value(std::int64_t* val)
141{
142 *val = std::numeric_limits<std::int64_t>::max();
143}
144template <> void retrieve_single_present_value(std::int64_t* val, PBMeta m)
145{
146 *val = m.refl->GetInt64(m.msg, m.field_desc);
147}
148template <> void retrieve_repeated_value(std::int64_t* val, int index, PBMeta m)
149{
150 *val = m.refl->GetRepeatedInt64(m.msg, m.field_desc, index);
151}
152
153template <>
154void retrieve_default_value(std::uint64_t* val, const google::protobuf::FieldDescriptor* field_desc)
155{
156 *val = field_desc->default_value_uint64();
157}
158template <> void retrieve_empty_value(std::uint64_t* val)
159{
160 *val = std::numeric_limits<std::uint64_t>::max();
161}
162template <> void retrieve_single_present_value(std::uint64_t* val, PBMeta m)
163{
164 *val = m.refl->GetUInt64(m.msg, m.field_desc);
165}
166template <> void retrieve_repeated_value(std::uint64_t* val, int index, PBMeta m)
167{
168 *val = m.refl->GetRepeatedUInt64(m.msg, m.field_desc, index);
169}
170
171template <>
172void retrieve_default_value(double* val, const google::protobuf::FieldDescriptor* field_desc)
173{
174 *val = field_desc->default_value_double();
175}
176template <> void retrieve_empty_value(double* val)
177{
178 *val = std::numeric_limits<double>::quiet_NaN();
179}
180template <> void retrieve_single_present_value(double* val, PBMeta m)
181{
182 *val = m.refl->GetDouble(m.msg, m.field_desc);
183}
184template <> void retrieve_repeated_value(double* val, int index, PBMeta m)
185{
186 *val = m.refl->GetRepeatedDouble(m.msg, m.field_desc, index);
187}
188
189template <>
190void retrieve_default_value(float* val, const google::protobuf::FieldDescriptor* field_desc)
191{
192 *val = field_desc->default_value_float();
193}
194template <> void retrieve_empty_value(float* val)
195{
196 *val = std::numeric_limits<float>::quiet_NaN();
197}
198template <> void retrieve_single_present_value(float* val, PBMeta m)
199{
200 *val = m.refl->GetFloat(m.msg, m.field_desc);
201}
202template <> void retrieve_repeated_value(float* val, int index, PBMeta m)
203{
204 *val = m.refl->GetRepeatedFloat(m.msg, m.field_desc, index);
205}
206
207// used for bool
208template <>
209void retrieve_default_value(unsigned char* val, const google::protobuf::FieldDescriptor* field_desc)
210{
211 if (field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_BOOL)
212 *val = field_desc->default_value_bool();
213}
214template <> void retrieve_empty_value(unsigned char* val)
215{
216 *val = std::numeric_limits<unsigned char>::max();
217}
218template <> void retrieve_single_present_value(unsigned char* val, PBMeta m)
219{
220 if (m.field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_BOOL)
221 *val = m.refl->GetBool(m.msg, m.field_desc);
222}
223template <> void retrieve_repeated_value(unsigned char* val, int index, PBMeta m)
224{
225 if (m.field_desc->cpp_type() == google::protobuf::FieldDescriptor::CPPTYPE_BOOL)
226 *val = m.refl->GetRepeatedBool(m.msg, m.field_desc, index);
227}
228
229template <>
230void retrieve_default_value(std::string* val, const google::protobuf::FieldDescriptor* field_desc)
231{
232 *val = field_desc->default_value_string();
233}
234template <> void retrieve_empty_value(std::string* val) { val->clear(); }
235template <> void retrieve_single_value(std::string* val, PBMeta m)
236{
237 *val = m.refl->GetString(m.msg, m.field_desc);
238}
239template <> void retrieve_repeated_value(std::string* val, int index, PBMeta m)
240{
241 *val = m.refl->GetRepeatedString(m.msg, m.field_desc, index);
242}
243
244} // namespace hdf5
245} // namespace common
246} // namespace goby
247
248#endif
int32_t GetRepeatedInt32(const Message &message, const FieldDescriptor *field, int index) const
float GetFloat(const Message &message, const FieldDescriptor *field) const
std::string GetString(const Message &message, const FieldDescriptor *field) const
uint64_t GetUInt64(const Message &message, const FieldDescriptor *field) const
int64_t GetInt64(const Message &message, const FieldDescriptor *field) const
uint64_t GetRepeatedUInt64(const Message &message, const FieldDescriptor *field, int index) const
bool GetBool(const Message &message, const FieldDescriptor *field) const
bool HasField(const Message &message, const FieldDescriptor *field) const
double GetRepeatedDouble(const Message &message, const FieldDescriptor *field, int index) const
double GetDouble(const Message &message, const FieldDescriptor *field) const
uint32_t GetRepeatedUInt32(const Message &message, const FieldDescriptor *field, int index) const
const EnumValueDescriptor * GetRepeatedEnum(const Message &message, const FieldDescriptor *field, int index) const
const EnumValueDescriptor * GetEnum(const Message &message, const FieldDescriptor *field) const
int64_t GetRepeatedInt64(const Message &message, const FieldDescriptor *field, int index) const
int32_t GetInt32(const Message &message, const FieldDescriptor *field) const
uint32_t GetUInt32(const Message &message, const FieldDescriptor *field) const
float GetRepeatedFloat(const Message &message, const FieldDescriptor *field, int index) const
bool GetRepeatedBool(const Message &message, const FieldDescriptor *field, int index) const
std::string GetRepeatedString(const Message &message, const FieldDescriptor *field, int index) const
void retrieve_single_present_value(T *val, PBMeta meta)
void retrieve_repeated_value(T *val, int index, PBMeta meta)
void retrieve_default_value(T *val, const google::protobuf::FieldDescriptor *field_desc)
void retrieve_single_value(T *val, PBMeta m)
The global namespace for the Goby project.
const google::protobuf::Reflection * refl
const google::protobuf::Message & msg
const google::protobuf::FieldDescriptor * field_desc
PBMeta(const google::protobuf::Reflection *r, const google::protobuf::FieldDescriptor *f, const google::protobuf::Message &m)