C++ Library Extensions 2022.12.09
To help learn modern C++ programming
28-set_of_variants.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
15
16 set_t my_set;
17
18 my_set.emplace("Thomas Kim");
19 my_set.emplace(20);
20 my_set.emplace(56.0);
21
23 auto endl = tpf::endl;
24
25 types::variant_handlers handle_map
26 {
27 [&stream, &endl](const int& age)
28 {
29 stream << "I am " << age << " years old. " << endl;
30 },
31
32 [&stream, &endl](const double& weight)
33 {
34 stream << "Weight is " << weight << " kg. " << endl;
35 },
36
37 [&stream, &endl](const char* const& name)
38 {
39 stream << "My name is " << name <<". " << endl;
40 },
41
42 [&stream, &endl](auto&& var)
43 {
44 stream <<"Possibly wrong parameter definition: "
45 << Tpf_GetTypeCategory(var) << endl;
46 }
47 };
48
49 handle_map.for_each(my_set);
50 stream << endl;
51}
52
54{
55 using category_t = const char*;
56 using name_t = std::string;
57 using age_t = int;
58 using weight_t = double;
59
60 using map_t =
62
63 map_t info;
64
65 info["Tom's name"] = "Thomas Kim";
66 info["Toms' age"] = 20;
67 info["Tom's weight"] = 60.5;
68
70 auto endl = tpf::endl;
71
72 types::variant_handlers handle_map
73 {
74 [&stream, &endl](const category_t& key, name_t& name)
75 {
76 stream << key << " is " << name << endl;
77 },
78
79 [&stream, &endl](const category_t& key, age_t& age)
80 {
81 stream << key << " is " << age << endl;
82 },
83
84 [&stream, &endl](const category_t& key, weight_t& weight)
85 {
86 stream << key << " is " << weight << endl;
87 },
88
89 [&stream, &endl](const category_t& key, auto catch_all)
90 {
91 stream << "Catch all: " << Tpf_GetTypeCategory(catch_all) << endl;
92 }
93 };
94
95 handle_map.for_each(info);
96 stream << endl;
97}
98
100{
101 using category_t = const char*;
102 using name_t = std::string;
103 using age_t = int;
104 using weight_t = double;
105
106 using map_t =
108
109 map_t info;
110
111 info["Tom's name"] = "Thomas Kim";
112 info["Toms' age"] = 20;
113 info["Tom's weight"] = 60.5;
114
116 auto endl = tpf::endl;
117
118 types::variant_handlers handle_map
119 {
120 [&stream, &endl](const category_t& key, name_t& name)
121 {
122 stream << key << " is " << name << endl;
123 },
124
125 [&stream, &endl](const category_t& key, age_t& age)
126 {
127 stream << key << " is " << age << endl;
128 },
129
130 [&stream, &endl](const category_t& key, weight_t& weight)
131 {
132 stream << key << " is " << weight << endl;
133 },
134
135 [&stream, &endl](const category_t& key, auto catch_all)
136 {
137 stream << "Catch all: " << Tpf_GetTypeCategory(catch_all) << endl;
138 }
139 };
140
141 handle_map.for_each(info);
142 stream << endl;
143}
144
146{
147 using category_t = const char*;
148 using name_t = std::string;
149 using age_t = int;
150 using weight_t = double;
151
153
154 map_t info;
155
156 info.emplace("Tom's name", "Thomas Kim");
157 info.emplace("Toms' age", 20);
158 info.emplace("Tom's weight", 60.5);
159
160 for(auto p: info)
161 {
162 auto& [key, value] = p;
163
164 stream <<key <<" is " << value << endl;
165 }
166}
167
169{
170 using category_t = const char*;
171 using name_t = const char*;
172 using age_t = int;
173 using weight_t = double;
174
176
177 using key_type = map_t::key_type;
178 using value_type = map_t::value_type;
179
180 map_t info;
181
182 info["Tom's name"] = "Thomas Kim";
183 info["Toms' age"] = 20;
184 info["Tom's weight"] = 60.5;
185
187 auto endl = tpf::endl;
188
189 types::variant_handlers handle_map
190 {
191 [&stream, &endl](const category_t& key, name_t& value)
192 {
193 stream << key << " is " << value << endl;
194 },
195
196 [&stream, &endl](const category_t& key, age_t& value)
197 {
198 stream << key << " is " << value << endl;
199 },
200
201 [&stream, &endl](const category_t& key, weight_t& value)
202 {
203 stream << key << " is "<< value << endl;
204 }
205 };
206
207 handle_map.for_each(info);
208
209}
210
212{
213 using category_t = const char*;
214 using name_t = std::string;
215 using age_t = int;
216 using weight_t = double;
217
219
220 map_t info;
221
222 info.emplace("Tom's name", "Thomas Kim");
223 info.emplace("Toms' age", 20);
224 info.emplace("Tom's weight", 60.5);
225
227 auto endl = tpf::endl;
228
229 types::variant_handlers handle_map
230 {
231 [&stream, &endl](const category_t& key, name_t& name)
232 {
233 stream << key << " is " << name << endl;
234 },
235
236 [&stream, &endl](const category_t& key, age_t& age)
237 {
238 stream << key << " is " << age << endl;
239 },
240
241 [&stream, &endl](const category_t& key, weight_t& weight)
242 {
243 stream << key << " is " << weight << endl;
244 }
245 };
246
247 handle_map.for_each(info);
248 stream << endl;
249
250 handle_map.for_each_reverse(info);
251 }
252
253
254int main()
255{
259
261
262
263}
std::deque< element_t > set_t
Definition: 061-subsets.cpp:9
tpf::sstream stream
void test_handle_unordered_maps()
void test_multi_maps()
void test_map_of_variants()
void test_unordered_map_of_variants()
auto endl
void test_set_of_variants()
void test_multimap_of_variants()
int main()
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
hidden::unordered_multimap_of_variants_t< KeyType, ElementTypes... > unordered_multimap_of_variants_t
Definition: tpf_types.hpp:6654
hidden::map_of_variants_t< KeyType, ElementTypes... > map_of_variants_t
Definition: tpf_types.hpp:6645
hidden::multimap_of_variants_t< KeyType, ElementTypes... > multimap_of_variants_t
Definition: tpf_types.hpp:6648
hidden::set_of_variants_t< ElementTypes... > set_of_variants_t
Definition: tpf_types.hpp:6630
hidden::unordered_map_of_variants_t< KeyType, ElementTypes... > unordered_map_of_variants_t
Definition: tpf_types.hpp:6651
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.
#define Tpf_GetTypeCategory(instance_arg)
A macro that returns instance_arg's type category string name.
Definition: tpf_types.hpp:1428