|
| 1 | +// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd. |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +// Private member accessor using the explicit template instantiation technique. |
| 8 | +// |
| 9 | +// C++ Standard [temp.explicit]/12 states: |
| 10 | +// "The usual access checking rules do not apply to names used to |
| 11 | +// specify explicit instantiation definitions." |
| 12 | +// |
| 13 | +// This allows passing pointers to private/protected data members and |
| 14 | +// member functions as template arguments in explicit instantiations, |
| 15 | +// bypassing normal access control — without modifying the class definition |
| 16 | +// and without the UB caused by "#define private public". |
| 17 | +// |
| 18 | +// NOTE: These helper structs must be in the SAME namespace as the Tag structs |
| 19 | +// (global namespace, since the macros expand at file scope). If they were in a |
| 20 | +// sub-namespace, the friend definition would create a different function than |
| 21 | +// the friend declaration in the Tag struct, causing undefined-reference errors. |
| 22 | + |
| 23 | +#if defined(__GNUC__) && !defined(__clang__) |
| 24 | +# pragma GCC diagnostic push |
| 25 | +# pragma GCC diagnostic ignored "-Wnon-template-friend" |
| 26 | +#endif |
| 27 | + |
| 28 | +template<typename Tag> |
| 29 | +struct DtkCorePrivateAccessor |
| 30 | +{ |
| 31 | + using MemberPtr = typename Tag::MemberPtr; |
| 32 | + friend MemberPtr get(Tag) noexcept; |
| 33 | +}; |
| 34 | + |
| 35 | +template<typename Tag, typename Tag::MemberPtr Ptr> |
| 36 | +struct DtkCorePrivateAccessorImpl : DtkCorePrivateAccessor<Tag> |
| 37 | +{ |
| 38 | + friend typename Tag::MemberPtr get(Tag) noexcept { return Ptr; } |
| 39 | +}; |
| 40 | + |
| 41 | +#if defined(__GNUC__) && !defined(__clang__) |
| 42 | +# pragma GCC diagnostic pop |
| 43 | +#endif |
| 44 | + |
| 45 | +#define D_DECLARE_PRIVATE_MEMBER(TagName, ClassName, Member, MemberType) \ |
| 46 | + struct TagName { \ |
| 47 | + using MemberPtr = MemberType ClassName::*; \ |
| 48 | + friend MemberPtr get(TagName) noexcept; \ |
| 49 | + }; \ |
| 50 | + template struct DtkCorePrivateAccessorImpl<TagName, &ClassName::Member> |
| 51 | + |
| 52 | +#define D_DECLARE_PRIVATE_METHOD(TagName, ClassName, MethodName, RetType, ...) \ |
| 53 | + struct TagName { \ |
| 54 | + using MemberPtr = RetType (ClassName::*)(__VA_ARGS__); \ |
| 55 | + friend MemberPtr get(TagName) noexcept; \ |
| 56 | + }; \ |
| 57 | + template struct DtkCorePrivateAccessorImpl<TagName, &ClassName::MethodName> |
| 58 | + |
| 59 | +#define D_DECLARE_PRIVATE_CONST_METHOD(TagName, ClassName, MethodName, RetType, ...) \ |
| 60 | + struct TagName { \ |
| 61 | + using MemberPtr = RetType (ClassName::*)(__VA_ARGS__) const; \ |
| 62 | + friend MemberPtr get(TagName) noexcept; \ |
| 63 | + }; \ |
| 64 | + template struct DtkCorePrivateAccessorImpl<TagName, &ClassName::MethodName> |
| 65 | + |
| 66 | +// Trampoline: ensures get(tag) is called from a context with no class-scope |
| 67 | +// get() member that might suppress ADL (C++ [basic.lookup.argdep] para 3). |
| 68 | +namespace dtk_private_detail { |
| 69 | + template<typename Tag> |
| 70 | + inline constexpr typename Tag::MemberPtr access(Tag t) noexcept { return get(t); } |
| 71 | +} |
| 72 | + |
| 73 | +#define D_PRIVATE_MEMBER(obj, tag) ((obj).*dtk_private_detail::access(tag)) |
| 74 | +#define D_PRIVATE_CALL(obj, tag, ...) ((obj).*dtk_private_detail::access(tag))(__VA_ARGS__) |
| 75 | + |
0 commit comments