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 
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 
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 
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
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:430
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:295
Defines mappings from names of formatters and selectors to functions implementing them...
UObject is the common ICU "boilerplate" class.
Definition: uobject.h:223
Interface that selector classes must implement.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition: umachine.h:247
A Locale object represents a specific geographical, political, or cultural region.
Definition: locid.h:195