refactor: replace private access hacks with accessor pattern#564
Conversation
0d1a967 to
7144e90
Compare
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: zccrs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
1 similar comment
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: zccrs The full list of commands accepted by this bot can be found here. DetailsNeeds approval from an approver in each of these files:Approvers can indicate their approval by writing |
Remove all '#define private/protected public' hacks and replace them with a proper C++ template-based private accessor pattern using explicit template instantiation (friend injection trick), mirroring the approach used in linuxdeepin/treeland#875. Add src/util/dprivateaccessor_p.h with Accessor/AccessorImpl templates and D_DECLARE_PRIVATE_MEMBER, D_DECLARE_PRIVATE_METHOD, D_DECLARE_PRIVATE_CONST_METHOD, D_PRIVATE_MEMBER, D_PRIVATE_CALL macros. All helpers are in global namespace so ADL correctly finds the friend-injected get() function.
deepin pr auto review你好!我是CodeGeeX。我已仔细审查了你提供的Git Diff,该变更主要包含三个部分:新增Arch Linux和Deepin的CI构建工作流、引入基于显式模板实例化的私有成员访问机制替代危险的宏、以及相关的CMake和版权声明调整。 以下是我对语法逻辑、代码质量、代码性能和代码安全方面的详细审查意见和改进建议: 一、 语法与逻辑
二、 代码质量
三、 代码性能
四、 代码安全
综合修改建议代码示例针对 1. 完善 // ... 前面保持不变 ...
// 针对对象实例的访问 (.*)
#define D_PRIVATE_MEMBER(obj, tag) ((obj).*dtk_private_detail::access(tag))
#define D_PRIVATE_CALL(obj, tag, ...) ((obj).*dtk_private_detail::access(tag))(__VA_ARGS__)
// 针对指针实例的访问 (->*) - 新增
#define D_PRIVATE_PTR_MEMBER(obj_ptr, tag) ((obj_ptr)->*dtk_private_detail::access(tag))
#define D_PRIVATE_PTR_CALL(obj_ptr, tag, ...) ((obj_ptr)->*dtk_private_detail::access(tag))(__VA_ARGS__)2. 修正 // 假设 d_func() 返回的是 QFilePrivate* 指针
bool DDciFileEngine::syncToDisk()
{
if (!flush())
return false;
// 使用 D_PRIVATE_PTR_CALL 来通过指针调用私有方法
return D_PRIVATE_PTR_CALL(
D_PRIVATE_CALL(realDciFile, QFile_d_func_tag{}),
QFilePrivate_engine_tag{}
)->syncToDisk();
}3. 修正版权声明: // SPDX-FileCopyrightText: 2021 - 2023 UnionTech Software Technology Co., Ltd.整体来看,这是一次非常棒的重构,特别是使用模板技术替换宏的黑魔法,极大提升了代码的健壮性。只需在CI流程和宏的边界条件上稍加打磨即可。 |
Remove all '#define private/protected public' hacks and replace them with a proper C++ template-based private accessor pattern using explicit template instantiation (friend injection trick), mirroring the approach used in linuxdeepin/treeland#875.
Add src/util/dprivateaccessor_p.h with Accessor/AccessorImpl templates and D_DECLARE_PRIVATE_MEMBER, D_DECLARE_PRIVATE_METHOD, D_DECLARE_PRIVATE_CONST_METHOD, D_PRIVATE_MEMBER, D_PRIVATE_CALL macros. All helpers are in global namespace so ADL correctly finds the friend-injected get() function.