ICU 78.3  78.3
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();
71  FormatterFactory& operator=(const FormatterFactory&) = delete;
72  }; // class FormatterFactory
73 
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 
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
The mutable Builder class allows each formatter and selector factory to be initialized separately; ca...
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside...
Definition: utypes.h:316
Interface that factory classes for creating formatters must implement.
Interface that formatter classes must implement.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition: utypes.h:509
Structure encapsulating named options passed to a custom selector or formatter.
A FormattablePlaceholder encapsulates an input value (a message2::Formattable) together with an optio...
Basic definitions for ICU, for both C and C++ APIs.
Interface that factory classes for creating selectors must implement.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition: unistr.h:302
Defines mappings from names of formatters and selectors to functions implementing them...
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:222
Interface that selector classes must implement.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:269
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:198