Skip to content

Commit

Permalink
Rebased and cleaned up memory in struct unit tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
nlupugla committed Sep 18, 2024
1 parent 5701057 commit b295f0c
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 106 deletions.
18 changes: 9 additions & 9 deletions core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,10 +1627,6 @@ bool ClassDB::is_class_enum_bitfield(const StringName &p_class, const StringName
return ::ClassDB::is_enum_bitfield(p_class, p_enum, p_no_inheritance);
}

bool ClassDB::is_class_enabled(const StringName &p_class) const {
return ::ClassDB::is_class_enabled(p_class);
}

bool ClassDB::class_has_struct(const StringName &p_class, const StringName &p_struct, bool p_no_inheritance) const {
return ::ClassDB::get_struct_info(p_class, p_struct, p_no_inheritance) != nullptr;
}
Expand Down Expand Up @@ -1672,16 +1668,20 @@ TypedArray<Dictionary> ClassDB::class_get_struct_members(const StringName &p_cla
return ret;
}

bool ClassDB::is_class_enabled(const StringName &p_class) const {
return ::ClassDB::is_class_enabled(p_class);
}

#ifdef TOOLS_ENABLED
void ClassDB::get_argument_options(const StringName &p_function, int p_idx, List<String> *r_options) const {
const String pf = p_function;
bool first_argument_is_class = false;
if (p_idx == 0) {
first_argument_is_class = (pf == "get_inheriters_from_class" || pf == "get_parent_class" ||
pf == "class_exists" || pf == "can_instantiate" || pf == "instantiate" ||
pf == "class_has_signal" || pf == "class_get_signal" || pf == "class_get_signal_list" ||
pf == "class_get_property_list" || pf == "class_get_property" || pf == "class_set_property" ||
pf == "class_has_method" || pf == "class_get_method_list" ||
pf == "class_has_signal" || pf == "class_get_signal" || pf == "class_get_signal_list" || pf == "class_get_signal_list_as_struct" ||
pf == "class_get_property_list" || pf == "class_get_property_list_as_structs" || pf == "class_get_property" || pf == "class_set_property" ||
pf == "class_has_method" || pf == "class_get_method_list" || pf == "class_get_method_list_as_structs" ||
pf == "class_get_integer_constant_list" || pf == "class_has_integer_constant" || pf == "class_get_integer_constant" ||
pf == "class_has_enum" || pf == "class_get_enum_list" || pf == "class_get_enum_constants" || pf == "class_get_integer_constant_enum" ||
pf == "is_class_enabled" || pf == "is_class_enum_bitfield");
Expand Down Expand Up @@ -1741,11 +1741,11 @@ void ClassDB::_bind_methods() {

::ClassDB::bind_method(D_METHOD("is_class_enum_bitfield", "class", "enum", "no_inheritance"), &ClassDB::is_class_enum_bitfield, DEFVAL(false));

::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);

::ClassDB::bind_method(D_METHOD("class_has_struct", "class", "struct", "no_inheritance"), &ClassDB::class_has_struct, DEFVAL(false));
::ClassDB::bind_method(D_METHOD("class_get_struct_list", "class", "no_inheritance"), &ClassDB::class_get_struct_list, DEFVAL(false));
::ClassDB::bind_method(D_METHOD("class_get_struct_members", "class", "struct"), &ClassDB::class_get_struct_members);

::ClassDB::bind_method(D_METHOD("is_class_enabled", "class"), &ClassDB::is_class_enabled);
}

} // namespace special
Expand Down
Empty file added core/object/object.compat.inc
Empty file.
11 changes: 6 additions & 5 deletions core/object/property_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@

enum PropertyHint {
PROPERTY_HINT_NONE, ///< no hint provided.
PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians][,degrees][,exp][,suffix:<keyword>] range.
PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians_as_degrees][,degrees][,exp][,suffix:<keyword>] range.
PROPERTY_HINT_ENUM, ///< hint_text= "val1,val2,val3,etc"
PROPERTY_HINT_ENUM_SUGGESTION, ///< hint_text= "val1,val2,val3,etc"
PROPERTY_HINT_EXP_EASING, /// exponential easing function (Math::ease) use "attenuation" hint string to revert (flip h), "positive_only" to exclude in-out and out-in. (ie: "attenuation,positive_only")
Expand All @@ -60,12 +60,12 @@ enum PropertyHint {
PROPERTY_HINT_COLOR_NO_ALPHA, ///< used for ignoring alpha component when editing a color
PROPERTY_HINT_OBJECT_ID,
PROPERTY_HINT_TYPE_STRING, ///< a type string, the hint is the base type to choose
PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, ///< so something else can provide this (used in scripts)
PROPERTY_HINT_NODE_PATH_TO_EDITED_NODE, // Deprecated.
PROPERTY_HINT_OBJECT_TOO_BIG, ///< object is too big to send
PROPERTY_HINT_NODE_PATH_VALID_TYPES,
PROPERTY_HINT_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
PROPERTY_HINT_GLOBAL_SAVE_FILE, ///< a file path must be passed, hint_text (optionally) is a filter "*.png,*.wav,*.doc,". This opens a save dialog
PROPERTY_HINT_INT_IS_OBJECTID,
PROPERTY_HINT_INT_IS_OBJECTID, // Deprecated.
PROPERTY_HINT_INT_IS_POINTER,
PROPERTY_HINT_ARRAY_TYPE,
PROPERTY_HINT_LOCALE_ID,
Expand All @@ -74,6 +74,7 @@ enum PropertyHint {
PROPERTY_HINT_HIDE_QUATERNION_EDIT, /// Only Node3D::transform should hide the quaternion editor.
PROPERTY_HINT_PASSWORD,
PROPERTY_HINT_LAYERS_AVOIDANCE,
PROPERTY_HINT_DICTIONARY_TYPE,
PROPERTY_HINT_MAX,
};

Expand All @@ -93,7 +94,7 @@ enum PropertyUsageFlags {
PROPERTY_USAGE_SCRIPT_VARIABLE = 1 << 12,
PROPERTY_USAGE_STORE_IF_NULL = 1 << 13,
PROPERTY_USAGE_UPDATE_ALL_IF_MODIFIED = 1 << 14,
PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 15,
PROPERTY_USAGE_SCRIPT_DEFAULT_VALUE = 1 << 15, // Deprecated.
PROPERTY_USAGE_CLASS_IS_ENUM = 1 << 16,
PROPERTY_USAGE_NIL_IS_VARIANT = 1 << 17,
PROPERTY_USAGE_ARRAY = 1 << 18, // Used in the inspector to group properties as elements of an array.
Expand All @@ -103,7 +104,7 @@ enum PropertyUsageFlags {
PROPERTY_USAGE_NODE_PATH_FROM_SCENE_ROOT = 1 << 22,
PROPERTY_USAGE_RESOURCE_NOT_PERSISTENT = 1 << 23,
PROPERTY_USAGE_KEYING_INCREMENTS = 1 << 24, // Used in inspector to increment property when keyed in animation player.
PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 25, // when loading, the resource for this property can be set at the end of loading.
PROPERTY_USAGE_DEFERRED_SET_RESOURCE = 1 << 25, // Deprecated.
PROPERTY_USAGE_EDITOR_INSTANTIATE_OBJECT = 1 << 26, // For Object properties, instantiate them when creating in editor.
PROPERTY_USAGE_EDITOR_BASIC_SETTING = 1 << 27, //for project or editor settings, show when basic settings are selected.
PROPERTY_USAGE_READ_ONLY = 1 << 28, // Mark a property as read-only in the inspector.
Expand Down
12 changes: 9 additions & 3 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,7 +564,14 @@ Array Array::duplicate(bool p_deep) const {

Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
Array new_arr;
new_arr._p->typed = _p->typed;
if (const StructInfo *struct_info = get_struct_info()) {
new_arr.set_struct(*struct_info, is_array_of_structs());
} else {
new_arr._p->typed = _p->typed;
if (p_deep) {
new_arr.resize(size());
}
}

if (recursion_count > MAX_RECURSION) {
ERR_PRINT("Max recursion reached");
Expand All @@ -574,7 +581,6 @@ Array Array::recursive_duplicate(bool p_deep, int recursion_count) const {
if (p_deep) {
recursion_count++;
int element_count = size();
new_arr.resize(element_count);
for (int i = 0; i < element_count; i++) {
new_arr[i] = get(i).recursive_duplicate(true, recursion_count);
}
Expand Down Expand Up @@ -1040,7 +1046,7 @@ Array::Array(const StructInfo &p_struct_info, bool is_array_of_structs) {
if (!is_array_of_structs) {
Variant *pw = _p->array.ptrw();
for (int32_t i = 0; i < p_struct_info.count; i++) {
pw[i] = p_struct_info.default_values[i];
pw[i] = p_struct_info.default_values[i].duplicate(true);
}
}
}
Expand Down
72 changes: 37 additions & 35 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,9 @@ Variant *Dictionary::getptr(const Variant &p_key) {
}

Variant Dictionary::get_valid(const Variant &p_key) const {
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get_valid"), Variant());
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(key));
ValidatedVariant validated = _p->typed_key.validate(p_key, "get_valid");
ERR_FAIL_COND_V(!validated.valid, Variant());
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::ConstIterator E(_p->variant_map.find(validated.value));

if (!E) {
return Variant();
Expand All @@ -164,9 +164,9 @@ Variant Dictionary::get_valid(const Variant &p_key) const {
}

Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
const Variant *result = getptr(key);
ValidatedVariant validated = _p->typed_key.validate(p_key, "get");
ERR_FAIL_COND_V(!validated.valid, p_default);
const Variant *result = getptr(validated.value);
if (!result) {
return p_default;
}
Expand All @@ -175,16 +175,18 @@ Variant Dictionary::get(const Variant &p_key, const Variant &p_default) const {
}

Variant Dictionary::get_or_add(const Variant &p_key, const Variant &p_default) {
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "get"), p_default);
const Variant *result = getptr(key);
if (!result) {
Variant value = p_default;
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "add"), value);
operator[](key) = value;
return value;
ValidatedVariant validated_key = _p->typed_key.validate(p_key, "get");
ERR_FAIL_COND_V(!validated_key.valid, p_default);
const Variant *result = getptr(validated_key.value);

if (result) {
return *result;
}
return *result;

ValidatedVariant validated_default = _p->typed_key.validate(p_default, "add");
ERR_FAIL_COND_V(!validated_default.valid, validated_default.value);
operator[](validated_key.value) = validated_default.value;
return validated_default.value;
}

bool Dictionary::set(const Variant &p_key, const Variant &p_value) {
Expand All @@ -206,38 +208,38 @@ bool Dictionary::is_empty() const {
}

bool Dictionary::has(const Variant &p_key) const {
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has'"), false);
ValidatedVariant validated = _p->typed_key.validate(p_key, "use 'has'");
ERR_FAIL_COND_V(!validated.valid, false);
return _p->variant_map.has(p_key);
}

bool Dictionary::has_all(const Array &p_keys) const {
for (int i = 0; i < p_keys.size(); i++) {
Variant key = p_keys[i];
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "use 'has_all'"), false);
if (!has(key)) {
ValidatedVariant validated = _p->typed_key.validate(p_keys[i], "use 'has_all'");
ERR_FAIL_COND_V(!validated.valid, false);
if (!has(validated.value)) {
return false;
}
}
return true;
}

Variant Dictionary::find_key(const Variant &p_value) const {
Variant value = p_value;
ERR_FAIL_COND_V(!_p->typed_value.validate(value, "find_key"), Variant());
ValidatedVariant validated = _p->typed_key.validate(p_value, "find_key");
ERR_FAIL_COND_V(!validated.valid, Variant());
for (const KeyValue<Variant, Variant> &E : _p->variant_map) {
if (E.value == value) {
if (E.value == validated.value) {
return E.key;
}
}
return Variant();
}

bool Dictionary::erase(const Variant &p_key) {
Variant key = p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "erase"), false);
ValidatedVariant validated = _p->typed_key.validate(p_key, "erase");
ERR_FAIL_COND_V(!validated.valid, false);
ERR_FAIL_COND_V_MSG(_p->read_only, false, "Dictionary is in read-only state.");
return _p->variant_map.erase(key);
return _p->variant_map.erase(validated.value);
}

bool Dictionary::operator==(const Dictionary &p_dictionary) const {
Expand Down Expand Up @@ -297,12 +299,12 @@ void Dictionary::clear() {
void Dictionary::merge(const Dictionary &p_dictionary, bool p_overwrite) {
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
for (const KeyValue<Variant, Variant> &E : p_dictionary._p->variant_map) {
Variant key = E.key;
Variant value = E.value;
ERR_FAIL_COND(!_p->typed_key.validate(key, "merge"));
ERR_FAIL_COND(!_p->typed_value.validate(value, "merge"));
if (p_overwrite || !has(key)) {
operator[](key) = value;
ValidatedVariant validated_key = _p->typed_key.validate(E.key, "merge");
ERR_FAIL_COND(!validated_key.valid);
ValidatedVariant validated_value = _p->typed_key.validate(E.value, "merge");
ERR_FAIL_COND(!validated_value.valid);
if (p_overwrite || !has(validated_key.value)) {
operator[](validated_key.value) = validated_value.value;
}
}
}
Expand Down Expand Up @@ -536,9 +538,9 @@ const Variant *Dictionary::next(const Variant *p_key) const {
}
return nullptr;
}
Variant key = *p_key;
ERR_FAIL_COND_V(!_p->typed_key.validate(key, "next"), nullptr);
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(key);
ValidatedVariant validated = _p->typed_key.validate(*p_key, "next");
ERR_FAIL_COND_V(!validated.valid, nullptr);
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator>::Iterator E = _p->variant_map.find(validated.value);

if (!E) {
return nullptr;
Expand Down
42 changes: 0 additions & 42 deletions core/variant/struct.cpp

This file was deleted.

33 changes: 24 additions & 9 deletions core/variant/struct_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,33 @@ struct StructInfo {
Vector<Variant> default_values;

StructInfo(){};
StructInfo(const StringName &p_name, const int32_t p_count, const Vector<StringName> &p_names, const Vector<Variant::Type> &p_types, const Vector<StringName> &p_class_names, const Vector<const StructInfo *> &p_struct_member_infos, const Vector<Variant> &p_default_values) {
name = p_name;
count = p_count;
names = p_names;
types = p_types;
class_names = p_class_names;
struct_member_infos = p_struct_member_infos;
default_values = p_default_values;
};
StructInfo(const StringName &p_name, const int32_t p_count) :
name(p_name), count(p_count) {
names.resize(p_count);
types.resize(p_count);
class_names.resize(p_count);
struct_member_infos.resize(p_count);
default_values.resize(p_count);
}
StructInfo(const StringName &p_name, const int32_t p_count, const Vector<StringName> &p_names, const Vector<Variant::Type> &p_types, const Vector<StringName> &p_class_names, const Vector<const StructInfo *> &p_struct_member_infos, const Vector<Variant> &p_default_values) :
name(p_name),
count(p_count),
names(p_names),
types(p_types),
class_names(p_class_names),
struct_member_infos(p_struct_member_infos),
default_values(p_default_values){};

Dictionary to_dict() const;

_FORCE_INLINE_ void set(int32_t p_index, const StringName &p_name, const Variant::Type &p_type, const StringName &p_class_name, const StructInfo *p_struct_member_info, const Variant &p_default_value) {
names.write[p_index] = p_name;
types.write[p_index] = p_type;
class_names.write[p_index] = p_class_name;
struct_member_infos.write[p_index] = p_struct_member_info;
default_values.write[p_index] = p_default_value;
}

_FORCE_INLINE_ bool operator==(const StructInfo &p_struct_info) const {
return name == p_struct_info.name;
}
Expand Down
7 changes: 4 additions & 3 deletions tests/core/variant/test_struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ TEST_CASE("[Struct] PropertyInfo") {

List<PropertyInfo> list;
my_node->get_property_list(&list);
PropertyInfo info = list[0];
PropertyInfo info = list.get(0);

TypedArray<Struct<PropertyInfo>> property_list = my_node->call(SNAME("get_property_list_as_structs"));
Struct<PropertyInfo> prop = property_list[0];
Expand Down Expand Up @@ -226,6 +226,7 @@ TEST_CASE("[Struct] PropertyInfo") {
variant_prop.set_named(SNAME("oops"), SNAME("oh no"), valid);
CHECK_EQ(valid, false);
}
memdelete(my_node);
}

TEST_CASE("[Struct] Validation") {
Expand Down Expand Up @@ -282,8 +283,6 @@ TEST_CASE("[Struct] Validation") {
ERR_PRINT_ON;

named_int.assign(also_a_match);
bool matches = named_int == also_a_match;
print_line(matches);
CHECK_MESSAGE(named_int == also_a_match, "failed to assign an array with correct types using 'assign' function");
}
}
Expand Down Expand Up @@ -346,6 +345,8 @@ TEST_CASE("[Struct] Nesting") {
nested_struct.set_named("value", basic_struct_lookalike);
CHECK_EQ(nested_struct["value"], basic_struct);
ERR_PRINT_ON;

memdelete(node);
}

SUBCASE("Typed Array of Struct") {
Expand Down

0 comments on commit b295f0c

Please sign in to comment.