forked from matkatmusic/PFMCPP_Project4
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
362 lines (292 loc) · 8.52 KB
/
main.cpp
File metadata and controls
362 lines (292 loc) · 8.52 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/*
Project 4: Part 9 / 9
video: Chapter 5 Part 8
Create a branch named Part9
Rule of 3-5-0 and S.R.P.
DO NOT EDIT YOUR PREVIOUS main().
1) add the Leak Detector files from Project5
2) add these macros after the JUCE_LEAK_DETECTOR macro :
*/
// #define JUCE_DECLARE_NON_COPYABLE(className) \
// className (const className&) = delete;\
// className& operator= (const className&) = delete;
// #define JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(className) \
// JUCE_DECLARE_NON_COPYABLE(className) \
// JUCE_LEAK_DETECTOR(className)
/*
3) add JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Temporary) to the end of the Temporary<> struct
4) add JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Numeric) to the end of the Numeric<> struct
if you compile it, you'll see lots of errors about deleted functions.
5) Implement the Rule of 5 on Numeric<> and Temporary<> to fix this.
You should end up with the same program output as Part 8's task if you did it right.
*/
#include "LeakedObjectDetector.h"
#include <iostream>
#include <cmath>
#include <functional>
#include <memory>
#include <limits>
#include <typeinfo>
template<typename NumericType>
struct Temporary
{
//constructor
Temporary(NumericType t) : v(t)
{
std::cout << "I'm a Temporary<" << typeid(v).name() << "> object, #"
<< counter++ << std::endl;
}
//destructor
~Temporary() = default;
//move constructor
Temporary(Temporary&& t) : v( std::move(t.v) ) {}
//move assignment
Temporary& operator=(Temporary&& t)
{
this->v = std::move(t.v);
return *this;
}
/*
revise these conversion functions to read/write to 'v' here
hint: what qualifier do read-only functions usually have?
*/
operator NumericType() const { /* read-only function */ return v; }
operator NumericType&() { /* read/write function */ return v; }
private:
static int counter;
NumericType v;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Temporary)
};
template<typename NumericType>
int Temporary<NumericType>::counter = 0;
/*
If you did everything correctly, this is the output you should get:
I'm a Temporary<f> object, #0
I'm a Temporary<i> object, #0
I'm a Temporary<d> object, #0
f: -1.89
d: -3.024
i: -9
Point { x: -1.89, y: -9 }
d: 3.024
I'm a Temporary<d> object, #1
I'm a Temporary<d> object, #2
d: 1.49519e+08
Point { x: -2.82591e+08, y: -1.34567e+09 }
I'm a Temporary<f> object, #1
I'm a Temporary<i> object, #1
I'm a Temporary<i> object, #2
I'm a Temporary<i> object, #3
intNum: 5
I'm a Temporary<f> object, #2
f squared: 3.5721
I'm a Temporary<f> object, #3
f cubed: 45.5796
I'm a Temporary<d> object, #3
d squared: 2.2356e+16
I'm a Temporary<d> object, #4
d cubed: 1.11733e+49
I'm a Temporary<i> object, #4
i squared: 81
I'm a Temporary<i> object, #5
i cubed: 531441
Use a service like https://www.diffchecker.com/diff to compare your output.
*/
template <typename T>
struct Numeric
{
using Type = Temporary<T>;
//constructor
Numeric( T varA ) : a( std::make_unique<Type>(varA) ) {}
//destructor
~Numeric() = default;
//move constructor
Numeric( Numeric&& n ) : a( std::move(n.a) ) {}
//move assignment
Numeric& operator=( Numeric&& n )
{
this->a = std::move(n.a);
return *this;
}
//copy operators
operator T() const { return *a; }
operator T&() { return *a; }
template<typename FuncPtr>
Numeric& apply( FuncPtr func )
{
func( a );
return *this;
}
template<typename OtherType>
Numeric& pow( const OtherType& rhs )
{
if( a != nullptr )
*a = std::pow( *a, static_cast<T>(rhs) );
return *this;
}
template<typename OtherType>
Numeric& operator=( const OtherType& rhs )
{
*a = static_cast<T>(rhs);
return *this;
}
template<typename OtherType>
Numeric& operator+=( const OtherType& rhs )
{
*a += static_cast<T>(rhs);
return *this;
}
template<typename OtherType>
Numeric& operator-=( const OtherType& rhs )
{
*a -= static_cast<T>(rhs);
return *this;
}
template<typename OtherType>
Numeric& operator*=( const OtherType& rhs )
{
*a *= static_cast<T>(rhs);
return *this;
}
template<typename OtherType>
Numeric& operator/=( const OtherType& rhs )
{
if constexpr ( std::is_same<Type, int>::value )
{
if constexpr ( std::is_same< OtherType, int>::value )
{
if ( rhs == 0 )
{
std::cout << "Can't divide by 0.\n";
return *this;
}
else if ( static_cast<Type>(rhs) < std::numeric_limits<Type>::epsilon() )
{
std::cout << "Can't divide by 0.\n";
return *this;
}
}
}
else if ( static_cast<OtherType>(rhs) < std::numeric_limits<OtherType>::epsilon() )
{
std::cout << "Warning, dividing by 0.\n";
}
*a /= static_cast<T>(rhs);
return *this;
}
private:
std::unique_ptr<Type> a;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR(Numeric)
};
struct Point
{
Point( float a, float b ) : x( a ), y( b ) {}
template<typename OtherType>
Point& multiply( const OtherType& m)
{
x *= static_cast<float>(m);
y *= static_cast<float>(m);
return *this;
}
template<typename OtherType>
Point& operator*=( const OtherType& m )
{
return multiply( static_cast<float>(m) );
}
void toString()
{
std::cout << "The x coordinate is: " << x << "\nThe y coordinate is: " << y << std::endl;
}
private:
float x{0}, y{0};
};
/* Free Functions */
template <typename T>
void updateValue( T& value)
{
value += value;
}
template <typename T>
void cube( std::unique_ptr<T>& value )
{
*value = (*value) * (*value) * (*value);
}
void divider()
{
std::cout << "\n\n===============================\n\n";
}
/*
MAKE SURE YOU ARE NOT ON THE MASTER BRANCH
Commit your changes by clicking on the Source Control panel on the left, entering a message, and click [Commit and push].
If you didn't already:
Make a pull request after you make your first commit
pin the pull request link and this repl.it link to our DM thread in a single message.
send me a DM to review your pull request when the project is ready for review.
Wait for my code review.
*/
int main()
{
Numeric<float> f(0.1f);
Numeric<int> i(3);
Numeric<double> d(4.2);
f += 2.f;
f -= i;
f *= d;
f /= 2.f;
std::cout << "f: " << f << std::endl;
d += 2.f;
d -= i;
d *= f;
d /= 2.f;
std::cout << "d: " << d << std::endl;
i += 2.f; i -= f; i *= d; i /= 2.f;
std::cout << "i: "<< i << std::endl;
Point p(f, i);
p.toString();
d *= -1;
std::cout << "d: " << d << std::endl;
p.multiply(d.pow(f).pow(i));
std::cout << "d: " << d << std::endl;
p.toString();
Numeric<float> floatNum(4.3f);
Numeric<int> intNum(2);
Numeric<int> intNum2(6);
intNum = 2 + (intNum2 - 4) + static_cast<double>(floatNum) / 2.3;
std::cout << "intNum: " << intNum << std::endl;
{
using Type = decltype(f)::Type;
f.apply([&f](std::unique_ptr<Type>&value) -> decltype(f)&
{
auto& v = *value;
v = v * v;
return f;
});
std::cout << "f squared: " << f << std::endl;
f.apply( cube<Type> );
std::cout << "f cubed: " << f << std::endl;
}
{
using Type = decltype(d)::Type;
d.apply([&d](std::unique_ptr<Type>&value) -> decltype(d)&
{
auto& v = *value;
v = v * v;
return d;
});
std::cout << "d squared: " << d << std::endl;
d.apply( cube<Type> );
std::cout << "d cubed: " << d << std::endl;
}
{
using Type = decltype(i)::Type;
i.apply([&i](std::unique_ptr<Type>&value) -> decltype(i)&
{
auto& v = *value;
v = v * v;
return i;
});
std::cout << "i squared: " << i << std::endl;
i.apply( cube<Type> );
std::cout << "i cubed: " << i << std::endl;
}
}