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/core_constants.cpp | |
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/core_constants.cpp')
-rw-r--r-- | core/core_constants.cpp | 147 |
1 files changed, 88 insertions, 59 deletions
diff --git a/core/core_constants.cpp b/core/core_constants.cpp index 500a554a24..504b343736 100644 --- a/core/core_constants.cpp +++ b/core/core_constants.cpp @@ -39,6 +39,7 @@ struct _CoreConstant { #ifdef DEBUG_METHODS_ENABLED StringName enum_name; bool ignore_value_in_docs = false; + bool is_bitfield = false; #endif const char *name = nullptr; int64_t value = 0; @@ -46,9 +47,10 @@ struct _CoreConstant { _CoreConstant() {} #ifdef DEBUG_METHODS_ENABLED - _CoreConstant(const StringName &p_enum_name, const char *p_name, int64_t p_value, bool p_ignore_value_in_docs = false) : + _CoreConstant(const StringName &p_enum_name, const char *p_name, int64_t p_value, bool p_ignore_value_in_docs = false, bool p_is_bitfield = false) : enum_name(p_enum_name), ignore_value_in_docs(p_ignore_value_in_docs), + is_bitfield(p_is_bitfield), name(p_name), value(p_value) { } @@ -70,13 +72,22 @@ static Vector<_CoreConstant> _global_constants; #define BIND_CORE_ENUM_CONSTANT(m_constant) \ _global_constants.push_back(_CoreConstant(__constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant)); +#define BIND_CORE_BITFIELD_FLAG(m_constant) \ + _global_constants.push_back(_CoreConstant(__constant_get_bitfield_name(m_constant, #m_constant), #m_constant, m_constant, false, true)); + // This just binds enum classes as if they were regular enum constants. #define BIND_CORE_ENUM_CLASS_CONSTANT(m_enum, m_prefix, m_member) \ _global_constants.push_back(_CoreConstant(__constant_get_enum_name(m_enum::m_member, #m_prefix "_" #m_member), #m_prefix "_" #m_member, (int64_t)m_enum::m_member)); +#define BIND_CORE_BITFIELD_CLASS_FLAG(m_enum, m_prefix, m_member) \ + _global_constants.push_back(_CoreConstant(__constant_get_bitfield_name(m_enum::m_member, #m_prefix "_" #m_member), #m_prefix "_" #m_member, (int64_t)m_enum::m_member, false, true)); + #define BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(m_enum, m_name, m_member) \ _global_constants.push_back(_CoreConstant(__constant_get_enum_name(m_enum::m_member, #m_name), #m_name, (int64_t)m_enum::m_member)); +#define BIND_CORE_BITFIELD_CLASS_FLAG_CUSTOM(m_enum, m_name, m_member) \ + _global_constants.push_back(_CoreConstant(__constant_get_bitfield_name(m_enum::m_member, #m_name), #m_name, (int64_t)m_enum::m_member, false, true)); + #define BIND_CORE_ENUM_CLASS_CONSTANT_NO_VAL(m_enum, m_prefix, m_member) \ _global_constants.push_back(_CoreConstant(__constant_get_enum_name(m_enum::m_member, #m_prefix "_" #m_member), #m_prefix "_" #m_member, (int64_t)m_enum::m_member, true)); @@ -100,13 +111,22 @@ static Vector<_CoreConstant> _global_constants; #define BIND_CORE_ENUM_CONSTANT(m_constant) \ _global_constants.push_back(_CoreConstant(#m_constant, m_constant)); +#define BIND_CORE_BITFIELD_FLAG(m_constant) \ + _global_constants.push_back(_CoreConstant(#m_constant, m_constant)); + // This just binds enum classes as if they were regular enum constants. #define BIND_CORE_ENUM_CLASS_CONSTANT(m_enum, m_prefix, m_member) \ _global_constants.push_back(_CoreConstant(#m_prefix "_" #m_member, (int64_t)m_enum::m_member)); +#define BIND_CORE_BITFIELD_CLASS_FLAG(m_enum, m_prefix, m_member) \ + _global_constants.push_back(_CoreConstant(#m_prefix "_" #m_member, (int64_t)m_enum::m_member)); + #define BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(m_enum, m_name, m_member) \ _global_constants.push_back(_CoreConstant(#m_name, (int64_t)m_enum::m_member)); +#define BIND_CORE_BITFIELD_CLASS_FLAG_CUSTOM(m_enum, m_name, m_member) \ + _global_constants.push_back(_CoreConstant(#m_name, (int64_t)m_enum::m_member)); + #define BIND_CORE_ENUM_CLASS_CONSTANT_NO_VAL(m_enum, m_prefix, m_member) \ _global_constants.push_back(_CoreConstant(#m_prefix "_" #m_member, (int64_t)m_enum::m_member)); @@ -439,15 +459,15 @@ void register_global_constants() { BIND_CORE_ENUM_CLASS_CONSTANT(Key, KEY, DIVISION); BIND_CORE_ENUM_CLASS_CONSTANT(Key, KEY, YDIAERESIS); - BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(KeyModifierMask, KEY_CODE_MASK, CODE_MASK); - BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(KeyModifierMask, KEY_MODIFIER_MASK, MODIFIER_MASK); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, CMD_OR_CTRL); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, SHIFT); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, ALT); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, META); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, CTRL); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, KPAD); - BIND_CORE_ENUM_CLASS_CONSTANT(KeyModifierMask, KEY_MASK, GROUP_SWITCH); + BIND_CORE_BITFIELD_CLASS_FLAG_CUSTOM(KeyModifierMask, KEY_CODE_MASK, CODE_MASK); + BIND_CORE_BITFIELD_CLASS_FLAG_CUSTOM(KeyModifierMask, KEY_MODIFIER_MASK, MODIFIER_MASK); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, CMD_OR_CTRL); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, SHIFT); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, ALT); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, META); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, CTRL); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, KPAD); + BIND_CORE_BITFIELD_CLASS_FLAG(KeyModifierMask, KEY_MASK, GROUP_SWITCH); BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, NONE); BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, LEFT); @@ -459,11 +479,12 @@ void register_global_constants() { BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, WHEEL_RIGHT); BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(MouseButton, MOUSE_BUTTON_XBUTTON1, MB_XBUTTON1); BIND_CORE_ENUM_CLASS_CONSTANT_CUSTOM(MouseButton, MOUSE_BUTTON_XBUTTON2, MB_XBUTTON2); - BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, MASK_LEFT); - BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, MASK_RIGHT); - BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, MASK_MIDDLE); - BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, MASK_XBUTTON1); - BIND_CORE_ENUM_CLASS_CONSTANT(MouseButton, MOUSE_BUTTON, MASK_XBUTTON2); + + BIND_CORE_BITFIELD_CLASS_FLAG(MouseButtonMask, MOUSE_BUTTON_MASK, LEFT); + BIND_CORE_BITFIELD_CLASS_FLAG(MouseButtonMask, MOUSE_BUTTON_MASK, RIGHT); + BIND_CORE_BITFIELD_CLASS_FLAG(MouseButtonMask, MOUSE_BUTTON_MASK, MIDDLE); + BIND_CORE_BITFIELD_CLASS_FLAG(MouseButtonMask, MOUSE_BUTTON_MASK, MB_XBUTTON1); + BIND_CORE_BITFIELD_CLASS_FLAG(MouseButtonMask, MOUSE_BUTTON_MASK, MB_XBUTTON2); BIND_CORE_ENUM_CLASS_CONSTANT(JoyButton, JOY_BUTTON, INVALID); BIND_CORE_ENUM_CLASS_CONSTANT(JoyButton, JOY_BUTTON, A); @@ -622,50 +643,50 @@ void register_global_constants() { BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_PASSWORD); BIND_CORE_ENUM_CONSTANT(PROPERTY_HINT_MAX); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NONE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_STORAGE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR); - - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_CHECKABLE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_CHECKED); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_INTERNATIONALIZED); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_GROUP); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_CATEGORY); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_SUBGROUP); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_CLASS_IS_BITFIELD); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NO_INSTANCE_STATE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_RESTART_IF_CHANGED); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_SCRIPT_VARIABLE); - - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_STORE_IF_NULL); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_CLASS_IS_ENUM); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NIL_IS_VARIANT); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_INTERNAL); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_HIGH_END_GFX); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_KEYING_INCREMENTS); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_DEFERRED_SET_RESOURCE); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_EDITOR_BASIC_SETTING); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_READ_ONLY); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_ARRAY); - - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_DEFAULT); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_DEFAULT_INTL); - BIND_CORE_ENUM_CONSTANT(PROPERTY_USAGE_NO_EDITOR); - - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_NORMAL); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_EDITOR); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_CONST); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_VIRTUAL); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_VARARG); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_STATIC); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAG_OBJECT_CORE); - BIND_CORE_ENUM_CONSTANT(METHOD_FLAGS_DEFAULT); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NONE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_STORAGE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_EDITOR); + + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_CHECKABLE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_CHECKED); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_INTERNATIONALIZED); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_GROUP); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_CATEGORY); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_SUBGROUP); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_CLASS_IS_BITFIELD); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NO_INSTANCE_STATE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_RESTART_IF_CHANGED); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_SCRIPT_VARIABLE); + + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_STORE_IF_NULL); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_CLASS_IS_ENUM); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NIL_IS_VARIANT); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_INTERNAL); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_DO_NOT_SHARE_ON_DUPLICATE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_HIGH_END_GFX); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_KEYING_INCREMENTS); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_DEFERRED_SET_RESOURCE); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_EDITOR_BASIC_SETTING); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_READ_ONLY); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_ARRAY); + + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_DEFAULT); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_DEFAULT_INTL); + BIND_CORE_BITFIELD_FLAG(PROPERTY_USAGE_NO_EDITOR); + + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_NORMAL); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_EDITOR); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_CONST); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_VIRTUAL); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_VARARG); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_STATIC); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAG_OBJECT_CORE); + BIND_CORE_BITFIELD_FLAG(METHOD_FLAGS_DEFAULT); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_NIL", Variant::NIL); BIND_CORE_ENUM_CONSTANT_CUSTOM("TYPE_BOOL", Variant::BOOL); @@ -753,6 +774,10 @@ StringName CoreConstants::get_global_constant_enum(int p_idx) { return _global_constants[p_idx].enum_name; } +bool CoreConstants::is_global_constant_bitfield(int p_idx) { + return _global_constants[p_idx].is_bitfield; +} + bool CoreConstants::get_ignore_value_in_docs(int p_idx) { return _global_constants[p_idx].ignore_value_in_docs; } @@ -761,6 +786,10 @@ StringName CoreConstants::get_global_constant_enum(int p_idx) { return StringName(); } +bool CoreConstants::is_global_constant_bitfield(int p_idx) { + return false; +} + bool CoreConstants::get_ignore_value_in_docs(int p_idx) { return false; } |