31#ifndef FENNEC_CONTAINERS_VARIANT_H
32#define FENNEC_CONTAINERS_VARIANT_H
36#include <fennec/math/ext/common.h>
45template<
typename...TypesT>
51 not (is_reference_v<TypesT> or ...) and
52 not (is_array_v<TypesT> or ...) and
53 not (is_void_v<TypesT> or ...)
60 static constexpr size_t nulltype =
sizeof...(TypesT);
73 fennec::construct<construct_t>(_handle);
85 using same_t = search_element_args<is_same, type_sequence<T>, TypesT...>;
86 using convert_t = search_element_args<is_constructible, type_sequence<T>, TypesT...>;
88 fennec::construct<construct_t>(_handle, fennec::forward<T>(t));
95 template<
typename T,
typename...ArgsT>
100 fennec::construct<T>(_handle, fennec::forward<ArgsT>(args)...);
101 _type = find_element_v<T>;
111 if (v._type == nulltype) {
116 fennec::construct<TypesT>(_handle, v.get<TypesT>()) :
129 if (v._type == nulltype) {
133 ((v._type == find_element_v<TypesT, TypesT...> ?
134 fennec::construct<TypesT>(_handle, fennec::move(v.get<TypesT>())) :
153 if constexpr((contains_element_v<T, TypesT> or ...)) {
155 if (_type == find_element_v<type_t, TypesT...>) {
156 *
static_cast<type_t*
>(_handle) = fennec::forward<T>(t);
159 fennec::construct<type_t>(_handle, fennec::forward<T>(t));
160 _type = find_element_v<type_t, TypesT...>;
166 bool assigned =
false;
167 if (_type != nulltype) {
169 (*
static_cast<TypesT*
>(_handle) = fennec::forward<T>(t), assigned =
true) :
180 using construct_t = search_element_args<is_constructible, type_sequence<T>, TypesT...>;
181 fennec::construct<construct_t>(_handle, fennec::forward<T>(t));
186 void emplace(ArgsT&&...args) {
188 fennec::construct<T>(_handle, fennec::forward<ArgsT>(args)...);
191 template<
size_t I,
typename...ArgsT>
192 void emplace(ArgsT&&...args) {
193 using type_t = nth_element_t<I, TypesT...>;
195 fennec::construct<type_t>(fennec::forward<ArgsT>(args)...);
203 return *
static_cast<T*
>(_handle);
207 const T& get()
const {
208 return *
static_cast<T*
>(_handle);
211 template<
size_t I,
typename T = nth_element_t<I, TypesT...>>
requires(
contains_element_v<T, TypesT...>)
213 return *
static_cast<T*
>(_handle);
216 template<
size_t I,
typename T = nth_element_t<I, TypesT...>>
requires(
contains_element_v<T, TypesT...>)
217 const T& get()
const {
218 return *
static_cast<T*
>(_handle);
232 if (_type == nulltype) {
237 fennec::destruct<TypesT>(_handle) :
typename conditional< B, TrueT, FalseT >::type conditional_t
Shorthand for typename conditional<ConditionV, TrueT, FalseT>::type
Definition conditional_types.h:90
A header containing the definition for a container with an optionally present variable.
Check if ClassT is default constructible.
Definition type_traits.h:1110
Base Class for Type Transformations.
Definition type_identity.h:49
A structure that represents a union between TypesT...
Definition variant.h:46
variant(variant &&v) noexcept
Move Constructor.
Definition variant.h:125
variant(const variant &v)
Copy Constructor.
Definition variant.h:107
~variant()
Destructor, if a type is held, destruct it.
Definition variant.h:142
variant(T &&t)
Conversion Constructor, constructs the type in TypesT... that is identical to T or the first that is ...
Definition variant.h:82
variant()
Default Constructor, constructs the first type in TypesT... that is default constructible.
Definition variant.h:69
variant(type_identity< T >, ArgsT &&...args)
Emplace Constructor, constructs the first type in TypesT... that is constructible with ArgsT....
Definition variant.h:96
constexpr bool contains_element_v
Shorthand for contains_element_v<T, Ts...>::value
Definition type_sequences.h:181
constexpr size_t max_element_size_v
Shorthand for max_element_size<Ts...>::value
Definition type_sequences.h:123
constexpr bool is_unique_v
Shorthand for is_unique<Ts...>::value
Definition type_sequences.h:202
constexpr size_t find_element_v
Shorthand for find_element<T, Ts...>::value
Definition type_sequences.h:139
search_element< SearchT, TypesT... >::type search_element_t
Shorthand for search_element_t<T, Ts...>::type
Definition type_sequences.h:156
unsigned char byte_t
A type capable of holding a single byte.
Definition types.h:216