BsHString.h 2.7 KB

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