home/src/optional.hh

127 lines
3.1 KiB
C++
Raw Normal View History

#ifndef optional_hh_INCLUDED
#define optional_hh_INCLUDED
#include "assert.hh"
#include <utility>
namespace Kakoune
{
template<typename T>
struct Optional
{
public:
2017-09-01 11:54:06 +02:00
constexpr Optional() : m_valid{false} {}
Optional(const T& other) : m_valid{true} { new (&m_value) T(other); }
Optional(T&& other) : m_valid{true} { new (&m_value) T(std::move(other)); }
Optional(const Optional& other)
2017-09-01 11:54:06 +02:00
: m_valid{other.m_valid}
{
if (m_valid)
new (&m_value) T(other.m_value);
}
Optional(Optional&& other)
2014-12-25 11:59:06 +01:00
noexcept(noexcept(new (nullptr) T(std::move(other.m_value))))
2017-09-01 11:54:06 +02:00
: m_valid{other.m_valid}
{
if (m_valid)
new (&m_value) T(std::move(other.m_value));
}
Optional& operator=(const Optional& other)
{
2014-12-25 11:59:06 +01:00
destruct_ifn();
if ((m_valid = other.m_valid))
new (&m_value) T(other.m_value);
return *this;
}
Optional& operator=(Optional&& other)
{
2014-12-25 11:59:06 +01:00
destruct_ifn();
if ((m_valid = other.m_valid))
new (&m_value) T(std::move(other.m_value));
return *this;
}
2014-12-25 11:59:06 +01:00
~Optional() { destruct_ifn(); }
constexpr explicit operator bool() const noexcept { return m_valid; }
2014-08-19 19:55:36 +02:00
bool operator==(const Optional& other) const
{
2016-07-04 20:19:40 +02:00
return m_valid == other.m_valid and
(not m_valid or m_value == other.m_value);
2014-08-19 19:55:36 +02:00
}
bool operator!=(const Optional& other) const { return !(*this == other); }
2016-06-22 23:17:13 +02:00
template<typename... Args>
void emplace(Args&&... args)
{
destruct_ifn();
new (&m_value) T{std::forward<Args>(args)...};
m_valid = true;
}
T& operator*()
{
kak_assert(m_valid);
return m_value;
}
const T& operator*() const { return *const_cast<Optional&>(*this); }
T* operator->()
{
kak_assert(m_valid);
return &m_value;
}
const T* operator->() const { return const_cast<Optional&>(*this).operator->(); }
template<typename U> struct DecayOptionalImpl { using Type = U; };
template<typename U> struct DecayOptionalImpl<Optional<U>> { using Type = typename DecayOptionalImpl<U>::Type; };
template<typename U> using DecayOptional = typename DecayOptionalImpl<U>::Type;
template<typename F>
auto map(F f) -> Optional<DecayOptional<decltype(f(std::declval<T&&>()))>>
{
if (not m_valid)
return {};
return {f(m_value)};
}
template<typename U>
2019-11-02 21:49:26 +01:00
auto cast() const -> Optional<U>
{
if (not m_valid)
return {};
return {(U)m_value};
}
template<typename U>
2017-09-01 11:54:06 +02:00
T value_or(U&& fallback) const { return m_valid ? m_value : T{std::forward<U>(fallback)}; }
template<typename U>
T value_or_compute(U&& compute_func) const { return m_valid ? m_value : compute_func(); }
void reset() { destruct_ifn(); m_valid = false; }
private:
2014-12-25 11:59:06 +01:00
void destruct_ifn() { if (m_valid) m_value.~T(); }
2015-07-10 20:05:21 +02:00
struct Empty {};
union
{
2015-07-10 20:05:21 +02:00
Empty m_empty; // disable default construction of value
T m_value;
};
bool m_valid;
};
}
#endif // optional_hh_INCLUDED