-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (140 loc) · 5.42 KB
/
main.cpp
File metadata and controls
164 lines (140 loc) · 5.42 KB
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
#include <iostream>
#include <tuple>
#include <unordered_map>
using namespace std;
/** Possible child objects */
struct ChildObj1
{};
struct ChildObj2
{};
/* A base group object that can host child objects, possible children known in compile time */
template<typename... Visitables>
struct VisitableGroup
{
using VisitableChildren = std::tuple<Visitables...>;
template<typename T>
using Map = std::unordered_map<size_t, T>;
using MapType = std::tuple<Map<Visitables>...>;
const MapType &getChildMap() const noexcept { return _chidMap; }
MapType &getChildMap() noexcept { return _chidMap; }
MapType _chidMap;
};
/* A group object that can host child objects, possible children known in compile time */
struct Group : VisitableGroup<VisitableGroup<ChildObj1, ChildObj2>, ChildObj1, ChildObj2>
{
using VisitableGroup<VisitableGroup<ChildObj1, ChildObj2>, ChildObj1, ChildObj2>::VisitableChildren;
using VisitableGroup<VisitableGroup<ChildObj1, ChildObj2>, ChildObj1, ChildObj2>::getChildMap;
using VisitableGroup<VisitableGroup<ChildObj1, ChildObj2>, ChildObj1, ChildObj2>::MapType;
};
/* SFINAE class for requesting a group class to have a const getChildMap function */
template<typename T>
struct HasGetChildren
{
private:
typedef std::true_type yes;
typedef std::false_type no;
template<typename U>
static yes test(int) {
static_cast<const typename U::MapType&(U::*)() const>(&U::getChildMap);
return yes();
}
template<typename> static no test(...);
public:
static constexpr bool value = std::is_same<decltype(test<T>(0)),yes>::value;
};
template<typename VisitFunc>
struct FunctorTypeTraits;
/* Based on the functor it can be decided to execute lambdas differently */
template<typename Ret, typename T, typename... Params>
struct FunctorTypeTraits<Ret (T::*)(Params...)>
{
using Type = std::tuple_element_t<0, std::tuple<Params...>>;
constexpr static bool hasChildren = HasGetChildren<std::remove_reference_t<Type>>::value;
};
template<typename Ret, typename T, typename... Params>
struct FunctorTypeTraits<Ret (T::*)(Params...) const>
{
using Type = std::tuple_element_t<0, std::tuple<Params...>>;
constexpr static bool hasChildren = HasGetChildren<std::remove_reference_t<Type>>::value;
};
template<typename Ret, typename... Params>
struct FunctorTypeTraits<Ret (*)(Params...)>
{
using Type = std::tuple_element_t<0, std::tuple<Params...>>;
constexpr static bool hasChildren = HasGetChildren<std::remove_reference_t<Type>>::value;
};
template<typename VisitableFunc,
bool hasChildren = FunctorTypeTraits<decltype(&VisitableFunc::operator())>::hasChildren>
struct VisitFunc;
template<typename... VisitableFuncs>
struct Visitors;
/* A lambda inherited visitors class */
template<typename VisitableFunc, typename... VisitableFuncs>
struct Visitors<VisitableFunc, VisitableFuncs...> : VisitFunc<VisitableFunc>,
Visitors<VisitableFuncs...>
{
using VisitFunc<VisitableFunc>::operator();
Visitors(VisitableFunc func, VisitableFuncs... funcs)
: VisitFunc<VisitableFunc>(std::move(func)), Visitors<VisitableFuncs...>(std::move(funcs)...) {
}
};
template<typename VisitableFunc>
struct Visitors<VisitableFunc> : VisitFunc<VisitableFunc>
{
using VisitFunc<VisitableFunc>::operator();
Visitors(VisitableFunc func) : VisitFunc<VisitableFunc>(std::move(func)) {}
};
template<typename... Funcs>
auto makeVisitors(Funcs &&... funcs) {
return Visitors<Funcs...>(std::forward<Funcs>(funcs)...);
}
template<typename Visitable, typename... Funcs>
void visit(Visitable &visitable, Funcs &&... funcs) {
auto visitors = makeVisitors(std::forward<Funcs>(funcs)...);
visitors(visitable);
}
/* A custom lambda based on original lambda, with extra children traversal possibility */
template<typename VisitableFunc>
struct VisitFunc<VisitableFunc, true> : VisitableFunc
{
VisitFunc(VisitableFunc func) : VisitableFunc(std::move(func)) {}
template<size_t... Size, typename Type>
void visitChildren(std::index_sequence<Size...>, Type &¶m) const {
const auto visitor = [](auto &map)
{
// Visit your children
std::cout << typeid (decltype (map)).name() << std::endl;
};
const int32_t visits[] = {(visitor(std::get<Size>(param.getChildMap())), 0)...};
(void) visits;
}
template<typename Type, typename... Params>
auto operator()(Type &¶m, Params &&... params) const {
using ChildrenTuple = typename std::decay_t<Type>::VisitableChildren;
constexpr size_t size = std::tuple_size<ChildrenTuple>::value;
std::cout << "has children" << std::endl;
visitChildren(std::make_index_sequence<size>{}, std::forward<Type>(param));
return VisitableFunc::operator()(std::forward<Type>(param), std::forward<Type>(params)...);
}
};
/* A custom lambda based on original lambda, for nodes whic dont have traversal */
template<typename VisitableFunc>
struct VisitFunc<VisitableFunc, false> : VisitableFunc
{
VisitFunc(VisitableFunc func) : VisitableFunc(std::move(func)) {}
template<typename... Params>
auto operator()(Params &&... params) const {
std::cout << "has no children" << std::endl;
return VisitableFunc::operator()(std::forward<Params>(params)...);
}
};
int main()
{
Group group;
/*
* Visit group recursively. All possible lambdas here but better (and right) approach would be the ones
* provided by the group class.
*/
visit(group, [](const Group&) {}, [&](const ChildObj1&) {}, [&](const ChildObj2&) {});
return 0;
}