settings.h 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. #ifndef SETTINGS_HEADER
  17. #define SETTINGS_HEADER
  18. #include "irrlichttypes_bloated.h"
  19. #include "util/string.h"
  20. #include "threading/mutex.h"
  21. #include <string>
  22. #include "util/cpp11_container.h"
  23. #include <list>
  24. #include <set>
  25. class Settings;
  26. struct NoiseParams;
  27. // Global objects
  28. extern Settings *g_settings;
  29. extern std::string g_settings_path;
  30. // Type for a settings changed callback function
  31. typedef void (*SettingsChangedCallback)(const std::string &name, void *data);
  32. typedef std::vector<
  33. std::pair<
  34. SettingsChangedCallback,
  35. void *
  36. >
  37. > SettingsCallbackList;
  38. typedef UNORDERED_MAP<std::string, SettingsCallbackList> SettingsCallbackMap;
  39. enum ValueType {
  40. VALUETYPE_STRING,
  41. VALUETYPE_FLAG // Doesn't take any arguments
  42. };
  43. enum SettingsParseEvent {
  44. SPE_NONE,
  45. SPE_INVALID,
  46. SPE_COMMENT,
  47. SPE_KVPAIR,
  48. SPE_END,
  49. SPE_GROUP,
  50. SPE_MULTILINE,
  51. };
  52. struct ValueSpec {
  53. ValueSpec(ValueType a_type, const char *a_help=NULL)
  54. {
  55. type = a_type;
  56. help = a_help;
  57. }
  58. ValueType type;
  59. const char *help;
  60. };
  61. struct SettingsEntry {
  62. SettingsEntry() :
  63. group(NULL),
  64. is_group(false)
  65. {}
  66. SettingsEntry(const std::string &value_) :
  67. value(value_),
  68. group(NULL),
  69. is_group(false)
  70. {}
  71. SettingsEntry(Settings *group_) :
  72. group(group_),
  73. is_group(true)
  74. {}
  75. std::string value;
  76. Settings *group;
  77. bool is_group;
  78. };
  79. typedef UNORDERED_MAP<std::string, SettingsEntry> SettingEntries;
  80. class Settings {
  81. public:
  82. Settings() {}
  83. ~Settings();
  84. Settings & operator += (const Settings &other);
  85. Settings & operator = (const Settings &other);
  86. /***********************
  87. * Reading and writing *
  88. ***********************/
  89. // Read configuration file. Returns success.
  90. bool readConfigFile(const char *filename);
  91. //Updates configuration file. Returns success.
  92. bool updateConfigFile(const char *filename);
  93. // NOTE: Types of allowed_options are ignored. Returns success.
  94. bool parseCommandLine(int argc, char *argv[],
  95. std::map<std::string, ValueSpec> &allowed_options);
  96. bool parseConfigLines(std::istream &is, const std::string &end = "");
  97. void writeLines(std::ostream &os, u32 tab_depth=0) const;
  98. SettingsParseEvent parseConfigObject(const std::string &line,
  99. const std::string &end, std::string &name, std::string &value);
  100. bool updateConfigObject(std::istream &is, std::ostream &os,
  101. const std::string &end, u32 tab_depth=0);
  102. static bool checkNameValid(const std::string &name);
  103. static bool checkValueValid(const std::string &value);
  104. static std::string getMultiline(std::istream &is, size_t *num_lines=NULL);
  105. static void printEntry(std::ostream &os, const std::string &name,
  106. const SettingsEntry &entry, u32 tab_depth=0);
  107. /***********
  108. * Getters *
  109. ***********/
  110. const SettingsEntry &getEntry(const std::string &name) const;
  111. Settings *getGroup(const std::string &name) const;
  112. const std::string &get(const std::string &name) const;
  113. bool getBool(const std::string &name) const;
  114. u16 getU16(const std::string &name) const;
  115. s16 getS16(const std::string &name) const;
  116. s32 getS32(const std::string &name) const;
  117. u64 getU64(const std::string &name) const;
  118. float getFloat(const std::string &name) const;
  119. v2f getV2F(const std::string &name) const;
  120. v3f getV3F(const std::string &name) const;
  121. u32 getFlagStr(const std::string &name, const FlagDesc *flagdesc,
  122. u32 *flagmask) const;
  123. // N.B. if getStruct() is used to read a non-POD aggregate type,
  124. // the behavior is undefined.
  125. bool getStruct(const std::string &name, const std::string &format,
  126. void *out, size_t olen) const;
  127. bool getNoiseParams(const std::string &name, NoiseParams &np) const;
  128. bool getNoiseParamsFromValue(const std::string &name, NoiseParams &np) const;
  129. bool getNoiseParamsFromGroup(const std::string &name, NoiseParams &np) const;
  130. // return all keys used
  131. std::vector<std::string> getNames() const;
  132. bool exists(const std::string &name) const;
  133. /***************************************
  134. * Getters that don't throw exceptions *
  135. ***************************************/
  136. bool getEntryNoEx(const std::string &name, SettingsEntry &val) const;
  137. bool getGroupNoEx(const std::string &name, Settings *&val) const;
  138. bool getNoEx(const std::string &name, std::string &val) const;
  139. bool getFlag(const std::string &name) const;
  140. bool getU16NoEx(const std::string &name, u16 &val) const;
  141. bool getS16NoEx(const std::string &name, s16 &val) const;
  142. bool getS32NoEx(const std::string &name, s32 &val) const;
  143. bool getU64NoEx(const std::string &name, u64 &val) const;
  144. bool getFloatNoEx(const std::string &name, float &val) const;
  145. bool getV2FNoEx(const std::string &name, v2f &val) const;
  146. bool getV3FNoEx(const std::string &name, v3f &val) const;
  147. // N.B. getFlagStrNoEx() does not set val, but merely modifies it. Thus,
  148. // val must be initialized before using getFlagStrNoEx(). The intention of
  149. // this is to simplify modifying a flags field from a default value.
  150. bool getFlagStrNoEx(const std::string &name, u32 &val, FlagDesc *flagdesc) const;
  151. /***********
  152. * Setters *
  153. ***********/
  154. // N.B. Groups not allocated with new must be set to NULL in the settings
  155. // tree before object destruction.
  156. bool setEntry(const std::string &name, const void *entry,
  157. bool set_group, bool set_default);
  158. bool set(const std::string &name, const std::string &value);
  159. bool setDefault(const std::string &name, const std::string &value);
  160. bool setGroup(const std::string &name, Settings *group);
  161. bool setGroupDefault(const std::string &name, Settings *group);
  162. bool setBool(const std::string &name, bool value);
  163. bool setS16(const std::string &name, s16 value);
  164. bool setU16(const std::string &name, u16 value);
  165. bool setS32(const std::string &name, s32 value);
  166. bool setU64(const std::string &name, u64 value);
  167. bool setFloat(const std::string &name, float value);
  168. bool setV2F(const std::string &name, v2f value);
  169. bool setV3F(const std::string &name, v3f value);
  170. bool setFlagStr(const std::string &name, u32 flags,
  171. const FlagDesc *flagdesc, u32 flagmask);
  172. bool setNoiseParams(const std::string &name, const NoiseParams &np,
  173. bool set_default=false);
  174. // N.B. if setStruct() is used to write a non-POD aggregate type,
  175. // the behavior is undefined.
  176. bool setStruct(const std::string &name, const std::string &format, void *value);
  177. // remove a setting
  178. bool remove(const std::string &name);
  179. void clear();
  180. void clearDefaults();
  181. void updateValue(const Settings &other, const std::string &name);
  182. void update(const Settings &other);
  183. void registerChangedCallback(const std::string &name,
  184. SettingsChangedCallback cbf, void *userdata = NULL);
  185. void deregisterChangedCallback(const std::string &name,
  186. SettingsChangedCallback cbf, void *userdata = NULL);
  187. private:
  188. void updateNoLock(const Settings &other);
  189. void clearNoLock();
  190. void clearDefaultsNoLock();
  191. void doCallbacks(const std::string &name) const;
  192. SettingEntries m_settings;
  193. SettingEntries m_defaults;
  194. SettingsCallbackMap m_callbacks;
  195. mutable Mutex m_callback_mutex;
  196. // All methods that access m_settings/m_defaults directly should lock this.
  197. mutable Mutex m_mutex;
  198. };
  199. #endif