| 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_FORMATTABLE_H |
| 7 | #define MESSAGEFORMAT2_FORMATTABLE_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/chariter.h" |
| 18 | #include "unicode/numberformatter.h" |
| 19 | #include "unicode/messageformat2_data_model_names.h" |
| 20 | |
| 21 | #ifndef U_HIDE_DEPRECATED_API |
| 22 | |
| 23 | #include <map> |
| 24 | #include <variant> |
| 25 | |
| 26 | U_NAMESPACE_BEGIN |
| 27 | |
| 28 | class Hashtable; |
| 29 | class UVector; |
| 30 | |
| 31 | namespace message2 { |
| 32 | |
| 33 | class Formatter; |
| 34 | class MessageContext; |
| 35 | class Selector; |
| 36 | |
| 37 | // Formattable |
| 38 | // ---------- |
| 39 | |
| 40 | /** |
| 41 | * `FormattableObject` is an abstract class that can be implemented in order to define |
| 42 | * an arbitrary class that can be passed to a custom formatter or selector function. |
| 43 | * To be passed in such a way, it must be wrapped in a `Formattable` object. |
| 44 | * |
| 45 | * @internal ICU 75 technology preview |
| 46 | * @deprecated This API is for technology preview only. |
| 47 | */ |
| 48 | class U_I18N_API FormattableObject : public UObject { |
| 49 | public: |
| 50 | /** |
| 51 | * Returns an arbitrary string representing the type of this object. |
| 52 | * It's up to the implementor of this class, as well as the implementors |
| 53 | * of any custom functions that rely on particular values of this tag |
| 54 | * corresponding to particular classes that the object contents can be |
| 55 | * downcast to, to ensure that the type tags are used soundly. |
| 56 | * @internal ICU 75 technology preview |
| 57 | * @deprecated This API is for technology preview only. |
| 58 | */ |
| 59 | virtual const UnicodeString& tag() const = 0; |
| 60 | /** |
| 61 | * Destructor. |
| 62 | * |
| 63 | * @internal ICU 75 technology preview |
| 64 | * @deprecated This API is for technology preview only. |
| 65 | */ |
| 66 | virtual ~FormattableObject(); |
| 67 | }; // class FormattableObject |
| 68 | |
| 69 | class Formattable; |
| 70 | } // namespace message2 |
| 71 | |
| 72 | U_NAMESPACE_END |
| 73 | |
| 74 | /// @cond DOXYGEN_IGNORE |
| 75 | // Export an explicit template instantiation of the std::variant that is used |
| 76 | // to represent the message2::Formattable class. |
| 77 | // (When building DLLs for Windows this is required.) |
| 78 | // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others |
| 79 | // for similar examples.) |
| 80 | #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN |
| 81 | #if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION) |
| 82 | template class U_I18N_API std::_Variant_storage_<false, |
| 83 | double, |
| 84 | int64_t, |
| 85 | icu::UnicodeString, |
| 86 | icu::Formattable, |
| 87 | const icu::message2::FormattableObject *, |
| 88 | std::pair<const icu::message2::Formattable *,int32_t>>; |
| 89 | #endif |
| 90 | typedef std::pair<const icu::message2::Formattable*, int32_t> P; |
| 91 | template class U_I18N_API std::variant<double, |
| 92 | int64_t, |
| 93 | icu::UnicodeString, |
| 94 | icu::Formattable, |
| 95 | const icu::message2::FormattableObject*, |
| 96 | P>; |
| 97 | #endif |
| 98 | /// @endcond |
| 99 | |
| 100 | U_NAMESPACE_BEGIN |
| 101 | |
| 102 | namespace message2 { |
| 103 | /** |
| 104 | * The `Formattable` class represents a typed value that can be formatted, |
| 105 | * originating either from a message argument or a literal in the code. |
| 106 | * ICU's Formattable class is not used in MessageFormat 2 because it's unsafe to copy an |
| 107 | * icu::Formattable value that contains an object. (See ICU-20275). |
| 108 | * |
| 109 | * `Formattable` is immutable (not deeply immutable) and |
| 110 | * is movable and copyable. |
| 111 | * (Copying does not do a deep copy when the wrapped value is an array or |
| 112 | * object. Likewise, while a pointer to a wrapped array or object is `const`, |
| 113 | * the referents of the pointers may be mutated by other code.) |
| 114 | * |
| 115 | * @internal ICU 75 technology preview |
| 116 | * @deprecated This API is for technology preview only. |
| 117 | */ |
| 118 | class U_I18N_API Formattable : public UObject { |
| 119 | public: |
| 120 | |
| 121 | /** |
| 122 | * Gets the data type of this Formattable object. |
| 123 | * @return the data type of this Formattable object. |
| 124 | * @internal ICU 75 technology preview |
| 125 | * @deprecated This API is for technology preview only. |
| 126 | */ |
| 127 | UFormattableType getType() const; |
| 128 | |
| 129 | /** |
| 130 | * Gets the double value of this object. If this object is not of type |
| 131 | * UFMT_DOUBLE, then the result is undefined and the error code is set. |
| 132 | * |
| 133 | * @param status Input/output error code. |
| 134 | * @return the double value of this object. |
| 135 | * @internal ICU 75 technology preview |
| 136 | * @deprecated This API is for technology preview only. |
| 137 | */ |
| 138 | double getDouble(UErrorCode& status) const { |
| 139 | if (U_SUCCESS(status)) { |
| 140 | if (isDecimal() && getType() == UFMT_DOUBLE) { |
| 141 | return (std::get_if<icu::Formattable>(&contents))->getDouble(); |
| 142 | } |
| 143 | if (std::holds_alternative<double>(contents)) { |
| 144 | return *(std::get_if<double>(&contents)); |
| 145 | } |
| 146 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 147 | } |
| 148 | return 0; |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Gets the long value of this object. If this object is not of type |
| 153 | * UFMT_LONG then the result is undefined and the error code is set. |
| 154 | * |
| 155 | * @param status Input/output error code. |
| 156 | * @return the long value of this object. |
| 157 | * @internal ICU 75 technology preview |
| 158 | * @deprecated This API is for technology preview only. |
| 159 | */ |
| 160 | int32_t getLong(UErrorCode& status) const { |
| 161 | if (U_SUCCESS(status)) { |
| 162 | if (isDecimal() && getType() == UFMT_LONG) { |
| 163 | return std::get_if<icu::Formattable>(&contents)->getLong(); |
| 164 | } |
| 165 | if (std::holds_alternative<int64_t>(contents)) { |
| 166 | return static_cast<int32_t>(*(std::get_if<int64_t>(&contents))); |
| 167 | } |
| 168 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 169 | } |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Gets the int64 value of this object. If this object is not of type |
| 175 | * kInt64 then the result is undefined and the error code is set. |
| 176 | * If conversion to int64 is desired, call getInt64() |
| 177 | * |
| 178 | * @param status Input/output error code. |
| 179 | * @return the int64 value of this object. |
| 180 | * @internal ICU 75 technology preview |
| 181 | * @deprecated This API is for technology preview only. |
| 182 | */ |
| 183 | int64_t getInt64Value(UErrorCode& status) const { |
| 184 | if (U_SUCCESS(status)) { |
| 185 | if (isDecimal() && getType() == UFMT_INT64) { |
| 186 | return std::get_if<icu::Formattable>(&contents)->getInt64(); |
| 187 | } |
| 188 | if (std::holds_alternative<int64_t>(contents)) { |
| 189 | return *(std::get_if<int64_t>(&contents)); |
| 190 | } |
| 191 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Gets the int64 value of this object. If this object is of a numeric |
| 198 | * type and the magnitude is too large to fit in an int64, then |
| 199 | * the maximum or minimum int64 value, as appropriate, is returned |
| 200 | * and the status is set to U_INVALID_FORMAT_ERROR. If the |
| 201 | * magnitude fits in an int64, then a casting conversion is |
| 202 | * performed, with truncation of any fractional part. If this object is |
| 203 | * not a numeric type, then 0 is returned and |
| 204 | * the status is set to U_INVALID_FORMAT_ERROR. |
| 205 | * @param status the error code |
| 206 | * @return the int64 value of this object. |
| 207 | * @internal ICU 75 technology preview |
| 208 | * @deprecated This API is for technology preview only. |
| 209 | */ |
| 210 | int64_t getInt64(UErrorCode& status) const; |
| 211 | /** |
| 212 | * Gets the string value of this object. If this object is not of type |
| 213 | * kString then the result is undefined and the error code is set. |
| 214 | * |
| 215 | * @param status Input/output error code. |
| 216 | * @return A reference to the string value of this object. |
| 217 | * @internal ICU 75 technology preview |
| 218 | * @deprecated This API is for technology preview only. |
| 219 | */ |
| 220 | const UnicodeString& getString(UErrorCode& status) const { |
| 221 | if (U_SUCCESS(status)) { |
| 222 | if (std::holds_alternative<UnicodeString>(contents)) { |
| 223 | return *std::get_if<UnicodeString>(&contents); |
| 224 | } |
| 225 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 226 | } |
| 227 | return bogusString; |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Gets the Date value of this object. If this object is not of type |
| 232 | * kDate then the result is undefined and the error code is set. |
| 233 | * |
| 234 | * @param status Input/output error code. |
| 235 | * @return the Date value of this object. |
| 236 | * @internal ICU 75 technology preview |
| 237 | * @deprecated This API is for technology preview only. |
| 238 | */ |
| 239 | UDate getDate(UErrorCode& status) const { |
| 240 | if (U_SUCCESS(status)) { |
| 241 | if (isDate()) { |
| 242 | return *std::get_if<double>(&contents); |
| 243 | } |
| 244 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | /** |
| 250 | * Returns true if the data type of this Formattable object |
| 251 | * is kDouble |
| 252 | * @return true if this is a pure numeric object |
| 253 | * @internal ICU 75 technology preview |
| 254 | * @deprecated This API is for technology preview only. |
| 255 | */ |
| 256 | UBool isNumeric() const { return (getType() == UFMT_DOUBLE || getType() == UFMT_LONG || getType() == UFMT_INT64); } |
| 257 | |
| 258 | /** |
| 259 | * Gets the array value and count of this object. If this object |
| 260 | * is not of type kArray then the result is undefined and the error code is set. |
| 261 | * |
| 262 | * @param count fill-in with the count of this object. |
| 263 | * @param status Input/output error code. |
| 264 | * @return the array value of this object. |
| 265 | * @internal ICU 75 technology preview |
| 266 | * @deprecated This API is for technology preview only. |
| 267 | */ |
| 268 | const Formattable* getArray(int32_t& count, UErrorCode& status) const; |
| 269 | |
| 270 | /** |
| 271 | * Returns a pointer to the FormattableObject contained within this |
| 272 | * formattable, or if this object does not contain a FormattableObject, |
| 273 | * returns nullptr and sets the error code. |
| 274 | * |
| 275 | * @param status Input/output error code. |
| 276 | * @return a FormattableObject pointer, or nullptr |
| 277 | * @internal ICU 75 technology preview |
| 278 | * @deprecated This API is for technology preview only. |
| 279 | */ |
| 280 | const FormattableObject* getObject(UErrorCode& status) const { |
| 281 | if (U_SUCCESS(status)) { |
| 282 | // Can't return a reference since FormattableObject |
| 283 | // is an abstract class |
| 284 | if (getType() == UFMT_OBJECT) { |
| 285 | return *std::get_if<const FormattableObject*>(&contents); |
| 286 | // TODO: should assert that if type is object, object is non-null |
| 287 | } |
| 288 | status = U_ILLEGAL_ARGUMENT_ERROR; |
| 289 | } |
| 290 | return nullptr; |
| 291 | } |
| 292 | /** |
| 293 | * Non-member swap function. |
| 294 | * @param f1 will get f2's contents |
| 295 | * @param f2 will get f1's contents |
| 296 | * |
| 297 | * @internal ICU 75 technology preview |
| 298 | * @deprecated This API is for technology preview only. |
| 299 | */ |
| 300 | friend inline void swap(Formattable& f1, Formattable& f2) noexcept { |
| 301 | using std::swap; |
| 302 | |
| 303 | swap(f1.contents, f2.contents); |
| 304 | swap(f1.holdsDate, f2.holdsDate); |
| 305 | } |
| 306 | /** |
| 307 | * Copy constructor. |
| 308 | * |
| 309 | * @internal ICU 75 technology preview |
| 310 | * @deprecated This API is for technology preview only. |
| 311 | */ |
| 312 | Formattable(const Formattable&); |
| 313 | /** |
| 314 | * Assignment operator |
| 315 | * |
| 316 | * @internal ICU 75 technology preview |
| 317 | * @deprecated This API is for technology preview only. |
| 318 | */ |
| 319 | Formattable& operator=(Formattable) noexcept; |
| 320 | /** |
| 321 | * Default constructor. Leaves the Formattable in a |
| 322 | * valid but undefined state. |
| 323 | * |
| 324 | * @internal ICU 75 technology preview |
| 325 | * @deprecated This API is for technology preview only. |
| 326 | */ |
| 327 | Formattable() : contents(0.0) {} |
| 328 | /** |
| 329 | * String constructor. |
| 330 | * |
| 331 | * @param s A string to wrap as a Formattable. |
| 332 | * |
| 333 | * @internal ICU 75 technology preview |
| 334 | * @deprecated This API is for technology preview only. |
| 335 | */ |
| 336 | Formattable(const UnicodeString& s) : contents(s) {} |
| 337 | /** |
| 338 | * Double constructor. |
| 339 | * |
| 340 | * @param d A double value to wrap as a Formattable. |
| 341 | * |
| 342 | * @internal ICU 75 technology preview |
| 343 | * @deprecated This API is for technology preview only. |
| 344 | */ |
| 345 | Formattable(double d) : contents(d) {} |
| 346 | /** |
| 347 | * Int64 constructor. |
| 348 | * |
| 349 | * @param i An int64 value to wrap as a Formattable. |
| 350 | * |
| 351 | * @internal ICU 75 technology preview |
| 352 | * @deprecated This API is for technology preview only. |
| 353 | */ |
| 354 | Formattable(int64_t i) : contents(i) {} |
| 355 | /** |
| 356 | * Date factory method. |
| 357 | * |
| 358 | * @param d A UDate value to wrap as a Formattable. |
| 359 | * @internal ICU 75 technology preview |
| 360 | * @deprecated This API is for technology preview only. |
| 361 | */ |
| 362 | static Formattable forDate(UDate d) { |
| 363 | Formattable f; |
| 364 | f.contents = d; |
| 365 | f.holdsDate = true; |
| 366 | return f; |
| 367 | } |
| 368 | /** |
| 369 | * Creates a Formattable object of an appropriate numeric type from a |
| 370 | * a decimal number in string form. The Formattable will retain the |
| 371 | * full precision of the input in decimal format, even when it exceeds |
| 372 | * what can be represented by a double or int64_t. |
| 373 | * |
| 374 | * @param number the unformatted (not localized) string representation |
| 375 | * of the Decimal number. |
| 376 | * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR |
| 377 | * if the format of the string does not conform to that of a |
| 378 | * decimal number. |
| 379 | * @internal ICU 75 technology preview |
| 380 | * @deprecated This API is for technology preview only. |
| 381 | */ |
| 382 | static Formattable forDecimal(std::string_view number, UErrorCode& status); |
| 383 | /** |
| 384 | * Array constructor. |
| 385 | * |
| 386 | * @param arr An array of Formattables, which is adopted. |
| 387 | * @param len The length of the array. |
| 388 | * |
| 389 | * @internal ICU 75 technology preview |
| 390 | * @deprecated This API is for technology preview only. |
| 391 | */ |
| 392 | Formattable(const Formattable* arr, int32_t len) : contents(std::pair(arr, len)) {} |
| 393 | /** |
| 394 | * Object constructor. |
| 395 | * |
| 396 | * @param obj A FormattableObject (not adopted). |
| 397 | * |
| 398 | * @internal ICU 75 technology preview |
| 399 | * @deprecated This API is for technology preview only. |
| 400 | */ |
| 401 | Formattable(const FormattableObject* obj) : contents(obj) {} |
| 402 | /** |
| 403 | * Destructor. |
| 404 | * |
| 405 | * @internal ICU 75 technology preview |
| 406 | * @deprecated This API is for technology preview only. |
| 407 | */ |
| 408 | virtual ~Formattable(); |
| 409 | /** |
| 410 | * Converts the Formattable object to an ICU Formattable object. |
| 411 | * If this has type UFMT_OBJECT or kArray, then `status` is set to |
| 412 | * U_ILLEGAL_ARGUMENT_ERROR. |
| 413 | * |
| 414 | * @param status Input/output error code. |
| 415 | * @return An icu::Formattable value with the same value as this. |
| 416 | * |
| 417 | * @internal ICU 75 technology preview |
| 418 | * @deprecated This API is for technology preview only. |
| 419 | */ |
| 420 | icu::Formattable asICUFormattable(UErrorCode& status) const; |
| 421 | private: |
| 422 | |
| 423 | std::variant<double, |
| 424 | int64_t, |
| 425 | UnicodeString, |
| 426 | icu::Formattable, // represents a Decimal |
| 427 | const FormattableObject*, |
| 428 | std::pair<const Formattable*, int32_t>> contents; |
| 429 | bool holdsDate = false; // otherwise, we get type errors about UDate being a duplicate type |
| 430 | UnicodeString bogusString; // :(((( |
| 431 | |
| 432 | UBool isDecimal() const { |
| 433 | return std::holds_alternative<icu::Formattable>(contents); |
| 434 | } |
| 435 | UBool isDate() const { |
| 436 | return std::holds_alternative<double>(contents) && holdsDate; |
| 437 | } |
| 438 | }; // class Formattable |
| 439 | |
| 440 | /** |
| 441 | * Internal use only, but has to be included here as part of the implementation |
| 442 | * of the header-only `FunctionOptions::getOptions()` method |
| 443 | * |
| 444 | * A `ResolvedFunctionOption` represents the result of evaluating |
| 445 | * a single named function option. It pairs the given name with the `Formattable` |
| 446 | * value resulting from evaluating the option's value. |
| 447 | * |
| 448 | * `ResolvedFunctionOption` is immutable and is not copyable or movable. |
| 449 | * |
| 450 | * @internal ICU 75 technology preview |
| 451 | * @deprecated This API is for technology preview only. |
| 452 | */ |
| 453 | #ifndef U_IN_DOXYGEN |
| 454 | class U_I18N_API ResolvedFunctionOption : public UObject { |
| 455 | private: |
| 456 | |
| 457 | /* const */ UnicodeString name; |
| 458 | /* const */ Formattable value; |
| 459 | |
| 460 | public: |
| 461 | const UnicodeString& getName() const { return name; } |
| 462 | const Formattable& getValue() const { return value; } |
| 463 | ResolvedFunctionOption(const UnicodeString& n, const Formattable& f) : name(n), value(f) {} |
| 464 | ResolvedFunctionOption() {} |
| 465 | ResolvedFunctionOption(ResolvedFunctionOption&&); |
| 466 | ResolvedFunctionOption& operator=(ResolvedFunctionOption&& other) noexcept { |
| 467 | name = std::move(other.name); |
| 468 | value = std::move(other.value); |
| 469 | return *this; |
| 470 | } |
| 471 | virtual ~ResolvedFunctionOption(); |
| 472 | }; // class ResolvedFunctionOption |
| 473 | #endif |
| 474 | |
| 475 | /** |
| 476 | * Mapping from option names to `message2::Formattable` objects, obtained |
| 477 | * by calling `getOptions()` on a `FunctionOptions` object. |
| 478 | * |
| 479 | * @internal ICU 75 technology preview |
| 480 | * @deprecated This API is for technology preview only. |
| 481 | */ |
| 482 | using FunctionOptionsMap = std::map<UnicodeString, message2::Formattable>; |
| 483 | |
| 484 | /** |
| 485 | * Structure encapsulating named options passed to a custom selector or formatter. |
| 486 | * |
| 487 | * @internal ICU 75 technology preview |
| 488 | * @deprecated This API is for technology preview only. |
| 489 | */ |
| 490 | class U_I18N_API FunctionOptions : public UObject { |
| 491 | public: |
| 492 | /** |
| 493 | * Returns a map of all name-value pairs provided as options to this function. |
| 494 | * The syntactic order of options is not guaranteed to |
| 495 | * be preserved. |
| 496 | * |
| 497 | * This class is immutable and movable but not copyable. |
| 498 | * |
| 499 | * @return A map from strings to `message2::Formattable` objects representing |
| 500 | * the results of resolving each option value. |
| 501 | * |
| 502 | * @internal ICU 75 technology preview |
| 503 | * @deprecated This API is for technology preview only. |
| 504 | */ |
| 505 | FunctionOptionsMap getOptions() const { |
| 506 | int32_t len; |
| 507 | const ResolvedFunctionOption* resolvedOptions = getResolvedFunctionOptions(len); |
| 508 | FunctionOptionsMap result; |
| 509 | for (int32_t i = 0; i < len; i++) { |
| 510 | const ResolvedFunctionOption& opt = resolvedOptions[i]; |
| 511 | result[opt.getName()] = opt.getValue(); |
| 512 | } |
| 513 | return result; |
| 514 | } |
| 515 | /** |
| 516 | * Default constructor. |
| 517 | * Returns an empty mapping. |
| 518 | * |
| 519 | * @internal ICU 75 technology preview |
| 520 | * @deprecated This API is for technology preview only. |
| 521 | */ |
| 522 | FunctionOptions() { options = nullptr; } |
| 523 | /** |
| 524 | * Destructor. |
| 525 | * |
| 526 | * @internal ICU 75 technology preview |
| 527 | * @deprecated This API is for technology preview only. |
| 528 | */ |
| 529 | virtual ~FunctionOptions(); |
| 530 | /** |
| 531 | * Move assignment operator: |
| 532 | * The source FunctionOptions will be left in a valid but undefined state. |
| 533 | * |
| 534 | * @internal ICU 75 technology preview |
| 535 | * @deprecated This API is for technology preview only. |
| 536 | */ |
| 537 | FunctionOptions& operator=(FunctionOptions&&) noexcept; |
| 538 | /** |
| 539 | * Move constructor: |
| 540 | * The source FunctionOptions will be left in a valid but undefined state. |
| 541 | * |
| 542 | * @internal ICU 75 technology preview |
| 543 | * @deprecated This API is for technology preview only. |
| 544 | */ |
| 545 | FunctionOptions(FunctionOptions&&); |
| 546 | /** |
| 547 | * Copy constructor. |
| 548 | * |
| 549 | * @internal ICU 75 technology preview |
| 550 | * @deprecated This API is for technology preview only. |
| 551 | */ |
| 552 | FunctionOptions& operator=(const FunctionOptions&) = delete; |
| 553 | private: |
| 554 | friend class InternalValue; |
| 555 | friend class MessageFormatter; |
| 556 | friend class StandardFunctions; |
| 557 | |
| 558 | explicit FunctionOptions(UVector&&, UErrorCode&); |
| 559 | |
| 560 | const ResolvedFunctionOption* getResolvedFunctionOptions(int32_t& len) const; |
| 561 | UBool getFunctionOption(const UnicodeString&, Formattable&) const; |
| 562 | // Returns empty string if option doesn't exist |
| 563 | UnicodeString getStringFunctionOption(const UnicodeString&) const; |
| 564 | int32_t optionsCount() const { return functionOptionsLen; } |
| 565 | |
| 566 | // Named options passed to functions |
| 567 | // This is not a Hashtable in order to make it possible for code in a public header file |
| 568 | // to construct a std::map from it, on-the-fly. Otherwise, it would be impossible to put |
| 569 | // that code in the header because it would have to call internal Hashtable methods. |
| 570 | ResolvedFunctionOption* options; |
| 571 | int32_t functionOptionsLen = 0; |
| 572 | |
| 573 | // Returns a new FunctionOptions |
| 574 | FunctionOptions mergeOptions(FunctionOptions&& other, UErrorCode&); |
| 575 | }; // class FunctionOptions |
| 576 | |
| 577 | /** |
| 578 | * A `FormattedValue` represents the result of formatting a `message2::Formattable`. |
| 579 | * It contains either a string or a formatted number. (More types could be added |
| 580 | * in the future.) |
| 581 | * |
| 582 | * `FormattedValue` is immutable and movable. It is not copyable. |
| 583 | * |
| 584 | * @internal ICU 75 technology preview |
| 585 | * @deprecated This API is for technology preview only. |
| 586 | */ |
| 587 | class U_I18N_API FormattedValue : public UObject { |
| 588 | public: |
| 589 | /** |
| 590 | * Formatted string constructor. |
| 591 | * @internal ICU 75 technology preview |
| 592 | * @deprecated This API is for technology preview only. |
| 593 | */ |
| 594 | explicit FormattedValue(const UnicodeString&); |
| 595 | /** |
| 596 | * Formatted number constructor. |
| 597 | * @internal ICU 75 technology preview |
| 598 | * @deprecated This API is for technology preview only. |
| 599 | */ |
| 600 | explicit FormattedValue(number::FormattedNumber&&); |
| 601 | /** |
| 602 | * Default constructor. Leaves the FormattedValue in |
| 603 | * a valid but undefined state. |
| 604 | * @internal ICU 75 technology preview |
| 605 | * @deprecated This API is for technology preview only. |
| 606 | */ |
| 607 | FormattedValue() : type(kString) {} |
| 608 | /** |
| 609 | * Returns true iff this is a formatted string. |
| 610 | * |
| 611 | * @return True if and only if this value is a formatted string. |
| 612 | * |
| 613 | * @internal ICU 75 technology preview |
| 614 | * @deprecated This API is for technology preview only. |
| 615 | */ |
| 616 | bool isString() const { return type == kString; } |
| 617 | /** |
| 618 | * Returns true iff this is a formatted number. |
| 619 | * |
| 620 | * @return True if and only if this value is a formatted number. |
| 621 | * |
| 622 | * @internal ICU 75 technology preview |
| 623 | * @deprecated This API is for technology preview only. |
| 624 | */ |
| 625 | bool isNumber() const { return type == kNumber; } |
| 626 | /** |
| 627 | * Gets the string contents of this value. If !isString(), then |
| 628 | * the result is undefined. |
| 629 | * @return A reference to a formatted string. |
| 630 | * @internal ICU 75 technology preview |
| 631 | * @deprecated This API is for technology preview only. |
| 632 | */ |
| 633 | const UnicodeString& getString() const { return stringOutput; } |
| 634 | /** |
| 635 | * Gets the number contents of this value. If !isNumber(), then |
| 636 | * the result is undefined. |
| 637 | * @return A reference to a formatted number. |
| 638 | * @internal ICU 75 technology preview |
| 639 | * @deprecated This API is for technology preview only. |
| 640 | */ |
| 641 | const number::FormattedNumber& getNumber() const { return numberOutput; } |
| 642 | /** |
| 643 | * Move assignment operator: |
| 644 | * The source FormattedValue will be left in a valid but undefined state. |
| 645 | * |
| 646 | * @internal ICU 75 technology preview |
| 647 | * @deprecated This API is for technology preview only. |
| 648 | */ |
| 649 | FormattedValue& operator=(FormattedValue&&) noexcept; |
| 650 | /** |
| 651 | * Move constructor: |
| 652 | * The source FormattedValue will be left in a valid but undefined state. |
| 653 | * |
| 654 | * @internal ICU 75 technology preview |
| 655 | * @deprecated This API is for technology preview only. |
| 656 | */ |
| 657 | FormattedValue(FormattedValue&& other) { *this = std::move(other); } |
| 658 | /** |
| 659 | * Destructor. |
| 660 | * |
| 661 | * @internal ICU 75 technology preview |
| 662 | * @deprecated This API is for technology preview only. |
| 663 | */ |
| 664 | virtual ~FormattedValue(); |
| 665 | private: |
| 666 | enum Type { |
| 667 | kString, |
| 668 | kNumber |
| 669 | }; |
| 670 | Type type; |
| 671 | UnicodeString stringOutput; |
| 672 | number::FormattedNumber numberOutput; |
| 673 | }; // class FormattedValue |
| 674 | |
| 675 | /** |
| 676 | * A `FormattablePlaceholder` encapsulates an input value (a `message2::Formattable`) |
| 677 | * together with an optional output value (a `message2::FormattedValue`). |
| 678 | * More information, such as source line/column numbers, could be added to the class |
| 679 | * in the future. |
| 680 | * |
| 681 | * `FormattablePlaceholder` is immutable (not deeply immutable) and movable. |
| 682 | * It is not copyable. |
| 683 | * |
| 684 | * @internal ICU 75 technology preview |
| 685 | * @deprecated This API is for technology preview only. |
| 686 | */ |
| 687 | class U_I18N_API FormattedPlaceholder : public UObject { |
| 688 | public: |
| 689 | /** |
| 690 | * Fallback constructor. Constructs a value that represents a formatting error, |
| 691 | * without recording an input `Formattable` as the source. |
| 692 | * |
| 693 | * @param s An error string. (See the MessageFormat specification for details |
| 694 | * on fallback strings.) |
| 695 | * |
| 696 | * @internal ICU 75 technology preview |
| 697 | * @deprecated This API is for technology preview only. |
| 698 | */ |
| 699 | explicit FormattedPlaceholder(const UnicodeString& s) : fallback(s), type(kFallback) {} |
| 700 | /** |
| 701 | * Constructor for fully formatted placeholders. |
| 702 | * |
| 703 | * @param input A `FormattedPlaceholder` containing the fallback string and source |
| 704 | * `Formattable` used to construct the formatted value. |
| 705 | * @param output A `FormattedValue` representing the formatted output of `input`. |
| 706 | * Passed by move. |
| 707 | * |
| 708 | * @internal ICU 75 technology preview |
| 709 | * @deprecated This API is for technology preview only. |
| 710 | */ |
| 711 | FormattedPlaceholder(const FormattedPlaceholder& input, FormattedValue&& output) |
| 712 | : fallback(input.fallback), source(input.source), |
| 713 | formatted(std::move(output)), previousOptions(FunctionOptions()), type(kEvaluated) {} |
| 714 | /** |
| 715 | * Constructor for fully formatted placeholders with options. |
| 716 | * |
| 717 | * @param input A `FormattedPlaceholder` containing the fallback string and source |
| 718 | * `Formattable` used to construct the formatted value. |
| 719 | * @param opts Function options that were used to construct `output`. May be the empty map. |
| 720 | * @param output A `FormattedValue` representing the formatted output of `input`. |
| 721 | * Passed by move. |
| 722 | * |
| 723 | * @internal ICU 75 technology preview |
| 724 | * @deprecated This API is for technology preview only. |
| 725 | */ |
| 726 | FormattedPlaceholder(const FormattedPlaceholder& input, FunctionOptions&& opts, FormattedValue&& output) |
| 727 | : fallback(input.fallback), source(input.source), |
| 728 | formatted(std::move(output)), previousOptions(std::move(opts)), type(kEvaluated) {} |
| 729 | /** |
| 730 | * Constructor for unformatted placeholders. |
| 731 | * |
| 732 | * @param input A `Formattable` object. |
| 733 | * @param fb Fallback string to use if an error occurs while formatting the input. |
| 734 | * |
| 735 | * @internal ICU 75 technology preview |
| 736 | * @deprecated This API is for technology preview only. |
| 737 | */ |
| 738 | FormattedPlaceholder(const Formattable& input, const UnicodeString& fb) |
| 739 | : fallback(fb), source(input), type(kUnevaluated) {} |
| 740 | /** |
| 741 | * Default constructor. Leaves the FormattedPlaceholder in a |
| 742 | * valid but undefined state. |
| 743 | * |
| 744 | * @internal ICU 75 technology preview |
| 745 | * @deprecated This API is for technology preview only. |
| 746 | */ |
| 747 | FormattedPlaceholder() : type(kNull) {} |
| 748 | /** |
| 749 | * Returns the source `Formattable` value for this placeholder. |
| 750 | * The result is undefined if this is a null operand. |
| 751 | * |
| 752 | * @return A message2::Formattable value. |
| 753 | * |
| 754 | * @internal ICU 75 technology preview |
| 755 | * @deprecated This API is for technology preview only. |
| 756 | */ |
| 757 | const message2::Formattable& asFormattable() const; |
| 758 | /** |
| 759 | * Returns true iff this is a fallback placeholder. |
| 760 | * |
| 761 | * @return True if and only if this placeholder was constructed from a fallback string, |
| 762 | * with no `Formattable` source or formatting output. |
| 763 | * |
| 764 | * @internal ICU 75 technology preview |
| 765 | * @deprecated This API is for technology preview only. |
| 766 | */ |
| 767 | bool isFallback() const { return type == kFallback; } |
| 768 | /** |
| 769 | * Returns true iff this is a null placeholder. |
| 770 | * |
| 771 | * @return True if and only if this placeholder represents the absent argument to a formatter |
| 772 | * that was invoked without an argument. |
| 773 | * |
| 774 | * @internal ICU 75 technology preview |
| 775 | * @deprecated This API is for technology preview only. |
| 776 | */ |
| 777 | bool isNullOperand() const { return type == kNull; } |
| 778 | /** |
| 779 | * Returns true iff this has formatting output. |
| 780 | * |
| 781 | * @return True if and only if this was constructed from both an input `Formattable` and |
| 782 | * output `FormattedValue`. |
| 783 | * |
| 784 | * @internal ICU 75 technology preview |
| 785 | * @deprecated This API is for technology preview only. |
| 786 | */ |
| 787 | bool isEvaluated() const { return (type == kEvaluated); } |
| 788 | /** |
| 789 | * Returns true iff this represents a valid argument to the formatter. |
| 790 | * |
| 791 | * @return True if and only if this is neither the null argument nor a fallback placeholder. |
| 792 | * |
| 793 | * @internal ICU 75 technology preview |
| 794 | * @deprecated This API is for technology preview only. |
| 795 | */ |
| 796 | bool canFormat() const { return !(isFallback() || isNullOperand()); } |
| 797 | /** |
| 798 | * Gets the fallback value of this placeholder, to be used in its place if an error occurs while |
| 799 | * formatting it. |
| 800 | * @return A reference to this placeholder's fallback string. |
| 801 | * @internal ICU 75 technology preview |
| 802 | * @deprecated This API is for technology preview only. |
| 803 | */ |
| 804 | const UnicodeString& getFallback() const { return fallback; } |
| 805 | /** |
| 806 | * Returns the options of this placeholder. The result is the empty map if !isEvaluated(). |
| 807 | * @return A reference to an option map, capturing the options that were used |
| 808 | * in producing the output of this `FormattedPlaceholder` |
| 809 | * (or empty if there is no output) |
| 810 | * @internal ICU 75 technology preview |
| 811 | * @deprecated This API is for technology preview only. |
| 812 | */ |
| 813 | const FunctionOptions& options() const { return previousOptions; } |
| 814 | |
| 815 | /** |
| 816 | * Returns the formatted output of this placeholder. The result is undefined if !isEvaluated(). |
| 817 | * @return A fully formatted `FormattedPlaceholder`. |
| 818 | * @internal ICU 75 technology preview |
| 819 | * @deprecated This API is for technology preview only. |
| 820 | */ |
| 821 | const FormattedValue& output() const { return formatted; } |
| 822 | /** |
| 823 | * Move assignment operator: |
| 824 | * The source FormattedPlaceholder will be left in a valid but undefined state. |
| 825 | * |
| 826 | * @internal ICU 75 technology preview |
| 827 | * @deprecated This API is for technology preview only. |
| 828 | */ |
| 829 | FormattedPlaceholder& operator=(FormattedPlaceholder&&) noexcept; |
| 830 | /** |
| 831 | * Move constructor: |
| 832 | * The source FormattedPlaceholder will be left in a valid but undefined state. |
| 833 | * |
| 834 | * @internal ICU 75 technology preview |
| 835 | * @deprecated This API is for technology preview only. |
| 836 | */ |
| 837 | FormattedPlaceholder(FormattedPlaceholder&& other) { *this = std::move(other); } |
| 838 | /** |
| 839 | * Formats this as a string, using defaults. If this is |
| 840 | * either the null operand or is a fallback value, the return value is the result of formatting the |
| 841 | * fallback value (which is the default fallback string if this is the null operand). |
| 842 | * If there is no formatted output and the input is object- or array-typed, |
| 843 | * then the argument is treated as a fallback value, since there is no default formatter |
| 844 | * for objects or arrays. |
| 845 | * |
| 846 | * @param locale The locale to use for formatting numbers or dates |
| 847 | * @param status Input/output error code |
| 848 | * @return The result of formatting this placeholder. |
| 849 | * |
| 850 | * @internal ICU 75 technology preview |
| 851 | * @deprecated This API is for technology preview only. |
| 852 | */ |
| 853 | UnicodeString formatToString(const Locale& locale, |
| 854 | UErrorCode& status) const; |
| 855 | |
| 856 | private: |
| 857 | friend class MessageFormatter; |
| 858 | |
| 859 | enum Type { |
| 860 | kFallback, // Represents the result of formatting that encountered an error |
| 861 | kNull, // Represents the absence of both an output and an input (not necessarily an error) |
| 862 | kUnevaluated, // `source` should be valid, but there's no result yet |
| 863 | kEvaluated, // `formatted` exists |
| 864 | }; |
| 865 | UnicodeString fallback; |
| 866 | Formattable source; |
| 867 | FormattedValue formatted; |
| 868 | FunctionOptions previousOptions; // Ignored unless type is kEvaluated |
| 869 | Type type; |
| 870 | }; // class FormattedPlaceholder |
| 871 | |
| 872 | /** |
| 873 | * Not yet implemented: The result of a message formatting operation. Based on |
| 874 | * ICU4J's FormattedMessage.java. |
| 875 | * |
| 876 | * The class will contain information allowing the result to be viewed as a string, |
| 877 | * iterator, etc. (TBD) |
| 878 | * |
| 879 | * @internal ICU 75 technology preview |
| 880 | * @deprecated This API is for technology preview only. |
| 881 | */ |
| 882 | class U_I18N_API FormattedMessage : public icu::FormattedValue { |
| 883 | public: |
| 884 | /** |
| 885 | * Not yet implemented. |
| 886 | * |
| 887 | * @internal ICU 75 technology preview |
| 888 | * @deprecated This API is for ICU internal use only. |
| 889 | */ |
| 890 | FormattedMessage(UErrorCode& status) { |
| 891 | if (U_SUCCESS(status)) { |
| 892 | status = U_UNSUPPORTED_ERROR; |
| 893 | } |
| 894 | } |
| 895 | /** |
| 896 | * Not yet implemented. |
| 897 | * |
| 898 | * @internal ICU 75 technology preview |
| 899 | * @deprecated This API is for ICU internal use only. |
| 900 | */ |
| 901 | int32_t length(UErrorCode& status) const { |
| 902 | if (U_SUCCESS(status)) { |
| 903 | status = U_UNSUPPORTED_ERROR; |
| 904 | } |
| 905 | return -1; |
| 906 | } |
| 907 | /** |
| 908 | * Not yet implemented. |
| 909 | * |
| 910 | * @internal ICU 75 technology preview |
| 911 | * @deprecated This API is for ICU internal use only. |
| 912 | */ |
| 913 | char16_t charAt(int32_t index, UErrorCode& status) const { |
| 914 | (void) index; |
| 915 | if (U_SUCCESS(status)) { |
| 916 | status = U_UNSUPPORTED_ERROR; |
| 917 | } |
| 918 | return 0; |
| 919 | } |
| 920 | /** |
| 921 | * Not yet implemented. |
| 922 | * |
| 923 | * @internal ICU 75 technology preview |
| 924 | * @deprecated This API is for ICU internal use only. |
| 925 | */ |
| 926 | StringPiece subSequence(int32_t start, int32_t end, UErrorCode& status) const { |
| 927 | (void) start; |
| 928 | (void) end; |
| 929 | if (U_SUCCESS(status)) { |
| 930 | status = U_UNSUPPORTED_ERROR; |
| 931 | } |
| 932 | return ""; |
| 933 | } |
| 934 | /** |
| 935 | * Not yet implemented. |
| 936 | * |
| 937 | * @internal ICU 75 technology preview |
| 938 | * @deprecated This API is for ICU internal use only. |
| 939 | */ |
| 940 | UnicodeString toString(UErrorCode& status) const override { |
| 941 | if (U_SUCCESS(status)) { |
| 942 | status = U_UNSUPPORTED_ERROR; |
| 943 | } |
| 944 | return {}; |
| 945 | } |
| 946 | /** |
| 947 | * Not yet implemented. |
| 948 | * |
| 949 | * @internal ICU 75 technology preview |
| 950 | * @deprecated This API is for ICU internal use only. |
| 951 | */ |
| 952 | UnicodeString toTempString(UErrorCode& status) const override { |
| 953 | if (U_SUCCESS(status)) { |
| 954 | status = U_UNSUPPORTED_ERROR; |
| 955 | } |
| 956 | return {}; |
| 957 | } |
| 958 | /** |
| 959 | * Not yet implemented. |
| 960 | * |
| 961 | * @internal ICU 75 technology preview |
| 962 | * @deprecated This API is for ICU internal use only. |
| 963 | */ |
| 964 | Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override { |
| 965 | if (U_SUCCESS(status)) { |
| 966 | status = U_UNSUPPORTED_ERROR; |
| 967 | } |
| 968 | return appendable; |
| 969 | } |
| 970 | /** |
| 971 | * Not yet implemented. |
| 972 | * |
| 973 | * @internal ICU 75 technology preview |
| 974 | * @deprecated This API is for ICU internal use only. |
| 975 | */ |
| 976 | UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override { |
| 977 | (void) cfpos; |
| 978 | if (U_SUCCESS(status)) { |
| 979 | status = U_UNSUPPORTED_ERROR; |
| 980 | } |
| 981 | return false; |
| 982 | } |
| 983 | /** |
| 984 | * Not yet implemented. |
| 985 | * |
| 986 | * @internal ICU 75 technology preview |
| 987 | * @deprecated This API is for ICU internal use only. |
| 988 | */ |
| 989 | CharacterIterator* toCharacterIterator(UErrorCode& status) { |
| 990 | if (U_SUCCESS(status)) { |
| 991 | status = U_UNSUPPORTED_ERROR; |
| 992 | } |
| 993 | return nullptr; |
| 994 | } |
| 995 | /** |
| 996 | * Destructor. |
| 997 | * |
| 998 | * @internal ICU 75 technology preview |
| 999 | * @deprecated This API is for ICU internal use only. |
| 1000 | */ |
| 1001 | virtual ~FormattedMessage(); |
| 1002 | }; // class FormattedMessage |
| 1003 | |
| 1004 | } // namespace message2 |
| 1005 | |
| 1006 | U_NAMESPACE_END |
| 1007 | |
| 1008 | #endif // U_HIDE_DEPRECATED_API |
| 1009 | |
| 1010 | #endif /* #if !UCONFIG_NO_MF2 */ |
| 1011 | |
| 1012 | #endif /* #if !UCONFIG_NO_FORMATTING */ |
| 1013 | |
| 1014 | #endif /* #if !UCONFIG_NO_NORMALIZATION */ |
| 1015 | |
| 1016 | #endif /* U_SHOW_CPLUSPLUS_API */ |
| 1017 | |
| 1018 | #endif // MESSAGEFORMAT2_FORMATTABLE_H |
| 1019 | |
| 1020 | // eof |