ICU 77.1  77.1
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
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 
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();
72  }; // class FormatterFactory
73 
81  public:
92  virtual Selector* createSelector(const Locale& locale, UErrorCode& status) const = 0;
99  virtual ~SelectorFactory();
107  }; // class SelectorFactory
108 
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);
231 
244  Builder& adoptSelector(const data_model::FunctionName& selectorName, SelectorFactory* selectorFactory, UErrorCode& errorCode);
268  Builder(UErrorCode& errorCode);
275  virtual ~Builder();
276  }; // class MFFunctionRegistry::Builder
277 
293  MFFunctionRegistry(MFFunctionRegistry&& other) { *this = std::move(other); }
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:
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
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.
Builder & adoptFormatter(const data_model::FunctionName &formatterName, FormatterFactory *formatterFactory, UErrorCode &errorCode)
Registers a formatter factory to a given formatter name.
MFFunctionRegistry build()
Creates an immutable MFFunctionRegistry object with the selectors and formatters that were previously...
Builder & adoptSelector(const data_model::FunctionName &selectorName, SelectorFactory *selectorFactory, UErrorCode &errorCode)
Registers a selector factory to a given selector name.
Builder & setDefaultFormatterNameByType(const UnicodeString &type, const data_model::FunctionName &functionName, UErrorCode &errorCode)
Registers a formatter factory to a given type tag.
Defines mappings from names of formatters and selectors to functions implementing them.
virtual ~MFFunctionRegistry()
Destructor.
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.
const SelectorFactory * getSelector(const FunctionName &selectorName) const
Looks up a selector factory by the name of the selector.
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:430
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition: utypes.h:316