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
41
42#include "ugmisc/typelist.hpp"
43#include <utility>
44
45
46
47
48#ifdef UGMISC_DOCS
49#error
50#endif
51
52
53
54
55namespace ugmisc {
56
57
58
59
60#ifdef UGMISC_DOCS
61
69template<class...T>
76 template<class...U>
77 static constexpr decltype(auto) call(U&&...args);
78
79
90 template<class...U>
91 constexpr decltype(auto) operator() (U&&...) const;
92
93
120 template<class F>
121 static constexpr auto fallback(
122 F&& functor
123 );
124
125
131 template<class...U>
132 using matched_type = ???;
133
138 template<class...U>
139 static constexpr bool is_matched_v = ???;
140
141
145 template<class...U>
146 static constexpr bool is_matched(U&&...);
147
148
155 template<class...U>
156 static matched_type<U...> declval_matched(U&&...) noexcept;
157};
158
159#endif /* UGMISC_DOCS */
160
161
163namespace member_ {
164
165
166
167
182template<class StaticCaller, class Functor>
183class fallback_caller {
184 Functor m_functor;
185
186 /*
187 * The use of Dummy* for the first override and Dummy for the second, makes
188 * the first more specific.
189 */
190 template<class Dummy, class...T>
191 constexpr auto forward(Dummy*, T&&...args) const
192 -> decltype(m_functor(std::forward<T>(args)...))
193 {
194 return m_functor(std::forward<T>(args)...);
195 }
196
197 template<class Dummy, class...T>
198 constexpr decltype(auto) forward(Dummy, T&&...args) const {
199 return m_functor();
200 }
201
202
203public:
204 constexpr fallback_caller(const Functor& f) : m_functor(f) {}
205 constexpr fallback_caller(Functor&& f) : m_functor(std::move(f)) {}
206
207 fallback_caller(fallback_caller&&) = default;
208 fallback_caller(const fallback_caller&) = delete;
209
210 template<class...T>
211 constexpr decltype(auto) call(T&&...args) const {
212 if constexpr ( StaticCaller::template is_matched_v<T...> ) {
213 return StaticCaller::call(std::forward<T>(args)...);
214 } else {
215 auto dummy = (int*)nullptr;
216 return forward(dummy, std::forward<T>(args)...);
217 }
218 }
219
220 template<class...T>
221 constexpr decltype(auto) operator()(T&&...args) const {
222 return call(std::forward<T>(args)...);
223 }
224};
225
226
227
228
229
230} /* member_ (::ugmisc::member_) */
231
232
234
235} /* ::ugmisc */
236
237
238
239
280#define UGMISC_NAMED_MEMBER_TYPE_TEST(TEMPLATENAME, ALIASNAME, HASMEMBERNAME, NAME) template<class TYPE, class DEFAULTTYPE = void> \
281 class TEMPLATENAME { \
282 template<class...> struct get_defined_type; \
283 \
284 template<class V, class T, class...Ts> struct get_defined_type<V, T, Ts...> { \
285 static constexpr bool has_member = get_defined_type<void, Ts...>::has_member; \
286 using type = typename get_defined_type<void, Ts...>::type; \
287 }; \
288 \
289 template<class T, class...Ts> struct get_defined_type<std::void_t<typename T::NAME>, T, Ts...> { \
290 static constexpr bool has_member = true; \
291 using type = typename T::NAME; \
292 }; \
293 \
294 template<class V> struct get_defined_type<V> { \
295 static constexpr bool has_member = false; \
296 using type = DEFAULTTYPE; \
297 }; \
298 \
299 using tlist = ::ugmisc::flatten_t<void, TYPE>; \
300 using dt = typename tlist::template apply<get_defined_type>; \
301 \
302 public: \
303 using type = typename dt::type; \
304 static constexpr bool has_member = dt::has_member; \
305 }; \
306 \
307 template<class T, class D=void> \
308 using ALIASNAME = typename TEMPLATENAME<T, D>::type; \
309 \
310 template<class...T> \
311 static constexpr bool HASMEMBERNAME = TEMPLATENAME<type_list<T...>>::has_member
312
313
314
315
343#define UGMISC_MEMBER_TYPE_TEST(NAME) \
344 UGMISC_NAMED_MEMBER_TYPE_TEST(member_type_test_##NAME, member_type_##NAME, has_member_type_##NAME, NAME)
345
346
347
348
387#define UGMISC_NAMED_MEMBER_STATIC_METHOD_CALL(TEMPLATENAME, NAME) \
388 template<class...> class TEMPLATENAME; \
389 \
390 template<class FirstTry, class...Tries> \
391 struct TEMPLATENAME<FirstTry, Tries...> { \
392 template<class...> friend class TEMPLATENAME; \
393 \
394 private: \
395 template<class...T> struct Call : public TEMPLATENAME<Tries...>::template Call<T...> { \
396 }; \
397 \
398 template<class...T> struct Call<std::void_t<decltype(FirstTry::NAME(std::declval<T>()...))>, T...> { \
399 static constexpr bool is_matched = true; \
400 \
401 using matched_type = FirstTry; \
402 \
403 static constexpr decltype(auto) call(T...args) { \
404 return FirstTry::NAME(std::forward<T>(args)...); \
405 } \
406 }; \
407 \
408 public: \
409 \
410 template<class...T> \
411 static constexpr decltype(auto) call(T&&...args) { \
412 return Call<void, T...>::call(std::forward<T>(args)...); \
413 } \
414 \
415 template<class...T> \
416 decltype(auto) constexpr operator() (T&&...args) const { \
417 return TEMPLATENAME::call(std::forward<T>(args)...); \
418 } \
419 \
420 template<class F> \
421 static constexpr \
422 member_::fallback_caller<TEMPLATENAME, std::remove_reference_t<F>> \
423 fallback(F&& functor) { \
424 return std::forward<F>(functor); \
425 } \
426 \
427 template<class...T> \
428 using matched_type = typename Call<void, T...>::matched_type; \
429 \
430 template<class...T> \
431 static constexpr bool is_matched(T&&...) { return Call<void, T...>::is_matched; } \
432 \
433 template<class...T> \
434 static constexpr bool is_matched_v = Call<void, T...>::is_matched; \
435 \
436 template<class...T> \
437 static matched_type<T...> declval_matched(T&&...) noexcept; \
438 }; \
439 \
440 /* An empty type list doesn't ever match the call. */ \
441 template<> class TEMPLATENAME<> { \
442 template<class...> friend class TEMPLATENAME; \
443 private: \
444 template<class...> struct Call { \
445 static constexpr bool is_matched = false; \
446 }; \
447 \
448 public: \
449 template<class...T> \
450 static constexpr bool is_matched(T&&...) { return false; } \
451 }
452
453
461#define UGMISC_MEMBER_STATIC_METHOD_CALL(NAME) \
462 UGMISC_NAMED_MEMBER_STATIC_METHOD_CALL(member_call_##NAME, NAME)
Definition member.hpp:70
static constexpr decltype(auto) call(U &&...args)
static constexpr auto fallback(F &&functor)
??? matched_type
Definition member.hpp:132
static matched_type< U... > declval_matched(U &&...) noexcept
static constexpr bool is_matched(U &&...)
static constexpr bool is_matched_v
Definition member.hpp:139
Lists of types which may be used in some of the ugmisc templates where a single type would usually be...