messageformat2_function_registry.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. // © 2024 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. #include "unicode/utypes.h"
  4. #ifndef MESSAGEFORMAT2_FUNCTION_REGISTRY_H
  5. #define MESSAGEFORMAT2_FUNCTION_REGISTRY_H
  6. #if U_SHOW_CPLUSPLUS_API
  7. #if !UCONFIG_NO_FORMATTING
  8. #if !UCONFIG_NO_MF2
  9. #include "unicode/messageformat2_data_model_names.h"
  10. #include "unicode/messageformat2_formattable.h"
  11. #ifndef U_HIDE_DEPRECATED_API
  12. #include <map>
  13. U_NAMESPACE_BEGIN
  14. class Hashtable;
  15. class UVector;
  16. namespace message2 {
  17. using namespace data_model;
  18. /**
  19. * Interface that factory classes for creating formatters must implement.
  20. *
  21. * @internal ICU 75 technology preview
  22. * @deprecated This API is for technology preview only.
  23. */
  24. class U_I18N_API FormatterFactory : public UObject {
  25. // TODO: the coding guidelines say that interface classes
  26. // shouldn't inherit from UObject, but if I change it so these
  27. // classes don't, and the individual formatter factory classes
  28. // inherit from public FormatterFactory, public UObject, then
  29. // memory leaks ensue
  30. public:
  31. /**
  32. * Constructs a new formatter object. This method is not const;
  33. * formatter factories with local state may be defined.
  34. *
  35. * @param locale Locale to be used by the formatter.
  36. * @param status Input/output error code.
  37. * @return The new Formatter, which is non-null if U_SUCCESS(status).
  38. *
  39. * @internal ICU 75 technology preview
  40. * @deprecated This API is for technology preview only.
  41. */
  42. virtual Formatter* createFormatter(const Locale& locale, UErrorCode& status) = 0;
  43. /**
  44. * Destructor.
  45. *
  46. * @internal ICU 75 technology preview
  47. * @deprecated This API is for technology preview only.
  48. */
  49. virtual ~FormatterFactory();
  50. /**
  51. * Copy constructor.
  52. *
  53. * @internal ICU 75 technology preview
  54. * @deprecated This API is for technology preview only.
  55. */
  56. FormatterFactory& operator=(const FormatterFactory&) = delete;
  57. }; // class FormatterFactory
  58. /**
  59. * Interface that factory classes for creating selectors must implement.
  60. *
  61. * @internal ICU 75 technology preview
  62. * @deprecated This API is for technology preview only.
  63. */
  64. class U_I18N_API SelectorFactory : public UObject {
  65. public:
  66. /**
  67. * Constructs a new selector object.
  68. *
  69. * @param locale Locale to be used by the selector.
  70. * @param status Input/output error code.
  71. * @return The new selector, which is non-null if U_SUCCESS(status).
  72. *
  73. * @internal ICU 75 technology preview
  74. * @deprecated This API is for technology preview only.
  75. */
  76. virtual Selector* createSelector(const Locale& locale, UErrorCode& status) const = 0;
  77. /**
  78. * Destructor.
  79. *
  80. * @internal ICU 75 technology preview
  81. * @deprecated This API is for technology preview only.
  82. */
  83. virtual ~SelectorFactory();
  84. /**
  85. * Copy constructor.
  86. *
  87. * @internal ICU 75 technology preview
  88. * @deprecated This API is for technology preview only.
  89. */
  90. SelectorFactory& operator=(const SelectorFactory&) = delete;
  91. }; // class SelectorFactory
  92. /**
  93. * Defines mappings from names of formatters and selectors to functions implementing them.
  94. * The required set of formatter and selector functions is defined in the spec. Users can
  95. * also define custom formatter and selector functions.
  96. *
  97. * `MFFunctionRegistry` is immutable and movable. It is not copyable.
  98. *
  99. * @internal ICU 75 technology preview
  100. * @deprecated This API is for technology preview only.
  101. */
  102. class U_I18N_API MFFunctionRegistry : public UObject {
  103. private:
  104. using FormatterMap = Hashtable; // Map from stringified function names to FormatterFactory*
  105. using SelectorMap = Hashtable; // Map from stringified function names to SelectorFactory*
  106. public:
  107. /**
  108. * Looks up a formatter factory by the name of the formatter. The result is non-const,
  109. * since formatter factories may have local state. Returns the result by pointer
  110. * rather than by reference since it can fail.
  111. *
  112. * @param formatterName Name of the desired formatter.
  113. * @return A pointer to the `FormatterFactory` registered under `formatterName`, or null
  114. * if no formatter was registered under that name. The pointer is not owned
  115. * by the caller.
  116. *
  117. * @internal ICU 75 technology preview
  118. * @deprecated This API is for technology preview only.
  119. */
  120. FormatterFactory* getFormatter(const FunctionName& formatterName) const;
  121. /**
  122. * Looks up a selector factory by the name of the selector. (This returns the result by pointer
  123. * rather than by reference since `FormatterFactory` is an abstract class.)
  124. *
  125. * @param selectorName Name of the desired selector.
  126. * @return A pointer to the `SelectorFactory` registered under `selectorName`, or null
  127. * if no formatter was registered under that name.
  128. *
  129. * @internal ICU 75 technology preview
  130. * @deprecated This API is for technology preview only.
  131. */
  132. const SelectorFactory* getSelector(const FunctionName& selectorName) const;
  133. /**
  134. * Looks up a formatter factory by a type tag. This method gets the name of the default formatter registered
  135. * for that type. If no formatter was explicitly registered for this type, it returns false.
  136. *
  137. * @param formatterType Type tag for the desired `FormattableObject` type to be formatted.
  138. * @param name Output parameter; initialized to the name of the default formatter for `formatterType`
  139. * if one has been registered. Its value is undefined otherwise.
  140. * @return True if and only if the function registry contains a default formatter for `formatterType`.
  141. * If the return value is false, then the value of `name` is undefined.
  142. *
  143. * @internal ICU 75 technology preview
  144. * @deprecated This API is for technology preview only.
  145. */
  146. UBool getDefaultFormatterNameByType(const UnicodeString& formatterType, FunctionName& name) const;
  147. /**
  148. * The mutable Builder class allows each formatter and selector factory
  149. * to be initialized separately; calling its `build()` method yields an
  150. * immutable MFFunctionRegistry object.
  151. *
  152. * Builder is not copyable or movable.
  153. *
  154. * @internal ICU 75 technology preview
  155. * @deprecated This API is for technology preview only.
  156. */
  157. class U_I18N_API Builder : public UObject {
  158. private:
  159. // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
  160. FormatterMap* formatters;
  161. SelectorMap* selectors;
  162. Hashtable* formattersByType;
  163. // Do not define copy constructor/assignment operator
  164. Builder& operator=(const Builder&) = delete;
  165. Builder(const Builder&) = delete;
  166. public:
  167. /*
  168. Notes about `adoptFormatter()`'s type signature:
  169. Alternative considered: take a non-owned FormatterFactory*
  170. This is unsafe.
  171. Alternative considered: take a FormatterFactory&
  172. This requires getFormatter() to cast the reference to a pointer,
  173. as it must return an unowned FormatterFactory* since it can fail.
  174. That is also unsafe, since the caller could delete the pointer.
  175. The "TemperatureFormatter" test from the previous ICU4J version doesn't work now,
  176. as it only works if the `formatterFactory` argument is non-owned.
  177. If registering a non-owned FormatterFactory is desirable, this could
  178. be re-thought.
  179. */
  180. /**
  181. * Registers a formatter factory to a given formatter name.
  182. *
  183. * @param formatterName Name of the formatter being registered.
  184. * @param formatterFactory A pointer to a FormatterFactory object to use
  185. * for creating `formatterName` formatters. This argument is adopted.
  186. * @param errorCode Input/output error code
  187. * @return A reference to the builder.
  188. *
  189. * @internal ICU 75 technology preview
  190. * @deprecated This API is for technology preview only.
  191. */
  192. Builder& adoptFormatter(const data_model::FunctionName& formatterName, FormatterFactory* formatterFactory, UErrorCode& errorCode);
  193. /**
  194. * Registers a formatter factory to a given type tag.
  195. * (See `FormattableObject` for details on type tags.)
  196. *
  197. * @param type Tag for objects to be formatted with this formatter.
  198. * @param functionName A reference to the name of the function to use for
  199. * creating formatters for `formatterType` objects.
  200. * @param errorCode Input/output error code
  201. * @return A reference to the builder.
  202. *
  203. * @internal ICU 75 technology preview
  204. * @deprecated This API is for technology preview only.
  205. */
  206. Builder& setDefaultFormatterNameByType(const UnicodeString& type, const data_model::FunctionName& functionName, UErrorCode& errorCode);
  207. /**
  208. * Registers a selector factory to a given selector name. Adopts `selectorFactory`.
  209. *
  210. * @param selectorName Name of the selector being registered.
  211. * @param selectorFactory A SelectorFactory object to use for creating `selectorName`
  212. * selectors.
  213. * @param errorCode Input/output error code
  214. * @return A reference to the builder.
  215. *
  216. * @internal ICU 75 technology preview
  217. * @deprecated This API is for technology preview only.
  218. */
  219. Builder& adoptSelector(const data_model::FunctionName& selectorName, SelectorFactory* selectorFactory, UErrorCode& errorCode);
  220. /**
  221. * Creates an immutable `MFFunctionRegistry` object with the selectors and formatters
  222. * that were previously registered. The builder cannot be used after this call.
  223. * The `build()` method is destructive to avoid the need for a deep copy of the
  224. * `FormatterFactory` and `SelectorFactory` objects (this would be necessary because
  225. * `FormatterFactory` can have mutable state), which in turn would require implementors
  226. * of those interfaces to implement a `clone()` method.
  227. *
  228. * @return The new MFFunctionRegistry
  229. *
  230. * @internal ICU 75 technology preview
  231. * @deprecated This API is for technology preview only.
  232. */
  233. MFFunctionRegistry build();
  234. /**
  235. * Default constructor.
  236. * Returns a Builder with no functions registered.
  237. *
  238. * @param errorCode Input/output error code
  239. *
  240. * @internal ICU 75 technology preview
  241. * @deprecated This API is for technology preview only.
  242. */
  243. Builder(UErrorCode& errorCode);
  244. /**
  245. * Destructor.
  246. *
  247. * @internal ICU 75 technology preview
  248. * @deprecated This API is for technology preview only.
  249. */
  250. virtual ~Builder();
  251. }; // class MFFunctionRegistry::Builder
  252. /**
  253. * Move assignment operator:
  254. * The source MFFunctionRegistry will be left in a valid but undefined state.
  255. *
  256. * @internal ICU 75 technology preview
  257. * @deprecated This API is for technology preview only.
  258. */
  259. MFFunctionRegistry& operator=(MFFunctionRegistry&&) noexcept;
  260. /**
  261. * Move constructor:
  262. * The source MFFunctionRegistry will be left in a valid but undefined state.
  263. *
  264. * @internal ICU 75 technology preview
  265. * @deprecated This API is for technology preview only.
  266. */
  267. MFFunctionRegistry(MFFunctionRegistry&& other) { *this = std::move(other); }
  268. /**
  269. * Destructor.
  270. *
  271. * @internal ICU 75 technology preview
  272. * @deprecated This API is for technology preview only.
  273. */
  274. virtual ~MFFunctionRegistry();
  275. private:
  276. friend class MessageContext;
  277. friend class MessageFormatter;
  278. // Do not define copy constructor or copy assignment operator
  279. MFFunctionRegistry& operator=(const MFFunctionRegistry&) = delete;
  280. MFFunctionRegistry(const MFFunctionRegistry&) = delete;
  281. MFFunctionRegistry(FormatterMap* f, SelectorMap* s, Hashtable* byType);
  282. MFFunctionRegistry() {}
  283. // Debugging; should only be called on a function registry with
  284. // all the standard functions registered
  285. void checkFormatter(const char*) const;
  286. void checkSelector(const char*) const;
  287. void checkStandard() const;
  288. bool hasFormatter(const data_model::FunctionName& f) const;
  289. bool hasSelector(const data_model::FunctionName& s) const;
  290. void cleanup() noexcept;
  291. // Must use raw pointers to avoid instantiating `LocalPointer` on an internal type
  292. FormatterMap* formatters = nullptr;
  293. SelectorMap* selectors = nullptr;
  294. // Mapping from strings (type tags) to FunctionNames
  295. Hashtable* formattersByType = nullptr;
  296. }; // class MFFunctionRegistry
  297. /**
  298. * Interface that formatter classes must implement.
  299. *
  300. * @internal ICU 75 technology preview
  301. * @deprecated This API is for technology preview only.
  302. */
  303. class U_I18N_API Formatter : public UObject {
  304. public:
  305. /**
  306. * Formats the input passed in `context` by setting an output using one of the
  307. * `FormattingContext` methods or indicating an error.
  308. *
  309. * @param toFormat Placeholder, including a source formattable value and possibly
  310. * the output of a previous formatter applied to it; see
  311. * `message2::FormattedPlaceholder` for details. Passed by move.
  312. * @param options The named function options. Passed by move
  313. * @param status Input/output error code. Should not be set directly by the
  314. * custom formatter, which should use `FormattingContext::setFormattingWarning()`
  315. * to signal errors. The custom formatter may pass `status` to other ICU functions
  316. * that can signal errors using this mechanism.
  317. *
  318. * @return The formatted value.
  319. *
  320. * @internal ICU 75 technology preview
  321. * @deprecated This API is for technology preview only.
  322. */
  323. virtual FormattedPlaceholder format(FormattedPlaceholder&& toFormat,
  324. FunctionOptions&& options,
  325. UErrorCode& status) const = 0;
  326. /**
  327. * Destructor.
  328. *
  329. * @internal ICU 75 technology preview
  330. * @deprecated This API is for technology preview only.
  331. */
  332. virtual ~Formatter();
  333. }; // class Formatter
  334. /**
  335. * Interface that selector classes must implement.
  336. *
  337. * @internal ICU 75 technology preview
  338. * @deprecated This API is for technology preview only.
  339. */
  340. class U_I18N_API Selector : public UObject {
  341. public:
  342. /**
  343. * Compares the input to an array of keys, and returns an array of matching
  344. * keys sorted by preference.
  345. *
  346. * @param toFormat The unnamed function argument; passed by move.
  347. * @param options A reference to the named function options.
  348. * @param keys An array of strings that are compared to the input
  349. * (`context.getFormattableInput()`) in an implementation-specific way.
  350. * @param keysLen The length of `keys`.
  351. * @param prefs An array of strings with length `keysLen`. The contents of
  352. * the array is undefined. `selectKey()` should set the contents
  353. * of `prefs` to a subset of `keys`, with the best match placed at the lowest index.
  354. * @param prefsLen A reference that `selectKey()` should set to the length of `prefs`,
  355. * which must be less than or equal to `keysLen`.
  356. * @param status Input/output error code. Should not be set directly by the
  357. * custom selector, which should use `FormattingContext::setSelectorError()`
  358. * to signal errors. The custom selector may pass `status` to other ICU functions
  359. * that can signal errors using this mechanism.
  360. *
  361. * @internal ICU 75 technology preview
  362. * @deprecated This API is for technology preview only.
  363. */
  364. virtual void selectKey(FormattedPlaceholder&& toFormat,
  365. FunctionOptions&& options,
  366. const UnicodeString* keys,
  367. int32_t keysLen,
  368. UnicodeString* prefs,
  369. int32_t& prefsLen,
  370. UErrorCode& status) const = 0;
  371. // Note: This takes array arguments because the internal MessageFormat code has to
  372. // call this method, and can't include any code that constructs std::vectors.
  373. /**
  374. * Destructor.
  375. *
  376. * @internal ICU 75 technology preview
  377. * @deprecated This API is for technology preview only.
  378. */
  379. virtual ~Selector();
  380. }; // class Selector
  381. } // namespace message2
  382. U_NAMESPACE_END
  383. #endif // U_HIDE_DEPRECATED_API
  384. #endif /* #if !UCONFIG_NO_MF2 */
  385. #endif /* #if !UCONFIG_NO_FORMATTING */
  386. #endif /* U_SHOW_CPLUSPLUS_API */
  387. #endif // MESSAGEFORMAT2_FUNCTION_REGISTRY_H
  388. // eof