2
0

Config.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. #pragma once
  2. #include "App.h"
  3. namespace gameplay
  4. {
  5. /**
  6. * Defines a way to get/set config values from the system.
  7. *
  8. * Config system uses .toml files with subset of features within this api.
  9. *
  10. * It supports string, bool, int and float, simple arrays and table arrays.
  11. *
  12. * For ease of developer usage for configuration purposes:
  13. * int are casted down from int64
  14. * float are casted down from double
  15. * all time/date configuration is not supported.
  16. * Only simple non-mixed array types are supported.
  17. *
  18. * For more information on toml markup see:
  19. * https://github.com/toml-lang/toml
  20. *
  21. * Sub-systems supporting config values should set their default values using set_xxx in their constuctors.
  22. * IF the config has specified values, they will be used instead of the system specified values.
  23. */
  24. class GP_API Config
  25. {
  26. friend class App;
  27. public:
  28. /**
  29. * Constructor.
  30. *
  31. * @see App::get_config() instead.
  32. */
  33. Config();
  34. /**
  35. * Destuctor.
  36. */
  37. ~Config();
  38. /**
  39. * Gets a string value from config using the lookup key.
  40. *
  41. * @param key The lookup key for the string value.
  42. * @param altValue The alternative value to use if key not found.
  43. */
  44. std::string get_string(const char* key, const char* altValue);
  45. /**
  46. * Sets a (default) string value for config to use for the specified key.
  47. *
  48. * If the key has a value already loaded from config file. The value will not be set.
  49. *
  50. * @param key The key for which to set the value.
  51. * @param value The (default) value to be set into config.
  52. * @return The resolved value in config after attempting to be set
  53. */
  54. std::string set_string(const char* key, const char* value);
  55. /**
  56. * Gets a boolean value from config using the lookup key.
  57. *
  58. * @param key The lookup key for the boolean value.
  59. * @param altValue The alternative value to use if key not found.
  60. */
  61. bool get_bool(const char* key, bool altValue);
  62. /**
  63. * Sets a (default) boolean value for config to use for the specified key.
  64. *
  65. * If the key has a value already loaded from config file. The value will not be set.
  66. *
  67. * @param key The key for which to set the value.
  68. * @param value The (default) value to be set into config.
  69. * @return The resolved value in config after attempting to be set
  70. */
  71. bool set_bool(const char* key, bool value);
  72. /**
  73. * Gets a integer value from config using the lookup key.
  74. *
  75. * @param key The lookup key for the integer value.
  76. * @param altValue The alternative value to use if key not found.
  77. */
  78. int get_int(const char* key, int altValue);
  79. /**
  80. * Sets a (default) integer value for config to use for the specified key.
  81. *
  82. * If the key has a value already loaded from config file. The value will not be set.
  83. *
  84. * @param key The key for which to set the value.
  85. * @param value The (default) value to be set into config.
  86. * @return The resolved value in config after attempting to be set
  87. */
  88. int set_int(const char* key, int value);
  89. /**
  90. * Gets a floating-point value from config using the lookup key.
  91. *
  92. * @param key The lookup key for the floating-point value.
  93. * @param altValue The alternative value to use if key not found.
  94. */
  95. float get_float(const char* key, float altValue);
  96. /**
  97. * Sets a (default) floating-point value for config to use for the specified key.
  98. *
  99. * If the key has a value already loaded from config file. The value will not be set.
  100. *
  101. * @param key The key for which to set the value.
  102. * @param value The (default) value to be set into config.
  103. * @return The resolved value in config after attempting to be set
  104. */
  105. float set_float(const char* key, float value);
  106. /**
  107. * Gets the size (number of elements) for the array at the specified key.
  108. *
  109. * @param key The key for which to lookup the array size.
  110. * @return The size of the array.
  111. */
  112. size_t get_array_size(const char* key);
  113. /**
  114. * Gets the string array (copies the contents) from config using the lookup key.
  115. *
  116. * @param key The key for which to get the string array.
  117. * @param arr The destination array to be copied into from config.
  118. * @param arrSize The size of the array passed.
  119. */
  120. void get_string_array(const char* key, std::string* arr, size_t arrSize);
  121. /**
  122. * Sets the string array (copies the contents) to config using the lookup key.
  123. *
  124. * @param key The key for which to set the string array.
  125. * @param arr The source array to be copied from.
  126. * @param arrSize The size of the array passed.
  127. */
  128. void set_string_array(const char* key, const std::string* arr, size_t arrSize);
  129. /**
  130. * Gets the boolean array (copies the contents) from config using the lookup key.
  131. *
  132. * @param key The key for which to get the boolean array.
  133. * @param arr The destination array to be copied into from config.
  134. * @param arrSize The size of the array passed.
  135. */
  136. void get_bool_array(const char* key, bool* arr, size_t arrSize);
  137. /**
  138. * Sets the boolean array (copies the contents) to config using the lookup key.
  139. *
  140. * @param key The key for which to set the boolean array.
  141. * @param arr The source array to be copied from.
  142. * @param arrSize The size of the array passed.
  143. */
  144. void set_bool_array(const char* key, const bool* arr, size_t arrSize);
  145. /**
  146. * Gets the integer array (copies the contents) from config using the lookup key.
  147. *
  148. * @param key The key for which to get the integer array.
  149. * @param arr The destination array to be copied into from config.
  150. * @param arrSize The size of the array passed.
  151. */
  152. void get_int_array(const char* key, int* arr, size_t arrSize);
  153. /**
  154. * Sets the integer array (copies the contents) to config using the lookup key.
  155. *
  156. * @param key The key for which to set the integer array.
  157. * @param arr The source array to be copied from.
  158. * @param arrSize The size of the array passed.
  159. */
  160. void set_int_array(const char* key, const int* arr, size_t arrSize);
  161. /**
  162. * Gets the floating-point array (copies the contents) from config using the lookup key.
  163. *
  164. * @param key The key for which to get the floating-point array.
  165. * @param arr The destination array to be copied into from config.
  166. * @param arrSize The size of the array passed.
  167. */
  168. void get_float_array(const char* key, float* arr, size_t arrSize);
  169. /**
  170. * Sets the floating-point array (copies the contents) to config using the lookup key.
  171. *
  172. * @param key The key for which to set the floating-point array.
  173. * @param arr The source array to be copied from.
  174. * @param arrSize The size of the array passed.
  175. */
  176. void set_float_array(const char* key, const float* arr, size_t arrSize);
  177. /**
  178. * Callback function used for iterating over array of tables for the specified key.
  179. *
  180. * This function is called for each visit to a table in the array of tables.
  181. *
  182. * @param The config interface to use for accessing internal table config.
  183. * @param userPtr The user pointer specified when for_each_table
  184. */
  185. typedef bool(*OnVisitTableFn)(Config* config, void* userPtr);
  186. /**
  187. * Iterates of the table of array at the specifed key.
  188. *
  189. * This function calls the callback function on each visit to table in the array.
  190. *
  191. * @param key The key for the table array to iterate over.
  192. * @param fn The function to call on each vist to an element in the table array.
  193. * @param userPtr The user pointer to be pass to each callback function call.
  194. */
  195. void for_each_table(const char* key, OnVisitTableFn fn, void* userPtr);
  196. private:
  197. void load(int argc, char** argv);
  198. struct Impl;
  199. Impl* _impl = nullptr;
  200. };
  201. }