Skip to content

Commit 446135f

Browse files
author
James Foster
authored
Merge pull request #306 from jgfoster/Client_assignment_operator
Rule of three for Client
2 parents ffe8a12 + 5cac675 commit 446135f

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
1919
- Replace `#define yield() _NOP()` with `inline void yield() { _NOP(); }` so that other code can define a `yield()` function.
2020
- Update .gitattributes so we have consistent line endings
2121
- Run tests on push as well as on a pull request so developers can see impact
22+
- Apply "rule of three" to Client copy constructor and copy assignment operator
2223

2324
### Deprecated
2425

SampleProjects/TestSomething/test/clientServer.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,29 @@ unittest(Client) {
1919
assertEqual(outData + "\r\n", inData);
2020
}
2121

22+
unittest(Client_copy_constructor) {
23+
{ // Client object contains a reference to a String object
24+
Client c1; // Constructor instantiates a String (string1)
25+
Client c2; // Constructor instantiates a String (string2)
26+
c1.write('1');
27+
c2.write('2');
28+
assertEqual("1", *(c1.mGodmodeDataIn));
29+
assertEqual("2", *(c2.mGodmodeDataIn));
30+
c2 = c1; // c2 should get a copy of s1, not a reference to it
31+
// and string2 should have been deleted during the assignment
32+
assertNotEqual(c1.mGodmodeDataIn, c2.mGodmodeDataIn);
33+
assertEqual("1", *(c1.mGodmodeDataIn));
34+
assertEqual("1", *(c2.mGodmodeDataIn));
35+
c1.write('1');
36+
c2.write('2');
37+
assertEqual("11", *(c1.mGodmodeDataIn));
38+
assertEqual("12", *(c2.mGodmodeDataIn));
39+
} // End of scope calls destructor on c1 and c2
40+
// Memory monitoring will give an error if delete is called twice on string1
41+
// The following assertion is just to confirm that we got through the above
42+
assertTrue(true);
43+
}
44+
2245
unittest(IPAddress) {
2346
IPAddress ipAddress0;
2447
assertEqual(0, ipAddress0.asWord());

cpp/arduino/Client.h

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22

3-
#include <Stream.h>
43
#include <IPAddress.h>
4+
#include <Stream.h>
55

66
class Client : public Stream {
77
public:
@@ -11,6 +11,25 @@ class Client : public Stream {
1111
mGodmodeDataIn = new String;
1212
}
1313
}
14+
Client(const Client &client) { // copy constructor
15+
if (this != &client) { // not a self-assignment
16+
if (mGodmodeDataIn &&
17+
client.mGodmodeDataIn) { // replace what we previously had
18+
delete mGodmodeDataIn; // get rid of previous value
19+
mGodmodeDataIn = new String(client.mGodmodeDataIn->c_str());
20+
}
21+
}
22+
}
23+
Client &operator=(const Client &client) { // copy assignment operator
24+
if (this != &client) { // not a self-assignment
25+
if (mGodmodeDataIn &&
26+
client.mGodmodeDataIn) { // replace what we previously had
27+
delete mGodmodeDataIn; // get rid of previous value
28+
mGodmodeDataIn = new String(client.mGodmodeDataIn->c_str());
29+
}
30+
}
31+
return *this;
32+
}
1433
~Client() {
1534
if (mGodmodeDataIn) {
1635
delete mGodmodeDataIn;

0 commit comments

Comments
 (0)