6 #ifndef SWIFT_MISC_OPTIONAL_H
7 #define SWIFT_MISC_OPTIONAL_H
29 Optional(T value) noexcept(std::is_nothrow_move_constructible_v<T>)
31 new (m_data.bytes) T(std::move(value));
41 if (other.m_isValid) {
new (m_data.bytes) T(*other); }
42 m_isValid = other.m_isValid;
48 if (other.m_isValid) {
new (m_data.bytes) T(std::move(*other)); }
49 m_isValid = other.m_isValid;
63 if (other.m_isValid) {
new (m_data.bytes) T(*other); }
64 m_isValid = other.m_isValid;
72 if (other.m_isValid) {
new (m_data.bytes) T(std::move(*other)); }
73 m_isValid = other.m_isValid;
80 if (m_isValid) { (*this)->~T(); }
84 explicit operator bool() const noexcept {
return m_isValid; }
89 if (m_isValid) { (*this)->~T(); }
97 const T &
operator*()
const {
return dereference(); }
106 bool m_isValid =
false;
111 return m_data.object;
113 const T &dereference()
const
116 return m_data.object;
122 char bytes[
sizeof(T)];
131 template <
typename T>
Own implementation of std::optional.
void reset() noexcept
If object is valid, destroy to make it invalid.
const T & operator*() const
Dereference operator, returns reference to contained value, undefined if there is no value contained.
T & operator*()
Dereference operator, returns reference to contained value, undefined if there is no value contained.
Optional & operator=(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Copy assignment.
T * operator->()
Indirection operator, returns pointer to contained value, undefined if there is no value contained.
Optional & operator=(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Move assignment.
Optional(std::nullptr_t) noexcept
Construct from a nullptr, equivalent to default constructor.
Optional() noexcept=default
Default constructor.
Optional & operator=(std::nullptr_t) noexcept
Assign a nullptr.
const T * operator->() const
Indirection operator, returns pointer to contained value, undefined if there is no value contained.
Optional(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Move constructor.
Optional(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Copy constructor.
Free functions in swift::misc.
void swap(Optional< T > &a, Optional< T > &b) noexcept(std::is_nothrow_swappable_v< T >)
Efficient swap for two Optional objects.