forked from wx257osn2/cxx_adc2020_sal_wrapper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sal_wrapper.hpp
206 lines (174 loc) · 6.64 KB
/
sal_wrapper.hpp
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
#ifndef SAL_WRAPPER_HPP_INCLUDED_
#define SAL_WRAPPER_HPP_INCLUDED_
// implement here
#include <functional>
#include <optional>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "super-awesome-library-that-has-breaking-changes-frequently.hpp"
namespace sal = super_awesome_library_that_has_breaking_changes_frequently;
namespace sal_wrapper {
namespace detail {
#define SAL_WRAPPER_DETAIL_DEF(name, expr) \
template <class T, class = void> \
struct name##_impl : ::std::false_type {}; \
template <class T> \
struct name##_impl<T, ::std::enable_if_t<expr>> : ::std::true_type {}; \
template <class T> \
using name = name##_impl<T>; \
template <class T> \
inline constexpr bool name##_v = name<T>::value;
#define SAL_WRAPPER_DETAIL_UNWRAP(...) __VA_ARGS__
SAL_WRAPPER_DETAIL_DEF(
has_a, SAL_WRAPPER_DETAIL_UNWRAP(
::std::is_same_v<decltype(::std::declval<T>().a(
::std::declval<::std::size_t>())),
int>))
SAL_WRAPPER_DETAIL_DEF(
has_a2, SAL_WRAPPER_DETAIL_UNWRAP(
::std::is_same_v<decltype(::std::declval<T>().a2(
::std::declval<::std::size_t>())),
int>))
SAL_WRAPPER_DETAIL_DEF(
has_bp, SAL_WRAPPER_DETAIL_UNWRAP(
::std::is_same_v<decltype(::std::declval<T>().b(
::std::declval<::std::size_t>())),
const int*>))
SAL_WRAPPER_DETAIL_DEF(
has_bo, SAL_WRAPPER_DETAIL_UNWRAP(
::std::is_same_v<decltype(::std::declval<T>().b(
::std::declval<::std::size_t>())),
::std::optional<const int*>>))
SAL_WRAPPER_DETAIL_DEF(
has_c, SAL_WRAPPER_DETAIL_UNWRAP(::std::is_same_v<decltype(T::c()), int>))
SAL_WRAPPER_DETAIL_DEF(
has_d1,
SAL_WRAPPER_DETAIL_UNWRAP(
::std::is_same_v<decltype(::std::declval<T>().d(::std::declval<int>())),
void>))
SAL_WRAPPER_DETAIL_DEF(
has_d2, SAL_WRAPPER_DETAIL_UNWRAP(
::std::is_same_v<decltype(::std::declval<T>().d(
::std::declval<int>(),
::std::declval<::sal::super_flags>())),
void>))
SAL_WRAPPER_DETAIL_DEF(
has_idx,
SAL_WRAPPER_DETAIL_UNWRAP(
::std::conjunction_v<
::std::is_same<decltype(::std::declval<T>().hyper_size()),
::std::size_t>,
::std::is_same<decltype(::std::declval<T>().access_hyper_data(
::std::declval<::std::size_t>())),
const ::std::string&>>))
SAL_WRAPPER_DETAIL_DEF(
has_rng,
SAL_WRAPPER_DETAIL_UNWRAP(
::std::conjunction_v<
::std::is_same<
decltype(::std::declval<T>().hypers().begin()->access()),
const ::std::string&>,
::std::is_same<
decltype(::std::declval<T>().hypers().end()->access()),
const ::std::string&>>))
#undef SAL_WRAPPER_DETAIL_UNWRAP
#undef SAL_WRAPPER_DETAIL_DEF
template <class T, class...>
struct sal_type_identity {
using type = T;
};
template <class T, class... Args>
using sal_type_identity_t = typename sal_type_identity<T, Args...>::type;
template <class A>
constexpr int a_impl(const A& awesome, ::std::size_t i) {
static_assert(::std::is_base_of_v<::sal::awesome_type, A>);
if constexpr (has_a_v<A>) {
return awesome.a(i);
} else if constexpr (has_a2_v<A>) {
return awesome.a2(i);
} else {
static_assert(!::std::is_base_of_v<::sal::awesome_type, A>);
return 0;
}
}
template <class A>
constexpr const int* b_impl(const A& awesome, ::std::size_t i) {
static_assert(::std::is_base_of_v<::sal::awesome_type, A>);
if constexpr (has_bp_v<A>) {
return awesome.b(i);
} else if constexpr (has_bo_v<A>) {
return awesome.b(i).value_or(nullptr);
} else {
static_assert(!::std::is_base_of_v<::sal::awesome_type, A>);
return nullptr;
}
}
} // namespace detail
constexpr int a(const ::sal::awesome_type& awesome, ::std::size_t i) {
return detail::a_impl(awesome, i);
}
constexpr const int* b(const ::sal::awesome_type& awesome, ::std::size_t i) {
return detail::b_impl(awesome, i);
}
} // namespace sal_wrapper
namespace super_awesome_library_that_has_breaking_changes_frequently {
namespace detail {
template <class... Args>
constexpr auto c_impl(Args&&... args) {
using A = ::sal_wrapper::detail::sal_type_identity_t<awesome_type, Args...>;
if constexpr (::sal_wrapper::detail::has_c_v<A>)
return A::c(::std::forward<Args>(args)...);
else
return c(::std::forward<Args>(args)...);
}
} // namespace detail
} // namespace super_awesome_library_that_has_breaking_changes_frequently
namespace sal_wrapper {
template <class... Args>
constexpr int c(Args&&... args) {
return ::sal::detail::c_impl(::std::forward<Args>(args)...);
}
namespace detail {
template <class E, class = void>
struct excellent_base_impl;
template <class E>
struct excellent_base_impl<E, ::std::enable_if_t<has_d1_v<E>>> : E {};
template <class E>
struct excellent_base_impl<E, ::std::enable_if_t<has_d2_v<E>>> : E {
virtual void d(int) = 0;
void d(int l, ::sal::super_flags) { this->d(l); }
};
template <class E>
auto hypers_impl(const E& excellent) {
if constexpr (has_idx_v<E>) {
struct hyper_data {
const E* ex;
::std::size_t i;
hyper_data(const E* e, ::std::size_t i) : ex(e), i(i) {}
const ::std::string& access() const { return ex->access_hyper_data(i); }
};
struct hyper_range {
const E* ex;
hyper_range(const E* e) : ex(e) {
::std::size_t sz = ex->hyper_size();
data.reserve(sz);
for (size_t i = 0; i < sz; ++i) data.emplace_back(ex, i);
}
auto begin() const { return data.begin(); }
auto end() const { return data.end(); }
private:
::std::vector<hyper_data> data;
};
return hyper_range(&excellent);
} else if constexpr (has_rng_v<E>)
return excellent.hypers();
}
} // namespace detail
struct excellent_base : detail::excellent_base_impl<::sal::excellent_type> {};
auto hypers(const ::sal::excellent_type& excellent) {
return detail::hypers_impl(excellent);
}
} // namespace sal_wrapper
#endif // SAL_WRAPPER_HPP_INCLUDED_