6#include <unordered_map>
10#include <viam/sdk/common/proto_convert.hpp>
27namespace proto_value_details {
41 : std::integral_constant<bool,
42 std::is_nothrow_move_constructible<std::vector<move_may_throw>>{} &&
43 std::is_nothrow_move_constructible<
44 std::unordered_map<std::string, move_may_throw>>{}> {};
60 enum Kind { k_null = 0, k_bool = 1, k_double = 2, k_string = 3, k_list = 4, k_struct = 5 };
85 typename = std::enable_if_t<std::is_same<Val,
ProtoValue>{}>>
86 ProtoValue(std::vector<Val>)
noexcept(std::is_nothrow_move_constructible<std::vector<Val>>{});
90 typename = std::enable_if_t<std::is_same<Val, ProtoValue>{}>>
91 ProtoValue(std::unordered_map<std::string, Val>)
noexcept(
92 std::is_nothrow_move_constructible<std::unordered_map<std::string, Val>>{});
124 template <
typename T>
131 template <
typename T>
135 template <
typename T>
139 template <
typename T>
143 template <
typename T>
147 template <typename T>
161 void (*copy)(
void const*,
void*);
162 void (*move)(
void*,
void*);
163 void (*to_value)(
void const*, google::protobuf::Value*);
165 bool (*equal_to)(
void const*,
void const*,
const vtable&);
170 template <
typename T>
172 static_assert(std::is_nothrow_destructible<T>{},
"T has a throwing destructor");
174 model(T t)
noexcept(std::is_nothrow_move_constructible<T>{});
176 static void dtor(
void* self)
noexcept;
178 static void copy(
void const* self,
void* dest);
182 static void move(
void* self,
void* dest);
184 static void to_value(
void const* self, google::protobuf::Value* v);
186 static Kind kind() noexcept;
188 static
bool equal_to(
void const* self,
void const* other, const vtable& other_vtable);
190 static constexpr vtable vtable_{dtor, copy, move, to_value, kind, equal_to};
202 using BufType = std::aligned_union_t<0,
208 std::unordered_map<std::string, void*>>;
210 static constexpr std::size_t local_storage_size =
sizeof(BufType);
211 static constexpr std::size_t local_storage_alignment =
alignof(BufType);
214 template <
typename T>
215 storage(T t)
noexcept(std::is_nothrow_move_constructible<T>{});
221 storage(
const storage&) =
delete;
222 storage(storage&&) =
delete;
223 storage& operator=(
const storage&) =
delete;
224 storage& operator=(storage&&) =
delete;
227 storage(
const storage& other,
const vtable& vtable);
230 storage(storage&& other,
231 const vtable& vtable)
noexcept(proto_value_details::all_moves_noexcept{});
235 void swap(
const vtable& this_vtable,
237 const vtable& other_vtable)
noexcept(proto_value_details::all_moves_noexcept{});
240 void destruct(
const vtable& vtable)
noexcept;
242 template <
typename T =
void>
244 return static_cast<T*
>(
static_cast<void*
>(&buf_));
247 template <
typename T =
void>
248 T
const* get()
const {
249 return static_cast<T const*
>(
static_cast<void const*
>(&buf_));
255 ProtoValue(
const google::protobuf::Value* value);
259 template <
typename T>
260 ProtoValue(T t, std::nullptr_t)
noexcept(std::is_nothrow_move_constructible<T>{});
268constexpr ProtoValue::vtable ProtoValue::model<T>::vtable_;
272using ProtoList = std::vector<ProtoValue>;
277using ProtoStruct = std::unordered_map<std::string, ProtoValue>;
281 std::is_nothrow_move_constructible<ProtoList>{});
283 std::is_nothrow_move_constructible<ProtoStruct>{});
286extern template bool& ProtoValue::get_unchecked<bool>() &;
287extern template double& ProtoValue::get_unchecked<double>() &;
288extern template std::string& ProtoValue::get_unchecked<std::string>() &;
289extern template ProtoList& ProtoValue::get_unchecked<ProtoList>() &;
290extern template ProtoStruct& ProtoValue::get_unchecked<ProtoStruct>() &;
292extern template bool const& ProtoValue::get_unchecked<bool>() const&;
293extern template
double const& ProtoValue::get_unchecked<
double>() const&;
294extern template std::
string const& ProtoValue::get_unchecked<std::
string>() const&;
295extern template ProtoList const& ProtoValue::get_unchecked<ProtoList>() const&;
296extern template ProtoStruct const& ProtoValue::get_unchecked<ProtoStruct>() const&;
298extern template
bool&& ProtoValue::get_unchecked<
bool>() &&;
299extern template
double&& ProtoValue::get_unchecked<
double>() &&;
300extern template std::
string&& ProtoValue::get_unchecked<std::
string>() &&;
301extern template ProtoList&& ProtoValue::get_unchecked<ProtoList>() &&;
302extern template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;
304namespace proto_convert_details {
307struct to_proto_impl<ProtoValue> {
308 void operator()(
const ProtoValue&, google::protobuf::Value*)
const;
312struct to_proto_impl<ProtoStruct> {
313 void operator()(
const ProtoStruct&, google::protobuf::Struct*)
const;
317struct from_proto_impl<google::protobuf::Value> {
318 ProtoValue operator()(
const google::protobuf::Value*)
const;
322struct from_proto_impl<google::protobuf::Struct> {
323 ProtoStruct operator()(
const google::protobuf::Struct*)
const;
328namespace proto_value_details {
330void to_value(std::nullptr_t, google::protobuf::Value* v);
331void to_value(
bool b, google::protobuf::Value* v);
332void to_value(
double d, google::protobuf::Value* v);
333void to_value(std::string s, google::protobuf::Value* v);
334void to_value(
const ProtoList& vec, google::protobuf::Value* v);
335void to_value(
const ProtoStruct& m, google::protobuf::Value* v);
340template <ProtoValue::Kind k>
341using KindConstant = std::integral_constant<ProtoValue::Kind, k>;
344struct kind<std::nullptr_t> {
345 using type = KindConstant<ProtoValue::Kind::k_null>;
350 using type = KindConstant<ProtoValue::Kind::k_bool>;
355 using type = KindConstant<ProtoValue::Kind::k_double>;
359struct kind<std::string> {
360 using type = KindConstant<ProtoValue::Kind::k_string>;
364struct kind<ProtoList> {
365 using type = KindConstant<ProtoValue::Kind::k_list>;
369struct kind<ProtoStruct> {
370 using type = KindConstant<ProtoValue::Kind::k_struct>;
382 if (this->is_a<T>()) {
383 return this->self_.template get<T>();
391 if (this->is_a<T>()) {
392 return this->self_.template get<T>();
Type-erased value for storing google::protobuf::Value types. A ProtoValue can be nullptr,...
Definition proto_value.hpp:55
bool is_null() const
Convenience version of is_a<T> to check if the value is nullptr.
ProtoValue(ProtoValue &&other) noexcept(proto_value_details::all_moves_noexcept{})
Move construct this from other, leaving other in its unspecified-but-valid moved from state.
const T & get_unchecked() const &
Return an immutable reference to the underlying T, without checking.
T * get()
Return a T pointer if this is_a<T>(), else return nullptr.
Definition proto_value.hpp:381
ProtoValue & operator=(ProtoValue &&other) noexcept(proto_value_details::all_moves_noexcept{})
Move assignment from other, leaving other in its unspecified-but-valid moved from state.
ProtoValue() noexcept
Construct a null object.
bool is_a() const
Checks whether this ProtoT is an instance of type T.
Definition proto_value.hpp:376
friend bool operator==(const ProtoValue &lhs, const ProtoValue &rhs)
Test equality of two types.
Kind kind() const
Obtain enumerator constant representing the stored data type.
ProtoValue(std::unordered_map< std::string, Val >) noexcept(std::is_nothrow_move_constructible< std::unordered_map< std::string, Val > >{})
Construct from a ProtoStruct.
T & get_unchecked() &
Return a reference to the underlying T, without checking.
ProtoValue(std::vector< Val >) noexcept(std::is_nothrow_move_constructible< std::vector< Val > >{})
Construct from a ProtoList.
T const * get() const
Return a T pointer if this is_a<T>(), else return nullptr.
Kind
Type discriminator constants for possible values stored in a ProtoValue.
Definition proto_value.hpp:60
Definition proto_convert.hpp:18
Definition proto_value.hpp:44
Definition proto_value.hpp:338
Definition proto_value.hpp:29