CConfigHandler.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 "../lib/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. void init(const std::string & dataFilename, const std::string & schema);
  41. // Get write access to config node at path
  42. const NodeAccessor<Settings> write;
  43. // Get access to listener at path
  44. const NodeAccessor<SettingsListener> listen;
  45. //Read access, see JsonNode::operator[]
  46. const JsonNode & operator[](const std::string & value) const;
  47. const JsonNode & toJsonNode() const;
  48. friend class SettingsListener;
  49. friend class Settings;
  50. };
  51. /// Class for listening changes in specific part of configuration (e.g. change of music volume)
  52. class DLL_LINKAGE SettingsListener
  53. {
  54. SettingsStorage &parent;
  55. // Path to this node
  56. std::vector<std::string> path;
  57. // Callback
  58. std::function<void(const JsonNode&)> callback;
  59. SettingsListener(SettingsStorage & _parent, std::vector<std::string> _path);
  60. // Executes callback if changedpath begins with path
  61. void nodeInvalidated(const std::vector<std::string> & changedPath);
  62. public:
  63. SettingsListener(const SettingsListener &sl);
  64. ~SettingsListener();
  65. // assign callback function
  66. void operator()(std::function<void(const JsonNode&)> _callback);
  67. friend class SettingsStorage;
  68. };
  69. /// System options, provides write access to config tree with auto-saving on change
  70. class DLL_LINKAGE Settings
  71. {
  72. SettingsStorage &parent;
  73. //path to this node
  74. std::vector<std::string> path;
  75. JsonNode &node;
  76. JsonNode copy;
  77. //Get access to node pointed by path
  78. Settings(SettingsStorage &_parent, const std::vector<std::string> &_path);
  79. public:
  80. //Saves config if it was modified
  81. ~Settings();
  82. //Returns node selected during construction
  83. JsonNode* operator ->();
  84. const JsonNode* operator ->() const;
  85. //Helper, replaces JsonNode::operator[]
  86. JsonNode & operator[](const std::string & value);
  87. const JsonNode & operator[](const std::string & value) const;
  88. friend class SettingsStorage;
  89. };
  90. extern DLL_LINKAGE SettingsStorage settings;
  91. extern DLL_LINKAGE SettingsStorage persistentStorage;
  92. VCMI_LIB_NAMESPACE_END