refactor: replace private access hacks with accessor pattern#564
Conversation
8e57e23 to
0d1a967
Compare
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这份 diff 包含了 CI/CD 工作流的构建配置、一个用于安全访问 Qt 私有成员的 C++ 模板黑魔法实现,以及相关代码的适配修改。整体来看,代码构思巧妙,但存在一些语法/兼容性隐患、CI 配置冗余以及潜在的安全风险。 以下是详细的审查意见和改进建议: 一、 语法与逻辑1.
2.
3.
二、 代码质量1. 替换
2. 命名规范与 Tag 结构体的声明位置
3. CI 工作流中的冗余依赖
三、 代码性能1. CI 中 Arch Linux 的包初始化策略
2. CI 中 Deepin 的源替换策略
四、 代码安全1. Deepin CI 中使用了
2. 版权年份的随意修改
3. CMake 中暴露内部源码路径
总结与修改示例针对最关键的语法和逻辑问题,建议对 1. 修改调用逻辑(确认 // ddcifileengine.cpp
// 如果 d_func 返回指针,则不需要解引用,直接用 ->* 或在宏外层用 ->
return D_PRIVATE_CALL(D_PRIVATE_CALL(realDciFile, QFile_d_func_tag{}), QFilePrivate_engine_tag{})->syncToDisk();2. 宏定义兼容性增强(可选,针对无参数方法): // 专门用于无参方法的宏,避免 __VA_ARGS__ 为空时的潜在问题
#define D_DECLARE_PRIVATE_CONST_METHOD_NO_ARGS(TagName, ClassName, MethodName, RetType) \
struct TagName { \
using MemberPtr = RetType (ClassName::*)() const; \
friend MemberPtr get(TagName) noexcept; \
}; \
template struct DtkCorePrivateAccessorImpl<TagName, &ClassName::MethodName>
// 调用处:
D_DECLARE_PRIVATE_CONST_METHOD_NO_ARGS(QFilePrivate_engine_tag, QFilePrivate, engine, QAbstractFileEngine *);3. 修复 CMake 的 PUBLIC/PRIVATE 属性: target_include_directories(${BIN_NAME} PRIVATE
../../src/
) |
|
[APPROVALNOTIFIER] This PR is NOT APPROVED This pull-request has been approved by: 18202781743, 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.