Files
2026-02-09 00:53:38 +09:00

208 lines
13 KiB
C++

#pragma once
#include "core/templates/vector.h"
#include "core/object/ref_counted.h"
#include "core/variant/variant.h"
#include "core/variant/typed_array.h"
template <typename T>
TypedArray<T> VectorToTypedArray(const Vector<Ref<T>>& vector)
{
TypedArray<T> arr;
arr.resize(vector.size());
for (int i{}; i < vector.size(); ++i)
{
arr[i] = Variant(vector[i]);
}
return arr;
}
template <typename T>
Vector<Ref<T>> TypedArrayToVector(const TypedArray<T>& arr)
{
Vector<Ref<T>> vector;
vector.resize(arr.size());
for (int i{}; i < vector.size(); ++i)
{
vector.set(i, Ref<T>(arr[i]));
}
return vector;
}
template <typename T>
TypedArray<T> VectorToTypedArrayVariant(const Vector<T>& vector)
{
TypedArray<T> arr;
arr.resize(vector.size());
for (int i{}; i < vector.size(); ++i)
{
arr[i] = Variant(vector[i]);
}
return arr;
}
template <typename T>
Vector<T> TypedArrayToVectorVariant(const TypedArray<T>& arr)
{
Vector<T> vector;
vector.resize(arr.size());
for (int i{}; i < vector.size(); ++i)
{
vector.set(i, arr[i]);
}
return vector;
}
template <typename To, typename From>
TypedArray<To> VectorToTypedArrayCast(const Vector<From>& vector)
{
TypedArray<To> arr;
arr.resize(vector.size());
for (int i{}; i < vector.size(); ++i)
{
arr[i] = Variant(static_cast<To>(vector[i]));
}
return arr;
}
// template <typename T>
// static Type get_type_t();
// template <> static Type get_type_t<bool>() { return Type::BOOL; }
// template <> static Type get_type_t<int>() { return Type::INT; }
// template <> static Type get_type_t<float>() { return Type::FLOAT; }
// template <> static Type get_type_t<String>() { return Type::STRING; }
// template <> static Type get_type_t<Vector2>() { return Type::VECTOR2; }
// template <> static Type get_type_t<Vector2i>() { return Type::VECTOR2I; }
// template <> static Type get_type_t<Rect2>() { return Type::RECT2; }
// template <> static Type get_type_t<Rect2i>() { return Type::RECT2I; }
// template <> static Type get_type_t<Vector3>() { return Type::VECTOR3; }
// template <> static Type get_type_t<Vector3i>() { return Type::VECTOR3I; }
// template <> static Type get_type_t<Transform2D>() { return Type::TRANSFORM2D; }
// template <> static Type get_type_t<Vector4>() { return Type::VECTOR4; }
// template <> static Type get_type_t<Vector4i>() { return Type::VECTOR4I; }
// template <> static Type get_type_t<Plane>() { return Type::PLANE; }
// template <> static Type get_type_t<Quaternion>() { return Type::QUATERNION; }
// template <> static Type get_type_t<::AABB>() { return Type::AABB; }
// template <> static Type get_type_t<Basis>() { return Type::BASIS; }
// template <> static Type get_type_t<Transform3D>() { return Type::TRANSFORM3D; }
// template <> static Type get_type_t<Projection>() { return Type::PROJECTION; }
// template <> static Type get_type_t<Color>() { return Type::COLOR; }
// template <> static Type get_type_t<StringName>() { return Type::STRING_NAME; }
// template <> static Type get_type_t<NodePath>() { return Type::NODE_PATH; }
// template <> static Type get_type_t<::RID>() { return Type::RID; }
// template <> static Type get_type_t<Object*>() { return Type::OBJECT; }
// template <> static Type get_type_t<Callable>() { return Type::CALLABLE; }
// template <> static Type get_type_t<Signal>() { return Type::SIGNAL; }
// template <> static Type get_type_t<Dictionary>() { return Type::DICTIONARY; }
// template <> static Type get_type_t<Array>() { return Type::ARRAY; }
// template <> static Type get_type_t<PackedByteArray>() { return Type::PACKED_BYTE_ARRAY; }
// template <> static Type get_type_t<PackedInt32Array>() { return Type::PACKED_INT32_ARRAY; }
// template <> static Type get_type_t<PackedInt64Array>() { return Type::PACKED_INT64_ARRAY; }
// template <> static Type get_type_t<PackedFloat32Array>() { return Type::PACKED_FLOAT32_ARRAY; }
// template <> static Type get_type_t<PackedFloat64Array>() { return Type::PACKED_FLOAT64_ARRAY; }
// template <> static Type get_type_t<PackedStringArray>() { return Type::PACKED_STRING_ARRAY; }
// template <> static Type get_type_t<PackedVector2Array>() { return Type::PACKED_VECTOR2_ARRAY; }
// template <> static Type get_type_t<PackedVector3Array>() { return Type::PACKED_VECTOR3_ARRAY; }
// template <> static Type get_type_t<PackedColorArray>() { return Type::PACKED_COLOR_ARRAY; }
// template <> static Type get_type_t<PackedVector4Array>() { return Type::PACKED_VECTOR4_ARRAY; }
// //template <typename T> static Type get_type_t() { return Type::NIL; }
// bool get_unsafe_bool() const { DEV_ASSERT(type == Type::BOOL); return _data._bool; }
// int get_unsafe_int() const { DEV_ASSERT(type == Type::INT); return _data._int; }
// float get_unsafe_float() const { DEV_ASSERT(type == Type::FLOAT); return _data._float; }
// String get_unsafe_string() const { DEV_ASSERT(type == Type::STRING); return *reinterpret_cast<const String *>(_data._mem); }
// Vector2 get_unsafe_vector2() const { DEV_ASSERT(type == Type::VECTOR2); return *reinterpret_cast<const Vector2 *>(_data._mem); }
// Vector2i get_unsafe_vector2i() const { DEV_ASSERT(type == Type::VECTOR2I); return *reinterpret_cast<const Vector2i *>(_data._mem); }
// Vector3 get_unsafe_vector3() const { DEV_ASSERT(type == Type::VECTOR3); return *reinterpret_cast<const Vector3 *>(_data._mem); }
// Vector3i get_unsafe_vector3i() const { DEV_ASSERT(type == Type::VECTOR3I); return *reinterpret_cast<const Vector3i *>(_data._mem); }
// Transform2D get_unsafe_transform2d() const { DEV_ASSERT(type == Type::TRANSFORM2D); return *reinterpret_cast<const Transform2D *>(_data._mem); }
// Vector4 get_unsafe_vector4() const { DEV_ASSERT(type == Type::VECTOR4); return *reinterpret_cast<const Vector4 *>(_data._mem); }
// Vector4i get_unsafe_vector4i() const { DEV_ASSERT(type == Type::VECTOR4I); return *reinterpret_cast<const Vector4i *>(_data._mem); }
// Plane get_unsafe_plane() const { DEV_ASSERT(type == Type::PLANE); return *reinterpret_cast<const Plane *>(_data._mem); }
// Quaternion get_unsafe_quaternion() const { DEV_ASSERT(type == Type::QUATERNION); return *reinterpret_cast<const Quaternion *>(_data._mem); }
// ::AABB get_unsafe_aabb() const { DEV_ASSERT(type == Type::AABB); return *reinterpret_cast<const ::AABB *>(_data._mem); }
// Basis get_unsafe_basis() const { DEV_ASSERT(type == Type::BASIS); return *reinterpret_cast<const Basis *>(_data._mem); }
// Transform3D get_unsafe_transform3d() const { DEV_ASSERT(type == Type::TRANSFORM3D); return *reinterpret_cast<const Transform3D *>(_data._mem); }
// Projection get_unsafe_projection() const { DEV_ASSERT(type == Type::PROJECTION); return *reinterpret_cast<const Projection *>(_data._mem); }
// Color get_unsafe_color() const { DEV_ASSERT(type == Type::COLOR); return *reinterpret_cast<const Color *>(_data._mem); }
// StringName get_unsafe_string_name() const { DEV_ASSERT(type == Type::STRING_NAME); return *reinterpret_cast<const StringName *>(_data._mem); }
// NodePath get_unsafe_node_path() const { DEV_ASSERT(type == Type::NODE_PATH); return *reinterpret_cast<const NodePath *>(_data._mem); }
// ::RID get_unsafe_rid() const { DEV_ASSERT(type == Type::RID); return *reinterpret_cast<const ::RID *>(_data._mem); }
// Object* get_unsafe_object() const { DEV_ASSERT(type == Type::OBJECT); return reinterpret_cast<const ObjData *>(&_data._mem[0])->obj; }
// Callable get_unsafe_callable() const { DEV_ASSERT(type == Type::CALLABLE); return *reinterpret_cast<const Callable *>(_data._mem); }
// Signal get_unsafe_signal() const { DEV_ASSERT(type == Type::SIGNAL); return *reinterpret_cast<const Signal *>(_data._mem); }
// Dictionary get_unsafe_dictionary() const { DEV_ASSERT(type == Type::DICTIONARY); return *reinterpret_cast<const Dictionary *>(_data._mem); }
// Array get_unsafe_array() const { DEV_ASSERT(type == Type::ARRAY); return *reinterpret_cast<const Array *>(_data._mem); }
// PackedByteArray get_unsafe_packed_byte_array() const { DEV_ASSERT(type == Type::PACKED_BYTE_ARRAY); return static_cast<PackedArrayRef<uint8_t> *>(_data.packed_array)->array; }
// PackedInt32Array get_unsafe_packed_int32_array() const { DEV_ASSERT(type == Type::PACKED_INT32_ARRAY); return static_cast<PackedArrayRef<int32_t> *>(_data.packed_array)->array; }
// PackedInt64Array get_unsafe_packed_int64_array() const { DEV_ASSERT(type == Type::PACKED_INT64_ARRAY); return static_cast<PackedArrayRef<int64_t> *>(_data.packed_array)->array; }
// PackedFloat32Array get_unsafe_packed_float32_array() const { DEV_ASSERT(type == Type::PACKED_FLOAT32_ARRAY); return static_cast<PackedArrayRef<float> *>(_data.packed_array)->array; }
// PackedFloat64Array get_unsafe_packed_float64_array() const { DEV_ASSERT(type == Type::PACKED_FLOAT64_ARRAY); return static_cast<PackedArrayRef<double> *>(_data.packed_array)->array; }
// PackedStringArray get_unsafe_packed_string_array() const { DEV_ASSERT(type == Type::PACKED_STRING_ARRAY); return static_cast<PackedArrayRef<String> *>(_data.packed_array)->array; }
// PackedVector2Array get_unsafe_packed_vector2_array() const { DEV_ASSERT(type == Type::PACKED_VECTOR2_ARRAY); return static_cast<PackedArrayRef<Vector2> *>(_data.packed_array)->array; }
// PackedVector3Array get_unsafe_packed_vector3_array() const { DEV_ASSERT(type == Type::PACKED_VECTOR3_ARRAY); return static_cast<PackedArrayRef<Vector3> *>(_data.packed_array)->array; }
// PackedColorArray get_unsafe_packed_color_array() const { DEV_ASSERT(type == Type::PACKED_COLOR_ARRAY); return static_cast<PackedArrayRef<Color> *>(_data.packed_array)->array; }
// PackedVector4Array get_unsafe_packed_vector4_array() const { DEV_ASSERT(type == Type::PACKED_VECTOR4_ARRAY); return static_cast<PackedArrayRef<Vector4> *>(_data.packed_array)->array; }
// template <typename T>
// T get_unsafe_t() const;
// template <> bool get_unsafe_t<bool>() const { return get_unsafe_bool(); }
// template <> int get_unsafe_t<int>() const { return get_unsafe_int(); }
// template <> float get_unsafe_t<float>() const { return get_unsafe_float(); }
// template <> String get_unsafe_t<String>() const { return get_unsafe_string(); }
// template <> Vector2 get_unsafe_t<Vector2>() const { return get_unsafe_vector2(); }
// template <> Vector2i get_unsafe_t<Vector2i>() const { return get_unsafe_vector2i(); }
// template <> Vector3 get_unsafe_t<Vector3>() const { return get_unsafe_vector3(); }
// template <> Vector3i get_unsafe_t<Vector3i>() const { return get_unsafe_vector3i(); }
// template <> Transform2D get_unsafe_t<Transform2D>() const { return get_unsafe_transform2d(); }
// template <> Vector4 get_unsafe_t<Vector4>() const { return get_unsafe_vector4(); }
// template <> Vector4i get_unsafe_t<Vector4i>() const { return get_unsafe_vector4i(); }
// template <> Plane get_unsafe_t<Plane>() const { return get_unsafe_plane(); }
// template <> Quaternion get_unsafe_t<Quaternion>() const { return get_unsafe_quaternion(); }
// template <> ::AABB get_unsafe_t<::AABB>() const { return get_unsafe_aabb(); }
// template <> Basis get_unsafe_t<Basis>() const { return get_unsafe_basis(); }
// template <> Transform3D get_unsafe_t<Transform3D>() const { return get_unsafe_transform3d(); }
// template <> Projection get_unsafe_t<Projection>() const { return get_unsafe_projection(); }
// template <> Color get_unsafe_t<Color>() const { return get_unsafe_color(); }
// template <> StringName get_unsafe_t<StringName>() const { return get_unsafe_string_name(); }
// template <> NodePath get_unsafe_t<NodePath>() const { return get_unsafe_node_path(); }
// template <> ::RID get_unsafe_t<::RID>() const { return get_unsafe_rid(); }
// template <> Object* get_unsafe_t<Object*>() const { return get_unsafe_object(); }
// template <> Callable get_unsafe_t<Callable>() const { return get_unsafe_callable(); }
// template <> Signal get_unsafe_t<Signal>() const { return get_unsafe_signal(); }
// template <> Dictionary get_unsafe_t<Dictionary>() const { return get_unsafe_dictionary(); }
// template <> Array get_unsafe_t<Array>() const { return get_unsafe_array(); }
// template <> PackedByteArray get_unsafe_t<PackedByteArray>() const { return get_unsafe_packed_byte_array(); }
// template <> PackedInt32Array get_unsafe_t<PackedInt32Array>() const { return get_unsafe_packed_int32_array(); }
// template <> PackedInt64Array get_unsafe_t<PackedInt64Array>() const { return get_unsafe_packed_int64_array(); }
// template <> PackedFloat32Array get_unsafe_t<PackedFloat32Array>() const { return get_unsafe_packed_float32_array(); }
// template <> PackedFloat64Array get_unsafe_t<PackedFloat64Array>() const { return get_unsafe_packed_float64_array(); }
// template <> PackedStringArray get_unsafe_t<PackedStringArray>() const { return get_unsafe_packed_string_array(); }
// template <> PackedVector2Array get_unsafe_t<PackedVector2Array>() const { return get_unsafe_packed_vector2_array(); }
// template <> PackedVector3Array get_unsafe_t<PackedVector3Array>() const { return get_unsafe_packed_vector3_array(); }
// template <> PackedColorArray get_unsafe_t<PackedColorArray>() const { return get_unsafe_packed_color_array(); }
// template <> PackedVector4Array get_unsafe_t<PackedVector4Array>() const { return get_unsafe_packed_vector4_array(); }