From 66343dcd760ed19ab36a31338ebede7d455c871c Mon Sep 17 00:00:00 2001 From: yeshanshan Date: Thu, 22 Jan 2026 11:02:19 +0800 Subject: [PATCH] feat: add version macros and property definitions to dconfig2cpp MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 1. Added version macros (VERSION_MAJOR/VERSION_MINOR) to generated header files for version tracking 2. Added property definition macros for all configuration properties to facilitate caller compatibility 3. Added version compatibility check in dconfig2cpp tool to warn when JSON file version exceeds supported version 4. Updated header comment to include dconfig2cpp tool version information 5. Added version struct and supported version constant to main.cpp 6. Modified code generation to output property macros before class definition Log: Added version and property macros to dconfig2cpp generated files for better compatibility Influence: 1. Test dconfig2cpp tool with JSON files of different versions (older, same, newer) 2. Verify generated header files contain correct version macros 3. Check that property definition macros are generated for all configuration properties 4. Test compilation of projects using the generated header files 5. Verify backward compatibility with existing code using the generated classes 6. Test version warning mechanism when using newer JSON files feat: dconfig2cpp添加版本宏和属性定义宏 1. 在生成的头文件中添加版本宏(VERSION_MAJOR/VERSION_MINOR)用于版本跟踪 2. 为所有配置属性添加属性定义宏,方便调用方兼容性处理 3. 在dconfig2cpp工具中添加版本兼容性检查,当JSON文件版本超过支持版本时发 出警告 4. 更新头文件注释以包含dconfig2cpp工具版本信息 5. 在main.cpp中添加版本结构和支持的版本常量 6. 修改代码生成逻辑,在类定义之前输出属性宏 Log: 为dconfig2cpp生成的文件添加版本和属性宏,提高兼容性 Influence: 1. 测试dconfig2cpp工具处理不同版本(旧版、相同、新版)的JSON文件 2. 验证生成的头文件包含正确的版本宏 3. 检查是否为所有配置属性生成了属性定义宏 4. 测试使用生成头文件的项目的编译情况 5. 验证与现有使用生成类的代码的向后兼容性 6. 测试使用新版JSON文件时的版本警告机制 --- src/log/dconfig_org_deepin_dtk_preference.hpp | 20 +++++++++- tools/dconfig2cpp/main.cpp | 39 ++++++++++++++++++- 2 files changed, 56 insertions(+), 3 deletions(-) diff --git a/src/log/dconfig_org_deepin_dtk_preference.hpp b/src/log/dconfig_org_deepin_dtk_preference.hpp index 5a318c8a..abd3ae85 100644 --- a/src/log/dconfig_org_deepin_dtk_preference.hpp +++ b/src/log/dconfig_org_deepin_dtk_preference.hpp @@ -1,8 +1,9 @@ /** * This file is generated by dconfig2cpp. * Command line arguments: ./build/tools/dconfig2cpp/dconfig2cpp /usr/share/dsg/configs/org.deepin.dtk.preference.json -o ./src/log/dconfig_org_deepin_dtk_preference.hpp - * Generation time: 2026-01-21T14:01:24 + * Generation time: 2026-01-22T14:49:01 * JSON file version: 1.0 + * Tool version: 1.1 * * WARNING: DO NOT MODIFY THIS FILE MANUALLY. * If you need to change the content, please modify the dconfig2cpp tool. @@ -11,6 +12,21 @@ #ifndef DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_H #define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_H +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_VERSION_MAJOR 1 +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_VERSION_MINOR 1 +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_autoDisplayFeature +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_colorMode +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_defaultColorMode +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_enableDtkAnimations +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_featureUpdated +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_keyboardsearchDisabled +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_rules +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_scrollBarPolicy +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_sizeMode +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_themeType +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_titlebarHeight +#define DCONFIG_ORG_DEEPIN_DTK_PREFERENCE_DCONFIG_FILE_underlineShortcut + #include #include #include @@ -119,6 +135,7 @@ class dconfig_org_deepin_dtk_preference : public QObject { // Initialize through Data class safeData->initializeInConfigThread(config); + // Queue state transition to main thread, to ensure config values are fully initialized. QMetaObject::invokeMethod(safeData, [safeData, config]() { // Try to transition from Initializing to Succeeded if (safeData->m_status.testAndSetOrdered(static_cast(Data::Status::Initializing), @@ -135,7 +152,6 @@ class dconfig_org_deepin_dtk_preference : public QObject { safeData->deleteLater(); } }, Qt::QueuedConnection); - }); } static dconfig_org_deepin_dtk_preference* create(const QString &appId = {}, const QString &subpath = {}, QObject *parent = nullptr, QThread *thread = DTK_CORE_NAMESPACE::DConfig::globalThread()) diff --git a/tools/dconfig2cpp/main.cpp b/tools/dconfig2cpp/main.cpp index 4c1804b1..3ca2ffc2 100644 --- a/tools/dconfig2cpp/main.cpp +++ b/tools/dconfig2cpp/main.cpp @@ -12,6 +12,12 @@ #include #include +struct Version { + quint16 major; + quint16 minor; +}; +static constexpr Version ToolVersion{1, 1}; + static QString toUnicodeEscape(const QString& input) { QString result; for (QChar ch : input) { @@ -82,9 +88,11 @@ static QString jsonValueToCppCode(const QJsonValue &value){ int main(int argc, char *argv[]) { QCoreApplication app(argc, argv); + app.setApplicationVersion(QString("%1.%2").arg(ToolVersion.major).arg(ToolVersion.minor)); QCommandLineParser parser; parser.setApplicationDescription(QLatin1String("DConfig to C++ class generator")); parser.addHelpOption(); + parser.addVersionOption(); // Define command line options QCommandLineOption classNameOption(QStringList() << QLatin1String("c") << QLatin1String("class-name"), @@ -151,6 +159,18 @@ int main(int argc, char *argv[]) { // Extract version and add it as a comment in the generated code QString version = root[QLatin1String("version")].toString(); + + // Parse version to get major + QString fileMajorVersion; + const auto versionParts = version.split('.'); + if (versionParts.size() == 2) { + fileMajorVersion = versionParts[0]; + } + + if (fileMajorVersion != "1") { + qWarning() << QLatin1String("Warning: The JSON file version isn't supported."); + return -1; + } // Generate header and source file comments QString commandLineArgs = QCoreApplication::arguments().join(QLatin1String(" ")); @@ -163,20 +183,37 @@ int main(int argc, char *argv[]) { " * Command line arguments: %1\n" " * Generation time: %2\n" " * JSON file version: %3\n" + " * Tool version: %4\n" " *\n" " * WARNING: DO NOT MODIFY THIS FILE MANUALLY.\n" " * If you need to change the content, please modify the dconfig2cpp tool.\n" " */\n\n" - ).arg(commandLineArgs, generationTime, version); + ).arg(commandLineArgs, generationTime, version, QString("%1.%2").arg(ToolVersion.major).arg(ToolVersion.minor)); headerStream << headerComment; } QJsonObject contents = root[QLatin1String("contents")].toObject(); + // First pass: collect all property names for macro definitions + QStringList allPropertyNames; + for (auto it = contents.begin(); it != contents.end(); ++it) { + allPropertyNames << it.key(); + } + // Write header file content headerStream << "#ifndef " << className.toUpper() << "_H\n"; headerStream << "#define " << className.toUpper() << "_H\n\n"; + + // Add version macros + headerStream << "#define " << className.toUpper() << "_DCONFIG_FILE_VERSION_MAJOR " << ToolVersion.major << "\n"; + headerStream << "#define " << className.toUpper() << "_DCONFIG_FILE_VERSION_MINOR " << ToolVersion.minor << "\n"; + + // Add property macros + for (const QString &propName : allPropertyNames) { + headerStream << "#define " << className.toUpper() << "_DCONFIG_FILE_" << propName << "\n"; + } + headerStream << "\n"; headerStream << "#include \n"; headerStream << "#include \n"; headerStream << "#include \n";