-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRuleOfFive.h
96 lines (80 loc) · 1.84 KB
/
RuleOfFive.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
/**
* \file RuleOfFive.h
* \brief Rule of five
*
* Because the presence of a user-defined destructor, copy-constructor, or copy-assignment operator
* prevents implicit definition of the move constructor and the move assignment operator, any class
* for which move semantics are desirable, has to declare all five special member functions
*/
#include <StdStream/StdStream.h>
#include <StdTest/StdTest.h>
#include <Stl.h>
//--------------------------------------------------------------------------------------------------
class RuleOfFive
{
public:
///@name ctors, dtor
///@{
RuleOfFive()
{
_trace("RuleOfFive()");
}
explicit RuleOfFive(const char *a_buff)
{
if (a_buff) {
std::size_t n = std::strlen(a_buff) + 1;
_buff = new char[n];
std::memcpy(_buff, a_buff, n);
}
_trace("RuleOfFive(const char *)");
}
~RuleOfFive()
{
delete[] _buff;
_buff = nullptr;
_trace("~RuleOfFive()");
}
///@}
///@name Copy
///@{
// Ctor
RuleOfFive(const RuleOfFive &a_other) :
RuleOfFive(a_other._buff)
{
_trace("RuleOfFive(const RuleOfFive &)");
}
// Assignment
RuleOfFive &
operator = (const RuleOfFive &a_other)
{
*this = RuleOfFive(a_other);
_trace("operator = (const RuleOfFive &)");
return *this;
}
///@}
///@name Move
///@{
// Ctor
RuleOfFive(RuleOfFive &&a_other) noexcept :
_buff(std::exchange(a_other._buff, nullptr))
{
_trace("RuleOfFive(RuleOfFive &&a_other)");
}
// Assignment
RuleOfFive &
operator = (RuleOfFive &&a_other) noexcept
{
std::swap(_buff, a_other._buff);
_trace("operator = (RuleOfFive &&)");
return *this;
}
///@}
private:
char *_buff {};
void
_trace(const std::string &a_funcName) const
{
std::cout << a_funcName << ": " << (_buff ? _buff : "nullptr") << std::endl; \
}
};
//--------------------------------------------------------------------------------------------------