AWS IoT Device SDK C++ v2: crt/aws-crt-cpp/include/aws/crt/Optional.h Source File

AWS IoT Device SDK C++ v2 1.14.0 AWS IoT Device SDK C++ v2

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