-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixin.hpp
163 lines (127 loc) · 2.69 KB
/
mixin.hpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef MIXOR_MIXIN_HPP
#define MIXOR_MIXIN_HPP
#include<type_traits>
#define REQUIRES(C) std::enable_if_t<(C), int> = 0
namespace mixor {
template<class X>
constexpr bool any(X const& x) {
return (bool)x;
}
template<class X, class Y>
constexpr bool any(X const& x, Y const& y) {
return x || y;
}
template<class X, class...Xs>
constexpr bool any(X const& x, Xs const&...xs) {
return any(x, any(xs...) );
}
template<class B, class L, class R,
REQUIRES( (std::is_convertible<R,B>::value) )>
B either(L const&, R const& r) {
return r;
}
template<class B, class L, class R,
REQUIRES( (!std::is_convertible<R,B>::value) )>
B either(L const& l, R const&) {
return l;
}
template<class...>
struct Mixin
{};
template<class B>
struct Mixin<B>
: B {
Mixin(B const& b)
: B{b}
{}
template<class...X>
Mixin( Mixin<X...> const& rhs)
: B{ either<B>(B{}, rhs) }
{}
Mixin()=default;
Mixin(Mixin const&)=default;
};
template<class B, class...Bs>
struct Mixin<B, Bs...>
: B
, Mixin<Bs...> {
Mixin(B const& b, Bs const&...bs)
: B{b}, Mixin<Bs...>{bs...}
{}
template<class...X>
Mixin( Mixin<X...> const& rhs)
: B{ either<B>(B{}, rhs) }
, Mixin<Bs...>{ either<Bs>(Bs{}, rhs)... }
{}
Mixin()=default;
Mixin(Mixin const&)=default;
};
template<class...B>
constexpr auto mergeFragment(
Mixin<B...> const& b
) {
return b;
}
template<class X, class...B,
REQUIRES( !any(
std::is_same<X,B>::value...
))>
constexpr auto mergeFragment(
Mixin<B...> const& b,
X const& x
) {
return Mixin<B..., X>{
(B const&)b..., x
};
}
template<class X, class...B,
REQUIRES( any(
std::is_same<X,B>::value...
))>
constexpr auto mergeFragment(Mixin<B...> const& b, X const& x) {
return Mixin<B...> {
either<B>(b, x)...
};
}
template<class X, class...Xs, class...B>
constexpr auto mergeFragment(Mixin<B...> const& b, X const& x, Xs const&...xs) {
return mergeFragment(mergeFragment(b,x), xs...);
}
template<class L>
constexpr auto merge(L const& l) {
return Mixin<L>{l};
}
template<class...L>
constexpr auto merge(Mixin<L...> const& l) {
return l;
}
template<class...L, class...R>
constexpr auto merge(Mixin<L...> const& l, Mixin<R...> const& r) {
return mergeFragment(
l, (R)r...
);
}
template<class X, class...Xs>
constexpr auto merge(X const& x, Xs const&...xs) {
return merge(
merge(x),
merge(xs...)
);
}
static auto mix= [](auto const&...x) {
return merge(x...);
};
template<class...B, class...X>
constexpr Mixin<B...> strip(Mixin<X...> const& m) {
return {
either<B>(B{},m)...
};
}
template<class...B, class...X>
constexpr Mixin<B...> strip(Mixin<B...> base, Mixin<X...> const& m) {
return {
either<B>(base, m)...
};
}
}
#endif