Best practices for storing settings

假如想象 提交于 2019-12-03 12:33:07

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

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

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"

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
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!