-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpreparedcontext.h
60 lines (45 loc) · 1.59 KB
/
preparedcontext.h
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
#ifndef JWUTIL_PREPAREDCONTEXT_H
#define JWUTIL_PREPAREDCONTEXT_H
#include <tuple>
namespace {
template<int Index, typename Search, typename First, typename... Types>
struct get_internal {
typedef typename get_internal<Index + 1, Search, Types...>::type type;
static constexpr int index = Index;
};
template<int Index, typename Search, typename... Types>
struct get_internal<Index, Search, Search, Types...> {
typedef get_internal type;
static constexpr int index = Index;
};
template<typename GetType, typename... Types>
GetType getFirst(std::tuple<Types...> tuple) {
return std::get<get_internal<0, GetType, Types...>::type::index>(tuple);
}
}
namespace jw_util {
template <typename... ArgTypes>
class PreparedContext {
template <typename... FriendArgTypes>
friend class PreparedContext;
public:
PreparedContext(ArgTypes &... newArgs)
: refs(newArgs...)
{}
template <typename... ParentContextArgs, typename... NewArgTypes>
PreparedContext(const PreparedContext<ParentContextArgs...> &parentContext, NewArgTypes &... newArgs)
: refs(getFirst<ArgTypes &>(std::tuple_cat(std::tuple<NewArgTypes &...>(newArgs...), parentContext.refs))...)
{}
template <typename GetType>
GetType &get() const {
return std::get<GetType &>(refs);
}
template <typename... NewArgTypes>
PreparedContext<ArgTypes..., NewArgTypes...> extend(NewArgTypes &... newArgs) const {
return PreparedContext<ArgTypes..., NewArgTypes...>(*this, newArgs...);
}
private:
std::tuple<ArgTypes &...> refs;
};
}
#endif // JWUTIL_PREPAREDCONTEXT_H