34 #include <initializer_list> 37 #include <type_traits> 40 #define TR2_OPTIONAL_REQUIRES(...) \ 41 typename std::enable_if<__VA_ARGS__::value, bool>::type = false 50 template <std::
size_t I>
74 return static_cast<T&&
>(t);
80 static_assert(!std::is_lvalue_reference<T>::value,
"!!");
81 return static_cast<T&&
>(t);
91 #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) (EXPR) 93 #define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR) \ 94 ((CHECK) ? (EXPR) : ([] { assert(!#CHECK); }(), (EXPR))) 97 #if defined(__CUDA_ARCH__) 98 #define TR2_OPTIONAL_HOST_CONSTEXPR 100 #define TR2_OPTIONAL_HOST_CONSTEXPR constexpr 106 #if (defined _MSC_VER) 107 template <
typename T>
109 return std::addressof(ref);
113 template <
typename T>
120 template <
class X,
size_t S = sizeof(std::declval<X&>().operator&())>
125 constexpr
static bool value = has_overload<T>(
true);
128 template <
typename T, TR2_OPTIONAL_REQUIRES(!has_overloaded_addressof<T>)>
133 template <
typename T, TR2_OPTIONAL_REQUIRES(has_overloaded_addressof<T>)>
135 return std::addressof(ref);
162 : logic_error{what_arg} {}
164 : logic_error{what_arg} {}
174 template <
class... Args>
188 template <
class... Args>
207 template <
class... Args>
214 std::is_constructible<T, std::initializer_list<U>>)>
216 std::initializer_list<U> il,
218 : init_(true), storage_(il,
std::forward<Args>(args)...) {}
221 if (init_) storage_.
value_.T::~T();
234 : init_(true), storage_(v) {}
239 template <
class... Args>
246 std::is_constructible<T, std::initializer_list<U>>)>
248 std::initializer_list<U> il,
250 : init_(true), storage_(il,
std::forward<Args>(args)...) {}
257 std::is_trivially_destructible<T>::value,
266 std::is_trivially_destructible<U>::value,
277 constexpr
bool initialized()
const noexcept {
283 constexpr
const T* dataptr()
const {
287 constexpr
const T& contained_val()
const& {
290 constexpr T&& contained_val() && {
295 void clear() noexcept {
296 if (initialized()) dataptr()->~T();
300 template <
class... Args>
301 void initialize(Args&&... args) noexcept(
302 noexcept(T(std::forward<Args>(args)...))) {
304 ::new (static_cast<void*>(dataptr())) T(std::forward<Args>(args)...);
308 template <
class U,
class... Args>
309 void initialize(std::initializer_list<U> il, Args&&... args) noexcept(
310 noexcept(T(il, std::forward<Args>(args)...))) {
312 ::new (static_cast<void*>(dataptr()))
313 T(il, std::forward<Args>(args)...);
321 constexpr
optional() noexcept : OptionalBase<T>(){};
325 if (rhs.initialized()) {
326 ::new (static_cast<void*>(dataptr())) T(*rhs);
327 OptionalBase<T>::init_ =
true;
332 std::is_nothrow_move_constructible<T>::value)
333 : OptionalBase<T>() {
334 if (rhs.initialized()) {
335 ::new (static_cast<void*>(dataptr())) T(std::move(*rhs));
336 OptionalBase<T>::init_ =
true;
346 template <
typename U = T,
352 std::is_convertible<U&&, T>)>
353 constexpr
optional(U&& u) : OptionalBase<T>(
std::forward<U>(u)) {}
356 template <
typename U = T,
362 !std::is_convertible<U&&, T>)>
363 explicit constexpr
optional(U&& u) : OptionalBase<T>(
std::forward<U>(u)) {}
365 template <
class... Args>
367 : OptionalBase<T>(
in_place_t{}, constexpr_forward<Args>(args)...) {}
372 std::is_constructible<T, std::initializer_list<U>>)>
374 std::initializer_list<U> il,
376 : OptionalBase<T>(
in_place_t{}, il, constexpr_forward<Args>(args)...) {}
388 if (initialized() ==
true && rhs.initialized() ==
false)
390 else if (initialized() ==
false && rhs.initialized() ==
true)
392 else if (initialized() ==
true && rhs.initialized() ==
true)
393 contained_val() = *rhs;
398 std::is_nothrow_move_assignable<T>::value&&
399 std::is_nothrow_move_constructible<T>::value) {
400 if (initialized() ==
true && rhs.initialized() ==
false)
402 else if (initialized() ==
false && rhs.initialized() ==
true)
403 initialize(std::move(*rhs));
404 else if (initialized() ==
true && rhs.initialized() ==
true)
405 contained_val() = std::move(*rhs);
409 template <
class U = T>
411 std::is_constructible<T, U>::value &&
414 (std::is_scalar<T>::value ||
416 std::is_assignable<T&, U>::value,
419 contained_val() = std::forward<U>(v);
421 initialize(std::forward<U>(v));
426 template <
class... Args>
429 initialize(std::forward<Args>(args)...);
432 template <
class U,
class... Args>
433 void emplace(std::initializer_list<U> il, Args&&... args) {
435 initialize<U, Args...>(il, std::forward<Args>(args)...);
440 std::is_nothrow_move_constructible<T>::value&& noexcept(
441 std::swap(std::declval<T&>(), std::declval<T&>()))) {
442 if (initialized() ==
true && rhs.initialized() ==
false) {
443 rhs.initialize(std::move(**
this));
445 }
else if (initialized() ==
false && rhs.initialized() ==
true) {
446 initialize(std::move(*rhs));
448 }
else if (initialized() ==
true && rhs.initialized() ==
true) {
456 explicit constexpr
operator bool() const noexcept {
return initialized(); }
457 constexpr
bool has_value() const noexcept {
return initialized(); }
464 assert(initialized());
473 assert(initialized());
474 return contained_val();
478 assert(initialized());
498 return std::move(contained_val());
503 return *
this ? **this : detail_::convert<T>(constexpr_forward<V>(v));
510 : detail_::convert<T>(constexpr_forward<V>(v));
529 static_assert(
sizeof(T) == 0,
530 "optional references is ill-formed, \ 531 consider use optional of a std::reference_wrapper of type T to \ 532 hold a reference if you really need to");
534 static_assert(!std::is_same<T, nullopt_t>::value,
"bad T");
535 static_assert(!std::is_same<T, in_place_t>::value,
"bad T");
544 template <
typename U = T>
547 template <
typename U = T>
575 template <
typename U>
576 auto operator=(U&& rhs) noexcept ->
typename std::enable_if<
583 template <
typename U>
584 auto operator=(U&& rhs) noexcept ->
typename std::enable_if<
585 !std::is_same<typename std::decay<U>::type,
optional<T&>>::value,
590 void emplace(T&&) =
delete;
608 explicit constexpr
operator bool() const noexcept {
return ref !=
nullptr; }
610 constexpr
bool has_value() const noexcept {
return ref !=
nullptr; }
614 return *
this ? **this
616 constexpr_forward<V>(v));
620 void reset() noexcept { ref =
nullptr; }
625 static_assert(
sizeof(T) == 0,
"optional rvalue references disallowed");
631 return bool(x) != bool(y) ? false : bool(x) ==
false ? true : *x == *y;
640 constexpr
bool operator<(const optional<T>& x,
const optional<T>& y) {
641 return (!y) ? false : (!x) ?
true : *x < *y;
650 constexpr
bool operator<=(const optional<T>& x,
const optional<T>& y) {
681 constexpr
bool operator<(const optional<T>&,
nullopt_t) noexcept {
686 constexpr
bool operator<(nullopt_t, const optional<T>& x) noexcept {
691 constexpr
bool operator<=(const optional<T>& x,
nullopt_t) noexcept {
696 constexpr
bool operator<=(nullopt_t, const optional<T>&) noexcept {
723 return bool(x) ? *x == v :
false;
728 return bool(x) ? v == *x :
false;
733 return bool(x) ? *x != v :
true;
738 return bool(x) ? v != *x :
true;
742 constexpr
bool operator<(const optional<T>& x,
const T& v) {
743 return bool(x) ? *x < v :
true;
748 return bool(x) ? v > *x :
true;
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;
763 return bool(x) ? *x >= v :
false;
767 constexpr
bool operator<=(const T& v, const optional<T>& x) {
768 return bool(x) ? v <= *x :
false;
772 constexpr
bool operator<=(const optional<T>& x,
const T& v) {
773 return bool(x) ? *x <= v :
true;
778 return bool(x) ? v >= *x :
true;
784 return bool(x) ? *x == v :
false;
789 return bool(x) ? v == *x :
false;
794 return bool(x) ? *x != v :
true;
799 return bool(x) ? v != *x :
true;
803 constexpr
bool operator<(const optional<T&>& x,
const T& v) {
804 return bool(x) ? *x < v :
true;
809 return bool(x) ? v > *x :
true;
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;
824 return bool(x) ? *x >= v :
false;
828 constexpr
bool operator<=(const T& v, const optional<T&>& x) {
829 return bool(x) ? v <= *x :
false;
833 constexpr
bool operator<=(const optional<T&>& x,
const T& v) {
834 return bool(x) ? *x <= v :
true;
839 return bool(x) ? v >= *x :
true;
845 return bool(x) ? *x == v :
false;
850 return bool(x) ? v == *x :
false;
855 return bool(x) ? *x != v :
true;
860 return bool(x) ? v != *x :
true;
864 constexpr
bool operator<(const optional<const T&>& x,
const T& v) {
865 return bool(x) ? *x < v :
true;
870 return bool(x) ? v > *x :
true;
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;
885 return bool(x) ? *x >= v :
false;
889 constexpr
bool operator<=(const T& v, const optional<const T&>& x) {
890 return bool(x) ? v <= *x :
false;
894 constexpr
bool operator<=(const optional<const T&>& x,
const T& v) {
895 return bool(x) ? *x <= v :
true;
900 return bool(x) ? v >= *x :
true;
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{};
933 template <
typename T>
934 struct hash<
open3d::utility::optional<T&>> {
938 constexpr result_type
operator()(argument_type
const& arg)
const {
939 return arg ? std::hash<T>{}(*arg) : result_type{};
944 #undef TR2_OPTIONAL_REQUIRES 945 #undef TR2_OPTIONAL_ASSERTED_EXPRESSION 946 #undef TR2_OPTIONAL_HOST_CONSTEXPR constexpr result_type operator()(argument_type const &arg) const
Definition: Optional.h:938
TR2_OPTIONAL_HOST_CONSTEXPR T & operator*() const
Definition: Optional.h:599
constexpr storage_t(Args &&... args)
Definition: Optional.h:175
constexpr bool operator>=(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:655
optional & operator=(nullopt_t) noexcept
Definition: Optional.h:560
constexpr in_place_t in_place
Definition: Optional.h:60
constexpr_storage_t< T > storage_
Definition: Optional.h:228
constexpr nullopt_t nullopt
Definition: Optional.h:156
void reset() noexcept
Definition: Optional.h:514
constexpr optional(U &&u)
Definition: Optional.h:353
constexpr std::decay< T >::type value_or(V &&v) const
Definition: Optional.h:613
void swap(optional< T &> &rhs) noexcept
Definition: Optional.h:592
FN_SPECIFIERS bool operator!=(const MiniVec< T, N > &a, const MiniVec< T, N > &b)
Definition: MiniVec.h:101
constexpr constexpr_optional_base(const T &v)
Definition: Optional.h:233
constexpr T && constexpr_forward(typename std::remove_reference< T >::type &t) noexcept
Definition: Optional.h:72
bad_optional_access(const char *what_arg)
Definition: Optional.h:163
#define TR2_OPTIONAL_HOST_CONSTEXPR
Definition: Optional.h:100
hash< T >::result_type result_type
Definition: Optional.h:925
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:439
constexpr result_type operator()(argument_type const &arg) const
Definition: Optional.h:928
unsigned char dummy_
Definition: Optional.h:169
Definition: Optional.h:153
constexpr constexpr_optional_base(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:247
static constexpr bool has_overload(...)
Definition: Optional.h:116
Definition: Optional.h:922
TR2_OPTIONAL_HOST_CONSTEXPR T const & operator*() const &
Definition: Optional.h:468
constexpr constexpr_optional_base(T &&v)
Definition: Optional.h:236
storage_t< T > storage_
Definition: Optional.h:198
TR2_OPTIONAL_HOST_CONSTEXPR T && operator*() &&
Definition: Optional.h:477
Definition: Optional.h:114
T value_
Definition: Optional.h:170
optional_base(in_place_t, Args &&... args)
Definition: Optional.h:208
constexpr struct open3d::utility::trivial_init_t trivial_init
T value_type
Definition: Optional.h:318
constexpr optional(const optional &rhs) noexcept
Definition: Optional.h:550
unsigned char dummy_
Definition: Optional.h:183
void reset() noexcept
Definition: Optional.h:620
constexpr constexpr_storage_t(trivial_init_t) noexcept
Definition: Optional.h:186
optional & operator=(const optional &rhs)
Definition: Optional.h:387
open3d::utility::optional< T > argument_type
Definition: Optional.h:926
constexpr constexpr_optional_base(in_place_t, Args &&... args)
Definition: Optional.h:240
TR2_OPTIONAL_HOST_CONSTEXPR T && value() &&
Definition: Optional.h:496
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:410
optional & operator=(optional &&rhs) noexcept(std::is_nothrow_move_assignable< T >::value &&std::is_nothrow_move_constructible< T >::value)
Definition: Optional.h:397
void emplace(Args &&... args)
Definition: Optional.h:427
constexpr optional_base() noexcept
Definition: Optional.h:200
hash< T >::result_type result_type
Definition: Optional.h:935
TR2_OPTIONAL_HOST_CONSTEXPR T & value() &
Definition: Optional.h:489
constexpr bool has_value() const noexcept
Definition: Optional.h:457
static constexpr bool has_overload(bool)
Definition: Optional.h:121
constexpr T * static_addressof(T &ref)
Definition: Optional.h:129
Definition: Optional.h:152
constexpr optional(nullopt_t) noexcept
Definition: Optional.h:542
void swap(optional< T > &x, optional< T > &y) noexcept(noexcept(x.swap(y)))
Definition: Optional.h:905
Definition: Optional.h:159
constexpr T value_or(V &&v) const &
Definition: Optional.h:502
open3d::utility::optional< T & > argument_type
Definition: Optional.h:936
TR2_OPTIONAL_HOST_CONSTEXPR T const & value() const &
Definition: Optional.h:482
~optional_base()
Definition: Optional.h:220
bad_optional_access(const std::string &what_arg)
Definition: Optional.h:161
constexpr optional() noexcept
Definition: Optional.h:321
char type
Definition: FilePCD.cpp:60
void emplace(T &v) noexcept
Definition: Optional.h:588
constexpr bool operator>(const optional< T > &x, const optional< T > &y)
Definition: Optional.h:645
Definition: Optional.h:64
~storage_t()
Definition: Optional.h:178
constexpr optional(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:373
Definition: Optional.h:182
#define TR2_OPTIONAL_ASSERTED_EXPRESSION(CHECK, EXPR)
Definition: Optional.h:93
constexpr optional(in_place_t, T &v) noexcept
Definition: Optional.h:552
Definition: PinholeCameraIntrinsic.cpp:35
constexpr optional_base(T &&v)
Definition: Optional.h:204
Definition: Optional.h:51
Definition: Optional.h:526
constexpr constexpr_optional_base() noexcept
Definition: Optional.h:230
optional_base(in_place_t, std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:215
constexpr optional(nullopt_t) noexcept
Definition: Optional.h:322
constexpr optional< typename std::decay< T >::type > make_optional(T &&v)
Definition: Optional.h:910
optional & operator=(nullopt_t) noexcept
Definition: Optional.h:382
T value_
Definition: Optional.h:184
constexpr U convert(U v)
Definition: Optional.h:142
optional(const optional &rhs)
Definition: Optional.h:324
Definition: Optional.h:148
Definition: Optional.h:46
constexpr nullopt_t(init)
Definition: Optional.h:154
constexpr constexpr_storage_t(Args &&... args)
Definition: Optional.h:189
TR2_OPTIONAL_HOST_CONSTEXPR T * operator->() const
Definition: Optional.h:595
Definition: Optional.h:196
TR2_OPTIONAL_HOST_CONSTEXPR T * operator->()
Definition: Optional.h:463
T * static_addressof(T &ref)
Definition: Optional.h:134
constexpr optional_base(const T &v)
Definition: Optional.h:202
TR2_OPTIONAL_HOST_CONSTEXPR T const * operator->() const
Definition: Optional.h:459
Definition: Optional.h:56
Definition: Optional.h:226
#define TR2_OPTIONAL_REQUIRES(...)
Definition: Optional.h:40
bool init_
Definition: Optional.h:197
constexpr bool has_value() const noexcept
Definition: Optional.h:610
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:260
constexpr T value_or(V &&v) &&
Definition: Optional.h:507
constexpr optional(in_place_t, Args &&... args)
Definition: Optional.h:366
constexpr optional() noexcept
Definition: Optional.h:540
FN_SPECIFIERS bool operator==(const MiniVec< T, N > &a, const MiniVec< T, N > &b)
Definition: MiniVec.h:94
Definition: Optional.h:168
void emplace(std::initializer_list< U > il, Args &&... args)
Definition: Optional.h:433
bool init_
Definition: Optional.h:227
constexpr optional(U &u) noexcept
Definition: Optional.h:545
TR2_OPTIONAL_HOST_CONSTEXPR T & operator*() &
Definition: Optional.h:472
optional(optional &&rhs) noexcept(std::is_nothrow_move_constructible< T >::value)
Definition: Optional.h:331
constexpr T & value() const
Definition: Optional.h:603
constexpr storage_t(trivial_init_t) noexcept
Definition: Optional.h:172
constexpr std::remove_reference< T >::type && constexpr_move(T &&t) noexcept
Definition: Optional.h:85
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:576