-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExercises_10_CRTP.cpp
More file actions
209 lines (166 loc) · 6.15 KB
/
Copy pathExercises_10_CRTP.cpp
File metadata and controls
209 lines (166 loc) · 6.15 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
197
198
199
200
201
202
203
204
205
206
207
208
209
// =====================================================================================
// Exercises_10_CRTP.cpp
// =====================================================================================
module modern_cpp_exercises:crtp;
import std;
import scoped_timer;
namespace Exercises_CRTP {
namespace Exercise_01 {
constexpr long MaxIterations = 10000;
constexpr bool Verbose = false;
constexpr int Width = 400;
constexpr int Height = 400;
// classical approach: base class for image types
class Image {
protected:
long m_width;
long m_height;
public:
// c'tor
Image(int width, int height) : m_width{ width }, m_height{ height } {}
public:
// public interface
virtual void draw() = 0;
virtual void drawPixel(int position) = 0;
virtual long getNumPixels() = 0;
};
class PngImage : public Image {
private:
long m_numPixels;
long m_currPixel;
public:
PngImage() : Image{ 0, 0 }, m_currPixel(0) { setNumPixels(); }
PngImage(int width, int height) : Image{ width, height } { setNumPixels(); }
virtual void draw() override {
// just to prevent optimizer to optimize "too much" some sloppy stuff
m_currPixel = 0;
int numPixels = getNumPixels();
int currPosition = getCurrPixel();
while (currPosition != numPixels) {
drawPixel(currPosition);
incCurrPixel();
currPosition = getCurrPixel();
}
}
virtual void drawPixel(int position) override {
if (Verbose) {
std::cout << "draw pixel at position " << position << std::endl;
}
}
virtual long getNumPixels() override {
return m_numPixels;
}
private:
// private helper methods
void setNumPixels() {
m_numPixels = m_width * m_height;
}
long getCurrPixel() {
return m_currPixel;
}
void incCurrPixel() {
m_currPixel++;
}
};
// driver code for classical polymorphism benchmark
static void testExercise_01a_classic_benchmark()
{
std::cout << "Classical Polymorphism Benchmark: " << std::endl;
Image* pImage = new PngImage(Width, Height);
ScopedTimer watch{};
// call draw several times to make sure performance is visible
for (int i = 0; i < MaxIterations; ++i) {
pImage->draw();
}
long pixels = pImage->getNumPixels();
std::cout << "Pixels: " << pixels << std::endl;
std::cout << "Done." << std::endl;
}
// CRTP approach: base class for all image types.
// The template parameter T is used to specify the type
// of a derived class pointed to by a pointer
template <class T>
class ImageCRTP {
protected:
long m_width;
long m_height;
public:
// c'tor
ImageCRTP(int width, int height) : m_width{ width }, m_height{ height } {}
void draw() {
static_cast<T*>(this)->draw(); // dispatch call to exact type
}
void drawPixel(int position) {
return static_cast<T*>(this)->drawPixel(position);
}
long getNumPixels() {
return static_cast<T*>(this)->getNumPixels();
}
};
class PngImageCRTP : public ImageCRTP<PngImageCRTP> {
private:
long m_numPixels;
long m_currPixel;
public:
PngImageCRTP() : ImageCRTP{ 1, 1 }, m_currPixel(0) { setNumPixels(); }
PngImageCRTP(int width, int height) : ImageCRTP{ width, height } { setNumPixels(); }
void draw() {
// just to prevent optimizer to optimize "too much" some sloppy stuff
m_currPixel = 0;
int numPixels = getNumPixels();
int currPosition = getCurrPixel();
while (currPosition != numPixels) {
drawPixel(currPosition);
incCurrPixel();
currPosition = getCurrPixel();
}
}
void drawPixel(int position) {
if (Verbose) {
std::cout << "draw pixel at position " << position << std::endl;
}
}
long getNumPixels() {
return m_numPixels;
}
private:
// private helper methods
void setNumPixels() {
m_numPixels = m_width * m_height;
}
long getCurrPixel() {
return m_currPixel;
}
void incCurrPixel() {
m_currPixel++;
}
};
// driver code for CRTP benchmark
static void testExercise_01a_crtp_benchmark()
{
std::cout << "CRTP Benchmark: " << std::endl;
ImageCRTP<PngImageCRTP>* pImage = new PngImageCRTP(Width, Height);
ScopedTimer watch{};
// call draw several times to make sure performance is visible
for (int i = 0; i < MaxIterations; ++i) {
pImage->draw();
}
long pixels = pImage->getNumPixels();
std::cout << "Pixels: " << pixels << std::endl;
std::cout << "Done." << std::endl;
}
static void testExercise()
{
testExercise_01a_classic_benchmark();
testExercise_01a_crtp_benchmark();
}
}
}
void test_exercices_crtp()
{
using namespace Exercises_CRTP;
Exercise_01::testExercise();
}
// =====================================================================================
// End-of-File
// =====================================================================================