34 #include <initializer_list> 37 #include <type_traits> 40 #define TR2_OPTIONAL_REQUIRES(...) \ 41 typename std::enable_if<__VA_ARGS__::value, bool>::type = false 64 return static_cast<T&&
>(t);
70 static_assert(!std::is_lvalue_reference<T>::value,
"!!");
71 return static_cast<T&&
>(t);
81 #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR) 83 #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) \ 84 ((CHECK) ? (EXPR) : ([] { assert(!#CHECK); }(), (EXPR))) 87 #if defined(__CUDA_ARCH__) 88 #define TR2_OPTIONAL_HOST_CONSTEXPR 90 #define TR2_OPTIONAL_HOST_CONSTEXPR constexpr 96 #if (defined _MSC_VER) 99 return std::addressof(ref);
103 template <
typename T>
110 template <
class X,
size_t S = sizeof(std::declval<X&>().operator&())>
115 constexpr
static bool value = has_overload<T>(
true);
118 template <
typename T, TR2_OPTIONAL_REQUIRES(!has_overloaded_addressof<T>)>
123 template <
typename T, TR2_OPTIONAL_REQUIRES(has_overloaded_addressof<T>)>
125 return std::addressof(ref);
152 : logic_error{what_arg} {}
154 : logic_error{what_arg} {}
164 template <
class... Args>
178 template <
class... Args>
197 template <
class... Args>
204 std::is_constructible<T, std::initializer_list<U>>)>
206 std::initializer_list<U> il,
208 : init_(true), storage_(il,
std::forward<Args>(args)...) {}
211 if (init_) storage_.
value_.T::~T();
224 : init_(true), storage_(v) {}
229 template <
class... Args>
236 std::is_constructible<T, std::initializer_list<U>>)>
238 std::initializer_list<U> il,
240 : init_(true), storage_(il,
std::forward<Args>(args)...) {}
247 std::is_trivially_destructible<T>::value,
256 std::is_trivially_destructible<U>::value,
267 constexpr
bool initialized()
const noexcept {
273 constexpr
const T* dataptr()
const {
277 constexpr
const T& contained_val()
const& {
280 constexpr T&& contained_val() && {
285 void clear() noexcept {
286 if (initialized()) dataptr()->~T();
290 template <
class... Args>
291 void initialize(Args&&... args) noexcept(
292 noexcept(T(std::forward<Args>(args)...))) {
294 ::new (static_cast<void*>(dataptr())) T(std::forward<Args>(args)...);
298 template <
class U,
class... Args>
299 void initialize(std::initializer_list<U> il, Args&&... args) noexcept(
300 noexcept(T(il, std::forward<Args>(args)...))) {
302 ::new (static_cast<void*>(dataptr()))
303 T(il, std::forward<Args>(args)...);
311 constexpr
optional() noexcept : OptionalBase<T>(){};
315 if (rhs.initialized()) {
316 ::new (static_cast<void*>(dataptr())) T(*rhs);
317 OptionalBase<T>::init_ =
true;
322 std::is_nothrow_move_constructible<T>::value)
323 : OptionalBase<T>() {
324 if (rhs.initialized()) {
325 ::new (static_cast<void*>(dataptr())) T(std::move(*rhs));
326 OptionalBase<T>::init_ =
true;
336 template <
typename U = T,
342 std::is_convertible<U&&, T>)>
343 constexpr
optional(U&& u) : OptionalBase<T>(
std::forward<U>(u)) {}
346 template <
typename U = T,
352 !std::is_convertible<U&&, T>)>
353 explicit constexpr
optional(U&& u) : OptionalBase<T>(
std::forward<U>(u)) {}
355 template <
class... Args>
357 : OptionalBase<T>(
in_place_t{}, constexpr_forward<Args>(args)...) {}
362 std::is_constructible<T, std::initializer_list<U>>)>
364 std::initializer_list<U> il,
366 : OptionalBase<T>(
in_place_t{}, il, constexpr_forward<Args>(args)...) {}
378 if (initialized() ==
true && rhs.initialized() ==
false)
380 else if (initialized() ==
false && rhs.initialized() ==
true)
382 else if (initialized() ==
true && rhs.initialized() ==
true)
383 contained_val() = *rhs;
388 std::is_nothrow_move_assignable<T>::value&&
389 std::is_nothrow_move_constructible<T>::value) {
390 if (initialized() ==
true && rhs.initialized() ==
false)
392 else if (initialized() ==
false && rhs.initialized() ==
true)
393 initialize(std::move(*rhs));
394 else if (initialized() ==
true && rhs.initialized() ==
true)
395 contained_val() = std::move(*rhs);
399 template <
class U = T>
401 std::is_constructible<T, U>::value &&
404 (std::is_scalar<T>::value ||
406 std::is_assignable<T&, U>::value,
409 contained_val() = std::forward<U>(v);
411 initialize(std::forward<U>(v));
416 template <
class... Args>
419 initialize(std::forward<Args>(args)...);
422 template <
class U,
class... Args>
423 void emplace(std::initializer_list<U> il, Args&&... args) {
425 initialize<U, Args...>(il, std::forward<Args>(args)...);
430 std::is_nothrow_move_constructible<T>::value&& noexcept(
431 std::swap(std::declval<T&>(), std::declval<T&>()))) {
432 if (initialized() ==
true && rhs.initialized() ==
false) {
433 rhs.initialize(std::move(**
this));
435 }
else if (initialized() ==
false && rhs.initialized() ==
true) {
436 initialize(std::move(*rhs));
438 }
else if (initialized() ==
true && rhs.initialized() ==
true) {
446 explicit constexpr
operator bool() const noexcept {
return initialized(); }
447 constexpr
bool has_value() const noexcept {
return initialized(); }
454 assert(initialized());
463 assert(initialized());
464 return contained_val();
468 assert(initialized());
488 return std::move(contained_val());
493 return *
this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
500 : detail_::convert<T>(constexpr_forward<V>(v));
519 static_assert(
sizeof(T) == 0,
520 "optional references is ill-formed, \ 521 consider use optional of a std::reference_wrapper of type T to \ 522 hold a reference if you really need to");
524 static_assert(!std::is_same<T, nullopt_t>::value,
"bad T");
525 static_assert(!std::is_same<T, in_place_t>::value,
"bad T");
534 template <
typename U = T>
537 template <
typename U = T>
565 template <
typename U>
566 auto operator=(U&& rhs) noexcept ->
typename std::enable_if<
573 template <
typename U>
574 auto operator=(U&& rhs) noexcept ->
typename std::enable_if<
575 !std::is_same<typename std::decay<U>::type,
optional<T&>>::value,
580 void emplace(T&&) =
delete;
598 explicit constexpr
operator bool() const noexcept {
return ref !=
nullptr; }
600 constexpr
bool has_value() const noexcept {
return ref !=
nullptr; }
604 return *
this ? **this
606 constexpr_forward<V>(v));
610 void reset() noexcept { ref =
nullptr; }
615 static_assert(
sizeof(T) == 0,
"optional rvalue references disallowed");
621 return bool(x) != bool(y) ? false : bool(x) ==
false ? true : *x == *y;
630 constexpr
bool operator<(const optional<T>& x,
const optional<T>& y) {
631 return (!y) ? false : (!x) ?
true : *x < *y;
640 constexpr
bool operator<=(const optional<T>& x,
const optional<T>& y) {
671 constexpr
bool operator<(const optional<T>&,
nullopt_t) noexcept {
676 constexpr
bool operator<(nullopt_t, const optional<T>& x) noexcept {
681 constexpr
bool operator<=(const optional<T>& x,
nullopt_t) noexcept {
686 constexpr
bool operator<=(nullopt_t, const optional<T>&) noexcept {
713 return bool(x) ? *x == v :
false;
718 return bool(x) ? v == *x :
false;
723 return bool(x) ? *x != v :
true;
728 return bool(x) ? v != *x :
true;
732 constexpr
bool operator<(const optional<T>& x,
const T& v) {
733 return bool(x) ? *x < v :
true;
738 return bool(x) ? v > *x :
true;
743 return bool(x) ? *x > v :
false;
747 constexpr
bool operator<(const T& v, const optional<T>& x) {
748 return bool(x) ? v < *x :
false;
753 return bool(x) ? *x >= v :
false;
757 constexpr
bool operator<=(const T& v, const optional<T>& x) {
758 return bool(x) ? v <= *x :
false;
762 constexpr
bool operator<=(const optional<T>& x,
const T& v) {
763 return bool(x) ? *x <= v :
true;
768 return bool(x) ? v >= *x :
true;
774 return bool(x) ? *x == v :
false;
779 return bool(x) ? v == *x :
false;
784 return bool(x) ? *x != v :
true;
789 return bool(x) ? v != *x :
true;
793 constexpr
bool operator<(const optional<T&>& x,
const T& v) {
794 return bool(x) ? *x < v :
true;
799 return bool(x) ? v > *x :
true;
804 return bool(x) ? *x > v :
false;
808 constexpr
bool operator<(const T& v, const optional<T&>& x) {
809 return bool(x) ? v < *x :
false;
814 return bool(x) ? *x >= v :
false;
818 constexpr
bool operator<=(const T& v, const optional<T&>& x) {
819 return bool(x) ? v <= *x :
false;
823 constexpr
bool operator<=(const optional<T&>& x,
const T& v) {
824 return bool(x) ? *x <= v :
true;
829 return bool(x) ? v >= *x :
true;
835 return bool(x) ? *x == v :
false;
840 return bool(x) ? v == *x :
false;
845 return bool(x) ? *x != v :
true;
850 return bool(x) ? v != *x :
true;
854 constexpr
bool operator<(const optional<const T&>& x,
const T& v) {
855 return bool(x) ? *x < v :
true;
860 return bool(x) ? v > *x :
true;
865 return bool(x) ? *x > v :
false;
869 constexpr
bool operator<(const T& v, const optional<const T&>& x) {
870 return bool(x) ? v < *x :
false;
875 return bool(x) ? *x >= v :
false;
879 constexpr
bool operator<=(const T& v, const optional<const T&>& x) {
880 return bool(x) ? v <= *x :
false;
884 constexpr
bool operator<=(const optional<const T&>& x,
const T& v) {
885 return bool(x) ? *x <= v :
true;
890 return bool(x) ? v >= *x :
true;
913 template <
typename T>
914 struct hash<
open3d::utility::optional<T>> {
918 constexpr result_type
operator()(argument_type
const& arg)
const {
919 return arg ? std::hash<T>{}(*arg) : result_type{};
923 template <
typename T>
924 struct hash<
open3d::utility::optional<T&>> {
928 constexpr result_type
operator()(argument_type
const& arg)
const {
929 return arg ? std::hash<T>{}(*arg) : result_type{};
934 #undef TR2_OPTIONAL_REQUIRES 935 #undef TR2_OPTIONAL_ASSERTED_EXPRESSION 936 #undef TR2_OPTIONAL_HOST_CONSTEXPR constexpr bool operator==(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:620
constexpr result_type operator()(argument_type const &arg) const
Definition: Optional.h:928
TR2_OPTIONAL_HOST_CONSTEXPR T & operator*() const
Definition: Optional.h:589
constexpr storage_t(Args &&... args)
Definition: Optional.h:165
constexpr bool operator>=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:645
optional & operator=(nullopt_t) noexcept
Definition: Optional.h:550
constexpr in_place_t in_place
Definition: Optional.h:50
constexpr_storage_t< T > storage_
Definition: Optional.h:218
constexpr nullopt_t nullopt
Definition: Optional.h:146
void reset() noexcept
Definition: Optional.h:504
constexpr optional(U &&u)
Definition: Optional.h:343
constexpr std::decay< T >::type value_or(V &&v) const
Definition: Optional.h:603
void swap(optional< T &> &rhs) noexcept
Definition: Optional.h:582
constexpr constexpr_optional_base(const T &v)
Definition: Optional.h:223
constexpr T && constexpr_forward(typename std::remove_reference< T >::type &t) noexcept
Definition: Optional.h:62
bad_optional_access(const char *what_arg)
Definition: Optional.h:153
#define TR2_OPTIONAL_HOST_CONSTEXPR
Definition: Optional.h:90
hash< T >::result_type result_type
Definition: Optional.h:915
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:429
constexpr result_type operator()(argument_type const &arg) const
Definition: Optional.h:918
unsigned char dummy_
Definition: Optional.h:159
Definition: Optional.h:143
constexpr constexpr_optional_base(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:237
static constexpr bool has_overload(...)
Definition: Optional.h:106
Definition: Optional.h:912
TR2_OPTIONAL_HOST_CONSTEXPR T const & operator*() const &
Definition: Optional.h:458
constexpr constexpr_optional_base(T &&v)
Definition: Optional.h:226
storage_t< T > storage_
Definition: Optional.h:188
TR2_OPTIONAL_HOST_CONSTEXPR T && operator*() &&
Definition: Optional.h:467
Definition: Optional.h:104
T value_
Definition: Optional.h:160
constexpr bool operator!=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:625
optional_base(in_place_t, Args &&... args)
Definition: Optional.h:198
constexpr struct open3d::utility::trivial_init_t trivial_init
T value_type
Definition: Optional.h:308
constexpr optional(const optional &rhs) noexcept
Definition: Optional.h:540
unsigned char dummy_
Definition: Optional.h:173
void reset() noexcept
Definition: Optional.h:610
constexpr constexpr_storage_t(trivial_init_t) noexcept
Definition: Optional.h:176
optional & operator=(const optional &rhs)
Definition: Optional.h:377
open3d::utility::optional< T > argument_type
Definition: Optional.h:916
constexpr constexpr_optional_base(in_place_t, Args &&... args)
Definition: Optional.h:230
TR2_OPTIONAL_HOST_CONSTEXPR T && value() &&
Definition: Optional.h:486
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:400
optional & operator=(optional &&rhs) noexcept(std::is_nothrow_move_assignable< T >::value &&std::is_nothrow_move_constructible< T >::value)
Definition: Optional.h:387
void emplace(Args &&... args)
Definition: Optional.h:417
constexpr optional_base() noexcept
Definition: Optional.h:190
hash< T >::result_type result_type
Definition: Optional.h:925
TR2_OPTIONAL_HOST_CONSTEXPR T & value() &
Definition: Optional.h:479
constexpr bool has_value() const noexcept
Definition: Optional.h:447
static constexpr bool has_overload(bool)
Definition: Optional.h:111
constexpr T * static_addressof(T &ref)
Definition: Optional.h:119
Definition: Optional.h:142
constexpr optional(nullopt_t) noexcept
Definition: Optional.h:532
void swap(optional< T > &x, optional< T > &y) noexcept(noexcept(x.swap(y)))
Definition: Optional.h:895
Definition: Optional.h:149
constexpr T value_or(V &&v) const &
Definition: Optional.h:492
open3d::utility::optional< T & > argument_type
Definition: Optional.h:926
TR2_OPTIONAL_HOST_CONSTEXPR T const & value() const &
Definition: Optional.h:472
~optional_base()
Definition: Optional.h:210
bad_optional_access(const std::string &what_arg)
Definition: Optional.h:151
constexpr optional() noexcept
Definition: Optional.h:311
char type
Definition: FilePCD.cpp:60
void emplace(T &v) noexcept
Definition: Optional.h:578
constexpr bool operator>(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:635
Definition: Optional.h:54
~storage_t()
Definition: Optional.h:168
constexpr optional(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:363
Definition: Optional.h:172
#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR)
Definition: Optional.h:83
constexpr optional(in_place_t, T &v) noexcept
Definition: Optional.h:542
Definition: PinholeCameraIntrinsic.cpp:35
constexpr optional_base(T &&v)
Definition: Optional.h:194
Definition: Optional.h:516
constexpr constexpr_optional_base() noexcept
Definition: Optional.h:220
optional_base(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:205
constexpr optional(nullopt_t) noexcept
Definition: Optional.h:312
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
Definition: Optional.h:900
optional & operator=(nullopt_t) noexcept
Definition: Optional.h:372
T value_
Definition: Optional.h:174
constexpr U convert(U v)
Definition: Optional.h:132
optional(const optional &rhs)
Definition: Optional.h:314
Definition: Optional.h:138
Definition: Optional.h:46
constexpr nullopt_t(init)
Definition: Optional.h:144
constexpr constexpr_storage_t(Args &&... args)
Definition: Optional.h:179
TR2_OPTIONAL_HOST_CONSTEXPR T * operator->() const
Definition: Optional.h:585
Definition: Optional.h:186
TR2_OPTIONAL_HOST_CONSTEXPR T * operator->()
Definition: Optional.h:453
T * static_addressof(T &ref)
Definition: Optional.h:124
constexpr optional_base(const T &v)
Definition: Optional.h:192
TR2_OPTIONAL_HOST_CONSTEXPR T const * operator->() const
Definition: Optional.h:449
Definition: Optional.h:216
#define TR2_OPTIONAL_REQUIRES(...)
Definition: Optional.h:40
bool init_
Definition: Optional.h:187
constexpr bool has_value() const noexcept
Definition: Optional.h:600
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:250
constexpr T value_or(V &&v) &&
Definition: Optional.h:497
constexpr optional(in_place_t, Args &&... args)
Definition: Optional.h:356
constexpr optional() noexcept
Definition: Optional.h:530
Definition: Optional.h:158
void emplace(std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:423
bool init_
Definition: Optional.h:217
constexpr optional(U &u) noexcept
Definition: Optional.h:535
TR2_OPTIONAL_HOST_CONSTEXPR T & operator*() &
Definition: Optional.h:462
optional(optional &&rhs) noexcept(std::is_nothrow_move_constructible< T >::value)
Definition: Optional.h:321
constexpr T & value() const
Definition: Optional.h:593
constexpr storage_t(trivial_init_t) noexcept
Definition: Optional.h:162
constexpr std::remove_reference< T >::type && constexpr_move(T &&t) noexcept
Definition: Optional.h:75
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:566