40 #include <initializer_list>
43 #include <type_traits>
46 #define TR2_OPTIONAL_REQUIRES(...) \
47 typename std::enable_if<__VA_ARGS__::value, bool>::type = false
70 return static_cast<T&&
>(t);
76 static_assert(!std::is_lvalue_reference<T>::value,
"!!");
77 return static_cast<T&&
>(t);
87 #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR)
89 #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) \
90 ((CHECK) ? (EXPR) : ([] { assert(!#CHECK); }(), (EXPR)))
93 #if defined(__CUDA_ARCH__)
94 #define TR2_OPTIONAL_HOST_CONSTEXPR
96 #define TR2_OPTIONAL_HOST_CONSTEXPR constexpr
102 #if (defined _MSC_VER)
103 template <
typename T>
105 return std::addressof(ref);
109 template <
typename T>
116 template <class X, size_t S = sizeof(std::declval<X&>().operator&())>
121 constexpr
static bool value = has_overload<T>(
true);
124 template <
typename T, TR2_OPTIONAL_REQUIRES(!has_overloaded_addressof<T>)>
129 template <
typename T, TR2_OPTIONAL_REQUIRES(has_overloaded_addressof<T>)>
131 return std::addressof(ref);
158 : logic_error{what_arg} {}
160 : logic_error{what_arg} {}
170 template <
class... Args>
184 template <
class... Args>
203 template <
class... Args>
210 std::is_constructible<T, std::initializer_list<U>>)>
212 std::initializer_list<U> il,
235 template <
class... Args>
242 std::is_constructible<T, std::initializer_list<U>>)>
244 std::initializer_list<U> il,
253 std::is_trivially_destructible<T>::value,
261 using OptionalBase =
typename std::conditional<
262 std::is_trivially_destructible<U>::value,
273 constexpr
bool initialized()
const noexcept {
274 return OptionalBase<T>::init_;
277 return std::addressof(OptionalBase<T>::storage_.value_);
279 constexpr
const T* dataptr()
const {
283 constexpr
const T& contained_val()
const& {
284 return OptionalBase<T>::storage_.value_;
286 constexpr T&& contained_val() && {
287 return std::move(OptionalBase<T>::storage_.value_);
289 constexpr T& contained_val() & {
return OptionalBase<T>::storage_.value_; }
291 void clear() noexcept {
292 if (initialized()) dataptr()->~T();
293 OptionalBase<T>::init_ =
false;
296 template <
class... Args>
297 void initialize(Args&&... args) noexcept(
298 noexcept(T(std::forward<Args>(args)...))) {
299 assert(!OptionalBase<T>::init_);
300 ::new (
static_cast<void*
>(dataptr())) T(std::forward<Args>(args)...);
301 OptionalBase<T>::init_ =
true;
304 template <
class U,
class... Args>
305 void initialize(std::initializer_list<U> il, Args&&... args) noexcept(
306 noexcept(T(il, std::forward<Args>(args)...))) {
307 assert(!OptionalBase<T>::init_);
308 ::new (
static_cast<void*
>(dataptr()))
309 T(il, std::forward<Args>(args)...);
310 OptionalBase<T>::init_ =
true;
317 constexpr
optional() noexcept : OptionalBase<T>(){};
321 if (rhs.initialized()) {
322 ::new (
static_cast<void*
>(dataptr())) T(*rhs);
323 OptionalBase<T>::init_ =
true;
328 std::is_nothrow_move_constructible<T>::value)
329 : OptionalBase<T>() {
330 if (rhs.initialized()) {
331 ::new (
static_cast<void*
>(dataptr())) T(std::move(*rhs));
332 OptionalBase<T>::init_ =
true;
342 template <
typename U = T,
348 std::is_convertible<U&&, T>)>
349 constexpr
optional(U&& u) : OptionalBase<T>(
std::forward<U>(u)) {}
352 template <
typename U = T,
358 !std::is_convertible<U&&, T>)>
359 explicit constexpr
optional(U&& u) : OptionalBase<T>(
std::forward<U>(u)) {}
361 template <
class... Args>
368 std::is_constructible<T, std::initializer_list<U>>)>
370 std::initializer_list<U> il,
384 if (initialized() ==
true && rhs.initialized() ==
false)
386 else if (initialized() ==
false && rhs.initialized() ==
true)
388 else if (initialized() ==
true && rhs.initialized() ==
true)
389 contained_val() = *rhs;
394 std::is_nothrow_move_assignable<T>::value&&
395 std::is_nothrow_move_constructible<T>::value) {
396 if (initialized() ==
true && rhs.initialized() ==
false)
398 else if (initialized() ==
false && rhs.initialized() ==
true)
399 initialize(std::move(*rhs));
400 else if (initialized() ==
true && rhs.initialized() ==
true)
401 contained_val() = std::move(*rhs);
405 template <
class U = T>
407 std::is_constructible<T, U>::value &&
410 (std::is_scalar<T>::value ||
412 std::is_assignable<T&, U>::value,
415 contained_val() = std::forward<U>(v);
417 initialize(std::forward<U>(v));
422 template <
class... Args>
425 initialize(std::forward<Args>(args)...);
428 template <
class U,
class... Args>
429 void emplace(std::initializer_list<U> il, Args&&... args) {
431 initialize<U, Args...>(il, std::forward<Args>(args)...);
436 std::is_nothrow_move_constructible<T>::value&& noexcept(
437 std::swap(std::declval<T&>(), std::declval<T&>()))) {
438 if (initialized() ==
true && rhs.initialized() ==
false) {
439 rhs.initialize(std::move(**
this));
441 }
else if (initialized() ==
false && rhs.initialized() ==
true) {
442 initialize(std::move(*rhs));
444 }
else if (initialized() ==
true && rhs.initialized() ==
true) {
452 explicit constexpr
operator bool() const noexcept {
return initialized(); }
453 constexpr
bool has_value() const noexcept {
return initialized(); }
460 assert(initialized());
469 assert(initialized());
470 return contained_val();
474 assert(initialized());
494 return std::move(contained_val());
499 return *
this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
506 : detail_::convert<T>(constexpr_forward<V>(v));
525 static_assert(
sizeof(T) == 0,
526 "optional references is ill-formed, \
527 consider use optional of a std::reference_wrapper of type T to \
528 hold a reference if you really need to");
530 static_assert(!std::is_same<T, nullopt_t>::value,
"bad T");
531 static_assert(!std::is_same<T, in_place_t>::value,
"bad T");
540 template <
typename U = T>
543 template <
typename U = T>
571 template <
typename U>
572 auto operator=(U&& rhs) noexcept ->
typename std::enable_if<
579 template <
typename U>
580 auto operator=(U&& rhs) noexcept ->
typename std::enable_if<
604 explicit constexpr
operator bool() const noexcept {
return ref !=
nullptr; }
606 constexpr
bool has_value() const noexcept {
return ref !=
nullptr; }
610 return *
this ? **this
612 constexpr_forward<V>(v));
616 void reset() noexcept { ref =
nullptr; }
621 static_assert(
sizeof(T) == 0,
"optional rvalue references disallowed");
627 return bool(x) != bool(y) ? false : bool(x) ==
false ? true : *x == *y;
637 return (!y) ? false : (!x) ?
true : *x < *y;
719 return bool(x) ? *x == v :
false;
724 return bool(x) ? v == *x :
false;
729 return bool(x) ? *x != v :
true;
734 return bool(x) ? v != *x :
true;
739 return bool(x) ? *x < v :
true;
744 return bool(x) ? v > *x :
true;
749 return bool(x) ? *x > v :
false;
754 return bool(x) ? v < *x :
false;
759 return bool(x) ? *x >= v :
false;
764 return bool(x) ? v <= *x :
false;
769 return bool(x) ? *x <= v :
true;
774 return bool(x) ? v >= *x :
true;
780 return bool(x) ? *x == v :
false;
785 return bool(x) ? v == *x :
false;
790 return bool(x) ? *x != v :
true;
795 return bool(x) ? v != *x :
true;
800 return bool(x) ? *x < v :
true;
805 return bool(x) ? v > *x :
true;
810 return bool(x) ? *x > v :
false;
815 return bool(x) ? v < *x :
false;
820 return bool(x) ? *x >= v :
false;
825 return bool(x) ? v <= *x :
false;
830 return bool(x) ? *x <= v :
true;
835 return bool(x) ? v >= *x :
true;
841 return bool(x) ? *x == v :
false;
846 return bool(x) ? v == *x :
false;
851 return bool(x) ? *x != v :
true;
856 return bool(x) ? v != *x :
true;
861 return bool(x) ? *x < v :
true;
866 return bool(x) ? v > *x :
true;
871 return bool(x) ? *x > v :
false;
876 return bool(x) ? v < *x :
false;
881 return bool(x) ? *x >= v :
false;
886 return bool(x) ? v <= *x :
false;
891 return bool(x) ? *x <= v :
true;
896 return bool(x) ? v >= *x :
true;
919 template <
typename T>
920 struct hash<
open3d::utility::optional<T>> {
929 template <
typename T>
930 struct hash<
open3d::utility::optional<T&>> {
940 #undef TR2_OPTIONAL_REQUIRES
941 #undef TR2_OPTIONAL_ASSERTED_EXPRESSION
942 #undef TR2_OPTIONAL_HOST_CONSTEXPR
#define TR2_OPTIONAL_REQUIRES(...)
Definition: Optional.h:46
#define TR2_OPTIONAL_HOST_CONSTEXPR
Definition: Optional.h:96
#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR)
Definition: Optional.h:89
Definition: Optional.h:155
bad_optional_access(const char *what_arg)
Definition: Optional.h:159
bad_optional_access(const std::string &what_arg)
Definition: Optional.h:157
Definition: Optional.h:522
constexpr T & value() const
Definition: Optional.h:599
constexpr optional(U &u) noexcept
Definition: Optional.h:541
constexpr optional() noexcept
Definition: Optional.h:536
auto operator=(U &&rhs) noexcept -> typename std::enable_if< std::is_same< typename std::decay< U >::type, optional< T & >>::value, optional & >::type
Definition: Optional.h:572
TR2_OPTIONAL_HOST_CONSTEXPR T * operator->() const
Definition: Optional.h:591
TR2_OPTIONAL_HOST_CONSTEXPR T & operator*() const
Definition: Optional.h:595
void reset() noexcept
Definition: Optional.h:616
constexpr optional(nullopt_t) noexcept
Definition: Optional.h:538
void emplace(T &v) noexcept
Definition: Optional.h:584
constexpr std::decay< T >::type value_or(V &&v) const
Definition: Optional.h:609
auto operator=(U &&rhs) noexcept -> typename std::enable_if< !std::is_same< typename std::decay< U >::type, optional< T & >>::value, optional & >::type=delete
optional(in_place_t, T &&)=delete
void emplace(T &&)=delete
void swap(optional< T & > &rhs) noexcept
Definition: Optional.h:588
constexpr optional(in_place_t, T &v) noexcept
Definition: Optional.h:548
constexpr bool has_value() const noexcept
Definition: Optional.h:606
optional & operator=(nullopt_t) noexcept
Definition: Optional.h:556
constexpr optional(const optional &rhs) noexcept
Definition: Optional.h:546
Definition: Optional.h:259
constexpr bool has_value() const noexcept
Definition: Optional.h:453
optional & operator=(const optional &rhs)
Definition: Optional.h:383
optional(const optional &rhs)
Definition: Optional.h:320
TR2_OPTIONAL_HOST_CONSTEXPR T && operator*() &&
Definition: Optional.h:473
optional & operator=(nullopt_t) noexcept
Definition: Optional.h:378
T value_type
Definition: Optional.h:314
TR2_OPTIONAL_HOST_CONSTEXPR T const & value() const &
Definition: Optional.h:478
TR2_OPTIONAL_HOST_CONSTEXPR T & operator*() &
Definition: Optional.h:468
constexpr T value_or(V &&v) const &
Definition: Optional.h:498
constexpr optional() noexcept
Definition: Optional.h:317
optional & operator=(optional &&rhs) noexcept(std::is_nothrow_move_assignable< T >::value &&std::is_nothrow_move_constructible< T >::value)
Definition: Optional.h:393
TR2_OPTIONAL_HOST_CONSTEXPR T const & operator*() const &
Definition: Optional.h:464
TR2_OPTIONAL_HOST_CONSTEXPR T & value() &
Definition: Optional.h:485
void emplace(std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:429
constexpr optional(U &&u)
Definition: Optional.h:349
constexpr T value_or(V &&v) &&
Definition: Optional.h:503
void emplace(Args &&... args)
Definition: Optional.h:423
auto operator=(U &&v) -> typename std::enable_if< std::is_constructible< T, U >::value &&!std::is_same< typename std::decay< U >::type, optional< T >>::value &&(std::is_scalar< T >::value||std::is_same< typename std::decay< U >::type, T >::value) &&std::is_assignable< T &, U >::value, optional & >::type
Definition: Optional.h:406
void reset() noexcept
Definition: Optional.h:510
TR2_OPTIONAL_HOST_CONSTEXPR T * operator->()
Definition: Optional.h:459
constexpr optional(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:369
optional(optional &&rhs) noexcept(std::is_nothrow_move_constructible< T >::value)
Definition: Optional.h:327
constexpr optional(in_place_t, Args &&... args)
Definition: Optional.h:362
void swap(optional< T > &rhs) noexcept(std::is_nothrow_move_constructible< T >::value &&noexcept(std::swap(std::declval< T & >(), std::declval< T & >())))
Definition: Optional.h:435
TR2_OPTIONAL_HOST_CONSTEXPR T const * operator->() const
Definition: Optional.h:455
TR2_OPTIONAL_HOST_CONSTEXPR T && value() &&
Definition: Optional.h:492
constexpr optional(nullopt_t) noexcept
Definition: Optional.h:318
constexpr U convert(U v)
Definition: Optional.h:138
constexpr T * static_addressof(T &ref)
Definition: Optional.h:125
constexpr in_place_t in_place
Definition: Optional.h:56
constexpr struct open3d::utility::trivial_init_t trivial_init
constexpr bool operator<(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:636
constexpr bool operator>=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:651
constexpr std::remove_reference< T >::type && constexpr_move(T &&t) noexcept
Definition: Optional.h:81
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
Definition: Optional.h:906
constexpr bool operator==(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:626
typename std::conditional< std::is_trivially_destructible< T >::value, constexpr_optional_base< typename std::remove_const< T >::type >, optional_base< typename std::remove_const< T >::type > >::type OptionalBase
Definition: Optional.h:256
void swap(optional< T > &x, optional< T > &y) noexcept(noexcept(x.swap(y)))
Definition: Optional.h:901
constexpr bool operator<=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:646
constexpr bool operator>(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:641
constexpr nullopt_t nullopt
Definition: Optional.h:152
constexpr bool operator!=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:631
constexpr T && constexpr_forward(typename std::remove_reference< T >::type &t) noexcept
Definition: Optional.h:68
Definition: PinholeCameraIntrinsic.cpp:16
void swap(open3d::core::SmallVectorImpl< T > &LHS, open3d::core::SmallVectorImpl< T > &RHS)
Implement std::swap in terms of SmallVector swap.
Definition: SmallVector.h:1370
Definition: Optional.h:222
constexpr constexpr_optional_base() noexcept
Definition: Optional.h:226
constexpr_storage_t< T > storage_
Definition: Optional.h:224
constexpr constexpr_optional_base(in_place_t, Args &&... args)
Definition: Optional.h:236
constexpr constexpr_optional_base(T &&v)
Definition: Optional.h:232
bool init_
Definition: Optional.h:223
constexpr constexpr_optional_base(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:243
constexpr constexpr_optional_base(const T &v)
Definition: Optional.h:229
~constexpr_optional_base()=default
Definition: Optional.h:110
constexpr static bool value
Definition: Optional.h:121
constexpr static bool has_overload(bool)
Definition: Optional.h:117
constexpr static bool has_overload(...)
Definition: Optional.h:112
Definition: Optional.h:52
Definition: Optional.h:149
Definition: Optional.h:148
constexpr nullopt_t(init)
Definition: Optional.h:150
Definition: Optional.h:192
~optional_base()
Definition: Optional.h:216
bool init_
Definition: Optional.h:193
optional_base(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:211
storage_t< T > storage_
Definition: Optional.h:194
optional_base(in_place_t, Args &&... args)
Definition: Optional.h:204
constexpr optional_base(const T &v)
Definition: Optional.h:198
constexpr optional_base() noexcept
Definition: Optional.h:196
constexpr optional_base(T &&v)
Definition: Optional.h:200
Definition: Optional.h:144
open3d::utility::optional< T > argument_type
Definition: Optional.h:922
constexpr result_type operator()(argument_type const &arg) const
Definition: Optional.h:924
hash< T >::result_type result_type
Definition: Optional.h:921
open3d::utility::optional< T & > argument_type
Definition: Optional.h:932
constexpr result_type operator()(argument_type const &arg) const
Definition: Optional.h:934
hash< T >::result_type result_type
Definition: Optional.h:931
Definition: Optional.h:178
T value_
Definition: Optional.h:180
constexpr constexpr_storage_t(trivial_init_t) noexcept
Definition: Optional.h:182
unsigned char dummy_
Definition: Optional.h:179
~constexpr_storage_t()=default
constexpr constexpr_storage_t(Args &&... args)
Definition: Optional.h:185
Definition: Optional.h:164
constexpr storage_t(Args &&... args)
Definition: Optional.h:171
unsigned char dummy_
Definition: Optional.h:165
constexpr storage_t(trivial_init_t) noexcept
Definition: Optional.h:168
~storage_t()
Definition: Optional.h:174
T value_
Definition: Optional.h:166