CmHString.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #pragma once
  2. #include <boost/signals.hpp>
  3. namespace CamelotFramework
  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 CM_UTILITY_EXPORT HString
  16. {
  17. public:
  18. class CM_UTILITY_EXPORT StringData
  19. {
  20. public:
  21. StringData();
  22. ~StringData();
  23. mutable boost::signal<void()> onStringModified;
  24. private:
  25. friend class HString;
  26. LocalizedStringData* mStringData;
  27. WString* mParameters;
  28. boost::signals::connection 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 English version of the string.
  38. */
  39. explicit HString(const WString& identifierString);
  40. HString();
  41. HString(const HString& copy);
  42. ~HString();
  43. operator const WString& () const;
  44. const WString& getValue() const;
  45. /**
  46. * @brief Sets a value of a string parameter. Parameters are specified as bracketed values
  47. * within the string itself (e.g. {0}, {1}) etc.
  48. *
  49. * @note Useful for strings that have dynamically changing values, like numbers, embedded in them.
  50. */
  51. void setParameter(UINT32 idx, const WString& value);
  52. /**
  53. * @brief Registers a callback that gets triggered whenever string value changes. This may happen
  54. * when the string table is modified, or when the active language is changed.
  55. */
  56. boost::signals::connection addOnStringModifiedCallback(std::function<void()> callback) const;
  57. /**
  58. * @brief Returns an empty string.
  59. */
  60. static const HString& dummy();
  61. private:
  62. std::shared_ptr<StringData> mData;
  63. };
  64. }