-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRule3Copy.h
118 lines (105 loc) · 3.18 KB
/
Rule3Copy.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
* \file Rule3Copy.h
* \brief The rule of three/five/zero
*
* \see https://en.cppreference.com/w/cpp/language/rule_of_three
*
* 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>
//--------------------------------------------------------------------------------------------------
#if !defined(RULE_3_COPY_OPTION_LOG)
#define RULE_3_COPY_OPTION_LOG 0
#endif
//--------------------------------------------------------------------------------------------------
class Rule3Copy
{
public:
Rule3Copy();
explicit Rule3Copy(const std::string &value);
Rule3Copy(const Rule3Copy &obj) noexcept;
Rule3Copy & operator = (const Rule3Copy &obj) noexcept;
Rule3Copy(Rule3Copy &&obj) noexcept = delete;
Rule3Copy & operator = (Rule3Copy &&obj) noexcept = delete;
~Rule3Copy();
const std::string &value() const;
friend std::ostream & operator << (std::ostream &os, const Rule3Copy &obj);
private:
std::string _value;
void _log(const std::string &title, const std::string &msg) const;
};
//--------------------------------------------------------------------------------------------------
Rule3Copy::Rule3Copy()
{
_log("Ctor", "Default");
}
//-------------------------------------------------------------------------------------------------
Rule3Copy::Rule3Copy(
const std::string &a_value
) :
_value(a_value)
{
_log("Ctor", "const std::string &");
}
//-------------------------------------------------------------------------------------------------
Rule3Copy::Rule3Copy(
const Rule3Copy &a_obj
) noexcept :
_value{a_obj._value}
{
_log("Ctor", "Copy");
}
//--------------------------------------------------------------------------------------------------
Rule3Copy &
Rule3Copy::operator = (
const Rule3Copy &a_obj
) noexcept
{
if (this == &a_obj) {
return *this;
}
_log("Op", "Copy");
_value = a_obj._value;
return *this;
}
//-------------------------------------------------------------------------------------------------
Rule3Copy::~Rule3Copy() /* final */
{
_log("Dtor", "");
_log("", "");
}
//--------------------------------------------------------------------------------------------------
const std::string &
Rule3Copy::value() const
{
return _value;
}
//--------------------------------------------------------------------------------------------------
void
Rule3Copy::_log(
const std::string &a_title,
const std::string &a_msg
) const
{
#if RULE_3_COPY_OPTION_LOG
const std::string title = a_title.empty() ? a_title : ("[" + a_title + "] ");
std::cout << title << a_msg << std::endl;
#else
STD_UNUSED(a_title);
STD_UNUSED(a_msg);
#endif
}
//--------------------------------------------------------------------------------------------------
inline std::ostream &
operator << (
std::ostream &out_os,
const Rule3Copy &a_obj
)
{
return out_os << "{" << a_obj._value << "}";
}
//-------------------------------------------------------------------------------------------------