-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvariant.hpp
192 lines (144 loc) · 4.17 KB
/
variant.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
/*!
\file variant.hpp
\date 24.12.2020
\author Ildar Kasimov
This file is a single-header library which was written in C++14 standard.
The library provides an implementation of a sigma type which is also known as a tagged union or a variant.
The library has almost zero dependencies except a few standard library's containers.
*/
#pragma once
#include <algorithm>
#include <cassert>
///< Library's configs
#define VARIANT_DISABLE_EXCEPTIONS 1
#define VARIANT_ENABLE_EXPORT 1
#if VARIANT_DISABLE_EXCEPTIONS
#define VARIANT_NOEXCEPT noexcept
#else
#define VARIANT_NOEXCEPT
#endif
namespace Wrench
{
/*!
\brief Utilities
If you don't want to be scared just skip this horrible meta-programming magic stuffs
*/
template <typename... TArgs> struct TCountOf;
template<> struct TCountOf<> { static constexpr size_t mValue = 0; };
template <typename T, typename... TArgs> struct TCountOf<T, TArgs...> { static constexpr size_t mValue = 1 + TCountOf<TArgs...>::mValue; };
template <typename T> constexpr size_t GetMaxSize() { return sizeof(T); }
template <typename T, typename... TArgs>
constexpr typename std::enable_if<TCountOf<TArgs...>::mValue != 0, size_t>::type GetMaxSize()
{
return sizeof(T) > GetMaxSize<TArgs...>() ? sizeof(T) : GetMaxSize<TArgs...>();
}
template <size_t, typename> constexpr size_t GetIndexOfTypeInternal() { return (std::numeric_limits<size_t>::max)(); } // nothing found
template <size_t index, typename TWhat, typename TCurrent, typename... TArgs>
constexpr size_t GetIndexOfTypeInternal()
{
return std::is_same<TWhat, TCurrent>::value ? index : GetIndexOfTypeInternal<index + 1, TWhat, TArgs...>();
}
template <typename TWhat, typename... TArgs> constexpr size_t GetIndexOfType() { return GetIndexOfTypeInternal<0, TWhat, TArgs...>(); }
/*!
class Variant
\brief An implementation of a sigma type which is also known as a tagged union or a variant
*/
template <typename... TArgs>
class Variant
{
public:
using TStorageType = typename std::aligned_storage<GetMaxSize<TArgs...>()>::type;
using TTypeIndex = size_t;
friend void Swap(Variant<TArgs...>& v1, Variant<TArgs...>& v2)
{
std::swap(v1.mStorage, v2.mStorage);
std::swap(v1.mCurrTypeId, v2.mCurrTypeId);
}
public:
Variant() VARIANT_NOEXCEPT :
mStorage(), mCurrTypeId(0)
{
}
template <typename T>
Variant(T value) VARIANT_NOEXCEPT :
mStorage(), mCurrTypeId(0)
{
if (mCurrTypeId)
{
mStorage.~TStorageType();
}
mCurrTypeId = GetIndexOfType<T, TArgs...>();
new (&mStorage) T(value);
}
Variant(const Variant& ref) VARIANT_NOEXCEPT :
mStorage(ref.mStorage), mCurrTypeId(ref.mCurrTypeId)
{
}
Variant(Variant&& ref) VARIANT_NOEXCEPT :
mStorage(std::move(ref.mStorage)), mCurrTypeId(ref.mCurrTypeId)
{
}
~Variant() VARIANT_NOEXCEPT
{
mStorage.~TStorageType();
}
// Assignment operator
template <typename T>
const T& operator= (const T& value) VARIANT_NOEXCEPT
{
if (mCurrTypeId)
{
mStorage.~TStorageType();
}
mCurrTypeId = GetIndexOfType<T, TArgs...>();
new (&mStorage) T(value);
return value;
}
Variant<TArgs...>& operator= (Variant<TArgs...> value) VARIANT_NOEXCEPT
{
mStorage.~TStorageType();
Swap(*this, value);
return *this;
}
Variant<TArgs...>& operator= (Variant<TArgs...>&& value) VARIANT_NOEXCEPT
{
mStorage.~TStorageType();
Swap(*this, value);
return *this;
}
template <typename T>
bool Is() const VARIANT_NOEXCEPT
{
return mCurrTypeId == GetIndexOfType<T, TArgs...>();
}
template <typename T>
const T& As() const VARIANT_NOEXCEPT
{
if (!Is<T>())
{
assert(false);
std::terminate();
}
return reinterpret_cast<const T&>(mStorage);
}
template <typename T>
T& As() VARIANT_NOEXCEPT
{
if (!Is<T>())
{
assert(false);
std::terminate();
}
return reinterpret_cast<T&>(mStorage);
}
private:
TStorageType mStorage;
TTypeIndex mCurrTypeId = 0x0;
};
template <typename T, typename... TArgs>
Variant<TArgs...> MakeVariant(T value)
{
Variant<TArgs...> v; v = std::forward<T>(value);
return std::move(v);
}
}