-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptional.h
167 lines (135 loc) · 2.89 KB
/
optional.h
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
/*
*
* (C) Copyright Sarfaraz Nawaz 2013-14, [email protected]
*
*
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt Or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/
#pragma once
#include <stdexcept>
#include <type_traits>
#include <utility>
#include "alias.h"
namespace foam
{
static const struct none_t {} none;
template<typename T>
class optional
{
public:
typedef T value_type;
typedef alias::aligned_storage<T> storage_type;
optional() : _valid(false) {}
template<typename U>
optional(U && value) : _valid(true)
{
new (&_storage) T(static_cast<T>(std::forward<U>(value)));
}
~optional()
{
if ( has_value() ) value().~T();
}
optional(optional const & other) : _valid(other._valid)
{
if ( _valid )
{
copy(*this, other);
}
}
optional(optional && other) : _valid(other._valid)
{
if ( _valid )
{
move(*this, other);
}
}
optional& operator=(none_t const &)
{
if ( has_value() )
{
value().~T();
}
_valid = false;
return *this;
}
template<typename U>
optional& operator=(U && other)
{
return *this = optional(std::forward<U>(other));
}
optional& operator=(optional const & other)
{
if ( has_value() )
{
value().~T();
}
_valid = other._valid;
if ( _valid )
{
copy(*this, other);
}
return *this;
}
optional& operator=(optional && other)
{
if ( has_value() )
{
value().~T();
}
_valid = other._valid;
if ( _valid )
{
move(*this, other);
}
return *this;
}
bool has_value() const { return _valid; }
explicit operator bool() const { return has_value(); }
T & value()
{
return const_cast<T&>(static_cast<optional<T> const &>(*this).value());
}
T const & value() const
{
if ( !has_value() )
{
throw std::logic_error("optional<T> doesn't have value.");
}
return *reinterpret_cast<T const*>(&_storage);
}
T& operator*() { return value(); }
T const & operator*() const { return value(); }
bool operator==(optional const & other) const
{
if ( has_value() && other.has_value() )
return value() == other.value();
return !has_value() && !other.has_value();
}
bool operator!=(optional const & other) const
{
return !(*this == other);
}
bool operator == (none_t const &) const
{
return !has_value();
}
bool operator != (none_t const &) const
{
return !(*this == none);
}
private:
void copy(optional<T> & dst, optional<T> const & src) const
{
new (&dst._storage) T(src.value());
}
void move(optional<T> & dst, optional<T> const & src) const
{
new (&dst._storage) T(std::move(const_cast<T&>(src.value())));
}
private:
bool _valid;
storage_type _storage;
};
}