Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add move constructor and move assignment to CowData, String, Char16String, CharString, and Vector. #100239

Merged
merged 1 commit into from
Dec 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions core/string/ustring.h
Original file line number Diff line number Diff line change
@@ -39,6 +39,8 @@
#include "core/typedefs.h"
#include "core/variant/array.h"

#include <utility>

/*************************************************************************/
/* Utility Functions */
/*************************************************************************/
@@ -193,7 +195,10 @@ class Char16String {

_FORCE_INLINE_ Char16String() {}
_FORCE_INLINE_ Char16String(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
_FORCE_INLINE_ Char16String(Char16String &&p_str) :
_cowdata(std::move(p_str._cowdata)) {}
_FORCE_INLINE_ void operator=(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
_FORCE_INLINE_ void operator=(Char16String &&p_str) { _cowdata = std::move(p_str._cowdata); }
_FORCE_INLINE_ Char16String(const char16_t *p_cstr) { copy_from(p_cstr); }

void operator=(const char16_t *p_cstr);
@@ -235,7 +240,10 @@ class CharString {

_FORCE_INLINE_ CharString() {}
_FORCE_INLINE_ CharString(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
_FORCE_INLINE_ CharString(CharString &&p_str) :
_cowdata(std::move(p_str._cowdata)) {}
_FORCE_INLINE_ void operator=(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
_FORCE_INLINE_ void operator=(CharString &&p_str) { _cowdata = std::move(p_str._cowdata); }
_FORCE_INLINE_ CharString(const char *p_cstr) { copy_from(p_cstr); }

void operator=(const char *p_cstr);
@@ -594,7 +602,10 @@ class String {

_FORCE_INLINE_ String() {}
_FORCE_INLINE_ String(const String &p_str) { _cowdata._ref(p_str._cowdata); }
_FORCE_INLINE_ String(String &&p_str) :
_cowdata(std::move(p_str._cowdata)) {}
_FORCE_INLINE_ void operator=(const String &p_str) { _cowdata._ref(p_str._cowdata); }
_FORCE_INLINE_ void operator=(String &&p_str) { _cowdata = std::move(p_str._cowdata); }

Vector<uint8_t> to_ascii_buffer() const;
Vector<uint8_t> to_utf8_buffer() const;
15 changes: 14 additions & 1 deletion core/templates/cowdata.h
Original file line number Diff line number Diff line change
@@ -167,6 +167,15 @@ class CowData {

public:
void operator=(const CowData<T> &p_from) { _ref(p_from); }
void operator=(CowData<T> &&p_from) {
if (_ptr == p_from._ptr) {
return;
}

_unref();
_ptr = p_from._ptr;
p_from._ptr = nullptr;
}

_FORCE_INLINE_ T *ptrw() {
_copy_on_write();
@@ -241,7 +250,11 @@ class CowData {

_FORCE_INLINE_ CowData() {}
_FORCE_INLINE_ ~CowData();
_FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); }
_FORCE_INLINE_ CowData(const CowData<T> &p_from) { _ref(p_from); }
_FORCE_INLINE_ CowData(CowData<T> &&p_from) {
_ptr = p_from._ptr;
p_from._ptr = nullptr;
}
};

template <typename T>
8 changes: 5 additions & 3 deletions core/templates/vector.h
Original file line number Diff line number Diff line change
@@ -47,6 +47,7 @@

#include <climits>
#include <initializer_list>
#include <utility>

template <typename T>
class VectorWriteProxy {
@@ -147,9 +148,8 @@ class Vector {
insert(i, p_val);
}

inline void operator=(const Vector &p_from) {
_cowdata._ref(p_from._cowdata);
}
void operator=(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
void operator=(Vector &&p_from) { _cowdata = std::move(p_from._cowdata); }

Vector<uint8_t> to_byte_array() const {
Vector<uint8_t> ret;
@@ -290,6 +290,8 @@ class Vector {
}
}
_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
_FORCE_INLINE_ Vector(Vector &&p_from) :
_cowdata(std::move(p_from._cowdata)) {}

_FORCE_INLINE_ ~Vector() {}
};
Loading