-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStaticShapes.cpp
More file actions
196 lines (153 loc) · 5.5 KB
/
StaticShapes.cpp
File metadata and controls
196 lines (153 loc) · 5.5 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// ===========================================================================
// StaticShapes.cpp // Decorator Pattern
// ===========================================================================
#include <concepts>
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
namespace StaticDecoration {
class IShape
{
public:
virtual ~IShape() {}
virtual std::string draw() const = 0;
};
class Circle : public IShape
{
private:
double m_radius;
public:
Circle() : m_radius{ 0.0 } {}
Circle(double radius) : m_radius{ radius } {}
void resize(double factor) { m_radius *= factor; }
std::string draw() const override {
std::ostringstream oss;
oss << "A circle of radius " << std::setprecision(5) << m_radius;
return oss.str();
}
};
struct Square : public IShape
{
private:
double m_side;
public:
Square() : m_side{ 0.0 } {}
Square(double side) : m_side{ side } {}
void setSide(double side) { m_side = side; }
std::string draw() const override {
std::ostringstream oss;
oss << "A square with side " << m_side;
return oss.str();
}
};
struct Rectangle : public IShape
{
private:
double m_width;
double m_height;
public:
Rectangle() : m_width{ 0.0 }, m_height{ 0.0 } {}
Rectangle(double width, double height)
: m_width{ width }, m_height{ height } {}
void setWidth(double width) { m_width = width; }
void setHeight(double height) { m_height = height; }
std::string draw() const override {
std::ostringstream oss;
oss << "A Rectangle with width " << m_width << " and height " << m_height;
return oss.str();
}
};
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
// template argument must be derived from IShape
template <typename T>
concept ConceptIShapeObject = std::derived_from<T, IShape>;
// ---------------------------------------------------------------------------
template <typename T>
requires ConceptIShapeObject<T>
class ColoredShape : public T
{
private:
std::string m_color;
public:
// c'tor
template <typename ... TArgs>
ColoredShape(const std::string& color, TArgs&& ...args) :
T{ std::forward<TArgs>(args) ... }, m_color{ color }
{}
void setColor(const std::string& color) { m_color = color; }
std::string draw() const override {
std::ostringstream oss;
std::string s{ T::draw() };
oss << s << " has color " << m_color;
return oss.str();
}
};
// ---------------------------------------------------------------------------
template <typename T>
requires ConceptIShapeObject<T>
struct TransparentShape : public T
{
private:
std::uint8_t m_transparency;
public:
// c'tor
template<typename ... TArgs>
TransparentShape(std::uint8_t transparency, TArgs&& ...args)
: T{ std::forward<TArgs>(args)... }, m_transparency{ transparency } {}
std::string draw() const override {
std::ostringstream oss;
std::string s{ T::draw() };
// present transparency as percentage with two decimals
double pct = (static_cast<double>(m_transparency) / 255.0) * 100.0;
oss << s << " has " << std::fixed << std::setprecision(2) << pct << "% transparency";
return oss.str();
}
};
}
class NonConformantCircle {};
void test_static_decoration_01()
{
using namespace StaticDecoration;
Circle circle{ 3.0 };
std::cout << circle.draw() << std::endl;
Square square{ 20.0 };
std::cout << square.draw() << std::endl;
Rectangle rectangle{ 30.0, 40.0 };
std::cout << rectangle.draw() << std::endl;
// just testing 'concepts' constraints
// ColoredShape<NonConformantCircle> whatCircle{};
ColoredShape<Circle> greenCircle{
std::string{ "green" }, 5.0
};
std::cout << greenCircle.draw() << std::endl;
TransparentShape<Square> transparentSquare{
static_cast<std::uint8_t>(0), 0.0
};
std::cout << transparentSquare.draw() << std::endl;
ColoredShape<Rectangle> yellowRectangle{
std::string{ "yellow" }, 50.0, 60.0
};
std::cout << yellowRectangle.draw() << std::endl;
TransparentShape<ColoredShape<Square>> blueTransparentSquare{
0, std::string{ "blue" }, 0.0
};
blueTransparentSquare.setColor("yellow");
blueTransparentSquare.setSide(100);
std::cout << blueTransparentSquare.draw() << std::endl;
ColoredShape<TransparentShape<Square>> redOpaqueSquare{
std::string{ "red" }, static_cast<std::uint8_t>(0), 255.0
};
redOpaqueSquare.setColor("red");
redOpaqueSquare.setSide(300);
std::cout << redOpaqueSquare.draw() << std::endl;
TransparentShape<ColoredShape<Rectangle>> whiteNonTransparentRectangle{
127, std::string{ "white" }, 70.0, 80.0,
};
std::cout << whiteNonTransparentRectangle.draw() << std::endl;
}
// ===========================================================================
// End-of-File
// ===========================================================================