QEverCloud  6.1.0
Unofficial Evernote Cloud API for Qt
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 
16 namespace qevercloud {
17 
42 template<typename T>
43 class Optional
44 {
45 public:
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 
113  Optional & operator=(const T & value)
114  {
115  m_value = value;
116  m_isSet = true;
117  return *this;
118  }
119 
123  template<typename X>
124  Optional & operator=(const X & value)
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 
387  Optional(Optional && other)
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 
413 private:
414  bool m_isSet;
415  T m_value;
416 };
417 
418 } // namespace qevercloud
419 
420 #endif // QEVERCLOUD_OPTIONAL_H
qevercloud::Optional::operator=
Optional & operator=(const Optional< X > &o)
Definition: Optional.h:103
qevercloud::Optional::operator==
bool operator==(const Optional< T > &other) const
Definition: Optional.h:354
qevercloud::Optional::isEqual
bool isEqual(const Optional< T > &other) const
Definition: Optional.h:345
qevercloud::Optional::Optional
Optional()
Definition: Optional.h:49
qevercloud::Optional::operator=
Optional & operator=(const Optional &o)
Definition: Optional.h:92
qevercloud::Optional::operator!=
bool operator!=(const Optional< T > &other) const
Definition: Optional.h:359
qevercloud::Optional::Optional
Optional(const Optional &o)
Definition: Optional.h:57
qevercloud::Optional::Optional
Optional(T &&other)
Definition: Optional.h:398
qevercloud::Optional::operator=
Optional & operator=(T &&other)
Definition: Optional.h:405
EverCloudException.h
qevercloud::Optional::operator->
const T * operator->() const
Definition: Optional.h:319
qevercloud
Definition: AsyncResult.h:21
qevercloud::Optional::operator=
Optional & operator=(Optional &&other)
Definition: Optional.h:392
qevercloud::Optional::init
Optional & init()
Definition: Optional.h:265
qevercloud::Optional::swap
friend void swap(Optional &first, Optional &second)
Definition: Optional.h:380
qevercloud::Optional
Definition: Optional.h:44
qevercloud::Optional::Optional
Optional(const T &value)
Definition: Optional.h:75
qevercloud::Optional::operator->
T * operator->()
Definition: Optional.h:306
qevercloud::Optional::clear
void clear()
Definition: Optional.h:236
qevercloud::Optional::ref
const T & ref() const
Definition: Optional.h:167
qevercloud::Optional::operator=
Optional & operator=(const T &value)
Definition: Optional.h:113
qevercloud::Optional::Optional
Optional(const Optional< X > &o)
Definition: Optional.h:67
qevercloud::Optional::operator=
Optional & operator=(const X &value)
Definition: Optional.h:124
qevercloud::Optional::operator==
bool operator==(const T &other) const
Definition: Optional.h:364
qevercloud::Optional::value
T value(T defaultValue=T()) const
Definition: Optional.h:336
qevercloud::Optional::isSet
bool isSet() const
Checks if value is set.
Definition: Optional.h:220
qevercloud::EverCloudException
Definition: EverCloudException.h:33
qevercloud::Optional::Optional
Optional(const X &value)
Definition: Optional.h:84
qevercloud::Optional::ref
T & ref()
Definition: Optional.h:204
qevercloud::Optional::operator!=
bool operator!=(const T &other) const
Definition: Optional.h:373
qevercloud::Optional::Optional
Optional(Optional &&other)
Definition: Optional.h:387