Debian Codegraph @

yo/advancedtemplates/examples/multi.h

raw
1 #include <vector>
2
3 template <typename ...Types>
4 struct TypeList
5 {
6 TypeList(TypeList const &) = delete;
7 enum { size = sizeof ...(Types) };
8 };
9
10 //VECTOR
11 template <class Type>
12 struct Vector: public std::vector<Type>
13 {
14 Vector(std::initializer_list<Type> iniValues)
15 :
16 std::vector<Type>(iniValues)
17 {}
18 };
19 //=
20
21 //UWRAP
22 template <size_t nr, typename Type>
23 struct UWrap: public Type
24 {
25 UWrap(Type const &type)
26 :
27 Type(type)
28 {}
29 };
30 //=
31
32 //MULTIBASE
33 template <size_t nr, typename ...PolicyTypes>
34 struct MultiBase;
35 //=
36
37 //MULTIBASEREC
38 template <size_t nr, typename PolicyT1, typename ...PolicyTypes>
39 struct MultiBase<nr, PolicyT1, PolicyTypes...> :
40 public UWrap<nr, PolicyT1>,
41 public MultiBase<nr + 1, PolicyTypes...>
42 {
43 using Type = PolicyT1;
44 using Base = MultiBase<nr + 1, PolicyTypes...>;
45
46 MultiBase(PolicyT1 && policyt1, PolicyTypes &&...policytypes)
47 :
48 UWrap<nr, PolicyT1>(std::forward<PolicyT1>(policyt1)),
49 MultiBase<nr + 1, PolicyTypes...>(
50 std::forward<PolicyTypes>(policytypes)...)
51 {}
52 };
53 //=
54
55 //MULTIBASEDONE
56 template <size_t nr>
57 struct MultiBase<nr>
58 {};
59 //=
60
61 //MULTI
62 template <template <typename> class Policy, typename ...Types>
63 struct Multi: public MultiBase<0, Policy<Types>...>
64 {
65 using PlainTypes = TypeList<Types...>;
66 using Base = MultiBase<0, Policy<Types>...>;
67
68 enum { size = PlainTypes::size };
69
70 Multi(Policy<Types> &&...types)
71 :
72 MultiBase<0, Policy<Types>...>(
73 std::forward<Policy<Types>>(types)...)
74 {}
75 };
76 //=
77
78 template <typename TP>
79 struct st
80 {
81 static_assert(sizeof(TP) == 0, "");
82 };
83
84 //TYPEAT
85 template <size_t index, typename Multi>
86 class typeAt
87 {
88 template <size_t idx, typename MultiBase>
89 struct PolType;
90
91 template <size_t idx,
92 size_t nr, typename PolicyT1, typename ...PolicyTypes>
93 struct PolType<idx, MultiBase<nr, PolicyT1, PolicyTypes...>>
94 {
95 using Type = typename PolType< idx - 1, MultiBase<nr + 1,
96 PolicyTypes...> >::Type;
97 };
98
99 template <size_t nr, typename PolicyT1, typename ...PolicyTypes>
100 struct PolType<0, MultiBase<nr, PolicyT1, PolicyTypes...>>
101 {
102 using Type = PolicyT1;
103 };
104 public:
105 typeAt(typeAt const &) = delete;
106 using Type = typename PolType<index, typename Multi::Base>::Type;
107 };
108 //=
109
110 //GET
111 template <size_t idx, typename Multi>
112 inline typename typeAt<idx, Multi>::Type &get(Multi &multi)
113 {
114 return static_cast<
115 UWrap<idx, typename typeAt<idx, Multi>::Type> &>(multi);
116 }
117 //=
118
119 //PLAINAT
120 template <size_t index, typename Multi>
121 class plainTypeAt
122 {
123 template <size_t idx, typename List>
124 struct At;
125
126 template <size_t idx, typename Head, typename ...Tail>
127 struct At<idx, TypeList<Head, Tail...>>
128 {
129 using Type = typename At<idx - 1, TypeList<Tail...>>::Type;
130 };
131
132 template <typename Head, typename ...Tail>
133 struct At<0, TypeList<Head, Tail...>>
134 {
135 using Type = Head;
136 };
137
138 public:
139 plainTypeAt(plainTypeAt const &) = delete;
140 using Type = typename At<index, typename Multi::PlainTypes>::Type;
141 };
142 //=