ConfigSet.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Copyright (C) 2009-2021, Panagiotis Christopoulos Charitos and contributors.
  2. // All rights reserved.
  3. // Code licensed under the BSD License.
  4. // http://www.anki3d.org/LICENSE
  5. #pragma once
  6. #include <AnKi/Core/Common.h>
  7. #include <AnKi/Util/List.h>
  8. #include <AnKi/Util/String.h>
  9. namespace anki {
  10. /// @addtogroup core
  11. /// @{
  12. /// A storage of configuration variables.
  13. class ConfigSet
  14. {
  15. public:
  16. ConfigSet();
  17. /// Copy.
  18. ConfigSet(const ConfigSet& b)
  19. {
  20. operator=(b);
  21. }
  22. ~ConfigSet();
  23. /// Copy.
  24. ConfigSet& operator=(const ConfigSet& b);
  25. /// @name Set the value of an option.
  26. /// @{
  27. void set(CString option, CString value);
  28. template<typename T, ANKI_ENABLE(std::is_integral<T>::value)>
  29. void set(CString option, T value)
  30. {
  31. setInternal(option, U64(value));
  32. }
  33. template<typename T, ANKI_ENABLE(std::is_floating_point<T>::value)>
  34. void set(CString option, T value)
  35. {
  36. setInternal(option, F64(value));
  37. }
  38. /// @}
  39. /// @name Find an option and return its value.
  40. /// @{
  41. F64 getNumberF64(CString option) const;
  42. F32 getNumberF32(CString option) const;
  43. U64 getNumberU64(CString option) const;
  44. U32 getNumberU32(CString option) const;
  45. U16 getNumberU16(CString option) const;
  46. U8 getNumberU8(CString option) const;
  47. Bool getBool(CString option) const;
  48. CString getString(CString option) const;
  49. /// @}
  50. /// @name Create new options.
  51. /// @{
  52. void newOption(CString optionName, CString value, CString helpMsg);
  53. template<typename T, ANKI_ENABLE(std::is_integral<T>::value)>
  54. void newOption(CString optionName, T value, T minValue, T maxValue, CString helpMsg = "")
  55. {
  56. newOptionInternal(optionName, U64(value), U64(minValue), U64(maxValue), helpMsg);
  57. }
  58. template<typename T, ANKI_ENABLE(std::is_floating_point<T>::value)>
  59. void newOption(CString optionName, T value, T minValue, T maxValue, CString helpMsg = "")
  60. {
  61. newOptionInternal(optionName, F64(value), F64(minValue), F64(maxValue), helpMsg);
  62. }
  63. /// @}
  64. ANKI_USE_RESULT Error loadFromFile(CString filename);
  65. ANKI_USE_RESULT Error saveToFile(CString filename) const;
  66. ANKI_USE_RESULT Error setFromCommandLineArguments(U32 cmdLineArgsCount, char* cmdLineArgs[]);
  67. private:
  68. class Option;
  69. HeapAllocator<U8> m_alloc;
  70. List<Option> m_options;
  71. Option* tryFind(CString name);
  72. const Option* tryFind(CString name) const;
  73. Option& find(CString name)
  74. {
  75. Option* o = tryFind(name);
  76. ANKI_ASSERT(o && "Couldn't find config option");
  77. return *o;
  78. }
  79. const Option& find(CString name) const
  80. {
  81. const Option* o = tryFind(name);
  82. ANKI_ASSERT(o && "Couldn't find config option");
  83. return *o;
  84. }
  85. void setInternal(CString option, F64 value);
  86. void setInternal(CString option, U64 value);
  87. void newOptionInternal(CString optionName, U64 value, U64 minValue, U64 maxValue, CString helpMsg);
  88. void newOptionInternal(CString optionName, F64 value, F64 minValue, F64 maxValue, CString helpMsg);
  89. };
  90. /// The default config set. Copy that to your own to override.
  91. using DefaultConfigSet = Singleton<ConfigSet>;
  92. /// @}
  93. } // end namespace anki