Skip to content

Commit

Permalink
Core: Integrate VariantType in Array/Dictionary
Browse files Browse the repository at this point in the history
- Replaces `uint32_t`, which acted as a generic fallback
  • Loading branch information
Repiteo committed Feb 11, 2025
1 parent 213a5e7 commit 8e1eca1
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 39 deletions.
4 changes: 2 additions & 2 deletions core/doc_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@

String DocData::get_default_value_string(const Variant &p_value) {
if (p_value.get_type() == Variant::ARRAY) {
return Variant(Array(p_value, 0, StringName(), Variant())).get_construct_string().replace("\n", " ");
return Variant(Array(p_value, VariantType::NIL, StringName(), Variant())).get_construct_string().replace("\n", " ");
} else if (p_value.get_type() == Variant::DICTIONARY) {
return Variant(Dictionary(p_value, 0, StringName(), Variant(), 0, StringName(), Variant())).get_construct_string().replace("\n", " ");
return Variant(Dictionary(p_value, VariantType::NIL, StringName(), Variant(), VariantType::NIL, StringName(), Variant())).get_construct_string().replace("\n", " ");
} else {
return p_value.get_construct_string().replace("\n", " ");
}
Expand Down
4 changes: 2 additions & 2 deletions core/extension/gdextension_interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,7 @@ void gdextension_array_set_typed(GDExtensionTypePtr p_self, GDExtensionVariantTy
Array *self = reinterpret_cast<Array *>(p_self);
const StringName *class_name = reinterpret_cast<const StringName *>(p_class_name);
const Variant *script = reinterpret_cast<const Variant *>(p_script);
self->set_typed((uint32_t)p_type, *class_name, *script);
self->set_typed((VariantType)p_type, *class_name, *script);
}

/* Dictionary functions */
Expand All @@ -1299,7 +1299,7 @@ void gdextension_dictionary_set_typed(GDExtensionTypePtr p_self, GDExtensionVari
const Variant *key_script = reinterpret_cast<const Variant *>(p_key_script);
const StringName *value_class_name = reinterpret_cast<const StringName *>(p_value_class_name);
const Variant *value_script = reinterpret_cast<const Variant *>(p_value_script);
self->set_typed((uint32_t)p_key_type, *key_class_name, *key_script, (uint32_t)p_value_type, *value_class_name, *value_script);
self->set_typed((VariantType)p_key_type, *key_class_name, *key_script, (VariantType)p_value_type, *value_class_name, *value_script);
}

/* OBJECT API */
Expand Down
8 changes: 4 additions & 4 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -834,7 +834,7 @@ const void *Array::id() const {
return _p;
}

Array::Array(const Array &p_from, uint32_t p_type, const StringName &p_class_name, const Variant &p_script) {
Array::Array(const Array &p_from, VariantType p_type, const StringName &p_class_name, const Variant &p_script) {
_p = memnew(ArrayPrivate);
_p->refcount.init();
set_typed(p_type, p_class_name, p_script);
Expand All @@ -845,7 +845,7 @@ void Array::set_typed(const ContainerType &p_element_type) {
set_typed(p_element_type.builtin_type, p_element_type.class_name, p_element_type.script);
}

void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script) {
void Array::set_typed(VariantType p_type, const StringName &p_class_name, const Variant &p_script) {
ERR_FAIL_COND_MSG(_p->read_only, "Array is in read-only state.");
ERR_FAIL_COND_MSG(_p->array.size() > 0, "Type can only be set when array is empty.");
ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when array has no more than one user.");
Expand All @@ -854,7 +854,7 @@ void Array::set_typed(uint32_t p_type, const StringName &p_class_name, const Var
Ref<Script> script = p_script;
ERR_FAIL_COND_MSG(script.is_valid() && p_class_name == StringName(), "Script class can only be set together with base class name");

_p->typed.type = Variant::Type(p_type);
_p->typed.type = p_type;
_p->typed.class_name = p_class_name;
_p->typed.script = script;
_p->typed.where = "TypedArray";
Expand All @@ -880,7 +880,7 @@ ContainerType Array::get_element_type() const {
return type;
}

uint32_t Array::get_typed_builtin() const {
VariantType Array::get_typed_builtin() const {
return _p->typed.type;
}

Expand Down
7 changes: 4 additions & 3 deletions core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#define ARRAY_H

#include "core/typedefs.h"
#include "core/variant/variant_enums.h"

#include <climits>

Expand Down Expand Up @@ -187,22 +188,22 @@ class Array {
const void *id() const;

void set_typed(const ContainerType &p_element_type);
void set_typed(uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
void set_typed(VariantType p_type, const StringName &p_class_name, const Variant &p_script);

bool is_typed() const;
bool is_same_typed(const Array &p_other) const;
bool is_same_instance(const Array &p_other) const;

ContainerType get_element_type() const;
uint32_t get_typed_builtin() const;
VariantType get_typed_builtin() const;
StringName get_typed_class_name() const;
Variant get_typed_script() const;

void make_read_only();
bool is_read_only() const;
static Array create_read_only();

Array(const Array &p_base, uint32_t p_type, const StringName &p_class_name, const Variant &p_script);
Array(const Array &p_base, VariantType p_type, const StringName &p_class_name, const Variant &p_script);
Array(const Array &p_from);
Array();
~Array();
Expand Down
12 changes: 6 additions & 6 deletions core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ void Dictionary::set_typed(const ContainerType &p_key_type, const ContainerType
set_typed(p_key_type.builtin_type, p_key_type.class_name, p_key_type.script, p_value_type.builtin_type, p_value_type.class_name, p_key_type.script);
}

void Dictionary::set_typed(uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
void Dictionary::set_typed(VariantType p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, VariantType p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
ERR_FAIL_COND_MSG(_p->read_only, "Dictionary is in read-only state.");
ERR_FAIL_COND_MSG(_p->variant_map.size() > 0, "Type can only be set when dictionary is empty.");
ERR_FAIL_COND_MSG(_p->refcount.get() > 1, "Type can only be set when dictionary has no more than one user.");
Expand All @@ -610,12 +610,12 @@ void Dictionary::set_typed(uint32_t p_key_type, const StringName &p_key_class_na
Ref<Script> value_script = p_value_script;
ERR_FAIL_COND_MSG(value_script.is_valid() && p_value_class_name == StringName(), "Script class can only be set together with base class name.");

_p->typed_key.type = Variant::Type(p_key_type);
_p->typed_key.type = p_key_type;
_p->typed_key.class_name = p_key_class_name;
_p->typed_key.script = key_script;
_p->typed_key.where = "TypedDictionary.Key";

_p->typed_value.type = Variant::Type(p_value_type);
_p->typed_value.type = p_value_type;
_p->typed_value.class_name = p_value_class_name;
_p->typed_value.script = value_script;
_p->typed_value.where = "TypedDictionary.Value";
Expand Down Expand Up @@ -661,11 +661,11 @@ ContainerType Dictionary::get_value_type() const {
return type;
}

uint32_t Dictionary::get_typed_key_builtin() const {
VariantType Dictionary::get_typed_key_builtin() const {
return _p->typed_key.type;
}

uint32_t Dictionary::get_typed_value_builtin() const {
VariantType Dictionary::get_typed_value_builtin() const {
return _p->typed_value.type;
}

Expand Down Expand Up @@ -696,7 +696,7 @@ const void *Dictionary::id() const {
return _p;
}

Dictionary::Dictionary(const Dictionary &p_base, uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
Dictionary::Dictionary(const Dictionary &p_base, VariantType p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, VariantType p_value_type, const StringName &p_value_class_name, const Variant &p_value_script) {
_p = memnew(DictionaryPrivate);
_p->refcount.init();
set_typed(p_key_type, p_key_class_name, p_key_script, p_value_type, p_value_class_name, p_value_script);
Expand Down
8 changes: 4 additions & 4 deletions core/variant/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ class Dictionary {
Dictionary recursive_duplicate(bool p_deep, int recursion_count) const;

void set_typed(const ContainerType &p_key_type, const ContainerType &p_value_type);
void set_typed(uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);
void set_typed(VariantType p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, VariantType p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);

bool is_typed() const;
bool is_typed_key() const;
Expand All @@ -105,8 +105,8 @@ class Dictionary {

ContainerType get_key_type() const;
ContainerType get_value_type() const;
uint32_t get_typed_key_builtin() const;
uint32_t get_typed_value_builtin() const;
VariantType get_typed_key_builtin() const;
VariantType get_typed_value_builtin() const;
StringName get_typed_key_class_name() const;
StringName get_typed_value_class_name() const;
Variant get_typed_key_script() const;
Expand All @@ -117,7 +117,7 @@ class Dictionary {

const void *id() const;

Dictionary(const Dictionary &p_base, uint32_t p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, uint32_t p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);
Dictionary(const Dictionary &p_base, VariantType p_key_type, const StringName &p_key_class_name, const Variant &p_key_script, VariantType p_value_type, const StringName &p_value_class_name, const Variant &p_value_script);
Dictionary(const Dictionary &p_from);
Dictionary(std::initializer_list<KeyValue<Variant, Variant>> p_init);
Dictionary();
Expand Down
18 changes: 9 additions & 9 deletions core/variant/variant_construct.h
Original file line number Diff line number Diff line change
Expand Up @@ -438,28 +438,28 @@ class VariantConstructorTypedDictionary {
}

const Dictionary &base_dict = *VariantGetInternalPtr<Dictionary>::get_ptr(p_args[0]);
const uint32_t key_type = p_args[1]->operator uint32_t();
const VariantType key_type = VariantType(p_args[1]->operator uint32_t());
const StringName &key_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
const uint32_t value_type = p_args[4]->operator uint32_t();
const VariantType value_type = VariantType(p_args[4]->operator uint32_t());
const StringName &value_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[5]);
r_ret = Dictionary(base_dict, key_type, key_class_name, *p_args[3], value_type, value_class_name, *p_args[6]);
}

static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
const Dictionary &base_dict = *VariantGetInternalPtr<Dictionary>::get_ptr(p_args[0]);
const uint32_t key_type = p_args[1]->operator uint32_t();
const VariantType key_type = VariantType(p_args[1]->operator uint32_t());
const StringName &key_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
const uint32_t value_type = p_args[4]->operator uint32_t();
const VariantType value_type = VariantType(p_args[4]->operator uint32_t());
const StringName &value_class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[5]);
*r_ret = Dictionary(base_dict, key_type, key_class_name, *p_args[3], value_type, value_class_name, *p_args[6]);
}

static void ptr_construct(void *base, const void **p_args) {
const Dictionary &base_dict = PtrToArg<Dictionary>::convert(p_args[0]);
const uint32_t key_type = PtrToArg<uint32_t>::convert(p_args[1]);
const VariantType key_type = PtrToArg<VariantType>::convert(p_args[1]);
const StringName &key_class_name = PtrToArg<StringName>::convert(p_args[2]);
const Variant &key_script = PtrToArg<Variant>::convert(p_args[3]);
const uint32_t value_type = PtrToArg<uint32_t>::convert(p_args[4]);
const VariantType value_type = PtrToArg<VariantType>::convert(p_args[4]);
const StringName &value_class_name = PtrToArg<StringName>::convert(p_args[5]);
const Variant &value_script = PtrToArg<Variant>::convert(p_args[6]);
Dictionary dst_arr = Dictionary(base_dict, key_type, key_class_name, key_script, value_type, value_class_name, value_script);
Expand Down Expand Up @@ -530,20 +530,20 @@ class VariantConstructorTypedArray {
}

const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
const uint32_t type = p_args[1]->operator uint32_t();
const VariantType type = VariantType(p_args[1]->operator uint32_t());
r_ret = Array(base_arr, type, *p_args[2], *p_args[3]);
}

static inline void validated_construct(Variant *r_ret, const Variant **p_args) {
const Array &base_arr = *VariantGetInternalPtr<Array>::get_ptr(p_args[0]);
const uint32_t type = p_args[1]->operator uint32_t();
const VariantType type = VariantType(p_args[1]->operator uint32_t());
const StringName &class_name = *VariantGetInternalPtr<StringName>::get_ptr(p_args[2]);
*r_ret = Array(base_arr, type, class_name, *p_args[3]);
}

static void ptr_construct(void *base, const void **p_args) {
const Array &base_arr = PtrToArg<Array>::convert(p_args[0]);
const uint32_t type = PtrToArg<uint32_t>::convert(p_args[1]);
const VariantType type = PtrToArg<VariantType>::convert(p_args[1]);
const StringName &class_name = PtrToArg<StringName>::convert(p_args[2]);
const Variant &script = PtrToArg<Variant>::convert(p_args[3]);
Array dst_arr = Array(base_arr, type, class_name, script);
Expand Down
18 changes: 9 additions & 9 deletions modules/gdscript/gdscript_vm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -845,7 +845,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
bool result = false;
if (value->get_type() == Variant::ARRAY) {
Array *array = VariantInternal::get_array(value);
result = array->get_typed_builtin() == ((uint32_t)builtin_type) && array->get_typed_class_name() == native_type && array->get_typed_script() == *script_type;
result = array->get_typed_builtin() == builtin_type && array->get_typed_class_name() == native_type && array->get_typed_script() == *script_type;
}

*dst = result;
Expand Down Expand Up @@ -874,8 +874,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a
bool result = false;
if (value->get_type() == Variant::DICTIONARY) {
Dictionary *dictionary = VariantInternal::get_dictionary(value);
result = dictionary->get_typed_key_builtin() == ((uint32_t)key_builtin_type) && dictionary->get_typed_key_class_name() == key_native_type && dictionary->get_typed_key_script() == *key_script_type &&
dictionary->get_typed_value_builtin() == ((uint32_t)value_builtin_type) && dictionary->get_typed_value_class_name() == value_native_type && dictionary->get_typed_value_script() == *value_script_type;
result = dictionary->get_typed_key_builtin() == key_builtin_type && dictionary->get_typed_key_class_name() == key_native_type && dictionary->get_typed_key_script() == *key_script_type &&
dictionary->get_typed_value_builtin() == value_builtin_type && dictionary->get_typed_value_class_name() == value_native_type && dictionary->get_typed_value_script() == *value_script_type;
}

*dst = result;
Expand Down Expand Up @@ -1426,7 +1426,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a

Array *array = VariantInternal::get_array(src);

if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
if (array->get_typed_builtin() != builtin_type || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
#ifdef DEBUG_ENABLED
err_text = vformat(R"(Trying to assign an array of type "%s" to a variable of type "Array[%s]".)",
_get_var_type(src), _get_element_type(builtin_type, native_type, *script_type));
Expand Down Expand Up @@ -1468,8 +1468,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a

Dictionary *dictionary = VariantInternal::get_dictionary(src);

if (dictionary->get_typed_key_builtin() != ((uint32_t)key_builtin_type) || dictionary->get_typed_key_class_name() != key_native_type || dictionary->get_typed_key_script() != *key_script_type ||
dictionary->get_typed_value_builtin() != ((uint32_t)value_builtin_type) || dictionary->get_typed_value_class_name() != value_native_type || dictionary->get_typed_value_script() != *value_script_type) {
if (dictionary->get_typed_key_builtin() != key_builtin_type || dictionary->get_typed_key_class_name() != key_native_type || dictionary->get_typed_key_script() != *key_script_type ||
dictionary->get_typed_value_builtin() != value_builtin_type || dictionary->get_typed_value_class_name() != value_native_type || dictionary->get_typed_value_script() != *value_script_type) {
#ifdef DEBUG_ENABLED
err_text = vformat(R"(Trying to assign a dictionary of type "%s" to a variable of type "Dictionary[%s, %s]".)",
_get_var_type(src), _get_element_type(key_builtin_type, key_native_type, *key_script_type),
Expand Down Expand Up @@ -2780,7 +2780,7 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a

Array *array = VariantInternal::get_array(r);

if (array->get_typed_builtin() != ((uint32_t)builtin_type) || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
if (array->get_typed_builtin() != builtin_type || array->get_typed_class_name() != native_type || array->get_typed_script() != *script_type) {
#ifdef DEBUG_ENABLED
err_text = vformat(R"(Trying to return an array of type "%s" where expected return type is "Array[%s]".)",
_get_var_type(r), _get_element_type(builtin_type, native_type, *script_type));
Expand Down Expand Up @@ -2823,8 +2823,8 @@ Variant GDScriptFunction::call(GDScriptInstance *p_instance, const Variant **p_a

Dictionary *dictionary = VariantInternal::get_dictionary(r);

if (dictionary->get_typed_key_builtin() != ((uint32_t)key_builtin_type) || dictionary->get_typed_key_class_name() != key_native_type || dictionary->get_typed_key_script() != *key_script_type ||
dictionary->get_typed_value_builtin() != ((uint32_t)value_builtin_type) || dictionary->get_typed_value_class_name() != value_native_type || dictionary->get_typed_value_script() != *value_script_type) {
if (dictionary->get_typed_key_builtin() != key_builtin_type || dictionary->get_typed_key_class_name() != key_native_type || dictionary->get_typed_key_script() != *key_script_type ||
dictionary->get_typed_value_builtin() != value_builtin_type || dictionary->get_typed_value_class_name() != value_native_type || dictionary->get_typed_value_script() != *value_script_type) {
#ifdef DEBUG_ENABLED
err_text = vformat(R"(Trying to return a dictionary of type "%s" where expected return type is "Dictionary[%s, %s]".)",
_get_var_type(r), _get_element_type(key_builtin_type, key_native_type, *key_script_type),
Expand Down

0 comments on commit 8e1eca1

Please sign in to comment.