diff options
Diffstat (limited to 'core/variant')
-rw-r--r-- | core/variant/binder_common.h | 23 | ||||
-rw-r--r-- | core/variant/type_info.h | 45 |
2 files changed, 68 insertions, 0 deletions
diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h index 22a13b0fab..84f894dcbf 100644 --- a/core/variant/binder_common.h +++ b/core/variant/binder_common.h @@ -106,6 +106,29 @@ struct VariantCaster<const T &> { static void initialize(m_enum &value) { value = (m_enum)0; } \ }; +#define VARIANT_BITFIELD_CAST(m_enum) \ + MAKE_BITFIELD_TYPE_INFO(m_enum) \ + template <> \ + struct VariantCaster<BitField<m_enum>> { \ + static _FORCE_INLINE_ BitField<m_enum> cast(const Variant &p_variant) { \ + return BitField<m_enum>(p_variant.operator int64_t()); \ + } \ + }; \ + template <> \ + struct PtrToArg<BitField<m_enum>> { \ + _FORCE_INLINE_ static BitField<m_enum> convert(const void *p_ptr) { \ + return BitField<m_enum>(*reinterpret_cast<const int64_t *>(p_ptr)); \ + } \ + typedef int64_t EncodeT; \ + _FORCE_INLINE_ static void encode(BitField<m_enum> p_val, const void *p_ptr) { \ + *(int64_t *)p_ptr = p_val; \ + } \ + }; \ + template <> \ + struct ZeroInitializer<BitField<m_enum>> { \ + static void initialize(BitField<m_enum> &value) { value = 0; } \ + }; + // Object enum casts must go here VARIANT_ENUM_CAST(Object::ConnectFlags); diff --git a/core/variant/type_info.h b/core/variant/type_info.h index bacd0d19ce..794274dd77 100644 --- a/core/variant/type_info.h +++ b/core/variant/type_info.h @@ -279,6 +279,51 @@ inline StringName __constant_get_enum_name(T param, const String &p_constant) { return GetTypeInfo<T>::get_class_info().class_name; } +template <class T> +class BitField { + uint32_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_ BitField(uint32_t p_value) { value = p_value; } + _FORCE_INLINE_ operator uint32_t() const { return value; } +}; + +#define TEMPL_MAKE_BITFIELD_TYPE_INFO(m_enum, m_impl) \ + template <> \ + struct GetTypeInfo<m_impl> { \ + static const Variant::Type VARIANT_TYPE = Variant::INT; \ + static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE; \ + static inline PropertyInfo get_class_info() { \ + return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CLASS_IS_BITFIELD, \ + godot::details::enum_qualified_name_to_class_info_name(String(#m_enum))); \ + } \ + }; \ + template <> \ + struct GetTypeInfo<BitField<m_impl>> { \ + static const Variant::Type VARIANT_TYPE = Variant::INT; \ + static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE; \ + static inline PropertyInfo get_class_info() { \ + return PropertyInfo(Variant::INT, String(), PROPERTY_HINT_NONE, String(), PROPERTY_USAGE_DEFAULT | PROPERTY_USAGE_CLASS_IS_BITFIELD, \ + godot::details::enum_qualified_name_to_class_info_name(String(#m_enum))); \ + } \ + }; + +#define MAKE_BITFIELD_TYPE_INFO(m_enum) \ + TEMPL_MAKE_BITFIELD_TYPE_INFO(m_enum, m_enum) \ + TEMPL_MAKE_BITFIELD_TYPE_INFO(m_enum, m_enum const) \ + TEMPL_MAKE_BITFIELD_TYPE_INFO(m_enum, m_enum &) \ + TEMPL_MAKE_BITFIELD_TYPE_INFO(m_enum, const m_enum &) + +template <typename T> +inline StringName __constant_get_bitfield_name(T param, const String &p_constant) { + if (GetTypeInfo<T>::VARIANT_TYPE == Variant::NIL) { + ERR_PRINT("Missing VARIANT_ENUM_CAST for constant's bitfield: " + p_constant); + } + return GetTypeInfo<BitField<T>>::get_class_info().class_name; +} #define CLASS_INFO(m_type) (GetTypeInfo<m_type *>::get_class_info()) template <typename T> |