2
0

CConfigHandler.h 3.1 KB

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