ICU4C 79
Loading...
Searching...
No Matches
messageformat2_formattable.h
1// © 2024 and later: Unicode, Inc. and others.
2// License & terms of use: http://www.unicode.org/copyright.html
3
4#include "unicode/utypes.h"
5
6#ifndef MESSAGEFORMAT2_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"
19#include "unicode/messageformat2_data_model_names.h"
20#include "unicode/smpdtfmt.h"
21
22#ifndef U_HIDE_DEPRECATED_API
23
24#include <map>
25#include <variant>
26
27U_NAMESPACE_BEGIN
28
29class Hashtable;
30class UVector;
31
32namespace message2 {
33
34 // Formattable
35 // ----------
36
46 public:
56 U_I18N_API virtual const UnicodeString& tag() const = 0;
64 }; // class FormattableObject
65
93
110 public:
111
119
129 U_I18N_API double getDouble(UErrorCode& status) const {
130 if (U_SUCCESS(status)) {
131 if (isDecimal() && getType() == UFMT_DOUBLE) {
132 return (std::get_if<icu::Formattable>(&contents))->getDouble();
133 }
134 if (std::holds_alternative<double>(contents)) {
135 return *(std::get_if<double>(&contents));
136 }
138 }
139 return 0;
140 }
141
151 U_I18N_API int32_t getLong(UErrorCode& status) const {
152 if (U_SUCCESS(status)) {
153 if (isDecimal() && getType() == UFMT_LONG) {
154 return std::get_if<icu::Formattable>(&contents)->getLong();
155 }
156 if (std::holds_alternative<int64_t>(contents)) {
157 return static_cast<int32_t>(*(std::get_if<int64_t>(&contents)));
158 }
160 }
161 return 0;
162 }
163
174 U_I18N_API int64_t getInt64Value(UErrorCode& status) const {
175 if (U_SUCCESS(status)) {
176 if (isDecimal() && getType() == UFMT_INT64) {
177 return std::get_if<icu::Formattable>(&contents)->getInt64();
178 }
179 if (std::holds_alternative<int64_t>(contents)) {
180 return *(std::get_if<int64_t>(&contents));
181 }
183 }
184 return 0;
185 }
186
201 U_I18N_API int64_t getInt64(UErrorCode& status) const;
212 if (U_SUCCESS(status)) {
213 if (std::holds_alternative<UnicodeString>(contents)) {
214 return *std::get_if<UnicodeString>(&contents);
215 }
217 }
218 return bogusString;
219 }
220
232 U_I18N_API const DateInfo* getDate(UErrorCode& status) const {
233 if (U_SUCCESS(status)) {
234 if (isDate()) {
235 return std::get_if<DateInfo>(&contents);
236 }
238 }
239 return nullptr;
240 }
241
250
261 U_I18N_API const Formattable* getArray(int32_t& count, UErrorCode& status) const;
262
274 if (U_SUCCESS(status)) {
275 // Can't return a reference since FormattableObject
276 // is an abstract class
277 if (getType() == UFMT_OBJECT) {
278 auto result = std::get_if<const FormattableObject*>(&contents);
279 // It would be better to U_ASSERT(result != nullptr) here, but
280 // this is a public header file so we can't include uassert.h
281 if (result != nullptr) {
282 return *result;
283 }
284 }
286 }
287 return nullptr;
288 }
289
297 U_I18N_API friend inline void swap(Formattable& f1, Formattable& f2) noexcept {
298 using std::swap;
299
300 swap(f1.contents, f2.contents);
301 }
302
323 U_I18N_API Formattable() : contents(0.0) {}
332 U_I18N_API Formattable(const UnicodeString& s) : contents(s) {}
341 U_I18N_API Formattable(double d) : contents(d) {}
350 U_I18N_API Formattable(int64_t i) : contents(i) {}
360 U_I18N_API Formattable(DateInfo&& d) : contents(std::move(d)) {}
375 U_I18N_API static Formattable forDecimal(std::string_view number, UErrorCode& status);
385 U_I18N_API Formattable(const Formattable* arr, int32_t len) : contents(std::pair(arr, len)) {}
394 U_I18N_API Formattable(const FormattableObject* obj) : contents(obj) {}
414 private:
415
416 std::variant<double,
417 int64_t,
419 icu::Formattable, // represents a Decimal
420 DateInfo,
421 const FormattableObject*,
422 std::pair<const Formattable*, int32_t>> contents;
423 UnicodeString bogusString; // :((((
424
425 UBool isDecimal() const {
426 return std::holds_alternative<icu::Formattable>(contents);
427 }
428 UBool isDate() const {
429 return std::holds_alternative<DateInfo>(contents);
430 }
431 }; // class Formattable
432
446#ifndef U_IN_DOXYGEN
447class FunctionValue;
448class U_I18N_API_CLASS ResolvedFunctionOption : public UObject {
449 private:
450 friend class FunctionOptions;
451
452 /* const */ UnicodeString name;
453 // owned by the global environment
454 const FunctionValue* value;
455 // True if this option is the product of merging two
456 // option maps together, and this option came from the
457 // first argument (the "older" options map).
458 bool thisWasMerged = false;
459
460 public:
461 U_I18N_API const UnicodeString& getName() const { return name; }
462 U_I18N_API const FunctionValue& getValue() const { return *value; }
463 U_I18N_API bool wasMerged() const { return thisWasMerged; }
464 U_I18N_API ResolvedFunctionOption(const UnicodeString& n, const FunctionValue& f, bool b);
465 U_I18N_API ResolvedFunctionOption() {}
466 U_I18N_API ResolvedFunctionOption(ResolvedFunctionOption&&);
467 U_I18N_API ResolvedFunctionOption& operator=(ResolvedFunctionOption&& other) = default;
468 U_I18N_API ResolvedFunctionOption& operator=(const ResolvedFunctionOption& other) = default;
469 U_I18N_API ResolvedFunctionOption(const ResolvedFunctionOption&) = default;
470 U_I18N_API virtual ~ResolvedFunctionOption();
471}; // class ResolvedFunctionOption
472#endif
473
481using FunctionOptionsMap = std::map<UnicodeString, const message2::FunctionValue*>;
482
492 public:
504 FunctionOptionsMap getOptions() const {
505 FunctionOptionsMap result;
506 for (int32_t i = 0; i < functionOptionsLen; i++) {
507 ResolvedFunctionOption& opt = options[i];
508 result[opt.getName()] = &opt.getValue();
509 }
510 return result;
511 }
512
530 FunctionOptions() { options = nullptr; }
546 friend inline void swap(FunctionOptions& f1, FunctionOptions& f2) noexcept {
547 using std::swap;
548
549 if (f1.bogus || f2.bogus) {
550 f1.bogus = f2.bogus = true;
551 return;
552 }
553 swap(f1.options, f2.options);
554 swap(f1.functionOptionsLen, f2.functionOptionsLen);
555 }
556
570 private:
571 friend class MessageFormatter;
572 friend class StandardFunctions;
573
574 explicit FunctionOptions(UVector&&, UErrorCode&);
575
576 const ResolvedFunctionOption* getResolvedFunctionOptions(int32_t& len) const;
577 const FunctionValue* getFunctionOption(const std::u16string_view, UErrorCode&) const;
578 // Returns empty string if option doesn't exist
579 UnicodeString getStringFunctionOption(const std::u16string_view) const;
580 UBool wasSetFromLiteral(const std::u16string_view) const;
581 // Sets error code if option doesn't exist
582 UnicodeString getStringFunctionOption(const std::u16string_view, UErrorCode&) const;
583 int32_t optionsCount() const { return functionOptionsLen; }
584
585 bool bogus = false; // Used in case a copy fails
586 // Named options passed to functions
587 // This is not a Hashtable in order to make it possible for code in a public header file
588 // to construct a std::map from it, on-the-fly. Otherwise, it would be impossible to put
589 // that code in the header because it would have to call internal Hashtable methods.
590 ResolvedFunctionOption* options;
591 int32_t functionOptionsLen = 0;
592}; // class FunctionOptions
593
605 public:
613 if (U_SUCCESS(status)) {
614 status = U_UNSUPPORTED_ERROR;
615 }
616 }
617
623 int32_t length(UErrorCode& status) const {
624 if (U_SUCCESS(status)) {
625 status = U_UNSUPPORTED_ERROR;
626 }
627 return -1;
628 }
629
635 char16_t charAt(int32_t index, UErrorCode& status) const {
636 (void) index;
637 if (U_SUCCESS(status)) {
638 status = U_UNSUPPORTED_ERROR;
639 }
640 return 0;
641 }
642
648 StringPiece subSequence(int32_t start, int32_t end, UErrorCode& status) const {
649 (void) start;
650 (void) end;
651 if (U_SUCCESS(status)) {
652 status = U_UNSUPPORTED_ERROR;
653 }
654 return "";
655 }
656
662 UnicodeString toString(UErrorCode& status) const override {
663 if (U_SUCCESS(status)) {
664 status = U_UNSUPPORTED_ERROR;
665 }
666 return {};
667 }
668
674 UnicodeString toTempString(UErrorCode& status) const override {
675 if (U_SUCCESS(status)) {
676 status = U_UNSUPPORTED_ERROR;
677 }
678 return {};
679 }
680
686 Appendable& appendTo(Appendable& appendable, UErrorCode& status) const override {
687 if (U_SUCCESS(status)) {
688 status = U_UNSUPPORTED_ERROR;
689 }
690 return appendable;
691 }
692
698 UBool nextPosition(ConstrainedFieldPosition& cfpos, UErrorCode& status) const override {
699 (void) cfpos;
700 if (U_SUCCESS(status)) {
701 status = U_UNSUPPORTED_ERROR;
702 }
703 return false;
704 }
705
712 if (U_SUCCESS(status)) {
713 status = U_UNSUPPORTED_ERROR;
714 }
715 return nullptr;
716 }
717
724 }; // class FormattedMessage
725
726} // namespace message2
727
728U_NAMESPACE_END
729
730#endif // U_HIDE_DEPRECATED_API
731
732#endif /* #if !UCONFIG_NO_MF2 */
733
734#endif /* #if !UCONFIG_NO_FORMATTING */
735
736#endif /* #if !UCONFIG_NO_NORMALIZATION */
737
738#endif /* U_SHOW_CPLUSPLUS_API */
739
740#endif // MESSAGEFORMAT2_FORMATTABLE_H
741
742// eof
C++ API: Character Iterator.
Abstract class that defines an API for iteration on text objects.
Definition chariter.h:363
Represents a span of a string containing a given field.
Formattable objects can be passed to the Format class or its subclasses for formatting.
Definition fmtable.h:63
An abstract formatted value: a string with associated field attributes.
A string-like object that points to a sized piece of memory.
Definition stringpiece.h:61
UObject is the common ICU "boilerplate" class.
Definition uobject.h:222
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:303
FormattableObject is an abstract class that can be implemented in order to define an arbitrary class ...
virtual U_I18N_API const UnicodeString & tag() const =0
Returns an arbitrary string representing the type of this object.
virtual U_I18N_API ~FormattableObject()
Destructor.
U_I18N_API friend void swap(Formattable &f1, Formattable &f2) noexcept
Non-member swap function.
U_I18N_API Formattable(const UnicodeString &s)
String constructor.
U_I18N_API int64_t getInt64(UErrorCode &status) const
Gets the int64 value of this object.
U_I18N_API int32_t getLong(UErrorCode &status) const
Gets the long value of this object.
U_I18N_API icu::Formattable asICUFormattable(UErrorCode &status) const
Converts the Formattable object to an ICU Formattable object.
U_I18N_API const DateInfo * getDate(UErrorCode &status) const
Gets the struct representing the date value of this object.
U_I18N_API UBool isNumeric() const
Returns true if the data type of this Formattable object is kDouble.
U_I18N_API Formattable(const FormattableObject *obj)
Object constructor.
U_I18N_API Formattable(double d)
Double constructor.
static U_I18N_API Formattable forDecimal(std::string_view number, UErrorCode &status)
Creates a Formattable object of an appropriate numeric type from a a decimal number in string form.
U_I18N_API Formattable()
Default constructor.
U_I18N_API const Formattable * getArray(int32_t &count, UErrorCode &status) const
Gets the array value and count of this object.
U_I18N_API Formattable(const Formattable *arr, int32_t len)
Array constructor.
U_I18N_API double getDouble(UErrorCode &status) const
Gets the double value of this object.
U_I18N_API UFormattableType getType() const
Gets the data type of this Formattable object.
U_I18N_API Formattable(int64_t i)
Int64 constructor.
U_I18N_API int64_t getInt64Value(UErrorCode &status) const
Gets the int64 value of this object.
U_I18N_API const FormattableObject * getObject(UErrorCode &status) const
Returns a pointer to the FormattableObject contained within this formattable, or if this object does ...
U_I18N_API Formattable(DateInfo &&d)
Date constructor.
U_I18N_API Formattable(const Formattable &)
Copy constructor.
virtual U_I18N_API ~Formattable()
Destructor.
U_I18N_API const UnicodeString & getString(UErrorCode &status) const
Gets the string value of this object.
U_I18N_API Formattable & operator=(Formattable) noexcept
Assignment operator.
UBool nextPosition(ConstrainedFieldPosition &cfpos, UErrorCode &status) const override
Not yet implemented.
StringPiece subSequence(int32_t start, int32_t end, UErrorCode &status) const
Not yet implemented.
int32_t length(UErrorCode &status) const
Not yet implemented.
UnicodeString toTempString(UErrorCode &status) const override
Not yet implemented.
FormattedMessage(UErrorCode &status)
Not yet implemented.
UnicodeString toString(UErrorCode &status) const override
Not yet implemented.
virtual ~FormattedMessage()
Destructor.
CharacterIterator * toCharacterIterator(UErrorCode &status)
Not yet implemented.
char16_t charAt(int32_t index, UErrorCode &status) const
Not yet implemented.
Appendable & appendTo(Appendable &appendable, UErrorCode &status) const override
Not yet implemented.
FunctionOptions & operator=(FunctionOptions) noexcept
Assignment operator.
FunctionOptions mergeOptions(const FunctionOptions &other, UErrorCode &) const
Returns a new FunctionOptions object containing all the key-value pairs from this and other.
virtual ~FunctionOptions()
Destructor.
FunctionOptionsMap getOptions() const
Returns a map of all name-value pairs provided as options to this function.
FunctionOptions(const FunctionOptions &)
Copy constructor.
friend void swap(FunctionOptions &f1, FunctionOptions &f2) noexcept
Non-member swap function.
Type representing argument and return values for custom functions.
C++ API: All-in-one formatter for localized numbers, currencies, and units.
C++ API: Format and parse dates in a language-independent manner.
The DateInfo struct represents all the information needed to format a date with a time zone.
UnicodeString zoneId
IANA time zone name; "UTC" if UTC; empty string if value is floating The time zone is required in ord...
UFormattableType
Enum designating the type of a UFormattable instance.
@ UFMT_LONG
ufmt_getLong() will return without conversion.
@ UFMT_OBJECT
ufmt_getObject() will return without conversion.
@ UFMT_DOUBLE
ufmt_getDouble() will return without conversion.
@ UFMT_INT64
ufmt_getInt64() will return without conversion.
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:269
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:509
@ U_ILLEGAL_ARGUMENT_ERROR
Start of codes indicating failure.
Definition utypes.h:546
@ U_UNSUPPORTED_ERROR
Requested operation not supported in current context.
Definition utypes.h:561
#define U_SUCCESS(x)
Does the error code indicate success?
Definition utypes.h:822
#define U_I18N_API_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
Definition utypes.h:457
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:316
double UDate
Date and Time data type.
Definition utypes.h:218