1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
|
#include <iostream>
struct A { A() {std::cout << "A";} };
struct B { B() {std::cout << "B";} };
struct C { C() {std::cout << "C";} };
struct D { D() {std::cout << "D";} };
struct E { E() {std::cout << "E";} };
struct F { F() {std::cout << "F";} };
struct G { G() {std::cout << "G";} };
struct H { H() {std::cout << "H";} };
template <int KEY, typename... RANGE> struct Map {}; // one-to-many map (mapping KEY to RANGE...)
template <typename...> struct Data {};
using Database1 = Data< Map<0, A,B,C>, Map<1, D,E,F,G> >;
using Database2 = Data<
Map<0,
Map<0, A,B,C,D,E>,
Map<1, F,G>
>,
Map<1,
Map<0, A,B>,
Map<1, C,D,E,F,G>
>,
Map<2,
Map<0, A,B,C,D>,
Map<1, E,F,G>
>
>;
using Database3 = Data<
Map<0,
Map<0,
Map<0, A,B,C,D>,
Map<1, G,F,E>
>,
Map<1,
Map<0, A,B,C>,
Map<1, F,D>,
Map<2, E,G>
>,
Map<2,
Map<0, A,B,D>,
Map<1, C,E,F,G>
>
>,
Map<1,
Map<0,
Map<0, B,C,D,E>,
Map<1, A,F,G>
>,
Map<1,
Map<0, A,C>,
Map<1, D,G>,
Map<2, B,E>,
Map<3, F,A,B>
>
>,
Map<2,
Map<0,
Map<0, A,B,C,D,E,F,G>,
Map<1>
>,
Map<1>
>
>;
template <typename, int...>
struct GetRange;
template <template <typename...> class DATA, template <int, typename...> class MAP, int N, int KEY, int... REST, typename... NESTED_MAPS, typename... MAPS>
struct GetRange<DATA<MAP<KEY, NESTED_MAPS...>, MAPS...>, N, REST...> : GetRange<DATA<MAPS...>, N, REST...> {};
// If KEY != N, check the next map in MAPS... until KEY equals N.
template <template <typename...> class DATA, template <int, typename...> class MAP, int N, int... REST, typename... NESTED_MAPS, typename... MAPS>
struct GetRange<DATA<MAP<N, NESTED_MAPS...>, MAPS...>, N, REST...> : GetRange<MAP<N, NESTED_MAPS...>, N, REST...> {};
// Here the key matches with N, so move into the first map in DATA.
template <template <int, typename...> class MAP, int N, int S, int KEY, int... REST, typename... NESTED_MAPS, typename... MAPS>
struct GetRange<MAP<N, MAP<KEY, NESTED_MAPS...>, MAPS...>, N, S, REST...> : GetRange<MAP<N, MAPS...>, N, S, REST...> {};
// Search in MAPS... until KEY equals S.
template <template <int, typename...> class MAP, int N, int KEY, int... REST, typename... NESTED_MAPS, typename... MAPS>
struct GetRange<MAP<N, MAP<KEY, NESTED_MAPS...>, MAPS...>, N, KEY, REST...> : GetRange<MAP<KEY, NESTED_MAPS...>, KEY, REST...> {};
// Outer and inner keys match, so move into the inner map.
template <template <int, typename...> class MAP, int N, typename... TYPES>
struct GetRange<MAP<N, TYPES...>, N> { // End of recursion.
using type = Data<TYPES...>;
};
template <typename...> struct DoWhatever {}; // Function object for testing only.
template <typename FIRST, typename... REST>
struct DoWhatever<FIRST, REST...> {
void operator()() {
FIRST* x = new FIRST;
// Do whatever with x.
delete x;
DoWhatever<REST...>()();
}
};
template <>
struct DoWhatever<> {
void operator()() {std::cout << "\n";}
};
template <typename... TYPES>
void extractTemplatePackHelper (Data<TYPES...>&&) {
DoWhatever<TYPES...>()();
}
template <typename DATABASE, int... I>
void extractTemplatePack() {
extractTemplatePackHelper (typename GetRange<DATABASE, I...>::type{});
}
int main() {
std::cout << "Database1:" << std::endl;
extractTemplatePack<Database1, 0>();
extractTemplatePack<Database1, 1>();
std::cout << "-------------------\nDatabase2:" << std::endl;
extractTemplatePack<Database2, 0,0>();
extractTemplatePack<Database2, 0,1>();
extractTemplatePack<Database2, 1,0>();
extractTemplatePack<Database2, 1,1>();
extractTemplatePack<Database2, 2,0>();
extractTemplatePack<Database2, 2,1>();
std::cout << "-------------------\nDatabase3:" << std::endl;
extractTemplatePack<Database3, 0,0,0>();
extractTemplatePack<Database3, 0,0,1>();
extractTemplatePack<Database3, 0,1,0>();
extractTemplatePack<Database3, 0,1,1>();
extractTemplatePack<Database3, 0,1,2>();
extractTemplatePack<Database3, 0,2,0>();
extractTemplatePack<Database3, 0,2,1>();
extractTemplatePack<Database3, 1,0,0>();
extractTemplatePack<Database3, 1,0,1>();
extractTemplatePack<Database3, 1,1,0>();
extractTemplatePack<Database3, 1,1,1>();
extractTemplatePack<Database3, 1,1,2>();
extractTemplatePack<Database3, 1,1,3>();
extractTemplatePack<Database3, 2,0,0>();
extractTemplatePack<Database3, 2,0,1>();
extractTemplatePack<Database3, 2,1>();
std::cin.get();
}
|