11#ifndef FRAMEWORK_VARIANT_H
12#define FRAMEWORK_VARIANT_H
22#include <initializer_list>
59template <VariantType V>
69template <VariantType V>
77template <VariantType V>
83template <VariantType V>
92template <VariantType V>
109struct variant_trait : std::integral_constant<VariantType, VariantType::Unknown> {
112#define DECLARE_VARIANT_TRAIT(_Type1_, _Type2_) \
114 struct variant_trait<_Type1_> : std::integral_constant<VariantType, VariantType::_Type2_> { \
193template <VariantType type>
197#define DECLARE_VARIANT_TYPE(_Type1_, _Type2_) \
199 struct variant_type<VariantType::_Type2_> { \
200 using type = _Type1_; \
231template <VariantType type>
235#define DECLARE_VARIANT_ARRAY_ELEMENT_TYPE(_Type1_, _Type2_) \
237 struct variant_array_element_type<VariantType::_Type2_> { \
238 using type = _Type1_; \
255template <VariantType V>
262 new (
reinterpret_cast<T*
>(store)) T{};
263 *(
reinterpret_cast<T*
>(store)) =
value;
265 static void set(
void* store, std::remove_pointer_t<T>
const*
values,
size_t size)
267 *
reinterpret_cast<T*
>(store) =
reinterpret_cast<T
>(std::memcpy(std::malloc(
size *
sizeof(std::remove_pointer_t<T>)),
reinterpret_cast<void const*
>(
values),
size *
sizeof(std::remove_pointer_t<T>)));
270 static T
get(
const void* store) {
return *(
reinterpret_cast<const T*
>(store)); }
276 static void set(
void* store, std::vector<std::string>
value)
278 auto ptr =
reinterpret_cast<std::vector<std::string>*
>(store);
279 new (
ptr) std::vector<std::string>{
value};
282 static std::vector<std::string>
const&
get(
const void* store) {
return *(
reinterpret_cast<std::vector<std::string> const*
>(store)); }
289 auto ptr =
reinterpret_cast<std::vector<std::string>*
>(store);
293 static std::string
const*
get(
const void* store) {
return (*(
reinterpret_cast<std::vector<std::string> const*
>(store))).data(); }
298 static const char*
get(
const void* store) {
return *
reinterpret_cast<const char* const*
>(store); }
300 static void set(
void* store,
const char*
value) { *
reinterpret_cast<char**
>(store) = strdup(
value); }
305 static std::string_view
get(
const void* store) {
return std::string_view(*
reinterpret_cast<const char* const*
>(store)); }
307 static void set(
void* store, std::string_view
value) { *
reinterpret_cast<char**
>(store) = strdup(
value.data()); }
312 static std::string
get(
const void* store) {
return std::string(*
reinterpret_cast<const char* const*
>(store)); }
314 static void set(
void* store, std::string
value) { *
reinterpret_cast<char**
>(store) = strdup(
value.data()); }
323 template <
typename T>
324 requires(!is_specialization_v<T, std::vector>)
330 template <
typename T>
337 template <
typename T>
349 template <
typename T>
352 template <
typename T>
355 static_assert(
sizeof(T) == 0,
356 "brace-enclosed initializer list forbidden for Variant"
357 "\n did you accidentally put braces around the default value?");
367 template <
typename T>
374 template <
typename T>
377 if (mType != variant_trait_v<T>) {
378 throw runtime_error_f(
"Variant::get: Mismatch between types %d %d.", mType, variant_trait_v<T>);
383 template <
typename T>
384 [[nodiscard]] std::string
const*
get() const
385 requires(
std::same_as<
std::
string*, T>)
394 template <
typename T>
400 template <
typename T>
406 template <
typename T>
408 requires(std::is_pod_v<T> || std::is_same_v<T, std::string>)
414 [[nodiscard]]
size_t size()
const {
return mSize; }
415 [[nodiscard]] std::string
asString()
const;
422 void destroyStore() noexcept;
423 using storage_t =
std::aligned_union<8,
int, int8_t, int16_t,
int64_t,
424 uint8_t, uint16_t, uint32_t, uint64_t,
425 const
char*,
float,
double,
bool,
426 int*,
float*,
double*,
bool*,
std::
string*,
#define DECLARE_VARIANT_TYPE(_Type1_, _Type2_)
#define DECLARE_VARIANT_TRAIT(_Type1_, _Type2_)
#define DECLARE_VARIANT_ARRAY_ELEMENT_TYPE(_Type1_, _Type2_)
Variant for configuration parameter storage. Owns stored data.
Variant(T values, size_t size)
void set(std::vector< T > &values)
std::string asString() const
Variant(std::vector< std::string > const &values)
Variant & operator=(std::vector< T > &&other)
Variant(std::vector< T > const &values)
Variant(std::vector< T > &&)=delete
Variant & operator=(const Variant &other)
friend std::ostream & operator<<(std::ostream &oss, Variant const &val)
void set(T value, size_t size)
Variant(std::initializer_list< T >)
std::string const * get() const
GLsizei const GLchar *const * string
GLsizei const GLfloat * value
GLint GLint GLsizei GLint GLenum GLenum type
GLenum GLsizei GLsizei GLint * values
Defining ITS Vertex explicitly as messageable.
constexpr auto isSimpleVariant()
constexpr auto isLabeledArrayString()
constexpr auto isLabeledArray()
constexpr auto isArray2D()
typename variant_array_element_type< V >::type variant_array_element_type_t
constexpr VariantType variant_trait_v
RuntimeErrorRef runtime_error_f(const char *,...)
static constexpr char symbol
static const char * get(const void *store)
static void set(void *store, const char *value)
static void set(void *store, std::string value)
static std::string get(const void *store)
static std::string const * get(const void *store)
static void set(void *store, std::string *values, size_t size)
static void set(void *store, std::string_view value)
static std::string_view get(const void *store)
static std::vector< std::string > const & get(const void *store)
static void set(void *store, std::vector< std::string > value)
static T get(const void *store)
static void set(void *store, std::remove_pointer_t< T > const *values, size_t size)
static void set(void *store, T value)
VectorOfTObjectPtrs other