-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathOptional.h
185 lines (157 loc) · 5.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#pragma once
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/crt/TypeTraits.h>
#include <aws/crt/Utility.h>
#include <utility>
namespace Aws
{
namespace Crt
{
/**
* Custom implementation of an Option type. std::optional requires C++17
* @tparam T type of the optional value
*/
template <typename T> class Optional
{
public:
using ValueType = T;
Optional() : m_value(nullptr) {}
Optional(const T &val)
{
new (m_storage) T(val);
m_value = reinterpret_cast<T *>(m_storage);
}
Optional(T &&val)
{
new (m_storage) T(std::forward<T>(val));
m_value = reinterpret_cast<T *>(m_storage);
}
~Optional()
{
if (m_value)
{
m_value->~T();
}
}
/**
* Assignment operator for a case when the parameter type is not Optional.
*/
template <
typename U = T,
typename std::enable_if<
!IsSpecializationOf<typename std::decay<U>::type, Aws::Crt::Optional>::value,
bool>::type = true>
Optional &operator=(U &&u)
{
if (m_value)
{
*m_value = std::forward<U>(u);
return *this;
}
new (m_storage) T(std::forward<U>(u));
m_value = reinterpret_cast<T *>(m_storage);
return *this;
}
Optional(const Optional<T> &other)
{
if (other.m_value)
{
new (m_storage) T(*other.m_value);
m_value = reinterpret_cast<T *>(m_storage);
}
else
{
m_value = nullptr;
}
}
Optional(Optional<T> &&other)
{
if (other.m_value)
{
new (m_storage) T(std::forward<T>(*other.m_value));
m_value = reinterpret_cast<T *>(m_storage);
}
else
{
m_value = nullptr;
}
}
template <typename... Args> explicit Optional(Aws::Crt::InPlaceT, Args &&...args)
{
new (m_storage) T(std::forward<Args>(args)...);
m_value = reinterpret_cast<T *>(m_storage);
}
Optional<T> &operator=(const Optional &other) { return assign(other); }
template <typename U = T> Optional<T> &operator=(const Optional<U> &other) { return assign(other); }
template <typename U = T> Optional<T> &operator=(Optional<U> &&other) { return assign(std::move(other)); }
template <typename... Args> T &emplace(Args &&...args)
{
reset();
new (m_storage) T(std::forward<Args>(args)...);
m_value = reinterpret_cast<T *>(m_storage);
return *m_value;
}
const T *operator->() const { return m_value; }
T *operator->() { return m_value; }
const T &operator*() const & { return *m_value; }
T &operator*() & { return *m_value; }
const T &&operator*() const && { return std::move(*m_value); }
T &&operator*() && { return std::move(*m_value); }
explicit operator bool() const noexcept { return m_value != nullptr; }
bool has_value() const noexcept { return m_value != nullptr; }
T &value() & { return *m_value; }
const T &value() const & { return *m_value; }
T &&value() && { return std::move(*m_value); }
const T &&value() const && { return std::move(*m_value); }
void reset()
{
if (m_value)
{
m_value->~T();
m_value = nullptr;
}
}
private:
template <typename Op> Optional &assign(Op &&other)
{
// U is an underlying type of the Optional type passed to this function. Depending on constness of Op,
// U will be either value or const ref.
// NOTE: std::is_const<const C&> == false, that's why std::remove_reference is needed here.
using U = typename std::conditional<
std::is_const<typename std::remove_reference<Op>::type>::value,
const typename std::decay<Op>::type::ValueType &,
typename std::decay<Op>::type::ValueType>::type;
if ((void *)this == (void *)&other)
{
return *this;
}
if (m_value)
{
// Optional<U> is a completely different class from the C++ specifics pov. So, we can use only
// public members of `other`.
if (other.has_value())
{
*m_value = std::forward<U>(other.value());
}
else
{
m_value->~T();
m_value = nullptr;
}
return *this;
}
if (other.has_value())
{
new (m_storage) T(std::forward<U>(other.value()));
m_value = reinterpret_cast<T *>(m_storage);
}
return *this;
}
alignas(T) char m_storage[sizeof(T)];
T *m_value;
};
} // namespace Crt
} // namespace Aws