ug_misc 0.1
Miscellaneous C++ header library
Loading...
Searching...
No Matches
member.hpp
Go to the documentation of this file.
1/*
2 * SPDX-Licence-Identifier: MIT
3 *
4 * Copyright 2025 Larry Chips
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the “Software”), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#pragma once
25
38
39#include "ugmisc/typelist.hpp"
40#include <utility>
41
42
43
44
45#ifdef UGMISC_DOCS
46#error
47#endif
48
49
50
51
52namespace ugmisc {
53
54
55
56
57#ifdef UGMISC_DOCS
58
66template<class...T>
73 template<class...U>
74 static constexpr decltype(auto) call(U&&...args);
75
76
87 template<class...U>
88 constexpr decltype(auto) operator() (U&&...) const;
89
90
117 template<class F>
118 static constexpr auto fallback(
119 F&& functor
120 );
121
122
128 template<class...U>
129 using matched_type = ???;
130
135 template<class...U>
136 static constexpr bool is_matched_v = ???;
137
138
142 template<class...U>
143 static constexpr bool is_matched(U&&...);
144
145
152 template<class...U>
153 static matched_type<U...> declval_matched(U&&...) noexcept;
154};
155
156#endif /* UGMISC_DOCS */
157
158
160namespace member_ {
161
162
163
164
179template<class StaticCaller, class Functor>
180class fallback_caller {
181 Functor m_functor;
182
183 /*
184 * The use of Dummy* for the first override and Dummy for the second, makes
185 * the first more specific.
186 */
187 template<class Dummy, class...T>
188 constexpr auto forward(Dummy*, T&&...args) const
189 -> decltype(m_functor(std::forward<T>(args)...))
190 {
191 return m_functor(std::forward<T>(args)...);
192 }
193
194 template<class Dummy, class...T>
195 constexpr decltype(auto) forward(Dummy, T&&...args) const {
196 return m_functor();
197 }
198
199
200public:
201 constexpr fallback_caller(const Functor& f) : m_functor(f) {}
202 constexpr fallback_caller(Functor&& f) : m_functor(std::move(f)) {}
203
204 fallback_caller(fallback_caller&&) = default;
205 fallback_caller(const fallback_caller&) = delete;
206
207 template<class...T>
208 constexpr decltype(auto) call(T&&...args) const {
209 if constexpr ( StaticCaller::template is_matched_v<T...> ) {
210 return StaticCaller::call(std::forward<T>(args)...);
211 } else {
212 auto dummy = (int*)nullptr;
213 return forward(dummy, std::forward<T>(args)...);
214 }
215 }
216
217 template<class...T>
218 constexpr decltype(auto) operator()(T&&...args) const {
219 return call(std::forward<T>(args)...);
220 }
221};
222
223
224
225
226
227} /* member_ (::ugmisc::member_) */
228
229
231
232} /* ::ugmisc */
233
234
235
236
277#define UGMISC_NAMED_MEMBER_TYPE_TEST(TEMPLATENAME, ALIASNAME, HASMEMBERNAME, NAME) template<class TYPE, class DEFAULTTYPE = void> \
278 class TEMPLATENAME { \
279 template<class...> struct get_defined_type; \
280 \
281 template<class V, class T, class...Ts> struct get_defined_type<V, T, Ts...> { \
282 static constexpr bool has_member = get_defined_type<void, Ts...>::has_member; \
283 using type = typename get_defined_type<void, Ts...>::type; \
284 }; \
285 \
286 template<class T, class...Ts> struct get_defined_type<std::void_t<typename T::NAME>, T, Ts...> { \
287 static constexpr bool has_member = true; \
288 using type = typename T::NAME; \
289 }; \
290 \
291 template<class V> struct get_defined_type<V> { \
292 static constexpr bool has_member = false; \
293 using type = DEFAULTTYPE; \
294 }; \
295 \
296 using tlist = ::ugmisc::flatten_t<void, TYPE>; \
297 using dt = typename tlist::template apply<get_defined_type>; \
298 \
299 public: \
300 using type = typename dt::type; \
301 static constexpr bool has_member = dt::has_member; \
302 }; \
303 \
304 template<class T, class D=void> \
305 using ALIASNAME = typename TEMPLATENAME<T, D>::type; \
306 \
307 template<class...T> \
308 static constexpr bool HASMEMBERNAME = TEMPLATENAME<type_list<T...>>::has_member
309
310
311
312
340#define UGMISC_MEMBER_TYPE_TEST(NAME) \
341 UGMISC_NAMED_MEMBER_TYPE_TEST(member_type_test_##NAME, member_type_##NAME, has_member_type_##NAME, NAME)
342
343
344
345
384#define UGMISC_NAMED_MEMBER_STATIC_METHOD_CALL(TEMPLATENAME, NAME) \
385 template<class...> class TEMPLATENAME; \
386 \
387 template<class FirstTry, class...Tries> \
388 struct TEMPLATENAME<FirstTry, Tries...> { \
389 template<class...> friend class TEMPLATENAME; \
390 \
391 private: \
392 template<class...T> struct Call : public TEMPLATENAME<Tries...>::template Call<T...> { \
393 }; \
394 \
395 template<class...T> struct Call<std::void_t<decltype(FirstTry::NAME(std::declval<T>()...))>, T...> { \
396 static constexpr bool is_matched = true; \
397 \
398 using matched_type = FirstTry; \
399 \
400 static constexpr decltype(auto) call(T...args) { \
401 return FirstTry::NAME(std::forward<T>(args)...); \
402 } \
403 }; \
404 \
405 public: \
406 \
407 template<class...T> \
408 static constexpr decltype(auto) call(T&&...args) { \
409 return Call<void, T...>::call(std::forward<T>(args)...); \
410 } \
411 \
412 template<class...T> \
413 decltype(auto) constexpr operator() (T&&...args) const { \
414 return TEMPLATENAME::call(std::forward<T>(args)...); \
415 } \
416 \
417 template<class F> \
418 static constexpr \
419 member_::fallback_caller<TEMPLATENAME, std::remove_reference_t<F>> \
420 fallback(F&& functor) { \
421 return std::forward<F>(functor); \
422 } \
423 \
424 template<class...T> \
425 using matched_type = typename Call<void, T...>::matched_type; \
426 \
427 template<class...T> \
428 static constexpr bool is_matched(T&&...) { return Call<void, T...>::is_matched; } \
429 \
430 template<class...T> \
431 static constexpr bool is_matched_v = Call<void, T...>::is_matched; \
432 \
433 template<class...T> \
434 static matched_type<T...> declval_matched(T&&...) noexcept; \
435 }; \
436 \
437 /* An empty type list doesn't ever match the call. */ \
438 template<> class TEMPLATENAME<> { \
439 template<class...> friend class TEMPLATENAME; \
440 private: \
441 template<class...> struct Call { \
442 static constexpr bool is_matched = false; \
443 }; \
444 \
445 public: \
446 template<class...T> \
447 static constexpr bool is_matched(T&&...) { return false; } \
448 }
449
450
458#define UGMISC_MEMBER_STATIC_METHOD_CALL(NAME) \
459 UGMISC_NAMED_MEMBER_STATIC_METHOD_CALL(member_call_##NAME, NAME)
Definition member.hpp:67
static constexpr decltype(auto) call(U &&...args)
static constexpr auto fallback(F &&functor)
??? matched_type
Definition member.hpp:129
static matched_type< U... > declval_matched(U &&...) noexcept
static constexpr bool is_matched(U &&...)
static constexpr bool is_matched_v
Definition member.hpp:136
Lists of types which may be used in some of the ugmisc templates where a single type would usually be...