简体   繁体   English

存储设置的最佳做法

[英]Best practices for storing settings

I have a fairly large C++ application (on Windows, no other platforms planned), which currently stores all settings (even some kind of addresses) in the Windows registry. 我有一个相当大的C ++应用程序(在Windows上,没有计划其他平台),它目前存储Windows注册表中的所有设置(甚至某种地址)。

Sometimes this is inconvenient, because the users have difficulties changing entries in the registry. 有时这很不方便,因为用户在更改注册表中的条目时遇到困难。 I would like to have settings versioned, so settings always match the current code. 我想设置版本,因此设置始终与当前代码匹配。 At the moment we version reg-files, but you are never sure, if all reg-files have been added on the target machines. 目前我们对reg-files进行了版本控制,但是如果所有的reg文件都已添加到目标计算机上,那么您永远不会确定。 With C# you can define default values in app.config, but don't overwrite existing settings. 使用C#,您可以在app.config中定义默认值,但不要覆盖现有设置。 I don't know, if such a mechanism or library exists for C++. 我不知道,如果C ++存在这样的机制或库。

I would like to have the following "features": 我想拥有以下“功能”:

  • Settings can be versioned 设置可以进行版本控制
  • Simple update on target machines (can be done by user) 目标机器上的简单更新(可由用户完成)
  • Ensure that on update only new settings are added and no existing settings are overwritten with default values 确保在更新时仅添加新设置,并且不会使用默认值覆盖现有设置
  • Simple change of settings for user 简单更改用户的设置
  • Same workflow under Win XP and Win 7 Win XP和Win 7下的相同工作流程

As far as I see it, there are 3 possibilities to store settings on Windows: 据我所知,在Windows上存储设置有3种可能性:

  • Registry 注册处
  • Ini file Ini文件
  • XML file XML文件

Only one application of our suite uses Qt at the moment, but Boost would be available. 我们套件中只有一个应用程序目前使用Qt,但Boost可用。

For addresses, we will put them in some kind of XML address book, but for the other settings we are not sure, what's the best practise. 对于地址,我们会将它们放在某种XML地址簿中,但对于其他设置,我们不确定,最佳做法是什么。

As comments mention, tree-based key/value structures are a common solution and libraries are easy to find. 正如评论所提到的,基于树的键/值结构是一种常见的解决方案,并且库很容易找到。

Boost's property_tree is an excellent choice, as it is well-tested and can easily be exported as XML or JSON Boost的property_tree是一个很好的选择,因为它经过了充分测试,可以轻松导出为XML或JSON

Regarding your requirements: 关于您的要求:

  • Settings can be versioned 设置可以进行版本控制

Yes! 是! Make "version" a top-level key. 使“版本”成为顶级密钥。 Make it easily comparable with other versions. 使其易于与其他版本相媲美。

You can also categorize your settings into various tree nodes and give each node a version. 您还可以将设置分类为各种树节点,并为每个节点提供版本。

  • Simple update on target machines (can be done by user) 目标机器上的简单更新(可由用户完成)

Have your application do that when it runs. 让您的应用程序在运行时执行此操作。 See below. 见下文。

  • Ensure that on update only new settings are added and no existing settings are overwritten with default values 确保在更新时仅添加新设置,并且不会使用默认值覆盖现有设置
  • Simple change of settings for user 简单更改用户的设置
  • Same workflow under Win XP and Win 7 Win XP和Win 7下的相同工作流程

As settings change from one version to another, usually these changes fall into three categories. 随着设置从一个版本更改为另一个版本,通常这些更改分为三类。 New properties are needed, old settings are abandoned, and some settings change their expected format. 需要新属性,放弃旧设置,并且某些设置会更改其预期格式。 Eg "32 Fahrenheit" becomes "0 Celsius" 例如“32华氏度”变为“0摄氏度”

When your application initializes: 当您的应用程序初始化时:

  • Load the existing configuration file, regardless of its version. 加载现有配置文件,无论其版本如何。
  • If the version does not match what's current for the application: 如果版本与应用程序的当前版本不匹配:
    • Create a new blank property tree for the new configuration 为新配置创建新的空白属性树
    • For each node in the tree, have a set of expected property names, and a function pointer or similar to get this setting if it's absent in the old file's tree. 对于树中的每个节点,如果旧文件的树中不存在,则具有一组预期的属性名称,以及一个函数指针或类似设置以获取此设置。 If a setting changes its format, give it a new name. 如果设置更改其格式,请为其指定新名称。
    • Search for each setting in the old tree, copying it if it's found, and using the result of the supplied function if it's not. 搜索旧树中的每个设置,如果找到则复制它,如果不是,则使用提供的功能的结果。
    • Save your new settings file. 保存新的设置文件。

Your "missing setting" functions can: 您的“缺失设置”功能可以:

  • Return a constant default value. 返回一个常量默认值。
  • Query the tree for a different setting and convert it (with a default value if the old setting isn't found either) 查询树以获取不同的设置并进行转换(如果未找到旧设置,则使用默认值)
  • Ask the user 询问用户

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM