ICU 75.1 75.1
Loading...
Searching...
No Matches
messageformat2.h
Go to the documentation of this file.
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_H
7#define MESSAGEFORMAT2_H
8
9#if U_SHOW_CPLUSPLUS_API
10
11#if !UCONFIG_NO_FORMATTING
12
13#if !UCONFIG_NO_MF2
14
21#include "unicode/messageformat2_data_model.h"
22#include "unicode/messageformat2_function_registry.h"
23#include "unicode/unistr.h"
24
25#ifndef U_HIDE_DEPRECATED_API
26
27U_NAMESPACE_BEGIN
28
29namespace message2 {
30
31 class CachedFormatters;
32 class Environment;
33 class MessageContext;
34 class ResolvedSelector;
35 class StaticErrors;
36
53 // Note: This class does not currently inherit from the existing
54 // `Format` class.
55 public:
71
87
104 (void) arguments;
105 if (U_SUCCESS(status)) {
107 }
108 return FormattedMessage(status);
109 }
110
119 const Locale& getLocale() const { return locale; }
120
131
141 const MFDataModel& getDataModel() const;
142
149 class U_I18N_API Builder : public UObject {
150 private:
151 friend class MessageFormatter;
152
153 // The pattern to be parsed to generate the formatted message
154 UnicodeString pattern;
155 bool hasPattern = false;
156 bool hasDataModel = false;
157 // The data model to be used to generate the formatted message
158 // Initialized either by `setDataModel()`, or by the parser
159 // through a call to `setPattern()`
160 MFDataModel dataModel;
161 // Normalized representation of the pattern;
162 // ignored if `setPattern()` wasn't called
163 UnicodeString normalizedInput;
164 // Errors (internal representation of parse errors)
165 // Ignored if `setPattern()` wasn't called
166 StaticErrors* errors;
167 Locale locale;
168 // Not owned
169 const MFFunctionRegistry* customMFFunctionRegistry;
170
171 public:
181 Builder& setLocale(const Locale& locale);
221 Builder& setDataModel(MFDataModel&& dataModel);
255 virtual ~Builder();
256 }; // class MessageFormatter::Builder
257
258 // TODO: Shouldn't be public; only used for testing
267 const UnicodeString& getNormalizedPattern() const { return normalizedInput; }
268
269 private:
270 friend class Builder;
271 friend class MessageContext;
272
274
275 MessageFormatter() = delete; // default constructor not implemented
276
277 // Do not define default assignment operator
278 const MessageFormatter &operator=(const MessageFormatter &) = delete;
279
280 ResolvedSelector resolveVariables(const Environment& env, const data_model::Operand&, MessageContext&, UErrorCode &) const;
281 ResolvedSelector resolveVariables(const Environment& env, const data_model::Expression&, MessageContext&, UErrorCode &) const;
282
283 // Selection methods
284
285 // Takes a vector of FormattedPlaceholders
286 void resolveSelectors(MessageContext&, const Environment& env, UErrorCode&, UVector&) const;
287 // Takes a vector of vectors of strings (input) and a vector of PrioritizedVariants (output)
288 void filterVariants(const UVector&, UVector&, UErrorCode&) const;
289 // Takes a vector of vectors of strings (input) and a vector of PrioritizedVariants (input/output)
290 void sortVariants(const UVector&, UVector&, UErrorCode&) const;
291 // Takes a vector of strings (input) and a vector of strings (output)
292 void matchSelectorKeys(const UVector&, MessageContext&, ResolvedSelector&& rv, UVector&, UErrorCode&) const;
293 // Takes a vector of FormattedPlaceholders (input),
294 // and a vector of vectors of strings (output)
295 void resolvePreferences(MessageContext&, UVector&, UVector&, UErrorCode&) const;
296
297 // Formatting methods
298 [[nodiscard]] FormattedPlaceholder formatLiteral(const data_model::Literal&) const;
299 void formatPattern(MessageContext&, const Environment&, const data_model::Pattern&, UErrorCode&, UnicodeString&) const;
300 // Formats a call to a formatting function
301 // Dispatches on argument type
303 MessageContext& context,
304 UErrorCode& status) const;
305 // Dispatches on function name
306 [[nodiscard]] FormattedPlaceholder evalFormatterCall(const FunctionName& functionName,
308 FunctionOptions&& options,
309 MessageContext& context,
310 UErrorCode& status) const;
311 // Formats an expression that appears as a selector
312 ResolvedSelector formatSelectorExpression(const Environment& env, const data_model::Expression&, MessageContext&, UErrorCode&) const;
313 // Formats an expression that appears in a pattern or as the definition of a local variable
314 [[nodiscard]] FormattedPlaceholder formatExpression(const Environment&, const data_model::Expression&, MessageContext&, UErrorCode&) const;
315 [[nodiscard]] FunctionOptions resolveOptions(const Environment& env, const OptionMap&, MessageContext&, UErrorCode&) const;
316 [[nodiscard]] FormattedPlaceholder formatOperand(const Environment&, const data_model::Operand&, MessageContext&, UErrorCode&) const;
317 [[nodiscard]] FormattedPlaceholder evalArgument(const data_model::VariableName&, MessageContext&, UErrorCode&) const;
318 void formatSelectors(MessageContext& context, const Environment& env, UErrorCode &status, UnicodeString& result) const;
319
320 // Function registry methods
321 bool hasCustomMFFunctionRegistry() const {
322 return (customMFFunctionRegistry != nullptr);
323 }
324
325 // Precondition: custom function registry exists
326 // Note: this is non-const because the values in the MFFunctionRegistry are mutable
327 // (a FormatterFactory can have mutable state)
328 const MFFunctionRegistry& getCustomMFFunctionRegistry() const;
329
330 bool isCustomFormatter(const FunctionName&) const;
331 FormatterFactory* lookupFormatterFactory(MessageContext&, const FunctionName&, UErrorCode& status) const;
332 bool isBuiltInSelector(const FunctionName&) const;
333 bool isBuiltInFormatter(const FunctionName&) const;
334 bool isCustomSelector(const FunctionName&) const;
335 const SelectorFactory* lookupSelectorFactory(MessageContext&, const FunctionName&, UErrorCode&) const;
336 bool isSelector(const FunctionName& fn) const { return isBuiltInSelector(fn) || isCustomSelector(fn); }
337 bool isFormatter(const FunctionName& fn) const { return isBuiltInFormatter(fn) || isCustomFormatter(fn); }
338 const Formatter* maybeCachedFormatter(MessageContext&, const FunctionName&, UErrorCode&) const;
339
340 Selector* getSelector(MessageContext&, const FunctionName&, UErrorCode&) const;
341 const Formatter& getFormatter(MessageContext&, const FunctionName&, UErrorCode&) const;
342 bool getDefaultFormatterNameByType(const UnicodeString&, FunctionName&) const;
343
344 // Checking for resolution errors
345 void checkDeclarations(MessageContext&, Environment*&, UErrorCode&) const;
346 void check(MessageContext&, const Environment&, const data_model::Expression&, UErrorCode&) const;
347 void check(MessageContext&, const Environment&, const data_model::Operand&, UErrorCode&) const;
348 void check(MessageContext&, const Environment&, const OptionMap&, UErrorCode&) const;
349
350 void initErrors(UErrorCode&);
351 void clearErrors() const;
352 void cleanup() noexcept;
353
354 // The locale this MessageFormatter was created with
355 /* const */ Locale locale;
356
357 // Registry for built-in functions
358 MFFunctionRegistry standardMFFunctionRegistry;
359 // Registry for custom functions; may be null if no custom registry supplied
360 // Note: this is *not* owned by the MessageFormatter object
361 // The reason for this choice is to have a non-destructive MessageFormatter::Builder,
362 // while also not requiring the function registry to be deeply-copyable. Making the
363 // function registry copyable would impose a requirement on any implementations
364 // of the FormatterFactory and SelectorFactory interfaces to implement a custom
365 // clone() method, which is necessary to avoid sharing between copies of the
366 // function registry (and thus double-frees)
367 // Not deeply immutable (the values in the function registry are mutable,
368 // as a FormatterFactory can have mutable state
369 const MFFunctionRegistry* customMFFunctionRegistry;
370
371 // Data model, representing the parsed message
372 MFDataModel dataModel;
373
374 // Normalized version of the input string (optional whitespace removed)
375 UnicodeString normalizedInput;
376
377 // Formatter cache
378 // Must be a raw pointer to avoid including the internal header file
379 // defining CachedFormatters
380 // Owned by `this`
381 // TODO: This is an optimization that the "TemperatureFormatter" test
382 // (ported from ICU4J) was checking for; however, that test was removed
383 // in order to make `setFormatter()` safe, so maybe this should be
384 // removed too
385 CachedFormatters* cachedFormatters;
386
387 // Errors -- only used while parsing and checking for data model errors; then
388 // the MessageContext keeps track of errors
389 // Must be a raw pointer to avoid including the internal header file
390 // defining StaticErrors
391 // Owned by `this`
392 StaticErrors* errors;
393
394 }; // class MessageFormatter
395
396} // namespace message2
397
398U_NAMESPACE_END
399
400#endif // U_HIDE_DEPRECATED_API
401
402#endif /* #if !UCONFIG_NO_MF2 */
403
404#endif /* #if !UCONFIG_NO_FORMATTING */
405
406#endif /* U_SHOW_CPLUSPLUS_API */
407
408#endif // MESSAGEFORMAT2_H
409
410// eof
"Smart pointer" base class; do not use directly: use LocalPointer etc.
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:195
UObject is the common ICU "boilerplate" class.
Definition uobject.h:223
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
Not yet implemented: The result of a message formatting operation.
A FormattablePlaceholder encapsulates an input value (a message2::Formattable) together with an optio...
Structure encapsulating named options passed to a custom selector or formatter.
Defines mappings from names of formatters and selectors to functions implementing them.
The MessageArguments class represents the named arguments to a message.
The mutable Builder class allows each part of the MessageFormatter to be initialized separately; call...
Builder & setFunctionRegistry(const MFFunctionRegistry &functionRegistry)
Sets a custom function registry.
MessageFormatter build(UErrorCode &status) const
Constructs a new immutable MessageFormatter using the pattern or data model that was previously set,...
Builder & setLocale(const Locale &locale)
Sets the locale to use for formatting.
Builder(UErrorCode &status)
Default constructor.
Builder & setPattern(const UnicodeString &pattern, UParseError &parseError, UErrorCode &status)
Sets the pattern (contents of the message) and parses it into a data model.
Builder & setDataModel(MFDataModel &&dataModel)
Sets a data model.
const Locale & getLocale() const
Accesses the locale that this MessageFormatter object was created with.
const UnicodeString & getNormalizedPattern() const
Returns a string consisting of the input with optional spaces removed.
UnicodeString getPattern() const
Serializes the data model as a string in MessageFormat 2.0 syntax.
MessageFormatter & operator=(MessageFormatter &&) noexcept
Move assignment operator: The source MessageFormatter will be left in a valid but undefined state.
const MFDataModel & getDataModel() const
Accesses the data model referred to by this MessageFormatter object.
The Expression class corresponds to the expression nonterminal in the MessageFormat 2 grammar and the...
The Literal class corresponds to the literal nonterminal in the MessageFormat 2 grammar,...
C++ API: Formats messages using the draft MessageFormat 2.0.
A UParseError struct is used to returned detailed information about parsing errors.
Definition parseerr.h:58
C++ API: Unicode String.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:415
@ U_UNSUPPORTED_ERROR
Requested operation not supported in current context.
Definition utypes.h:467
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:728
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:301