Skip to content

Commit

Permalink
Core: Migrate typed container logic
Browse files Browse the repository at this point in the history
• Dedicated typed container files entirely removed
• Typed container classes now defined in their base headers; methods declared in `variant.h`
• Consolidate to a single template for both typed containers, thanks to `constexpr`
• Nest private containers in their base classes
  • Loading branch information
Repiteo committed Sep 15, 2024
1 parent 6681f25 commit c63cf08
Show file tree
Hide file tree
Showing 73 changed files with 311 additions and 703 deletions.
1 change: 0 additions & 1 deletion core/config/engine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "core/config/project_settings.h"
#include "core/donors.gen.h"
#include "core/license.gen.h"
#include "core/variant/typed_array.h"
#include "core/version.h"

void Engine::set_physics_ticks_per_second(int p_ips) {
Expand Down
3 changes: 0 additions & 3 deletions core/config/engine.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
#include "core/templates/list.h"
#include "core/templates/vector.h"

template <typename T>
class TypedArray;

class Engine {
public:
struct Singleton {
Expand Down
1 change: 0 additions & 1 deletion core/config/project_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
#include "core/object/script_language.h"
#include "core/os/keyboard.h"
#include "core/templates/rb_set.h"
#include "core/variant/typed_array.h"
#include "core/variant/variant_parser.h"
#include "core/version.h"

Expand Down
3 changes: 0 additions & 3 deletions core/config/project_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@

#include "core/object/class_db.h"

template <typename T>
class TypedArray;

class ProjectSettings : public Object {
GDCLASS(ProjectSettings, Object);
_THREAD_SAFE_CLASS_
Expand Down
1 change: 0 additions & 1 deletion core/core_bind.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
#include "core/math/geometry_3d.h"
#include "core/os/keyboard.h"
#include "core/os/thread_safe.h"
#include "core/variant/typed_array.h"

namespace core_bind {

Expand Down
2 changes: 0 additions & 2 deletions core/core_bind.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@
#include "core/templates/safe_refcount.h"

class MainLoop;
template <typename T>
class TypedArray;

namespace core_bind {

Expand Down
1 change: 0 additions & 1 deletion core/input/input.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
#include "core/os/keyboard.h"
#include "core/os/thread_safe.h"
#include "core/templates/rb_set.h"
#include "core/variant/typed_array.h"

class Input : public Object {
GDCLASS(Input, Object);
Expand Down
1 change: 0 additions & 1 deletion core/input/input_map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "core/input/input.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/variant/typed_array.h"

InputMap *InputMap::singleton = nullptr;

Expand Down
3 changes: 0 additions & 3 deletions core/input/input_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,6 @@
#include "core/object/object.h"
#include "core/templates/hash_map.h"

template <typename T>
class TypedArray;

class InputMap : public Object {
GDCLASS(InputMap, Object);

Expand Down
1 change: 0 additions & 1 deletion core/io/ip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@
#include "core/os/semaphore.h"
#include "core/os/thread.h"
#include "core/templates/hash_map.h"
#include "core/variant/typed_array.h"

/************* RESOLVER ******************/

Expand Down
3 changes: 0 additions & 3 deletions core/io/ip.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,6 @@
#include "core/io/ip_address.h"
#include "core/os/os.h"

template <typename T>
class TypedArray;

struct _IP_ResolverPrivate;

class IP : public Object {
Expand Down
2 changes: 0 additions & 2 deletions core/math/a_star_grid_2d.compat.inc
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@

#ifndef DISABLE_DEPRECATED

#include "core/variant/typed_array.h"

TypedArray<Vector2i> AStarGrid2D::_get_id_path_bind_compat_88047(const Vector2i &p_from_id, const Vector2i &p_to_id) {
return get_id_path(p_from_id, p_to_id, false);
}
Expand Down
2 changes: 0 additions & 2 deletions core/math/a_star_grid_2d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
#include "a_star_grid_2d.h"
#include "a_star_grid_2d.compat.inc"

#include "core/variant/typed_array.h"

static real_t heuristic_euclidean(const Vector2i &p_from, const Vector2i &p_to) {
real_t dx = (real_t)ABS(p_to.x - p_from.x);
real_t dy = (real_t)ABS(p_to.y - p_from.y);
Expand Down
1 change: 0 additions & 1 deletion core/object/object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
#include "core/string/print_string.h"
#include "core/string/translation_server.h"
#include "core/templates/local_vector.h"
#include "core/variant/typed_array.h"

#ifdef DEBUG_ENABLED

Expand Down
3 changes: 0 additions & 3 deletions core/object/object.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@
#include "core/variant/callable_bind.h"
#include "core/variant/variant.h"

template <typename T>
class TypedArray;

enum PropertyHint {
PROPERTY_HINT_NONE, ///< no hint provided.
PROPERTY_HINT_RANGE, ///< hint_text = "min,max[,step][,or_greater][,or_less][,hide_slider][,radians_as_degrees][,degrees][,exp][,suffix:<keyword>] range.
Expand Down
3 changes: 0 additions & 3 deletions core/object/script_language.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,8 @@
#include "core/templates/pair.h"
#include "core/templates/rb_map.h"
#include "core/templates/safe_refcount.h"
#include "core/variant/typed_array.h"

class ScriptLanguage;
template <typename T>
class TypedArray;

typedef void (*ScriptEditRequestFunction)(const String &p_path);

Expand Down
1 change: 0 additions & 1 deletion core/object/script_language_extension.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
#include "core/object/gdvirtual.gen.inc"
#include "core/object/script_language.h"
#include "core/variant/native_ptr.h"
#include "core/variant/typed_array.h"

class ScriptExtension : public Script {
GDCLASS(ScriptExtension, Script)
Expand Down
5 changes: 5 additions & 0 deletions core/typedefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ static_assert(__cplusplus >= 201703L);
#define _MKSTR(m_x) _STR(m_x)
#endif

// Allow comma separators within macros.
#ifndef _COMMA
#define _COMMA ,
#endif

// Should always inline no matter what.
#ifndef _ALWAYS_INLINE_
#if defined(__GNUC__)
Expand Down
3 changes: 1 addition & 2 deletions core/variant/array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,7 @@
#include "core/variant/dictionary.h"
#include "core/variant/variant.h"

class ArrayPrivate {
public:
struct Array::ArrayPrivate {
SafeRefCount refcount;
Vector<Variant> array;
Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
Expand Down
11 changes: 10 additions & 1 deletion core/variant/array.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@
#include <climits>

class Variant;
class ArrayPrivate;
class Object;
class StringName;
class Callable;

class Array {
struct ArrayPrivate;
mutable ArrayPrivate *_p;
void _unref() const;

Expand Down Expand Up @@ -201,4 +201,13 @@ class Array {
~Array();
};

template <typename T>
class TypedArray : public Array {
public:
_FORCE_INLINE_ void operator=(const Array &p_array);
_FORCE_INLINE_ TypedArray(const Variant &p_variant);
_FORCE_INLINE_ TypedArray(const Array &p_array);
_FORCE_INLINE_ TypedArray();
};

#endif // ARRAY_H
2 changes: 1 addition & 1 deletion core/variant/dictionary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
#include "core/variant/type_info.h"
#include "core/variant/variant_internal.h"

struct DictionaryPrivate {
struct Dictionary::DictionaryPrivate {
SafeRefCount refcount;
Variant *read_only = nullptr; // If enabled, a pointer is used to a temporary value that is used to return read-only values.
HashMap<Variant, Variant, VariantHasher, StringLikeVariantComparator> variant_map;
Expand Down
15 changes: 10 additions & 5 deletions core/variant/dictionary.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,11 @@
#ifndef DICTIONARY_H
#define DICTIONARY_H

#include "core/string/ustring.h"
#include "core/templates/list.h"
#include "core/variant/array.h"

class Variant;

struct DictionaryPrivate;

class Dictionary {
struct DictionaryPrivate;
mutable DictionaryPrivate *_p;

void _ref(const Dictionary &p_from) const;
Expand Down Expand Up @@ -114,4 +110,13 @@ class Dictionary {
~Dictionary();
};

template <typename K, typename V>
class TypedDictionary : public Dictionary {
public:
_FORCE_INLINE_ void operator=(const Dictionary &p_dictionary);
_FORCE_INLINE_ TypedDictionary(const Variant &p_variant);
_FORCE_INLINE_ TypedDictionary(const Dictionary &p_dictionary);
_FORCE_INLINE_ TypedDictionary();
};

#endif // DICTIONARY_H
46 changes: 46 additions & 0 deletions core/variant/method_ptrcall.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,52 @@ struct PtrToArg<ObjectID> {
}
};

// This is for typed containers.

template <typename T>
struct PtrToArg<TypedArray<T>> {
_FORCE_INLINE_ static TypedArray<T> convert(const void *p_ptr) {
return TypedArray<T>(*reinterpret_cast<const Array *>(p_ptr));
}
typedef Array EncodeT;
_FORCE_INLINE_ static void encode(TypedArray<T> p_val, void *p_ptr) {
*(Array *)p_ptr = p_val;
}
};

template <typename T>
struct PtrToArg<const TypedArray<T> &> {
_FORCE_INLINE_ static TypedArray<T> convert(const void *p_ptr) {
return TypedArray<T>(*reinterpret_cast<const Array *>(p_ptr));
}
typedef Array EncodeT;
_FORCE_INLINE_ static void encode(TypedArray<T> p_val, void *p_ptr) {
*(Array *)p_ptr = p_val;
}
};

template <typename K, typename V>
struct PtrToArg<TypedDictionary<K, V>> {
_FORCE_INLINE_ static TypedDictionary<K, V> convert(const void *p_ptr) {
return TypedDictionary<K, V>(*reinterpret_cast<const Dictionary *>(p_ptr));
}
typedef Dictionary EncodeT;
_FORCE_INLINE_ static void encode(TypedDictionary<K, V> p_val, void *p_ptr) {
*(Dictionary *)p_ptr = p_val;
}
};

template <typename K, typename V>
struct PtrToArg<const TypedDictionary<K, V> &> {
_FORCE_INLINE_ static TypedDictionary<K, V> convert(const void *p_ptr) {
return TypedDictionary<K, V>(*reinterpret_cast<const Dictionary *>(p_ptr));
}
typedef Dictionary EncodeT;
_FORCE_INLINE_ static void encode(TypedDictionary<K, V> p_val, void *p_ptr) {
*(Dictionary *)p_ptr = p_val;
}
};

// This is for the special cases used by Variant.

// No EncodeT because direct pointer conversion not possible.
Expand Down
75 changes: 74 additions & 1 deletion core/variant/type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
#ifndef TYPE_INFO_H
#define TYPE_INFO_H

#include "core/typedefs.h"
#include "core/object/object.h"

#include <type_traits>

Expand Down Expand Up @@ -148,6 +148,79 @@ MAKE_TYPE_INFO(PackedVector4Array, Variant::PACKED_VECTOR4_ARRAY)

MAKE_TYPE_INFO(IPAddress, Variant::STRING)

// Typed containers.
template <typename T>
struct GetTypeInfo<TypedArray<T>> {
static const Variant::Type VARIANT_TYPE = Variant::ARRAY;
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
static inline PropertyInfo get_class_info() {
String hint_string;
if constexpr (GTI::variant_type_v<T> == Variant::OBJECT) {
hint_string = T::get_class_static();
} else {
hint_string = Variant::get_type_name(GTI::variant_type_v<T>);
}
return PropertyInfo(VARIANT_TYPE, String(), PROPERTY_HINT_ARRAY_TYPE, hint_string);
}
};

template <typename T>
struct GetTypeInfo<const TypedArray<T> &> {
static const Variant::Type VARIANT_TYPE = Variant::ARRAY;
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
static inline PropertyInfo get_class_info() {
String hint_string;
if constexpr (GTI::variant_type_v<T> == Variant::OBJECT) {
hint_string = T::get_class_static();
} else {
hint_string = Variant::get_type_name(GTI::variant_type_v<T>);
}
return PropertyInfo(VARIANT_TYPE, String(), PROPERTY_HINT_ARRAY_TYPE, hint_string);
}
};

template <typename K, typename V>
struct GetTypeInfo<TypedDictionary<K, V>> {
static const Variant::Type VARIANT_TYPE = Variant::DICTIONARY;
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
static inline PropertyInfo get_class_info() {
String hint_key;
if constexpr (GTI::variant_type_v<K> == Variant::OBJECT) {
hint_key = K::get_class_static();
} else {
hint_key = GTI::variant_type_v<K> == Variant::NIL ? "Variant" : Variant::get_type_name(GTI::variant_type_v<K>);
}
String hint_value;
if constexpr (GTI::variant_type_v<V> == Variant::OBJECT) {
hint_value = V::get_class_static();
} else {
hint_value = GTI::variant_type_v<V> == Variant::NIL ? "Variant" : Variant::get_type_name(GTI::variant_type_v<V>);
}
return PropertyInfo(VARIANT_TYPE, String(), PROPERTY_HINT_DICTIONARY_TYPE, vformat("%s;%s", hint_key, hint_value));
}
};

template <typename K, typename V>
struct GetTypeInfo<const TypedDictionary<K, V> &> {
static const Variant::Type VARIANT_TYPE = Variant::DICTIONARY;
static const GodotTypeInfo::Metadata METADATA = GodotTypeInfo::METADATA_NONE;
static inline PropertyInfo get_class_info() {
String hint_key;
if constexpr (GTI::variant_type_v<K> == Variant::OBJECT) {
hint_key = K::get_class_static();
} else {
hint_key = GTI::variant_type_v<K> == Variant::NIL ? "Variant" : Variant::get_type_name(GTI::variant_type_v<K>);
}
String hint_value;
if constexpr (GTI::variant_type_v<V> == Variant::OBJECT) {
hint_value = V::get_class_static();
} else {
hint_value = GTI::variant_type_v<V> == Variant::NIL ? "Variant" : Variant::get_type_name(GTI::variant_type_v<V>);
}
return PropertyInfo(VARIANT_TYPE, String(), PROPERTY_HINT_DICTIONARY_TYPE, vformat("%s;%s", hint_key, hint_value));
}
};

//objectID
template <>
struct GetTypeInfo<ObjectID> {
Expand Down
Loading

0 comments on commit c63cf08

Please sign in to comment.