Debian Codegraph @

intl/icu/source/i18n/unicode/messageformat2_arguments.h

raw
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 /// @cond DOXYGEN_IGNORE
33 // Export an explicit template instantiation of the LocalPointer that is used as a
34 // data member of various MessageFormatDataModel classes.
35 // (When building DLLs for Windows this is required.)
36 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
37 // for similar examples.)
38 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
39 template class U_I18N_API LocalPointerBase<UnicodeString>;
40 template class U_I18N_API LocalPointerBase<message2::Formattable>;
41 template class U_I18N_API LocalArray<UnicodeString>;
42 template class U_I18N_API LocalArray<message2::Formattable>;
43 #endif
44 /// @endcond
45
46 namespace message2 {
47
48 class MessageFormatter;
49
50 // Arguments
51 // ----------
52
53 /**
54 *
55 * The `MessageArguments` class represents the named arguments to a message.
56 * It is immutable and movable. It is not copyable.
57 *
58 * @internal ICU 75 technology preview
59 * @deprecated This API is for technology preview only.
60 */
61 class U_I18N_API MessageArguments : public UObject {
62 public:
63 /**
64 * Message arguments constructor, which takes a map and returns a container
65 * of arguments that can be passed to a `MessageFormatter`.
66 *
67 * @param args A reference to a map from strings (argument names) to `message2::Formattable`
68 * objects (argument values). The keys and values of the map are copied into the result.
69 * @param status Input/output error code.
70 *
71 * @internal ICU 75 technology preview
72 * @deprecated This API is for technology preview only.
73 */
74 MessageArguments(const std::map<UnicodeString, Formattable>& args, UErrorCode& status) {
75 if (U_FAILURE(status)) {
76 return;
77 }
78 argumentNames = LocalArray<UnicodeString>(new UnicodeString[argsLen = static_cast<int32_t>(args.size())]);
79 arguments = LocalArray<Formattable>(new Formattable[argsLen]);
80 if (!argumentNames.isValid() || !arguments.isValid()) {
81 status = U_MEMORY_ALLOCATION_ERROR;
82 return;
83 }
84 int32_t i = 0;
85 for (auto iter = args.begin(); iter != args.end(); ++iter) {
86 argumentNames[i] = iter->first;
87 arguments[i] = iter->second;
88 i++;
89 }
90 }
91 /**
92 * Move operator:
93 * The source MessageArguments will be left in a valid but undefined state.
94 *
95 * @internal ICU 75 technology preview
96 * @deprecated This API is for technology preview only.
97 */
98 MessageArguments& operator=(MessageArguments&&) noexcept;
99 /**
100 * Default constructor.
101 * Returns an empty arguments mapping.
102 *
103 * @internal ICU 75 technology preview
104 * @deprecated This API is for technology preview only.
105 */
106 MessageArguments() = default;
107 /**
108 * Destructor.
109 *
110 * @internal ICU 75 technology preview
111 * @deprecated This API is for technology preview only.
112 */
113 virtual ~MessageArguments();
114 private:
115 friend class MessageContext;
116
117 const Formattable* getArgument(const MessageFormatter&,
118 const data_model::VariableName&,
119 UErrorCode&) const;
120
121 // Avoids using Hashtable so that code constructing a Hashtable
122 // doesn't have to appear in this header file
123 LocalArray<UnicodeString> argumentNames;
124 LocalArray<Formattable> arguments;
125 int32_t argsLen = 0;
126 }; // class MessageArguments
127
128 } // namespace message2
129
130 U_NAMESPACE_END
131
132 #endif // U_HIDE_DEPRECATED_API
133
134 #endif /* #if !UCONFIG_NO_MF2 */
135
136 #endif /* #if !UCONFIG_NO_FORMATTING */
137
138 #endif /* #if !UCONFIG_NO_NORMALIZATION */
139
140 #endif /* U_SHOW_CPLUSPLUS_API */
141
142 #endif // MESSAGEFORMAT2_ARGUMENTS_H
143
144 // eof