optional.hh (3280B)
1 #ifndef optional_hh_INCLUDED 2 #define optional_hh_INCLUDED 3 4 #include "assert.hh" 5 6 #include <utility> 7 8 namespace Kakoune 9 { 10 11 template<typename T> 12 struct Optional 13 { 14 public: 15 constexpr Optional() : m_valid{false} {} 16 Optional(const T& other) : m_valid{true} { new (&m_value) T(other); } 17 Optional(T&& other) : m_valid{true} { new (&m_value) T(std::move(other)); } 18 19 Optional(const Optional& other) 20 : m_valid{other.m_valid} 21 { 22 if (m_valid) 23 new (&m_value) T(other.m_value); 24 } 25 26 Optional(Optional&& other) 27 noexcept(noexcept(new (nullptr) T(std::move(other.m_value)))) 28 : m_valid{other.m_valid} 29 { 30 if (m_valid) 31 new (&m_value) T(std::move(other.m_value)); 32 } 33 34 Optional& operator=(const Optional& other) 35 { 36 destruct_ifn(); 37 if ((m_valid = other.m_valid)) 38 new (&m_value) T(other.m_value); 39 return *this; 40 } 41 42 Optional& operator=(Optional&& other) 43 { 44 destruct_ifn(); 45 if ((m_valid = other.m_valid)) 46 new (&m_value) T(std::move(other.m_value)); 47 return *this; 48 } 49 50 ~Optional() { destruct_ifn(); } 51 52 constexpr explicit operator bool() const noexcept { return m_valid; } 53 54 bool operator==(const Optional& other) const 55 { 56 return m_valid == other.m_valid and 57 (not m_valid or m_value == other.m_value); 58 } 59 60 template<typename... Args> 61 T& emplace(Args&&... args) 62 { 63 destruct_ifn(); 64 new (&m_value) T{std::forward<Args>(args)...}; 65 m_valid = true; 66 return m_value; 67 } 68 69 T& operator*() & 70 { 71 kak_assert(m_valid); 72 return m_value; 73 } 74 75 T&& operator*() && 76 { 77 kak_assert(m_valid); 78 return std::move(m_value); 79 } 80 81 const T& operator*() const & { return *const_cast<Optional&>(*this); } 82 const T& operator*() const && { return *const_cast<Optional&>(*this); } 83 84 T* operator->() 85 { 86 kak_assert(m_valid); 87 return &m_value; 88 } 89 const T* operator->() const { return const_cast<Optional&>(*this).operator->(); } 90 91 template<typename U> struct DecayOptionalImpl { using Type = U; }; 92 template<typename U> struct DecayOptionalImpl<Optional<U>> { using Type = typename DecayOptionalImpl<U>::Type; }; 93 template<typename U> using DecayOptional = typename DecayOptionalImpl<U>::Type; 94 95 template<typename F> 96 auto map(F f) -> Optional<DecayOptional<decltype(f(std::declval<T>()))>> 97 { 98 if (not m_valid) 99 return {}; 100 return {f(m_value)}; 101 } 102 103 template<typename U> 104 auto cast() const -> Optional<U> 105 { 106 if (not m_valid) 107 return {}; 108 return {(U)m_value}; 109 } 110 111 template<typename U> 112 T value_or(U&& fallback) const { return m_valid ? m_value : T{std::forward<U>(fallback)}; } 113 114 template<typename U> 115 T value_or_compute(U&& compute_func) const { return m_valid ? m_value : compute_func(); } 116 117 void reset() { destruct_ifn(); m_valid = false; } 118 119 private: 120 void destruct_ifn() { if (m_valid) m_value.~T(); } 121 122 struct Empty {}; 123 union 124 { 125 Empty m_empty; // disable default construction of value 126 T m_value; 127 }; 128 bool m_valid; 129 }; 130 131 } 132 133 #endif // optional_hh_INCLUDED