BAN: Optional can be constructed inplace

This commit is contained in:
Bananymous 2023-06-12 22:33:04 +03:00
parent 16e5f96b1d
commit b9603d9d23
1 changed files with 26 additions and 0 deletions

View File

@ -15,11 +15,17 @@ namespace BAN
Optional(); Optional();
Optional(const T&); Optional(const T&);
Optional(T&&); Optional(T&&);
template<typename... Args>
Optional(Args&&...);
~Optional(); ~Optional();
Optional& operator=(const Optional&); Optional& operator=(const Optional&);
Optional& operator=(Optional&&); Optional& operator=(Optional&&);
template<typename... Args>
Optional& emplace(Args&&...);
T* operator->(); T* operator->();
const T* operator->() const; const T* operator->() const;
@ -58,6 +64,14 @@ namespace BAN
new (m_storage) T(BAN::move(value)); new (m_storage) T(BAN::move(value));
} }
template<typename T>
template<typename... Args>
Optional<T>::Optional(Args&&... args)
: m_has_value(true)
{
new (m_storage) T(BAN::forward<Args>(args)...);
}
template<typename T> template<typename T>
Optional<T>::~Optional() Optional<T>::~Optional()
{ {
@ -73,6 +87,7 @@ namespace BAN
m_has_value = true; m_has_value = true;
new (m_storage) T(other.value()); new (m_storage) T(other.value());
} }
return *this;
} }
template<typename T> template<typename T>
@ -84,6 +99,17 @@ namespace BAN
m_has_value = true; m_has_value = true;
new (m_storage) T(BAN::move(other.release_value())); new (m_storage) T(BAN::move(other.release_value()));
} }
return *this;
}
template<typename T>
template<typename... Args>
Optional<T>& Optional<T>::emplace(Args&&... args)
{
clear();
m_has_value = true;
new (m_storage) T(BAN::forward<Args>(args)...);
return *this;
} }
template<typename T> template<typename T>