diff options
author | Juan Linietsky <reduzio@gmail.com> | 2023-01-08 00:55:54 +0100 |
---|---|---|
committer | Juan Linietsky <reduzio@gmail.com> | 2023-01-08 22:17:40 +0100 |
commit | 2b815df3c19499f9fcf1575cfce1477876030e81 (patch) | |
tree | 60ee896d674ff9194f51fb3b1e5d27d8efb7b328 /core/variant | |
parent | fcba87e696d58912838d8a4a6987b10efa28e78f (diff) |
Use BitField<> in core type masks
* All core types masks are now correctly marked as bitfields.
* The enum hacks in MouseButtonMask and many other types are gone. This ensures that binders to other languages non C++ can actually implement type safe bitmasks.
* Most bitmask operations replaced by functions in BitField<>
* Key is still a problem because its enum and mask at the same time. While it kind of works in C++, this most likely can't be implemented safely in other languages and will have to be changed at some point. Mostly left as-is.
* Documentation and API dump updated to reflect bitfields in core types.
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/binder_common.h | 39 | ||||
-rw-r--r-- | core/variant/type_info.h | 9 |
2 files changed, 41 insertions, 7 deletions
diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index 358b3df0c5..81ac5adba7 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -146,22 +146,53 @@ VARIANT_ENUM_CAST(Side); VARIANT_ENUM_CAST(ClockDirection); VARIANT_ENUM_CAST(Corner); VARIANT_ENUM_CAST(HatDir); -VARIANT_ENUM_CAST(HatMask); +VARIANT_BITFIELD_CAST(HatMask); VARIANT_ENUM_CAST(JoyAxis); VARIANT_ENUM_CAST(JoyButton); -VARIANT_ENUM_CAST(Key); -VARIANT_ENUM_CAST(KeyModifierMask); + VARIANT_ENUM_CAST(MIDIMessage); VARIANT_ENUM_CAST(MouseButton); +VARIANT_BITFIELD_CAST(MouseButtonMask); VARIANT_ENUM_CAST(Orientation); VARIANT_ENUM_CAST(HorizontalAlignment); VARIANT_ENUM_CAST(VerticalAlignment); VARIANT_ENUM_CAST(InlineAlignment); VARIANT_ENUM_CAST(PropertyHint); -VARIANT_ENUM_CAST(PropertyUsageFlags); +VARIANT_BITFIELD_CAST(PropertyUsageFlags); VARIANT_ENUM_CAST(Variant::Type); VARIANT_ENUM_CAST(Variant::Operator); +// Key + +VARIANT_ENUM_CAST(Key); +VARIANT_BITFIELD_CAST(KeyModifierMask); + +static inline Key &operator|=(Key &a, BitField<KeyModifierMask> b) { + a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b.operator int64_t())); + return a; +} + +static inline Key &operator&=(Key &a, BitField<KeyModifierMask> b) { + a = static_cast<Key>(static_cast<int>(a) & static_cast<int>(b.operator int64_t())); + return a; +} + +static inline Key operator|(Key a, BitField<KeyModifierMask> b) { + return (Key)((int)a | (int)b.operator int64_t()); +} + +static inline Key operator&(Key a, BitField<KeyModifierMask> b) { + return (Key)((int)a & (int)b.operator int64_t()); +} + +static inline Key operator+(BitField<KeyModifierMask> a, Key b) { + return (Key)((int)a.operator int64_t() + (int)b); +} + +static inline Key operator|(BitField<KeyModifierMask> a, Key b) { + return (Key)((int)a.operator int64_t() | (int)b); +} + template <> struct VariantCaster<char32_t> { static _FORCE_INLINE_ char32_t cast(const Variant &p_variant) { diff --git a/core/variant/type_info.h b/core/variant/type_info.h index d3b507ba4d..e89658d25b 100644 --- a/core/variant/type_info.h +++ b/core/variant/type_info.h @@ -287,11 +287,14 @@ class BitField { int64_t value = 0; public: - _FORCE_INLINE_ void set_flag(T p_flag) { value |= p_flag; } - _FORCE_INLINE_ bool has_flag(T p_flag) const { return value & p_flag; } - _FORCE_INLINE_ void clear_flag(T p_flag) { return value &= ~p_flag; } + _FORCE_INLINE_ void set_flag(T p_flag) { value |= (int64_t)p_flag; } + _FORCE_INLINE_ bool has_flag(T p_flag) const { return value & (int64_t)p_flag; } + _FORCE_INLINE_ bool is_empty() const { return value == 0; } + _FORCE_INLINE_ void clear_flag(T p_flag) { value &= ~(int64_t)p_flag; } + _FORCE_INLINE_ void clear() { value = 0; } _FORCE_INLINE_ BitField() = default; _FORCE_INLINE_ BitField(int64_t p_value) { value = p_value; } + _FORCE_INLINE_ BitField(T p_value) { value = (int64_t)p_value; } _FORCE_INLINE_ operator int64_t() const { return value; } _FORCE_INLINE_ operator Variant() const { return value; } }; |