-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathControl03.cpp
More file actions
125 lines (100 loc) · 3.12 KB
/
Control03.cpp
File metadata and controls
125 lines (100 loc) · 3.12 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
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
119
120
121
122
123
124
125
// ===========================================================================
// Control.cpp - Pimpl Idiom with Copy- & Move-Semantics
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
#include "Control.h"
namespace PimplVariantWithCopyMoveSemantics {
// ===========================================================================
// declaration of pimpl class
class ControlPimpl {
private:
std::string m_text;
int m_width;
int m_height;
bool m_visible;
void draw();
public:
ControlPimpl() : m_text(""), m_width(0), m_height(0), m_visible(true) {}
void setText(const std::string& text);
void resize(const int width, const int height);
void show();
void hide();
};
// ================================================================
// definition of pimpl class methods
void ControlPimpl::draw()
{
std::cout
<< "control "
<< std::endl
<< " visible: "
<< std::boolalpha
<< m_visible
<< std::noboolalpha
<< std::endl
<< " size: "
<< m_width
<< ", "
<< m_height
<< std::endl
<< " text: "
<< m_text
<< std::endl;
}
void ControlPimpl::setText(const std::string& text)
{
m_text = text;
draw();
}
void ControlPimpl::resize(const int width, const int height) {
m_width = width;
m_height = height;
draw();
}
void ControlPimpl::show()
{
m_visible = true;
draw();
}
void ControlPimpl::hide() {
m_visible = false;
draw();
}
// ===========================================================
// main class methods
// default c'tor
Control::Control() : m_pimpl(std::make_unique<ControlPimpl>()) {}
Control::~Control() = default; // due to 'can't delete an incomplete type'
// move semantics - using default behaviour
Control::Control(Control&&) noexcept = default;
Control& Control::operator=(Control&&) noexcept = default;
// copy semantics - using (automatically generated) public copy constructor of ControlPimpl
Control::Control(const Control& op) : m_pimpl{ new ControlPimpl(*op.m_pimpl) } {}
Control& Control::operator=(const Control& op) {
if (this != &op) {
m_pimpl = std::unique_ptr<ControlPimpl>(new ControlPimpl(*op.m_pimpl));
}
return *this;
}
// remaining member functions
void Control::setText(const std::string& text)
{
m_pimpl->setText(text);
}
void Control::resize(const int width, const int height)
{
m_pimpl->resize(width, height);
}
void Control::show()
{
m_pimpl->show();
}
void Control::hide() {
m_pimpl->hide();
}
}
// ===========================================================================
// End-of-File
// ===========================================================================