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));
43 new (m_data.bytes) T(*other);
45 m_isValid = other.m_isValid;
53 new (m_data.bytes) T(std::move(*other));
55 m_isValid = other.m_isValid;
71 new (m_data.bytes) T(*other);
73 m_isValid = other.m_isValid;
83 new (m_data.bytes) T(std::move(*other));
85 m_isValid = other.m_isValid;
99 explicit operator bool() const noexcept {
return m_isValid; }
124 bool m_isValid =
false;
129 return m_data.object;
131 const T &dereference()
const
134 return m_data.object;
140 char bytes[
sizeof(T)];
149 template <
typename T>
Own implementation of std::optional.
void reset() noexcept
If object is valid, destroy to make it invalid.
Optional & operator=(const Optional &other) noexcept(std::is_nothrow_copy_constructible_v< T >)
Copy assignment.
Optional & operator=(Optional &&other) noexcept(std::is_nothrow_move_constructible_v< T >)
Move assignment.
Optional & operator=(std::nullptr_t) noexcept
Assign a nullptr.
const T & operator*() const
Dereference operator, returns reference to contained value, undefined if there is no value contained.
Optional(std::nullptr_t) noexcept
Construct from a nullptr, equivalent to default constructor.
Optional() noexcept=default
Default constructor.
T & operator*()
Dereference operator, returns reference to contained value, undefined if there is no value contained.
T * operator->()
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.
const T * operator->() const
Indirection operator, returns pointer to contained value, undefined if there is no value contained.
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.