|
|
|||
File indexing completed on 2026-01-07 10:23:59
0001 // © 2024 and later: Unicode, Inc. and others. 0002 // License & terms of use: http://www.unicode.org/copyright.html 0003 0004 #include "unicode/utypes.h" 0005 0006 #ifndef MESSAGEFORMAT2_FORMATTABLE_H 0007 #define MESSAGEFORMAT2_FORMATTABLE_H 0008 0009 #if U_SHOW_CPLUSPLUS_API 0010 0011 #if !UCONFIG_NO_FORMATTING 0012 0013 #if !UCONFIG_NO_MF2 0014 0015 #include "unicode/chariter.h" 0016 #include "unicode/numberformatter.h" 0017 #include "unicode/messageformat2_data_model_names.h" 0018 0019 #ifndef U_HIDE_DEPRECATED_API 0020 0021 #include <map> 0022 #include <variant> 0023 0024 U_NAMESPACE_BEGIN 0025 0026 class Hashtable; 0027 class UVector; 0028 0029 namespace message2 { 0030 0031 class Formatter; 0032 class MessageContext; 0033 class Selector; 0034 0035 // Formattable 0036 // ---------- 0037 0038 /** 0039 * `FormattableObject` is an abstract class that can be implemented in order to define 0040 * an arbitrary class that can be passed to a custom formatter or selector function. 0041 * To be passed in such a way, it must be wrapped in a `Formattable` object. 0042 * 0043 * @internal ICU 75 technology preview 0044 * @deprecated This API is for technology preview only. 0045 */ 0046 class U_I18N_API FormattableObject : public UObject { 0047 public: 0048 /** 0049 * Returns an arbitrary string representing the type of this object. 0050 * It's up to the implementor of this class, as well as the implementors 0051 * of any custom functions that rely on particular values of this tag 0052 * corresponding to particular classes that the object contents can be 0053 * downcast to, to ensure that the type tags are used soundly. 0054 * @internal ICU 75 technology preview 0055 * @deprecated This API is for technology preview only. 0056 */ 0057 virtual const UnicodeString& tag() const = 0; 0058 /** 0059 * Destructor. 0060 * 0061 * @internal ICU 75 technology preview 0062 * @deprecated This API is for technology preview only. 0063 */ 0064 virtual ~FormattableObject(); 0065 }; // class FormattableObject 0066 0067 class Formattable; 0068 } // namespace message2 0069 0070 U_NAMESPACE_END 0071 0072 /// @cond DOXYGEN_IGNORE 0073 // Export an explicit template instantiation of the std::variant that is used 0074 // to represent the message2::Formattable class. 0075 // (When building DLLs for Windows this is required.) 0076 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others 0077 // for similar examples.) 0078 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN 0079 #if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION) 0080 template class U_I18N_API std::_Variant_storage_<false, 0081 double, 0082 int64_t, 0083 icu::UnicodeString, 0084 icu::Formattable, 0085 const icu::message2::FormattableObject *, 0086 std::pair<const icu::message2::Formattable *,int32_t>>; 0087 #endif 0088 typedef std::pair<const icu::message2::Formattable*, int32_t> P; 0089 template class U_I18N_API std::variant<double, 0090 int64_t, 0091 icu::UnicodeString, 0092 icu::Formattable, 0093 const icu::message2::FormattableObject*, 0094 P>; 0095 #endif 0096 /// @endcond 0097 0098 U_NAMESPACE_BEGIN 0099 0100 namespace message2 { 0101 /** 0102 * The `Formattable` class represents a typed value that can be formatted, 0103 * originating either from a message argument or a literal in the code. 0104 * ICU's Formattable class is not used in MessageFormat 2 because it's unsafe to copy an 0105 * icu::Formattable value that contains an object. (See ICU-20275). 0106 * 0107 * `Formattable` is immutable (not deeply immutable) and 0108 * is movable and copyable. 0109 * (Copying does not do a deep copy when the wrapped value is an array or 0110 * object. Likewise, while a pointer to a wrapped array or object is `const`, 0111 * the referents of the pointers may be mutated by other code.) 0112 * 0113 * @internal ICU 75 technology preview 0114 * @deprecated This API is for technology preview only. 0115 */ 0116 class U_I18N_API Formattable : public UObject { 0117 public: 0118 0119 /** 0120 * Gets the data type of this Formattable object. 0121 * @return the data type of this Formattable object. 0122 * @internal ICU 75 technology preview 0123 * @deprecated This API is for technology preview only. 0124 */ 0125 UFormattableType getType() const; 0126 0127 /** 0128 * Gets the double value of this object. If this object is not of type 0129 * UFMT_DOUBLE, then the result is undefined and the error code is set. 0130 * 0131 * @param status Input/output error code. 0132 * @return the double value of this object. 0133 * @internal ICU 75 technology preview 0134 * @deprecated This API is for technology preview only. 0135 */ 0136 double getDouble(UErrorCode& status) const { 0137 if (U_SUCCESS(status)) { 0138 if (isDecimal() && getType() == UFMT_DOUBLE) { 0139 return (std::get_if<icu::Formattable>(&contents))->getDouble(); 0140 } 0141 if (std::holds_alternative<double>(contents)) { 0142 return *(std::get_if<double>(&contents)); 0143 } 0144 status = U_ILLEGAL_ARGUMENT_ERROR; 0145 } 0146 return 0; 0147 } 0148 0149 /** 0150 * Gets the long value of this object. If this object is not of type 0151 * UFMT_LONG then the result is undefined and the error code is set. 0152 * 0153 * @param status Input/output error code. 0154 * @return the long value of this object. 0155 * @internal ICU 75 technology preview 0156 * @deprecated This API is for technology preview only. 0157 */ 0158 int32_t getLong(UErrorCode& status) const { 0159 if (U_SUCCESS(status)) { 0160 if (isDecimal() && getType() == UFMT_LONG) { 0161 return std::get_if<icu::Formattable>(&contents)->getLong(); 0162 } 0163 if (std::holds_alternative<int64_t>(contents)) { 0164 return static_cast<int32_t>(*(std::get_if<int64_t>(&contents))); 0165 } 0166 status = U_ILLEGAL_ARGUMENT_ERROR; 0167 } 0168 return 0; 0169 } 0170 0171 /** 0172 * Gets the int64 value of this object. If this object is not of type 0173 * kInt64 then the result is undefined and the error code is set. 0174 * If conversion to int64 is desired, call getInt64() 0175 * 0176 * @param status Input/output error code. 0177 * @return the int64 value of this object. 0178 * @internal ICU 75 technology preview 0179 * @deprecated This API is for technology preview only. 0180 */ 0181 int64_t getInt64Value(UErrorCode& status) const { 0182 if (U_SUCCESS(status)) { 0183 if (isDecimal() && getType() == UFMT_INT64) { 0184 return std::get_if<icu::Formattable>(&contents)->getInt64(); 0185 } 0186 if (std::holds_alternative<int64_t>(contents)) { 0187 return *(std::get_if<int64_t>(&contents)); 0188 } 0189 status = U_ILLEGAL_ARGUMENT_ERROR; 0190 } 0191 return 0; 0192 } 0193 0194 /** 0195 * Gets the int64 value of this object. If this object is of a numeric 0196 * type and the magnitude is too large to fit in an int64, then 0197 * the maximum or minimum int64 value, as appropriate, is returned 0198 * and the status is set to U_INVALID_FORMAT_ERROR. If the 0199 * magnitude fits in an int64, then a casting conversion is 0200 * performed, with truncation of any fractional part. If this object is 0201 * not a numeric type, then 0 is returned and 0202 * the status is set to U_INVALID_FORMAT_ERROR. 0203 * @param status the error code 0204 * @return the int64 value of this object. 0205 * @internal ICU 75 technology preview 0206 * @deprecated This API is for technology preview only. 0207 */ 0208 int64_t getInt64(UErrorCode& status) const; 0209 /** 0210 * Gets the string value of this object. If this object is not of type 0211 * kString then the result is undefined and the error code is set. 0212 * 0213 * @param status Input/output error code. 0214 * @return A reference to the string value of this object. 0215 * @internal ICU 75 technology preview 0216 * @deprecated This API is for technology preview only. 0217 */ 0218 const UnicodeString& getString(UErrorCode& status) const { 0219 if (U_SUCCESS(status)) { 0220 if (std::holds_alternative<UnicodeString>(contents)) { 0221 return *std::get_if<UnicodeString>(&contents); 0222 } 0223 status = U_ILLEGAL_ARGUMENT_ERROR; 0224 } 0225 return bogusString; 0226 } 0227 0228 /** 0229 * Gets the Date value of this object. If this object is not of type 0230 * kDate then the result is undefined and the error code is set. 0231 * 0232 * @param status Input/output error code. 0233 * @return the Date value of this object. 0234 * @internal ICU 75 technology preview 0235 * @deprecated This API is for technology preview only. 0236 */ 0237 UDate getDate(UErrorCode& status) const { 0238 if (U_SUCCESS(status)) { 0239 if (isDate()) { 0240 return *std::get_if<double>(&contents); 0241 } 0242 status = U_ILLEGAL_ARGUMENT_ERROR; 0243 } 0244 return 0; 0245 } 0246 0247 /** 0248 * Returns true if the data type of this Formattable object 0249 * is kDouble 0250 * @return true if this is a pure numeric object 0251 * @internal ICU 75 technology preview 0252 * @deprecated This API is for technology preview only. 0253 */ 0254 UBool isNumeric() const { return (getType() == UFMT_DOUBLE || getType() == UFMT_LONG || getType() == UFMT_INT64); } 0255 0256 /** 0257 * Gets the array value and count of this object. If this object 0258 * is not of type kArray then the result is undefined and the error code is set. 0259 * 0260 * @param count fill-in with the count of this object. 0261 * @param status Input/output error code. 0262 * @return the array value of this object. 0263 * @internal ICU 75 technology preview 0264 * @deprecated This API is for technology preview only. 0265 */ 0266 const Formattable* getArray(int32_t& count, UErrorCode& status) const; 0267 0268 /** 0269 * Returns a pointer to the FormattableObject contained within this 0270 * formattable, or if this object does not contain a FormattableObject, 0271 * returns nullptr and sets the error code. 0272 * 0273 * @param status Input/output error code. 0274 * @return a FormattableObject pointer, or nullptr 0275 * @internal ICU 75 technology preview 0276 * @deprecated This API is for technology preview only. 0277 */ 0278 const FormattableObject* getObject(UErrorCode& status) const { 0279 if (U_SUCCESS(status)) { 0280 // Can't return a reference since FormattableObject 0281 // is an abstract class 0282 if (getType() == UFMT_OBJECT) { 0283 return *std::get_if<const FormattableObject*>(&contents); 0284 // TODO: should assert that if type is object, object is non-null 0285 } 0286 status = U_ILLEGAL_ARGUMENT_ERROR; 0287 } 0288 return nullptr; 0289 } 0290 /** 0291 * Non-member swap function. 0292 * @param f1 will get f2's contents 0293 * @param f2 will get f1's contents 0294 * 0295 * @internal ICU 75 technology preview 0296 * @deprecated This API is for technology preview only. 0297 */ 0298 friend inline void swap(Formattable& f1, Formattable& f2) noexcept { 0299 using std::swap; 0300 0301 swap(f1.contents, f2.contents); 0302 swap(f1.holdsDate, f2.holdsDate); 0303 } 0304 /** 0305 * Copy constructor. 0306 * 0307 * @internal ICU 75 technology preview 0308 * @deprecated This API is for technology preview only. 0309 */ 0310 Formattable(const Formattable&); 0311 /** 0312 * Assignment operator 0313 * 0314 * @internal ICU 75 technology preview 0315 * @deprecated This API is for technology preview only. 0316 */ 0317 Formattable& operator=(Formattable) noexcept; 0318 /** 0319 * Default constructor. Leaves the Formattable in a 0320 * valid but undefined state. 0321 * 0322 * @internal ICU 75 technology preview 0323 * @deprecated This API is for technology preview only. 0324 */ 0325 Formattable() : contents(0.0) {} 0326 /** 0327 * String constructor. 0328 * 0329 * @param s A string to wrap as a Formattable. 0330 * 0331 * @internal ICU 75 technology preview 0332 * @deprecated This API is for technology preview only. 0333 */ 0334 Formattable(const UnicodeString& s) : contents(s) {} 0335 /** 0336 * Double constructor. 0337 * 0338 * @param d A double value to wrap as a Formattable. 0339 * 0340 * @internal ICU 75 technology preview 0341 * @deprecated This API is for technology preview only. 0342 */ 0343 Formattable(double d) : contents(d) {} 0344 /** 0345 * Int64 constructor. 0346 * 0347 * @param i An int64 value to wrap as a Formattable. 0348 * 0349 * @internal ICU 75 technology preview 0350 * @deprecated This API is for technology preview only. 0351 */ 0352 Formattable(int64_t i) : contents(i) {} 0353 /** 0354 * Date factory method. 0355 * 0356 * @param d A UDate value to wrap as a Formattable. 0357 * @internal ICU 75 technology preview 0358 * @deprecated This API is for technology preview only. 0359 */ 0360 static Formattable forDate(UDate d) { 0361 Formattable f; 0362 f.contents = d; 0363 f.holdsDate = true; 0364 return f; 0365 } 0366 /** 0367 * Creates a Formattable object of an appropriate numeric type from a 0368 * a decimal number in string form. The Formattable will retain the 0369 * full precision of the input in decimal format, even when it exceeds 0370 * what can be represented by a double or int64_t. 0371 * 0372 * @param number the unformatted (not localized) string representation 0373 * of the Decimal number. 0374 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR 0375 * if the format of the string does not conform to that of a 0376 * decimal number. 0377 * @internal ICU 75 technology preview 0378 * @deprecated This API is for technology preview only. 0379 */ 0380 static Formattable forDecimal(std::string_view number, UErrorCode& status); 0381 /** 0382 * Array constructor. 0383 * 0384 * @param arr An array of Formattables, which is adopted. 0385 * @param len The length of the array. 0386 * 0387 * @internal ICU 75 technology preview 0388 * @deprecated This API is for technology preview only. 0389 */ 0390 Formattable(const Formattable* arr, int32_t len) : contents(std::pair(arr, len)) {} 0391 /** 0392 * Object constructor. 0393 * 0394 * @param obj A FormattableObject (not adopted). 0395 * 0396 * @internal ICU 75 technology preview 0397 * @deprecated This API is for technology preview only. 0398 */ 0399 Formattable(const FormattableObject* obj) : contents(obj) {} 0400 /** 0401 * Destructor. 0402 * 0403 * @internal ICU 75 technology preview 0404 * @deprecated This API is for technology preview only. 0405 */ 0406 virtual ~Formattable(); 0407 /** 0408 * Converts the Formattable object to an ICU Formattable object. 0409 * If this has type UFMT_OBJECT or kArray, then `status` is set to 0410 * U_ILLEGAL_ARGUMENT_ERROR. 0411 * 0412 * @param status Input/output error code. 0413 * @return An icu::Formattable value with the same value as this. 0414 * 0415 * @internal ICU 75 technology preview 0416 * @deprecated This API is for technology preview only. 0417 */ 0418 icu::Formattable asICUFormattable(UErrorCode& status) const; 0419 private: 0420 0421 std::variant<double, 0422 int64_t, 0423 UnicodeString, 0424 icu::Formattable, // represents a Decimal 0425 const FormattableObject*, 0426 std::pair<const Formattable*, int32_t>> contents; 0427 bool holdsDate = false; // otherwise, we get type errors about UDate being a duplicate type 0428 UnicodeString bogusString; // :(((( 0429 0430 UBool isDecimal() const { 0431 return std::holds_alternative<icu::Formattable>(contents); 0432 } 0433 UBool isDate() const { 0434 return std::holds_alternative<double>(contents) && holdsDate; 0435 } 0436 }; // class Formattable 0437 0438 /** 0439 * Internal use only, but has to be included here as part of the implementation 0440 * of the header-only `FunctionOptions::getOptions()` method 0441 * 0442 * A `ResolvedFunctionOption` represents the result of evaluating 0443 * a single named function option. It pairs the given name with the `Formattable` 0444 * value resulting from evaluating the option's value. 0445 * 0446 * `ResolvedFunctionOption` is immutable and is not copyable or movable. 0447 * 0448 * @internal ICU 75 technology preview 0449 * @deprecated This API is for technology preview only. 0450 */ 0451 #ifndef U_IN_DOXYGEN 0452 class U_I18N_API ResolvedFunctionOption : public UObject { 0453 private: 0454 0455 /* const */ UnicodeString name; 0456 /* const */ Formattable value; 0457 0458 public: 0459 const UnicodeString& getName() const { return name; } 0460 const Formattable& getValue() const { return value; } 0461 ResolvedFunctionOption(const UnicodeString& n, const Formattable& f) : name(n), value(f) {} 0462 ResolvedFunctionOption() {} 0463 ResolvedFunctionOption(ResolvedFunctionOption&&); 0464 ResolvedFunctionOption& operator=(ResolvedFunctionOption&& other) noexcept { 0465 name = std::move(other.name); 0466 value = std::move(other.value); 0467 return *this; 0468 } 0469 virtual ~ResolvedFunctionOption(); 0470 }; // class ResolvedFunctionOption 0471 #endif 0472 0473 /** 0474 * Mapping from option names to `message2::Formattable` objects, obtained 0475 * by calling `getOptions()` on a `FunctionOptions` object. 0476 * 0477 * @internal ICU 75 technology preview 0478 * @deprecated This API is for technology preview only. 0479 */ 0480 using FunctionOptionsMap = std::map<UnicodeString, message2::Formattable>; 0481 0482 /** 0483 * Structure encapsulating named options passed to a custom selector or formatter. 0484 * 0485 * @internal ICU 75 technology preview 0486 * @deprecated This API is for technology preview only. 0487 */ 0488 class U_I18N_API FunctionOptions : public UObject { 0489 public: 0490 /** 0491 * Returns a map of all name-value pairs provided as options to this function. 0492 * The syntactic order of options is not guaranteed to 0493 * be preserved. 0494 * 0495 * This class is immutable and movable but not copyable. 0496 * 0497 * @return A map from strings to `message2::Formattable` objects representing 0498 * the results of resolving each option value. 0499 * 0500 * @internal ICU 75 technology preview 0501 * @deprecated This API is for technology preview only. 0502 */ 0503 FunctionOptionsMap getOptions() const { 0504 int32_t len; 0505 const ResolvedFunctionOption* resolvedOptions = getResolvedFunctionOptions(len); 0506 FunctionOptionsMap result; 0507 for (int32_t i = 0; i < len; i++) { 0508 const ResolvedFunctionOption& opt = resolvedOptions[i]; 0509 result[opt.getName()] = opt.getValue(); 0510 } 0511 return result; 0512 } 0513 /** 0514 * Default constructor. 0515 * Returns an empty mapping. 0516 * 0517 * @internal ICU 75 technology preview 0518 * @deprecated This API is for technology preview only. 0519 */ 0520 FunctionOptions() { options = nullptr; } 0521 /** 0522 * Destructor. 0523 * 0524 * @internal ICU 75 technology preview 0525 * @deprecated This API is for technology preview only. 0526 */ 0527 virtual ~FunctionOptions(); 0528 /** 0529 * Move assignment operator: 0530 * The source FunctionOptions will be left in a valid but undefined state. 0531 * 0532 * @internal ICU 75 technology preview 0533 * @deprecated This API is for technology preview only. 0534 */ 0535 FunctionOptions& operator=(FunctionOptions&&) noexcept; 0536 /** 0537 * Move constructor: 0538 * The source FunctionOptions will be left in a valid but undefined state. 0539 * 0540 * @internal ICU 75 technology preview 0541 * @deprecated This API is for technology preview only. 0542 */ 0543 FunctionOptions(FunctionOptions&&); 0544 /** 0545 * Copy constructor. 0546 * 0547 * @internal ICU 75 technology preview 0548 * @deprecated This API is for technology preview only. 0549 */ 0550 FunctionOptions& operator=(const FunctionOptions&) = delete; 0551 private: 0552 friend class MessageFormatter; 0553 friend class StandardFunctions; 0554 0555 explicit FunctionOptions(UVector&&, UErrorCode&); 0556 0557 const ResolvedFunctionOption* getResolvedFunctionOptions(int32_t& len) const; 0558 UBool getFunctionOption(const UnicodeString&, Formattable&) const; 0559 // Returns empty string if option doesn't exist 0560 UnicodeString getStringFunctionOption(const UnicodeString&) const; 0561 int32_t optionsCount() const { return functionOptionsLen; } 0562 0563 // Named options passed to functions 0564 // This is not a Hashtable in order to make it possible for code in a public header file 0565 // to construct a std::map from it, on-the-fly. Otherwise, it would be impossible to put 0566 // that code in the header because it would have to call internal Hashtable methods. 0567 ResolvedFunctionOption* options; 0568 int32_t functionOptionsLen = 0; 0569 }; // class FunctionOptions 0570 0571 0572 // TODO doc comments 0573 // Encapsulates either a formatted string or formatted number; 0574 // more output types could be added in the future. 0575 0576 /** 0577 * A `FormattedValue` represents the result of formatting a `message2::Formattable`. 0578 * It contains either a string or a formatted number. (More types could be added 0579 * in the future.) 0580 * 0581 * `FormattedValue` is immutable and movable. It is not copyable. 0582 * 0583 * @internal ICU 75 technology preview 0584 * @deprecated This API is for technology preview only. 0585 */ 0586 class U_I18N_API FormattedValue : public UObject { 0587 public: 0588 /** 0589 * Formatted string constructor. 0590 * @internal ICU 75 technology preview 0591 * @deprecated This API is for technology preview only. 0592 */ 0593 explicit FormattedValue(const UnicodeString&); 0594 /** 0595 * Formatted number constructor. 0596 * @internal ICU 75 technology preview 0597 * @deprecated This API is for technology preview only. 0598 */ 0599 explicit FormattedValue(number::FormattedNumber&&); 0600 /** 0601 * Default constructor. Leaves the FormattedValue in 0602 * a valid but undefined state. 0603 * @internal ICU 75 technology preview 0604 * @deprecated This API is for technology preview only. 0605 */ 0606 FormattedValue() : type(kString) {} 0607 /** 0608 * Returns true iff this is a formatted string. 0609 * 0610 * @return True if and only if this value is a formatted string. 0611 * 0612 * @internal ICU 75 technology preview 0613 * @deprecated This API is for technology preview only. 0614 */ 0615 bool isString() const { return type == kString; } 0616 /** 0617 * Returns true iff this is a formatted number. 0618 * 0619 * @return True if and only if this value is a formatted number. 0620 * 0621 * @internal ICU 75 technology preview 0622 * @deprecated This API is for technology preview only. 0623 */ 0624 bool isNumber() const { return type == kNumber; } 0625 /** 0626 * Gets the string contents of this value. If !isString(), then 0627 * the result is undefined. 0628 * @return A reference to a formatted string. 0629 * @internal ICU 75 technology preview 0630 * @deprecated This API is for technology preview only. 0631 */ 0632 const UnicodeString& getString() const { return stringOutput; } 0633 /** 0634 * Gets the number contents of this value. If !isNumber(), then 0635 * the result is undefined. 0636 * @return A reference to a formatted number. 0637 * @internal ICU 75 technology preview 0638 * @deprecated This API is for technology preview only. 0639 */ 0640 const number::FormattedNumber& getNumber() const { return numberOutput; } 0641 /** 0642 * Move assignment operator: 0643 * The source FormattedValue will be left in a valid but undefined state. 0644 * 0645 * @internal ICU 75 technology preview 0646 * @deprecated This API is for technology preview only. 0647 */ 0648 FormattedValue& operator=(FormattedValue&&) noexcept; 0649 /** 0650 * Move constructor: 0651 * The source FormattedValue will be left in a valid but undefined state. 0652 * 0653 * @internal ICU 75 technology preview 0654 * @deprecated This API is for technology preview only. 0655 */ 0656 FormattedValue(FormattedValue&& other) { *this = std::move(other); } 0657 /** 0658 * Destructor. 0659 * 0660 * @internal ICU 75 technology preview 0661 * @deprecated This API is for technology preview only. 0662 */ 0663 virtual ~FormattedValue(); 0664 private: 0665 enum Type { 0666 kString, 0667 kNumber 0668 }; 0669 Type type; 0670 UnicodeString stringOutput; 0671 number::FormattedNumber numberOutput; 0672 }; // class FormattedValue 0673 0674 /** 0675 * A `FormattablePlaceholder` encapsulates an input value (a `message2::Formattable`) 0676 * together with an optional output value (a `message2::FormattedValue`). 0677 * More information, such as source line/column numbers, could be added to the class 0678 * in the future. 0679 * 0680 * `FormattablePlaceholder` is immutable (not deeply immutable) and movable. 0681 * It is not copyable. 0682 * 0683 * @internal ICU 75 technology preview 0684 * @deprecated This API is for technology preview only. 0685 */ 0686 class U_I18N_API FormattedPlaceholder : public UObject { 0687 public: 0688 /** 0689 * Fallback constructor. Constructs a value that represents a formatting error, 0690 * without recording an input `Formattable` as the source. 0691 * 0692 * @param s An error string. (See the MessageFormat specification for details 0693 * on fallback strings.) 0694 * 0695 * @internal ICU 75 technology preview 0696 * @deprecated This API is for technology preview only. 0697 */ 0698 explicit FormattedPlaceholder(const UnicodeString& s) : fallback(s), type(kFallback) {} 0699 /** 0700 * Constructor for fully formatted placeholders. 0701 * 0702 * @param input A `FormattedPlaceholder` containing the fallback string and source 0703 * `Formattable` used to construct the formatted value. 0704 * @param output A `FormattedValue` representing the formatted output of `input`. 0705 * Passed by move. 0706 * 0707 * @internal ICU 75 technology preview 0708 * @deprecated This API is for technology preview only. 0709 */ 0710 FormattedPlaceholder(const FormattedPlaceholder& input, FormattedValue&& output) 0711 : fallback(input.fallback), source(input.source), 0712 formatted(std::move(output)), previousOptions(FunctionOptions()), type(kEvaluated) {} 0713 /** 0714 * Constructor for fully formatted placeholders with options. 0715 * 0716 * @param input A `FormattedPlaceholder` containing the fallback string and source 0717 * `Formattable` used to construct the formatted value. 0718 * @param opts Function options that were used to construct `output`. May be the empty map. 0719 * @param output A `FormattedValue` representing the formatted output of `input`. 0720 * Passed by move. 0721 * 0722 * @internal ICU 75 technology preview 0723 * @deprecated This API is for technology preview only. 0724 */ 0725 FormattedPlaceholder(const FormattedPlaceholder& input, FunctionOptions&& opts, FormattedValue&& output) 0726 : fallback(input.fallback), source(input.source), 0727 formatted(std::move(output)), previousOptions(std::move(opts)), type(kEvaluated) {} 0728 /** 0729 * Constructor for unformatted placeholders. 0730 * 0731 * @param input A `Formattable` object. 0732 * @param fb Fallback string to use if an error occurs while formatting the input. 0733 * 0734 * @internal ICU 75 technology preview 0735 * @deprecated This API is for technology preview only. 0736 */ 0737 FormattedPlaceholder(const Formattable& input, const UnicodeString& fb) 0738 : fallback(fb), source(input), type(kUnevaluated) {} 0739 /** 0740 * Default constructor. Leaves the FormattedPlaceholder in a 0741 * valid but undefined state. 0742 * 0743 * @internal ICU 75 technology preview 0744 * @deprecated This API is for technology preview only. 0745 */ 0746 FormattedPlaceholder() : type(kNull) {} 0747 /** 0748 * Returns the source `Formattable` value for this placeholder. 0749 * The result is undefined if this is a null operand. 0750 * 0751 * @return A message2::Formattable value. 0752 * 0753 * @internal ICU 75 technology preview 0754 * @deprecated This API is for technology preview only. 0755 */ 0756 const message2::Formattable& asFormattable() const; 0757 /** 0758 * Returns true iff this is a fallback placeholder. 0759 * 0760 * @return True if and only if this placeholder was constructed from a fallback string, 0761 * with no `Formattable` source or formatting output. 0762 * 0763 * @internal ICU 75 technology preview 0764 * @deprecated This API is for technology preview only. 0765 */ 0766 bool isFallback() const { return type == kFallback; } 0767 /** 0768 * Returns true iff this is a null placeholder. 0769 * 0770 * @return True if and only if this placeholder represents the absent argument to a formatter 0771 * that was invoked without an argument. 0772 * 0773 * @internal ICU 75 technology preview 0774 * @deprecated This API is for technology preview only. 0775 */ 0776 bool isNullOperand() const { return type == kNull; } 0777 /** 0778 * Returns true iff this has formatting output. 0779 * 0780 * @return True if and only if this was constructed from both an input `Formattable` and 0781 * output `FormattedValue`. 0782 * 0783 * @internal ICU 75 technology preview 0784 * @deprecated This API is for technology preview only. 0785 */ 0786 bool isEvaluated() const { return (type == kEvaluated); } 0787 /** 0788 * Returns true iff this represents a valid argument to the formatter. 0789 * 0790 * @return True if and only if this is neither the null argument nor a fallback placeholder. 0791 * 0792 * @internal ICU 75 technology preview 0793 * @deprecated This API is for technology preview only. 0794 */ 0795 bool canFormat() const { return !(isFallback() || isNullOperand()); } 0796 /** 0797 * Gets the fallback value of this placeholder, to be used in its place if an error occurs while 0798 * formatting it. 0799 * @return A reference to this placeholder's fallback string. 0800 * @internal ICU 75 technology preview 0801 * @deprecated This API is for technology preview only. 0802 */ 0803 const UnicodeString& getFallback() const { return fallback; } 0804 /** 0805 * Returns the options of this placeholder. The result is the empty map if !isEvaluated(). 0806 * @return A reference to an option map, capturing the options that were used 0807 * in producing the output of this `FormattedPlaceholder` 0808 * (or empty if there is no output) 0809 * @internal ICU 75 technology preview 0810 * @deprecated This API is for technology preview only. 0811 */ 0812 const FunctionOptions& options() const { return previousOptions; } 0813 0814 /** 0815 * Returns the formatted output of this placeholder. The result is undefined if !isEvaluated(). 0816 * @return A fully formatted `FormattedPlaceholder`. 0817 * @internal ICU 75 technology preview 0818 * @deprecated This API is for technology preview only. 0819 */ 0820 const FormattedValue& output() const { return formatted; } 0821 /** 0822 * Move assignment operator: 0823 * The source FormattedPlaceholder will be left in a valid but undefined state. 0824 * 0825 * @internal ICU 75 technology preview 0826 * @deprecated This API is for technology preview only. 0827 */ 0828 FormattedPlaceholder& operator=(FormattedPlaceholder&&) noexcept; 0829 /** 0830 * Move constructor: 0831 * The source FormattedPlaceholder will be left in a valid but undefined state. 0832 * 0833 * @internal ICU 75 technology preview 0834 * @deprecated This API is for technology preview only. 0835 */ 0836 FormattedPlaceholder(FormattedPlaceholder&& other) { *this = std::move(other); } 0837 /** 0838 * Formats this as a string, using defaults. If this is 0839 * either the null operand or is a fallback value, the return value is the result of formatting the 0840 * fallback value (which is the default fallback string if this is the null operand). 0841 * If there is no formatted output and the input is object- or array-typed, 0842 * then the argument is treated as a fallback value, since there is no default formatter 0843 * for objects or arrays. 0844 * 0845 * @param locale The locale to use for formatting numbers or dates 0846 * @param status Input/output error code 0847 * @return The result of formatting this placeholder. 0848 * 0849 * @internal ICU 75 technology preview 0850 * @deprecated This API is for technology preview only. 0851 */ 0852 UnicodeString formatToString(const Locale& locale, 0853 UErrorCode& status) const; 0854 0855 private: 0856 friend class MessageFormatter; 0857 0858 enum Type { 0859 kFallback, // Represents the result of formatting that encountered an error 0860 kNull, // Represents the absence of both an output and an input (not necessarily an error) 0861 kUnevaluated, // `source` should be valid, but there's no result yet 0862 kEvaluated, // `formatted` exists 0863 }; 0864 UnicodeString fallback; 0865 Formattable source; 0866 FormattedValue formatted; 0867 FunctionOptions previousOptions; // Ignored unless type is kEvaluated 0868 Type type; 0869 }; // class FormattedPlaceholder 0870 0871 /** 0872 * Not yet implemented: The result of a message formatting operation. Based on 0873 * ICU4J's FormattedMessage.java. 0874 * 0875 * The class will contain information allowing the result to be viewed as a string, 0876 * iterator, etc. (TBD) 0877 * 0878 * @internal ICU 75 technology preview 0879 * @deprecated This API is for technology preview only. 0880 */ 0881 class U_I18N_API FormattedMessage : public icu::FormattedValue { 0882 public: 0883 /** 0884 * Not yet implemented. 0885 * 0886 * @internal ICU 75 technology preview 0887 * @deprecated This API is for ICU internal use only. 0888 */ 0889 FormattedMessage(UErrorCode& status) { 0890 if (U_SUCCESS(status)) { 0891 status = U_UNSUPPORTED_ERROR; 0892 } 0893 } 0894 /** 0895 * Not yet implemented. 0896 * 0897 * @internal ICU 75 technology preview 0898 * @deprecated This API is for ICU internal use only. 0899 */ 0900 int32_t length(UErrorCode& status) const { 0901 if (U_SUCCESS(status)) { 0902 status = U_UNSUPPORTED_ERROR; 0903 } 0904 return -1; 0905 } 0906 /** 0907 * Not yet implemented. 0908 * 0909 * @internal ICU 75 technology preview 0910 * @deprecated This API is for ICU internal use only. 0911 */ 0912 char16_t charAt(int32_t index, UErrorCode& status) const { 0913 (void) index; 0914 if (U_SUCCESS(status)) { 0915 status = U_UNSUPPORTED_ERROR; 0916 } 0917 return 0; 0918 } 0919 /** 0920 * Not yet implemented. 0921 * 0922 * @internal ICU 75 technology preview 0923 * @deprecated This API is for ICU internal use only. 0924 */ 0925 StringPiece subSequence(int32_t start, int32_t end, UErrorCode& status) const { 0926 (void) start; 0927 (void) end; 0928 if (U_SUCCESS(status)) { 0929 status = U_UNSUPPORTED_ERROR; 0930 } 0931 return ""; 0932 } 0933 /** 0934 * Not yet implemented. 0935 * 0936 * @internal ICU 75 technology preview 0937 * @deprecated This API is for ICU internal use only. 0938 */ 0939 UnicodeString toString(UErrorCode& status) const override { 0940 if (U_SUCCESS(status)) { 0941 status = U_UNSUPPORTED_ERROR; 0942 } 0943 return {}; 0944 } 0945 /** 0946 * Not yet implemented. 0947 * 0948 * @internal ICU 75 technology preview 0949 * @deprecated This API is for ICU internal use only. 0950 */ 0951 UnicodeString toTempString(UErrorCode& status) const override { 0952 if (U_SUCCESS(status)) { 0953 status = U_UNSUPPORTED_ERROR; 0954 } 0955 return {}; 0956 } 0957 /** 0958 * Not yet implemented. 0959 * 0960 * @internal ICU 75 technology preview 0961 * @deprecated This API is for ICU internal use only. 0962 */ 0963 Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override { 0964 if (U_SUCCESS(status)) { 0965 status = U_UNSUPPORTED_ERROR; 0966 } 0967 return appendable; 0968 } 0969 /** 0970 * Not yet implemented. 0971 * 0972 * @internal ICU 75 technology preview 0973 * @deprecated This API is for ICU internal use only. 0974 */ 0975 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override { 0976 (void) cfpos; 0977 if (U_SUCCESS(status)) { 0978 status = U_UNSUPPORTED_ERROR; 0979 } 0980 return false; 0981 } 0982 /** 0983 * Not yet implemented. 0984 * 0985 * @internal ICU 75 technology preview 0986 * @deprecated This API is for ICU internal use only. 0987 */ 0988 CharacterIterator* toCharacterIterator(UErrorCode& status) { 0989 if (U_SUCCESS(status)) { 0990 status = U_UNSUPPORTED_ERROR; 0991 } 0992 return nullptr; 0993 } 0994 /** 0995 * Destructor. 0996 * 0997 * @internal ICU 75 technology preview 0998 * @deprecated This API is for ICU internal use only. 0999 */ 1000 virtual ~FormattedMessage(); 1001 }; // class FormattedMessage 1002 1003 } // namespace message2 1004 1005 U_NAMESPACE_END 1006 1007 #endif // U_HIDE_DEPRECATED_API 1008 1009 #endif /* #if !UCONFIG_NO_MF2 */ 1010 1011 #endif /* #if !UCONFIG_NO_FORMATTING */ 1012 1013 #endif /* U_SHOW_CPLUSPLUS_API */ 1014 1015 #endif // MESSAGEFORMAT2_FORMATTABLE_H 1016 1017 // eof
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|