Refactored Variant Operators.

-Using classes to call and a table
-For typed code (GDS or GDNative), can obtain functions to call prevalidated or ptr.
This commit is contained in:
reduz 2020-11-04 23:01:55 -03:00
parent 391d29f558
commit f2397809a8
26 changed files with 5322 additions and 4507 deletions

View File

@ -98,6 +98,37 @@ bool Array::operator==(const Array &p_array) const {
return _p == p_array._p;
}
bool Array::operator!=(const Array &p_array) const {
return !operator==(p_array);
}
bool Array::operator<(const Array &p_array) const {
int a_len = size();
int b_len = p_array.size();
int min_cmp = MIN(a_len, b_len);
for (int i = 0; i < min_cmp; i++) {
if (operator[](i) < p_array[i]) {
return true;
} else if (p_array[i] < operator[](i)) {
return false;
}
}
return a_len < b_len;
}
bool Array::operator<=(const Array &p_array) const {
return !operator>(p_array);
}
bool Array::operator>(const Array &p_array) const {
return p_array < *this;
}
bool Array::operator>=(const Array &p_array) const {
return !operator<(p_array);
}
uint32_t Array::hash() const {
uint32_t h = hash_djb2_one_32(0);

View File

@ -61,6 +61,7 @@ public:
void clear();
bool operator==(const Array &p_array) const;
bool operator!=(const Array &p_array) const;
uint32_t hash() const;
void operator=(const Array &p_array);
@ -98,6 +99,11 @@ public:
Array slice(int p_begin, int p_end, int p_step = 1, bool p_deep = false) const;
bool operator<(const Array &p_array) const;
bool operator<=(const Array &p_array) const;
bool operator>(const Array &p_array) const;
bool operator>=(const Array &p_array) const;
Variant min() const;
Variant max() const;

View File

@ -493,7 +493,7 @@ Color Color::operator*(const Color &p_color) const {
a * p_color.a);
}
Color Color::operator*(const real_t &rvalue) const {
Color Color::operator*(real_t rvalue) const {
return Color(
r * rvalue,
g * rvalue,
@ -508,7 +508,7 @@ void Color::operator*=(const Color &p_color) {
a = a * p_color.a;
}
void Color::operator*=(const real_t &rvalue) {
void Color::operator*=(real_t rvalue) {
r = r * rvalue;
g = g * rvalue;
b = b * rvalue;
@ -523,7 +523,7 @@ Color Color::operator/(const Color &p_color) const {
a / p_color.a);
}
Color Color::operator/(const real_t &rvalue) const {
Color Color::operator/(real_t rvalue) const {
return Color(
r / rvalue,
g / rvalue,
@ -538,7 +538,7 @@ void Color::operator/=(const Color &p_color) {
a = a / p_color.a;
}
void Color::operator/=(const real_t &rvalue) {
void Color::operator/=(real_t rvalue) {
if (rvalue == 0) {
r = 1.0;
g = 1.0;

View File

@ -79,14 +79,14 @@ struct Color {
void operator-=(const Color &p_color);
Color operator*(const Color &p_color) const;
Color operator*(const real_t &rvalue) const;
Color operator*(real_t rvalue) const;
void operator*=(const Color &p_color);
void operator*=(const real_t &rvalue);
void operator*=(real_t rvalue);
Color operator/(const Color &p_color) const;
Color operator/(const real_t &rvalue) const;
Color operator/(real_t rvalue) const;
void operator/=(const Color &p_color);
void operator/=(const real_t &rvalue);
void operator/=(real_t rvalue);
bool is_equal_approx(const Color &p_color) const;
@ -232,7 +232,7 @@ bool Color::operator<(const Color &p_color) const {
}
}
_FORCE_INLINE_ Color operator*(const real_t &p_real, const Color &p_color) {
_FORCE_INLINE_ Color operator*(real_t p_real, const Color &p_color) {
return p_color * p_real;
}

View File

@ -638,7 +638,6 @@ void register_global_constants() {
BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("OP_NEGATE", Variant::OP_NEGATE);
BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("OP_POSITIVE", Variant::OP_POSITIVE);
BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("OP_MODULE", Variant::OP_MODULE);
BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("OP_STRING_CONCAT", Variant::OP_STRING_CONCAT);
//bitwise
BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("OP_SHIFT_LEFT", Variant::OP_SHIFT_LEFT);
BIND_GLOBAL_ENUM_CONSTANT_CUSTOM("OP_SHIFT_RIGHT", Variant::OP_SHIFT_RIGHT);

View File

@ -91,6 +91,10 @@ public:
return v + ((uv * w) + u.cross(uv)) * ((real_t)2);
}
_FORCE_INLINE_ Vector3 xform_inv(const Vector3 &v) const {
return inverse().xform(v);
}
_FORCE_INLINE_ void operator+=(const Quat &q);
_FORCE_INLINE_ void operator-=(const Quat &q);
_FORCE_INLINE_ void operator*=(const real_t &s);

View File

@ -233,6 +233,19 @@ void Vector2i::operator/=(const int &rvalue) {
y /= rvalue;
}
Vector2i Vector2i::operator%(const Vector2i &p_v1) const {
return Vector2i(x % p_v1.x, y % p_v1.y);
}
Vector2i Vector2i::operator%(const int &rvalue) const {
return Vector2i(x % rvalue, y % rvalue);
}
void Vector2i::operator%=(const int &rvalue) {
x %= rvalue;
y %= rvalue;
}
Vector2i Vector2i::operator-() const {
return Vector2i(-x, -y);
}

View File

@ -290,11 +290,13 @@ struct Vector2i {
void operator*=(const int &rvalue);
Vector2i operator/(const Vector2i &p_v1) const;
Vector2i operator/(const int &rvalue) const;
void operator/=(const int &rvalue);
Vector2i operator%(const Vector2i &p_v1) const;
Vector2i operator%(const int &rvalue) const;
void operator%=(const int &rvalue);
Vector2i operator-() const;
bool operator<(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y < p_vec2.y) : (x < p_vec2.x); }
bool operator>(const Vector2i &p_vec2) const { return (x == p_vec2.x) ? (y > p_vec2.y) : (x > p_vec2.x); }

View File

@ -80,11 +80,15 @@ struct Vector3i {
_FORCE_INLINE_ Vector3i operator*(const Vector3i &p_v) const;
_FORCE_INLINE_ Vector3i &operator/=(const Vector3i &p_v);
_FORCE_INLINE_ Vector3i operator/(const Vector3i &p_v) const;
_FORCE_INLINE_ Vector3i &operator%=(const Vector3i &p_v);
_FORCE_INLINE_ Vector3i operator%(const Vector3i &p_v) const;
_FORCE_INLINE_ Vector3i &operator*=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator*(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator/=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator/(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i &operator%=(int32_t p_scalar);
_FORCE_INLINE_ Vector3i operator%(int32_t p_scalar) const;
_FORCE_INLINE_ Vector3i operator-() const;
@ -159,6 +163,17 @@ Vector3i Vector3i::operator/(const Vector3i &p_v) const {
return Vector3i(x / p_v.x, y / p_v.y, z / p_v.z);
}
Vector3i &Vector3i::operator%=(const Vector3i &p_v) {
x %= p_v.x;
y %= p_v.y;
z %= p_v.z;
return *this;
}
Vector3i Vector3i::operator%(const Vector3i &p_v) const {
return Vector3i(x % p_v.x, y % p_v.y, z % p_v.z);
}
Vector3i &Vector3i::operator*=(int32_t p_scalar) {
x *= p_scalar;
y *= p_scalar;
@ -185,6 +200,17 @@ Vector3i Vector3i::operator/(int32_t p_scalar) const {
return Vector3i(x / p_scalar, y / p_scalar, z / p_scalar);
}
Vector3i &Vector3i::operator%=(int32_t p_scalar) {
x %= p_scalar;
y %= p_scalar;
z %= p_scalar;
return *this;
}
Vector3i Vector3i::operator%(int32_t p_scalar) const {
return Vector3i(x % p_scalar, y % p_scalar, z % p_scalar);
}
Vector3i Vector3i::operator-() const {
return Vector3i(-x, -y, -z);
}

View File

@ -99,6 +99,8 @@ extern void register_global_constants();
extern void unregister_global_constants();
extern void register_variant_methods();
extern void unregister_variant_methods();
extern void register_variant_operators();
extern void unregister_variant_operators();
void register_core_types() {
//consistency check
@ -112,6 +114,7 @@ void register_core_types() {
register_global_constants();
register_variant_methods();
register_variant_operators();
CoreStringNames::create();
@ -319,6 +322,7 @@ void unregister_core_types() {
ClassDB::cleanup_defaults();
ObjectDB::cleanup();
unregister_variant_operators();
unregister_variant_methods();
unregister_global_constants();

View File

@ -52,6 +52,9 @@ public:
_FORCE_INLINE_ bool operator>(const RID &p_rid) const {
return _id > p_rid._id;
}
_FORCE_INLINE_ bool operator>=(const RID &p_rid) const {
return _id >= p_rid._id;
}
_FORCE_INLINE_ bool operator!=(const RID &p_rid) const {
return _id != p_rid._id;
}

View File

@ -377,3 +377,17 @@ StringName StringName::search(const String &p_name) {
StringName::~StringName() {
unref();
}
bool operator==(const String &p_name, const StringName &p_string_name) {
return p_name == p_string_name.operator String();
}
bool operator!=(const String &p_name, const StringName &p_string_name) {
return p_name != p_string_name.operator String();
}
bool operator==(const char *p_name, const StringName &p_string_name) {
return p_name == p_string_name.operator String();
}
bool operator!=(const char *p_name, const StringName &p_string_name) {
return p_name != p_string_name.operator String();
}

View File

@ -155,6 +155,11 @@ public:
~StringName();
};
bool operator==(const String &p_name, const StringName &p_string_name);
bool operator!=(const String &p_name, const StringName &p_string_name);
bool operator==(const char *p_name, const StringName &p_string_name);
bool operator!=(const char *p_name, const StringName &p_string_name);
StringName _scs_create(const char *p_chr);
#endif // STRING_NAME_H

View File

@ -637,6 +637,20 @@ bool operator==(const wchar_t *p_chr, const String &p_str) {
#endif
}
bool operator!=(const char *p_chr, const String &p_str) {
return !(p_str == p_chr);
}
bool operator!=(const wchar_t *p_chr, const String &p_str) {
#ifdef WINDOWS_ENABLED
// wchar_t is 16-bit
return !(p_str == String::utf16((const char16_t *)p_chr));
#else
// wchar_t is 32-bi
return !(p_str == String((const char32_t *)p_chr));
#endif
}
bool String::operator!=(const char *p_str) const {
return (!(*this == p_str));
}
@ -654,7 +668,14 @@ bool String::operator!=(const String &p_str) const {
}
bool String::operator<=(const String &p_str) const {
return (*this < p_str) || (*this == p_str);
return !(p_str < *this);
}
bool String::operator>(const String &p_str) const {
return p_str < *this;
}
bool String::operator>=(const String &p_str) const {
return !(*this < p_str);
}
bool String::operator<(const char *p_str) const {
@ -4455,7 +4476,9 @@ String String::sprintf(const Array &values, bool *error) const {
bool left_justified = false;
bool show_sign = false;
*error = true;
if (error) {
*error = true;
}
for (; *self; self++) {
const char32_t c = *self;
@ -4716,7 +4739,9 @@ String String::sprintf(const Array &values, bool *error) const {
return "not all arguments converted during string formatting";
}
*error = false;
if (error) {
*error = false;
}
return formatted;
}

View File

@ -254,6 +254,8 @@ public:
bool operator<(const String &p_str) const;
bool operator<=(const String &p_str) const;
bool operator>(const String &p_str) const;
bool operator>=(const String &p_str) const;
signed char casecmp_to(const String &p_str) const;
signed char nocasecmp_to(const String &p_str) const;
@ -456,6 +458,8 @@ public:
bool operator==(const char *p_chr, const String &p_str);
bool operator==(const wchar_t *p_chr, const String &p_str);
bool operator!=(const char *p_chr, const String &p_str);
bool operator!=(const wchar_t *p_chr, const String &p_str);
String operator+(const char *p_chr, const String &p_str);
String operator+(const wchar_t *p_chr, const String &p_str);

View File

@ -381,7 +381,6 @@ public:
OP_NEGATE,
OP_POSITIVE,
OP_MODULE,
OP_STRING_CONCAT,
//bitwise
OP_SHIFT_LEFT,
OP_SHIFT_RIGHT,
@ -409,6 +408,14 @@ public:
return res;
}
Variant::Type get_operator_return_type(Operator p_operator, Type p_type_a, Type p_type_b);
typedef void (*ValidatedOperatorEvaluator)(const Variant *left, const Variant *right, Variant *r_ret);
static ValidatedOperatorEvaluator get_validated_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
#ifdef PTRCALL_ENABLED
typedef void (*PTROperatorEvaluator)(const void *left, const void *right, void *r_ret);
static PTROperatorEvaluator get_ptr_operator_evaluator(Operator p_operator, Type p_type_a, Type p_type_b);
#endif
void zero();
Variant duplicate(bool deep = false) const;
static void blend(const Variant &a, const Variant &b, float c, Variant &r_dst);

File diff suppressed because it is too large Load Diff

3132
core/variant_setget.cpp Normal file

File diff suppressed because it is too large Load Diff

View File

@ -157,6 +157,32 @@ public:
return slice;
}
bool operator==(const Vector<T> &p_arr) const {
int s = size();
if (s != p_arr.size()) {
return false;
}
for (int i = 0; i < s; i++) {
if (operator[](i) != p_arr[i]) {
return false;
}
}
return true;
}
bool operator!=(const Vector<T> &p_arr) const {
int s = size();
if (s != p_arr.size()) {
return true;
}
for (int i = 0; i < s; i++) {
if (operator[](i) != p_arr[i]) {
return true;
}
}
return false;
}
_FORCE_INLINE_ Vector() {}
_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }

View File

@ -471,28 +471,28 @@ void DependencyRemoveDialog::ok_pressed() {
// If the file we are deleting for e.g. the main scene, default environment,
// or audio bus layout, we must clear its definition in Project Settings.
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("application/config/icon")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("application/config/icon"))) {
ProjectSettings::get_singleton()->set("application/config/icon", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("application/run/main_scene")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("application/run/main_scene"))) {
ProjectSettings::get_singleton()->set("application/run/main_scene", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("application/boot_splash/image")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("application/boot_splash/image"))) {
ProjectSettings::get_singleton()->set("application/boot_splash/image", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("rendering/environment/default_environment")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("rendering/environment/default_environment"))) {
ProjectSettings::get_singleton()->set("rendering/environment/default_environment", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("display/mouse_cursor/custom_image"))) {
ProjectSettings::get_singleton()->set("display/mouse_cursor/custom_image", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("gui/theme/custom")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("gui/theme/custom"))) {
ProjectSettings::get_singleton()->set("gui/theme/custom", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("gui/theme/custom_font")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("gui/theme/custom_font"))) {
ProjectSettings::get_singleton()->set("gui/theme/custom_font", "");
}
if (files_to_delete[i] == ProjectSettings::get_singleton()->get("audio/default_bus_layout")) {
if (files_to_delete[i] == String(ProjectSettings::get_singleton()->get("audio/default_bus_layout"))) {
ProjectSettings::get_singleton()->set("audio/default_bus_layout", "");
}

View File

@ -4687,7 +4687,7 @@ void Node3DEditor::set_state(const Dictionary &p_state) {
}
int state = EditorNode3DGizmoPlugin::VISIBLE;
for (int i = 0; i < keys.size(); i++) {
if (gizmo_plugins_by_name.write[j]->get_name() == keys[i]) {
if (gizmo_plugins_by_name.write[j]->get_name() == String(keys[i])) {
state = gizmos_status[keys[i]];
break;
}

View File

@ -2397,7 +2397,7 @@ void ScriptEditor::_editor_settings_changed() {
if (current_theme == "") {
current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
} else if (current_theme != EditorSettings::get_singleton()->get("text_editor/theme/color_theme")) {
} else if (current_theme != String(EditorSettings::get_singleton()->get("text_editor/theme/color_theme"))) {
current_theme = EditorSettings::get_singleton()->get("text_editor/theme/color_theme");
EditorSettings::get_singleton()->load_text_editor_theme();
}

View File

@ -875,7 +875,8 @@ void TextureRegionEditor::_changed_callback(Object *p_changed, const char *p_pro
if (!is_visible()) {
return;
}
if (p_prop == StringName("atlas") || p_prop == StringName("texture") || p_prop == StringName("region")) {
String prop = p_prop;
if (prop == "atlas" || prop == "texture" || prop == "region") {
_edit_region();
}
}

View File

@ -2232,7 +2232,7 @@ bool Main::start() {
#ifdef TOOLS_ENABLED
if (editor) {
if (game_path != GLOBAL_GET("application/run/main_scene") || !editor_node->has_scenes_in_session()) {
if (game_path != String(GLOBAL_GET("application/run/main_scene")) || !editor_node->has_scenes_in_session()) {
Error serr = editor_node->load_scene(local_game_path);
if (serr != OK) {
ERR_PRINT("Failed to load scene");

View File

@ -828,7 +828,6 @@ PropertyInfo VisualScriptOperator::get_input_value_port_info(int p_idx) const {
{ Variant::NIL, Variant::NIL }, //OP_NEGATE,
{ Variant::NIL, Variant::NIL }, //OP_POSITIVE,
{ Variant::INT, Variant::INT }, //OP_MODULE,
{ Variant::STRING, Variant::STRING }, //OP_STRING_CONCAT,
//bitwise
{ Variant::INT, Variant::INT }, //OP_SHIFT_LEFT,
{ Variant::INT, Variant::INT }, //OP_SHIFT_RIGHT,
@ -873,7 +872,6 @@ PropertyInfo VisualScriptOperator::get_output_value_port_info(int p_idx) const {
Variant::NIL, //OP_NEGATE,
Variant::NIL, //OP_POSITIVE,
Variant::INT, //OP_MODULE,
Variant::STRING, //OP_STRING_CONCAT,
//bitwise
Variant::INT, //OP_SHIFT_LEFT,
Variant::INT, //OP_SHIFT_RIGHT,
@ -3881,7 +3879,6 @@ void register_visual_script_nodes() {
VisualScriptLanguage::singleton->add_register_func("operators/math/negate", create_op_node<Variant::OP_NEGATE>);
VisualScriptLanguage::singleton->add_register_func("operators/math/positive", create_op_node<Variant::OP_POSITIVE>);
VisualScriptLanguage::singleton->add_register_func("operators/math/remainder", create_op_node<Variant::OP_MODULE>);
VisualScriptLanguage::singleton->add_register_func("operators/math/string_concat", create_op_node<Variant::OP_STRING_CONCAT>);
//bitwise
VisualScriptLanguage::singleton->add_register_func("operators/bitwise/shift_left", create_op_node<Variant::OP_SHIFT_LEFT>);
VisualScriptLanguage::singleton->add_register_func("operators/bitwise/shift_right", create_op_node<Variant::OP_SHIFT_RIGHT>);

View File

@ -381,7 +381,7 @@ TEST_CASE("[Expression] Unusual expressions") {
ERR_PRINT_OFF;
CHECK_MESSAGE(
expression.parse("$1.00 + 5") == OK,
expression.parse("$1.00 + ???5") == OK,
"The expression should parse successfully.");
CHECK_MESSAGE(
int(expression.execute()) == 0,
@ -410,8 +410,8 @@ TEST_CASE("[Expression] Unusual expressions") {
"The expression should parse successfully.");
ERR_PRINT_OFF;
CHECK_MESSAGE(
Math::is_zero_approx(float(expression.execute())),
"`-25.4 / 0` should return 0.");
Math::is_inf(float(expression.execute())),
"`-25.4 / 0` should return inf.");
ERR_PRINT_ON;
CHECK_MESSAGE(