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