QEverCloud  3.0.0
Unofficial Evernote Cloud API for Qt
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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 #include <algorithm>
14 
15 namespace qevercloud {
16 
36 template<typename T>
37 class Optional
38 {
39 public:
44  m_isSet(false),
45  m_value(T())
46  {}
47 
51  Optional(const Optional & o) :
52  m_isSet(o.m_isSet),
53  m_value(o.m_value)
54  {}
55 
59  template<typename X>
60  Optional(const Optional<X> & o) :
61  m_isSet(o.m_isSet),
62  m_value(o.m_value)
63  {}
64 
68  Optional(const T & value) :
69  m_isSet(true),
70  m_value(value)
71  {}
72 
76  template<typename X>
77  Optional(const X & value) :
78  m_isSet(true),
79  m_value(value)
80  {}
81 
86  {
87  m_value = o.m_value;
88  m_isSet = o.m_isSet;
89  return *this;
90  }
91 
95  template<typename X>
97  {
98  m_value = o.m_value;
99  m_isSet = o.m_isSet;
100  return *this;
101  }
102 
106  Optional & operator=(const T & value)
107  {
108  m_value = value;
109  m_isSet = true;
110  return *this;
111  }
112 
116  template<typename X>
117  Optional & operator=(const X & value)
118  {
119  m_value = value;
120  m_isSet = true;
121  return *this;
122  }
123 
129  operator const T&() const
130  {
131  if (!m_isSet) {
132  throw EverCloudException("qevercloud::Optional: nonexistent value access");
133  }
134 
135  return m_value;
136  }
137 
143  operator T&()
144  {
145  if (!m_isSet) {
146  throw EverCloudException("qevercloud::Optional: nonexistent value access");
147  }
148 
149  return m_value;
150  }
151 
158  const T & ref() const
159  {
160  if (!m_isSet) {
161  throw EverCloudException("qevercloud::Optional: nonexistent value access");
162  }
163 
164  return m_value;
165  }
166 
192  T & ref()
193  {
194  if (!m_isSet) {
195  throw EverCloudException("qevercloud::Optional: nonexistent value access");
196  }
197 
198  return m_value;
199  }
200 
207  bool isSet() const
208  {
209  return m_isSet;
210  }
211 
223  void clear()
224  {
225  m_isSet = false;
226  m_value = T();
227  }
228 
253  {
254  m_isSet = true;
255  m_value = T();
256  return *this;
257  }
258 
288  {
289  if (!m_isSet) {
290  throw EverCloudException("qevercloud::Optional: nonexistent value access");
291  }
292 
293  return &m_value;
294  }
295 
299  const T * operator->() const
300  {
301  if (!m_isSet) {
302  throw EverCloudException("qevercloud::Optional: nonexistent value access");
303  }
304 
305  return &m_value;
306  }
307 
314  T value(T defaultValue = T()) const
315  {
316  return m_isSet ? m_value : defaultValue;
317  }
318 
327  bool isEqual(const Optional<T> & other) const
328  {
329  if(m_isSet != other.m_isSet) return false;
330  return !m_isSet || (m_value == other.m_value);
331  }
332 
333  template<typename X> friend class Optional;
334 
335  friend void swap(Optional & first, Optional & second)
336  {
337  using std::swap;
338  swap(first.m_isSet, second.m_isSet);
339  swap(first.m_value, second.m_value);
340  }
341 
342 // Visual C++ does not to generate implicit move constructors so this stuff doesn't work with even recent MSVC compilers
343 #if defined(Q_COMPILER_RVALUE_REFS) && !defined(_MSC_VER)
344  Optional(Optional && other)
345  {
346  swap(*this, other);
347  }
348 
349  Optional & operator=(Optional && other)
350  {
351  swap(*this, other);
352  return *this;
353  }
354 
355  Optional(T && other)
356  {
357  using std::swap;
358  m_isSet = true;
359  swap(m_value, other);
360  }
361 
362  Optional & operator=(T && other)
363  {
364  using std::swap;
365  m_isSet = true;
366  swap(m_value, other);
367  return *this;
368  }
369 #endif
370 
371 private:
372  bool m_isSet;
373  T m_value;
374 };
375 
376 } // namespace qevercloud
377 
378 #endif // QEVERCLOUD_OPTIONAL_H
T value(T defaultValue=T()) const
Definition: Optional.h:314
Optional & operator=(const Optional< X > &o)
Definition: Optional.h:96
Optional(const T &value)
Definition: Optional.h:68
friend void swap(Optional &first, Optional &second)
Definition: Optional.h:335
Definition: Optional.h:37
const T * operator->() const
Definition: Optional.h:299
const T & ref() const
Definition: Optional.h:158
Optional(const Optional &o)
Definition: Optional.h:51
void clear()
Definition: Optional.h:223
Optional & init()
Definition: Optional.h:252
Optional & operator=(const T &value)
Definition: Optional.h:106
Optional & operator=(const Optional &o)
Definition: Optional.h:85
Optional(const X &value)
Definition: Optional.h:77
Optional(const Optional< X > &o)
Definition: Optional.h:60
bool isEqual(const Optional< T > &other) const
Definition: Optional.h:327
Definition: AsyncResult.h:18
T * operator->()
Definition: Optional.h:287
Optional()
Definition: Optional.h:43
Optional & operator=(const X &value)
Definition: Optional.h:117
Definition: EverCloudException.h:26
T & ref()
Definition: Optional.h:192
bool isSet() const
Checks if value is set.
Definition: Optional.h:207