cpp-common/bt2c/fmt.hpp: use `wise_enum::string_type` in `EnableIfIsWiseEnum` definition
[babeltrace.git] / src / cpp-common / vendor / wise-enum / optional.h
1 #pragma once
2
3 #include "optional_common.h"
4
5 #include <stdexcept>
6 #include <type_traits>
7
8 #if __cplusplus == 201103
9 #define WISE_ENUM_CONSTEXPR_14
10 #else
11 #define WISE_ENUM_CONSTEXPR_14 constexpr
12 #endif
13
14 namespace wise_enum {
15
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.
20 */
21 template <class T>
22 class optional {
23 public:
24 static_assert(std::is_enum<T>::value,
25 "wise enum optional is only for enum types");
26 optional() = default;
27 optional(T t) : m_t(t), m_active(true) {}
28
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; }
33
34 constexpr explicit operator bool() const noexcept { return m_active; }
35 constexpr bool has_value() const noexcept { return m_active; }
36
37 WISE_ENUM_CONSTEXPR_14 T &value() & {
38 if (m_active)
39 return m_t;
40 else
41 WISE_ENUM_OPTIONAL_BAD_ACCESS;
42 }
43 WISE_ENUM_CONSTEXPR_14 const T &value() const & {
44 if (m_active)
45 return m_t;
46 else
47 WISE_ENUM_OPTIONAL_BAD_ACCESS;
48 }
49
50 WISE_ENUM_CONSTEXPR_14 T &&value() && {
51 if (m_active)
52 return m_t;
53 else
54 WISE_ENUM_OPTIONAL_BAD_ACCESS;
55 }
56 WISE_ENUM_CONSTEXPR_14 const T &&value() const && {
57 if (m_active)
58 return m_t;
59 else
60 WISE_ENUM_OPTIONAL_BAD_ACCESS;
61 }
62
63 template <class U>
64 WISE_ENUM_CONSTEXPR_14 T value_or(U &&u) {
65 if (m_active)
66 return m_t;
67 else
68 return std::forward<U>(u);
69 }
70
71 void reset() noexcept { m_active = false; }
72
73 optional(const optional &other) = default;
74 optional(optional &&other) = default;
75 optional &operator=(const optional &other) = default;
76 optional &operator=(optional &&other) = default;
77
78 private:
79 T m_t;
80 bool m_active = false;
81 };
82 } // namespace wise_enum
This page took 0.030114 seconds and 4 git commands to generate.