3 #include "optional_common.h"
8 #if __cplusplus == 201103
9 #define WISE_ENUM_CONSTEXPR_14
11 #define WISE_ENUM_CONSTEXPR_14 constexpr
16 /* A simple, *forward* compatible optional implementation. That is, it does not
17 * provide the full std::optional interface, but all the interface it does
18 * provide is found on std::optional, so it should not be a breaking change to
19 * upgrade to std::optional.
24 static_assert(std::is_enum
<T
>::value
,
25 "wise enum optional is only for enum types");
27 optional(T t
) : m_t(t
), m_active(true) {}
29 WISE_ENUM_CONSTEXPR_14 T
&operator*() & { return m_t
; }
30 constexpr const T
&operator*() const & { return m_t
; }
31 WISE_ENUM_CONSTEXPR_14 T
&&operator*() && { return m_t
; }
32 constexpr const T
&&operator*() const && { return m_t
; }
34 constexpr explicit operator bool() const noexcept
{ return m_active
; }
35 constexpr bool has_value() const noexcept
{ return m_active
; }
37 WISE_ENUM_CONSTEXPR_14 T
&value() & {
41 WISE_ENUM_OPTIONAL_BAD_ACCESS
;
43 WISE_ENUM_CONSTEXPR_14
const T
&value() const & {
47 WISE_ENUM_OPTIONAL_BAD_ACCESS
;
50 WISE_ENUM_CONSTEXPR_14 T
&&value() && {
54 WISE_ENUM_OPTIONAL_BAD_ACCESS
;
56 WISE_ENUM_CONSTEXPR_14
const T
&&value() const && {
60 WISE_ENUM_OPTIONAL_BAD_ACCESS
;
64 WISE_ENUM_CONSTEXPR_14 T
value_or(U
&&u
) {
68 return std::forward
<U
>(u
);
71 void reset() noexcept
{ m_active
= false; }
73 optional(const optional
&other
) = default;
74 optional(optional
&&other
) = default;
75 optional
&operator=(const optional
&other
) = default;
76 optional
&operator=(optional
&&other
) = default;
80 bool m_active
= false;
82 } // namespace wise_enum