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