Agora Server Gateway SDK C++ API Reference
AgoraOptional.h
Go to the documentation of this file.
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
34struct in_place_t {};
35
36// Specification:
37// http://en.cppreference.com/w/cpp/utility/optional/nullopt_t
38struct 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
54namespace internal {
55
56template <typename T>
58 // Initializing |empty_| here instead of using default member initializing
59 // to avoid errors in g++ 4.8.
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.
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
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_;
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>
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.
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.
143
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>
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:
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
194
196 CopyAssign(other);
197 return *this;
198 }
199
200#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
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
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
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>
266
267template <>
268struct CopyConstructible<false> {
271#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
273 CopyConstructible& operator=(CopyConstructible&&) { return *this; }
274#endif
275 private:
276 CONSTEXPR CopyConstructible(const CopyConstructible&);
277};
278
279template <bool is_move_constructible>
281
282template <>
283struct MoveConstructible<false> {
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>
296
297template <>
298struct CopyAssignable<false> {
301#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
303 CopyAssignable& operator=(CopyAssignable&&) { return *this; }
304#endif
305 private:
306 CopyAssignable& operator=(const CopyAssignable&);
307};
308
309template <bool is_move_assignable>
311
312template <>
313struct MoveAssignable<false> {
316 MoveAssignable& operator=(const MoveAssignable&) { return *this; }
317#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
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>
328struct 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>
341struct 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.
352namespace swappable_impl {
353using std::swap;
354
355struct 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>
368struct 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>
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.
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>
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
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
478 return *this;
479 }
480
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>
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
521 return &storage_.value_;
522 }
523
524 const T& operator*() const {
525 return storage_.value_;
526 }
527
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.
672#if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
674#endif
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>
683bool 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>
692bool 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>
701bool 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>
710bool 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>
719bool 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>
728bool 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>
738 return !opt;
739}
740
741template <class T>
743 return !opt;
744}
745
746template <class T>
748 return opt.has_value();
749}
750
751template <class T>
753 return opt.has_value();
754}
755
756template <class T>
758 return false;
759}
760
761template <class T>
763 return opt.has_value();
764}
765
766template <class T>
768 return !opt;
769}
770
771template <class T>
773 return true;
774}
775
776template <class T>
778 return opt.has_value();
779}
780
781template <class T>
783 return false;
784}
785
786template <class T>
788 return true;
789}
790
791template <class T>
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>
874void 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)
881namespace std {
882template <class T>
883struct 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
#define NOEXCEPT(Expr)
Definition: AgoraOptional.h:26
#define OPTIONAL_DECLSPEC_EMPTY_BASES
Definition: AgoraOptional.h:380
#define CONSTEXPR
Definition: AgoraOptional.h:18
Definition: AgoraOptional.h:413
CONSTEXPR T value_or(const U &default_value) const
Definition: AgoraOptional.h:559
Optional & operator=(nullopt_t)
Definition: AgoraOptional.h:481
T value_type
Definition: AgoraOptional.h:417
CONSTEXPR Optional(const U &value)
Definition: AgoraOptional.h:465
Optional & operator=(const U &value)
Definition: AgoraOptional.h:494
const T & operator*() const
Definition: AgoraOptional.h:524
T * operator->()
Definition: AgoraOptional.h:520
T & emplace(const U il[], const T &_value)
Definition: AgoraOptional.h:659
Optional & operator=(const Optional &other)
Definition: AgoraOptional.h:472
CONSTEXPR Optional(nullopt_t)
Definition: AgoraOptional.h:423
Optional & operator=(const Optional< U > &other)
Definition: AgoraOptional.h:502
CONSTEXPR Optional(in_place_t, const U il[], const T &_value)
Definition: AgoraOptional.h:451
void swap(Optional &other)
Definition: AgoraOptional.h:616
CONSTEXPR Optional()
Definition: AgoraOptional.h:420
void reset()
Definition: AgoraOptional.h:636
~Optional()
Definition: AgoraOptional.h:469
CONSTEXPR Optional(in_place_t, const T &_value)
Definition: AgoraOptional.h:448
T & operator*()
Definition: AgoraOptional.h:528
CONSTEXPR bool has_value() const
Definition: AgoraOptional.h:539
T & emplace(const T &_value)
Definition: AgoraOptional.h:653
CONSTEXPR Optional(const Optional &other)
Definition: AgoraOptional.h:421
Optional(const Optional< U > &other)
Definition: AgoraOptional.h:430
const T * operator->() const
Definition: AgoraOptional.h:516
const T & value() const
Definition: AgoraOptional.h:542
Definition: AgoraOptional.h:160
OptionalStorage< T > storage_
Definition: AgoraOptional.h:257
CONSTEXPR OptionalBase(in_place_t, const T &_value)
Definition: AgoraOptional.h:174
void FreeIfNeeded()
Definition: AgoraOptional.h:245
OptionalBase(const OptionalBase< U > &other)
Definition: AgoraOptional.h:180
void InitOrAssign(const U &value)
Definition: AgoraOptional.h:236
CONSTEXPR OptionalBase(const OptionalBase &other)
Definition: AgoraOptional.h:166
OptionalBase & operator=(const OptionalBase &other)
Definition: AgoraOptional.h:195
CONSTEXPR OptionalBase()
Definition: AgoraOptional.h:165
friend class OptionalBase
Definition: AgoraOptional.h:255
void CopyAssign(const OptionalBase< U > &other)
Definition: AgoraOptional.h:210
~OptionalBase()
Definition: AgoraOptional.h:193
Definition: AgoraBase.h:86
void swap(Optional< T > &lhs, Optional< T > &rhs)
Definition: AgoraOptional.h:874
bool operator!=(const Optional< T > &lhs, const Optional< U > &rhs)
Definition: AgoraOptional.h:692
const in_place_t in_place
Definition: AgoraOptional.h:44
bool operator==(const Optional< T > &lhs, const Optional< U > &rhs)
Definition: AgoraOptional.h:683
bool operator>=(const Optional< T > &lhs, const Optional< U > &rhs)
Definition: AgoraOptional.h:728
bool operator<(const Optional< T > &lhs, const Optional< U > &rhs)
Definition: AgoraOptional.h:701
bool operator<=(const Optional< T > &lhs, const Optional< U > &rhs)
Definition: AgoraOptional.h:710
const nullopt_t nullopt(0)
bool operator>(const Optional< T > &lhs, const Optional< U > &rhs)
Definition: AgoraOptional.h:719
Definition: AgoraOptional.h:34
CONSTEXPR CopyAssignable(const CopyAssignable &)
Definition: AgoraOptional.h:300
CONSTEXPR CopyAssignable()
Definition: AgoraOptional.h:299
Definition: AgoraOptional.h:295
CONSTEXPR CopyConstructible()
Definition: AgoraOptional.h:269
CopyConstructible & operator=(const CopyConstructible &)
Definition: AgoraOptional.h:270
Definition: AgoraOptional.h:265
MoveAssignable & operator=(const MoveAssignable &)
Definition: AgoraOptional.h:316
CONSTEXPR MoveAssignable()
Definition: AgoraOptional.h:314
CONSTEXPR MoveAssignable(const MoveAssignable &)
Definition: AgoraOptional.h:315
Definition: AgoraOptional.h:310
MoveConstructible & operator=(const MoveConstructible &)
Definition: AgoraOptional.h:286
CONSTEXPR MoveConstructible(const MoveConstructible &)
Definition: AgoraOptional.h:285
CONSTEXPR MoveConstructible()
Definition: AgoraOptional.h:284
Definition: AgoraOptional.h:280
Definition: AgoraOptional.h:57
char empty_
Definition: AgoraOptional.h:105
CONSTEXPR OptionalStorageBase(in_place_t, const T &_value)
Definition: AgoraOptional.h:67
~OptionalStorageBase()
Definition: AgoraOptional.h:81
void Init(const T &_value)
Definition: AgoraOptional.h:93
bool is_populated_
Definition: AgoraOptional.h:99
CONSTEXPR OptionalStorageBase()
Definition: AgoraOptional.h:60
T value_
Definition: AgoraOptional.h:106
Definition: AgoraOptional.h:118
CONSTEXPR OptionalStorage(in_place_t in_place, const T &_value)
Definition: AgoraOptional.h:136
OptionalStorage(const OptionalStorage &other)
Definition: AgoraOptional.h:144
OptionalStorage()
Definition: AgoraOptional.h:142
Definition: AgoraOptional.h:38
CONSTEXPR nullopt_t(int)
Definition: AgoraOptional.h:39