14#if __has_include(<new.h>)
21#if UDON_PLATFORM_ENABLE_EXCEPTION
22 struct BadOptionalAccess :
public std::exception
24 const char* what() const noexcept
override
26 return "Bad Optional Access";
33 throw BadOptionalAccess{};
64 static_assert(std::is_nothrow_destructible<T>::value,
"T must be nothrow destructible");
66 static_assert(not std::is_same<typename std::remove_cv<T>::type,
NulloptT>
::value,
"T must not be the same as NulloptT");
68 static_assert(not std::is_array<T>::value,
"T must not be an array");
100 template <typename U, typename std::enable_if<std::is_constructible<ValueType, U>::value, std::nullptr_t>::type =
nullptr>
101 Optional(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType>::value)
105 ConstructValue(std::forward<U>(
value));
114 template <typename U, typename std::enable_if<std::is_constructible<ValueType, const U&>::value, std::nullptr_t>::type =
nullptr>
117 , mHasValue(other.mHasValue)
121 ConstructValue(other.mStorage.value);
131 template <typename U, typename std::enable_if<std::is_constructible<ValueType, U&&>::value, std::nullptr_t>::type =
nullptr>
134 , mHasValue(other.mHasValue)
138 ConstructValue(std::move(other.mStorage.value));
170 template <typename U, typename std::enable_if<std::is_constructible<ValueType, U>::value and std::is_assignable<ValueType, U>::value, std::nullptr_t>::type =
nullptr>
171 Optional&
operator=(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
173 AssignmentValue(std::forward<U>(
value));
182 template <typename U, typename std::enable_if<std::is_constructible<ValueType, const U&>::value and std::is_assignable<ValueType, const U&>::value, std::nullptr_t>::type =
nullptr>
183 Optional&
operator=(
const Optional<U>& other)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
187 AssignmentValue(other.value());
201 template <typename U, typename std::enable_if<std::is_constructible<ValueType, U&&>::value and std::is_assignable<ValueType, U&&>::value, std::nullptr_t>::type =
nullptr>
202 Optional&
operator=(
Optional<U>&& other)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
206 AssignmentValue(std::move(other.value()));
227 if (not lhs and not rhs)
241 return not(lhs == rhs);
279 return not(lhs < rhs);
289 return not(rhs < lhs);
297 explicit constexpr operator bool() const noexcept
321 return mStorage.value;
332 return mStorage.value;
343 return std::move(mStorage.value);
354 return std::move(mStorage.value);
384 return std::move(*this).value();
394 return std::move(*this).value();
412 template <
typename U>
415 return mHasValue ? **this :
static_cast<ValueType>(std::forward<U>(defaultValue));
423 template <
typename U>
426 return mHasValue ? std::move(**
this) :
static_cast<ValueType>(std::forward<U>(defaultValue));
433 void swap(
Optional& other)
noexcept( std::is_nothrow_move_assignable<ValueType>::value)
437 std::swap(**
this, *other);
439 else if (not *
this and other)
441 mStorage.value = std::move(*other);
444 else if (*
this and not other)
446 *other = std::move(**
this);
456 template <typename U = ValueType, typename std::enable_if<std::is_trivially_destructible<U>::value, std::nullptr_t>::type =
nullptr>
470 template <typename U = ValueType, typename std::enable_if<not std::is_trivially_destructible<U>::value, std::nullptr_t>::type =
nullptr>
475 mStorage.value.~ValueType();
496#if UDON_PLATFORM_OUTPUT_STREAM == UDON_PLATFORM_OUTPUT_CONSOLE
505 template <
typename CharType>
506 friend std::basic_ostream<CharType>&
operator<<(std::basic_ostream<CharType>& os,
const Optional& opt)
514 return os <<
"nullopt";
531 constexpr Storage() noexcept
536 constexpr Storage(Storage&& other)
noexcept(std::is_nothrow_move_constructible<ValueType>::value)
537 : value{
std::move(other.value) }
541 constexpr Storage(
const Storage& other)
noexcept(std::is_nothrow_move_constructible<ValueType>::value)
542 : value{ other.value }
546 ~Storage() noexcept {}
548 Storage& operator=(
const Storage& other)
554 Storage& operator=(Storage&& other)
noexcept(std::is_nothrow_assignable<ValueType, ValueType>::value)
556 value = std::move(other.value);
564 template <
typename U>
565 void ConstructValue(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType, U>::value)
570 template <
typename U>
571 void AssignmentValue(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
575 mStorage.value = std::forward<U>(
value);
579 ConstructValue(std::forward<U>(
value));
オプショナル型
Definition Optional.hpp:62
Optional & operator=(const Optional< U > &other) noexcept(std::is_nothrow_constructible< ValueType, U >::value and std::is_nothrow_assignable< ValueType, U >::value)
コピー代入演算子
Definition Optional.hpp:183
void show() const noexcept
値を表示
Definition Optional.hpp:484
friend bool operator<(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:249
friend bool operator>(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:267
friend bool operator==(const Optional &lhs, const Optional &rhs) noexcept
等価演算子
Definition Optional.hpp:221
Optional(U &&value) noexcept(std::is_nothrow_constructible< ValueType >::value)
値から構築
Definition Optional.hpp:101
constexpr ValueType valueOr(U &&defaultValue) &&
有効であれば自身を、無効であればデフォルト値を取得する
Definition Optional.hpp:424
ValueType && operator*() &&
値を取得
Definition Optional.hpp:382
const ValueType & operator*() const &
値を取得
Definition Optional.hpp:372
T ValueType
Definition Optional.hpp:73
Optional & operator=(NulloptT) noexcept
無効値代入演算子
Definition Optional.hpp:157
constexpr bool hasValue() const noexcept
値が有効か
Definition Optional.hpp:307
const ValueType && operator*() const &&
値を取得
Definition Optional.hpp:392
Optional & operator=(U &&value) noexcept(std::is_nothrow_constructible< ValueType, U >::value and std::is_nothrow_assignable< ValueType, U >::value)
値代入演算子
Definition Optional.hpp:171
constexpr ValueType valueOr(U &&defaultValue) const &
有効であれば自身を、無効であればデフォルト値を取得する
Definition Optional.hpp:413
constexpr Optional(NulloptT)
無効値として構築
Definition Optional.hpp:89
void swap(Optional &other) noexcept(/*std::is_nothrow_swappable< ValueType >::value and */std::is_nothrow_move_assignable< ValueType >::value)
他のOptionalと値を交換する
Definition Optional.hpp:433
ValueType & value() &
値を取得
Definition Optional.hpp:318
ValueType & operator*() &
値を取得
Definition Optional.hpp:362
Optional(Optional< U > &&other) noexcept(std::is_nothrow_constructible< ValueType, U >::value)
ムーブコンストラクタ
Definition Optional.hpp:132
friend bool operator!=(const Optional &lhs, const Optional &rhs) noexcept
非等価演算子
Definition Optional.hpp:239
constexpr Optional() noexcept
デフォルトコンストラクタ
Definition Optional.hpp:78
const ValueType & value() const &
値を取得
Definition Optional.hpp:329
friend bool operator<=(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:287
void reset() noexcept(std::is_nothrow_destructible< ValueType >::value)
無効状態にする (トリビアルな型)
Definition Optional.hpp:457
ValueType && value() &&
値を取得
Definition Optional.hpp:340
friend bool operator>=(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:277
~Optional() noexcept
デストラクタ
Definition Optional.hpp:146
const ValueType * operator->() const
Definition Optional.hpp:402
Optional(const Optional< U > &other) noexcept(std::is_nothrow_constructible< ValueType, U >::value)
コピーコンストラクタ
Definition Optional.hpp:115
ValueType * operator->()
Definition Optional.hpp:397
Optional & operator=(Optional< U > &&other) noexcept(std::is_nothrow_constructible< ValueType, U >::value and std::is_nothrow_assignable< ValueType, U >::value)
ムーブ代入演算子
Definition Optional.hpp:202
friend std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &os, const Optional &opt)
std::basic_ostream への出力
Definition Optional.hpp:506
const ValueType && value() const &&
値を取得
Definition Optional.hpp:351
void ThrowBadOptionalAccess(bool)
Definition Optional.hpp:37
void ShowRaw(Args &&... args)
改行、区切り文字なしで出力する
Definition Show.hpp:362
constexpr NulloptT nullopt
無効値
Definition Optional.hpp:52
Definition Typedef.hpp:94
無効値型
Definition Optional.hpp:46
Definition Optional.hpp:525
constexpr Dummy() noexcept
Definition Optional.hpp:526