Skip to content

Commit

Permalink
Merge pull request #10351 from neikeq/enums-are-for-the-weak
Browse files Browse the repository at this point in the history
ClassDB: Provide the enum name of integer constants
  • Loading branch information
reduz authored Aug 21, 2017
2 parents 92410df + 32dd9a9 commit 2cc8309
Show file tree
Hide file tree
Showing 107 changed files with 2,166 additions and 1,999 deletions.
120 changes: 60 additions & 60 deletions core/bind/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ void _ResourceSaver::_bind_methods() {
ClassDB::bind_method(D_METHOD("save", "path", "resource", "flags"), &_ResourceSaver::save, DEFVAL(0));
ClassDB::bind_method(D_METHOD("get_recognized_extensions", "type"), &_ResourceSaver::get_recognized_extensions);

BIND_CONSTANT(FLAG_RELATIVE_PATHS);
BIND_CONSTANT(FLAG_BUNDLE_RESOURCES);
BIND_CONSTANT(FLAG_CHANGE_PATH);
BIND_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
BIND_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
BIND_CONSTANT(FLAG_COMPRESS);
BIND_ENUM_CONSTANT(FLAG_RELATIVE_PATHS);
BIND_ENUM_CONSTANT(FLAG_BUNDLE_RESOURCES);
BIND_ENUM_CONSTANT(FLAG_CHANGE_PATH);
BIND_ENUM_CONSTANT(FLAG_OMIT_EDITOR_PROPERTIES);
BIND_ENUM_CONSTANT(FLAG_SAVE_BIG_ENDIAN);
BIND_ENUM_CONSTANT(FLAG_COMPRESS);
}

_ResourceSaver::_ResourceSaver() {
Expand Down Expand Up @@ -1104,49 +1104,49 @@ void _OS::_bind_methods() {
ClassDB::bind_method(D_METHOD("get_power_seconds_left"), &_OS::get_power_seconds_left);
ClassDB::bind_method(D_METHOD("get_power_percent_left"), &_OS::get_power_percent_left);

BIND_CONSTANT(DAY_SUNDAY);
BIND_CONSTANT(DAY_MONDAY);
BIND_CONSTANT(DAY_TUESDAY);
BIND_CONSTANT(DAY_WEDNESDAY);
BIND_CONSTANT(DAY_THURSDAY);
BIND_CONSTANT(DAY_FRIDAY);
BIND_CONSTANT(DAY_SATURDAY);

BIND_CONSTANT(MONTH_JANUARY);
BIND_CONSTANT(MONTH_FEBRUARY);
BIND_CONSTANT(MONTH_MARCH);
BIND_CONSTANT(MONTH_APRIL);
BIND_CONSTANT(MONTH_MAY);
BIND_CONSTANT(MONTH_JUNE);
BIND_CONSTANT(MONTH_JULY);
BIND_CONSTANT(MONTH_AUGUST);
BIND_CONSTANT(MONTH_SEPTEMBER);
BIND_CONSTANT(MONTH_OCTOBER);
BIND_CONSTANT(MONTH_NOVEMBER);
BIND_CONSTANT(MONTH_DECEMBER);

BIND_CONSTANT(SCREEN_ORIENTATION_LANDSCAPE);
BIND_CONSTANT(SCREEN_ORIENTATION_PORTRAIT);
BIND_CONSTANT(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
BIND_CONSTANT(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR_PORTRAIT);
BIND_CONSTANT(SCREEN_ORIENTATION_SENSOR);

BIND_CONSTANT(SYSTEM_DIR_DESKTOP);
BIND_CONSTANT(SYSTEM_DIR_DCIM);
BIND_CONSTANT(SYSTEM_DIR_DOCUMENTS);
BIND_CONSTANT(SYSTEM_DIR_DOWNLOADS);
BIND_CONSTANT(SYSTEM_DIR_MOVIES);
BIND_CONSTANT(SYSTEM_DIR_MUSIC);
BIND_CONSTANT(SYSTEM_DIR_PICTURES);
BIND_CONSTANT(SYSTEM_DIR_RINGTONES);

BIND_CONSTANT(POWERSTATE_UNKNOWN);
BIND_CONSTANT(POWERSTATE_ON_BATTERY);
BIND_CONSTANT(POWERSTATE_NO_BATTERY);
BIND_CONSTANT(POWERSTATE_CHARGING);
BIND_CONSTANT(POWERSTATE_CHARGED);
BIND_ENUM_CONSTANT(DAY_SUNDAY);
BIND_ENUM_CONSTANT(DAY_MONDAY);
BIND_ENUM_CONSTANT(DAY_TUESDAY);
BIND_ENUM_CONSTANT(DAY_WEDNESDAY);
BIND_ENUM_CONSTANT(DAY_THURSDAY);
BIND_ENUM_CONSTANT(DAY_FRIDAY);
BIND_ENUM_CONSTANT(DAY_SATURDAY);

BIND_ENUM_CONSTANT(MONTH_JANUARY);
BIND_ENUM_CONSTANT(MONTH_FEBRUARY);
BIND_ENUM_CONSTANT(MONTH_MARCH);
BIND_ENUM_CONSTANT(MONTH_APRIL);
BIND_ENUM_CONSTANT(MONTH_MAY);
BIND_ENUM_CONSTANT(MONTH_JUNE);
BIND_ENUM_CONSTANT(MONTH_JULY);
BIND_ENUM_CONSTANT(MONTH_AUGUST);
BIND_ENUM_CONSTANT(MONTH_SEPTEMBER);
BIND_ENUM_CONSTANT(MONTH_OCTOBER);
BIND_ENUM_CONSTANT(MONTH_NOVEMBER);
BIND_ENUM_CONSTANT(MONTH_DECEMBER);

BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_LANDSCAPE);
BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_PORTRAIT);
BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_REVERSE_PORTRAIT);
BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR_PORTRAIT);
BIND_ENUM_CONSTANT(SCREEN_ORIENTATION_SENSOR);

BIND_ENUM_CONSTANT(SYSTEM_DIR_DESKTOP);
BIND_ENUM_CONSTANT(SYSTEM_DIR_DCIM);
BIND_ENUM_CONSTANT(SYSTEM_DIR_DOCUMENTS);
BIND_ENUM_CONSTANT(SYSTEM_DIR_DOWNLOADS);
BIND_ENUM_CONSTANT(SYSTEM_DIR_MOVIES);
BIND_ENUM_CONSTANT(SYSTEM_DIR_MUSIC);
BIND_ENUM_CONSTANT(SYSTEM_DIR_PICTURES);
BIND_ENUM_CONSTANT(SYSTEM_DIR_RINGTONES);

BIND_ENUM_CONSTANT(POWERSTATE_UNKNOWN);
BIND_ENUM_CONSTANT(POWERSTATE_ON_BATTERY);
BIND_ENUM_CONSTANT(POWERSTATE_NO_BATTERY);
BIND_ENUM_CONSTANT(POWERSTATE_CHARGING);
BIND_ENUM_CONSTANT(POWERSTATE_CHARGED);
}

_OS::_OS() {
Expand Down Expand Up @@ -1772,15 +1772,15 @@ void _File::_bind_methods() {
ClassDB::bind_method(D_METHOD("file_exists", "path"), &_File::file_exists);
ClassDB::bind_method(D_METHOD("get_modified_time", "file"), &_File::get_modified_time);

BIND_CONSTANT(READ);
BIND_CONSTANT(WRITE);
BIND_CONSTANT(READ_WRITE);
BIND_CONSTANT(WRITE_READ);
BIND_ENUM_CONSTANT(READ);
BIND_ENUM_CONSTANT(WRITE);
BIND_ENUM_CONSTANT(READ_WRITE);
BIND_ENUM_CONSTANT(WRITE_READ);

BIND_CONSTANT(COMPRESSION_FASTLZ);
BIND_CONSTANT(COMPRESSION_DEFLATE);
BIND_CONSTANT(COMPRESSION_ZSTD);
BIND_CONSTANT(COMPRESSION_GZIP);
BIND_ENUM_CONSTANT(COMPRESSION_FASTLZ);
BIND_ENUM_CONSTANT(COMPRESSION_DEFLATE);
BIND_ENUM_CONSTANT(COMPRESSION_ZSTD);
BIND_ENUM_CONSTANT(COMPRESSION_GZIP);
}

_File::_File() {
Expand Down Expand Up @@ -2285,9 +2285,9 @@ void _Thread::_bind_methods() {
ClassDB::bind_method(D_METHOD("is_active"), &_Thread::is_active);
ClassDB::bind_method(D_METHOD("wait_to_finish"), &_Thread::wait_to_finish);

BIND_CONSTANT(PRIORITY_LOW);
BIND_CONSTANT(PRIORITY_NORMAL);
BIND_CONSTANT(PRIORITY_HIGH);
BIND_ENUM_CONSTANT(PRIORITY_LOW);
BIND_ENUM_CONSTANT(PRIORITY_NORMAL);
BIND_ENUM_CONSTANT(PRIORITY_HIGH);
}
_Thread::_Thread() {

Expand Down
9 changes: 9 additions & 0 deletions core/bind/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ class _ResourceSaver : public Object {
_ResourceSaver();
};

VARIANT_ENUM_CAST(_ResourceSaver::SaverFlags);

class MainLoop;

class _OS : public Object {
Expand Down Expand Up @@ -310,6 +312,8 @@ class _OS : public Object {
_OS();
};

VARIANT_ENUM_CAST(_OS::Weekday);
VARIANT_ENUM_CAST(_OS::Month);
VARIANT_ENUM_CAST(_OS::SystemDir);
VARIANT_ENUM_CAST(_OS::ScreenOrientation);

Expand Down Expand Up @@ -447,6 +451,9 @@ class _File : public Reference {
virtual ~_File();
};

VARIANT_ENUM_CAST(_File::ModeFlags);
VARIANT_ENUM_CAST(_File::CompressionMode);

class _Directory : public Reference {

GDCLASS(_Directory, Reference);
Expand Down Expand Up @@ -578,6 +585,8 @@ class _Thread : public Reference {
~_Thread();
};

VARIANT_ENUM_CAST(_Thread::Priority);

class _ClassDB : public Object {

GDCLASS(_ClassDB, Object)
Expand Down
83 changes: 82 additions & 1 deletion core/class_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ MethodBind *ClassDB::get_method(StringName p_class, StringName p_name) {
return NULL;
}

void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_name, int p_constant) {
void ClassDB::bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant) {

OBJTYPE_WLOCK;

Expand All @@ -600,6 +600,16 @@ void ClassDB::bind_integer_constant(const StringName &p_class, const StringName

type->constant_map[p_name] = p_constant;
#ifdef DEBUG_METHODS_ENABLED
List<StringName> *constants_list = type->enum_map.getptr(p_enum);

if (constants_list) {
constants_list->push_back(p_name);
} else {
List<StringName> new_list;
new_list.push_back(p_name);
type->enum_map[p_enum] = new_list;
}

type->constant_order.push_back(p_name);
#endif
}
Expand Down Expand Up @@ -655,6 +665,77 @@ int ClassDB::get_integer_constant(const StringName &p_class, const StringName &p
return 0;
}

#ifdef DEBUG_METHODS_ENABLED
StringName ClassDB::get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance) {

OBJTYPE_RLOCK;

ClassInfo *type = classes.getptr(p_class);

while (type) {

const StringName *k = NULL;
while ((k = type->enum_map.next(k))) {

List<StringName> &constants_list = type->enum_map.get(*k);
const List<StringName>::Element *found = constants_list.find(p_name);
if (found)
return found->get();
}

if (p_no_inheritance)
break;

type = type->inherits_ptr;
}

return StringName();
}

void ClassDB::get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance) {

OBJTYPE_RLOCK;

ClassInfo *type = classes.getptr(p_class);

while (type) {

const StringName *k = NULL;
while ((k = type->enum_map.next(k))) {
p_enums->push_back(*k);
}

if (p_no_inheritance)
break;

type = type->inherits_ptr;
}
}

void ClassDB::get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance) {

OBJTYPE_RLOCK;

ClassInfo *type = classes.getptr(p_class);

while (type) {

const List<StringName> *constants = type->enum_map.getptr(p_enum);

if (constants) {
for (const List<StringName>::Element *E = constants->front(); E; E = E->next()) {
p_constants->push_back(E->get());
}
}

if (p_no_inheritance)
break;

type = type->inherits_ptr;
}
}
#endif

void ClassDB::add_signal(StringName p_class, const MethodInfo &p_signal) {

OBJTYPE_WLOCK;
Expand Down
27 changes: 25 additions & 2 deletions core/class_db.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ class ClassDB {
HashMap<StringName, MethodInfo, StringNameHasher> signal_map;
List<PropertyInfo> property_list;
#ifdef DEBUG_METHODS_ENABLED
HashMap<StringName, List<StringName> > enum_map;
List<StringName> constant_order;
List<StringName> method_order;
Set<StringName> methods_in_properties;
Expand Down Expand Up @@ -488,9 +489,16 @@ class ClassDB {
static void add_virtual_method(const StringName &p_class, const MethodInfo &p_method, bool p_virtual = true);
static void get_virtual_methods(const StringName &p_class, List<MethodInfo> *p_methods, bool p_no_inheritance = false);

static void bind_integer_constant(const StringName &p_class, const StringName &p_name, int p_constant);
static void bind_integer_constant(const StringName &p_class, const StringName &p_enum, const StringName &p_name, int p_constant);
static void get_integer_constant_list(const StringName &p_class, List<String> *p_constants, bool p_no_inheritance = false);
static int get_integer_constant(const StringName &p_class, const StringName &p_name, bool *p_success = NULL);

#ifdef DEBUG_METHODS_ENABLED
static StringName get_integer_constant_enum(const StringName &p_class, const StringName &p_name, bool p_no_inheritance = false);
static void get_enum_list(const StringName &p_class, List<StringName> *p_enums, bool p_no_inheritance = false);
static void get_enum_constants(const StringName &p_class, const StringName &p_enum, List<StringName> *p_constants, bool p_no_inheritance = false);
#endif

static StringName get_category(const StringName &p_node);

static bool get_setter_and_type_for_property(const StringName &p_class, const StringName &p_prop, StringName &r_class, StringName &r_setter);
Expand All @@ -509,8 +517,23 @@ class ClassDB {
static void cleanup();
};

#ifdef DEBUG_METHODS_ENABLED

#define BIND_CONSTANT(m_constant) \
ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);

#define BIND_ENUM_CONSTANT(m_constant) \
ClassDB::bind_integer_constant(get_class_static(), __constant_get_enum_name(m_constant, #m_constant), #m_constant, m_constant);

#else

#define BIND_CONSTANT(m_constant) \
ClassDB::bind_integer_constant(get_class_static(), #m_constant, m_constant);
ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);

#define BIND_ENUM_CONSTANT(m_constant) \
ClassDB::bind_integer_constant(get_class_static(), StringName(), #m_constant, m_constant);

#endif

#ifdef TOOLS_ENABLED

Expand Down
Loading

0 comments on commit 2cc8309

Please sign in to comment.