settings.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes_bloated.h"
  18. #include "util/string.h"
  19. #include <string>
  20. #include <list>
  21. #include <set>
  22. #include <mutex>
  23. class Settings;
  24. struct NoiseParams;
  25. // Global objects
  26. extern Settings *g_settings;
  27. extern std::string g_settings_path;
  28. // Type for a settings changed callback function
  29. typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
  30. typedef std::vector<
  31. std::pair<
  32. SettingsChangedCallback,
  33. void *
  34. >
  35. > SettingsCallbackList;
  36. typedef std::unordered_map<std::string, SettingsCallbackList> SettingsCallbackMap;
  37. enum ValueType {
  38. VALUETYPE_STRING,
  39. VALUETYPE_FLAG // Doesn't take any arguments
  40. };
  41. enum SettingsParseEvent {
  42. SPE_NONE,
  43. SPE_INVALID,
  44. SPE_COMMENT,
  45. SPE_KVPAIR,
  46. SPE_END,
  47. SPE_GROUP,
  48. SPE_MULTILINE,
  49. };
  50. struct ValueSpec {
  51. ValueSpec(ValueType a_type, const char *a_help=NULL)
  52. {
  53. type = a_type;
  54. help = a_help;
  55. }
  56. ValueType type;
  57. const char *help;
  58. };
  59. struct SettingsEntry {
  60. SettingsEntry() = default;
  61. SettingsEntry(const std::string &value_) :
  62. value(value_)
  63. {}
  64. SettingsEntry(Settings *group_) :
  65. group(group_),
  66. is_group(true)
  67. {}
  68. std::string value = "";
  69. Settings *group = nullptr;
  70. bool is_group = false;
  71. };
  72. typedef std::unordered_map<std::string, SettingsEntry> SettingEntries;
  73. class Settings {
  74. public:
  75. Settings() = default;
  76. ~Settings();
  77. Settings & operator += (const Settings &other);
  78. Settings & operator = (const Settings &other);
  79. /***********************
  80. * Reading and writing *
  81. ***********************/
  82. // Read configuration file. Returns success.
  83. bool readConfigFile(const char *filename);
  84. //Updates configuration file. Returns success.
  85. bool updateConfigFile(const char *filename);
  86. // NOTE: Types of allowed_options are ignored. Returns success.
  87. bool parseCommandLine(int argc, char *argv[],
  88. std::map<std::string, ValueSpec> &allowed_options);
  89. bool parseConfigLines(std::istream &is, const std::string &end = "");
  90. void writeLines(std::ostream &os, u32 tab_depth=0) const;
  91. SettingsParseEvent parseConfigObject(const std::string &line,
  92. const std::string &end, std::string &name, std::string &value);
  93. bool updateConfigObject(std::istream &is, std::ostream &os,
  94. const std::string &end, u32 tab_depth=0);
  95. static bool checkNameValid(const std::string &name);
  96. static bool checkValueValid(const std::string &value);
  97. static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
  98. static void printEntry(std::ostream &os, const std::string &name,
  99. const SettingsEntry &entry, u32 tab_depth=0);
  100. /***********
  101. * Getters *
  102. ***********/
  103. const SettingsEntry &getEntry(const std::string &name) const;
  104. const SettingsEntry &getEntryDefault(const std::string &name) const;
  105. Settings *getGroup(const std::string &name) const;
  106. const std::string &get(const std::string &name) const;
  107. const std::string &getDefault(const std::string &name) const;
  108. bool getBool(const std::string &name) const;
  109. u16 getU16(const std::string &name) const;
  110. s16 getS16(const std::string &name) const;
  111. u32 getU32(const std::string &name) const;
  112. s32 getS32(const std::string &name) const;
  113. u64 getU64(const std::string &name) const;
  114. float getFloat(const std::string &name) const;
  115. v2f getV2F(const std::string &name) const;
  116. v3f getV3F(const std::string &name) const;
  117. u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
  118. u32 *flagmask) const;
  119. // N.B. if getStruct() is used to read a non-POD aggregate type,
  120. // the behavior is undefined.
  121. bool getStruct(const std::string &name, const std::string &format,
  122. void *out, size_t olen) const;
  123. bool getNoiseParams(const std::string &name, NoiseParams &np) const;
  124. bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
  125. bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
  126. // return all keys used
  127. std::vector<std::string> getNames() const;
  128. bool exists(const std::string &name) const;
  129. /***************************************
  130. * Getters that don't throw exceptions *
  131. ***************************************/
  132. bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
  133. bool getEntryDefaultNoEx(const std::string &name, SettingsEntry &val) const;
  134. bool getGroupNoEx(const std::string &name, Settings *&val) const;
  135. bool getNoEx(const std::string &name, std::string &val) const;
  136. bool getDefaultNoEx(const std::string &name, std::string &val) const;
  137. bool getFlag(const std::string &name) const;
  138. bool getU16NoEx(const std::string &name, u16 &val) const;
  139. bool getS16NoEx(const std::string &name, s16 &val) const;
  140. bool getS32NoEx(const std::string &name, s32 &val) const;
  141. bool getU64NoEx(const std::string &name, u64 &val) const;
  142. bool getFloatNoEx(const std::string &name, float &val) const;
  143. bool getV2FNoEx(const std::string &name, v2f &val) const;
  144. bool getV3FNoEx(const std::string &name, v3f &val) const;
  145. // Like other getters, but handling each flag individualy:
  146. // 1) Read default flags (or 0)
  147. // 2) Override using user-defined flags
  148. bool getFlagStrNoEx(const std::string &name, u32 &val,
  149. const FlagDesc *flagdesc) const;
  150. /***********
  151. * Setters *
  152. ***********/
  153. // N.B. Groups not allocated with new must be set to NULL in the settings
  154. // tree before object destruction.
  155. bool setEntry(const std::string &name, const void *entry,
  156. bool set_group, bool set_default);
  157. bool set(const std::string &name, const std::string &value);
  158. bool setDefault(const std::string &name, const std::string &value);
  159. bool setGroup(const std::string &name, Settings *group);
  160. bool setGroupDefault(const std::string &name, Settings *group);
  161. bool setBool(const std::string &name, bool value);
  162. bool setS16(const std::string &name, s16 value);
  163. bool setU16(const std::string &name, u16 value);
  164. bool setS32(const std::string &name, s32 value);
  165. bool setU64(const std::string &name, u64 value);
  166. bool setFloat(const std::string &name, float value);
  167. bool setV2F(const std::string &name, v2f value);
  168. bool setV3F(const std::string &name, v3f value);
  169. bool setFlagStr(const std::string &name, u32 flags,
  170. const FlagDesc *flagdesc = nullptr, u32 flagmask = U32_MAX);
  171. bool setNoiseParams(const std::string &name, const NoiseParams &np,
  172. bool set_default=false);
  173. // N.B. if setStruct() is used to write a non-POD aggregate type,
  174. // the behavior is undefined.
  175. bool setStruct(const std::string &name, const std::string &format, void *value);
  176. // remove a setting
  177. bool remove(const std::string &name);
  178. void clear();
  179. void clearDefaults();
  180. void updateValue(const Settings &other, const std::string &name);
  181. void update(const Settings &other);
  182. /**************
  183. * Miscellany *
  184. **************/
  185. void setDefault(const std::string &name, const FlagDesc *flagdesc, u32 flags);
  186. // Takes the provided setting values and uses them as new defaults
  187. void overrideDefaults(Settings *other);
  188. const FlagDesc *getFlagDescFallback(const std::string &name) const;
  189. void registerChangedCallback(const std::string &name,
  190. SettingsChangedCallback cbf, void *userdata = NULL);
  191. void deregisterChangedCallback(const std::string &name,
  192. SettingsChangedCallback cbf, void *userdata = NULL);
  193. private:
  194. void updateNoLock(const Settings &other);
  195. void clearNoLock();
  196. void clearDefaultsNoLock();
  197. void doCallbacks(const std::string &name) const;
  198. SettingEntries m_settings;
  199. SettingEntries m_defaults;
  200. std::unordered_map<std::string, const FlagDesc *> m_flags;
  201. SettingsCallbackMap m_callbacks;
  202. mutable std::mutex m_callback_mutex;
  203. // All methods that access m_settings/m_defaults directly should lock this.
  204. mutable std::mutex m_mutex;
  205. };