From 689b5fb98b4e3e618ed3571986d9b860f00b194e Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Sun, 30 May 2021 12:30:11 +0530 Subject: [PATCH 1/7] added mover constructor --- include/core/String.hpp | 1 + src/core/String.cpp | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/include/core/String.hpp b/include/core/String.hpp index 7c11ac78..62b58e4d 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -40,6 +40,7 @@ public: String(const wchar_t *contents); String(const wchar_t c); String(const String &other); + String(String&& other); ~String(); diff --git a/src/core/String.cpp b/src/core/String.cpp index c1efee54..19e44f90 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -73,6 +73,11 @@ String::String(const String &other) { godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); } +String::String(String&& other) { + godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); + godot::api->godot_string_destroy(&_godot_string); +} + String::~String() { godot::api->godot_string_destroy(&_godot_string); } From bdc5674ace9fc06a58497d0fc25cdff07cddf72b Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Sun, 30 May 2021 12:33:40 +0530 Subject: [PATCH 2/7] added move assignment operator --- include/core/String.hpp | 1 + src/core/String.cpp | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/include/core/String.hpp b/include/core/String.hpp index 62b58e4d..ae3dd89e 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -56,6 +56,7 @@ public: wchar_t operator[](const int idx) const; void operator=(const String &s); + void operator=(String&& s); bool operator==(const String &s) const; bool operator!=(const String &s) const; String operator+(const String &s) const; diff --git a/src/core/String.cpp b/src/core/String.cpp index 19e44f90..3414adfc 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -99,6 +99,12 @@ void String::operator=(const String &s) { godot::api->godot_string_new_copy(&_godot_string, &s._godot_string); } +void String::operator=(String&& s) { + godot::api->godot_string_destroy(&_godot_string); + godot::api->godot_string_new_copy(&_godot_string, &s._godot_string); + godot::api->godot_string_destroy(&s._godot_string); +} + bool String::operator==(const String &s) const { return godot::api->godot_string_operator_equal(&_godot_string, &s._godot_string); } From 492285f681e68e61431380283e7a04f56de4cb08 Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Sun, 30 May 2021 22:29:41 +0530 Subject: [PATCH 3/7] changed spaces to tabs --- include/core/String.hpp | 2 +- src/core/String.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/core/String.hpp b/include/core/String.hpp index ae3dd89e..fa3944d7 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -40,7 +40,7 @@ public: String(const wchar_t *contents); String(const wchar_t c); String(const String &other); - String(String&& other); + String(String&& other); ~String(); diff --git a/src/core/String.cpp b/src/core/String.cpp index 3414adfc..767578fb 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -74,8 +74,8 @@ String::String(const String &other) { } String::String(String&& other) { - godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); - godot::api->godot_string_destroy(&_godot_string); + godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); + godot::api->godot_string_destroy(&_godot_string); } String::~String() { From cd05371ce86cc5a4d4fb66187f4df1edc2f03e16 Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Mon, 31 May 2021 00:52:36 +0530 Subject: [PATCH 4/7] added class member, safety check in the destructor --- include/core/String.hpp | 1 + src/core/String.cpp | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/include/core/String.hpp b/include/core/String.hpp index fa3944d7..4992adb2 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -17,6 +17,7 @@ class CharString { friend class String; godot_char_string _char_string; + bool _deleted = false; public: ~CharString(); diff --git a/src/core/String.cpp b/src/core/String.cpp index 767578fb..04b73879 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -74,12 +74,15 @@ String::String(const String &other) { } String::String(String&& other) { - godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); - godot::api->godot_string_destroy(&_godot_string); + _godot_string = other._godot_string; + other._deleted = true; } String::~String() { - godot::api->godot_string_destroy(&_godot_string); + if (!_deleted) { + godot::api->godot_string_destroy(&_godot_string); + _deleted = true; + } } wchar_t &String::operator[](const int idx) { @@ -101,8 +104,8 @@ void String::operator=(const String &s) { void String::operator=(String&& s) { godot::api->godot_string_destroy(&_godot_string); - godot::api->godot_string_new_copy(&_godot_string, &s._godot_string); - godot::api->godot_string_destroy(&s._godot_string); + _godot_string = s._godot_string; + s._deleted = true; } bool String::operator==(const String &s) const { From b44b98a94c564a6eeb619c476ea2a3a75e1e6297 Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Mon, 31 May 2021 09:10:59 +0530 Subject: [PATCH 5/7] removed _deleted class member --- include/core/String.hpp | 1 - src/core/String.cpp | 7 +------ 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/include/core/String.hpp b/include/core/String.hpp index 4992adb2..fa3944d7 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -17,7 +17,6 @@ class CharString { friend class String; godot_char_string _char_string; - bool _deleted = false; public: ~CharString(); diff --git a/src/core/String.cpp b/src/core/String.cpp index 04b73879..68ead4df 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -75,14 +75,10 @@ String::String(const String &other) { String::String(String&& other) { _godot_string = other._godot_string; - other._deleted = true; } String::~String() { - if (!_deleted) { - godot::api->godot_string_destroy(&_godot_string); - _deleted = true; - } + godot::api->godot_string_destroy(&_godot_string); } wchar_t &String::operator[](const int idx) { @@ -105,7 +101,6 @@ void String::operator=(const String &s) { void String::operator=(String&& s) { godot::api->godot_string_destroy(&_godot_string); _godot_string = s._godot_string; - s._deleted = true; } bool String::operator==(const String &s) const { From 67e2c6145c8650defb5af6ff88d805dc07fe3975 Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Mon, 31 May 2021 09:31:11 +0530 Subject: [PATCH 6/7] used godot_string_new_copy to copy --- src/core/String.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/core/String.cpp b/src/core/String.cpp index 68ead4df..ac72f061 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -74,7 +74,7 @@ String::String(const String &other) { } String::String(String&& other) { - _godot_string = other._godot_string; + godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); } String::~String() { @@ -100,7 +100,7 @@ void String::operator=(const String &s) { void String::operator=(String&& s) { godot::api->godot_string_destroy(&_godot_string); - _godot_string = s._godot_string; + godot::api->godot_string_new_copy(&_godot_string, &s._godot_string); } bool String::operator==(const String &s) const { From 7a1890345b2658b422823bce889f88cc7dbb76d5 Mon Sep 17 00:00:00 2001 From: DhruvMaroo Date: Tue, 1 Jun 2021 23:16:09 +0530 Subject: [PATCH 7/7] edited according to clang-format --- include/core/String.hpp | 4 ++-- src/core/String.cpp | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/include/core/String.hpp b/include/core/String.hpp index fa3944d7..2dea8458 100644 --- a/include/core/String.hpp +++ b/include/core/String.hpp @@ -40,7 +40,7 @@ public: String(const wchar_t *contents); String(const wchar_t c); String(const String &other); - String(String&& other); + String(String &&other); ~String(); @@ -56,7 +56,7 @@ public: wchar_t operator[](const int idx) const; void operator=(const String &s); - void operator=(String&& s); + void operator=(String &&s); bool operator==(const String &s) const; bool operator!=(const String &s) const; String operator+(const String &s) const; diff --git a/src/core/String.cpp b/src/core/String.cpp index ac72f061..7564572e 100644 --- a/src/core/String.cpp +++ b/src/core/String.cpp @@ -73,7 +73,7 @@ String::String(const String &other) { godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); } -String::String(String&& other) { +String::String(String &&other) { godot::api->godot_string_new_copy(&_godot_string, &other._godot_string); } @@ -98,7 +98,7 @@ void String::operator=(const String &s) { godot::api->godot_string_new_copy(&_godot_string, &s._godot_string); } -void String::operator=(String&& s) { +void String::operator=(String &&s) { godot::api->godot_string_destroy(&_godot_string); godot::api->godot_string_new_copy(&_godot_string, &s._godot_string); }