2023-01-10 15:14:30 +00:00
|
|
|
/**************************************************************************/
|
|
|
|
/* variant.hpp */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* This file is part of: */
|
|
|
|
/* GODOT ENGINE */
|
|
|
|
/* https://godotengine.org */
|
|
|
|
/**************************************************************************/
|
|
|
|
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
|
|
|
|
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
|
|
|
|
/* */
|
|
|
|
/* Permission is hereby granted, free of charge, to any person obtaining */
|
|
|
|
/* a copy of this software and associated documentation files (the */
|
|
|
|
/* "Software"), to deal in the Software without restriction, including */
|
|
|
|
/* without limitation the rights to use, copy, modify, merge, publish, */
|
|
|
|
/* distribute, sublicense, and/or sell copies of the Software, and to */
|
|
|
|
/* permit persons to whom the Software is furnished to do so, subject to */
|
|
|
|
/* the following conditions: */
|
|
|
|
/* */
|
|
|
|
/* The above copyright notice and this permission notice shall be */
|
|
|
|
/* included in all copies or substantial portions of the Software. */
|
|
|
|
/* */
|
|
|
|
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
|
|
|
|
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
|
|
|
|
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
|
|
|
|
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
|
|
|
|
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
|
|
|
|
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
|
|
|
|
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
|
|
|
|
/**************************************************************************/
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2022-10-09 06:47:07 +00:00
|
|
|
#ifndef GODOT_VARIANT_HPP
|
|
|
|
#define GODOT_VARIANT_HPP
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
#include <godot_cpp/core/defs.hpp>
|
|
|
|
|
|
|
|
#include <godot_cpp/variant/builtin_types.hpp>
|
|
|
|
#include <godot_cpp/variant/variant_size.hpp>
|
|
|
|
|
2022-12-13 23:40:17 +00:00
|
|
|
#include <gdextension_interface.h>
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
#include <array>
|
|
|
|
|
|
|
|
namespace godot {
|
|
|
|
|
2023-01-01 20:27:08 +00:00
|
|
|
class ObjectID;
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
class Variant {
|
|
|
|
uint8_t opaque[GODOT_CPP_VARIANT_SIZE]{ 0 };
|
2021-08-19 17:47:56 +00:00
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
friend class GDExtensionBinding;
|
|
|
|
friend class MethodBind;
|
|
|
|
|
|
|
|
static void init_bindings();
|
|
|
|
|
|
|
|
public:
|
|
|
|
enum Type {
|
|
|
|
NIL,
|
|
|
|
|
|
|
|
// atomic types
|
|
|
|
BOOL,
|
|
|
|
INT,
|
|
|
|
FLOAT,
|
|
|
|
STRING,
|
|
|
|
|
|
|
|
// math types
|
|
|
|
VECTOR2,
|
|
|
|
VECTOR2I,
|
|
|
|
RECT2,
|
|
|
|
RECT2I,
|
|
|
|
VECTOR3,
|
|
|
|
VECTOR3I,
|
|
|
|
TRANSFORM2D,
|
2022-07-20 20:49:08 +00:00
|
|
|
VECTOR4,
|
|
|
|
VECTOR4I,
|
2021-08-18 14:03:52 +00:00
|
|
|
PLANE,
|
|
|
|
QUATERNION,
|
|
|
|
AABB,
|
|
|
|
BASIS,
|
|
|
|
TRANSFORM3D,
|
2022-07-20 20:49:08 +00:00
|
|
|
PROJECTION,
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
// misc types
|
|
|
|
COLOR,
|
|
|
|
STRING_NAME,
|
|
|
|
NODE_PATH,
|
|
|
|
RID,
|
|
|
|
OBJECT,
|
|
|
|
CALLABLE,
|
|
|
|
SIGNAL,
|
|
|
|
DICTIONARY,
|
|
|
|
ARRAY,
|
|
|
|
|
|
|
|
// typed arrays
|
|
|
|
PACKED_BYTE_ARRAY,
|
|
|
|
PACKED_INT32_ARRAY,
|
|
|
|
PACKED_INT64_ARRAY,
|
|
|
|
PACKED_FLOAT32_ARRAY,
|
|
|
|
PACKED_FLOAT64_ARRAY,
|
|
|
|
PACKED_STRING_ARRAY,
|
|
|
|
PACKED_VECTOR2_ARRAY,
|
|
|
|
PACKED_VECTOR3_ARRAY,
|
|
|
|
PACKED_COLOR_ARRAY,
|
|
|
|
|
|
|
|
VARIANT_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
enum Operator {
|
2022-06-06 09:18:07 +00:00
|
|
|
// comparison
|
2021-08-18 14:03:52 +00:00
|
|
|
OP_EQUAL,
|
|
|
|
OP_NOT_EQUAL,
|
|
|
|
OP_LESS,
|
|
|
|
OP_LESS_EQUAL,
|
|
|
|
OP_GREATER,
|
|
|
|
OP_GREATER_EQUAL,
|
2022-06-06 09:18:07 +00:00
|
|
|
// mathematic
|
2021-08-18 14:03:52 +00:00
|
|
|
OP_ADD,
|
|
|
|
OP_SUBTRACT,
|
|
|
|
OP_MULTIPLY,
|
|
|
|
OP_DIVIDE,
|
|
|
|
OP_NEGATE,
|
|
|
|
OP_POSITIVE,
|
|
|
|
OP_MODULE,
|
2024-01-06 19:54:57 +00:00
|
|
|
OP_POWER,
|
2022-06-06 09:18:07 +00:00
|
|
|
// bitwise
|
2021-08-18 14:03:52 +00:00
|
|
|
OP_SHIFT_LEFT,
|
|
|
|
OP_SHIFT_RIGHT,
|
|
|
|
OP_BIT_AND,
|
|
|
|
OP_BIT_OR,
|
|
|
|
OP_BIT_XOR,
|
|
|
|
OP_BIT_NEGATE,
|
2022-06-06 09:18:07 +00:00
|
|
|
// logic
|
2021-08-18 14:03:52 +00:00
|
|
|
OP_AND,
|
|
|
|
OP_OR,
|
|
|
|
OP_XOR,
|
|
|
|
OP_NOT,
|
2022-06-06 09:18:07 +00:00
|
|
|
// containment
|
2021-08-18 14:03:52 +00:00
|
|
|
OP_IN,
|
|
|
|
OP_MAX
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2022-12-07 13:11:44 +00:00
|
|
|
static GDExtensionVariantFromTypeConstructorFunc from_type_constructor[VARIANT_MAX];
|
|
|
|
static GDExtensionTypeFromVariantConstructorFunc to_type_constructor[VARIANT_MAX];
|
2021-08-18 14:03:52 +00:00
|
|
|
|
|
|
|
public:
|
2024-02-01 20:22:26 +00:00
|
|
|
_FORCE_INLINE_ GDExtensionVariantPtr _native_ptr() const { return const_cast<uint8_t(*)[GODOT_CPP_VARIANT_SIZE]>(&opaque); }
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant();
|
|
|
|
Variant(std::nullptr_t n) :
|
|
|
|
Variant() {}
|
2022-12-07 13:11:44 +00:00
|
|
|
explicit Variant(GDExtensionConstVariantPtr native_ptr);
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant(const Variant &other);
|
|
|
|
Variant(Variant &&other);
|
|
|
|
Variant(bool v);
|
|
|
|
Variant(int64_t v);
|
|
|
|
Variant(int32_t v) :
|
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
2023-11-04 21:53:09 +00:00
|
|
|
Variant(int16_t v) :
|
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
|
|
|
Variant(int8_t v) :
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
|
|
|
Variant(uint64_t v) :
|
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
2023-11-04 21:53:09 +00:00
|
|
|
Variant(uint32_t v) :
|
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
|
|
|
Variant(uint16_t v) :
|
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
|
|
|
Variant(uint8_t v) :
|
|
|
|
Variant(static_cast<int64_t>(v)) {}
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant(double v);
|
|
|
|
Variant(float v) :
|
|
|
|
Variant((double)v) {}
|
|
|
|
Variant(const String &v);
|
|
|
|
Variant(const char *v) :
|
|
|
|
Variant(String(v)) {}
|
|
|
|
Variant(const char16_t *v) :
|
|
|
|
Variant(String(v)) {}
|
|
|
|
Variant(const char32_t *v) :
|
|
|
|
Variant(String(v)) {}
|
|
|
|
Variant(const wchar_t *v) :
|
|
|
|
Variant(String(v)) {}
|
|
|
|
Variant(const Vector2 &v);
|
|
|
|
Variant(const Vector2i &v);
|
|
|
|
Variant(const Rect2 &v);
|
|
|
|
Variant(const Rect2i &v);
|
|
|
|
Variant(const Vector3 &v);
|
|
|
|
Variant(const Vector3i &v);
|
|
|
|
Variant(const Transform2D &v);
|
2022-07-20 20:49:08 +00:00
|
|
|
Variant(const Vector4 &v);
|
|
|
|
Variant(const Vector4i &v);
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant(const Plane &v);
|
|
|
|
Variant(const Quaternion &v);
|
|
|
|
Variant(const godot::AABB &v);
|
|
|
|
Variant(const Basis &v);
|
|
|
|
Variant(const Transform3D &v);
|
2022-07-20 20:49:08 +00:00
|
|
|
Variant(const Projection &v);
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant(const Color &v);
|
|
|
|
Variant(const StringName &v);
|
|
|
|
Variant(const NodePath &v);
|
|
|
|
Variant(const godot::RID &v);
|
2023-01-01 20:27:08 +00:00
|
|
|
Variant(const ObjectID &v);
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant(const Object *v);
|
|
|
|
Variant(const Callable &v);
|
|
|
|
Variant(const Signal &v);
|
|
|
|
Variant(const Dictionary &v);
|
|
|
|
Variant(const Array &v);
|
|
|
|
Variant(const PackedByteArray &v);
|
|
|
|
Variant(const PackedInt32Array &v);
|
|
|
|
Variant(const PackedInt64Array &v);
|
|
|
|
Variant(const PackedFloat32Array &v);
|
|
|
|
Variant(const PackedFloat64Array &v);
|
|
|
|
Variant(const PackedStringArray &v);
|
|
|
|
Variant(const PackedVector2Array &v);
|
|
|
|
Variant(const PackedVector3Array &v);
|
|
|
|
Variant(const PackedColorArray &v);
|
|
|
|
~Variant();
|
|
|
|
|
|
|
|
operator bool() const;
|
|
|
|
operator int64_t() const;
|
|
|
|
operator int32_t() const;
|
2023-11-04 21:53:09 +00:00
|
|
|
operator int16_t() const;
|
|
|
|
operator int8_t() const;
|
2021-08-18 14:03:52 +00:00
|
|
|
operator uint64_t() const;
|
|
|
|
operator uint32_t() const;
|
2023-11-04 21:53:09 +00:00
|
|
|
operator uint16_t() const;
|
|
|
|
operator uint8_t() const;
|
2021-08-18 14:03:52 +00:00
|
|
|
operator double() const;
|
|
|
|
operator float() const;
|
|
|
|
operator String() const;
|
|
|
|
operator Vector2() const;
|
|
|
|
operator Vector2i() const;
|
|
|
|
operator Rect2() const;
|
|
|
|
operator Rect2i() const;
|
|
|
|
operator Vector3() const;
|
|
|
|
operator Vector3i() const;
|
|
|
|
operator Transform2D() const;
|
2022-07-20 20:49:08 +00:00
|
|
|
operator Vector4() const;
|
|
|
|
operator Vector4i() const;
|
2021-08-18 14:03:52 +00:00
|
|
|
operator Plane() const;
|
|
|
|
operator Quaternion() const;
|
|
|
|
operator godot::AABB() const;
|
|
|
|
operator Basis() const;
|
|
|
|
operator Transform3D() const;
|
2022-07-20 20:49:08 +00:00
|
|
|
operator Projection() const;
|
2021-08-18 14:03:52 +00:00
|
|
|
operator Color() const;
|
|
|
|
operator StringName() const;
|
|
|
|
operator NodePath() const;
|
|
|
|
operator godot::RID() const;
|
2023-01-01 20:27:08 +00:00
|
|
|
operator ObjectID() const;
|
2021-08-18 14:03:52 +00:00
|
|
|
operator Object *() const;
|
|
|
|
operator Callable() const;
|
|
|
|
operator Signal() const;
|
|
|
|
operator Dictionary() const;
|
|
|
|
operator Array() const;
|
|
|
|
operator PackedByteArray() const;
|
|
|
|
operator PackedInt32Array() const;
|
|
|
|
operator PackedInt64Array() const;
|
|
|
|
operator PackedFloat32Array() const;
|
|
|
|
operator PackedFloat64Array() const;
|
|
|
|
operator PackedStringArray() const;
|
|
|
|
operator PackedVector2Array() const;
|
|
|
|
operator PackedVector3Array() const;
|
|
|
|
operator PackedColorArray() const;
|
|
|
|
|
|
|
|
Variant &operator=(const Variant &other);
|
|
|
|
Variant &operator=(Variant &&other);
|
|
|
|
bool operator==(const Variant &other) const;
|
|
|
|
bool operator!=(const Variant &other) const;
|
|
|
|
bool operator<(const Variant &other) const;
|
|
|
|
|
2023-01-28 04:43:01 +00:00
|
|
|
void callp(const StringName &method, const Variant **args, int argcount, Variant &r_ret, GDExtensionCallError &r_error);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2024-03-10 21:02:43 +00:00
|
|
|
template <typename... Args>
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant call(const StringName &method, Args... args) {
|
2023-01-28 04:43:01 +00:00
|
|
|
std::array<Variant, sizeof...(args)> vargs = { args... };
|
|
|
|
std::array<const Variant *, sizeof...(args)> argptrs;
|
|
|
|
for (size_t i = 0; i < vargs.size(); i++) {
|
|
|
|
argptrs[i] = &vargs[i];
|
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant result;
|
2022-12-07 13:11:44 +00:00
|
|
|
GDExtensionCallError error;
|
2023-01-28 04:43:01 +00:00
|
|
|
callp(method, argptrs.data(), argptrs.size(), result, error);
|
2021-08-18 14:03:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2023-01-28 04:43:01 +00:00
|
|
|
static void callp_static(Variant::Type type, const StringName &method, const Variant **args, int argcount, Variant &r_ret, GDExtensionCallError &r_error);
|
2021-08-18 14:03:52 +00:00
|
|
|
|
2024-03-10 21:02:43 +00:00
|
|
|
template <typename... Args>
|
2021-08-18 14:03:52 +00:00
|
|
|
static Variant call_static(Variant::Type type, const StringName &method, Args... args) {
|
2023-01-28 04:43:01 +00:00
|
|
|
std::array<Variant, sizeof...(args)> vargs = { args... };
|
|
|
|
std::array<const Variant *, sizeof...(args)> argptrs;
|
|
|
|
for (size_t i = 0; i < vargs.size(); i++) {
|
|
|
|
argptrs[i] = &vargs[i];
|
|
|
|
}
|
2021-08-18 14:03:52 +00:00
|
|
|
Variant result;
|
2022-12-07 13:11:44 +00:00
|
|
|
GDExtensionCallError error;
|
2023-01-28 04:43:01 +00:00
|
|
|
callp_static(type, method, argptrs.data(), argptrs.size(), sizeof...(args), result, error);
|
2021-08-18 14:03:52 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void evaluate(const Operator &op, const Variant &a, const Variant &b, Variant &r_ret, bool &r_valid);
|
|
|
|
|
|
|
|
void set(const Variant &key, const Variant &value, bool *r_valid = nullptr);
|
|
|
|
void set_named(const StringName &name, const Variant &value, bool &r_valid);
|
|
|
|
void set_indexed(int64_t index, const Variant &value, bool &r_valid, bool &r_oob);
|
|
|
|
void set_keyed(const Variant &key, const Variant &value, bool &r_valid);
|
|
|
|
Variant get(const Variant &key, bool *r_valid = nullptr) const;
|
|
|
|
Variant get_named(const StringName &name, bool &r_valid) const;
|
|
|
|
Variant get_indexed(int64_t index, bool &r_valid, bool &r_oob) const;
|
|
|
|
Variant get_keyed(const Variant &key, bool &r_valid) const;
|
|
|
|
bool in(const Variant &index, bool *r_valid = nullptr) const;
|
|
|
|
|
|
|
|
bool iter_init(Variant &r_iter, bool &r_valid) const;
|
|
|
|
bool iter_next(Variant &r_iter, bool &r_valid) const;
|
|
|
|
Variant iter_get(const Variant &r_iter, bool &r_valid) const;
|
|
|
|
|
|
|
|
Variant::Type get_type() const;
|
|
|
|
bool has_method(const StringName &method) const;
|
|
|
|
bool has_key(const Variant &key, bool *r_valid = nullptr) const;
|
|
|
|
static bool has_member(Variant::Type type, const StringName &member);
|
|
|
|
|
2022-06-06 09:18:07 +00:00
|
|
|
uint32_t hash() const;
|
|
|
|
uint32_t recursive_hash(int recursion_count) const;
|
2021-08-18 14:03:52 +00:00
|
|
|
bool hash_compare(const Variant &variant) const;
|
|
|
|
bool booleanize() const;
|
|
|
|
String stringify() const;
|
|
|
|
Variant duplicate(bool deep = false) const;
|
|
|
|
static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);
|
|
|
|
static void interpolate(const Variant &a, const Variant &b, float c, Variant &r_dst);
|
|
|
|
|
|
|
|
static String get_type_name(Variant::Type type);
|
|
|
|
static bool can_convert(Variant::Type from, Variant::Type to);
|
|
|
|
static bool can_convert_strict(Variant::Type from, Variant::Type to);
|
|
|
|
|
|
|
|
void clear();
|
|
|
|
};
|
|
|
|
|
2022-06-06 09:18:07 +00:00
|
|
|
struct VariantHasher {
|
|
|
|
static _FORCE_INLINE_ uint32_t hash(const Variant &p_variant) { return p_variant.hash(); }
|
|
|
|
};
|
|
|
|
|
|
|
|
struct VariantComparator {
|
|
|
|
static _FORCE_INLINE_ bool compare(const Variant &p_lhs, const Variant &p_rhs) { return p_lhs.hash_compare(p_rhs); }
|
|
|
|
};
|
|
|
|
|
2022-11-28 12:47:55 +00:00
|
|
|
template <typename... VarArgs>
|
|
|
|
String vformat(const String &p_text, const VarArgs... p_args) {
|
|
|
|
Variant args[sizeof...(p_args) + 1] = { p_args..., Variant() }; // +1 makes sure zero sized arrays are also supported.
|
|
|
|
Array args_array;
|
|
|
|
args_array.resize(sizeof...(p_args));
|
|
|
|
for (uint32_t i = 0; i < sizeof...(p_args); i++) {
|
|
|
|
args_array[i] = args[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
return p_text % args_array;
|
|
|
|
}
|
|
|
|
|
2023-04-14 07:33:49 +00:00
|
|
|
#include <godot_cpp/variant/builtin_vararg_methods.hpp>
|
|
|
|
|
2023-12-19 10:17:20 +00:00
|
|
|
#ifdef REAL_T_IS_DOUBLE
|
|
|
|
using PackedRealArray = PackedFloat64Array;
|
|
|
|
#else
|
|
|
|
using PackedRealArray = PackedFloat32Array;
|
|
|
|
#endif // REAL_T_IS_DOUBLE
|
|
|
|
|
2021-08-18 14:03:52 +00:00
|
|
|
} // namespace godot
|
|
|
|
|
2022-10-09 06:47:07 +00:00
|
|
|
#endif // GODOT_VARIANT_HPP
|