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

ICU 76.1 76.1

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_FORMATTING
12
13 #if !UCONFIG_NO_MF2
14
15 #include "unicode/messageformat2_data_model_names.h"
16 #include "unicode/messageformat2_formattable.h"
17
18 #ifndef U_HIDE_DEPRECATED_API
19
20 #include <map>
21
22 U_NAMESPACE_BEGIN
23
24 class Hashtable;
25 class UVector;
26
27 namespace message2 {
28
29 using namespace data_model;
30
37 class U_I18N_API FormatterFactory : public UObject {
38 // TODO: the coding guidelines say that interface classes
39 // shouldn't inherit from UObject, but if I change it so these
40 // classes don't, and the individual formatter factory classes
41 // inherit from public FormatterFactory, public UObject, then
42 // memory leaks ensue
43 public:
55 virtual Formatter* createFormatter(const Locale& locale, UErrorCode& status) = 0;
62 virtual ~FormatterFactory();
69 FormatterFactory& operator=(const FormatterFactory&) = delete;
70 }; // class FormatterFactory
71
78 class U_I18N_API SelectorFactory : public UObject {
79 public:
90 virtual Selector* createSelector(const Locale& locale, UErrorCode& status) const = 0;
97 virtual ~SelectorFactory();
104 SelectorFactory& operator=(const SelectorFactory&) = delete;
105 }; // class SelectorFactory
106
117 class U_I18N_API MFFunctionRegistry : public UObject {
118 private:
119
120 using FormatterMap = Hashtable; // Map from stringified function names to FormatterFactory*
121 using SelectorMap = Hashtable; // Map from stringified function names to SelectorFactory*
122
123 public:
137 FormatterFactory* getFormatter(const FunctionName& formatterName) const;
149 const SelectorFactory* getSelector(const FunctionName& selectorName) const;
163 UBool getDefaultFormatterNameByType(const UnicodeString& formatterType, FunctionName& name) const;
174 class U_I18N_API Builder : public UObject {
175 private:
176 // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
177 FormatterMap* formatters;
178 SelectorMap* selectors;
179 Hashtable* formattersByType;
180
181 // Do not define copy constructor/assignment operator
182 Builder& operator=(const Builder&) = delete;
183 Builder(const Builder&) = delete;
184
185 public:
186 /*
187 Notes about `adoptFormatter()`'s type signature:
188
189 Alternative considered: take a non-owned FormatterFactory*
190 This is unsafe.
191
192 Alternative considered: take a FormatterFactory&
193 This requires getFormatter() to cast the reference to a pointer,
194 as it must return an unowned FormatterFactory* since it can fail.
195 That is also unsafe, since the caller could delete the pointer.
196
197 The "TemperatureFormatter" test from the previous ICU4J version doesn't work now,
198 as it only works if the `formatterFactory` argument is non-owned.
199 If registering a non-owned FormatterFactory is desirable, this could
200 be re-thought.
201 */
214 Builder& adoptFormatter(const data_model::FunctionName& formatterName, FormatterFactory* formatterFactory, UErrorCode& errorCode);
228 Builder& setDefaultFormatterNameByType(const UnicodeString& type, const data_model::FunctionName& functionName, UErrorCode& errorCode);
229
242 Builder& adoptSelector(const data_model::FunctionName& selectorName, SelectorFactory* selectorFactory, UErrorCode& errorCode);
256 MFFunctionRegistry build();
266 Builder(UErrorCode& errorCode);
273 virtual ~Builder();
274 }; // class MFFunctionRegistry::Builder
275
283 MFFunctionRegistry& operator=(MFFunctionRegistry&&) noexcept;
291 MFFunctionRegistry(MFFunctionRegistry&& other) { *this = std::move(other); }
298 virtual ~MFFunctionRegistry();
299
300 private:
301 friend class MessageContext;
302 friend class MessageFormatter;
303
304 // Do not define copy constructor or copy assignment operator
305 MFFunctionRegistry& operator=(const MFFunctionRegistry&) = delete;
306 MFFunctionRegistry(const MFFunctionRegistry&) = delete;
307
308 MFFunctionRegistry(FormatterMap* f, SelectorMap* s, Hashtable* byType);
309
310 MFFunctionRegistry() {}
311
312 // Debugging; should only be called on a function registry with
313 // all the standard functions registered
314 void checkFormatter(const char*) const;
315 void checkSelector(const char*) const;
316 void checkStandard() const;
317
318 bool hasFormatter(const data_model::FunctionName& f) const;
319 bool hasSelector(const data_model::FunctionName& s) const;
320 void cleanup() noexcept;
321
322 // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
323 FormatterMap* formatters = nullptr;
324 SelectorMap* selectors = nullptr;
325 // Mapping from strings (type tags) to FunctionNames
326 Hashtable* formattersByType = nullptr;
327 }; // class MFFunctionRegistry
328
335 class U_I18N_API Formatter : public UObject {
336 public:
355 virtual FormattedPlaceholder format(FormattedPlaceholder&& toFormat,
356 FunctionOptions&& options,
357 UErrorCode& status) const = 0;
364 virtual ~Formatter();
365 }; // class Formatter
366
373 class U_I18N_API Selector : public UObject {
374 public:
397 virtual void selectKey(FormattedPlaceholder&& toFormat,
398 FunctionOptions&& options,
399 const UnicodeString* keys,
400 int32_t keysLen,
401 UnicodeString* prefs,
402 int32_t& prefsLen,
403 UErrorCode& status) const = 0;
404 // Note: This takes array arguments because the internal MessageFormat code has to
405 // call this method, and can't include any code that constructs std::vectors.
412 virtual ~Selector();
413 }; // class Selector
414
415 } // namespace message2
416
417 U_NAMESPACE_END
418
419 #endif // U_HIDE_DEPRECATED_API
420
421 #endif /* #if !UCONFIG_NO_MF2 */
422
423 #endif /* #if !UCONFIG_NO_FORMATTING */
424
425 #endif /* U_SHOW_CPLUSPLUS_API */
426
427 #endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_H
428
429 // eof
icu::Locale
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:296
icu::message2::FormattedPlaceholder
A FormattablePlaceholder encapsulates an input value (a message2::Formattable) together with an optio...
Definition: messageformat2_formattable.h:686
icu::message2::FormatterFactory
Interface that factory classes for creating formatters must implement.
Definition: messageformat2_function_registry.h:37
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:335
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:488
icu::message2::MFFunctionRegistry::Builder
The mutable Builder class allows each formatter and selector factory to be initialized separately; ca...
Definition: messageformat2_function_registry.h:174
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:117
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:51
icu::message2::SelectorFactory
Interface that factory classes for creating selectors must implement.
Definition: messageformat2_function_registry.h:78
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:373
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:247
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:430
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