summaryrefslogtreecommitdiff
path: root/core/variant
diff options
context:
space:
mode:
Diffstat (limited to 'core/variant')
-rw-r--r--core/variant/array.cpp10
-rw-r--r--core/variant/binder_common.h39
-rw-r--r--core/variant/callable.cpp53
-rw-r--r--core/variant/callable.h6
-rw-r--r--core/variant/callable_bind.cpp60
-rw-r--r--core/variant/callable_bind.h5
-rw-r--r--core/variant/dictionary.cpp10
-rw-r--r--core/variant/type_info.h9
-rw-r--r--core/variant/variant.cpp92
-rw-r--r--core/variant/variant_call.cpp3
10 files changed, 221 insertions, 66 deletions
diff --git a/core/variant/array.cpp b/core/variant/array.cpp
index 0fecc2fe94..f8af78f3c1 100644
--- a/core/variant/array.cpp
+++ b/core/variant/array.cpp
@@ -54,16 +54,6 @@ void Array::_ref(const Array &p_from) const {
ERR_FAIL_COND(!_fp); // should NOT happen.
- if (unlikely(_fp->read_only != nullptr)) {
- // If p_from is a read-only array, just copy the contents to avoid further modification.
- _unref();
- _p = memnew(ArrayPrivate);
- _p->refcount.init();
- _p->array = _fp->array;
- _p->typed = _fp->typed;
- return;
- }
-
if (_fp == _p) {
return; // whatever it is, nothing to do here move along
}
diff --git a/core/variant/binder_common.h b/core/variant/binder_common.h
index 358b3df0c5..81ac5adba7 100644
--- a/core/variant/binder_common.h
+++ b/core/variant/binder_common.h
@@ -146,22 +146,53 @@ VARIANT_ENUM_CAST(Side);
VARIANT_ENUM_CAST(ClockDirection);
VARIANT_ENUM_CAST(Corner);
VARIANT_ENUM_CAST(HatDir);
-VARIANT_ENUM_CAST(HatMask);
+VARIANT_BITFIELD_CAST(HatMask);
VARIANT_ENUM_CAST(JoyAxis);
VARIANT_ENUM_CAST(JoyButton);
-VARIANT_ENUM_CAST(Key);
-VARIANT_ENUM_CAST(KeyModifierMask);
+
VARIANT_ENUM_CAST(MIDIMessage);
VARIANT_ENUM_CAST(MouseButton);
+VARIANT_BITFIELD_CAST(MouseButtonMask);
VARIANT_ENUM_CAST(Orientation);
VARIANT_ENUM_CAST(HorizontalAlignment);
VARIANT_ENUM_CAST(VerticalAlignment);
VARIANT_ENUM_CAST(InlineAlignment);
VARIANT_ENUM_CAST(PropertyHint);
-VARIANT_ENUM_CAST(PropertyUsageFlags);
+VARIANT_BITFIELD_CAST(PropertyUsageFlags);
VARIANT_ENUM_CAST(Variant::Type);
VARIANT_ENUM_CAST(Variant::Operator);
+// Key
+
+VARIANT_ENUM_CAST(Key);
+VARIANT_BITFIELD_CAST(KeyModifierMask);
+
+static inline Key &operator|=(Key &a, BitField<KeyModifierMask> b) {
+ a = static_cast<Key>(static_cast<int>(a) | static_cast<int>(b.operator int64_t()));
+ return a;
+}
+
+static inline Key &operator&=(Key &a, BitField<KeyModifierMask> b) {
+ a = static_cast<Key>(static_cast<int>(a) & static_cast<int>(b.operator int64_t()));
+ return a;
+}
+
+static inline Key operator|(Key a, BitField<KeyModifierMask> b) {
+ return (Key)((int)a | (int)b.operator int64_t());
+}
+
+static inline Key operator&(Key a, BitField<KeyModifierMask> b) {
+ return (Key)((int)a & (int)b.operator int64_t());
+}
+
+static inline Key operator+(BitField<KeyModifierMask> a, Key b) {
+ return (Key)((int)a.operator int64_t() + (int)b);
+}
+
+static inline Key operator|(BitField<KeyModifierMask> a, Key b) {
+ return (Key)((int)a.operator int64_t() | (int)b);
+}
+
template <>
struct VariantCaster<char32_t> {
static _FORCE_INLINE_ char32_t cast(const Variant &p_variant) {
diff --git a/core/variant/callable.cpp b/core/variant/callable.cpp
index ba3fc536d7..2f2acc55a6 100644
--- a/core/variant/callable.cpp
+++ b/core/variant/callable.cpp
@@ -102,7 +102,22 @@ Callable Callable::bindp(const Variant **p_arguments, int p_argcount) const {
}
return Callable(memnew(CallableCustomBind(*this, args)));
}
+
+Callable Callable::bindv(const Array &p_arguments) {
+ if (p_arguments.is_empty()) {
+ return *this; // No point in creating a new callable if nothing is bound.
+ }
+
+ Vector<Variant> args;
+ args.resize(p_arguments.size());
+ for (int i = 0; i < p_arguments.size(); i++) {
+ args.write[i] = p_arguments[i];
+ }
+ return Callable(memnew(CallableCustomBind(*this, args)));
+}
+
Callable Callable::unbind(int p_argcount) const {
+ ERR_FAIL_COND_V_MSG(p_argcount <= 0, Callable(*this), "Amount of unbind() arguments must be 1 or greater.");
return Callable(memnew(CallableCustomUnbind(*this, p_argcount)));
}
@@ -137,6 +152,35 @@ StringName Callable::get_method() const {
return method;
}
+int Callable::get_bound_arguments_count() const {
+ if (!is_null() && is_custom()) {
+ return custom->get_bound_arguments_count();
+ } else {
+ return 0;
+ }
+}
+
+void Callable::get_bound_arguments_ref(Vector<Variant> &r_arguments, int &r_argcount) const {
+ if (!is_null() && is_custom()) {
+ custom->get_bound_arguments(r_arguments, r_argcount);
+ } else {
+ r_arguments.clear();
+ r_argcount = 0;
+ }
+}
+
+Array Callable::get_bound_arguments() const {
+ Vector<Variant> arr;
+ int ac;
+ get_bound_arguments_ref(arr, ac);
+ Array ret;
+ ret.resize(arr.size());
+ for (int i = 0; i < arr.size(); i++) {
+ ret[i] = arr[i];
+ }
+ return ret;
+}
+
CallableCustom *Callable::get_custom() const {
ERR_FAIL_COND_V_MSG(!is_custom(), nullptr,
vformat("Can't get custom on non-CallableCustom \"%s\".", operator String()));
@@ -344,6 +388,15 @@ const Callable *CallableCustom::get_base_comparator() const {
return nullptr;
}
+int CallableCustom::get_bound_arguments_count() const {
+ return 0;
+}
+
+void CallableCustom::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
+ r_arguments = Vector<Variant>();
+ r_argcount = 0;
+}
+
CallableCustom::CallableCustom() {
ref_count.init();
}
diff --git a/core/variant/callable.h b/core/variant/callable.h
index c9d32afb61..0abbb64c0b 100644
--- a/core/variant/callable.h
+++ b/core/variant/callable.h
@@ -98,6 +98,7 @@ public:
template <typename... VarArgs>
Callable bind(VarArgs... p_args);
+ Callable bindv(const Array &p_arguments);
Callable bindp(const Variant **p_arguments, int p_argcount) const;
Callable unbind(int p_argcount) const;
@@ -106,6 +107,9 @@ public:
ObjectID get_object_id() const;
StringName get_method() const;
CallableCustom *get_custom() const;
+ int get_bound_arguments_count() const;
+ void get_bound_arguments_ref(Vector<Variant> &r_arguments, int &r_argcount) const; // Internal engine use, the exposed one is below.
+ Array get_bound_arguments() const;
uint32_t hash() const;
@@ -146,6 +150,8 @@ public:
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const = 0;
virtual Error rpc(int p_peer_id, const Variant **p_arguments, int p_argcount, Callable::CallError &r_call_error) const;
virtual const Callable *get_base_comparator() const;
+ virtual int get_bound_arguments_count() const;
+ virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const;
CallableCustom();
virtual ~CallableCustom() {}
diff --git a/core/variant/callable_bind.cpp b/core/variant/callable_bind.cpp
index 4713a49303..5be91c6e11 100644
--- a/core/variant/callable_bind.cpp
+++ b/core/variant/callable_bind.cpp
@@ -87,6 +87,47 @@ const Callable *CallableCustomBind::get_base_comparator() const {
return &callable;
}
+int CallableCustomBind::get_bound_arguments_count() const {
+ return callable.get_bound_arguments_count() + binds.size();
+}
+
+void CallableCustomBind::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
+ Vector<Variant> sub_args;
+ int sub_count;
+ callable.get_bound_arguments_ref(sub_args, sub_count);
+
+ if (sub_count == 0) {
+ r_arguments = binds;
+ r_argcount = binds.size();
+ return;
+ }
+
+ int new_count = sub_count + binds.size();
+ r_argcount = new_count;
+
+ if (new_count <= 0) {
+ // Removed more arguments than it adds.
+ r_arguments = Vector<Variant>();
+ return;
+ }
+
+ r_arguments.resize(new_count);
+
+ if (sub_count > 0) {
+ for (int i = 0; i < sub_count; i++) {
+ r_arguments.write[i] = sub_args[i];
+ }
+ for (int i = 0; i < binds.size(); i++) {
+ r_arguments.write[i + sub_count] = binds[i];
+ }
+ r_argcount = new_count;
+ } else {
+ for (int i = 0; i < binds.size() + sub_count; i++) {
+ r_arguments.write[i] = binds[i - sub_count];
+ }
+ }
+}
+
void CallableCustomBind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
const Variant **args = (const Variant **)alloca(sizeof(const Variant **) * (binds.size() + p_argcount));
for (int i = 0; i < p_argcount; i++) {
@@ -164,6 +205,25 @@ const Callable *CallableCustomUnbind::get_base_comparator() const {
return &callable;
}
+int CallableCustomUnbind::get_bound_arguments_count() const {
+ return callable.get_bound_arguments_count() - argcount;
+}
+
+void CallableCustomUnbind::get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const {
+ Vector<Variant> sub_args;
+ int sub_count;
+ callable.get_bound_arguments_ref(sub_args, sub_count);
+
+ r_argcount = sub_args.size() - argcount;
+
+ if (argcount >= sub_args.size()) {
+ r_arguments = Vector<Variant>();
+ } else {
+ sub_args.resize(sub_args.size() - argcount);
+ r_arguments = sub_args;
+ }
+}
+
void CallableCustomUnbind::call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const {
if (argcount > p_argcount) {
r_call_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
diff --git a/core/variant/callable_bind.h b/core/variant/callable_bind.h
index 2bdf7e4782..278ed335d0 100644
--- a/core/variant/callable_bind.h
+++ b/core/variant/callable_bind.h
@@ -51,7 +51,8 @@ public:
virtual ObjectID get_object() const override; //must always be able to provide an object
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;
virtual const Callable *get_base_comparator() const override;
-
+ virtual int get_bound_arguments_count() const override;
+ virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const override;
Callable get_callable() { return callable; }
Vector<Variant> get_binds() { return binds; }
@@ -76,6 +77,8 @@ public:
virtual ObjectID get_object() const override; //must always be able to provide an object
virtual void call(const Variant **p_arguments, int p_argcount, Variant &r_return_value, Callable::CallError &r_call_error) const override;
virtual const Callable *get_base_comparator() const override;
+ virtual int get_bound_arguments_count() const override;
+ virtual void get_bound_arguments(Vector<Variant> &r_arguments, int &r_argcount) const override;
Callable get_callable() { return callable; }
int get_unbinds() { return argcount; }
diff --git a/core/variant/dictionary.cpp b/core/variant/dictionary.cpp
index c545109bd8..f87064a0d1 100644
--- a/core/variant/dictionary.cpp
+++ b/core/variant/dictionary.cpp
@@ -211,16 +211,6 @@ bool Dictionary::recursive_equal(const Dictionary &p_dictionary, int recursion_c
}
void Dictionary::_ref(const Dictionary &p_from) const {
- if (unlikely(p_from._p->read_only != nullptr)) {
- // If p_from is a read-only dictionary, just copy the contents to avoid further modification.
- if (_p) {
- _unref();
- }
- _p = memnew(DictionaryPrivate);
- _p->refcount.init();
- _p->variant_map = p_from._p->variant_map;
- return;
- }
//make a copy first (thread safe)
if (!p_from._p->refcount.ref()) {
return; // couldn't copy
diff --git a/core/variant/type_info.h b/core/variant/type_info.h
index d3b507ba4d..e89658d25b 100644
--- a/core/variant/type_info.h
+++ b/core/variant/type_info.h
@@ -287,11 +287,14 @@ class BitField {
int64_t value = 0;
public:
- _FORCE_INLINE_ void set_flag(T p_flag) { value |= p_flag; }
- _FORCE_INLINE_ bool has_flag(T p_flag) const { return value & p_flag; }
- _FORCE_INLINE_ void clear_flag(T p_flag) { return value &= ~p_flag; }
+ _FORCE_INLINE_ void set_flag(T p_flag) { value |= (int64_t)p_flag; }
+ _FORCE_INLINE_ bool has_flag(T p_flag) const { return value & (int64_t)p_flag; }
+ _FORCE_INLINE_ bool is_empty() const { return value == 0; }
+ _FORCE_INLINE_ void clear_flag(T p_flag) { value &= ~(int64_t)p_flag; }
+ _FORCE_INLINE_ void clear() { value = 0; }
_FORCE_INLINE_ BitField() = default;
_FORCE_INLINE_ BitField(int64_t p_value) { value = p_value; }
+ _FORCE_INLINE_ BitField(T p_value) { value = (int64_t)p_value; }
_FORCE_INLINE_ operator int64_t() const { return value; }
_FORCE_INLINE_ operator Variant() const { return value; }
};
diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp
index 7039a5d9c4..ca42738b05 100644
--- a/core/variant/variant.cpp
+++ b/core/variant/variant.cpp
@@ -3240,33 +3240,34 @@ uint32_t Variant::recursive_hash(int recursion_count) const {
}
#define hash_compare_scalar(p_lhs, p_rhs) \
- ((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs))
-
-#define hash_compare_vector2(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
- (hash_compare_scalar((p_lhs).y, (p_rhs).y))
-
-#define hash_compare_vector3(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
- (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
- (hash_compare_scalar((p_lhs).z, (p_rhs).z))
-#define hash_compare_vector4(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
- (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
- (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
- (hash_compare_scalar((p_lhs).w, (p_rhs).w))
-
-#define hash_compare_quaternion(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).x, (p_rhs).x)) && \
- (hash_compare_scalar((p_lhs).y, (p_rhs).y)) && \
- (hash_compare_scalar((p_lhs).z, (p_rhs).z)) && \
- (hash_compare_scalar((p_lhs).w, (p_rhs).w))
-
-#define hash_compare_color(p_lhs, p_rhs) \
- (hash_compare_scalar((p_lhs).r, (p_rhs).r)) && \
- (hash_compare_scalar((p_lhs).g, (p_rhs).g)) && \
- (hash_compare_scalar((p_lhs).b, (p_rhs).b)) && \
- (hash_compare_scalar((p_lhs).a, (p_rhs).a))
+ (((p_lhs) == (p_rhs)) || (Math::is_nan(p_lhs) && Math::is_nan(p_rhs)))
+
+#define hash_compare_vector2(p_lhs, p_rhs) \
+ (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
+ hash_compare_scalar((p_lhs).y, (p_rhs).y))
+
+#define hash_compare_vector3(p_lhs, p_rhs) \
+ (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
+ hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
+ hash_compare_scalar((p_lhs).z, (p_rhs).z))
+
+#define hash_compare_vector4(p_lhs, p_rhs) \
+ (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
+ hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
+ hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
+ hash_compare_scalar((p_lhs).w, (p_rhs).w))
+
+#define hash_compare_quaternion(p_lhs, p_rhs) \
+ (hash_compare_scalar((p_lhs).x, (p_rhs).x) && \
+ hash_compare_scalar((p_lhs).y, (p_rhs).y) && \
+ hash_compare_scalar((p_lhs).z, (p_rhs).z) && \
+ hash_compare_scalar((p_lhs).w, (p_rhs).w))
+
+#define hash_compare_color(p_lhs, p_rhs) \
+ (hash_compare_scalar((p_lhs).r, (p_rhs).r) && \
+ hash_compare_scalar((p_lhs).g, (p_rhs).g) && \
+ hash_compare_scalar((p_lhs).b, (p_rhs).b) && \
+ hash_compare_scalar((p_lhs).a, (p_rhs).a))
#define hash_compare_packed_array(p_lhs, p_rhs, p_type, p_compare_func) \
const Vector<p_type> &l = PackedArrayRef<p_type>::get_array(p_lhs); \
@@ -3323,8 +3324,8 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
const Rect2 *l = reinterpret_cast<const Rect2 *>(_data._mem);
const Rect2 *r = reinterpret_cast<const Rect2 *>(p_variant._data._mem);
- return (hash_compare_vector2(l->position, r->position)) &&
- (hash_compare_vector2(l->size, r->size));
+ return hash_compare_vector2(l->position, r->position) &&
+ hash_compare_vector2(l->size, r->size);
} break;
case RECT2I: {
const Rect2i *l = reinterpret_cast<const Rect2i *>(_data._mem);
@@ -3338,7 +3339,7 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
Transform2D *r = p_variant._data._transform2d;
for (int i = 0; i < 3; i++) {
- if (!(hash_compare_vector2(l->columns[i], r->columns[i]))) {
+ if (!hash_compare_vector2(l->columns[i], r->columns[i])) {
return false;
}
}
@@ -3375,16 +3376,16 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
const Plane *l = reinterpret_cast<const Plane *>(_data._mem);
const Plane *r = reinterpret_cast<const Plane *>(p_variant._data._mem);
- return (hash_compare_vector3(l->normal, r->normal)) &&
- (hash_compare_scalar(l->d, r->d));
+ return hash_compare_vector3(l->normal, r->normal) &&
+ hash_compare_scalar(l->d, r->d);
} break;
case AABB: {
const ::AABB *l = _data._aabb;
const ::AABB *r = p_variant._data._aabb;
- return (hash_compare_vector3(l->position, r->position) &&
- (hash_compare_vector3(l->size, r->size)));
+ return hash_compare_vector3(l->position, r->position) &&
+ hash_compare_vector3(l->size, r->size);
} break;
@@ -3400,7 +3401,7 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
const Basis *r = p_variant._data._basis;
for (int i = 0; i < 3; i++) {
- if (!(hash_compare_vector3(l->rows[i], r->rows[i]))) {
+ if (!hash_compare_vector3(l->rows[i], r->rows[i])) {
return false;
}
}
@@ -3413,7 +3414,7 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
const Transform3D *r = p_variant._data._transform3d;
for (int i = 0; i < 3; i++) {
- if (!(hash_compare_vector3(l->basis.rows[i], r->basis.rows[i]))) {
+ if (!hash_compare_vector3(l->basis.rows[i], r->basis.rows[i])) {
return false;
}
}
@@ -3425,7 +3426,7 @@ bool Variant::hash_compare(const Variant &p_variant, int recursion_count) const
const Projection *r = p_variant._data._projection;
for (int i = 0; i < 4; i++) {
- if (!(hash_compare_vector4(l->columns[i], r->columns[i]))) {
+ if (!hash_compare_vector4(l->columns[i], r->columns[i])) {
return false;
}
}
@@ -3653,7 +3654,22 @@ String Variant::get_call_error_text(Object *p_base, const StringName &p_method,
}
String Variant::get_callable_error_text(const Callable &p_callable, const Variant **p_argptrs, int p_argcount, const Callable::CallError &ce) {
- return get_call_error_text(p_callable.get_object(), p_callable.get_method(), p_argptrs, p_argcount, ce);
+ Vector<Variant> binds;
+ int args_bound;
+ p_callable.get_bound_arguments_ref(binds, args_bound);
+ if (args_bound <= 0) {
+ return get_call_error_text(p_callable.get_object(), p_callable.get_method(), p_argptrs, MAX(0, p_argcount + args_bound), ce);
+ } else {
+ Vector<const Variant *> argptrs;
+ argptrs.resize(p_argcount + binds.size());
+ for (int i = 0; i < p_argcount; i++) {
+ argptrs.write[i] = p_argptrs[i];
+ }
+ for (int i = 0; i < binds.size(); i++) {
+ argptrs.write[i + p_argcount] = &binds[i];
+ }
+ return get_call_error_text(p_callable.get_object(), p_callable.get_method(), (const Variant **)argptrs.ptr(), argptrs.size(), ce);
+ }
}
void Variant::register_types() {
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index bc00bfca0f..05fb62ff12 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -2016,7 +2016,10 @@ static void _register_variant_builtin_methods() {
bind_method(Callable, get_object, sarray(), varray());
bind_method(Callable, get_object_id, sarray(), varray());
bind_method(Callable, get_method, sarray(), varray());
+ bind_method(Callable, get_bound_arguments_count, sarray(), varray());
+ bind_method(Callable, get_bound_arguments, sarray(), varray());
bind_method(Callable, hash, sarray(), varray());
+ bind_method(Callable, bindv, sarray("arguments"), varray());
bind_method(Callable, unbind, sarray("argcount"), varray());
bind_custom(Callable, call, _VariantCall::func_Callable_call, true, Variant);