C++ Library Extensions 2022.12.09
To help learn modern C++ programming
15-mixin.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5auto nl = tpf::nl;
6
7struct Point
8{
9 int m_x, m_y;
10};
11
12struct Circle
13{
16
17 void display() const
18 {
19 stream << " Circle - center: (" <<
20 m_center.m_x << ", " << m_center.m_y
21 << "), radius: " << m_radius << " ";
22 }
23};
24
25struct Square
26{
28 int m_side;
29
30 void display() const
31 {
32 stream << " Square - center: ("
33 << m_center.m_x <<", " << m_center.m_y
34 <<") - Side: " << m_side << " " <<endl;
35 }
36};
37
38struct Shape: public Circle, public Square
39{
40 Shape(Circle c, Square s): Circle{c}, Square{s} { }
41
42 void display() const
43 {
44 // auto& base_circle = static_cast<const Circle&>(*this);
45 // base_circle.display();
46
48
49 // auto& base_square = static_cast<const Square&>(*this);
50 // base_square.display();
51
53 }
54};
55
57{
58 Shape s{Circle{ {3, 5}, 6} , Square{{3, 3}, 7} };
59
60 s.display();
61}
62
63template<typename... BaseClasses>
64struct Mixin: public BaseClasses...
65{
66 // template<typename... Shapes>
67 Mixin(BaseClasses... shape): BaseClasses{ shape }... { }
68
69 // using BaseClasses::display() const...;
70
71 using base_types = tpf::types::type_list_t<BaseClasses...>;
72
73 void display() const
74 {
75 // Graph in C++ #8: Type List, std::tuple, container of tuples
76 // https://www.youtube.com/watch?v=HqHy8DVDtw4&t=410s
77
78 // 24 - Type List and Compile-Time Loop 1/N
79 // https://www.youtube.com/watch?v=snfLE8aYv3M&list=PL1_C6uWTeBDE6KepUEI9XeOpLc4hgDVOd&index=24
80
82 first_base::display();
83
85 second_base::display();
86 }
87};
88
89// this is called Class Template Parameter Type Deduction Guide
90template<typename... Shapes>
91Mixin(Shapes...)->Mixin<Shapes...>;
92
94{
95 Mixin m{ Circle{ {0, 0}, 5 }, Square{{3, 3}, 10 } };
96
97 m.display();
98}
99
100int main()
101{
102 // test_shape();
103
104 test_mixin();
105}
Mixin(Shapes...) -> Mixin< Shapes... >
tpf::sstream stream
Definition: 15-mixin.cpp:3
auto endl
Definition: 15-mixin.cpp:4
void test_mixin()
Definition: 15-mixin.cpp:93
void test_shape()
Definition: 15-mixin.cpp:56
auto nl
Definition: 15-mixin.cpp:5
int main()
Definition: 15-mixin.cpp:100
hidden::select_first_type_t< Types... > select_first_type_t
Definition: tpf_types.hpp:5567
hidden::select_nth_type_t< SelectIndex, Types... > select_nth_type_t
Definition: tpf_types.hpp:5585
constexpr auto endl
Definition: tpf_output.hpp:973
constexpr auto nl
Definition: tpf_output.hpp:971
Point m_center
Definition: 15-mixin.cpp:14
void display() const
Definition: 15-mixin.cpp:17
int m_radius
Definition: 15-mixin.cpp:15
void display() const
Definition: 15-mixin.cpp:73
Mixin(BaseClasses... shape)
Definition: 15-mixin.cpp:67
int m_y
Definition: 15-mixin.cpp:9
int m_x
Definition: 15-mixin.cpp:9
void display() const
Definition: 15-mixin.cpp:42
Shape(Circle c, Square s)
Definition: 15-mixin.cpp:40
int m_side
Definition: 15-mixin.cpp:28
Point m_center
Definition: 15-mixin.cpp:27
void display() const
Definition: 15-mixin.cpp:30
This type is used to manipulate type list.
Definition: tpf_types.hpp:956
Stream output operators << are implemented.