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>
140 template <
typename T>
145 template <
typename T>
150 template <
typename T>
155 template <
typename T>
160 template <typename T>
174 void (*copy)(
void const*,
void*);
175 void (*move)(
void*,
void*);
176 void (*to_value)(
void const*, google::protobuf::Value*);
178 bool (*equal_to)(
void const*,
void const*,
const vtable&);
183 template <
typename T>
185 static_assert(std::is_nothrow_destructible<T>{},
"T has a throwing destructor");
187 model(T t)
noexcept(std::is_nothrow_move_constructible<T>{});
189 static void dtor(
void* self)
noexcept;
191 static void copy(
void const* self,
void* dest);
195 static void move(
void* self,
void* dest);
197 static void to_value(
void const* self, google::protobuf::Value* v);
199 static Kind kind() noexcept;
201 static
bool equal_to(
void const* self,
void const* other, const vtable& other_vtable);
203 static constexpr vtable vtable_{dtor, copy, move, to_value, kind, equal_to};
215 using BufType = std::aligned_union_t<0,
221 std::unordered_map<std::string, void*>>;
223 static constexpr std::size_t local_storage_size =
sizeof(BufType);
224 static constexpr std::size_t local_storage_alignment =
alignof(BufType);
227 template <
typename T>
228 storage(T t)
noexcept(std::is_nothrow_move_constructible<T>{});
234 storage(
const storage&) =
delete;
235 storage(storage&&) =
delete;
236 storage& operator=(
const storage&) =
delete;
237 storage& operator=(storage&&) =
delete;
240 storage(
const storage& other,
const vtable& vtable);
243 storage(storage&& other,
244 const vtable& vtable)
noexcept(proto_value_details::all_moves_noexcept{});
248 void swap(
const vtable& this_vtable,
250 const vtable& other_vtable)
noexcept(proto_value_details::all_moves_noexcept{});
253 void destruct(
const vtable& vtable)
noexcept;
255 template <
typename T =
void>
257 return static_cast<T*
>(
static_cast<void*
>(&buf_));
260 template <
typename T =
void>
261 T
const* get()
const {
262 return static_cast<T const*
>(
static_cast<void const*
>(&buf_));
268 ProtoValue(
const google::protobuf::Value* value);
272 template <
typename T>
273 ProtoValue(T t, std::nullptr_t)
noexcept(std::is_nothrow_move_constructible<T>{});
281constexpr ProtoValue::vtable ProtoValue::model<T>::vtable_;
285using ProtoList = std::vector<ProtoValue>;
290using ProtoStruct = std::unordered_map<std::string, ProtoValue>;
294 std::is_nothrow_move_constructible<ProtoList>{});
296 std::is_nothrow_move_constructible<ProtoStruct>{});
299extern template bool& ProtoValue::get_unchecked<bool>();
300extern template double& ProtoValue::get_unchecked<double>();
302extern template bool ProtoValue::get_unchecked<bool>()
const;
303extern template double ProtoValue::get_unchecked<double>()
const;
306extern template std::string& ProtoValue::get_unchecked<std::string>() &;
307extern template ProtoList& ProtoValue::get_unchecked<ProtoList>() &;
308extern template ProtoStruct& ProtoValue::get_unchecked<ProtoStruct>() &;
310extern template std::string
const& ProtoValue::get_unchecked<std::string>() const&;
311extern template ProtoList const& ProtoValue::get_unchecked<ProtoList>() const&;
312extern template ProtoStruct const& ProtoValue::get_unchecked<ProtoStruct>() const&;
314extern template std::
string&& ProtoValue::get_unchecked<std::
string>() &&;
315extern template ProtoList&& ProtoValue::get_unchecked<ProtoList>() &&;
316extern template ProtoStruct&& ProtoValue::get_unchecked<ProtoStruct>() &&;
318namespace proto_convert_details {
321struct to_proto_impl<ProtoValue> {
322 void operator()(
const ProtoValue&, google::protobuf::Value*)
const;
326struct to_proto_impl<ProtoStruct> {
327 void operator()(
const ProtoStruct&, google::protobuf::Struct*)
const;
331struct from_proto_impl<google::protobuf::Value> {
332 ProtoValue operator()(
const google::protobuf::Value*)
const;
336struct from_proto_impl<google::protobuf::Struct> {
337 ProtoStruct operator()(
const google::protobuf::Struct*)
const;
342namespace proto_value_details {
344void to_value(std::nullptr_t, google::protobuf::Value* v);
345void to_value(
bool b, google::protobuf::Value* v);
346void to_value(
double d, google::protobuf::Value* v);
347void to_value(std::string s, google::protobuf::Value* v);
348void to_value(
const ProtoList& vec, google::protobuf::Value* v);
349void to_value(
const ProtoStruct& m, google::protobuf::Value* v);
354template <ProtoValue::Kind k>
355using KindConstant = std::integral_constant<ProtoValue::Kind, k>;
358struct kind<std::nullptr_t> {
359 using type = KindConstant<ProtoValue::Kind::k_null>;
364 using type = KindConstant<ProtoValue::Kind::k_bool>;
369 using type = KindConstant<ProtoValue::Kind::k_double>;
373struct kind<std::string> {
374 using type = KindConstant<ProtoValue::Kind::k_string>;
378struct kind<ProtoList> {
379 using type = KindConstant<ProtoValue::Kind::k_list>;
383struct kind<ProtoStruct> {
384 using type = KindConstant<ProtoValue::Kind::k_struct>;
396 if (this->is_a<T>()) {
397 return this->self_.template get<T>();
405 if (this->is_a<T>()) {
406 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.
T * get()
Return a T pointer if this is_a<T>(), else return nullptr.
Definition proto_value.hpp:395
std::enable_if_t< std::is_scalar< T >{}, T & > get_unchecked()
Return a reference to the underlying T, without checking.
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:390
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.
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:352
Definition proto_value.hpp:29