messageformat2_function_registry.h
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include "unicode/utypes.h"
#ifndef MESSAGEFORMAT2_FUNCTION_REGISTRY_H
#define MESSAGEFORMAT2_FUNCTION_REGISTRY_H
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_MF2
#include "unicode/messageformat2_data_model_names.h"
#include "unicode/messageformat2_formattable.h"
#ifndef U_HIDE_DEPRECATED_API
#include <map>
U_NAMESPACE_BEGIN
class Hashtable;
class UVector;
namespace message2 {
using namespace data_model;
class U_I18N_API FormatterFactory : public UObject {
// TODO: the coding guidelines say that interface classes
// shouldn't inherit from UObject, but if I change it so these
// classes don't, and the individual formatter factory classes
// inherit from public FormatterFactory, public UObject, then
// memory leaks ensue
public:
virtual Formatter* createFormatter(const Locale& locale, UErrorCode& status) = 0;
virtual ~FormatterFactory();
FormatterFactory& operator=(const FormatterFactory&) = delete;
}; // class FormatterFactory
class U_I18N_API SelectorFactory : public UObject {
public:
virtual Selector* createSelector(const Locale& locale, UErrorCode& status) const = 0;
virtual ~SelectorFactory();
SelectorFactory& operator=(const SelectorFactory&) = delete;
}; // class SelectorFactory
class U_I18N_API MFFunctionRegistry : public UObject {
private:
using FormatterMap = Hashtable; // Map from stringified function names to FormatterFactory*
using SelectorMap = Hashtable; // Map from stringified function names to SelectorFactory*
public:
FormatterFactory* getFormatter(const FunctionName& formatterName) const;
const SelectorFactory* getSelector(const FunctionName& selectorName) const;
UBool getDefaultFormatterNameByType(const UnicodeString& formatterType, FunctionName& name) const;
class U_I18N_API Builder : public UObject {
private:
// Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
FormatterMap* formatters;
SelectorMap* selectors;
Hashtable* formattersByType;
// Do not define copy constructor/assignment operator
Builder& operator=(const Builder&) = delete;
Builder(const Builder&) = delete;
public:
/*
Notes about `adoptFormatter()`'s type signature:
Alternative considered: take a non-owned FormatterFactory*
This is unsafe.
Alternative considered: take a FormatterFactory&
This requires getFormatter() to cast the reference to a pointer,
as it must return an unowned FormatterFactory* since it can fail.
That is also unsafe, since the caller could delete the pointer.
The "TemperatureFormatter" test from the previous ICU4J version doesn't work now,
as it only works if the `formatterFactory` argument is non-owned.
If registering a non-owned FormatterFactory is desirable, this could
be re-thought.
*/
Builder& adoptFormatter(const data_model::FunctionName& formatterName, FormatterFactory* formatterFactory, UErrorCode& errorCode);
Builder& setDefaultFormatterNameByType(const UnicodeString& type, const data_model::FunctionName& functionName, UErrorCode& errorCode);
Builder& adoptSelector(const data_model::FunctionName& selectorName, SelectorFactory* selectorFactory, UErrorCode& errorCode);
MFFunctionRegistry build();
Builder(UErrorCode& errorCode);
virtual ~Builder();
}; // class MFFunctionRegistry::Builder
MFFunctionRegistry& operator=(MFFunctionRegistry&&) noexcept;
MFFunctionRegistry(MFFunctionRegistry&& other) { *this = std::move(other); }
virtual ~MFFunctionRegistry();
private:
friend class MessageContext;
friend class MessageFormatter;
// Do not define copy constructor or copy assignment operator
MFFunctionRegistry& operator=(const MFFunctionRegistry&) = delete;
MFFunctionRegistry(const MFFunctionRegistry&) = delete;
MFFunctionRegistry(FormatterMap* f, SelectorMap* s, Hashtable* byType);
MFFunctionRegistry() {}
// Debugging; should only be called on a function registry with
// all the standard functions registered
void checkFormatter(const char*) const;
void checkSelector(const char*) const;
void checkStandard() const;
bool hasFormatter(const data_model::FunctionName& f) const;
bool hasSelector(const data_model::FunctionName& s) const;
void cleanup() noexcept;
// Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
FormatterMap* formatters = nullptr;
SelectorMap* selectors = nullptr;
// Mapping from strings (type tags) to FunctionNames
Hashtable* formattersByType = nullptr;
}; // class MFFunctionRegistry
class U_I18N_API Formatter : public UObject {
public:
virtual FormattedPlaceholder format(FormattedPlaceholder&& toFormat,
FunctionOptions&& options,
UErrorCode& status) const = 0;
virtual ~Formatter();
}; // class Formatter
class U_I18N_API Selector : public UObject {
public:
virtual void selectKey(FormattedPlaceholder&& toFormat,
FunctionOptions&& options,
const UnicodeString* keys,
int32_t keysLen,
UnicodeString* prefs,
int32_t& prefsLen,
UErrorCode& status) const = 0;
// Note: This takes array arguments because the internal MessageFormat code has to
// call this method, and can't include any code that constructs std::vectors.
virtual ~Selector();
}; // class Selector
} // namespace message2
U_NAMESPACE_END
#endif // U_HIDE_DEPRECATED_API
#endif /* #if !UCONFIG_NO_MF2 */
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_H
// 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:415
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:301