58 template <
typename>
struct IsOptional : std::false_type {};
59 template <
typename T>
struct IsOptional<Optional<T>> : std::true_type {};
63 Optional (
const Optional&) =
default;
64 Optional (Optional&&) =
default;
65 Optional& operator= (
const Optional&) =
default;
66 Optional& operator= (Optional&&) =
default;
68 Optional (Nullopt)
noexcept {}
70 template <
typename Head,
typename... Tail, std::enable_if_t<! IsOptional<std::decay_t<Head>>::value,
int> = 0>
71 Optional (Head&& head, Tail&&... tail)
72 noexcept (std::is_nothrow_constructible_v<std::optional<Value>, Head, Tail...>)
73 : opt (std::forward<Head> (head), std::forward<Tail> (tail)...) {}
75 template <
typename Other>
76 Optional (
const Optional<Other>& other)
77 noexcept (std::is_nothrow_constructible_v<std::optional<Value>,
const std::optional<Other>&>)
80 template <
typename Other>
81 Optional (Optional<Other>&& other)
82 noexcept (std::is_nothrow_constructible_v<std::optional<Value>, std::optional<Other>&&>)
83 : opt (std::move (other.opt)) {}
85 template <
typename Other, std::enable_if_t<! IsOptional<std::decay_t<Other>>::value,
int> = 0>
86 Optional& operator= (Other&& other)
87 noexcept (std::is_nothrow_assignable_v<std::optional<Value>, Other>)
89 opt = std::forward<Other> (other);
93 template <
typename Other>
94 Optional& operator= (
const Optional<Other>& other)
95 noexcept (std::is_nothrow_assignable_v<std::optional<Value>,
const std::optional<Other>&>)
101 template <
typename Other>
102 Optional& operator= (Optional<Other>&& other)
103 noexcept (std::is_nothrow_assignable_v<std::optional<Value>, std::optional<Other>&&>)
105 opt = std::move (other.opt);
109 template <
typename... Other>
110 auto& emplace (Other&&... other)
112 return opt.emplace (std::forward<Other> (other)...);
115 void reset()
noexcept
120 void swap (Optional& other)
121 noexcept (std::is_nothrow_swappable_v<std::optional<Value>>)
123 opt.swap (other.opt);
126 decltype (
auto) operator->() {
return opt.operator->(); }
127 decltype (
auto) operator->()
const {
return opt.operator->(); }
128 decltype (
auto)
operator* () {
return opt.operator* (); }
129 decltype (
auto)
operator* ()
const {
return opt.operator* (); }
131 explicit operator bool()
const noexcept {
return opt.has_value(); }
132 bool hasValue()
const noexcept {
return opt.has_value(); }
134 template <
typename U>
135 decltype (
auto) orFallback (U&& fallback)
const& {
return opt.value_or (std::forward<U> (fallback)); }
137 template <
typename U>
138 decltype (
auto) orFallback (U&& fallback) & {
return opt.value_or (std::forward<U> (fallback)); }
141 template <typename T, typename U> friend bool operator op (const Optional<T>&, const Optional<U>&); \
142 template <typename T> friend bool operator op (const Optional<T>&, Nullopt); \
143 template <typename T> friend bool operator op (Nullopt, const Optional<T>&); \
144 template <typename T, typename U> friend bool operator op (const Optional<T>&, const U&); \
145 template <typename T, typename U> friend bool operator op (const T&, const Optional<U>&);
147 JUCE_OPTIONAL_OPERATORS
152 template <
typename Other>
153 friend class Optional;
155 std::optional<Value> opt;