1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,59 @@ |
1 |
+#include "crany.hpp" |
|
2 |
+ |
|
3 |
+#include <cmath> |
|
4 |
+#include <iostream> |
|
5 |
+#include <vector> |
|
6 |
+ |
|
7 |
+ |
|
8 |
+auto constexpr pi = std::acos(-1.0F); |
|
9 |
+ |
|
10 |
+ |
|
11 |
+struct Circle |
|
12 |
+{ |
|
13 |
+ float radius; |
|
14 |
+ void scale(float scale) { radius *= scale; } |
|
15 |
+ float area() const { return pi*radius*radius; } |
|
16 |
+ float circumference() const { return 2.0F*pi*radius; } |
|
17 |
+}; |
|
18 |
+ |
|
19 |
+struct Rectangle |
|
20 |
+{ |
|
21 |
+ float side1; |
|
22 |
+ float side2; |
|
23 |
+ void scale(float scale) { side1 *= scale; side2 *= scale; } |
|
24 |
+ float area() const { return side1*side2; } |
|
25 |
+}; |
|
26 |
+ |
|
27 |
+ |
|
28 |
+template<typename Self> |
|
29 |
+struct ShapeConcept : Self |
|
30 |
+{ |
|
31 |
+ using Self::self; |
|
32 |
+ virtual void scale(float value) { return self().scale(value); } |
|
33 |
+ virtual float area() const { return self().area(); } |
|
34 |
+}; |
|
35 |
+ |
|
36 |
+using Shape = Crany<ShapeConcept>; |
|
37 |
+using Shapes = std::vector<Shape>; |
|
38 |
+ |
|
39 |
+ |
|
40 |
+int main() |
|
41 |
+{ |
|
42 |
+ static_assert(std::is_standard_layout <Circle>{}, "Circle not is standard layout"); |
|
43 |
+ static_assert(std::is_trivially_copyable<Circle>{}, "Circle not is trivially copyable"); |
|
44 |
+ |
|
45 |
+ auto shapes = Shapes{ |
|
46 |
+ Circle{1.0F}, |
|
47 |
+ Rectangle{2.0F, 3.0F}, |
|
48 |
+ }; |
|
49 |
+ shapes.push_back(shapes.front()); |
|
50 |
+ shapes.back().scale(2.0F); |
|
51 |
+ |
|
52 |
+ std::cout << std::boolalpha; |
|
53 |
+ for (auto const & shape : shapes) |
|
54 |
+ std::cout |
|
55 |
+ << shape.area() |
|
56 |
+ << " " |
|
57 |
+ << bool(crany_cast<Circle>(&shape)) |
|
58 |
+ << "\n"; |
|
59 |
+} |