 |
Flexiv AIDK APIs
1.2
|
Go to the documentation of this file.
13 #include <unordered_map>
20 template <
size_t arg1,
size_t... others>
24 struct static_max<arg>
26 static const size_t value = arg;
29 template <
size_t arg1,
size_t arg2,
size_t... others>
30 struct static_max<arg1, arg2, others...>
32 static const size_t value = arg1 >= arg2
33 ? static_max<arg1, others...>::value
34 : static_max<arg2, others...>::value;
37 template <
typename... Ts>
38 struct variant_helper;
40 template <
typename F,
typename... Ts>
41 struct variant_helper<F, Ts...>
43 inline static void destroy(
size_t id,
void *data)
45 if (
id ==
typeid(F).hash_code())
46 reinterpret_cast<F *
>(data)->~F();
51 inline static void move(
size_t old_t,
void *old_v,
void *new_v)
53 if (old_t ==
typeid(F).hash_code())
54 new (new_v) F(std::move(*
reinterpret_cast<F *
>(old_v)));
59 inline static void copy(
size_t old_t,
const void *old_v,
void *new_v)
61 if (old_t ==
typeid(F).hash_code())
62 new (new_v) F(*
reinterpret_cast<const F *
>(old_v));
69 struct variant_helper<>
71 inline static void destroy(
size_t id,
void *data) {}
72 inline static void move(
size_t old_t,
void *old_v,
void *new_v) {}
73 inline static void copy(
size_t old_t,
const void *old_v,
void *new_v) {}
76 template <
typename... Ts>
80 static const size_t data_size = static_max<
sizeof(Ts)...>::value;
81 static const size_t data_align = static_max<
alignof(Ts)...>::value;
83 using data_t =
typename std::aligned_storage<data_size, data_align>::type;
85 using helper_t = variant_helper<Ts...>;
87 static inline size_t invalid_type() {
return typeid(void).hash_code(); }
94 : type_id(invalid_type())
98 : type_id(old.type_id)
100 helper_t::copy(old.type_id, &old.data, &data);
104 : type_id(old.type_id)
106 helper_t::move(old.type_id, &old.data, &data);
111 std::swap(type_id, old.type_id);
112 std::swap(data, old.data);
117 template <
typename T>
120 return (type_id ==
typeid(T).hash_code());
123 bool valid() {
return (type_id != invalid_type()); }
125 template <
typename T,
typename... Args>
129 helper_t::destroy(type_id, &data);
130 new (&data) T(std::forward<Args>(args)...);
131 type_id =
typeid(T).hash_code();
134 template <
typename T>
138 if (type_id ==
typeid(T).hash_code())
139 return *
reinterpret_cast<T *
>(&data);
141 throw std::bad_cast();
147 using value_variant = variant<int, double, float, bool, std::string>;
T & get()
Definition: variant.hpp:135
variant< Ts... > & operator=(variant< Ts... > old)
Definition: variant.hpp:109
void set(Args &&... args)
Definition: variant.hpp:126
static void copy(size_t old_t, const void *old_v, void *new_v)
Definition: variant.hpp:59
bool is()
Definition: variant.hpp:118
~variant()
Definition: variant.hpp:144
variant(variant< Ts... > &&old)
Definition: variant.hpp:103
static void destroy(size_t id, void *data)
Definition: variant.hpp:71
variant(const variant< Ts... > &old)
Definition: variant.hpp:97
static void destroy(size_t id, void *data)
Definition: variant.hpp:43
variant()
Definition: variant.hpp:93
static void copy(size_t old_t, const void *old_v, void *new_v)
Definition: variant.hpp:73
static void move(size_t old_t, void *old_v, void *new_v)
Definition: variant.hpp:72
static void move(size_t old_t, void *old_v, void *new_v)
Definition: variant.hpp:51
variant< int, double, float, bool, std::string > value_variant
Definition: defs.hpp:319
bool valid()
Definition: variant.hpp:123