Agora C++ API Reference for All Platforms

Loading...
Searching...
No Matches
AgoraOptional.h
1// Copyright (c) 2019 Agora.io. All rights reserved
2
3// This program is confidential and proprietary to Agora.io.
4// And may not be copied, reproduced, modified, disclosed to others, published
5// or used, in whole or in part, without the express prior written permission
6// of Agora.io.
7#pragma once
8
9#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
10#include <type_traits>
11#endif
12#include <utility>
13
14#ifndef CONSTEXPR
15#if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
16#define CONSTEXPR constexpr
17#else
18#define CONSTEXPR
19#endif
20#endif // !CONSTEXPR
21
22#ifndef NOEXCEPT
23#if __cplusplus >= 201103L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201103L)
24#define NOEXCEPT(Expr) noexcept(Expr)
25#else
26#define NOEXCEPT(Expr)
27#endif
28#endif // !NOEXCEPT
29
30namespace agora {
31
32// Specification:
33// http://en.cppreference.com/w/cpp/utility/optional/in_place_t
34 struct in_place_t {};
35
36// Specification:
37// http://en.cppreference.com/w/cpp/utility/optional/nullopt_t
38 struct nullopt_t {
39 CONSTEXPR explicit nullopt_t(int) {}
40};
41
42// Specification:
43// http://en.cppreference.com/w/cpp/utility/optional/in_place
44/*CONSTEXPR*/ const in_place_t in_place = {};
45
46// Specification:
47// http://en.cppreference.com/w/cpp/utility/optional/nullopt
48/*CONSTEXPR*/ const nullopt_t nullopt(0);
49
50// Forward declaration, which is refered by following helpers.
51template <typename T>
52class Optional;
53
54 namespace internal {
55
56template <typename T>
57 struct OptionalStorageBase {
58 // Initializing |empty_| here instead of using default member initializing
59 // to avoid errors in g++ 4.8.
60 CONSTEXPR OptionalStorageBase() : is_populated_(false), empty_('\0') {}
61
62#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
63 template <class... Args>
64 CONSTEXPR explicit OptionalStorageBase(in_place_t, Args&&... args)
65 : is_populated_(true), value_(std::forward<Args>(args)...) {}
66#else
67 CONSTEXPR explicit OptionalStorageBase(in_place_t, const T& _value)
68 : is_populated_(true), value_(_value) {}
69#endif
70 // When T is not trivially destructible we must call its
71 // destructor before deallocating its memory.
72 // Note that this hides the (implicitly declared) move constructor, which
73 // would be used for constexpr move constructor in OptionalStorage<T>.
74 // It is needed iff T is trivially move constructible. However, the current
75 // is_trivially_{copy,move}_constructible implementation requires
76 // is_trivially_destructible (which looks a bug, cf:
77 // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51452 and
78 // http://cplusplus.github.io/LWG/lwg-active.html#2116), so it is not
79 // necessary for this case at the moment. Please see also the destructor
80 // comment in "is_trivially_destructible = true" specialization below.
81 ~OptionalStorageBase() {
82 if (is_populated_)
83 value_.~T();
84 }
85
86#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
87 template <class... Args>
88 void Init(Args&&... args) {
89 ::new (&value_) T(std::forward<Args>(args)...);
90 is_populated_ = true;
91 }
92#else
93 void Init(const T& _value) {
94 ::new (&value_) T(_value);
95 is_populated_ = true;
96 }
97#endif
98
99 bool is_populated_;
100
101 union {
102 // |empty_| exists so that the union will always be initialized, even when
103 // it doesn't contain a value. Union members must be initialized for the
104 // constructor to be 'constexpr'.
105 char empty_;
106 T value_;
107 };
108};
109
110// Implement conditional constexpr copy and move constructors. These are
111// constexpr if is_trivially_{copy,move}_constructible<T>::value is true
112// respectively. If each is true, the corresponding constructor is defined as
113// "= default;", which generates a constexpr constructor (In this case,
114// the condition of constexpr-ness is satisfied because the base class also has
115// compiler generated constexpr {copy,move} constructors). Note that
116// placement-new is prohibited in constexpr.
117template <typename T>
118 struct OptionalStorage : OptionalStorageBase<T> {
119 // This is no trivially {copy,move} constructible case. Other cases are
120 // defined below as specializations.
121
122 // Accessing the members of template base class requires explicit
123 // declaration.
124 using OptionalStorageBase<T>::is_populated_;
125 using OptionalStorageBase<T>::value_;
126 using OptionalStorageBase<T>::Init;
127
128 // Inherit constructors (specifically, the in_place constructor).
129 //using OptionalStorageBase<T>::OptionalStorageBase;
130
131#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
132 template <class... Args>
133 CONSTEXPR explicit OptionalStorage(in_place_t in_place, Args&&... args)
134 : OptionalStorageBase<T>(in_place, std::forward<Args>(args)...) {}
135#else
136 CONSTEXPR explicit OptionalStorage(in_place_t in_place, const T& _value)
137 : OptionalStorageBase<T>(in_place, _value) {}
138#endif
139
140 // User defined constructor deletes the default constructor.
141 // Define it explicitly.
142 OptionalStorage() {}
143
144 OptionalStorage(const OptionalStorage& other) {
145 if (other.is_populated_)
146 Init(other.value_);
147 }
148
149#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
150 OptionalStorage(OptionalStorage&& other) NOEXCEPT(std::is_nothrow_move_constructible<T>::value) {
151 if (other.is_populated_)
152 Init(std::move(other.value_));
153 }
154#endif
155};
156
157// Base class to support conditionally usable copy-/move- constructors
158// and assign operators.
159template <typename T>
160 class OptionalBase {
161 // This class provides implementation rather than public API, so everything
162 // should be hidden. Often we use composition, but we cannot in this case
163 // because of C++ language restriction.
164 protected:
165 CONSTEXPR OptionalBase() {}
166 CONSTEXPR OptionalBase(const OptionalBase& other) : storage_(other.storage_) {}
167#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
168 CONSTEXPR OptionalBase(OptionalBase&& other) : storage_(std::move(other.storage_)) {}
169
170 template <class... Args>
171 CONSTEXPR explicit OptionalBase(in_place_t, Args&&... args)
172 : storage_(in_place, std::forward<Args>(args)...) {}
173#else
174 CONSTEXPR explicit OptionalBase(in_place_t, const T& _value)
175 : storage_(in_place, _value) {}
176#endif
177
178 // Implementation of converting constructors.
179 template <typename U>
180 explicit OptionalBase(const OptionalBase<U>& other) {
181 if (other.storage_.is_populated_)
182 storage_.Init(other.storage_.value_);
183 }
184
185#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
186 template <typename U>
187 explicit OptionalBase(OptionalBase<U>&& other) {
188 if (other.storage_.is_populated_)
189 storage_.Init(std::move(other.storage_.value_));
190 }
191#endif
192
193 ~OptionalBase() {}
194
195 OptionalBase& operator=(const OptionalBase& other) {
196 CopyAssign(other);
197 return *this;
198 }
199
200#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
201 OptionalBase& operator=(OptionalBase&& other) NOEXCEPT(
202 std::is_nothrow_move_assignable<T>::value &&
203 std::is_nothrow_move_constructible<T>::value) {
204 MoveAssign(std::move(other));
205 return *this;
206 }
207#endif
208
209 template <typename U>
210 void CopyAssign(const OptionalBase<U>& other) {
211 if (other.storage_.is_populated_)
212 InitOrAssign(other.storage_.value_);
213 else
214 FreeIfNeeded();
215 }
216
217#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
218 template <typename U>
219 void MoveAssign(OptionalBase<U>&& other) {
220 if (other.storage_.is_populated_)
221 InitOrAssign(std::move(other.storage_.value_));
222 else
223 FreeIfNeeded();
224 }
225#endif
226
227 template <typename U>
228#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
229 void InitOrAssign(U&& value) {
230 if (storage_.is_populated_)
231 storage_.value_ = std::forward<U>(value);
232 else
233 storage_.Init(std::forward<U>(value));
234 }
235#else
236 void InitOrAssign(const U& value) {
237 if (storage_.is_populated_)
238 storage_.value_ = value;
239 else
240 storage_.Init(value);
241 }
242#endif
243
244
245 void FreeIfNeeded() {
246 if (!storage_.is_populated_)
247 return;
248 storage_.value_.~T();
249 storage_.is_populated_ = false;
250 }
251
252 // For implementing conversion, allow access to other typed OptionalBase
253 // class.
254 template <typename U>
255 friend class OptionalBase;
256
257 OptionalStorage<T> storage_;
258};
259
260// The following {Copy,Move}{Constructible,Assignable} structs are helpers to
261// implement constructor/assign-operator overloading. Specifically, if T is
262// is not movable but copyable, Optional<T>'s move constructor should not
263// participate in overload resolution. This inheritance trick implements that.
264template <bool is_copy_constructible>
265 struct CopyConstructible {};
266
267template <>
268 struct CopyConstructible<false> {
269 CONSTEXPR CopyConstructible() {}
270 CopyConstructible& operator=(const CopyConstructible&) { return *this; }
271#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
272 CONSTEXPR CopyConstructible(CopyConstructible&&) {}
273 CopyConstructible& operator=(CopyConstructible&&) { return *this; }
274#endif
275 private:
276 CONSTEXPR CopyConstructible(const CopyConstructible&);
277};
278
279template <bool is_move_constructible>
280 struct MoveConstructible {};
281
282template <>
283 struct MoveConstructible<false> {
284 CONSTEXPR MoveConstructible() {}
285 CONSTEXPR MoveConstructible(const MoveConstructible&) {}
286 MoveConstructible& operator=(const MoveConstructible&) { return *this; }
287#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
288 MoveConstructible& operator=(MoveConstructible&&) { return *this; }
289 private:
290 CONSTEXPR MoveConstructible(MoveConstructible&&);
291#endif
292};
293
294template <bool is_copy_assignable>
295 struct CopyAssignable {};
296
297template <>
298 struct CopyAssignable<false> {
299 CONSTEXPR CopyAssignable() {}
300 CONSTEXPR CopyAssignable(const CopyAssignable&) {}
301#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
302 CONSTEXPR CopyAssignable(CopyAssignable&&) {}
303 CopyAssignable& operator=(CopyAssignable&&) { return *this; }
304#endif
305 private:
306 CopyAssignable& operator=(const CopyAssignable&);
307};
308
309template <bool is_move_assignable>
310 struct MoveAssignable {};
311
312template <>
313 struct MoveAssignable<false> {
314 CONSTEXPR MoveAssignable() {}
315 CONSTEXPR MoveAssignable(const MoveAssignable&) {}
316 MoveAssignable& operator=(const MoveAssignable&) { return *this; }
317#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
318 CONSTEXPR MoveAssignable(MoveAssignable&&) {}
319
320 private:
321 MoveAssignable& operator=(MoveAssignable&&);
322#endif
323};
324
325#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
326// Helper to conditionally enable converting constructors and assign operators.
327template <typename T, typename U>
328 struct IsConvertibleFromOptional
329 : std::integral_constant<
330 bool,
331 std::is_constructible<T, Optional<U>&>::value ||
332 std::is_constructible<T, const Optional<U>&>::value ||
333 std::is_constructible<T, Optional<U>&&>::value ||
334 std::is_constructible<T, const Optional<U>&&>::value ||
335 std::is_convertible<Optional<U>&, T>::value ||
336 std::is_convertible<const Optional<U>&, T>::value ||
337 std::is_convertible<Optional<U>&&, T>::value ||
338 std::is_convertible<const Optional<U>&&, T>::value> {};
339
340template <typename T, typename U>
341 struct IsAssignableFromOptional
342 : std::integral_constant<
343 bool,
344 IsConvertibleFromOptional<T, U>::value ||
345 std::is_assignable<T&, Optional<U>&>::value ||
346 std::is_assignable<T&, const Optional<U>&>::value ||
347 std::is_assignable<T&, Optional<U>&&>::value ||
348 std::is_assignable<T&, const Optional<U>&&>::value> {};
349
350// Forward compatibility for C++17.
351// Introduce one more deeper nested namespace to avoid leaking using std::swap.
352 namespace swappable_impl {
353using std::swap;
354
355 struct IsSwappableImpl {
356 // Tests if swap can be called. Check<T&>(0) returns true_type iff swap
357 // is available for T. Otherwise, Check's overload resolution falls back
358 // to Check(...) declared below thanks to SFINAE, so returns false_type.
359 template <typename T>
360 static auto Check(int)
361 -> decltype(swap(std::declval<T>(), std::declval<T>()), std::true_type());
362
363 template <typename T>
364 static std::false_type Check(...);
365};
366} // namespace swappable_impl
367template <typename T>
368 struct IsSwappable : decltype(swappable_impl::IsSwappableImpl::Check<T&>(0)) {};
369#endif
370} // namespace internal
371
372// On Windows, by default, empty-base class optimization does not work,
373// which means even if the base class is empty struct, it still consumes one
374// byte for its body. __declspec(empty_bases) enables the optimization.
375// cf)
376// https://blogs.msdn.microsoft.com/vcblog/2016/03/30/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/
377#if defined(_WIN32)
378#define OPTIONAL_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
379#else
380#define OPTIONAL_DECLSPEC_EMPTY_BASES
381#endif
382
383// Optional is a Chromium version of the C++17 optional class:
384// std::optional documentation:
385// http://en.cppreference.com/w/cpp/utility/optional
386// Chromium documentation:
387// https://chromium.googlesource.com/chromium/src/+/master/docs/optional.md
388//
389// These are the differences between the specification and the implementation:
390// - Constructors do not use 'constexpr' as it is a C++14 extension.
391// - 'constexpr' might be missing in some places for reasons specified locally.
392// - No exceptions are thrown, because they are banned from Chromium.
393// Marked noexcept for only move constructor and move assign operators.
394// - All the non-members are in the 'base' namespace instead of 'std'.
395//
396// Note that T cannot have a constructor T(Optional<T>) etc. Optional<T> checks
397// T's constructor (specifically via IsConvertibleFromOptional), and in the
398// check whether T can be constructible from Optional<T>, which is recursive
399// so it does not work. As of Feb 2018, std::optional C++17 implementation in
400// both clang and gcc has same limitation. MSVC SFINAE looks to have different
401// behavior, but anyway it reports an error, too.
402template <typename T>
403 class OPTIONAL_DECLSPEC_EMPTY_BASES Optional
404 : public internal::OptionalBase<T>
405#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
406 , public internal::CopyConstructible<std::is_copy_constructible<T>::value>,
407 public internal::MoveConstructible<std::is_move_constructible<T>::value>,
408 public internal::CopyAssignable<std::is_copy_constructible<T>::value &&
409 std::is_copy_assignable<T>::value>,
410 public internal::MoveAssignable<std::is_move_constructible<T>::value &&
411 std::is_move_assignable<T>::value>
412#endif
413{
414 public:
415#undef OPTIONAL_DECLSPEC_EMPTY_BASES
416
417 typedef T value_type;
418
419 // Defer default/copy/move constructor implementation to OptionalBase.
420 CONSTEXPR Optional() {}
421 CONSTEXPR Optional(const Optional& other) : internal::OptionalBase<T>(other) {}
422
423 CONSTEXPR Optional(nullopt_t) {} // NOLINT(runtime/explicit)
424
425 // Converting copy constructor. "explicit" only if
426 // std::is_convertible<const U&, T>::value is false. It is implemented by
427 // declaring two almost same constructors, but that condition in enable_if_t
428 // is different, so that either one is chosen, thanks to SFINAE.
429 template <typename U>
430 Optional(const Optional<U>& other) : internal::OptionalBase<T>(other) {}
431
432#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
433 // Converting move constructor. Similar to converting copy constructor,
434 // declaring two (explicit and non-explicit) constructors.
435 template <typename U>
436 Optional(Optional<U>&& other) : internal::OptionalBase<T>(std::move(other)) {}
437
438 template <class... Args>
439 CONSTEXPR explicit Optional(in_place_t, Args&&... args)
440 : internal::OptionalBase<T>(in_place, std::forward<Args>(args)...) {}
441
442 template <class U, class... Args>
443 CONSTEXPR explicit Optional(in_place_t,
444 std::initializer_list<U> il,
445 Args&&... args)
446 : internal::OptionalBase<T>(in_place, il, std::forward<Args>(args)...) {}
447#else
448 CONSTEXPR explicit Optional(in_place_t, const T& _value)
449 : internal::OptionalBase<T>(in_place, _value) {}
450 template <class U>
451 CONSTEXPR explicit Optional(in_place_t,
452 const U il[],
453 const T& _value)
454 : internal::OptionalBase<T>(in_place, il, _value) {}
455#endif
456
457 // Forward value constructor. Similar to converting constructors,
458 // conditionally explicit.
459#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
460 template <typename U = value_type>
461 CONSTEXPR Optional(U&& value)
462 : internal::OptionalBase<T>(in_place, std::forward<U>(value)) {}
463#else
464 template <typename U>
465 CONSTEXPR Optional(const U& value)
466 : internal::OptionalBase<T>(in_place, value) {}
467#endif
468
469 ~Optional() {}
470
471 // Defer copy-/move- assign operator implementation to OptionalBase.
472 Optional& operator=(const Optional& other) {
473 if (&other == this) {
474 return *this;
475 }
476
477 internal::OptionalBase<T>::operator=(other);
478 return *this;
479 }
480
481 Optional& operator=(nullopt_t) {
482 FreeIfNeeded();
483 return *this;
484 }
485
486 // Perfect-forwarded assignment.
487 template <typename U>
488#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
489 Optional& operator=(U&& value) {
490 InitOrAssign(std::forward<U>(value));
491 return *this;
492 }
493#else
494 Optional& operator=(const U& value) {
495 InitOrAssign(value);
496 return *this;
497 }
498#endif
499
500 // Copy assign the state of other.
501 template <typename U>
502 Optional& operator=(const Optional<U>& other) {
503 CopyAssign(other);
504 return *this;
505 }
506
507#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
508 // Move assign the state of other.
509 template <typename U>
510 Optional& operator=(Optional<U>&& other) {
511 MoveAssign(std::move(other));
512 return *this;
513 }
514#endif
515
516 const T* operator->() const {
517 return &storage_.value_;
518 }
519
520 T* operator->() {
521 return &storage_.value_;
522 }
523
524 const T& operator*() const {
525 return storage_.value_;
526 }
527
528 T& operator*() {
529 return storage_.value_;
530 }
531
532
533#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
534 CONSTEXPR explicit operator bool() const { return storage_.is_populated_; }
535#else
536 CONSTEXPR operator bool() const { return storage_.is_populated_; }
537#endif
538
539 CONSTEXPR bool has_value() const { return storage_.is_populated_; }
540
541#if 1
542 const T& value() const {
543 return storage_.value_;
544 }
545
546 template <class U>
547#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
548 CONSTEXPR T value_or(U&& default_value) const {
549 // TODO(mlamouri): add the following assert when possible:
550 // static_assert(std::is_copy_constructible<T>::value,
551 // "T must be copy constructible");
552 static_assert(std::is_convertible<U, T>::value,
553 "U must be convertible to T");
554 return storage_.is_populated_
555 ? value()
556 : static_cast<T>(std::forward<U>(default_value));
557 }
558#else
559 CONSTEXPR T value_or(const U& default_value) const {
560 return storage_.is_populated_
561 ? value()
562 : static_cast<T>(default_value);
563 }
564#endif
565#else
566 const T& value() const & {
567 return storage_.value_;
568 }
569
570#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
571 const T&& value() const && {
572 return std::move(storage_.value_);
573 }
574#endif
575
576 template <class U>
577#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
578 CONSTEXPR T value_or(U&& default_value) const & {
579 // TODO(mlamouri): add the following assert when possible:
580 // static_assert(std::is_copy_constructible<T>::value,
581 // "T must be copy constructible");
582 static_assert(std::is_convertible<U, T>::value,
583 "U must be convertible to T");
584 return storage_.is_populated_
585 ? value()
586 : static_cast<T>(std::forward<U>(default_value));
587 }
588#else
589 CONSTEXPR T value_or(const U& default_value) const & {
590 // TODO(mlamouri): add the following assert when possible:
591 // static_assert(std::is_copy_constructible<T>::value,
592 // "T must be copy constructible");
593 static_assert(std::is_convertible<U, T>::value,
594 "U must be convertible to T");
595 return storage_.is_populated_
596 ? value()
597 : static_cast<T>(default_value);
598 }
599#endif
600
601 template <class U>
602#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
603 CONSTEXPR T value_or(U&& default_value) const && {
604 // TODO(mlamouri): add the following assert when possible:
605 // static_assert(std::is_move_constructible<T>::value,
606 // "T must be move constructible");
607 static_assert(std::is_convertible<U, T>::value,
608 "U must be convertible to T");
609 return storage_.is_populated_
610 ? std::move(value())
611 : static_cast<T>(std::forward<U>(default_value));
612 }
613#endif
614#endif // 1
615
616 void swap(Optional& other) {
617 if (!storage_.is_populated_ && !other.storage_.is_populated_)
618 return;
619
620#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
621 if (storage_.is_populated_ != other.storage_.is_populated_) {
622 if (storage_.is_populated_) {
623 other.storage_.Init(std::move(storage_.value_));
624 FreeIfNeeded();
625 } else {
626 storage_.Init(std::move(other.storage_.value_));
627 other.FreeIfNeeded();
628 }
629 return;
630 }
631#endif
632 using std::swap;
633 swap(**this, *other);
634 }
635
636 void reset() { FreeIfNeeded(); }
637
638#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
639 template <class... Args>
640 T& emplace(Args&&... args) {
641 FreeIfNeeded();
642 storage_.Init(std::forward<Args>(args)...);
643 return storage_.value_;
644 }
645
646 template <class U, class... Args>
647 T& emplace(std::initializer_list<U> il, Args&&... args) {
648 FreeIfNeeded();
649 storage_.Init(il, std::forward<Args>(args)...);
650 return storage_.value_;
651 }
652#else
653 T& emplace(const T& _value) {
654 FreeIfNeeded();
655 storage_.Init(_value);
656 return storage_.value_;
657 }
658 template <class U>
659 T& emplace(const U il[], const T& _value) {
660 FreeIfNeeded();
661 storage_.Init(il, _value);
662 return storage_.value_;
663 }
664#endif
665
666 private:
667 // Accessing template base class's protected member needs explicit
668 // declaration to do so.
669 using internal::OptionalBase<T>::CopyAssign;
670 using internal::OptionalBase<T>::FreeIfNeeded;
671 using internal::OptionalBase<T>::InitOrAssign;
672#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
673 using internal::OptionalBase<T>::MoveAssign;
674#endif
675 using internal::OptionalBase<T>::storage_;
676};
677
678// Here after defines comparation operators. The definition follows
679// http://en.cppreference.com/w/cpp/utility/optional/operator_cmp
680// while bool() casting is replaced by has_value() to meet the chromium
681// style guide.
682template <class T, class U>
683 bool operator==(const Optional<T>& lhs, const Optional<U>& rhs) {
684 if (lhs.has_value() != rhs.has_value())
685 return false;
686 if (!lhs.has_value())
687 return true;
688 return *lhs == *rhs;
689}
690
691template <class T, class U>
692 bool operator!=(const Optional<T>& lhs, const Optional<U>& rhs) {
693 if (lhs.has_value() != rhs.has_value())
694 return true;
695 if (!lhs.has_value())
696 return false;
697 return *lhs != *rhs;
698}
699
700template <class T, class U>
701 bool operator<(const Optional<T>& lhs, const Optional<U>& rhs) {
702 if (!rhs.has_value())
703 return false;
704 if (!lhs.has_value())
705 return true;
706 return *lhs < *rhs;
707}
708
709template <class T, class U>
710 bool operator<=(const Optional<T>& lhs, const Optional<U>& rhs) {
711 if (!lhs.has_value())
712 return true;
713 if (!rhs.has_value())
714 return false;
715 return *lhs <= *rhs;
716}
717
718template <class T, class U>
719 bool operator>(const Optional<T>& lhs, const Optional<U>& rhs) {
720 if (!lhs.has_value())
721 return false;
722 if (!rhs.has_value())
723 return true;
724 return *lhs > *rhs;
725}
726
727template <class T, class U>
728 bool operator>=(const Optional<T>& lhs, const Optional<U>& rhs) {
729 if (!rhs.has_value())
730 return true;
731 if (!lhs.has_value())
732 return false;
733 return *lhs >= *rhs;
734}
735
736template <class T>
737CONSTEXPR bool operator==(const Optional<T>& opt, nullopt_t) {
738 return !opt;
739}
740
741template <class T>
742CONSTEXPR bool operator==(nullopt_t, const Optional<T>& opt) {
743 return !opt;
744}
745
746template <class T>
747CONSTEXPR bool operator!=(const Optional<T>& opt, nullopt_t) {
748 return opt.has_value();
749}
750
751template <class T>
752CONSTEXPR bool operator!=(nullopt_t, const Optional<T>& opt) {
753 return opt.has_value();
754}
755
756template <class T>
757CONSTEXPR bool operator<(const Optional<T>& , nullopt_t) {
758 return false;
759}
760
761template <class T>
762CONSTEXPR bool operator<(nullopt_t, const Optional<T>& opt) {
763 return opt.has_value();
764}
765
766template <class T>
767CONSTEXPR bool operator<=(const Optional<T>& opt, nullopt_t) {
768 return !opt;
769}
770
771template <class T>
772CONSTEXPR bool operator<=(nullopt_t, const Optional<T>& ) {
773 return true;
774}
775
776template <class T>
777CONSTEXPR bool operator>(const Optional<T>& opt, nullopt_t) {
778 return opt.has_value();
779}
780
781template <class T>
782CONSTEXPR bool operator>(nullopt_t, const Optional<T>& ) {
783 return false;
784}
785
786template <class T>
787CONSTEXPR bool operator>=(const Optional<T>& , nullopt_t) {
788 return true;
789}
790
791template <class T>
792CONSTEXPR bool operator>=(nullopt_t, const Optional<T>& opt) {
793 return !opt;
794}
795
796template <class T, class U>
797CONSTEXPR bool operator==(const Optional<T>& opt, const U& value) {
798 return opt.has_value() ? *opt == value : false;
799}
800
801template <class T, class U>
802CONSTEXPR bool operator==(const U& value, const Optional<T>& opt) {
803 return opt.has_value() ? value == *opt : false;
804}
805
806template <class T, class U>
807CONSTEXPR bool operator!=(const Optional<T>& opt, const U& value) {
808 return opt.has_value() ? *opt != value : true;
809}
810
811template <class T, class U>
812CONSTEXPR bool operator!=(const U& value, const Optional<T>& opt) {
813 return opt.has_value() ? value != *opt : true;
814}
815
816template <class T, class U>
817CONSTEXPR bool operator<(const Optional<T>& opt, const U& value) {
818 return opt.has_value() ? *opt < value : true;
819}
820
821template <class T, class U>
822CONSTEXPR bool operator<(const U& value, const Optional<T>& opt) {
823 return opt.has_value() ? value < *opt : false;
824}
825
826template <class T, class U>
827CONSTEXPR bool operator<=(const Optional<T>& opt, const U& value) {
828 return opt.has_value() ? *opt <= value : true;
829}
830
831template <class T, class U>
832CONSTEXPR bool operator<=(const U& value, const Optional<T>& opt) {
833 return opt.has_value() ? value <= *opt : false;
834}
835
836template <class T, class U>
837CONSTEXPR bool operator>(const Optional<T>& opt, const U& value) {
838 return opt.has_value() ? *opt > value : false;
839}
840
841template <class T, class U>
842CONSTEXPR bool operator>(const U& value, const Optional<T>& opt) {
843 return opt.has_value() ? value > *opt : true;
844}
845
846template <class T, class U>
847CONSTEXPR bool operator>=(const Optional<T>& opt, const U& value) {
848 return opt.has_value() ? *opt >= value : false;
849}
850
851template <class T, class U>
852CONSTEXPR bool operator>=(const U& value, const Optional<T>& opt) {
853 return opt.has_value() ? value >= *opt : true;
854}
855
856#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
857template <class T, class... Args>
858CONSTEXPR Optional<T> make_optional(Args&&... args) {
859 return Optional<T>(in_place, std::forward<Args>(args)...);
860}
861
862template <class T, class U, class... Args>
863CONSTEXPR Optional<T> make_optional(std::initializer_list<U> il,
864 Args&&... args) {
865 return Optional<T>(in_place, il, std::forward<Args>(args)...);
866}
867#endif
868
869// Partial specialization for a function template is not allowed. Also, it is
870// not allowed to add overload function to std namespace, while it is allowed
871// to specialize the template in std. Thus, swap() (kind of) overloading is
872// defined in base namespace, instead.
873template <class T>
874 void swap(Optional<T>& lhs, Optional<T>& rhs) {
875 lhs.swap(rhs);
876}
877
878} // namespace agora
879
880#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
881 namespace std {
882template <class T>
883 struct hash<agora::Optional<T> > {
884 size_t operator()(const agora::Optional<T>& opt) const {
885 return opt == agora::nullopt ? 0 : std::hash<T>()(*opt);
886 }
887};
888} // namespace std
889#endif
890#undef CONSTEXPR
891#undef NOEXCEPT
agora::Optional
Definition AgoraOptional.h:413
agora::Optional::operator=
Optional & operator=(nullopt_t)
Definition AgoraOptional.h:481
agora::Optional::value_type
T value_type
Definition AgoraOptional.h:417
agora::Optional::value_or
CONSTEXPR T value_or(const U &default_value) const &
Definition AgoraOptional.h:589
agora::Optional::value_or
CONSTEXPR T value_or(U &&default_value) const &&
Definition AgoraOptional.h:603
agora::Optional::Optional
CONSTEXPR Optional(const U &value)
Definition AgoraOptional.h:465
agora::Optional::value_or
CONSTEXPR T value_or(U &&default_value) const
Definition AgoraOptional.h:548
agora::Optional::operator*
const T & operator*() const
Definition AgoraOptional.h:524
agora::Optional::emplace
T & emplace(std::initializer_list< U > il, Args &&... args)
Definition AgoraOptional.h:647
agora::Optional::operator->
T * operator->()
Definition AgoraOptional.h:520
agora::Optional::Optional
Optional(Optional< U > &&other)
Definition AgoraOptional.h:436
agora::Optional::emplace
T & emplace(const U il[], const T &_value)
Definition AgoraOptional.h:659
agora::Optional::operator=
Optional & operator=(const Optional &other)
Definition AgoraOptional.h:472
agora::Optional::Optional
CONSTEXPR Optional(nullopt_t)
Definition AgoraOptional.h:423
agora::Optional::value_or
CONSTEXPR T value_or(const U &default_value) const
Definition AgoraOptional.h:559
agora::Optional::Optional
CONSTEXPR Optional(in_place_t, Args &&... args)
Definition AgoraOptional.h:439
agora::Optional::operator=
Optional & operator=(const Optional< U > &other)
Definition AgoraOptional.h:502
agora::Optional::Optional
CONSTEXPR Optional(in_place_t, const U il[], const T &_value)
Definition AgoraOptional.h:451
agora::Optional::value
const T && value() const &&
Definition AgoraOptional.h:571
agora::Optional::swap
void swap(Optional &other)
Definition AgoraOptional.h:616
agora::Optional::Optional
CONSTEXPR Optional()
Definition AgoraOptional.h:420
agora::Optional::Optional
CONSTEXPR Optional(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition AgoraOptional.h:443
agora::Optional::reset
void reset()
Definition AgoraOptional.h:636
agora::Optional::value
const T & value() const &
Definition AgoraOptional.h:566
agora::Optional::value_or
CONSTEXPR T value_or(U &&default_value) const &
Definition AgoraOptional.h:578
agora::Optional::~Optional
~Optional()
Definition AgoraOptional.h:469
agora::Optional::operator=
Optional & operator=(Optional< U > &&other)
Definition AgoraOptional.h:510
agora::Optional::Optional
CONSTEXPR Optional(in_place_t, const T &_value)
Definition AgoraOptional.h:448
agora::Optional::operator*
T & operator*()
Definition AgoraOptional.h:528
agora::Optional::has_value
CONSTEXPR bool has_value() const
Definition AgoraOptional.h:539
agora::Optional::emplace
T & emplace(const T &_value)
Definition AgoraOptional.h:653
agora::Optional::Optional
CONSTEXPR Optional(const Optional &other)
Definition AgoraOptional.h:421
agora::Optional::Optional
Optional(const Optional< U > &other)
Definition AgoraOptional.h:430
agora::Optional::operator->
const T * operator->() const
Definition AgoraOptional.h:516
agora::Optional::emplace
T & emplace(Args &&... args)
Definition AgoraOptional.h:640
agora::Optional::operator=
Optional & operator=(const U &value)
Definition AgoraOptional.h:494
agora::Optional::value
const T & value() const
Definition AgoraOptional.h:542
agora::Optional::Optional
CONSTEXPR Optional(U &&value)
Definition AgoraOptional.h:461
agora::Optional::operator=
Optional & operator=(U &&value)
Definition AgoraOptional.h:489
agora::internal::OptionalBase
Definition AgoraOptional.h:160
agora::internal::OptionalBase::storage_
OptionalStorage< T > storage_
Definition AgoraOptional.h:257
agora::internal::OptionalBase::OptionalBase
CONSTEXPR OptionalBase(in_place_t, const T &_value)
Definition AgoraOptional.h:174
agora::internal::OptionalBase::operator=
OptionalBase & operator=(OptionalBase &&other) NOEXCEPT(std
Definition AgoraOptional.h:201
agora::internal::OptionalBase::FreeIfNeeded
void FreeIfNeeded()
Definition AgoraOptional.h:245
agora::internal::OptionalBase::OptionalBase
OptionalBase(const OptionalBase< U > &other)
Definition AgoraOptional.h:180
agora::internal::OptionalBase::OptionalBase
CONSTEXPR OptionalBase(const OptionalBase &other)
Definition AgoraOptional.h:166
agora::internal::OptionalBase::MoveAssign
void MoveAssign(OptionalBase< U > &&other)
Definition AgoraOptional.h:219
agora::internal::OptionalBase::operator=
OptionalBase & operator=(const OptionalBase &other)
Definition AgoraOptional.h:195
agora::internal::OptionalBase::OptionalBase
CONSTEXPR OptionalBase()
Definition AgoraOptional.h:165
agora::internal::OptionalBase::InitOrAssign
void InitOrAssign(const U &value)
Definition AgoraOptional.h:236
agora::internal::OptionalBase::OptionalBase
CONSTEXPR OptionalBase(in_place_t, Args &&... args)
Definition AgoraOptional.h:171
agora::internal::OptionalBase::OptionalBase
CONSTEXPR OptionalBase(OptionalBase &&other)
Definition AgoraOptional.h:168
agora::internal::OptionalBase::OptionalBase
friend class OptionalBase
Definition AgoraOptional.h:255
agora::internal::OptionalBase::CopyAssign
void CopyAssign(const OptionalBase< U > &other)
Definition AgoraOptional.h:210
agora::internal::OptionalBase::InitOrAssign
void InitOrAssign(U &&value)
Definition AgoraOptional.h:229
agora::internal::OptionalBase::~OptionalBase
~OptionalBase()
Definition AgoraOptional.h:193
agora::internal::OptionalBase::OptionalBase
OptionalBase(OptionalBase< U > &&other)
Definition AgoraOptional.h:187
agora::internal::swappable_impl
Definition AgoraOptional.h:352
agora::internal
Definition AgoraOptional.h:54
agora
Definition AgoraAtomicOps.h:21
agora::swap
void swap(Optional< T > &lhs, Optional< T > &rhs)
Definition AgoraOptional.h:874
agora::operator!=
bool operator!=(const Optional< T > &lhs, const Optional< U > &rhs)
Definition AgoraOptional.h:692
agora::in_place
const in_place_t in_place
Definition AgoraOptional.h:44
agora::operator==
bool operator==(const Optional< T > &lhs, const Optional< U > &rhs)
Definition AgoraOptional.h:683
agora::operator>=
bool operator>=(const Optional< T > &lhs, const Optional< U > &rhs)
Definition AgoraOptional.h:728
agora::operator<
bool operator<(const Optional< T > &lhs, const Optional< U > &rhs)
Definition AgoraOptional.h:701
agora::make_optional
CONSTEXPR Optional< T > make_optional(Args &&... args)
Definition AgoraOptional.h:858
agora::operator<=
bool operator<=(const Optional< T > &lhs, const Optional< U > &rhs)
Definition AgoraOptional.h:710
agora::nullopt
const nullopt_t nullopt(0)
agora::operator>
bool operator>(const Optional< T > &lhs, const Optional< U > &rhs)
Definition AgoraOptional.h:719
std
Definition AgoraOptional.h:881
agora::in_place_t
Definition AgoraOptional.h:34
agora::internal::CopyAssignable< false >::CopyAssignable
CONSTEXPR CopyAssignable(CopyAssignable &&)
Definition AgoraOptional.h:302
agora::internal::CopyAssignable< false >::CopyAssignable
CONSTEXPR CopyAssignable(const CopyAssignable &)
Definition AgoraOptional.h:300
agora::internal::CopyAssignable< false >::operator=
CopyAssignable & operator=(CopyAssignable &&)
Definition AgoraOptional.h:303
agora::internal::CopyAssignable< false >::CopyAssignable
CONSTEXPR CopyAssignable()
Definition AgoraOptional.h:299
agora::internal::CopyAssignable
Definition AgoraOptional.h:295
agora::internal::CopyConstructible< false >::CopyConstructible
CONSTEXPR CopyConstructible()
Definition AgoraOptional.h:269
agora::internal::CopyConstructible< false >::operator=
CopyConstructible & operator=(CopyConstructible &&)
Definition AgoraOptional.h:273
agora::internal::CopyConstructible< false >::operator=
CopyConstructible & operator=(const CopyConstructible &)
Definition AgoraOptional.h:270
agora::internal::CopyConstructible< false >::CopyConstructible
CONSTEXPR CopyConstructible(CopyConstructible &&)
Definition AgoraOptional.h:272
agora::internal::CopyConstructible
Definition AgoraOptional.h:265
agora::internal::IsAssignableFromOptional
Definition AgoraOptional.h:348
agora::internal::IsConvertibleFromOptional
Definition AgoraOptional.h:338
agora::internal::IsSwappable
Definition AgoraOptional.h:368
agora::internal::MoveAssignable< false >::operator=
MoveAssignable & operator=(const MoveAssignable &)
Definition AgoraOptional.h:316
agora::internal::MoveAssignable< false >::MoveAssignable
CONSTEXPR MoveAssignable(MoveAssignable &&)
Definition AgoraOptional.h:318
agora::internal::MoveAssignable< false >::MoveAssignable
CONSTEXPR MoveAssignable()
Definition AgoraOptional.h:314
agora::internal::MoveAssignable< false >::MoveAssignable
CONSTEXPR MoveAssignable(const MoveAssignable &)
Definition AgoraOptional.h:315
agora::internal::MoveAssignable
Definition AgoraOptional.h:310
agora::internal::MoveConstructible< false >::operator=
MoveConstructible & operator=(const MoveConstructible &)
Definition AgoraOptional.h:286
agora::internal::MoveConstructible< false >::MoveConstructible
CONSTEXPR MoveConstructible(const MoveConstructible &)
Definition AgoraOptional.h:285
agora::internal::MoveConstructible< false >::MoveConstructible
CONSTEXPR MoveConstructible()
Definition AgoraOptional.h:284
agora::internal::MoveConstructible< false >::operator=
MoveConstructible & operator=(MoveConstructible &&)
Definition AgoraOptional.h:288
agora::internal::MoveConstructible
Definition AgoraOptional.h:280
agora::internal::OptionalStorageBase::Init
void Init(Args &&... args)
Definition AgoraOptional.h:88
agora::internal::OptionalStorageBase::empty_
char empty_
Definition AgoraOptional.h:105
agora::internal::OptionalStorageBase::OptionalStorageBase
CONSTEXPR OptionalStorageBase(in_place_t, const T &_value)
Definition AgoraOptional.h:67
agora::internal::OptionalStorageBase::~OptionalStorageBase
~OptionalStorageBase()
Definition AgoraOptional.h:81
agora::internal::OptionalStorageBase::OptionalStorageBase
CONSTEXPR OptionalStorageBase(in_place_t, Args &&... args)
Definition AgoraOptional.h:64
agora::internal::OptionalStorageBase::Init
void Init(const T &_value)
Definition AgoraOptional.h:93
agora::internal::OptionalStorageBase::is_populated_
bool is_populated_
Definition AgoraOptional.h:99
agora::internal::OptionalStorageBase::OptionalStorageBase
CONSTEXPR OptionalStorageBase()
Definition AgoraOptional.h:60
agora::internal::OptionalStorageBase::value_
T value_
Definition AgoraOptional.h:106
agora::internal::OptionalStorage
Definition AgoraOptional.h:118
agora::internal::OptionalStorage::OptionalStorage
CONSTEXPR OptionalStorage(in_place_t in_place, const T &_value)
Definition AgoraOptional.h:136
agora::internal::OptionalStorage::OptionalStorage
CONSTEXPR OptionalStorage(in_place_t in_place, Args &&... args)
Definition AgoraOptional.h:133
agora::internal::OptionalStorage::OptionalStorage
OptionalStorage(const OptionalStorage &other)
Definition AgoraOptional.h:144
agora::internal::OptionalStorage::OptionalStorage
OptionalStorage()
Definition AgoraOptional.h:142
agora::internal::OptionalStorage::OptionalStorage
OptionalStorage(OptionalStorage &&other) NOEXCEPT(std
Definition AgoraOptional.h:150
agora::internal::swappable_impl::IsSwappableImpl
Definition AgoraOptional.h:355
agora::internal::swappable_impl::IsSwappableImpl::Check
static std::false_type Check(...)
agora::internal::swappable_impl::IsSwappableImpl::Check
static auto Check(int) -> decltype(swap(std::declval< T >(), std::declval< T >()), std::true_type())
agora::nullopt_t
Definition AgoraOptional.h:38
agora::nullopt_t::nullopt_t
CONSTEXPR nullopt_t(int)
Definition AgoraOptional.h:39
std::hash< agora::Optional< T > >::operator()
size_t operator()(const agora::Optional< T > &opt) const
Definition AgoraOptional.h:884