AlexaClientSDK  3.0.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
Optional.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #ifndef ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_OPTIONAL_H_
16 #define ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_OPTIONAL_H_
17 
19 
20 namespace alexaClientSDK {
21 namespace avsCommon {
22 namespace utils {
23 
31 template <typename ValueT>
32 class Optional {
33 public:
37  Optional();
38 
45  Optional(const ValueT& value);
46 
52  Optional(const Optional<ValueT>& other);
53 
60  void set(const ValueT& value);
61 
65  void reset();
66 
72  bool hasValue() const;
73 
80  ValueT valueOr(const ValueT& other) const;
81 
87  ValueT value() const;
88 
92  ~Optional();
93 
101 
109  bool operator==(const Optional<ValueT>& rhs) const;
110 
118  bool operator!=(const Optional<ValueT>& rhs) const;
119 
128  bool operator<(const Optional& rhs) const;
130  bool operator>(const Optional& rhs) const;
131  bool operator<=(const Optional& rhs) const;
132  bool operator>=(const Optional& rhs) const;
134 
135 private:
142  inline ValueT& getReference();
144  inline const ValueT& getReference() const;
146 
148  bool m_hasValue;
149 
155 };
156 
157 template <typename ValueT>
158 Optional<ValueT>::Optional() : m_hasValue{false} {
159 }
160 
161 template <typename ValueT>
162 Optional<ValueT>::Optional(const ValueT& other) : m_hasValue{true} {
163  // Create object in the allocated space.
164  new (&m_value) ValueT(other);
165 }
166 
167 template <typename ValueT>
168 Optional<ValueT>::Optional(const Optional<ValueT>& other) : m_hasValue{other.m_hasValue} {
169  if (hasValue()) {
170  new (&m_value) ValueT(other.getReference());
171  }
172 }
173 
174 template <typename ValueT>
176  return *reinterpret_cast<ValueT*>(&m_value);
177 }
178 
179 template <typename ValueT>
180 const ValueT& Optional<ValueT>::getReference() const {
181  return *reinterpret_cast<const ValueT*>(&m_value);
182 }
183 
184 template <typename ValueT>
185 void Optional<ValueT>::set(const ValueT& other) {
186  if (hasValue()) {
187  getReference() = other;
188  } else {
189  m_hasValue = true;
190  new (&m_value) ValueT(other);
191  }
192 }
193 
194 template <typename ValueT>
196  if (hasValue()) {
197  m_hasValue = false;
198  getReference().~ValueT();
199  }
200 }
201 
202 template <typename ValueT>
204  return m_hasValue;
205 }
206 
207 template <typename ValueT>
208 ValueT Optional<ValueT>::value() const {
209  if (hasValue()) {
210  return getReference();
211  }
212  logger::acsdkError(logger::LogEntry("Optional", "valueFailed").d("reason", "optionalHasNoValue"));
213  return ValueT();
214 }
215 
216 template <typename ValueT>
217 ValueT Optional<ValueT>::valueOr(const ValueT& other) const {
218  if (hasValue()) {
219  return getReference();
220  }
221  return other;
222 }
223 
224 template <typename ValueT>
226  if (hasValue()) {
227  getReference().~ValueT();
228  }
229 }
230 
231 template <typename ValueT>
233  if (hasValue()) {
234  if (rhs.hasValue()) {
235  getReference() = rhs.value();
236  } else {
237  m_hasValue = false;
238  getReference().~ValueT();
239  }
240  } else if (rhs.hasValue()) {
241  m_hasValue = true;
242  new (&m_value) ValueT(rhs.value());
243  }
244  return *this;
245 }
246 
247 template <typename ValueT>
249  if (this->hasValue()) {
250  return rhs.hasValue() && (this->value() == rhs.value());
251  }
252  return !rhs.hasValue();
253 }
254 
255 template <typename ValueT>
257  return !(*this == rhs);
258 }
259 
260 template <typename ValueT>
261 bool Optional<ValueT>::operator<(const Optional& rhs) const {
262  if (m_hasValue && rhs.m_hasValue) {
263  return getReference() < rhs.getReference();
264  }
265  return m_hasValue < rhs.m_hasValue;
266 }
267 
268 template <typename ValueT>
269 bool Optional<ValueT>::operator>(const Optional& rhs) const {
270  return rhs < *this;
271 }
272 
273 template <typename ValueT>
274 bool Optional<ValueT>::operator<=(const Optional& rhs) const {
275  return !(rhs < *this);
276 }
277 
278 template <typename ValueT>
279 bool Optional<ValueT>::operator>=(const Optional& rhs) const {
280  return !(*this < rhs);
281 }
282 
283 } // namespace utils
284 } // namespace avsCommon
285 } // namespace alexaClientSDK
286 
287 #endif // ALEXA_CLIENT_SDK_AVSCOMMON_UTILS_INCLUDE_AVSCOMMON_UTILS_OPTIONAL_H_
bool operator==(const Optional< ValueT > &rhs) const
Definition: Optional.h:248
ValueT valueOr(const ValueT &other) const
Definition: Optional.h:217
void reset()
Definition: Optional.h:195
Optional()
Definition: Optional.h:158
bool operator==(const BlockingPolicy &lhs, const BlockingPolicy &rhs)
bool operator<=(const Optional &rhs) const
Definition: Optional.h:274
bool operator!=(const Optional< ValueT > &rhs) const
Definition: Optional.h:256
bool operator!=(const BlockingPolicy &lhs, const BlockingPolicy &rhs)
~Optional()
Definition: Optional.h:225
Optional< ValueT > & operator=(const Optional< ValueT > &rhs)
Definition: Optional.h:232
bool operator>(const Optional &rhs) const
Definition: Optional.h:269
bool hasValue() const
Definition: Optional.h:203
set
Definition: gmock_class.py:44
bool operator>=(const Optional &rhs) const
Definition: Optional.h:279
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
type
Definition: upload.py:443
void acsdkError(const LogEntry &entry)
ValueT value() const
Definition: Optional.h:208
bool operator<(const Optional &rhs) const
Definition: Optional.h:261
LogEntry is used to compile the log entry text to log via Logger.
Definition: LogEntry.h:33

AlexaClientSDK 3.0.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0