1#ifndef NEFORCE_CORE_UTILITY_VARIANT_HPP__
2#define NEFORCE_CORE_UTILITY_VARIANT_HPP__
16NEFORCE_BEGIN_NAMESPACE__
30template <
typename Variant,
typename T>
33#ifdef NEFORCE_STANDARD_14
38template <
typename Variant,
typename T>
48template <
typename Variant,
size_t Idx>
55template <
typename Variant,
size_t Idx>
67template <
typename... Types>
69 static_assert(
sizeof...(Types) > 0,
"variant must have at least one type");
73 "if variant holds none, it should be in the first place");
85 alignas(_NEFORCE
max({
alignof(Types)...}))
byte_t union_[_NEFORCE
max({
sizeof(Types)...})]{};
87 using destruct_function = void (*)(
byte_t*);
95 static destruct_function* destructors_table()
noexcept {
96 static destruct_function function_ptrs[
sizeof...(Types)] = {
98 [](
byte_t*
const union_p)
noexcept {
reinterpret_cast<Types*
>(union_p)->~Types(); }...};
102 using copy_construct_function = void (*)(
byte_t*,
byte_t const*);
110 static copy_construct_function* copy_constructors_table()
noexcept {
111 static copy_construct_function function_ptrs[
sizeof...(Types)] = {
113 [](
byte_t*
const union_dst,
byte_t const* union_src)
noexcept {
114 new (union_dst) Types(*
reinterpret_cast<Types const*
>(union_src));
116 return function_ptrs;
119 using copy_assignment_function = void (*)(
byte_t*,
byte_t const*);
127 static copy_assignment_function* copy_assigment_functions_table()
noexcept {
128 static copy_assignment_function function_ptrs[
sizeof...(Types)] = {
129 [](
byte_t* union_dst,
byte_t const* union_src)
noexcept {
130 *
reinterpret_cast<Types*
>(union_dst) = *
reinterpret_cast<Types const*
>(union_src);
132 return function_ptrs;
135 using move_construct_function = void (*)(
byte_t*,
const byte_t*);
143 static move_construct_function* move_constructors_table()
noexcept {
144 static move_construct_function function_ptrs[
sizeof...(Types)] = {
146 [](
byte_t*
const union_dst,
const byte_t* union_src)
noexcept {
147 new (union_dst) Types(_NEFORCE
move(*
reinterpret_cast<const Types*
>(union_src)));
149 return function_ptrs;
152 using move_assignment_function = void (*)(
byte_t*,
byte_t*);
160 static move_assignment_function* move_assigment_functions_table()
noexcept {
161 static move_assignment_function function_ptrs[
sizeof...(Types)] = {
163 [](
byte_t* union_dst,
byte_t*
const union_src)
noexcept {
164 *
reinterpret_cast<Types*
>(union_dst) = _NEFORCE
move(*
reinterpret_cast<Types*
>(union_src));
166 return function_ptrs;
169 template <
typename Lambda>
180 template <
typename Lambda>
181 static const_visitor_function<Lambda>* const_visitors_table()
noexcept {
182 static const_visitor_function<Lambda> function_ptrs[
sizeof...(Types)] = {
186 return function_ptrs;
189 template <
typename Lambda>
190 using visitor_function =
200 template <
typename Lambda>
201 static visitor_function<Lambda>* visitors_table()
noexcept {
202 static visitor_function<Lambda> function_ptrs[
sizeof...(Types)] = {
208 return function_ptrs;
211 template <
size_t I,
typename... Args,
213 constexpr bool try_construct_impl_aux_aux(Args&&... args) {
219 template <
size_t I,
typename... Args,
221 constexpr bool try_construct_impl_aux_aux(Args&&... args) {
222 return variant::try_construct_impl<I + 1>(_NEFORCE
forward<Args>(args)...);
225 template <
size_t I,
typename... Args,
enable_if_t<(I <
sizeof...(Types)),
int> = 0>
226 constexpr bool try_construct_impl_aux(Args&&... args) {
227 return variant::try_construct_impl_aux_aux<I>(_NEFORCE
forward<Args>(args)...);
230 template <
size_t I,
typename... Args,
enable_if_t<(I >=
sizeof...(Types)),
int> = 0>
231 constexpr bool try_construct_impl_aux(Args&&... ) {
235 template <
size_t I,
typename... Args>
236 constexpr bool try_construct_impl(Args&&... args) {
237 return variant::try_construct_impl_aux<I>(_NEFORCE
forward<Args>(args)...);
240 template <
size_t I = 0,
typename... Args>
241 constexpr bool try_construct(Args&&... args) {
242 return variant::try_construct_impl<I>(_NEFORCE
forward<Args>(args)...);
245 template <
typename T,
typename U>
249 template <
typename T,
typename U>
255 template <
typename T,
typename U>
259 template <
typename T,
typename U>
282 template <
typename T, enable_if_t<disjunction_v<is_same<T, Types>...>,
int> = 0>
285 T* p =
reinterpret_cast<T*
>(union_);
296 index_(other.index_) {
297 copy_constructors_table()[
index()](union_, other.union_);
311 index_ = other.index_;
312 copy_assigment_functions_table()[
index()](union_, other.union_);
323 index_(other.index_) {
324 move_constructors_table()[
index()](union_, other.union_);
338 index_ = other.index_;
339 move_assigment_functions_table()[
index()](union_, other.union_);
351 template <
size_t Idx,
typename... Args,
353 NEFORCE_CONSTEXPR20
explicit variant(pass_size_construct_tag<Idx> , Args&&... args)
noexcept(
369 template <
size_t Idx,
typename U,
typename... Args,
373 pass_size_construct_tag<Idx> , std::initializer_list<U> ilist,
375 std::initializer_list<U>&, Args...>) :
389 if (!variant::try_construct(_NEFORCE
forward<Args>(args)...)) {
400 NEFORCE_CONSTEXPR20
~variant() noexcept { destructors_table()[
index()](union_); }
410 template <
typename Lambda, enable_if_t<conjunction_v<is_invocable<Lambda, Types&>...>,
int> = 0>
424 template <
typename Lambda, enable_if_t<conjunction_v<is_invocable<Lambda, const Types&>...>,
int> = 0>
434 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
size_t index() const noexcept {
return index_; }
452 template <
size_t Idx,
enable_if_t<(Idx <
sizeof...(Types)),
int> = 0>
466 template <
typename T>
467 NEFORCE_CONSTEXPR20 T&
get() {
477 template <
size_t Idx,
enable_if_t<(Idx <
sizeof...(Types)),
int> = 0>
491 template <
typename T>
492 NEFORCE_CONSTEXPR20 T
const&
get()
const {
501 template <
size_t Idx,
enable_if_t<(Idx <
sizeof...(Types)),
int> = 0>
514 template <
typename T>
515 NEFORCE_CONSTEXPR20 T*
get_if() noexcept {
524 template <
size_t Idx,
enable_if_t<(Idx <
sizeof...(Types)),
int> = 0>
537 template <
typename T>
538 NEFORCE_CONSTEXPR20 T
const*
get_if() const noexcept {
550 template <
size_t Idx,
typename... Args,
553 NEFORCE_CONSTEXPR20
void
555 destructors_table()[
index()](union_);
584 size_t this_index = index_;
585 const size_t other_index = other.index_;
586 alignas(_NEFORCE
max({
alignof(Types)...}))
byte_t temp_union[_NEFORCE
max({
sizeof(Types)...})];
587 move_constructors_table()[this_index](
reinterpret_cast<byte_t*
>(temp_union), union_);
588 destructors_table()[this_index](union_);
590 move_constructors_table()[other_index](union_, other.union_);
591 destructors_table()[other_index](other.union_);
592 move_constructors_table()[this_index](other.union_,
reinterpret_cast<byte_t*
>(temp_union));
593 destructors_table()[this_index](
reinterpret_cast<byte_t*
>(temp_union));
595 index_ = other_index;
596 other.index_ = this_index;
607 if (index_ != rhs.index_) {
610 return this->
visit([&](
const auto& value) {
611 return rhs.
visit([&](
const auto& other_value) {
return variant::equal_to_impl(value, other_value); });
621 if (index_ != rhs.index_) {
624 return this->
visit([&](
const auto& value) {
625 return rhs.
visit([&](
const auto& other_value) {
return variant::less_than_impl(value, other_value); });
633 NEFORCE_NODISCARD NEFORCE_CONSTEXPR20
size_t to_hash()
const;
636#ifdef NEFORCE_STANDARD_17
637template <
typename... Args>
642template <
typename T,
typename... Types>
646template <
typename T,
typename... Types,
size_t Idx>
648 using type =
typename variant_alternative<variant<Types...>, Idx - 1>::type;
651template <
typename T,
typename... Types>
653 static constexpr size_t value = 0;
655template <
typename T0,
typename T,
typename... Types>
657 static constexpr size_t value = variant_index<variant<Types...>, T>::value + 1;
668template <
size_t Idx,
typename... Types>
671 using variant_type =
variant<Types...>;
672 return static_cast<T&
>(
static_cast<variant_type&
>(v).template
get<Idx>());
682template <
size_t Idx,
typename... Types>
685 using variant_type =
variant<Types...>;
686 return static_cast<const T&
>(
static_cast<const variant_type&
>(v).template
get<Idx>());
696template <
size_t Idx,
typename... Types>
699 using variant_type =
variant<Types...>;
700 return static_cast<T&&
>(
static_cast<variant_type&&
>(v).template
get<Idx>());
710template <
size_t Idx,
typename... Types>
713 using variant_type =
variant<Types...>;
714 return static_cast<const T&&
>(
static_cast<const variant_type&&
>(v).template
get<Idx>());
721struct __variant_elem_hasher {
722 template <
typename T>
723 constexpr size_t operator()(
const T& value)
const {
724 return hash<decay_t<T>>{}(value);
729template <
typename... Types>
731 constexpr inner::__variant_elem_hasher hasher{};
739NEFORCE_END_NAMESPACE__
constexpr T && forward(remove_reference_t< T > &x) noexcept
完美转发左值
constexpr T * addressof(T &x) noexcept
获取对象的地址
constexpr const T & max(const T &a, const T &b, Compare comp) noexcept(noexcept(comp(a, b)))
返回两个值中的较大者
unsigned char byte_t
字节类型,定义为无符号字符
constexpr inner::__invoke_result_aux< Callable, Args... >::type invoke(Callable &&f, Args &&... args) noexcept(is_nothrow_invocable< Callable, Args... >::value)
统一调用接口
typename inner::__invoke_result_aux< F, Args... >::type invoke_result_t
invoke_result的便捷别名
constexpr Iterator2 move(Iterator1 first, Iterator1 last, Iterator2 result) noexcept(noexcept(inner::__move_aux(first, last, result)))
移动范围元素
typename get_first_para< Types... >::type get_first_para_t
get_first_para的便捷别名
typename common_type< Types... >::type common_type_t
common_type的便捷别名
constexpr bool is_constructible_v
is_constructible的便捷变量模板
constexpr bool is_nothrow_default_constructible_v
is_nothrow_default_constructible的便捷变量模板
constexpr bool is_nothrow_move_constructible_v
is_nothrow_move_constructible的便捷变量模板
constexpr bool is_nothrow_constructible_v
is_nothrow_constructible的便捷变量模板
constexpr bool conjunction_v
conjunction的便捷变量模板
constexpr bool is_any_of_v
is_any_of的便捷变量模板
constexpr bool disjunction_v
disjunction的便捷变量模板
typename enable_if< Test, T >::type enable_if_t
enable_if的便捷别名
constexpr variant_alternative_t< variant< Types... >, Idx > & get(variant< Types... > &v)
获取变体中指定索引位置的引用
constexpr size_t variant_index_v
variant_index的便捷别名,获取类型在变体中的索引值
typename variant_alternative< Variant, Idx >::type variant_alternative_t
variant_alternative的便捷别名,获取变体中指定索引位置的类型
constexpr T const * get_if() const noexcept
如果存在,获取指定类型值的常量指针
constexpr variant(pass_size_construct_tag< Idx >, Args &&... args) noexcept(is_nothrow_constructible_v< variant_alternative_t< variant, Idx >, Args... >)
参数列表原地构造函数
constexpr variant() noexcept(is_nothrow_default_constructible_v< variant_alternative_t< variant, 0 > >)
默认构造函数
variant(Args &&... args)
通用构造函数
constexpr variant & operator=(const variant &other)
拷贝赋值运算符
constexpr variant_alternative_t< variant, Idx > const & get() const
获取指定索引位置的常量引用
constexpr void emplace(Args &&... args) noexcept(is_nothrow_constructible_v< variant_alternative_t< variant, Idx >, Args... >)
constexpr variant(variant &&other) noexcept
移动构造函数
constexpr size_t to_hash() const
计算变体的哈希值
constexpr size_t index() const noexcept
获取当前存储类型的索引
constexpr variant(pass_size_construct_tag< Idx >, std::initializer_list< U > ilist, Args &&... args) noexcept(is_nothrow_constructible_v< variant_alternative_t< variant, Idx >, std::initializer_list< U > &, Args... >)
初始化列表原地构造函数
constexpr bool less_than(const variant &rhs) const
小于比较运算符
constexpr T const & get() const
获取指定类型值的常量引用
constexpr common_type_t< invoke_result_t< Lambda, Types & >... > visit(Lambda &&lambda) noexcept(conjunction_v< is_nothrow_invocable< Lambda, Types & >... >)
访问变体值
constexpr bool equal_to(const variant &rhs) const
相等比较运算符
constexpr T * get_if() noexcept
如果存在,获取指定类型值的指针
constexpr common_type_t< invoke_result_t< Lambda, const Types & >... > visit(Lambda &&lambda) const noexcept(conjunction_v< is_nothrow_invocable< Lambda, const Types & >... >)
常量访问变体值
constexpr bool holds_alternative() const noexcept
检查是否存储特定类型的值
constexpr variant(const variant &other)
拷贝构造函数
constexpr variant(T &&value) noexcept(is_nothrow_move_constructible_v< T >)
从特定类型值构造
constexpr variant_alternative_t< variant, Idx > & get()
获取指定索引位置的引用
constexpr variant & operator=(variant &&other) noexcept
移动赋值运算符
constexpr void emplace(Args &&... args) noexcept(is_nothrow_constructible_v< T, Args... >)
构造指定类型的新值
constexpr T & get()
获取指定类型值的引用
constexpr void swap(variant &other) noexcept
交换两个变体的内容
constexpr variant_alternative_t< variant, Idx > const * get_if() const noexcept
如果存在,获取指定索引位置的常量指针
constexpr variant_alternative_t< variant, Idx > * get_if() noexcept
如果存在,获取指定索引位置的指针
constexpr ~variant() noexcept
析构函数