ICU 78.2: i18n/unicode/messageformat2_function_registry.h Source File

ICU 78.2 78.2

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
20 #ifndef U_HIDE_DEPRECATED_API
21
22 #include <map>
23
24 U_NAMESPACE_BEGIN
25
26 class Hashtable;
27 class UVector;
28
29 namespace message2 {
30
31 using namespace data_model;
32
39 class U_I18N_API FormatterFactory : public UObject {
40 // TODO: the coding guidelines say that interface classes
41 // shouldn't inherit from UObject, but if I change it so these
42 // classes don't, and the individual formatter factory classes
43 // inherit from public FormatterFactory, public UObject, then
44 // memory leaks ensue
45 public:
57 virtual Formatter* createFormatter(const Locale& locale, UErrorCode& status) = 0;
64 virtual ~FormatterFactory();
71 FormatterFactory& operator=(const FormatterFactory&) = delete;
72 }; // class FormatterFactory
73
80 class U_I18N_API SelectorFactory : public UObject {
81 public:
92 virtual Selector* createSelector(const Locale& locale, UErrorCode& status) const = 0;
99 virtual ~SelectorFactory();
106 SelectorFactory& operator=(const SelectorFactory&) = delete;
107 }; // class SelectorFactory
108
119 class U_I18N_API MFFunctionRegistry : public UObject {
120 private:
121
122 using FormatterMap = Hashtable; // Map from stringified function names to FormatterFactory*
123 using SelectorMap = Hashtable; // Map from stringified function names to SelectorFactory*
124
125 public:
139 FormatterFactory* getFormatter(const FunctionName& formatterName) const;
151 const SelectorFactory* getSelector(const FunctionName& selectorName) const;
165 UBool getDefaultFormatterNameByType(const UnicodeString& formatterType, FunctionName& name) const;
176 class U_I18N_API Builder : public UObject {
177 private:
178 // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
179 FormatterMap* formatters;
180 SelectorMap* selectors;
181 Hashtable* formattersByType;
182
183 // Do not define copy constructor/assignment operator
184 Builder& operator=(const Builder&) = delete;
185 Builder(const Builder&) = delete;
186
187 public:
188 /*
189 Notes about `adoptFormatter()`'s type signature:
190
191 Alternative considered: take a non-owned FormatterFactory*
192 This is unsafe.
193
194 Alternative considered: take a FormatterFactory&
195 This requires getFormatter() to cast the reference to a pointer,
196 as it must return an unowned FormatterFactory* since it can fail.
197 That is also unsafe, since the caller could delete the pointer.
198
199 The "TemperatureFormatter" test from the previous ICU4J version doesn't work now,
200 as it only works if the `formatterFactory` argument is non-owned.
201 If registering a non-owned FormatterFactory is desirable, this could
202 be re-thought.
203 */
216 Builder& adoptFormatter(const data_model::FunctionName& formatterName, FormatterFactory* formatterFactory, UErrorCode& errorCode);
230 Builder& setDefaultFormatterNameByType(const UnicodeString& type, const data_model::FunctionName& functionName, UErrorCode& errorCode);
231
244 Builder& adoptSelector(const data_model::FunctionName& selectorName, SelectorFactory* selectorFactory, UErrorCode& errorCode);
258 MFFunctionRegistry build();
268 Builder(UErrorCode& errorCode);
275 virtual ~Builder();
276 }; // class MFFunctionRegistry::Builder
277
285 MFFunctionRegistry& operator=(MFFunctionRegistry&&) noexcept;
293 MFFunctionRegistry(MFFunctionRegistry&& other) { *this = std::move(other); }
300 virtual ~MFFunctionRegistry();
301
302 private:
303 friend class MessageContext;
304 friend class MessageFormatter;
305
306 // Do not define copy constructor or copy assignment operator
307 MFFunctionRegistry& operator=(const MFFunctionRegistry&) = delete;
308 MFFunctionRegistry(const MFFunctionRegistry&) = delete;
309
310 MFFunctionRegistry(FormatterMap* f, SelectorMap* s, Hashtable* byType);
311
312 MFFunctionRegistry() {}
313
314 // Debugging; should only be called on a function registry with
315 // all the standard functions registered
316 void checkFormatter(const char*) const;
317 void checkSelector(const char*) const;
318 void checkStandard() const;
319
320 bool hasFormatter(const data_model::FunctionName& f) const;
321 bool hasSelector(const data_model::FunctionName& s) const;
322 void cleanup() noexcept;
323
324 // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
325 FormatterMap* formatters = nullptr;
326 SelectorMap* selectors = nullptr;
327 // Mapping from strings (type tags) to FunctionNames
328 Hashtable* formattersByType = nullptr;
329 }; // class MFFunctionRegistry
330
337 class U_I18N_API Formatter : public UObject {
338 public:
357 virtual FormattedPlaceholder format(FormattedPlaceholder&& toFormat,
358 FunctionOptions&& options,
359 UErrorCode& status) const = 0;
366 virtual ~Formatter();
367 }; // class Formatter
368
375 class U_I18N_API Selector : public UObject {
376 public:
399 virtual void selectKey(FormattedPlaceholder&& toFormat,
400 FunctionOptions&& options,
401 const UnicodeString* keys,
402 int32_t keysLen,
403 UnicodeString* prefs,
404 int32_t& prefsLen,
405 UErrorCode& status) const = 0;
406 // Note: This takes array arguments because the internal MessageFormat code has to
407 // call this method, and can't include any code that constructs std::vectors.
414 virtual ~Selector();
415 }; // class Selector
416
417 } // namespace message2
418
419 U_NAMESPACE_END
420
421 #endif // U_HIDE_DEPRECATED_API
422
423 #endif /* #if !UCONFIG_NO_MF2 */
424
425 #endif /* #if !UCONFIG_NO_FORMATTING */
426
427 #endif /* #if !UCONFIG_NO_NORMALIZATION */
428
429 #endif /* U_SHOW_CPLUSPLUS_API */
430
431 #endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_H
432
433 // eof
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:222
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:303
icu::message2::FormattedPlaceholder
A FormattablePlaceholder encapsulates an input value (a message2::Formattable) together with an optio...
Definition: messageformat2_formattable.h:691
icu::message2::FormatterFactory
Interface that factory classes for creating formatters must implement.
Definition: messageformat2_function_registry.h:39
icu::message2::FormatterFactory::createFormatter
virtual Formatter * createFormatter(const Locale &locale, UErrorCode &status)=0
Constructs a new formatter object.
icu::message2::FormatterFactory::operator=
FormatterFactory & operator=(const FormatterFactory &)=delete
Copy constructor.
icu::message2::FormatterFactory::~FormatterFactory
virtual ~FormatterFactory()
Destructor.
icu::message2::Formatter
Interface that formatter classes must implement.
Definition: messageformat2_function_registry.h:337
icu::message2::Formatter::~Formatter
virtual ~Formatter()
Destructor.
icu::message2::Formatter::format
virtual FormattedPlaceholder format(FormattedPlaceholder &&toFormat, FunctionOptions &&options, UErrorCode &status) const =0
Formats the input passed in context by setting an output using one of the FormattingContext methods o...
icu::message2::FunctionOptions
Structure encapsulating named options passed to a custom selector or formatter.
Definition: messageformat2_formattable.h:490
icu::message2::MFFunctionRegistry::Builder
The mutable Builder class allows each formatter and selector factory to be initialized separately; ca...
Definition: messageformat2_function_registry.h:176
icu::message2::MFFunctionRegistry::Builder::Builder
Builder(UErrorCode &errorCode)
Default constructor.
icu::message2::MFFunctionRegistry::Builder::adoptFormatter
Builder & adoptFormatter(const data_model::FunctionName &formatterName, FormatterFactory *formatterFactory, UErrorCode &errorCode)
Registers a formatter factory to a given formatter name.
icu::message2::MFFunctionRegistry::Builder::build
MFFunctionRegistry build()
Creates an immutable MFFunctionRegistry object with the selectors and formatters that were previously...
icu::message2::MFFunctionRegistry::Builder::adoptSelector
Builder & adoptSelector(const data_model::FunctionName &selectorName, SelectorFactory *selectorFactory, UErrorCode &errorCode)
Registers a selector factory to a given selector name.
icu::message2::MFFunctionRegistry::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::MFFunctionRegistry::Builder::setDefaultFormatterNameByType
Builder & setDefaultFormatterNameByType(const UnicodeString &type, const data_model::FunctionName &functionName, UErrorCode &errorCode)
Registers a formatter factory to a given type tag.
icu::message2::MFFunctionRegistry
Defines mappings from names of formatters and selectors to functions implementing them.
Definition: messageformat2_function_registry.h:119
icu::message2::MFFunctionRegistry::~MFFunctionRegistry
virtual ~MFFunctionRegistry()
Destructor.
icu::message2::MFFunctionRegistry::operator=
MFFunctionRegistry & operator=(MFFunctionRegistry &&) noexcept
Move assignment operator: The source MFFunctionRegistry will be left in a valid but undefined state.
icu::message2::MFFunctionRegistry::getDefaultFormatterNameByType
UBool getDefaultFormatterNameByType(const UnicodeString &formatterType, FunctionName &name) const
Looks up a formatter factory by a type tag.
icu::message2::MFFunctionRegistry::getSelector
const SelectorFactory * getSelector(const FunctionName &selectorName) const
Looks up a selector factory by the name of the selector.
icu::message2::MFFunctionRegistry::getFormatter
FormatterFactory * getFormatter(const FunctionName &formatterName) const
Looks up a formatter factory by the name of the formatter.
icu::message2::MessageFormatter
Definition: messageformat2.h:54
icu::message2::SelectorFactory
Interface that factory classes for creating selectors must implement.
Definition: messageformat2_function_registry.h:80
icu::message2::SelectorFactory::createSelector
virtual Selector * createSelector(const Locale &locale, UErrorCode &status) const =0
Constructs a new selector object.
icu::message2::SelectorFactory::~SelectorFactory
virtual ~SelectorFactory()
Destructor.
icu::message2::SelectorFactory::operator=
SelectorFactory & operator=(const SelectorFactory &)=delete
Copy constructor.
icu::message2::Selector
Interface that selector classes must implement.
Definition: messageformat2_function_registry.h:375
icu::message2::Selector::selectKey
virtual void selectKey(FormattedPlaceholder &&toFormat, FunctionOptions &&options, const UnicodeString *keys, int32_t keysLen, UnicodeString *prefs, int32_t &prefsLen, UErrorCode &status) const =0
Compares the input to an array of keys, and returns an array of matching keys sorted by preference.
icu::message2::Selector::~Selector
virtual ~Selector()
Destructor.
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316