BsHString.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. #pragma once
  2. #include "BsEvent.h"
  3. namespace BansheeEngine
  4. {
  5. /**
  6. * @brief String handle. Provides a wrapper around an Unicode string, primarily
  7. * for localization purposes.
  8. *
  9. * Actual value for this string is looked up in a global string table based
  10. * on the provided identifier string and currently active language. If such value
  11. * doesn't exist then the identifier is used as is.
  12. *
  13. * Use {0}, {1}, etc. in the string value for values that might change dynamically.
  14. */
  15. class BS_UTILITY_EXPORT HString
  16. {
  17. public:
  18. class BS_UTILITY_EXPORT StringData
  19. {
  20. public:
  21. StringData();
  22. ~StringData();
  23. mutable Event<void()> onStringModified;
  24. private:
  25. friend class HString;
  26. LocalizedStringData* mStringData;
  27. WString* mParameters;
  28. HEvent mUpdateConn;
  29. mutable bool mIsDirty;
  30. mutable WString mCachedString;
  31. mutable WString* mStringPtr;
  32. void updateString();
  33. };
  34. /**
  35. * @brief Creates a new localized string with the specified identifier. If the identifier
  36. * doesn't previously exist in the string table, identifier value will also be used
  37. * for initializing the default language version of the string.
  38. */
  39. explicit HString(const WString& identifierString);
  40. /**
  41. * @brief Creates a new localized string with the specified identifier and sets the default language version
  42. * of the string. If a string with that identifier already exists default language string will be updated.
  43. */
  44. explicit HString(const WString& identifierString, const WString& englishString);
  45. HString();
  46. HString(const HString& copy);
  47. ~HString();
  48. operator const WString& () const;
  49. const WString& getValue() const;
  50. /**
  51. * @brief Sets a value of a string parameter. Parameters are specified as bracketed values
  52. * within the string itself (e.g. {0}, {1}) etc.
  53. *
  54. * @note Useful for strings that have dynamically changing values, like numbers, embedded in them.
  55. */
  56. void setParameter(UINT32 idx, const WString& value);
  57. /**
  58. * @brief Registers a callback that gets triggered whenever string value changes. This may happen
  59. * when the string table is modified, or when the active language is changed.
  60. */
  61. HEvent addOnStringModifiedCallback(std::function<void()> callback) const;
  62. /**
  63. * @brief Returns an empty string.
  64. */
  65. static const HString& dummy();
  66. private:
  67. std::shared_ptr<StringData> mData;
  68. };
  69. }