ICU 75.1 75.1
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_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
22U_NAMESPACE_BEGIN
23
24class Hashtable;
25class UVector;
26
27namespace message2 {
28
29 using namespace data_model;
30
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;
70 }; // class FormatterFactory
71
79 public:
90 virtual Selector* createSelector(const Locale& locale, UErrorCode& status) const = 0;
105 }; // class SelectorFactory
106
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:
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 */
229
266 Builder(UErrorCode& errorCode);
273 virtual ~Builder();
274 }; // class MFFunctionRegistry::Builder
275
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
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
336 public:
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,
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
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
"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
A FormattablePlaceholder encapsulates an input value (a message2::Formattable) together with an optio...
Interface that factory classes for creating formatters must implement.
virtual Formatter * createFormatter(const Locale &locale, UErrorCode &status)=0
Constructs a new formatter object.
FormatterFactory & operator=(const FormatterFactory &)=delete
Copy constructor.
virtual ~FormatterFactory()
Destructor.
Interface that formatter classes must implement.
virtual ~Formatter()
Destructor.
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...
Structure encapsulating named options passed to a custom selector or formatter.
The mutable Builder class allows each formatter and selector factory to be initialized separately; ca...
Builder(UErrorCode &errorCode)
Default constructor.
MFFunctionRegistry build()
Creates an immutable MFFunctionRegistry object with the selectors and formatters that were previously...
Builder & adoptFormatter(const data_model::FunctionName &formatterName, FormatterFactory *formatterFactory, UErrorCode &errorCode)
Registers a formatter factory to a given formatter name.
Builder & setDefaultFormatterNameByType(const UnicodeString &type, const data_model::FunctionName &functionName, UErrorCode &errorCode)
Registers a formatter factory to a given type tag.
Builder & adoptSelector(const data_model::FunctionName &selectorName, SelectorFactory *selectorFactory, UErrorCode &errorCode)
Registers a selector factory to a given selector name.
Defines mappings from names of formatters and selectors to functions implementing them.
virtual ~MFFunctionRegistry()
Destructor.
const SelectorFactory * getSelector(const FunctionName &selectorName) const
Looks up a selector factory by the name of the selector.
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 formatter factory by a type tag.
FormatterFactory * getFormatter(const FunctionName &formatterName) const
Looks up a formatter factory by the name of the formatter.
Interface that factory classes for creating selectors must implement.
virtual Selector * createSelector(const Locale &locale, UErrorCode &status) const =0
Constructs a new selector object.
virtual ~SelectorFactory()
Destructor.
SelectorFactory & operator=(const SelectorFactory &)=delete
Copy constructor.
Interface that selector classes must implement.
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.
virtual ~Selector()
Destructor.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:247
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:415
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:301