aws-crt-cpp
C++ wrapper around the aws-c-* libraries. Provides Cross-Platform Transport Protocols and SSL/TLS implementations for C++.
Optional.h
Go to the documentation of this file.
1#pragma once
6#include <utility>
7
8namespace Aws
9{
10 namespace Crt
11 {
12 template <typename T> class Optional
13 {
14 public:
15 Optional() : m_value(nullptr) {}
16 Optional(const T &val)
17 {
18 new (&m_storage) T(val);
19 m_value = reinterpret_cast<T *>(&m_storage);
20 }
21
22 Optional(T &&val)
23 {
24 new (&m_storage) T(std::forward<T>(val));
25 m_value = reinterpret_cast<T *>(&m_storage);
26 }
27
29 {
30 if (m_value)
31 {
32 m_value->~T();
33 }
34 }
35
36 template <typename U = T> Optional &operator=(U &&u)
37 {
38 if (m_value)
39 {
40 *m_value = std::forward<U>(u);
41 return *this;
42 }
43
44 new (&m_storage) T(std::forward<U>(u));
45 m_value = reinterpret_cast<T *>(&m_storage);
46
47 return *this;
48 }
49
50 Optional(const Optional<T> &other)
51 {
52 if (other.m_value)
53 {
54 new (&m_storage) T(*other.m_value);
55 m_value = reinterpret_cast<T *>(&m_storage);
56 }
57 else
58 {
59 m_value = nullptr;
60 }
61 }
62
64 {
65 if (other.m_value)
66 {
67 new (&m_storage) T(std::forward<T>(*other.m_value));
68 m_value = reinterpret_cast<T *>(&m_storage);
69 }
70 else
71 {
72 m_value = nullptr;
73 }
74 }
75
77 {
78 if (this == &other)
79 {
80 return *this;
81 }
82
83 if (m_value)
84 {
85 if (other.m_value)
86 {
87 *m_value = *other.m_value;
88 }
89 else
90 {
91 m_value->~T();
92 m_value = nullptr;
93 }
94
95 return *this;
96 }
97
98 if (other.m_value)
99 {
100 new (&m_storage) T(*other.m_value);
101 m_value = reinterpret_cast<T *>(&m_storage);
102 }
103
104 return *this;
105 }
106
107 template <typename U = T> Optional<T> &operator=(const Optional<U> &other)
108 {
109 if (this == &other)
110 {
111 return *this;
112 }
113
114 if (m_value)
115 {
116 if (other.m_value)
117 {
118 *m_value = *other.m_value;
119 }
120 else
121 {
122 m_value->~T();
123 m_value = nullptr;
124 }
125
126 return *this;
127 }
128
129 if (other.m_value)
130 {
131 new (&m_storage) T(*other.m_value);
132 m_value = reinterpret_cast<T *>(&m_storage);
133 }
134
135 return *this;
136 }
137
138 template <typename U = T> Optional<T> &operator=(Optional<U> &&other)
139 {
140 if (this == &other)
141 {
142 return *this;
143 }
144
145 if (m_value)
146 {
147 if (other.m_value)
148 {
149 *m_value = std::forward<U>(*other.m_value);
150 }
151 else
152 {
153 m_value->~T();
154 m_value = nullptr;
155 }
156
157 return *this;
158 }
159
160 if (other.m_value)
161 {
162 new (&m_storage) T(std::forward<U>(*other.m_value));
163 m_value = reinterpret_cast<T *>(&m_storage);
164 }
165
166 return *this;
167 }
168
169 const T *operator->() const { return m_value; }
170 T *operator->() { return m_value; }
171 const T &operator*() const & { return *m_value; }
172 T &operator*() & { return *m_value; }
173 const T &&operator*() const && { return std::move(*m_value); }
174 T &&operator*() && { return std::move(*m_value); }
175
176 explicit operator bool() const noexcept { return m_value != nullptr; }
177 bool has_value() const noexcept { return m_value != nullptr; }
178
179 T &value() & { return *m_value; }
180 const T &value() const & { return *m_value; }
181
182 T &&value() && { return std::move(*m_value); }
183 const T &&value() const && { return std::move(*m_value); }
184
185 private:
186 typename std::aligned_storage<sizeof(T)>::type m_storage;
187 T *m_value;
188 };
189 } // namespace Crt
190} // namespace Aws
Definition: Optional.h:13
const T & operator*() const &
Definition: Optional.h:171
Optional(const T &val)
Definition: Optional.h:16
Optional & operator=(const Optional &other)
Definition: Optional.h:76
const T & value() const &
Definition: Optional.h:180
Optional()
Definition: Optional.h:15
bool has_value() const noexcept
Definition: Optional.h:177
Optional(const Optional< T > &other)
Definition: Optional.h:50
T && operator*() &&
Definition: Optional.h:174
const T && operator*() const &&
Definition: Optional.h:173
Optional(Optional< T > &&other)
Definition: Optional.h:63
Optional< T > & operator=(const Optional< U > &other)
Definition: Optional.h:107
Optional(T &&val)
Definition: Optional.h:22
T & operator*() &
Definition: Optional.h:172
Optional< T > & operator=(Optional< U > &&other)
Definition: Optional.h:138
T && value() &&
Definition: Optional.h:182
const T * operator->() const
Definition: Optional.h:169
Optional & operator=(U &&u)
Definition: Optional.h:36
T & value() &
Definition: Optional.h:179
~Optional()
Definition: Optional.h:28
T * operator->()
Definition: Optional.h:170
const T && value() const &&
Definition: Optional.h:183
Definition: Api.h:17
newitem type
Definition: cJSON.cpp:2724