forked from steemit/fc
-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathoptional.hpp
270 lines (242 loc) · 6.55 KB
/
optional.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#pragma once
#include <assert.h>
#include <utility>
namespace fc {
#ifdef _MSC_VER
# pragma warning(push)
# pragma warning(disable:4521) /* multiple copy ctors */
# pragma warning(disable:4522) /* multiple assignment operators */
#endif
bool assert_optional(bool is_valid ); // defined in exception.cpp
/**
* @brief provides stack-based nullable value similar to boost::optional
*
* Simply including boost::optional adds 35,000 lines to each object file, using
* fc::optional adds less than 400.
*/
template<typename T>
class optional
{
public:
typedef T value_type;
optional():_valid(false){}
~optional(){ reset(); }
optional( optional& o )
:_valid(false)
{
if( o._valid ) new (ptr()) T( *o );
_valid = o._valid;
}
optional( const optional& o )
:_valid(false)
{
if( o._valid ) new (ptr()) T( *o );
_valid = o._valid;
}
optional( optional&& o )
:_valid(false)
{
if( o._valid ) new (ptr()) T( std::move(*o) );
_valid = o._valid;
o.reset();
}
template<typename U>
optional( const optional<U>& o )
:_valid(false)
{
if( o._valid ) new (ptr()) T( *o );
_valid = o._valid;
}
template<typename U>
optional( optional<U>& o )
:_valid(false)
{
if( o._valid )
{
new (ptr()) T( *o );
}
_valid = o._valid;
}
template<typename U>
optional( optional<U>&& o )
:_valid(false)
{
if( o._valid ) new (ptr()) T( std::move(*o) );
_valid = o._valid;
o.reset();
}
template<typename U>
optional( U&& u )
:_valid(true)
{
new ((char*)ptr()) T( std::forward<U>(u) );
}
template<typename U>
optional& operator=( U&& u )
{
reset();
new (ptr()) T( std::forward<U>(u) );
_valid = true;
return *this;
}
template<typename U>
optional& operator=( optional<U>& o ) {
if (this != &o) {
if( _valid && o._valid ) {
ref() = *o;
} else if( !_valid && o._valid ) {
new (ptr()) T( *o );
_valid = true;
} else if (_valid) {
reset();
}
}
return *this;
}
template<typename U>
optional& operator=( const optional<U>& o ) {
if (this != &o) {
if( _valid && o._valid ) {
ref() = *o;
} else if( !_valid && o._valid ) {
new (ptr()) T( *o );
_valid = true;
} else if (_valid) {
reset();
}
}
return *this;
}
optional& operator=( optional& o ) {
if (this != &o) {
if( _valid && o._valid ) {
ref() = *o;
} else if( !_valid && o._valid ) {
new (ptr()) T( *o );
_valid = true;
} else if (_valid) {
reset();
}
}
return *this;
}
optional& operator=( const optional& o ) {
if (this != &o) {
if( _valid && o._valid ) {
ref() = *o;
} else if( !_valid && o._valid ) {
new (ptr()) T( *o );
_valid = true;
} else if (_valid) {
reset();
}
}
return *this;
}
template<typename U>
optional& operator=( optional<U>&& o )
{
if (this != &o)
{
if( _valid && o._valid )
{
ref() = std::move(*o);
o.reset();
} else if ( !_valid && o._valid ) {
*this = std::move(*o);
} else if (_valid) {
reset();
}
}
return *this;
}
optional& operator=( optional&& o )
{
if (this != &o)
{
if( _valid && o._valid )
{
ref() = std::move(*o);
o.reset();
} else if ( !_valid && o._valid ) {
*this = std::move(*o);
} else if (_valid) {
reset();
}
}
return *this;
}
bool valid()const { return _valid; }
bool operator!()const { return !_valid; }
// this operation is not safe and can result in unintential
// casts and comparisons, use valid() or !!
explicit operator bool()const { return _valid; }
T& operator*() { assert(_valid); return ref(); }
const T& operator*()const { assert(_valid); return ref(); }
T* operator->()
{
assert(_valid);
return ptr();
}
const T* operator->()const
{
assert(_valid);
return ptr();
}
optional& operator=(std::nullptr_t)
{
reset();
return *this;
}
friend bool operator < ( const optional a, optional b )
{
if( a.valid() && b.valid() ) return *a < *b;
return a.valid() < b.valid();
}
friend bool operator == ( const optional a, optional b )
{
if( a.valid() && b.valid() ) return *a == *b;
return a.valid() == b.valid();
}
void reset()
{
if( _valid )
{
ref().~T(); // cal destructor
}
_valid = false;
}
private:
template<typename U> friend class optional;
T& ref() { return *ptr(); }
const T& ref()const { return *ptr(); }
T* ptr() { void* v = &_value[0]; return static_cast<T*>(v); }
const T* ptr()const { const void* v = &_value[0]; return static_cast<const T*>(v); }
// force alignment... to 16 byte boundaries
#if defined(_MSC_VER)
double _value[((sizeof(T)+7)/8)] __declspec((align(16)));
#else
double _value[((sizeof(T)+7)/8)] __attribute__((aligned(16)));
#endif
bool _valid;
};
template<typename T>
bool operator == ( const optional<T>& left, const optional<T>& right ) {
return (!left == !right) || (!!left && *left == *right);
}
template<typename T, typename U>
bool operator == ( const optional<T>& left, const U& u ) {
return !!left && *left == u;
}
template<typename T>
bool operator != ( const optional<T>& left, const optional<T>& right ) {
return (!left != !right) || (!!left && *left != *right);
}
template<typename T, typename U>
bool operator != ( const optional<T>& left, const U& u ) {
return !left || *left != u;
}
#ifdef _MSC_VER
# pragma warning(pop)
#endif
} // namespace fc