8 template<
typename Type>
11 static constexpr bool value =
false;
14 template<
typename Type_1,
typename Type_2>
15 struct is_pair_type_st<
std::pair<Type_1, Type_2>>
17 static constexpr bool value =
true;
20 template<
typename Type>
21 constexpr bool is_pair_type_v = is_pair_type_st<remove_cv_ref_t<Type>>::value;
23 template<
typename Type>
24 struct is_pair_of_variant_type_st
26 static constexpr bool value =
false;
29 template<
typename KeyType,
typename... Types>
32 static constexpr bool value =
true;
35 template<
typename Type>
39 template<
typename Type>
42 static constexpr bool value =
false;
45 template<
typename... Types>
46 struct is_variant_type_st<
std::variant<Types...>>
48 static constexpr bool value =
true;
51 template<
typename Type>
52 constexpr bool is_variant_type_v = is_variant_type_st<remove_cv_ref_t<Type>>::value;
54 template<
typename Type,
typename ReturnType =
void>
57 template<
typename Type,
typename ReturnType =
void>
60 template<
typename Type,
typename ReturnType =
void>
63 template<
size_t StartIndex,
size_t EndIndex>
64 struct compile_time_loop
67 template<
typename VisitorType,
typename VariantType>
68 static enable_if_variant_t<VariantType>
71 if constexpr(StartIndex < EndIndex)
73 if(
auto ptr = std::get_if<StartIndex>(&vt))
76 std::get<StartIndex>(visitor.m_visitors)(*ptr);
81 if constexpr (StartIndex + 1 < EndIndex)
84 visit_variant(std::forward<VisitorType>(visitor) , std::forward<VariantType>(vt));
89 template<
typename VisitorType,
typename PairType>
93 if constexpr(StartIndex < EndIndex)
95 auto& [key, vt] = std::forward<PairType>(pair);
97 if(
auto ptr = std::get_if<StartIndex>(&vt))
100 std::get<StartIndex>(visitor.m_visitors)(key, *ptr);
105 if constexpr (StartIndex + 1 < EndIndex)
108 visit_variant(std::forward<VisitorType>(visitor) , std::forward<PairType>(pair));
115 template<
typename VisitorType,
typename VariantType>
116 enable_if_variant_t<VariantType>
117 visit(VisitorType&& visitor, VariantType&& vt)
122 constexpr size_t VariantSize = std::variant_size_v<variant_t>;
124 compile_time_loop<0, VariantSize>:: template
125 visit_variant(std::forward<VisitorType>(visitor), std::forward<VariantType>(vt));
129 template<
typename VisitorType,
typename PairType>
130 enable_if_pair_of_variant_t<PairType>
131 visit(VisitorType&& visitor, PairType&& pair)
134 using variant_t =
typename pair_t::second_type;
138 constexpr size_t VariantSize = std::variant_size_v<variant_t>;
140 compile_time_loop<0, VariantSize>:: template
141 visit_variant(std::forward<VisitorType>(visitor), std::forward<PairType>(pair));
144 template<
typename... VisitorTypes>
154 template<
typename ContainerType>
157 for(
decltype(
auto) vt:
158 std::forward<ContainerType>(container))
164 template<
typename VariantType>
171 template<
typename PairType>
181 template<
typename... VisitorTypes>
184 template<
typename... VisitorTypes>
185 overloaded<remove_cv_ref_t<VisitorTypes>...>
188 return { std::forward<VisitorTypes>(visitors)... };
197 using name_t =
const char*;
199 using weight_t = double;
200 using variant_t = std::variant<name_t, age_t, weight_t>;
202 using container_t = std::vector<variant_t>;
206 info.emplace_back(
"Thomas Kim");
207 info.emplace_back(30);
208 info.emplace_back(60.5);
209 info.emplace_back(
"Sophie Turner");
210 info.emplace_back(20);
211 info.emplace_back(56.7);
240 using key_t =
const char*;
241 using name_t =
const char*;
243 using weight_t = double;
244 using variant_t = std::variant<name_t, age_t, weight_t>;
246 using container_t = std::map<key_t, variant_t>;
250 info[
"Programmer"] =
"Thomas Kim";
252 info[
"Weight"] = 60.5;
253 info[
"Actress"] =
"Sophie Turner";
254 info[
"Her age"] = 20;
255 info[
"Her weight"] = 56.7;
283 using name_t =
const char*;
285 using weight_t = double;
287 using inner_vt_t = std::variant<name_t, weight_t>;
289 using variant_t = std::variant<name_t, age_t, weight_t, inner_vt_t>;
291 using container_t = std::vector<variant_t>;
295 info.emplace_back(
"Thomas Kim");
296 info.emplace_back(30);
297 info.emplace_back(60.5);
298 info.emplace_back(
"Sophie Turner");
299 info.emplace_back(20);
300 info.emplace_back(56.7);
301 info.emplace_back(inner_vt_t{
"James Dean"} );
302 info.emplace_back(inner_vt_t{ 65.6 } );
303 info.emplace_back(
"Sophie Kim");
336 stream <<
"Inner Weight is " << weight <<
endl;
339 )(std::forward<
decltype(vt)>(vt));
349 using key_t =
const char*;
350 using name_t =
const char*;
352 using weight_t = double;
354 using map_t = std::map<key_t, name_t>;
358 using variant_t = std::variant<name_t, age_t, weight_t, map_t, map_vt_t>;
360 using container_t = std::map<key_t, variant_t>;
364 info[
"Programmer"] =
"Thomas Kim";
366 info[
"Weight"] = 60.5;
367 info[
"Actress"] =
"Sophie Turner";
368 info[
"Her age"] = 20;
369 info[
"Her weight"] = 56.7;
370 info[
"Map of Map"] = map_t{ {
"Friend",
"James Dean"}, {
"Buddy",
"Albert Kim"} };
371 info[
"Map of Variants"] = map_vt_t{ {
"Friend's Age", 30}, {
"Buddy's Weight", 56.7} };
397 auto& [inner_key, value] = m;
399 stream <<
"Outer Key - " << key
400 <<
", inner_key: " << inner_key
401 <<
", value: " << value <<
endl;
409 [&
stream, &
endl, &key](
auto&& inner_key,
auto&& age)
411 stream <<
"Outer key: " << key
412 <<
", inner_key: " << inner_key<<
", age: " << age <<
endl;
415 [&
stream, &
endl, &key](
auto&& inner_key,
auto&& weight)
417 stream <<
"Outer key: " << key
418 <<
", inner_key: " << inner_key<<
", weight: " << weight <<
endl;
421 ).for_each(std::forward<
decltype(map_vt)>(map_vt));
std::remove_cv_t< std::remove_reference_t< Type > > remove_cv_ref_t
void test_visit_simplified_map()
void test_visit_simplified()
void test_visit_map_of_variants_of_map()
void test_visit_variant_of_variants()
Type to string name conversions are defined.
hidden::map_of_variants_t< KeyType, ElementTypes... > map_of_variants_t
std::enable_if_t< is_variant_type_v< Type >, ReturnType > enable_if_variant_t
std::enable_if_t< is_pair_type_v< Type >, ReturnType > enable_if_pair_t
overloaded< remove_cv_ref_t< VisitorTypes >... > make_overloaded(VisitorTypes &&... visitors)
constexpr bool is_variant_type_v
enable_if_variant_t< VariantType > visit(VisitorType &&visitor, VariantType &&vt)
overloaded(VisitorTypes...) -> overloaded< VisitorTypes... >
constexpr bool is_pair_of_variant_type_v
std::enable_if_t< is_pair_of_variant_type_v< Type >, ReturnType > enable_if_pair_of_variant_t
constexpr bool is_pair_type_v
std::enable_if_t< is_pair_of_variant_v< remove_cv_ref_t< PairType > > > visit_variant(VisitorType &&visit, PairType &&vpr)
std::remove_cv_t< std::remove_reference_t< Type > > remove_cv_ref_t
Remove const volatile reference from Type.
static enable_if_pair_of_variant_t< PairType > visit_variant(VisitorType &&visitor, PairType &&pair)
static enable_if_variant_t< VariantType > visit_variant(VisitorType &&visitor, VariantType &&vt)
static constexpr bool value
static constexpr bool value
static constexpr bool value
void for_each(ContainerType &&container)
enable_if_variant_t< VariantType > operator()(VariantType &&vt)
std::tuple< VisitorTypes... > vistors_t
enable_if_pair_of_variant_t< PairType > operator()(PairType &&pvt)
overloaded(VisitorTypes... visitors)
Stream output operators << are implemented.