-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMovable.hpp
More file actions
62 lines (59 loc) · 1.77 KB
/
Copy pathMovable.hpp
File metadata and controls
62 lines (59 loc) · 1.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef Movable_hpp
#define Movable_hpp
#include <iostream>
#include <cstdint>
#include <cstring>
class Movable {
char *str;
const std::size_t id;
inline static std::size_t index{};
inline static std::ostream& os = std::cerr;
char *dup(const char *s1) {
auto len = strlen(s1);
auto s2 = new char[len + 1];
memcpy(s2, s1, len + 1);
return s2;
}
public:
Movable() : str{ nullptr }, id{ ++index } {
os << "Construct empty Movable with id " << id << '\n';
}
Movable(const char *s) : str{ dup(s) }, id{ ++index } {
os << "Construct Movable with id " << id << " and content: " << str << '\n';
}
Movable(const Movable& rhs) : str{ dup(rhs.str) }, id{ ++index } {
os << "Copy-construct Movable with id " << id << " from id " << rhs.id << '\n';
}
Movable(Movable&& rhs) : str{ rhs.str }, id{ ++index } {
rhs.str = nullptr;
os << "Move-construct Movable with id " << id << " from id " << rhs.id << '\n';
}
Movable& operator=(const Movable& rhs) {
char *str1 = dup(rhs.str);
delete[] str;
str = str1;
os << "Copy-assign Movable with id " << id << " from id " << rhs.id << '\n';
return *this;
}
Movable& operator=(Movable&& rhs) {
delete[] str;
str = rhs.str;
rhs.str = nullptr;
os << "Move-assign Movable with id " << id << " from id " << rhs.id << '\n';
return *this;
}
~Movable() {
os << "Delete Movable with id " << id << '\n';
delete[] str;
}
void print(std::ostream& out = std::cout) {
out << id << ": ";
if (str) {
out << str << '\n';
}
else {
out << "(No data)\n";
}
}
};
#endif // Movable_hpp