ICU4C 79
Loading...
Searching...
No Matches
messageformat2_function_registry.h
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_FUNCTION_REGISTRY_H
7#define MESSAGEFORMAT2_FUNCTION_REGISTRY_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#include "unicode/messageformat2_data_model_names.h"
18#include "unicode/messageformat2_formattable.h"
19#include "unicode/ubidi.h"
20
21#ifndef U_HIDE_DEPRECATED_API
22
23#include <map>
24
25U_NAMESPACE_BEGIN
26
27class Hashtable;
28class UVector;
29
30namespace message2 {
31
32 using namespace data_model;
33
34 class Function;
35
47 private:
48
49 using FunctionMap = Hashtable; // Map from function names to FunctionFactory*
50
51 public:
64 const Function* getFunction(const FunctionName& functionName) const;
78 UBool getDefaultFormatterNameByType(const UnicodeString& formatterType, FunctionName& name) const;
89 class U_I18N_API Builder : public UObject {
90 private:
91 // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
92 FunctionMap* functions;
93 // Mapping from strings (type tags) to FunctionNames
94 Hashtable* formattersByType = nullptr;
95
96 // Do not define copy constructor/assignment operator
97 Builder& operator=(const Builder&) = delete;
98 Builder(const Builder&) = delete;
99
100 public:
101 /*
102 Notes about `adoptFormatter()`'s type signature:
103
104 Alternative considered: take a non-owned FormatterFactory*
105 This is unsafe.
106
107 Alternative considered: take a FormatterFactory&
108 This requires getFormatter() to cast the reference to a pointer,
109 as it must return an unowned FormatterFactory* since it can fail.
110 That is also unsafe, since the caller could delete the pointer.
111
112 The "TemperatureFormatter" test from the previous ICU4J version doesn't work now,
113 as it only works if the `formatterFactory` argument is non-owned.
114 If registering a non-owned FormatterFactory is desirable, this could
115 be re-thought.
116 */
131 Builder& adoptFunction(const data_model::FunctionName& functionName,
132 Function* function,
133 UErrorCode& errorCode);
148 const data_model::FunctionName& functionName,
149 UErrorCode& errorCode);
173 Builder(UErrorCode& errorCode);
180 virtual ~Builder();
181 }; // class MFFunctionRegistry::Builder
182
198 MFFunctionRegistry(MFFunctionRegistry&& other) { *this = std::move(other); }
206
215
216 private:
217 friend class MessageContext;
218 friend class MessageFormatter;
219 friend class MF2RegistrySingleton;
220
221 // Do not define copy constructor or copy assignment operator
223 MFFunctionRegistry(const MFFunctionRegistry&) = delete;
224
225 MFFunctionRegistry(FunctionMap*, Hashtable*);
226
228
229 // Debugging; should only be called on a function registry with
230 // all the standard functions registered
231 void checkFunction(std::u16string_view) const;
232 void checkStandard() const;
233
234 bool hasFunction(const data_model::FunctionName& f) const;
235 void cleanup() noexcept;
236
237 // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
238 FunctionMap* functions = nullptr;
239 // Mapping from strings (type tags) to FunctionNames
240 Hashtable* formattersByType = nullptr;
241 }; // class MFFunctionRegistry
242
250 class U_I18N_API_CLASS FunctionContext : public UObject {
251 public:
260 U_I18N_API const Locale& getLocale() const { return locale; }
269 U_I18N_API UMFBidiOption getDirection() const { return dir; }
279 U_I18N_API const UnicodeString& getID() const { return id; }
280
289 U_I18N_API const FunctionName& getCalledFunctionName() const { return calledFunction; }
290
299 U_I18N_API FunctionContext withLocale(const Locale& loc) const {
300 return FunctionContext(loc, getDirection(), getID(), getCalledFunctionName());
301 }
302
303 private:
304 friend class MessageFormatter;
305
306 Locale locale;
307 UMFBidiOption dir;
308 UnicodeString id;
309 const FunctionName& calledFunction;
310
311 FunctionContext(const Locale& loc, UMFBidiOption d, UnicodeString i, const FunctionName& calledFn)
312 : locale(loc), dir(d), id(i), calledFunction(calledFn) {}
313 }; // class FunctionContext
314
315 class FunctionValue;
316
323 class U_I18N_API Function : public UObject {
324 public:
340 const FunctionValue& operand,
341 const FunctionOptions& options,
342 UErrorCode& status) const = 0;
349 virtual ~Function();
350 }; // class Function
351
369 public:
382 if (U_SUCCESS(status)) {
383 status = U_MF_FORMATTING_ERROR;
384 }
385 return {};
386 }
387
397 U_I18N_API virtual const Formattable& unwrap() const { return innerValue; }
406 U_I18N_API virtual const FunctionOptions& getResolvedOptions() const { return opts; }
417 U_I18N_API virtual UMFDirectionality getDirection() const { return dir; }
443 U_I18N_API virtual UBool isSelectable() const {
444 // In the future, this function could return a capability
445 // indicating whether this function can format, select, or both.
446 return false;
447 }
448
459 U_I18N_API virtual UBool isNullOperand() const { return false; }
480 U_I18N_API virtual void selectKeys(const UnicodeString* keys,
481 int32_t keysLen,
482 int32_t* prefs,
483 int32_t& prefsLen,
484 UErrorCode& status) const {
485 (void) keys;
486 (void) keysLen;
487 (void) prefs;
488 (void) prefsLen;
489 if (U_SUCCESS(status)) {
490 status = U_MF_SELECTOR_ERROR;
491 }
492 }
493
502 U_I18N_API virtual const FunctionName& getFunctionName() const { return functionName; }
512 U_I18N_API virtual const UnicodeString& getFallback() const { return fallback; }
520 protected:
536 FunctionOptions opts;
543 FunctionName functionName;
578 private:
579 friend class FunctionOptions;
580
581 // Should only be overridden by BaseValue
595 virtual UBool wasCreatedFromLiteral() const { return false; }
596 }; // class FunctionValue
597
598} // namespace message2
599
600U_NAMESPACE_END
601
602#endif // U_HIDE_DEPRECATED_API
603
604#endif /* #if !UCONFIG_NO_MF2 */
605
606#endif /* #if !UCONFIG_NO_FORMATTING */
607
608#endif /* #if !UCONFIG_NO_NORMALIZATION */
609
610#endif /* U_SHOW_CPLUSPLUS_API */
611
612#endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_H
613
614// eof
"Smart pointer" class, deletes objects via the standard C++ delete operator.
A Locale object represents a specific geographical, political, or cultural region.
Definition locid.h:198
UObject is the common ICU "boilerplate" class.
Definition uobject.h:222
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
The Formattable class represents a typed value that can be formatted, originating either from a messa...
Class implementing data from contextual options.
U_I18N_API const UnicodeString & getID() const
Returns the ID from this context.
U_I18N_API const FunctionName & getCalledFunctionName() const
Returns the original called function name from this context.
U_I18N_API UMFBidiOption getDirection() const
Returns the text direction from this context.
U_I18N_API FunctionContext withLocale(const Locale &loc) const
Returns a new context with the specified locale.
U_I18N_API const Locale & getLocale() const
Returns the locale from this context.
Structure encapsulating named options passed to a custom selector or formatter.
Type representing argument and return values for custom functions.
virtual U_I18N_API UMFBidiOption getDirectionAnnotation() const
Returns the directionality that this value was annotated with.
virtual U_I18N_API ~FunctionValue()
Destructor.
Formattable innerValue
Computed result of the function invocation that returned this FunctionValue.
Locale locale
Locale from u:locale option.
UMFBidiOption inputDir
Input directionality from u:dir option.
virtual U_I18N_API const UnicodeString & getFallback() const
Returns a fallback string that can be used as output if processing this function results in an error.
virtual U_I18N_API void selectKeys(const UnicodeString *keys, int32_t keysLen, int32_t *prefs, int32_t &prefsLen, UErrorCode &status) const
Compares this value to an array of keys, and returns an array of matching keys sorted by preference.
virtual U_I18N_API UnicodeString formatToString(UErrorCode &status) const
Returns the string representation of this value.
virtual U_I18N_API UBool isSelectable() const
Returns true if this value supports selection.
FunctionOptions opts
Resolved options attached to this value.
UnicodeString fallback
Fallback string that can be used if a later function encounters an error when processing this Functio...
FunctionName functionName
The name of the function that constructed this FunctionValue.
virtual U_I18N_API UBool isNullOperand() const
Returns true if this value represents a null operand, that is, the absence of an argument.
virtual U_I18N_API const FunctionOptions & getResolvedOptions() const
Returns a reference to the resolved options for this value.
UMFDirectionality dir
Directionality of formatted result.
virtual U_I18N_API UMFDirectionality getDirection() const
Returns the directionality of this value, i.e.
virtual U_I18N_API const FunctionName & getFunctionName() const
Returns the name of the function that constructed this value.
virtual U_I18N_API const Formattable & unwrap() const
Returns the Formattable operand that was used to construct this value.
Interface that function handler classes must implement.
virtual ~Function()
Destructor.
virtual LocalPointer< FunctionValue > call(const FunctionContext &context, const FunctionValue &operand, const FunctionOptions &options, UErrorCode &status) const =0
Calls this Function on a FunctionValue operand and its FunctionOptions options, returning a LocalPoin...
Builder(UErrorCode &errorCode)
Default constructor.
MFFunctionRegistry build()
Creates an immutable MFFunctionRegistry object with the selectors and formatters that were previously...
Builder & adoptFunction(const data_model::FunctionName &functionName, Function *function, UErrorCode &errorCode)
Registers a function to a given name.
Builder & setDefaultFormatterNameByType(const UnicodeString &type, const data_model::FunctionName &functionName, UErrorCode &errorCode)
Registers a formatter factory to a given type tag.
virtual ~MFFunctionRegistry()
Destructor.
MFFunctionRegistry(MFFunctionRegistry &&other)
Move constructor: The source MFFunctionRegistry will be left in a valid but undefined state.
MFFunctionRegistry & operator=(MFFunctionRegistry &&) noexcept
Move assignment operator: The source MFFunctionRegistry will be left in a valid but undefined state.
UBool getDefaultFormatterNameByType(const UnicodeString &formatterType, FunctionName &name) const
Looks up a function by a type tag.
static const MFFunctionRegistry * getStandardFunctionsRegistry(UErrorCode &status)
Get the standard function registry.
const Function * getFunction(const FunctionName &functionName) const
Looks up a function by the name of the function.
UMFBidiOption
Used to denote the directionality of the input to a function.
Definition ubidi.h:2226
@ U_MF_BIDI_OPTION_INHERIT
Directionality inherited from the message without requiring isolation of the expression value.
Definition ubidi.h:2256
UMFDirectionality
Used to represent the directionality of a message, where the AUTO setting has been resolved based on ...
Definition ubidi.h:2266
@ U_MF_DIRECTIONALITY_UNKNOWN
Denotes a message with unknown directionality.
Definition ubidi.h:2287
C API: Bidi algorithm.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
@ U_MF_FORMATTING_ERROR
Covers all runtime errors: for example, an internally inconsistent set of options.
Definition utypes.h:672
@ U_MF_SELECTOR_ERROR
A selector function is applied to an operand of the wrong type.
Definition utypes.h:675
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:822
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
Definition utypes.h:457
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:316