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

Commit76fa259

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 parent5f37ba4 commit76fa259

File tree

2 files changed

+18
-43
lines changed

2 files changed

+18
-43
lines changed

‎api/String.cpp

Lines changed: 18 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,15 @@ String::String(const __FlashStringHelper *pstr)
4545
*this = pstr;
4646
}
4747

48-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
4948
String::String(String &&rval)
49+
: buffer(rval.buffer)
50+
, capacity(rval.capacity)
51+
, len(rval.len)
5052
{
51-
init();
52-
move(rval);
53+
rval.buffer =NULL;
54+
rval.capacity =0;
55+
rval.len =0;
5356
}
54-
String::String(StringSumHelper &&rval)
55-
{
56-
init();
57-
move(rval);
58-
}
59-
#endif
6057

6158
String::String(char c)
6259
{
@@ -191,27 +188,21 @@ String & String::copy(const __FlashStringHelper *pstr, unsigned int length)
191188
return *this;
192189
}
193190

194-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
195191
voidString::move(String &rhs)
196192
{
197-
if (buffer) {
198-
if (rhs && capacity >= rhs.len) {
199-
strcpy(buffer, rhs.buffer);
200-
len = rhs.len;
201-
rhs.len =0;
202-
return;
203-
}else {
204-
free(buffer);
205-
}
193+
if (this != &rhs)
194+
{
195+
free(buffer);
196+
197+
buffer = rhs.buffer;
198+
len = rhs.len;
199+
capacity = rhs.capacity;
200+
201+
rhs.buffer =NULL;
202+
rhs.len =0;
203+
rhs.capacity =0;
206204
}
207-
buffer = rhs.buffer;
208-
capacity = rhs.capacity;
209-
len = rhs.len;
210-
rhs.buffer =NULL;
211-
rhs.capacity =0;
212-
rhs.len =0;
213205
}
214-
#endif
215206

216207
String & String::operator = (const String &rhs)
217208
{
@@ -223,19 +214,11 @@ String & String::operator = (const String &rhs)
223214
return *this;
224215
}
225216

226-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
227217
String & String::operator = (String &&rval)
228218
{
229-
if (this != &rval)move(rval);
230-
return *this;
231-
}
232-
233-
String & String::operator = (StringSumHelper &&rval)
234-
{
235-
if (this != &rval)move(rval);
219+
move(rval);
236220
return *this;
237221
}
238-
#endif
239222

240223
String & String::operator = (constchar *cstr)
241224
{

‎api/String.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ class String
6363
String(constchar *cstr ="");
6464
String(const String &str);
6565
String(const __FlashStringHelper *str);
66-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
6766
String(String &&rval);
68-
String(StringSumHelper &&rval);
69-
#endif
7067
explicitString(char c);
7168
explicitString(unsignedchar,unsignedchar base=10);
7269
explicitString(int,unsignedchar base=10);
@@ -90,10 +87,7 @@ class String
9087
String &operator = (const String &rhs);
9188
String &operator = (constchar *cstr);
9289
String &operator = (const __FlashStringHelper *str);
93-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
9490
String &operator = (String &&rval);
95-
String &operator = (StringSumHelper &&rval);
96-
#endif
9791

9892
// concatenate (works w/ built-in types)
9993

@@ -223,9 +217,7 @@ class String
223217
// copy and move
224218
String &copy(constchar *cstr,unsignedint length);
225219
String &copy(const __FlashStringHelper *pstr,unsignedint length);
226-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
227220
voidmove(String &rhs);
228-
#endif
229221
};
230222

231223
classStringSumHelper :publicString

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp