kDB: Knowledge DataBase
Loading...
Searching...
No Matches
NodeVisitor.h
1#include "AbstractNodeVisitor.h"
2
3namespace kDB::SPARQL::Algebra
4{
5 namespace details
6 {
7 template<typename _TVisitor_, typename _T_>
8 struct Call
9 {
10 template<int ...S>
11 static _TVisitor_::ReturnType accept(_TVisitor_* _visitor, const _T_& _t, const _TVisitor_::ParametersTuple& _parameters, std::index_sequence<S...>) {
12 return _visitor->visit(_t, std::get<S>(_parameters)...);
13 }
14 };
15 template<typename _TVisitor_>
16 struct Call<_TVisitor_, NodeCSP>
17 {
18 template<int ...S>
19 static _TVisitor_::ReturnType accept(_TVisitor_* _visitor, const NodeCSP& _t, const _TVisitor_::ParametersTuple& _parameters, std::index_sequence<S...>) {
20 return _visitor->accept(_t, std::get<S>(_parameters)...);
21 }
22 };
23
24 template<typename _T_>
25 struct RemoveConstExplicitlySharedDataPointer { typedef _T_ type; };
26 template<typename _T_>
27 struct RemoveConstExplicitlySharedDataPointer<knowCore::ConstExplicitlySharedDataPointer<_T_>> { typedef _T_ type; };
28
29 template<typename _T_>
30 using RemoveConstExplicitlySharedDataPointerT = typename RemoveConstExplicitlySharedDataPointer<_T_>::type;
31
32 template<typename _TVisitor_, typename _T_, typename = void>
34
35 template<typename _TVisitor_, typename _T_>
36 struct VisitorHelper<_TVisitor_, knowCore::ConstExplicitlySharedDataPointer<_T_>, std::enable_if_t<std::is_base_of_v<Node, _T_>>>
37 {
38 static _TVisitor_::ReturnType call(_TVisitor_* _visitor, const knowCore::ConstExplicitlySharedDataPointer<_T_>& _t, const _TVisitor_::ParametersTuple& _parameters)
39 {
40 if(_t)
41 {
42 return Call<_TVisitor_, knowCore::ConstExplicitlySharedDataPointer<_T_>>::accept(_visitor, _t, _parameters, std::make_index_sequence<_TVisitor_::ParametersCount>());
43 }
44 return typename _TVisitor_::ReturnType();
45 }
46 };
47 template<typename _TVisitor_, typename _T_>
48 struct VisitorHelper<_TVisitor_, _T_, std::enable_if_t<not std::is_base_of_v<Node, RemoveConstExplicitlySharedDataPointerT<_T_>>
49 and not knowCore::details::is_specialisation_of_v<QList, _T_>
50 and not knowCore::details::is_specialisation_of_v<Alternative, _T_>>>
51 {
52 static _TVisitor_::ReturnType call(_TVisitor_* _visitor, const _T_& _t, const _TVisitor_::ParametersTuple& _parameters)
53 {
54 Q_UNUSED(_t);
55 Q_UNUSED(_visitor);
56 Q_UNUSED(_parameters);
57 return typename _TVisitor_::ReturnType();
58 }
59 };
60 template<typename _TVisitor_, typename... _T_>
61 struct VisitorHelper<_TVisitor_, Alternative<_T_...>>
62 {
63 static _TVisitor_::ReturnType call(_TVisitor_* _visitor, const Alternative<_T_...>& _t, const _TVisitor_::ParametersTuple& _parameters)
64 {
65 if(_t.node())
66 {
67 return Call<_TVisitor_, NodeCSP>::accept(_visitor, _t.node(), _parameters, std::make_index_sequence<_TVisitor_::ParametersCount>());
68 } else {
69 return typename _TVisitor_::ReturnType();
70 }
71 }
72 };
73 template<typename _TVisitor_, typename _T_>
74 struct VisitorHelper<_TVisitor_, QList<_T_>, std::enable_if_t<std::is_base_of_v<Node, RemoveConstExplicitlySharedDataPointerT<_T_>>
75 or knowCore::details::is_specialisation_of_v<Alternative, _T_>>>
76 {
77 static _TVisitor_::ReturnType call(_TVisitor_* _visitor, const QList<_T_>& _t, const _TVisitor_::ParametersTuple& _parameters)
78 {
79 for(const _T_& t : _t)
80 {
81 VisitorHelper<_TVisitor_, _T_>::call(_visitor, t, _parameters);
82 }
83 return typename _TVisitor_::ReturnType();
84 }
85 };
86 template<typename _TVisitor_, typename _T_>
87 struct VisitorHelper<_TVisitor_, QList<_T_>, std::enable_if_t<not std::is_base_of_v<Node, RemoveConstExplicitlySharedDataPointerT<_T_>> and not knowCore::details::is_specialisation_of_v<Alternative, _T_> >>
88 {
89 static _TVisitor_::ReturnType call(_TVisitor_* _visitor, const QList<_T_>& _t, const _TVisitor_::ParametersTuple& _parameters)
90 {
91 Q_UNUSED(_t);
92 Q_UNUSED(_visitor);
93 Q_UNUSED(_parameters);
94 return typename _TVisitor_::ReturnType();
95 }
96 };
97 }
98 // TODO rename to TraverseNodeVisitor?
99 template<typename _TR_, typename... _TArgs_>
100 class NodeVisitor : public AbstractNodeVisitor<_TR_, _TArgs_...>
101 {
102 template<typename _TVisitor_, typename _T_>
103 friend struct details::Call;
104
105 public:
106 NodeVisitor() {}
107 ~NodeVisitor() {}
108 private:
109#define KDB_SPARQL_GENERATE_VISIT_CALL(_KLASS_NAME_, _TYPE_, _NAME_) \
110 details::VisitorHelper<NodeVisitor<_TR_, _TArgs_...>, _TYPE_>::call(this, _node->_NAME_(), std::make_tuple(_parameters...));
111
112#define KDB_SPARQL_ALGEBRA_GENERATE(_KLASS_NAME_, _MEMBER_DEF_) \
113 _TR_ visit(_KLASS_NAME_ ## CSP _node, const _TArgs_&... _parameters) override \
114 { \
115 _MEMBER_DEF_(_KLASS_NAME_, KDB_SPARQL_GENERATE_VISIT_CALL) \
116 return _TR_(); \
117 }
118#include "NodesDefs.h"
119#undef KDB_SPARQL_ALGEBRA_GENERATE
120#undef KDB_SPARQL_GENERATE_VISIT_CALL
121 };
122}
Definition Revision.h:9
Definition NodeVisitor.h:101
Definition NodeVisitor.h:9