messageformat2_arguments.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // © 2024 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #ifndef MESSAGEFORMAT2_ARGUMENTS_H
  5. #define MESSAGEFORMAT2_ARGUMENTS_H
  6. #if U_SHOW_CPLUSPLUS_API
  7. #if !UCONFIG_NO_FORMATTING
  8. #if !UCONFIG_NO_MF2
  9. /**
  10. * \file
  11. * \brief C++ API: Formats messages using the draft MessageFormat 2.0.
  12. */
  13. #include "unicode/messageformat2_data_model_names.h"
  14. #include "unicode/messageformat2_formattable.h"
  15. #include "unicode/unistr.h"
  16. #ifndef U_HIDE_DEPRECATED_API
  17. #include <map>
  18. U_NAMESPACE_BEGIN
  19. /// @cond DOXYGEN_IGNORE
  20. // Export an explicit template instantiation of the LocalPointer that is used as a
  21. // data member of various MessageFormatDataModel classes.
  22. // (When building DLLs for Windows this is required.)
  23. // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
  24. // for similar examples.)
  25. #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
  26. template class U_I18N_API LocalPointerBase<UnicodeString>;
  27. template class U_I18N_API LocalPointerBase<message2::Formattable>;
  28. template class U_I18N_API LocalArray<UnicodeString>;
  29. template class U_I18N_API LocalArray<message2::Formattable>;
  30. #endif
  31. /// @endcond
  32. namespace message2 {
  33. class MessageContext;
  34. // Arguments
  35. // ----------
  36. /**
  37. *
  38. * The `MessageArguments` class represents the named arguments to a message.
  39. * It is immutable and movable. It is not copyable.
  40. *
  41. * @internal ICU 75 technology preview
  42. * @deprecated This API is for technology preview only.
  43. */
  44. class U_I18N_API MessageArguments : public UObject {
  45. public:
  46. /**
  47. * Message arguments constructor, which takes a map and returns a container
  48. * of arguments that can be passed to a `MessageFormatter`.
  49. *
  50. * @param args A reference to a map from strings (argument names) to `message2::Formattable`
  51. * objects (argument values). The keys and values of the map are copied into the result.
  52. * @param status Input/output error code.
  53. *
  54. * @internal ICU 75 technology preview
  55. * @deprecated This API is for technology preview only.
  56. */
  57. MessageArguments(const std::map<UnicodeString, Formattable>& args, UErrorCode& status) {
  58. if (U_FAILURE(status)) {
  59. return;
  60. }
  61. argumentNames = LocalArray<UnicodeString>(new UnicodeString[argsLen = (int32_t) args.size()]);
  62. arguments = LocalArray<Formattable>(new Formattable[argsLen]);
  63. if (!argumentNames.isValid() || !arguments.isValid()) {
  64. status = U_MEMORY_ALLOCATION_ERROR;
  65. return;
  66. }
  67. int32_t i = 0;
  68. for (auto iter = args.begin(); iter != args.end(); ++iter) {
  69. argumentNames[i] = iter->first;
  70. arguments[i] = iter->second;
  71. i++;
  72. }
  73. }
  74. /**
  75. * Move operator:
  76. * The source MessageArguments will be left in a valid but undefined state.
  77. *
  78. * @internal ICU 75 technology preview
  79. * @deprecated This API is for technology preview only.
  80. */
  81. MessageArguments& operator=(MessageArguments&&) noexcept;
  82. /**
  83. * Default constructor.
  84. * Returns an empty arguments mapping.
  85. *
  86. * @internal ICU 75 technology preview
  87. * @deprecated This API is for technology preview only.
  88. */
  89. MessageArguments() = default;
  90. /**
  91. * Destructor.
  92. *
  93. * @internal ICU 75 technology preview
  94. * @deprecated This API is for technology preview only.
  95. */
  96. virtual ~MessageArguments();
  97. private:
  98. friend class MessageContext;
  99. const Formattable* getArgument(const data_model::VariableName&, UErrorCode&) const;
  100. // Avoids using Hashtable so that code constructing a Hashtable
  101. // doesn't have to appear in this header file
  102. LocalArray<UnicodeString> argumentNames;
  103. LocalArray<Formattable> arguments;
  104. int32_t argsLen = 0;
  105. }; // class MessageArguments
  106. } // namespace message2
  107. U_NAMESPACE_END
  108. #endif // U_HIDE_DEPRECATED_API
  109. #endif /* #if !UCONFIG_NO_MF2 */
  110. #endif /* #if !UCONFIG_NO_FORMATTING */
  111. #endif /* U_SHOW_CPLUSPLUS_API */
  112. #endif // MESSAGEFORMAT2_ARGUMENTS_H
  113. // eof