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/binder_common.h | |
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/binder_common.h')
-rw-r--r-- | core/variant/binder_common.h | 39 |
1 files changed, 35 insertions, 4 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) { |