C++ Library Extensions 2022.12.09
To help learn modern C++ programming
34-any.cpp
Go to the documentation of this file.
1#include <tpf_output.hpp>
2
5
6namespace types = tpf::types;
7
9{
10 using name_t = const char*;
11 using age_t = int;
12 using weight_t = double;
13
14 using price_t = float;
15 using title_t = const char*;
16
17 using author_t = const char*;
18
19 using snack_t = std::tuple<name_t, weight_t>;
20 using book_t = std::tuple<title_t, price_t, author_t>;
21 using device_t = std::tuple<name_t, price_t, weight_t>;
22
24
25 using bag_t = std::vector<item_t>;
26
27 bag_t bag;
28
29 bag.emplace_back( snack_t{ "Honey choco", 300.0 });
30
31 bag.emplace_back( book_t{ "Good Programmer", 30.0f, "Thomas Kim" });
32
33 bag.emplace_back( device_t{ "Good Programmer", 1000.0f, 1.6 });
34
35 stream << bag << endl;
36
37 for(auto&& e: bag)
38 {
39 stream << e << endl;
40 }
41}
42
44{
45 using name_t = const char*;
46 using age_t = int;
47 using weight_t = double;
48
49 using price_t = float;
50 using title_t = const char*;
51 using author_t = const char*;
52
53 using snack_t = std::tuple<name_t, weight_t>;
54 using snacks_t = std::vector<snack_t>;
55
56 using book_t = std::tuple<title_t, price_t, author_t>;
57 using books_t = std::vector<book_t>;
58
59 using device_t = std::tuple<name_t, price_t, weight_t>;
60 using devices_t = std::vector<device_t>;
61
62 using belongings_t =
64
65 belongings_t mine;
66
67 mine = snack_t{"Yummy Choco", 300.0 };
68
69 stream << mine << endl;
70
71 mine = snacks_t{ snack_t{" Yummy Choco", 300}, snack_t{"Sweet Potato", 200} };
72
73 stream << mine << endl;
74
75 mine = books_t{ book_t{"Good Book", 20.0f, "Thomas Kim"},
76 book_t{"Poor Book", 20.0f, "Thomas Kim"}, book_t{"Excellent Book", 20.0f, "Thomas Kim"} };
77
78 stream << mine << endl;
79
80}
81
83{
84
85 using cate_t = const char*;
86 using name_t = const char*;
87 using age_t = int;
88 using weight_t = double;
89
90 using price_t = float;
91 using title_t = const char*;
92 using author_t = const char*;
93
94 using book_t = std::tuple<cate_t, title_t, weight_t>;
95 using device_t = std::tuple<cate_t, name_t, price_t>;
96 using thing_t = types::any<book_t, device_t>;
97
98 using person_t = std::tuple<cate_t, thing_t>;
99
100 person_t tom{"person", book_t{ "book", "Good Man", 300 } };
101
102 stream << tom << endl;
103
104 tom = person_t{"person", device_t{"device", "MyPowerLaptop", 1000} };
105
106 stream << tom << endl;
107
108 auto& [cate, thing] = tom;
109
110 stream << "Person: " << cate << endl;
111
112 thing.if_current_type<book_t>([](auto&& book)
113 {
114 stream << "I love this book [" << book <<"]" << endl;
115 });
116
117 thing.if_current_type<device_t>([](auto&& device)
118 {
119 stream << "I love this device [" << device <<"]" << endl;
120 });
121}
122
124{
125 using name_t = const char*;
126 using age_t = int;
127 using weight_t = double;
128
129 enum {idx_name, idx_age, idx_weight};
130
132
133 using tuple_t = std::tuple<name_t, any_t>;
134
135 using container_t = std::vector<tuple_t>;
136
137 container_t things;
138
139 things.emplace_back("name", "Thomas Kim");
140 things.emplace_back("age", 30);
141 things.emplace_back("weight", 60.0);
142
143 stream << things << endl;
144
145 auto handle_name = [](auto&& value)
146 {
147 stream << "\""<< value << "\" is my friend!" << endl;
148 };
149
150 auto handle_age = [](auto&& value)
151 {
152 stream << "He is "<< value << " old." << endl;
153 };
154
155 auto handle_weight = [](auto&& value)
156 {
157 stream << "He weighs "<< value << " kilogram." << endl;
158 };
159
160 for(auto&& thing: things)
161 {
162 auto[cate, item] = thing;
163
164 types::handle_any(item, handle_name, handle_age, handle_weight);
165 }
166
167 stream << endl << endl;
168
169 for(auto&& thing: things)
170 {
171 auto[cate, item] = thing;
172
173 item.if_current_type<name_t>(handle_name);
174
175 // item.if_current_type<age_t>(handle_age);
176 // item.if_current_type<weight_t>(handle_weight);
177 }
178
179 for(auto&& thing: things)
180 {
181 auto[cate, item] = thing;
182
183 types::if_current_index<idx_name>(item, handle_name);
184
185 // item.if_current_type<age_t>(handle_age);
186 // item.if_current_type<weight_t>(handle_weight);
187 }
188
189
190}
191
192int main()
193{
194 // _1_test_type_functions();
195
196 // _2_test_print_elements();
197
198 // _3_test_any_as_tuple_elements();
199
201}
tpf::sstream stream
Definition: 34-any.cpp:3
void _2_test_print_elements()
Definition: 34-any.cpp:43
void _1_test_type_functions()
Definition: 34-any.cpp:8
void _4_test_any_as_container_elements()
Definition: 34-any.cpp:123
auto endl
Definition: 34-any.cpp:4
void _3_test_any_as_tuple_elements()
Definition: 34-any.cpp:82
int main()
Definition: 34-any.cpp:192
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.