summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorHein-Pieter van Braam <hp@tmm.cx>2017-09-18 20:02:47 +0200
committerHein-Pieter van Braam <hp@tmm.cx>2017-09-19 18:55:31 +0200
commit833c3917b247baa46f5a5f6ad6ce478cffc1911d (patch)
tree0f25e4584f1da4b0534c55c9b6f88e0358d8dd98 /core
parent85641c545bedbdb703ad923306786afe5c312110 (diff)
Allow booleanization of all types
We now allow booleanization of all types. This means that empty versions of all types now evaluate to false. So a Vector2(0,0), Dictionary(), etc. This allows you to write GDScript like: if not Dictionary(): print("Empty dict") Booleanization can now also no longer fail. There is no more valid flag, this changes Variant and GDNative API.
Diffstat (limited to 'core')
-rw-r--r--core/variant.h2
-rw-r--r--core/variant_op.cpp87
2 files changed, 13 insertions, 76 deletions
diff --git a/core/variant.h b/core/variant.h
index edc86dedd4..5ea540a63f 100644
--- a/core/variant.h
+++ b/core/variant.h
@@ -390,7 +390,7 @@ public:
uint32_t hash() const;
bool hash_compare(const Variant &p_variant) const;
- bool booleanize(bool &valid) const;
+ bool booleanize() const;
void static_assign(const Variant &p_variant);
static void get_constructor_list(Variant::Type p_type, List<MethodInfo> *p_list);
diff --git a/core/variant_op.cpp b/core/variant_op.cpp
index 8349f2fdf6..5d47936679 100644
--- a/core/variant_op.cpp
+++ b/core/variant_op.cpp
@@ -143,56 +143,13 @@
Variant::operator bool() const {
- bool b;
- return booleanize(b);
+ return booleanize();
}
-bool Variant::booleanize(bool &r_valid) const {
-
- r_valid = true;
- switch (type) {
- case NIL:
- return false;
- case BOOL:
- return _data._bool;
- case INT:
- return _data._int;
- case REAL:
- return _data._real;
- case STRING:
- return (*reinterpret_cast<const String *>(_data._mem)) != "";
- case VECTOR2:
- case RECT2:
- case TRANSFORM2D:
- case VECTOR3:
- case PLANE:
- case RECT3:
- case QUAT:
- case BASIS:
- case TRANSFORM:
- case COLOR:
- case _RID:
- return (*reinterpret_cast<const RID *>(_data._mem)).is_valid();
- case OBJECT:
- return _get_obj().obj;
- case NODE_PATH:
- return (*reinterpret_cast<const NodePath *>(_data._mem)) != NodePath();
- case DICTIONARY:
- case ARRAY:
- case POOL_BYTE_ARRAY:
- case POOL_INT_ARRAY:
- case POOL_REAL_ARRAY:
- case POOL_STRING_ARRAY:
- case POOL_VECTOR2_ARRAY:
- case POOL_VECTOR3_ARRAY:
- case POOL_COLOR_ARRAY:
- r_valid = false;
- return false;
- default: {
- }
- }
-
- return false;
+// We consider all unitialized or empty types to be false based on the type's
+// zeroiness.
+bool Variant::booleanize() const {
+ return !is_zero();
}
#define _RETURN(m_what) \
@@ -403,12 +360,6 @@ bool Variant::booleanize(bool &r_valid) const {
_RETURN(sum); \
}
-#define DEFAULT_OP_FAIL(m_name) \
- case m_name: { \
- r_valid = false; \
- return; \
- }
-
void Variant::evaluate(const Operator &p_op, const Variant &p_a,
const Variant &p_b, Variant &r_ret, bool &r_valid) {
@@ -1177,12 +1128,8 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
SWITCH_OP(math, OP_AND, p_a.type) {
CASE_TYPE_ALL(math, OP_AND) {
- bool l = p_a.booleanize(r_valid);
- if (!r_valid)
- return;
- bool r = p_b.booleanize(r_valid);
- if (!r_valid)
- return;
+ bool l = p_a.booleanize();
+ bool r = p_b.booleanize();
_RETURN(l && r);
}
@@ -1190,12 +1137,8 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
SWITCH_OP(math, OP_OR, p_a.type) {
CASE_TYPE_ALL(math, OP_OR) {
- bool l = p_a.booleanize(r_valid);
- if (!r_valid)
- return;
- bool r = p_b.booleanize(r_valid);
- if (!r_valid)
- return;
+ bool l = p_a.booleanize();
+ bool r = p_b.booleanize();
_RETURN(l || r);
}
@@ -1203,12 +1146,8 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
SWITCH_OP(math, OP_XOR, p_a.type) {
CASE_TYPE_ALL(math, OP_XOR) {
- bool l = p_a.booleanize(r_valid);
- if (!r_valid)
- return;
- bool r = p_b.booleanize(r_valid);
- if (!r_valid)
- return;
+ bool l = p_a.booleanize();
+ bool r = p_b.booleanize();
_RETURN((l || r) && !(l && r));
}
@@ -1216,9 +1155,7 @@ void Variant::evaluate(const Operator &p_op, const Variant &p_a,
SWITCH_OP(math, OP_NOT, p_a.type) {
CASE_TYPE_ALL(math, OP_NOT) {
- bool l = p_a.booleanize(r_valid);
- if (!r_valid)
- return;
+ bool l = p_a.booleanize();
_RETURN(!l);
}
}