[Scummvm-git-logs] scummvm master -> cdedda7fa8854ef93a5fd61248276067d4563d1e

sev- noreply at scummvm.org
Wed Sep 28 15:09:38 UTC 2022


This automated email contains information about 1 new commit which have been
pushed to the 'scummvm' repo located at https://github.com/scummvm/scummvm .

Summary:
cdedda7fa8 COMMON: Add move semantic assign and construct to strings


Commit: cdedda7fa8854ef93a5fd61248276067d4563d1e
    https://github.com/scummvm/scummvm/commit/cdedda7fa8854ef93a5fd61248276067d4563d1e
Author: elasota (ejlasota at gmail.com)
Date: 2022-09-28T17:09:34+02:00

Commit Message:
COMMON: Add move semantic assign and construct to strings

Changed paths:
    common/base-str.cpp
    common/base-str.h
    common/str.cpp
    common/str.h
    common/ustr.cpp
    common/ustr.h


diff --git a/common/base-str.cpp b/common/base-str.cpp
index c553e7d6dea..0c044d30dd7 100644
--- a/common/base-str.cpp
+++ b/common/base-str.cpp
@@ -84,6 +84,24 @@ BASESTRING::BaseString(const BASESTRING &str)
 	assert(_str != nullptr);
 }
 
+
+TEMPLATE
+BASESTRING::BaseString(BASESTRING &&str) : _size(str._size) {
+	if (str.isStorageIntern()) {
+		// String in internal storage: copy it
+		memcpy(_storage, str._storage, _builtinCapacity * sizeof(value_type));
+		_str = _storage;
+	} else {
+		// String in external storage: Take the reference
+		_extern = str._extern;
+		_str = str._str;
+	}
+
+	str._str = str._storage;
+	str._storage[0] = 0;
+	str._size = 0;
+}
+
 TEMPLATE BASESTRING::BaseString(const value_type *str) : _size(0), _str(_storage) {
 	if (str == nullptr) {
 		_storage[0] = 0;
@@ -517,6 +535,27 @@ TEMPLATE void BASESTRING::assign(const BaseString &str) {
 	}
 }
 
+TEMPLATE void BASESTRING::assign(BaseString &&str) {
+	if (&str == this)
+		return;
+
+	decRefCount(_extern._refCount);
+
+	if (str.isStorageIntern()) {
+		_str = _storage;
+		memcpy(_str, str._str, _builtinCapacity * sizeof(value_type));
+	} else {
+		_extern = str._extern;
+		_str = str._str;
+	}
+
+	_size = str._size;
+
+	str._storage[0] = 0;
+	str._size = 0;
+	str._str = str._storage;
+}
+
 TEMPLATE void BASESTRING::assign(value_type c) {
 	decRefCount(_extern._refCount);
 	_str = _storage;
diff --git a/common/base-str.h b/common/base-str.h
index 71c04589117..6be8e731462 100644
--- a/common/base-str.h
+++ b/common/base-str.h
@@ -86,6 +86,9 @@ public:
 	/** Construct a copy of the given string. */
 	BaseString(const BaseString &str);
 
+	/** Construct a string by moving an existing string. */
+	BaseString(BaseString &&str);
+
 	/** Construct a new string from the given NULL-terminated C string. */
 	explicit BaseString(const value_type *str);
 
@@ -247,6 +250,7 @@ protected:
 	void assignAppend(value_type c);
 	void assignAppend(const BaseString &str);
 	void assign(const BaseString &str);
+	void assign(BaseString &&str);
 	void assign(value_type c);
 	void assign(const value_type *str);
 
diff --git a/common/str.cpp b/common/str.cpp
index 4685f1ac2b8..af33638581d 100644
--- a/common/str.cpp
+++ b/common/str.cpp
@@ -55,6 +55,11 @@ String &String::operator=(const String &str) {
 	return *this;
 }
 
+String &String::operator=(String &&str) {
+	assign(static_cast<String &&>(str));
+	return *this;
+}
+
 String &String::operator=(char c) {
 	assign(c);
 	return *this;
diff --git a/common/str.h b/common/str.h
index 6ad6c4513a4..878883f019d 100644
--- a/common/str.h
+++ b/common/str.h
@@ -78,7 +78,10 @@ public:
 	String(const char *beginP, const char *endP) : BaseString<char>(beginP, endP) {}
 
 	/** Construct a copy of the given string. */
-	String(const String &str) : BaseString<char>(str) {};
+	String(const String &str) : BaseString<char>(str) {}
+
+	/** Construct a string by moving an existing string. */
+	String(String &&str) : BaseString<char>(static_cast<BaseString<char> &&>(str)) {}
 
 	/** Construct a string consisting of the given character. */
 	explicit String(char c);
@@ -88,6 +91,7 @@ public:
 
 	String &operator=(const char *str);
 	String &operator=(const String &str);
+	String &operator=(String &&str);
 	String &operator=(char c);
 	String &operator+=(const char *str);
 	String &operator+=(const String &str);
diff --git a/common/ustr.cpp b/common/ustr.cpp
index 0ad2076a15d..531eddfc185 100644
--- a/common/ustr.cpp
+++ b/common/ustr.cpp
@@ -60,6 +60,11 @@ U32String &U32String::operator=(const U32String &str) {
 	return *this;
 }
 
+U32String &U32String::operator=(U32String &&str) {
+	assign(static_cast<U32String &&>(str));
+	return *this;
+}
+
 U32String &U32String::operator=(const String &str) {
 	clear();
 	decodeInternal(str.c_str(), str.size(), Common::kUtf8);
diff --git a/common/ustr.h b/common/ustr.h
index 55a2ae66653..75be1856a99 100644
--- a/common/ustr.h
+++ b/common/ustr.h
@@ -76,6 +76,9 @@ public:
 	/** Construct a copy of the given string. */
 	U32String(const U32String &str) : BaseString<u32char_type_t>(str) {}
 
+	/** Construct a string by moving an existing string. */
+	U32String(U32String &&str) : BaseString<u32char_type_t>(static_cast<BaseString<u32char_type_t> &&>(str)) {}
+
 	/** Construct a new string from the given null-terminated C string that uses the given @p page encoding. */
 	explicit U32String(const char *str, CodePage page = kUtf8);
 
@@ -94,6 +97,9 @@ public:
 	/** Assign a given string to this string. */
 	U32String &operator=(const U32String &str);
 
+	/** Move a given string to this string. */
+	U32String &operator=(U32String &&str);
+
 	/** @overload */
 	U32String &operator=(const String &str);
 




More information about the Scummvm-git-logs mailing list