Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit14e923e

Browse files
committed
Make String move constructor move instead of copy.
The move constructor String::String(String&&) and String::operator=(String&&)now perform move instead of copy.Remove String(StringSumHelper&&) constructor because having it makes no sense:String(String&&) takes care of it - you can pass either String&& orStringSumHelper&& to this constructor. StringSumHelper is derived from Stringand has no data members other than those inherited from String. Even if it didhave some extra data members, truncation would have to happen during move, andnormally that is something you don't want.
1 parente2d2f20 commit14e923e

File tree

2 files changed

+18
-44
lines changed

2 files changed

+18
-44
lines changed

‎api/String.cpp‎

Lines changed: 18 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,15 @@ String::String(const __FlashStringHelper *pstr)
6363
*this = pstr;
6464
}
6565

66-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
6766
String::String(String &&rval)
67+
: buffer(rval.buffer)
68+
, capacity(rval.capacity)
69+
, len(rval.len)
6870
{
69-
init();
70-
move(rval);
71+
rval.buffer =NULL;
72+
rval.capacity =0;
73+
rval.len =0;
7174
}
72-
String::String(StringSumHelper &&rval)
73-
{
74-
init();
75-
move(rval);
76-
}
77-
#endif
7875

7976
String::String(char c)
8077
{
@@ -214,28 +211,21 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
214211
return *this;
215212
}
216213

217-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
218214
voidString::move(String &rhs)
219215
{
220-
if (buffer) {
221-
if (rhs && capacity >= rhs.len) {
222-
memcpy(buffer, rhs.buffer, rhs.len);
223-
len = rhs.len;
224-
buffer[len] ='\0';
225-
rhs.len =0;
226-
return;
227-
}else {
228-
free(buffer);
229-
}
216+
if (this != &rhs)
217+
{
218+
free(buffer);
219+
220+
buffer = rhs.buffer;
221+
len = rhs.len;
222+
capacity = rhs.capacity;
223+
224+
rhs.buffer =NULL;
225+
rhs.len =0;
226+
rhs.capacity =0;
230227
}
231-
buffer = rhs.buffer;
232-
capacity = rhs.capacity;
233-
len = rhs.len;
234-
rhs.buffer =NULL;
235-
rhs.capacity =0;
236-
rhs.len =0;
237228
}
238-
#endif
239229

240230
String & String::operator = (const String &rhs)
241231
{
@@ -247,19 +237,11 @@ String & String::operator = (const String &rhs)
247237
return *this;
248238
}
249239

250-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
251240
String & String::operator = (String &&rval)
252241
{
253-
if (this != &rval)move(rval);
254-
return *this;
255-
}
256-
257-
String & String::operator = (StringSumHelper &&rval)
258-
{
259-
if (this != &rval)move(rval);
242+
move(rval);
260243
return *this;
261244
}
262-
#endif
263245

264246
String & String::operator = (constchar *cstr)
265247
{

‎api/String.h‎

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,7 @@ class String
7272
String(constuint8_t *cstr,unsignedint length) : String((constchar*)cstr, length) {}
7373
String(const String &str);
7474
String(const __FlashStringHelper *str);
75-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
7675
String(String &&rval);
77-
String(StringSumHelper &&rval);
78-
#endif
7976
explicitString(char c);
8077
explicitString(unsignedchar,unsignedchar base=10);
8178
explicitString(int,unsignedchar base=10);
@@ -99,10 +96,7 @@ class String
9996
String &operator = (const String &rhs);
10097
String &operator = (constchar *cstr);
10198
String &operator = (const __FlashStringHelper *str);
102-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
10399
String &operator = (String &&rval);
104-
String &operator = (StringSumHelper &&rval);
105-
#endif
106100

107101
// concatenate (works w/ built-in types)
108102

@@ -233,9 +227,7 @@ class String
233227
// copy and move
234228
String &copy(constchar *cstr,unsignedint length);
235229
String &copy(const __FlashStringHelper *pstr,unsignedint length);
236-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
237230
voidmove(String &rhs);
238-
#endif
239231
};
240232

241233
classStringSumHelper :publicString

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp