Goby3  3.1.5a
2024.05.23
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 
32 namespace goby
33 {
34 namespace middleware
35 {
36 namespace hdf5
37 {
38 struct 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 
51 template <typename T>
52 void retrieve_default_value(T* val, const google::protobuf::FieldDescriptor* field_desc);
53 
54 template <typename T> void retrieve_empty_value(T* val);
55 template <typename T> T retrieve_empty_value()
56 {
57  T t;
59  return t;
60 }
61 
62 template <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 
70 template <typename T> void retrieve_single_present_value(T* val, PBMeta meta);
71 template <typename T> void retrieve_repeated_value(T* val, int index, PBMeta meta);
72 
73 template <>
74 void 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 }
86 template <> void retrieve_empty_value(std::int32_t* val)
87 {
88  *val = std::numeric_limits<std::int32_t>::max();
89 }
90 template <> 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 }
103 template <> 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 
117 template <>
118 void retrieve_default_value(std::uint32_t* val, const google::protobuf::FieldDescriptor* field_desc)
119 {
120  *val = field_desc->default_value_uint32();
121 }
122 template <> void retrieve_empty_value(std::uint32_t* val)
123 {
124  *val = std::numeric_limits<std::uint32_t>::max();
125 }
126 template <> void retrieve_single_present_value(std::uint32_t* val, PBMeta m)
127 {
128  *val = m.refl->GetUInt32(m.msg, m.field_desc);
129 }
130 template <> 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 
135 template <>
136 void retrieve_default_value(std::int64_t* val, const google::protobuf::FieldDescriptor* field_desc)
137 {
138  *val = field_desc->default_value_int64();
139 }
140 template <> void retrieve_empty_value(std::int64_t* val)
141 {
142  *val = std::numeric_limits<std::int64_t>::max();
143 }
144 template <> void retrieve_single_present_value(std::int64_t* val, PBMeta m)
145 {
146  *val = m.refl->GetInt64(m.msg, m.field_desc);
147 }
148 template <> 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 
153 template <>
154 void retrieve_default_value(std::uint64_t* val, const google::protobuf::FieldDescriptor* field_desc)
155 {
156  *val = field_desc->default_value_uint64();
157 }
158 template <> void retrieve_empty_value(std::uint64_t* val)
159 {
160  *val = std::numeric_limits<std::uint64_t>::max();
161 }
162 template <> void retrieve_single_present_value(std::uint64_t* val, PBMeta m)
163 {
164  *val = m.refl->GetUInt64(m.msg, m.field_desc);
165 }
166 template <> 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 
171 template <>
172 void retrieve_default_value(double* val, const google::protobuf::FieldDescriptor* field_desc)
173 {
174  *val = field_desc->default_value_double();
175 }
176 template <> void retrieve_empty_value(double* val)
177 {
178  *val = std::numeric_limits<double>::quiet_NaN();
179 }
180 template <> void retrieve_single_present_value(double* val, PBMeta m)
181 {
182  *val = m.refl->GetDouble(m.msg, m.field_desc);
183 }
184 template <> void retrieve_repeated_value(double* val, int index, PBMeta m)
185 {
186  *val = m.refl->GetRepeatedDouble(m.msg, m.field_desc, index);
187 }
188 
189 template <>
190 void retrieve_default_value(float* val, const google::protobuf::FieldDescriptor* field_desc)
191 {
192  *val = field_desc->default_value_float();
193 }
194 template <> void retrieve_empty_value(float* val)
195 {
196  *val = std::numeric_limits<float>::quiet_NaN();
197 }
198 template <> void retrieve_single_present_value(float* val, PBMeta m)
199 {
200  *val = m.refl->GetFloat(m.msg, m.field_desc);
201 }
202 template <> 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
208 template <>
209 void 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 }
214 template <> void retrieve_empty_value(unsigned char* val)
215 {
216  *val = std::numeric_limits<unsigned char>::max();
217 }
218 template <> 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 }
223 template <> 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 
229 template <>
230 void retrieve_default_value(std::string* val, const google::protobuf::FieldDescriptor* field_desc)
231 {
232  *val = field_desc->default_value_string();
233 }
234 template <> void retrieve_empty_value(std::string* val) { val->clear(); }
235 template <> void retrieve_single_value(std::string* val, PBMeta m)
236 {
237  *val = m.refl->GetString(m.msg, m.field_desc);
238 }
239 template <> 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
google::protobuf::Reflection::GetRepeatedFloat
virtual float GetRepeatedFloat(const Message &message, const FieldDescriptor *field, int index) const =0
goby::middleware::hdf5::retrieve_single_present_value
void retrieve_single_present_value(T *val, PBMeta meta)
goby::middleware::hdf5::retrieve_single_value
void retrieve_single_value(T *val, PBMeta m)
Definition: hdf5_protobuf_values.h:62
goby::middleware::hdf5::PBMeta::field_desc
const google::protobuf::FieldDescriptor * field_desc
Definition: hdf5_protobuf_values.h:47
google::protobuf::Reflection::GetRepeatedDouble
virtual double GetRepeatedDouble(const Message &message, const FieldDescriptor *field, int index) const =0
google::protobuf::Reflection::GetBool
virtual bool GetBool(const Message &message, const FieldDescriptor *field) const =0
goby
The global namespace for the Goby project.
Definition: acomms_constants.h:33
goby::middleware::hdf5::retrieve_empty_value
void retrieve_empty_value(T *val)
google::protobuf::Reflection::GetDouble
virtual double GetDouble(const Message &message, const FieldDescriptor *field) const =0
google::protobuf::Reflection::GetRepeatedString
virtual string GetRepeatedString(const Message &message, const FieldDescriptor *field, int index) const =0
google::protobuf::Reflection
Definition: message.h:412
goby::middleware::hdf5::retrieve_default_value
void retrieve_default_value(T *val, const google::protobuf::FieldDescriptor *field_desc)
google::protobuf::Reflection::GetInt32
virtual int32 GetInt32(const Message &message, const FieldDescriptor *field) const =0
google::protobuf::Reflection::HasField
virtual bool HasField(const Message &message, const FieldDescriptor *field) const =0
goby::middleware::hdf5::PBMeta::PBMeta
PBMeta(const google::protobuf::Reflection *r, const google::protobuf::FieldDescriptor *f, const google::protobuf::Message &m)
Definition: hdf5_protobuf_values.h:40
goby::middleware::hdf5::retrieve_repeated_value
void retrieve_repeated_value(T *val, int index, PBMeta meta)
google::protobuf::Reflection::GetRepeatedUInt32
virtual uint32 GetRepeatedUInt32(const Message &message, const FieldDescriptor *field, int index) const =0
google::protobuf::Reflection::GetRepeatedEnum
virtual const EnumValueDescriptor * GetRepeatedEnum(const Message &message, const FieldDescriptor *field, int index) const =0
message.h
google::protobuf::Reflection::GetInt64
virtual int64 GetInt64(const Message &message, const FieldDescriptor *field) const =0
goby::middleware::hdf5::PBMeta::msg
const google::protobuf::Message & msg
Definition: hdf5_protobuf_values.h:48
google::protobuf::Reflection::GetRepeatedInt32
virtual int32 GetRepeatedInt32(const Message &message, const FieldDescriptor *field, int index) const =0
goby::middleware::hdf5::PBMeta
Definition: hdf5_protobuf_values.h:38
google::protobuf::Reflection::GetRepeatedBool
virtual bool GetRepeatedBool(const Message &message, const FieldDescriptor *field, int index) const =0
google::protobuf::Message
Definition: message.h:189
google::protobuf::Reflection::GetUInt32
virtual uint32 GetUInt32(const Message &message, const FieldDescriptor *field) const =0
binary.h
google::protobuf::Reflection::GetRepeatedUInt64
virtual uint64 GetRepeatedUInt64(const Message &message, const FieldDescriptor *field, int index) const =0
google::protobuf::Reflection::GetEnum
virtual const EnumValueDescriptor * GetEnum(const Message &message, const FieldDescriptor *field) const =0
google::protobuf::Reflection::GetUInt64
virtual uint64 GetUInt64(const Message &message, const FieldDescriptor *field) const =0
google::protobuf::Reflection::GetString
virtual string GetString(const Message &message, const FieldDescriptor *field) const =0
goby::middleware::hdf5::PBMeta::refl
const google::protobuf::Reflection * refl
Definition: hdf5_protobuf_values.h:46
google::protobuf::Reflection::GetFloat
virtual float GetFloat(const Message &message, const FieldDescriptor *field) const =0
google::protobuf::Reflection::GetRepeatedInt64
virtual int64 GetRepeatedInt64(const Message &message, const FieldDescriptor *field, int index) const =0