BsSettings.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #pragma once
  4. #include "BsEditorPrerequisites.h"
  5. #include "Reflection/BsIReflectable.h"
  6. namespace bs
  7. {
  8. namespace impl
  9. {
  10. /** @addtogroup Implementation
  11. * @{
  12. */
  13. /** Union of possible primitive types a settings value field can take on. */
  14. union SettingsPrimitiveValue
  15. {
  16. SettingsPrimitiveValue() = default;
  17. SettingsPrimitiveValue(float floatVal)
  18. :floatVal(floatVal)
  19. { }
  20. SettingsPrimitiveValue(INT32 intVal)
  21. :intVal(intVal)
  22. { }
  23. SettingsPrimitiveValue(bool boolVal)
  24. :boolVal(boolVal)
  25. { }
  26. float floatVal;
  27. INT32 intVal;
  28. bool boolVal;
  29. };
  30. /** Types of possible values stored in the settings. */
  31. enum class SettingsValueType
  32. {
  33. Float, Int, Bool, String, Object
  34. };
  35. /** Information about where to find a value for a key. */
  36. struct SettingsKeyInfo
  37. {
  38. SettingsKeyInfo() = default;
  39. SettingsKeyInfo(UINT32 index, SettingsValueType type)
  40. : index(index), type(type)
  41. { }
  42. /** Index of the value in the relevant value array. */
  43. UINT32 index;
  44. /** Type of the value, determining which array is the value stored in. */
  45. SettingsValueType type;
  46. };
  47. /** Contains an object and corresponding key in the settings list. */
  48. struct SettingsObjectValue : IReflectable
  49. {
  50. SettingsObjectValue() = default;
  51. SettingsObjectValue(const String& key, const SPtr<IReflectable>& value)
  52. : key(key), value(value)
  53. { }
  54. /** Key used to look up the value. */
  55. String key;
  56. /** Stored value. */
  57. SPtr<IReflectable> value;
  58. /************************************************************************/
  59. /* RTTI */
  60. /************************************************************************/
  61. public:
  62. friend class SettingsObjectValueRTTI;
  63. static RTTITypeBase* getRTTIStatic();
  64. RTTITypeBase* getRTTI() const override;
  65. };
  66. /** Information about a value attached to a key. */
  67. template<class T>
  68. struct TSettingsValue
  69. {
  70. TSettingsValue() = default;
  71. TSettingsValue(const String& key, const T& value)
  72. : key(key), value(value)
  73. { }
  74. /** Key used to look up the value. */
  75. String key;
  76. /** Stored value. */
  77. T value;
  78. };
  79. /** @} */
  80. }
  81. /** @addtogroup Settings
  82. * @{
  83. */
  84. /** Contains a serializable set of generic key-value pairs. */
  85. class BS_ED_EXPORT Settings : public IReflectable
  86. {
  87. public:
  88. Settings() = default;
  89. virtual ~Settings() = default;
  90. /** Adds or updates a property key/value pair with a floating point value. */
  91. void setFloat(const String& name, float value);
  92. /** Adds or updates a property key/value pair with a signed integer value. */
  93. void setInt(const String& name, INT32 value);
  94. /** Adds or updates a property key/value pair with a boolean value. */
  95. void setBool(const String& name, bool value);
  96. /** Adds or updates a property key/value pair with a string value. */
  97. void setString(const String& name, const String& value);
  98. /** Adds or updates a property key/value pair with a serializable object. */
  99. void setObject(const String& name, const SPtr<IReflectable>& value);
  100. /** Returns the floating point value of the specified key, or the default value if such key cannot be found. */
  101. float getFloat(const String& name, float defaultValue = 0.0f);
  102. /** Returns the integer point value of the specified key, or the default value if such key cannot be found. */
  103. INT32 getInt(const String& name, INT32 defaultValue = 0);
  104. /** Returns the boolean point value of the specified key, or the default value if such key cannot be found. */
  105. bool getBool(const String& name, bool defaultValue = false);
  106. /** Returns the string point value of the specified key, or the default value if such key cannot be found. */
  107. String getString(const String& name, const String& defaultValue = StringUtil::BLANK);
  108. /** Returns the object value of the specified key, or null if such key cannot be found. */
  109. SPtr<IReflectable> getObject(const String& name);
  110. /** Returns true if the key with the specified name exists. */
  111. bool hasKey(const String& name);
  112. /** Deletes a key with the specified name. */
  113. void deleteKey(const String& name);
  114. /** Deletes all key/value pairs. */
  115. void deleteAllKeys();
  116. /** Returns a hash value that may be used for checking if any internal settings were modified. */
  117. UINT32 getHash() const { return mHash; }
  118. protected:
  119. /** Marks the object as dirty so that outside objects know when to update. */
  120. void markAsDirty() const { mHash++; }
  121. UnorderedMap<String, impl::SettingsKeyInfo> mKeyLookup;
  122. Vector<impl::TSettingsValue<impl::SettingsPrimitiveValue>> mPrimitives;
  123. Vector<impl::TSettingsValue<String>> mStrings;
  124. Vector<impl::SettingsObjectValue> mObjects;
  125. mutable UINT32 mHash = 0;
  126. /************************************************************************/
  127. /* RTTI */
  128. /************************************************************************/
  129. public:
  130. friend class SettingsRTTI;
  131. static RTTITypeBase* getRTTIStatic();
  132. RTTITypeBase* getRTTI() const override;
  133. };
  134. /** @} */
  135. }