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");
101 template <typename U, typename std::enable_if<std::is_constructible<ValueType, U>::value, std::nullptr_t>::type =
nullptr>
102 Optional(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType>::value)
106 ConstructValue(std::forward<U>(
value));
115 template <typename U, typename std::enable_if<std::is_constructible<ValueType, const U&>::value, std::nullptr_t>::type =
nullptr>
118 , mHasValue(other.mHasValue)
122 ConstructValue(other.mStorage.value);
132 template <typename U, typename std::enable_if<std::is_constructible<ValueType, U&&>::value, std::nullptr_t>::type =
nullptr>
135 , mHasValue(other.mHasValue)
139 ConstructValue(std::move(other.mStorage.value));
171 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>
172 Optional&
operator=(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
174 AssignmentValue(std::forward<U>(
value));
183 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>
184 Optional&
operator=(
const Optional<U>& other)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
188 AssignmentValue(other.value());
202 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>
203 Optional&
operator=(
Optional<U>&& other)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
207 AssignmentValue(std::move(other.value()));
228 if (not lhs and not rhs)
242 return not(lhs == rhs);
280 return not(lhs < rhs);
290 return not(rhs < lhs);
298 explicit constexpr operator bool() const noexcept
322 return mStorage.value;
333 return mStorage.value;
344 return std::move(mStorage.value);
355 return std::move(mStorage.value);
385 return std::move(*this).value();
395 return std::move(*this).value();
413 template <
typename U>
416 return mHasValue ? **this :
static_cast<ValueType>(std::forward<U>(defaultValue));
424 template <
typename U>
427 return mHasValue ? std::move(**
this) :
static_cast<ValueType>(std::forward<U>(defaultValue));
434 template <
typename Visitor>
445 template <
typename Visitor>
456 template <
typename Visitor>
467 void swap(
Optional& other)
noexcept( std::is_nothrow_move_assignable<ValueType>::value)
471 std::swap(**
this, *other);
473 else if (not *
this and other)
475 mStorage.value = std::move(*other);
478 else if (*
this and not other)
480 *other = std::move(**
this);
490 template <typename U = ValueType, typename std::enable_if<std::is_trivially_destructible<U>::value, std::nullptr_t>::type =
nullptr>
504 template <typename U = ValueType, typename std::enable_if<not std::is_trivially_destructible<U>::value, std::nullptr_t>::type =
nullptr>
509 mStorage.value.~ValueType();
530#if UDON_PLATFORM_OUTPUT_STREAM == UDON_PLATFORM_OUTPUT_CONSOLE
539 template <
typename CharType>
540 friend std::basic_ostream<CharType>&
operator<<(std::basic_ostream<CharType>& os,
const Optional& opt)
548 return os <<
"nullopt";
565 constexpr Storage() noexcept
570 constexpr Storage(Storage&& other)
noexcept(std::is_nothrow_move_constructible<ValueType>::value)
571 : value{
std::move(other.value) }
575 constexpr Storage(
const Storage& other)
noexcept(std::is_nothrow_move_constructible<ValueType>::value)
576 : value{ other.value }
580 ~Storage() noexcept {}
582 Storage& operator=(
const Storage& other)
588 Storage& operator=(Storage&& other)
noexcept(std::is_nothrow_assignable<ValueType, ValueType>::value)
590 value = std::move(other.value);
598 template <
typename U>
599 void ConstructValue(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType, U>::value)
604 template <
typename U>
605 void AssignmentValue(U&&
value)
noexcept(std::is_nothrow_constructible<ValueType, U>::value and std::is_nothrow_assignable<ValueType, U>::value)
609 mStorage.value = std::forward<U>(
value);
613 ConstructValue(std::forward<U>(
value));
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:184
void show() const noexcept
値を表示
Definition Optional.hpp:518
friend bool operator<(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:250
friend class Optional
Definition Optional.hpp:71
friend bool operator>(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:268
friend bool operator==(const Optional &lhs, const Optional &rhs) noexcept
等価演算子
Definition Optional.hpp:222
Optional(U &&value) noexcept(std::is_nothrow_constructible< ValueType >::value)
値から構築
Definition Optional.hpp:102
constexpr ValueType valueOr(U &&defaultValue) &&
有効であれば自身を、無効であればデフォルト値を取得する
Definition Optional.hpp:425
ValueType && operator*() &&
値を取得
Definition Optional.hpp:383
const ValueType & operator*() const &
値を取得
Definition Optional.hpp:373
constexpr auto transform(Visitor &&visitor) &&-> Optional< Traits::RemoveCVRefT< typename std::result_of< Visitor(ValueType)>::type > >
値を変換
Definition Optional.hpp:457
constexpr auto transform(Visitor &&visitor) &-> Optional< Traits::RemoveCVRefT< typename std::result_of< Visitor(ValueType &)>::type > >
値を変換
Definition Optional.hpp:435
T ValueType
Definition Optional.hpp:74
Optional & operator=(NulloptT) noexcept
無効値代入演算子
Definition Optional.hpp:158
constexpr bool hasValue() const noexcept
値が有効か
Definition Optional.hpp:308
const ValueType && operator*() const &&
値を取得
Definition Optional.hpp:393
Optional & operator=(U &&value) noexcept(std::is_nothrow_constructible< ValueType, U >::value and std::is_nothrow_assignable< ValueType, U >::value)
値代入演算子
Definition Optional.hpp:172
constexpr ValueType valueOr(U &&defaultValue) const &
有効であれば自身を、無効であればデフォルト値を取得する
Definition Optional.hpp:414
constexpr Optional(NulloptT)
無効値として構築
Definition Optional.hpp:90
void swap(Optional &other) noexcept(/*std::is_nothrow_swappable< ValueType >::value and */std::is_nothrow_move_assignable< ValueType >::value)
他のOptionalと値を交換する
Definition Optional.hpp:467
constexpr auto transform(Visitor &&visitor) const &-> Optional< Traits::RemoveCVRefT< typename std::result_of< Visitor(const ValueType &)>::type > >
値を変換
Definition Optional.hpp:446
ValueType & value() &
値を取得
Definition Optional.hpp:319
ValueType & operator*() &
値を取得
Definition Optional.hpp:363
Optional(Optional< U > &&other) noexcept(std::is_nothrow_constructible< ValueType, U >::value)
ムーブコンストラクタ
Definition Optional.hpp:133
friend bool operator!=(const Optional &lhs, const Optional &rhs) noexcept
非等価演算子
Definition Optional.hpp:240
constexpr Optional() noexcept
デフォルトコンストラクタ
Definition Optional.hpp:79
const ValueType & value() const &
値を取得
Definition Optional.hpp:330
friend bool operator<=(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:288
void reset() noexcept(std::is_nothrow_destructible< ValueType >::value)
無効状態にする (トリビアルな型)
Definition Optional.hpp:491
ValueType && value() &&
値を取得
Definition Optional.hpp:341
friend bool operator>=(const Optional &lhs, const Optional &rhs) noexcept
比較演算子
Definition Optional.hpp:278
~Optional() noexcept
デストラクタ
Definition Optional.hpp:147
const ValueType * operator->() const
Definition Optional.hpp:403
Optional(const Optional< U > &other) noexcept(std::is_nothrow_constructible< ValueType, U >::value)
コピーコンストラクタ
Definition Optional.hpp:116
ValueType * operator->()
Definition Optional.hpp:398
Optional & operator=(Optional< U > &&other) noexcept(std::is_nothrow_constructible< ValueType, U >::value and std::is_nothrow_assignable< ValueType, U >::value)
ムーブ代入演算子
Definition Optional.hpp:203
friend std::basic_ostream< CharType > & operator<<(std::basic_ostream< CharType > &os, const Optional &opt)
std::basic_ostream への出力
Definition Optional.hpp:540
const ValueType && value() const &&
値を取得
Definition Optional.hpp:352
RemoveConstT< RemoveVolatileT< RemoveReferenceT< T > > > RemoveCVRefT
Definition Typedef.hpp:28
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
constexpr Dummy() noexcept
Definition Optional.hpp:560