CConfigHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * CConfigHandler.h, part of VCMI engine
  3. *
  4. * Authors: listed in file AUTHORS in main folder
  5. *
  6. * License: GNU General Public License v2.0 or later
  7. * Full text of license available in license.txt file, in main folder
  8. *
  9. */
  10. #pragma once
  11. #include "json/JsonNode.h"
  12. VCMI_LIB_NAMESPACE_BEGIN
  13. class Settings;
  14. class SettingsListener;
  15. /// Main storage of game settings
  16. class DLL_LINKAGE SettingsStorage
  17. {
  18. //Helper struct to access specific node either via chain of operator[] or with one operator() (vector)
  19. template<typename Accessor>
  20. struct DLL_LINKAGE NodeAccessor
  21. {
  22. SettingsStorage & parent;
  23. std::vector<std::string> path;
  24. NodeAccessor(SettingsStorage & _parent, std::vector<std::string> _path);
  25. NodeAccessor<Accessor> operator[](const std::string & nextNode) const;
  26. NodeAccessor<Accessor> operator () (std::vector<std::string> _path) const;
  27. operator Accessor() const;
  28. };
  29. std::set<SettingsListener*> listeners;
  30. JsonNode config;
  31. std::string dataFilename;
  32. std::string schema;
  33. JsonNode & getNode(const std::vector<std::string> & path);
  34. // Calls all required listeners
  35. void invalidateNode(const std::vector<std::string> &changedPath);
  36. Settings get(const std::vector<std::string> & path);
  37. public:
  38. // Initialize config structure
  39. SettingsStorage();
  40. ~SettingsStorage();
  41. void init(const std::string & dataFilename, const std::string & schema);
  42. // Get write access to config node at path
  43. const NodeAccessor<Settings> write;
  44. // Get access to listener at path
  45. const NodeAccessor<SettingsListener> listen;
  46. //Read access, see JsonNode::operator[]
  47. const JsonNode & operator[](const std::string & value) const;
  48. const JsonNode & toJsonNode() const;
  49. friend class SettingsListener;
  50. friend class Settings;
  51. };
  52. /// Class for listening changes in specific part of configuration (e.g. change of music volume)
  53. class DLL_LINKAGE SettingsListener
  54. {
  55. SettingsStorage &parent;
  56. // Path to this node
  57. std::vector<std::string> path;
  58. // Callback
  59. std::function<void(const JsonNode&)> callback;
  60. // hack for crash due to static destruction order
  61. bool wasTerminated = false;
  62. SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path);
  63. // Executes callback if changedpath begins with path
  64. void nodeInvalidated(const std::vector<std::string> & changedPath);
  65. void terminate();
  66. public:
  67. SettingsListener(const SettingsListener &sl);
  68. ~SettingsListener();
  69. // assign callback function
  70. void operator()(std::function<void(const JsonNode&)> _callback);
  71. friend class SettingsStorage;
  72. };
  73. /// System options, provides write access to config tree with auto-saving on change
  74. class DLL_LINKAGE Settings
  75. {
  76. SettingsStorage &parent;
  77. //path to this node
  78. std::vector<std::string> path;
  79. JsonNode &node;
  80. JsonNode copy;
  81. //Get access to node pointed by path
  82. Settings(SettingsStorage &_parent, const std::vector<std::string> &_path);
  83. public:
  84. //Saves config if it was modified
  85. ~Settings();
  86. //Returns node selected during construction
  87. JsonNode* operator ->();
  88. const JsonNode* operator ->() const;
  89. //Helper, replaces JsonNode::operator[]
  90. JsonNode & operator[](const std::string & value);
  91. const JsonNode & operator[](const std::string & value) const;
  92. friend class SettingsStorage;
  93. };
  94. extern DLL_LINKAGE SettingsStorage settings;
  95. extern DLL_LINKAGE SettingsStorage persistentStorage;
  96. VCMI_LIB_NAMESPACE_END