Ice 3.7 C++11 API Reference
Loading...
Searching...
No Matches
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
12namespace IceUtilInternal
13{
14
15struct NoneType
16{
17};
18
19}
20
21namespace IceUtil
22{
23
25const IceUtilInternal::NoneType None = {};
26
28template<typename T>
29class Optional
30{
31public:
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
241private:
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
262template<class T> inline Optional<T>
263makeOptional(const T& v)
264{
265 return Optional<T>(v);
266}
267
269template<typename T> inline void
270Optional<T>::throwOptionalNotSetException(const char* file, int line) const
271{
272 throw OptionalNotSetException(file, line);
273}
275
276template<typename T, typename U>
277inline 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
289template<typename T, typename U>
290inline bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs)
291{
292 return !operator==(lhs, rhs);
293}
294
295template<typename T, typename U>
296inline 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
308template<typename T, typename U>
309inline bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs)
310{
311 return lhs < rhs || lhs == rhs;
312}
313
314template<typename T, typename U>
315inline bool operator>(const Optional<T>& lhs, const Optional<U>& rhs)
316{
317 return !(lhs < rhs || lhs == rhs);
318}
319
320template<typename T, typename U>
321inline bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs)
322{
323 return !(lhs < rhs);
324}
325
326// Optional<T> vs Y
327
328template<typename T, typename Y>
329inline 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
341template<typename T, typename Y>
342inline bool operator!=(const Optional<T>& lhs, const Y& rhs)
343{
344 return !operator==(lhs, rhs);
345}
346
347template<typename T, typename Y>
348inline 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
360template<typename T, typename Y>
361inline bool operator<=(const Optional<T>& lhs, const Y& rhs)
362{
363 return lhs < rhs || lhs == rhs;
364}
365
366template<typename T, typename Y>
367inline bool operator>(const Optional<T>& lhs, const Y& rhs)
368{
369 return !(lhs < rhs || lhs == rhs);
370}
371
372template<typename T, typename Y>
373inline bool operator>=(const Optional<T>& lhs, const Y& rhs)
374{
375 return !(lhs < rhs);
376}
377
378// Y vs Optional<T>
379
380template<typename T, typename Y>
381inline 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
393template<typename T, typename Y>
394inline bool operator!=(const Y& lhs, const Optional<T>& rhs)
395{
396 return !operator==(lhs, rhs);
397}
398
399template<typename T, typename Y>
400inline 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
412template<typename T, typename Y>
413inline bool operator<=(const Y& lhs, const Optional<T>& rhs)
414{
415 return lhs < rhs || lhs == rhs;
416}
417
418template<typename T, typename Y>
419inline bool operator>(const Y& lhs, const Optional<T>& rhs)
420{
421 return !(lhs < rhs || lhs == rhs);
422}
423
424template<typename T, typename Y>
425inline bool operator>=(const Y& lhs, const Optional<T>& rhs)
426{
427 return !(lhs < rhs);
428}
429
430}
431
432#endif
433#endif
This exception indicates an IceUtil::Optional is not set.
Definition Exception.h:372
Definition Optional.h:1095
bool operator>(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:130
bool operator<(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:109
bool operator<=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:124
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
bool operator>=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:136
std::experimental::Ice::optional< T > Optional
For compatibility with the Ice C++98 mapping, do not use in new code:
Definition Optional.h:1100
bool operator!=(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:103
bool operator==(const HandleBase< T > &lhs, const HandleBase< U > &rhs)
Definition Handle.h:88