Skip to content

Commit 76fa259

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&& or StringSumHelper&& to this constructor. StringSumHelper is derived from String and has no data members other than those inherited from String. Even if it did have some extra data members, truncation would have to happen during move, and normally that is something you don't want.
1 parent 5f37ba4 commit 76fa259

File tree

2 files changed

+18
-43
lines changed

2 files changed

+18
-43
lines changed

api/String.cpp

+18-35
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
void String::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 = (const char *cstr)
241224
{

api/String.h

-8
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,7 @@ class String
6363
String(const char *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
explicit String(char c);
7168
explicit String(unsigned char, unsigned char base=10);
7269
explicit String(int, unsigned char base=10);
@@ -90,10 +87,7 @@ class String
9087
String & operator = (const String &rhs);
9188
String & operator = (const char *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(const char *cstr, unsigned int length);
225219
String & copy(const __FlashStringHelper *pstr, unsigned int length);
226-
#if __cplusplus >= 201103L || defined(__GXX_EXPERIMENTAL_CXX0X__)
227220
void move(String &rhs);
228-
#endif
229221
};
230222

231223
class StringSumHelper : public String

0 commit comments

Comments
 (0)