mutable
A Database System for Research and Fast Prototyping
Loading...
Searching...
No Matches
crtp.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <utility>
4
5
6namespace m {
7
48template<typename ConcreteType, template<typename...> typename CRTPBaseType, typename... TParams>
49struct crtp
50{
51 using actual_type = ConcreteType;
52 actual_type & actual() { return *static_cast<actual_type*>(this); }
53 const actual_type & actual() const { return *static_cast<const actual_type*>(this); }
54
55 private:
56 crtp() { } // no one can construct this
57 friend CRTPBaseType<actual_type, TParams...>; // except classes that properly inherit from this class
58};
59
62template<typename ConcreteType, template<typename, bool, typename...> typename CRTPBaseType, bool B,
63 typename... TParams>
65{
66 using actual_type = ConcreteType;
67 actual_type & actual() { return *static_cast<actual_type*>(this); }
68 const actual_type & actual() const { return *static_cast<const actual_type*>(this); }
69
70 private:
71 crtp_boolean() { } // no one can construct this
72 friend CRTPBaseType<ConcreteType, B, TParams...>; // except classes that properly inherit from this class
73};
74
77template<typename ConcreteType, template<typename, template<bool> typename, typename...> typename CRTPBaseType,
78 template<bool> typename It, typename... TParams>
80{
81 using actual_type = ConcreteType;
82 actual_type & actual() { return *static_cast<actual_type*>(this); }
83 const actual_type & actual() const { return *static_cast<const actual_type*>(this); }
84
85 private:
86 crtp_boolean_templated() { } // no one can construct this
87 friend CRTPBaseType<actual_type, It, TParams...>; // except classes that properly inherit from this class
88};
89
91template<typename Tag, bool Const = false> // a type tag unique to the method
93{
94 template<typename ReturnType> // return type
95 struct returns
96 {
97 template<typename... CRTPArgs> // types of CRTP parameters
98 struct crtp_args
99 {
100 template<typename... Args> // types of remaining parameters
101 struct args
102 {
103 /*----- Virtual base ---------------------------------------------------------------------------------*/
104 template<bool C, typename T, typename... Ts>
105 struct base_type_helper : virtual base_type_helper<C, T>
106 , virtual base_type_helper<C, Ts...>
107 {
108 using base_type_helper<C, T>::operator();
109 using base_type_helper<C, Ts...>::operator();
110 };
111 template<typename T>
112 struct base_type_helper<false, T>
113 {
114 virtual ReturnType operator()(Tag, T, Args...) = 0;
115 };
116 template<typename T>
117 struct base_type_helper<true, T>
118 {
119 virtual ReturnType operator()(Tag, T, Args...) const = 0;
120 };
121
122 /*----- Overriding implementation --------------------------------------------------------------------*/
123 template<bool C, typename Actual, typename T, typename... Ts>
125 , derived_type_helper<C, Actual, Ts...>
126 {
127 using derived_type_helper<C, Actual, T>::operator();
128 using derived_type_helper<C, Actual, Ts...>::operator();
129 };
130 template<typename Actual, typename T>
131 struct derived_type_helper<false, Actual, T> : virtual base_type_helper<false, T>
132 {
133 ReturnType operator()(Tag, T o, Args... args) override {
134 return static_cast<Actual*>(this)->template operator()<T>(
135 Tag{}, o, std::forward<Args>(args)...
136 );
137 }
138 };
139 template<typename Actual, typename T>
140 struct derived_type_helper<true, Actual, T> : virtual base_type_helper<true, T>
141 {
142 ReturnType operator()(Tag, T o, Args... args) const override {
143 return static_cast<const Actual*>(this)->template operator()<T>(
144 Tag{}, o, std::forward<Args>(args)...
145 );
146 }
147 };
148
149 /*----- The types to inherit from --------------------------------------------------------------------*/
150 using base_type = base_type_helper<Const, CRTPArgs...>;
151 template<typename Actual>
152 using derived_type = derived_type_helper<Const, Actual, CRTPArgs...>;
153 };
154 };
155 };
156};
157
158template<typename T>
160
161template<typename T>
163
164}
‍mutable namespace
Definition: Backend.hpp:10
T(x)
A helper class to introduce a virtual method overload per type to a class hierarchy.
Definition: crtp.hpp:93
A helper class to define CRTP class hierarchies with an additional boolean template template paramete...
Definition: crtp.hpp:80
ConcreteType actual_type
Definition: crtp.hpp:81
actual_type & actual()
Definition: crtp.hpp:82
const actual_type & actual() const
Definition: crtp.hpp:83
A helper class to define CRTP class hierarchies with an additional boolean template parameter (this i...
Definition: crtp.hpp:65
actual_type & actual()
Definition: crtp.hpp:67
ConcreteType actual_type
Definition: crtp.hpp:66
const actual_type & actual() const
Definition: crtp.hpp:68
A helper class to define CRTP class hierarchies.
Definition: crtp.hpp:50
crtp()
Definition: crtp.hpp:56
ConcreteType actual_type
Definition: crtp.hpp:51
actual_type & actual()
Definition: crtp.hpp:52
const actual_type & actual() const
Definition: crtp.hpp:53