Skip to content

Commit 57073ba

Browse files
committed
Add move constructor and move assignment to CowData, String, Char16String, CharString and Vector.
1 parent a40fc23 commit 57073ba

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

core/string/ustring.h

+11
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
#include "core/typedefs.h"
4040
#include "core/variant/array.h"
4141

42+
#include <utility>
43+
4244
/*************************************************************************/
4345
/* Utility Functions */
4446
/*************************************************************************/
@@ -193,7 +195,10 @@ class Char16String {
193195

194196
_FORCE_INLINE_ Char16String() {}
195197
_FORCE_INLINE_ Char16String(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
198+
_FORCE_INLINE_ Char16String(Char16String &&p_str) :
199+
_cowdata(std::move(p_str._cowdata)) {}
196200
_FORCE_INLINE_ void operator=(const Char16String &p_str) { _cowdata._ref(p_str._cowdata); }
201+
_FORCE_INLINE_ void operator=(Char16String &&p_str) { _cowdata = std::move(p_str._cowdata); }
197202
_FORCE_INLINE_ Char16String(const char16_t *p_cstr) { copy_from(p_cstr); }
198203

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

236241
_FORCE_INLINE_ CharString() {}
237242
_FORCE_INLINE_ CharString(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
243+
_FORCE_INLINE_ CharString(CharString &&p_str) :
244+
_cowdata(std::move(p_str._cowdata)) {}
238245
_FORCE_INLINE_ void operator=(const CharString &p_str) { _cowdata._ref(p_str._cowdata); }
246+
_FORCE_INLINE_ void operator=(CharString &&p_str) { _cowdata = std::move(p_str._cowdata); }
239247
_FORCE_INLINE_ CharString(const char *p_cstr) { copy_from(p_cstr); }
240248

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

595603
_FORCE_INLINE_ String() {}
596604
_FORCE_INLINE_ String(const String &p_str) { _cowdata._ref(p_str._cowdata); }
605+
_FORCE_INLINE_ String(String &&p_str) :
606+
_cowdata(std::move(p_str._cowdata)) {}
597607
_FORCE_INLINE_ void operator=(const String &p_str) { _cowdata._ref(p_str._cowdata); }
608+
_FORCE_INLINE_ void operator=(String &&p_str) { _cowdata = std::move(p_str._cowdata); }
598609

599610
Vector<uint8_t> to_ascii_buffer() const;
600611
Vector<uint8_t> to_utf8_buffer() const;

core/templates/cowdata.h

+14-1
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,15 @@ class CowData {
167167

168168
public:
169169
void operator=(const CowData<T> &p_from) { _ref(p_from); }
170+
void operator=(CowData<T> &&p_from) {
171+
if (_ptr == p_from._ptr) {
172+
return;
173+
}
174+
175+
_unref();
176+
_ptr = p_from._ptr;
177+
p_from._ptr = nullptr;
178+
}
170179

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

242251
_FORCE_INLINE_ CowData() {}
243252
_FORCE_INLINE_ ~CowData();
244-
_FORCE_INLINE_ CowData(CowData<T> &p_from) { _ref(p_from); }
253+
_FORCE_INLINE_ CowData(const CowData<T> &p_from) { _ref(p_from); }
254+
_FORCE_INLINE_ CowData(CowData<T> &&p_from) {
255+
_ptr = p_from._ptr;
256+
p_from._ptr = nullptr;
257+
}
245258
};
246259

247260
template <typename T>

core/templates/vector.h

+5-3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747

4848
#include <climits>
4949
#include <initializer_list>
50+
#include <utility>
5051

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

150-
inline void operator=(const Vector &p_from) {
151-
_cowdata._ref(p_from._cowdata);
152-
}
151+
void operator=(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
152+
void operator=(Vector &&p_from) { _cowdata = std::move(p_from._cowdata); }
153153

154154
Vector<uint8_t> to_byte_array() const {
155155
Vector<uint8_t> ret;
@@ -290,6 +290,8 @@ class Vector {
290290
}
291291
}
292292
_FORCE_INLINE_ Vector(const Vector &p_from) { _cowdata._ref(p_from._cowdata); }
293+
_FORCE_INLINE_ Vector(Vector &&p_from) :
294+
_cowdata(std::move(p_from._cowdata)) {}
293295

294296
_FORCE_INLINE_ ~Vector() {}
295297
};

0 commit comments

Comments
 (0)