|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <immer/flex_vector.hpp> |
| 4 | +#include <immer/map.hpp> |
| 5 | +#include <immer/set.hpp> |
| 6 | +#include <immer/table.hpp> |
| 7 | +#include <immer/vector.hpp> |
| 8 | + |
| 9 | +#include <boost/hana.hpp> |
| 10 | + |
| 11 | +namespace immer::persist::util { |
| 12 | + |
| 13 | +namespace detail { |
| 14 | + |
| 15 | +namespace hana = boost::hana; |
| 16 | + |
| 17 | +template <class T, class = void> |
| 18 | +struct get_inner_types_t |
| 19 | +{ |
| 20 | + static auto apply() |
| 21 | + { |
| 22 | + return hana::make_tuple( |
| 23 | + hana::make_pair(hana::type_c<T>, BOOST_HANA_STRING(""))); |
| 24 | + } |
| 25 | +}; |
| 26 | + |
| 27 | +template <class T> |
| 28 | +struct get_inner_types_t<T, std::enable_if_t<hana::Struct<T>::value>> |
| 29 | +{ |
| 30 | + static auto apply() |
| 31 | + { |
| 32 | + auto value = T{}; |
| 33 | + return hana::transform(hana::keys(value), [&](auto key) { |
| 34 | + const auto& member = hana::at_key(value, key); |
| 35 | + return hana::make_pair(hana::typeid_(member), key); |
| 36 | + }); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +template <typename T, |
| 41 | + typename MemoryPolicy, |
| 42 | + immer::detail::rbts::bits_t B, |
| 43 | + immer::detail::rbts::bits_t BL> |
| 44 | +struct get_inner_types_t<immer::vector<T, MemoryPolicy, B, BL>> |
| 45 | + : get_inner_types_t<T> |
| 46 | +{}; |
| 47 | + |
| 48 | +template <typename T, |
| 49 | + typename MemoryPolicy, |
| 50 | + immer::detail::rbts::bits_t B, |
| 51 | + immer::detail::rbts::bits_t BL> |
| 52 | +struct get_inner_types_t<immer::flex_vector<T, MemoryPolicy, B, BL>> |
| 53 | + : get_inner_types_t<T> |
| 54 | +{}; |
| 55 | + |
| 56 | +template <typename T, typename MemoryPolicy> |
| 57 | +struct get_inner_types_t<immer::box<T, MemoryPolicy>> : get_inner_types_t<T> |
| 58 | +{}; |
| 59 | + |
| 60 | +template <typename T, |
| 61 | + typename Hash, |
| 62 | + typename Equal, |
| 63 | + typename MemoryPolicy, |
| 64 | + immer::detail::hamts::bits_t B> |
| 65 | +struct get_inner_types_t<immer::set<T, Hash, Equal, MemoryPolicy, B>> |
| 66 | + : get_inner_types_t<T> |
| 67 | +{}; |
| 68 | + |
| 69 | +template <typename K, |
| 70 | + typename T, |
| 71 | + typename Hash, |
| 72 | + typename Equal, |
| 73 | + typename MemoryPolicy, |
| 74 | + immer::detail::hamts::bits_t B> |
| 75 | +struct get_inner_types_t<immer::map<K, T, Hash, Equal, MemoryPolicy, B>> |
| 76 | +{ |
| 77 | + static auto apply() |
| 78 | + { |
| 79 | + return hana::concat(get_inner_types_t<K>::apply(), |
| 80 | + get_inner_types_t<T>::apply()); |
| 81 | + } |
| 82 | +}; |
| 83 | + |
| 84 | +template <typename T, |
| 85 | + typename KeyFn, |
| 86 | + typename Hash, |
| 87 | + typename Equal, |
| 88 | + typename MemoryPolicy, |
| 89 | + immer::detail::hamts::bits_t B> |
| 90 | +struct get_inner_types_t<immer::table<T, KeyFn, Hash, Equal, MemoryPolicy, B>> |
| 91 | + : get_inner_types_t<T> |
| 92 | +{}; |
| 93 | + |
| 94 | +template <class... Types> |
| 95 | +struct get_inner_types_t<std::variant<Types...>> |
| 96 | +{ |
| 97 | + static auto apply() |
| 98 | + { |
| 99 | + return hana::make_tuple( |
| 100 | + hana::make_pair(hana::type_c<Types>, BOOST_HANA_STRING(""))...); |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +constexpr auto insert_conditionally = [](auto map, auto pair) { |
| 105 | + namespace hana = hana; |
| 106 | + using contains_t = |
| 107 | + decltype(hana::is_just(hana::find(map, hana::first(pair)))); |
| 108 | + if constexpr (contains_t::value) { |
| 109 | + const auto empty_string = BOOST_HANA_STRING(""); |
| 110 | + using is_empty_t = decltype(hana::second(pair) == empty_string); |
| 111 | + if constexpr (is_empty_t::value) { |
| 112 | + // Do not replace with empty string |
| 113 | + return map; |
| 114 | + } else { |
| 115 | + return hana::insert(map, pair); |
| 116 | + } |
| 117 | + } else { |
| 118 | + return hana::insert(map, pair); |
| 119 | + } |
| 120 | +}; |
| 121 | + |
| 122 | +} // namespace detail |
| 123 | + |
| 124 | +/** |
| 125 | + * Generate a map (type, member_name) for all members of a given type, |
| 126 | + * recursively. |
| 127 | + */ |
| 128 | +inline auto get_inner_types(const auto& type) |
| 129 | +{ |
| 130 | + namespace hana = boost::hana; |
| 131 | + |
| 132 | + constexpr auto get_for_one_type = [](auto type) { |
| 133 | + using T = typename decltype(type)::type; |
| 134 | + return detail::get_inner_types_t<T>::apply(); |
| 135 | + }; |
| 136 | + |
| 137 | + constexpr auto get_for_many = [get_for_one_type](const auto& map) { |
| 138 | + auto new_pairs = hana::to_tuple(map) | [get_for_one_type](auto pair) { |
| 139 | + return get_for_one_type(hana::first(pair)); |
| 140 | + }; |
| 141 | + |
| 142 | + return hana::fold_left( |
| 143 | + hana::to_map(new_pairs), map, detail::insert_conditionally); |
| 144 | + }; |
| 145 | + |
| 146 | + constexpr auto can_expand = [get_for_many](const auto& set) { |
| 147 | + return set != get_for_many(set); |
| 148 | + }; |
| 149 | + |
| 150 | + auto expanded = hana::while_( |
| 151 | + can_expand, hana::to_map(get_for_one_type(type)), get_for_many); |
| 152 | + |
| 153 | + // Throw away types we don't know names for |
| 154 | + const auto empty_string = BOOST_HANA_STRING(""); |
| 155 | + auto result = |
| 156 | + hana::filter(hana::to_tuple(expanded), [empty_string](auto pair) { |
| 157 | + return hana::second(pair) != empty_string; |
| 158 | + }); |
| 159 | + return hana::to_map(result); |
| 160 | +} |
| 161 | + |
| 162 | +} // namespace immer::persist::util |
0 commit comments