QEverCloud 6.1.0
Unofficial Evernote Cloud API for Qt
Loading...
Searching...
No Matches
Optional.h
Go to the documentation of this file.
1
9#ifndef QEVERCLOUD_OPTIONAL_H
10#define QEVERCLOUD_OPTIONAL_H
11
12#include "EverCloudException.h"
13
14#include <algorithm>
15
16namespace qevercloud {
17
42template<typename T>
44{
45public:
50 m_isSet(false),
51 m_value(T())
52 {}
53
57 Optional(const Optional & o) :
58 m_isSet(o.m_isSet),
59 m_value(o.m_value)
60 {}
61
66 template<typename X>
67 Optional(const Optional<X> & o) :
68 m_isSet(o.m_isSet),
69 m_value(o.m_value)
70 {}
71
75 Optional(const T & value) :
76 m_isSet(true),
77 m_value(value)
78 {}
79
83 template<typename X>
84 Optional(const X & value) :
85 m_isSet(true),
86 m_value(value)
87 {}
88
93 {
94 m_value = o.m_value;
95 m_isSet = o.m_isSet;
96 return *this;
97 }
98
102 template<typename X>
104 {
105 m_value = o.m_value;
106 m_isSet = o.m_isSet;
107 return *this;
108 }
109
114 {
115 m_value = value;
116 m_isSet = true;
117 return *this;
118 }
119
123 template<typename X>
125 {
126 m_value = value;
127 m_isSet = true;
128 return *this;
129 }
130
136 operator const T&() const
137 {
138 if (!m_isSet) {
139 throw EverCloudException(
140 "qevercloud::Optional: nonexistent value access");
141 }
142
143 return m_value;
144 }
145
151 operator T&()
152 {
153 if (!m_isSet) {
154 throw EverCloudException(
155 "qevercloud::Optional: nonexistent value access");
156 }
157
158 return m_value;
159 }
160
167 const T & ref() const
168 {
169 if (!m_isSet) {
170 throw EverCloudException(
171 "qevercloud::Optional: nonexistent value access");
172 }
173
174 return m_value;
175 }
176
204 T & ref()
205 {
206 if (!m_isSet) {
207 throw EverCloudException(
208 "qevercloud::Optional: nonexistent value access");
209 }
210
211 return m_value;
212 }
213
220 bool isSet() const
221 {
222 return m_isSet;
223 }
224
236 void clear()
237 {
238 m_isSet = false;
239 m_value = T();
240 }
241
266 {
267 m_isSet = true;
268 m_value = T();
269 return *this;
270 }
271
307 {
308 if (!m_isSet) {
309 throw EverCloudException(
310 "qevercloud::Optional: nonexistent value access");
311 }
312
313 return &m_value;
314 }
315
319 const T * operator->() const
320 {
321 if (!m_isSet) {
322 throw EverCloudException(
323 "qevercloud::Optional: nonexistent value access");
324 }
325
326 return &m_value;
327 }
328
336 T value(T defaultValue = T()) const
337 {
338 return m_isSet ? m_value : defaultValue;
339 }
340
345 bool isEqual(const Optional<T> & other) const
346 {
347 if (m_isSet != other.m_isSet) {
348 return false;
349 }
350
351 return !m_isSet || (m_value == other.m_value);
352 }
353
354 bool operator==(const Optional<T> & other) const
355 {
356 return isEqual(other);
357 }
358
359 bool operator!=(const Optional<T> & other) const
360 {
361 return !operator==(other);
362 }
363
364 bool operator==(const T & other) const
365 {
366 if (!m_isSet) {
367 return false;
368 }
369
370 return m_value == other;
371 }
372
373 bool operator!=(const T & other) const
374 {
375 return !operator==(other);
376 }
377
378 template<typename X> friend class Optional;
379
380 friend void swap(Optional & first, Optional & second)
381 {
382 using std::swap;
383 swap(first.m_isSet, second.m_isSet);
384 swap(first.m_value, second.m_value);
385 }
386
388 {
389 swap(*this, other);
390 }
391
393 {
394 swap(*this, other);
395 return *this;
396 }
397
398 Optional(T && other)
399 {
400 using std::swap;
401 m_isSet = true;
402 swap(m_value, other);
403 }
404
405 Optional & operator=(T && other)
406 {
407 using std::swap;
408 m_isSet = true;
409 swap(m_value, other);
410 return *this;
411 }
412
413private:
414 bool m_isSet;
415 T m_value;
416};
417
418} // namespace qevercloud
419
420#endif // QEVERCLOUD_OPTIONAL_H
Definition: EverCloudException.h:33
Definition: Optional.h:44
Optional(T &&other)
Definition: Optional.h:398
Optional(const T &value)
Definition: Optional.h:75
void clear()
Definition: Optional.h:236
Optional & operator=(const Optional< X > &o)
Definition: Optional.h:103
Optional()
Definition: Optional.h:49
friend void swap(Optional &first, Optional &second)
Definition: Optional.h:380
Optional & operator=(const T &value)
Definition: Optional.h:113
Optional & operator=(T &&other)
Definition: Optional.h:405
Optional & init()
Definition: Optional.h:265
const T * operator->() const
Definition: Optional.h:319
Optional & operator=(const Optional &o)
Definition: Optional.h:92
const T & ref() const
Definition: Optional.h:167
T * operator->()
Definition: Optional.h:306
Optional(const X &value)
Definition: Optional.h:84
Optional(Optional &&other)
Definition: Optional.h:387
T value(T defaultValue=T()) const
Definition: Optional.h:336
bool operator!=(const T &other) const
Definition: Optional.h:373
T & ref()
Definition: Optional.h:204
Optional & operator=(const X &value)
Definition: Optional.h:124
bool isEqual(const Optional< T > &other) const
Definition: Optional.h:345
bool operator==(const T &other) const
Definition: Optional.h:364
Optional(const Optional< X > &o)
Definition: Optional.h:67
bool operator==(const Optional< T > &other) const
Definition: Optional.h:354
Optional & operator=(Optional &&other)
Definition: Optional.h:392
bool isSet() const
Checks if value is set.
Definition: Optional.h:220
Optional(const Optional &o)
Definition: Optional.h:57
bool operator!=(const Optional< T > &other) const
Definition: Optional.h:359
Definition: AsyncResult.h:21