-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgreatcontainer3.cpp
206 lines (173 loc) · 5.49 KB
/
greatcontainer3.cpp
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#ifndef PPTEST
#include <iostream>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/back.hpp>
#include <boost/mpl/front.hpp>
#include <boost/mpl/pop_front.hpp>
#include <boost/mpl/eval_if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/mpl/empty.hpp>
#include <boost/mpl/size.hpp>
#include <boost/mpl/at.hpp>
#include <boost/mpl/contains.hpp>
#include <boost/type_traits/is_same.hpp>
#endif
#include <boost/preprocessor.hpp>
#ifndef POLICY_LIMIT
#define POLICY_LIMIT 5
#endif
namespace detail {
template<typename Sequence>
struct nada {
typedef Sequence policies;
};
template<typename Sequence, int Size>
struct create_vector_impl;
template<typename Sequence>
struct create_vector {
typedef
typename create_vector_impl<Sequence, boost::mpl::size<Sequence>::value >::type
type;
};
#define ELEMENT_AT(z, n, t) \
BOOST_PP_COMMA_IF(n) \
typename boost::mpl::at_c<Sequence, n>::type
#define CREATE_VECTOR_IMPL(z, n, t) \
template<typename Sequence> \
struct create_vector_impl<Sequence, n> { \
typedef BOOST_PP_CAT(boost::mpl::vector, n)< \
BOOST_PP_REPEAT(n, ELEMENT_AT, ~) \
> type; \
};
BOOST_PP_REPEAT(BOOST_PP_INC(POLICY_LIMIT), CREATE_VECTOR_IMPL, ~)
#undef CREATE_VECTOR_IMPL
#undef ELEMENT_AT
template<typename Sequence, typename Before>
struct iterate_policies {
typedef Sequence sequence;
typedef typename boost::mpl::front<sequence>::type first;
typedef typename boost::mpl::pop_front<sequence>::type next_sequence_raw;
typedef typename create_vector<next_sequence_raw>::type next_sequence;
typedef typename first::template policy<Before, next_sequence> second;
};
template<typename, typename> struct next_policy;
template<typename Sequence, typename Policy, typename Before = detail::nada<Sequence> >
struct find_policy {
typedef iterate_policies<Sequence, Before> it;
typedef
typename boost::mpl::eval_if<
boost::is_same<Policy, typename it::first>,
boost::mpl::identity<typename it::second>,
next_policy<it, Policy>
>::type type;
};
template<typename It, typename Policy>
struct next_policy {
typedef
typename find_policy<
typename It::next_sequence,
Policy,
typename It::second
>::type type;
};
template<typename Sequence>
struct combine {
typedef typename create_vector<Sequence>::type sequence;
typedef typename boost::mpl::back<sequence>::type back;
typedef typename find_policy<sequence, back>::type type;
};
}
template<typename Sequence>
struct policy_holder
: detail::combine<Sequence>::type
{
// typedef typename resulting_concept<Sequence>::concept concept;
};
template<typename T, typename H>
typename detail::find_policy<typename H::policies, T>::type &
as_policy(H &c) {
return *static_cast<
typename detail::find_policy<typename H::policies, T>::type *
>(&c);
}
template<typename T, typename H>
typename detail::find_policy<typename H::policies, T>::type const &
as_policy(H const &c) {
return *static_cast<
typename detail::find_policy<typename H::policies, T>::type const *
>(&c);
}
template<typename T, typename Policy>
struct with_policy
: boost::mpl::contains<typename T::policies, Policy>::type {};
template<typename Policy, typename T>
bool has_policy(T const &) {
return with_policy<T, Policy>::value;
}
template<typename T>
struct type {
template<typename Before, typename After>
struct policy : public Before {
typedef T value_type;
};
};
template<typename Adapt>
struct adaptor {
template<typename Before, typename After>
struct policy : public Before, public Adapt { };
};
// EXAMPLE:
struct array {
template<typename Before, typename After>
struct policy : public Before {
void push_back(typename Before::value_type const &) {
std::cout << "PUSH_BACK\n";
}
};
};
struct simple_storage {
template<typename Before, typename After>
struct policy : public Before {
protected:
typename Before::value_type storage;
};
};
struct setter_interface {
template<typename Before, typename After>
struct policy : public Before {
void set(typename Before::value_type const &n) {
Before::storage = n;
}
};
};
struct getter_x2_interface {
template<typename Before, typename After>
struct policy : public Before {
typename Before::value_type get() const {
return Before::storage * 2;
}
};
};
struct getter_interface {
template<typename Before, typename After>
struct policy : public Before {
typename Before::value_type get() const {
//return Before::storage;
return as_policy<getter_x2_interface>(*this).get() / 2;
}
};
};
int main() {
std::cout << "_Z6__type" << typeid(detail::combine<boost::mpl::vector2<type<int>, array> >::type).name() << '\n';
policy_holder<boost::mpl::vector2<type<int>, array> > foo;
foo.push_back(10);
policy_holder<boost::mpl::vector5<type<int>, simple_storage, setter_interface, getter_interface, getter_x2_interface> > bar;
bar.set(99);
std::cout << bar.get() << '\n';
std::cout << as_policy<getter_interface>(bar).get() << '\n';
std::cout << as_policy<getter_interface>(as_policy<getter_x2_interface>(bar)).get() << '\n';
std::cout << as_policy<getter_x2_interface>(as_policy<type<int> >(bar)).get() << '\n';
std::cout << has_policy<type<double> >(bar) << ',' << has_policy<type<int> >(bar) << std::endl;
//as_policy<type<double> >(bar); //funny error message
std::cout << "sizes: " << sizeof(foo) << ", " << sizeof(bar) << std::endl;
}