| 1 | #ifndef GI_ENUMFLAG_HPP |
| 2 | #define GI_ENUMFLAG_HPP |
| 3 | |
| 4 | #include "base.hpp" |
| 5 | #include "exception.hpp" |
| 6 | #include "value.hpp" |
| 7 | |
| 8 | GI_MODULE_EXPORT |
| 9 | namespace gi |
| 10 | { |
| 11 | namespace detail |
| 12 | { |
| 13 | struct EnumValueTraits |
| 14 | { |
| 15 | typedef GEnumClass class_type; |
| 16 | typedef GEnumValue value_type; |
| 17 | |
| 18 | static class_type *get_class(GType gtype) |
| 19 | { |
| 20 | auto c = g_type_class_peek(gtype); |
| 21 | return (class_type *)(c ? G_ENUM_CLASS(c) : c); |
| 22 | } |
| 23 | static value_type *get_value(class_type *klass, int v) |
| 24 | { |
| 25 | return g_enum_get_value(klass, v); |
| 26 | } |
| 27 | static value_type *get_by_name(class_type *klass, const char *name) |
| 28 | { |
| 29 | return g_enum_get_value_by_name(klass, name); |
| 30 | } |
| 31 | static value_type *get_by_nick(class_type *klass, const char *name) |
| 32 | { |
| 33 | return g_enum_get_value_by_nick(klass, name); |
| 34 | } |
| 35 | }; |
| 36 | |
| 37 | struct FlagsValueTraits |
| 38 | { |
| 39 | typedef GFlagsClass class_type; |
| 40 | typedef GFlagsValue value_type; |
| 41 | |
| 42 | static class_type *get_class(GType gtype) |
| 43 | { |
| 44 | auto c = g_type_class_peek(gtype); |
| 45 | return (class_type *)(c ? G_FLAGS_CLASS(c) : c); |
| 46 | } |
| 47 | static value_type *get_value(class_type *klass, int v) |
| 48 | { |
| 49 | return g_flags_get_first_value(klass, v); |
| 50 | } |
| 51 | static value_type *get_by_name(class_type *klass, const char *name) |
| 52 | { |
| 53 | return g_flags_get_value_by_name(klass, name); |
| 54 | } |
| 55 | static value_type *get_by_nick(class_type *klass, const char *name) |
| 56 | { |
| 57 | return g_flags_get_value_by_nick(klass, name); |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | template<typename EnumType, typename Traits> |
| 62 | class EnumValue |
| 63 | { |
| 64 | typedef EnumValue self; |
| 65 | |
| 66 | typedef typename Traits::value_type value_type; |
| 67 | typedef typename Traits::class_type class_type; |
| 68 | |
| 69 | value_type *value_; |
| 70 | |
| 71 | static class_type *get_class() |
| 72 | { |
| 73 | auto c = Traits::get_class(get_type_()); |
| 74 | if (!c) { |
| 75 | detail::try_throw(std::invalid_argument("unknown class")); |
| 76 | } else { |
| 77 | return c; |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | EnumValue(gint v) : EnumValue(static_cast<EnumType>(v)) {} |
| 82 | EnumValue(value_type *_value) : value_(_value) {} |
| 83 | |
| 84 | public: |
| 85 | typedef EnumType enum_type; |
| 86 | |
| 87 | static GType get_type_() { return traits::gtype<EnumType>::get_type(); } |
| 88 | |
| 89 | EnumValue(EnumType v) : value_(Traits::get_value(get_class(), (int)v)) |
| 90 | { |
| 91 | if (!value_) |
| 92 | detail::try_throw(std::invalid_argument( |
| 93 | "invalid value " + std::to_string((unsigned)v))); |
| 94 | } |
| 95 | |
| 96 | static self get_by_name(const gi::cstring_v name) |
| 97 | { |
| 98 | auto v = Traits::get_by_name(get_class(), name.c_str()); |
| 99 | if (!v) |
| 100 | detail::try_throw(std::invalid_argument("unknown name")); |
| 101 | else |
| 102 | return self(v); |
| 103 | } |
| 104 | static self get_by_nick(const gi::cstring_v nick) |
| 105 | { |
| 106 | auto v = Traits::get_by_nick(get_class(), nick.c_str()); |
| 107 | if (!v) |
| 108 | detail::try_throw(std::invalid_argument("unknown nick")); |
| 109 | else |
| 110 | return self(v); |
| 111 | } |
| 112 | |
| 113 | operator EnumType() const noexcept |
| 114 | { |
| 115 | return static_cast<EnumType>(value_->value); |
| 116 | } |
| 117 | |
| 118 | gi::cstring_v value_name() const noexcept { return value_->value_name; } |
| 119 | |
| 120 | gi::cstring_v value_nick() const noexcept { return value_->value_nick; } |
| 121 | }; |
| 122 | |
| 123 | } // namespace detail |
| 124 | |
| 125 | // enumeration nick/name helpers |
| 126 | template<typename EnumType> |
| 127 | using EnumValue = detail::EnumValue<EnumType, detail::EnumValueTraits>; |
| 128 | |
| 129 | template<typename EnumType, |
| 130 | typename std::enable_if<traits::is_enumeration<EnumType>::value && |
| 131 | traits::gtype<EnumType>::value>::type * = nullptr> |
| 132 | inline EnumValue<EnumType> |
| 133 | value_info(EnumType v) |
| 134 | { |
| 135 | return EnumValue<EnumType>(v); |
| 136 | } |
| 137 | |
| 138 | // bitfield nick/name helpers |
| 139 | template<typename EnumType> |
| 140 | using FlagsValue = detail::EnumValue<EnumType, detail::FlagsValueTraits>; |
| 141 | |
| 142 | template<typename EnumType, |
| 143 | typename std::enable_if<traits::is_bitfield<EnumType>::value && |
| 144 | traits::gtype<EnumType>::value>::type * = nullptr> |
| 145 | inline FlagsValue<EnumType> |
| 146 | value_info(EnumType v) |
| 147 | { |
| 148 | return FlagsValue<EnumType>(v); |
| 149 | } |
| 150 | |
| 151 | // bitfield operation helpers |
| 152 | template<typename FlagType, |
| 153 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 154 | nullptr> |
| 155 | inline FlagType |
| 156 | operator|(FlagType lhs, FlagType rhs) |
| 157 | { |
| 158 | return static_cast<FlagType>( |
| 159 | static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs)); |
| 160 | } |
| 161 | |
| 162 | template<typename FlagType, |
| 163 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 164 | nullptr> |
| 165 | inline FlagType |
| 166 | operator&(FlagType lhs, FlagType rhs) |
| 167 | { |
| 168 | return static_cast<FlagType>( |
| 169 | static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs)); |
| 170 | } |
| 171 | |
| 172 | template<typename FlagType, |
| 173 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 174 | nullptr> |
| 175 | inline FlagType |
| 176 | operator^(FlagType lhs, FlagType rhs) |
| 177 | { |
| 178 | return static_cast<FlagType>( |
| 179 | static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs)); |
| 180 | } |
| 181 | |
| 182 | template<typename FlagType, |
| 183 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 184 | nullptr> |
| 185 | inline FlagType |
| 186 | operator~(FlagType flags) |
| 187 | { |
| 188 | return static_cast<FlagType>(~static_cast<unsigned>(flags)); |
| 189 | } |
| 190 | |
| 191 | template<typename FlagType, |
| 192 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 193 | nullptr> |
| 194 | inline FlagType & |
| 195 | operator|=(FlagType &lhs, FlagType rhs) |
| 196 | { |
| 197 | return (lhs = static_cast<FlagType>( |
| 198 | static_cast<unsigned>(lhs) | static_cast<unsigned>(rhs))); |
| 199 | } |
| 200 | |
| 201 | template<typename FlagType, |
| 202 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 203 | nullptr> |
| 204 | inline FlagType & |
| 205 | operator&=(FlagType &lhs, FlagType rhs) |
| 206 | { |
| 207 | return (lhs = static_cast<FlagType>( |
| 208 | static_cast<unsigned>(lhs) & static_cast<unsigned>(rhs))); |
| 209 | } |
| 210 | |
| 211 | template<typename FlagType, |
| 212 | typename std::enable_if<traits::is_bitfield<FlagType>::value>::type * = |
| 213 | nullptr> |
| 214 | inline FlagType & |
| 215 | operator^=(FlagType &lhs, FlagType rhs) |
| 216 | { |
| 217 | return (lhs = static_cast<FlagType>( |
| 218 | static_cast<unsigned>(lhs) ^ static_cast<unsigned>(rhs))); |
| 219 | } |
| 220 | |
| 221 | } // namespace gi |
| 222 | |
| 223 | // sadly, the above templates are not picked up by ADL |
| 224 | // and might therefore only end up used in case of a `using namespace gi` |
| 225 | |
| 226 | // so also explicitly add operators in generated namespace |
| 227 | // (by means of macro to simplify generation) |
| 228 | |
| 229 | // see macro interface |
| 230 | |
| 231 | /* NOTE: |
| 232 | * |
| 233 | * enumeration/bitfield TypeX is currently represented by an enum class TypeX |
| 234 | * along with a namespace TypeXNS_ that holds the member functions. |
| 235 | * |
| 236 | * This could be merged into single class TypeX |
| 237 | * (possibly using templated helpers); |
| 238 | * |
| 239 | * class TypeX |
| 240 | * { |
| 241 | * enum Enum { |
| 242 | * VALUE_A, |
| 243 | * VALUE_B |
| 244 | * } |
| 245 | * Enum value; |
| 246 | * |
| 247 | * (or C++17): |
| 248 | * inline static const TypeX VALUE_A; |
| 249 | * (note; constexpr not possible with incomplete type) |
| 250 | * |
| 251 | * constructor (Enum) |
| 252 | * operator Enum() |
| 253 | * ... etc ... |
| 254 | * std::string value_name(); [opt] |
| 255 | * std::string value_nick(); [opt] |
| 256 | * } |
| 257 | * |
| 258 | * Disadvantage is that TypeX::VALUE_A would not have type TypeX, |
| 259 | * although easily converted from/to TypeX, and so it would mostly work. |
| 260 | * However, the operators above would have to be (even more) complicated and |
| 261 | * accept various combinations of TypeX and TypeX::Enum (in case of a bitfield). |
| 262 | * |
| 263 | * All in all, the type mismatch (and moderately rare member function |
| 264 | * occurrence) does not warrant going this way at this time. Also avoid C++17 |
| 265 | * for now, so as it stands ... |
| 266 | */ |
| 267 | |
| 268 | #endif // GI_ENUMFLAG_HPP |