存储设置的最佳做法 [英] Best practices for storing settings

查看:95
本文介绍了存储设置的最佳做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个相当大的C ++应用程序(在Windows上,没有计划其他平台),当前将所有设置(甚至某种地址)存储在Windows注册表中.

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.

有时候这很不方便,因为用户很难更改注册表中的条目.我想对设置进行版本控制,因此设置始终与当前代码匹配.目前,我们对reg文件进行了版本控制,但是您不确定是否已将所有reg文件添加到目标计算机上.使用C#,您可以在app.config中定义默认值,但不要覆盖现有设置.我不知道C ++是否存在这样的机制或库.

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. With C# you can define default values in app.config, but don't overwrite existing settings. I don't know, if such a mechanism or library exists for C++.

我希望具有以下功能":

I would like to have the following "features":

  • 可以对设置进行版本控制
  • 目标计算机上的简单更新(可由用户完成)
  • 确保在更新时仅添加新设置,并且不使用默认值覆盖现有设置
  • 用户设置的简单更改
  • 在Win XP和Win 7下具有相同的工作流程

据我所知,有3种可能性可以在Windows上存储设置:

As far as I see it, there are 3 possibilities to store settings on Windows:

  • 注册表
  • ini文件
  • XML文件

目前我们套件中只有一个应用程序使用Qt,但Boost可用.

Only one application of our suite uses Qt at the moment, but Boost would be available.

对于地址,我们会将其放入某种XML通讯录中,但是对于其他设置,我们不确定,最好的做法是什么.

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.

推荐答案

如评论所述,基于树的键/值结构是一种常见的解决方案,并且易于查找库.

As comments mention, tree-based key/value structures are a common solution and libraries are easy to find.

Boost的property_tree 是一个很好的选择,因为它经过了严格的测试,可以轻松导出为XML或JSON

Boost's property_tree is an excellent choice, as it is well-tested and can easily be exported as XML or JSON

关于您的要求:

  • 可以对设置进行版本控制
  • 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.

  • 确保在更新时仅添加新设置,并且不使用默认值覆盖现有设置
  • 用户设置的简单更改
  • 在Win XP和Win 7下具有相同的工作流程
  • 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

随着设置从一个版本更改为另一个版本,通常这些更改可分为三类.需要新属性,废弃旧设置,并且某些设置会更改其预期格式.例如. "32华氏度"变为"0摄氏度"

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. E.g. "32 Fahrenheit" becomes "0 Celsius"

应用程序初始化时:

  • 加载现有配置文件,而不考虑其版本.
  • 如果版本与应用程序当前版本不匹配:
    • 为新配置创建新的空白属性树
    • 对于树中的每个节点,都有一组预期的属性名称,以及一个函数指针或类似的指针(如果旧文件的树中不存在此设置)以获取此设置.如果设置更改了格式,请给它起一个新名称.
    • 搜索旧树中的每个设置,如果找到,则将其复制;如果未找到,则使用提供的函数的结果.
    • 保存新设置文件.
    • 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:

      • 返回恒定的默认值.
      • 在树中查询其他设置并进行转换(如果未找到旧设置,则使用默认值)
      • 询问用户

      这篇关于存储设置的最佳做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆