ConfigSet.h 2.9 KB

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