Ice 3.7 C++11 API Reference
Optional.h
Go to the documentation of this file.
1 //
2 // Copyright (c) ZeroC, Inc. All rights reserved.
3 //
4 
5 #ifndef ICE_UTIL_OPTIONAL_H
6 #define ICE_UTIL_OPTIONAL_H
7 
8 #ifndef ICE_CPP11_MAPPING
9 
10 #include <IceUtil/Config.h>
11 
12 namespace IceUtilInternal
13 {
14 
15 struct NoneType
16 {
17 };
18 
19 }
20 
21 namespace IceUtil
22 {
23 
25 const IceUtilInternal::NoneType None = {};
26 
28 template<typename T>
29 class Optional
30 {
31 public:
32 
33  typedef T element_type;
34 
38  Optional() : _isSet(false)
39  {
40  }
41 
45  Optional(IceUtilInternal::NoneType) : _isSet(false)
46  {
47  }
48 
53  Optional(const Optional& r) : _value(r._value), _isSet(r._isSet)
54  {
55  }
56 
61  template<typename Y>
62  Optional(Y p) : _value(p), _isSet(true)
63  {
64  }
65 
70  template<typename Y>
71  Optional(const Optional<Y>& r) : _value(r._value), _isSet(r._isSet)
72  {
73  }
74 
75  ~Optional()
76  {
77  }
78 
82  Optional& operator=(IceUtilInternal::NoneType)
83  {
84  _value = T();
85  _isSet = false;
86  return *this;
87  }
88 
93  template<typename Y>
94  Optional& operator=(Y p)
95  {
96  _value = p;
97  _isSet = true;
98  return *this;
99  }
100 
105  template<typename Y>
106  Optional& operator=(const Optional<Y>& r)
107  {
108  _value = r._value;
109  _isSet = r._isSet;
110  return *this;
111  }
112 
117  Optional& operator=(const Optional& r)
118  {
119  _value = r._value;
120  _isSet = r._isSet;
121  return *this;
122  }
123 
129  const T& value() const
130  {
131  checkIsSet();
132  return _value;
133  }
134 
140  T& value()
141  {
142  checkIsSet();
143  return _value;
144  }
145 
151  const T& get() const
152  {
153  return value();
154  }
155 
161  T& get()
162  {
163  return value();
164  }
165 
171  const T* operator->() const
172  {
173  return &value();
174  }
175 
181  T* operator->()
182  {
183  return &value();
184  }
185 
191  const T& operator*() const
192  {
193  return value();
194  }
195 
201  T& operator*()
202  {
203  return value();
204  }
205 
210  operator bool() const
211  {
212  return _isSet;
213  }
214 
219  bool operator!() const
220  {
221  return !_isSet;
222  }
223 
228  void swap(Optional& other)
229  {
230  std::swap(_isSet, other._isSet);
231  std::swap(_value, other._value);
232  }
233 
235  void __setIsSet()
236  {
237  _isSet = true;
238  }
240 
241 private:
242 
243  void checkIsSet() const
244  {
245  if(!_isSet)
246  {
247  throwOptionalNotSetException(__FILE__, __LINE__);
248  }
249  }
250 
251  void throwOptionalNotSetException(const char *, int) const;
252 
253  T _value;
254  bool _isSet;
255 };
256 
262 template<class T> inline Optional<T>
263 makeOptional(const T& v)
264 {
265  return Optional<T>(v);
266 }
267 
269 template<typename T> inline void
270 Optional<T>::throwOptionalNotSetException(const char* file, int line) const
271 {
272  throw OptionalNotSetException(file, line);
273 }
275 
276 template<typename T, typename U>
277 inline bool operator==(const Optional<T>& lhs, const Optional<U>& rhs)
278 {
279  if(lhs && rhs)
280  {
281  return *lhs == *rhs;
282  }
283  else
284  {
285  return !lhs && !rhs;
286  }
287 }
288 
289 template<typename T, typename U>
290 inline bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs)
291 {
292  return !operator==(lhs, rhs);
293 }
294 
295 template<typename T, typename U>
296 inline bool operator<(const Optional<T>& lhs, const Optional<U>& rhs)
297 {
298  if(lhs && rhs)
299  {
300  return *lhs < *rhs;
301  }
302  else
303  {
304  return !lhs && rhs;
305  }
306 }
307 
308 template<typename T, typename U>
309 inline bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs)
310 {
311  return lhs < rhs || lhs == rhs;
312 }
313 
314 template<typename T, typename U>
315 inline bool operator>(const Optional<T>& lhs, const Optional<U>& rhs)
316 {
317  return !(lhs < rhs || lhs == rhs);
318 }
319 
320 template<typename T, typename U>
321 inline bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs)
322 {
323  return !(lhs < rhs);
324 }
325 
326 // Optional<T> vs Y
327 
328 template<typename T, typename Y>
329 inline bool operator==(const Optional<T>& lhs, const Y& rhs)
330 {
331  if(!lhs)
332  {
333  return false;
334  }
335  else
336  {
337  return *lhs == rhs;
338  }
339 }
340 
341 template<typename T, typename Y>
342 inline bool operator!=(const Optional<T>& lhs, const Y& rhs)
343 {
344  return !operator==(lhs, rhs);
345 }
346 
347 template<typename T, typename Y>
348 inline bool operator<(const Optional<T>& lhs, const Y& rhs)
349 {
350  if(lhs)
351  {
352  return *lhs < rhs;
353  }
354  else
355  {
356  return true;
357  }
358 }
359 
360 template<typename T, typename Y>
361 inline bool operator<=(const Optional<T>& lhs, const Y& rhs)
362 {
363  return lhs < rhs || lhs == rhs;
364 }
365 
366 template<typename T, typename Y>
367 inline bool operator>(const Optional<T>& lhs, const Y& rhs)
368 {
369  return !(lhs < rhs || lhs == rhs);
370 }
371 
372 template<typename T, typename Y>
373 inline bool operator>=(const Optional<T>& lhs, const Y& rhs)
374 {
375  return !(lhs < rhs);
376 }
377 
378 // Y vs Optional<T>
379 
380 template<typename T, typename Y>
381 inline bool operator==(const Y& lhs, const Optional<T>& rhs)
382 {
383  if(!rhs)
384  {
385  return false;
386  }
387  else
388  {
389  return lhs == *rhs;
390  }
391 }
392 
393 template<typename T, typename Y>
394 inline bool operator!=(const Y& lhs, const Optional<T>& rhs)
395 {
396  return !operator==(lhs, rhs);
397 }
398 
399 template<typename T, typename Y>
400 inline bool operator<(const Y& lhs, const Optional<T>& rhs)
401 {
402  if(rhs)
403  {
404  return lhs < *rhs;
405  }
406  else
407  {
408  return false;
409  }
410 }
411 
412 template<typename T, typename Y>
413 inline bool operator<=(const Y& lhs, const Optional<T>& rhs)
414 {
415  return lhs < rhs || lhs == rhs;
416 }
417 
418 template<typename T, typename Y>
419 inline bool operator>(const Y& lhs, const Optional<T>& rhs)
420 {
421  return !(lhs < rhs || lhs == rhs);
422 }
423 
424 template<typename T, typename Y>
425 inline bool operator>=(const Y& lhs, const Optional<T>& rhs)
426 {
427  return !(lhs < rhs);
428 }
429 
430 }
431 
432 #endif
433 #endif
IceUtil::operator==
bool operator==(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:88
IceUtil::operator>
bool operator>(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:130
IceUtil::None
constexpr std::experimental::Ice::nullopt_t None
For compatibility with the Ice C++98 mapping, do not use in new code:
Definition: Optional.h:1104
IceUtil::Optional
std::experimental::Ice::optional< T > Optional
For compatibility with the Ice C++98 mapping, do not use in new code:
Definition: Optional.h:1100
IceUtil::operator<
bool operator<(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:109
IceUtil
Definition: Optional.h:1095
IceUtil::operator>=
bool operator>=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:136
IceUtil::operator<=
bool operator<=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:124
Config.h
IceUtil::operator!=
bool operator!=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition: Handle.h:103