6#ifndef MESSAGEFORMAT_DATA_MODEL_H
7#define MESSAGEFORMAT_DATA_MODEL_H
9#if U_SHOW_CPLUSPLUS_API
11#if !UCONFIG_NO_NORMALIZATION
13#if !UCONFIG_NO_FORMATTING
18#include "unicode/messageformat2_data_model_names.h"
20#ifndef U_HIDE_DEPRECATED_API
39static inline std::vector<T> toStdVector(
const T* arr, int32_t len) {
40 std::vector<T> result;
41 for (int32_t i = 0; i < len; i++) {
42 result.push_back(arr[i]);
47#if defined(U_REAL_MSVC)
51#pragma warning(disable: 4251)
57 class MessageFormatter;
62 namespace data_model {
138 swap(
l1.thisIsQuoted,
l2.thisIsQuoted);
139 swap(
l1.contents,
l2.contents);
197 bool thisIsQuoted =
false;
209#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
213#if defined(U_REAL_MSVC)
226#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
227#if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
229template class U_I18N_API std::_Variant_storage_<false, icu::UnicodeString, icu::message2::data_model::Literal>;
231template class U_I18N_API std::variant<icu::UnicodeString, icu::message2::data_model::Literal>;
232template class U_I18N_API std::optional<std::variant<icu::UnicodeString, icu::message2::data_model::Literal>>;
233template class U_I18N_API std::optional<icu::message2::data_model::Literal>;
240 namespace data_model {
266 UBool isVariable()
const;
275 UBool isLiteral()
const;
284 virtual UBool isNull()
const;
304 const Literal& asLiteral()
const;
322 explicit Operand(
const UnicodeString& v) : contents(VariableName(v)) {}
332 explicit Operand(
const Literal& l) : contents(l) {}
341 friend inline void swap(Operand& o1, Operand& o2)
noexcept {
345 swap(o1.contents, o2.contents);
353 virtual Operand& operator=(Operand)
noexcept;
360 Operand(
const Operand&);
369 std::optional<std::variant<VariableName, Literal>> contents;
397 UBool isWildcard()
const {
return !contents.has_value(); }
407 const Literal& asLiteral()
const;
414 Key(
const Key& other) : contents(other.contents) {}
422 Key() : contents(std::nullopt) {}
432 explicit Key(
const Literal& lit) : contents(lit) {}
441 friend inline void swap(Key& k1, Key& k2)
noexcept {
444 swap(k1.contents, k2.contents);
452 Key& operator=(Key)
noexcept;
466 bool operator<(
const Key& other)
const;
489 std::optional<Literal> contents;
500#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
501template class U_I18N_API LocalPointerBase<message2::data_model::Key>;
502template class U_I18N_API LocalArray<message2::data_model::Key>;
507 namespace data_model {
518 class U_I18N_API SelectorKeys :
public UObject {
530 std::vector<Key> getKeys()
const {
531 return toStdVector<Key>(keys.getAlias(), len);
544 friend class SelectorKeys;
617 SelectorKeys() : len(0) {}
626 friend inline void swap(SelectorKeys&
s1, SelectorKeys&
s2)
noexcept {
629 swap(
s1.len,
s2.len);
630 swap(
s1.keys,
s2.keys);
638 SelectorKeys(
const SelectorKeys&
other);
645 SelectorKeys& operator=(SelectorKeys
other)
noexcept;
652 virtual ~SelectorKeys();
654 friend class Builder;
655 friend class message2::Checker;
657 friend class message2::Serializer;
662 const Key* getKeysInternal()
const;
670 namespace data_model {
731 swap(
o1.name,
o2.name);
732 swap(
o1.rand,
o2.rand);
768#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
775 namespace data_model {
788 swap(
m1.bogus,
m2.bogus);
789 swap(
m1.options,
m2.options);
790 swap(
m1.len,
m2.len);
792 OptionMap() : len(0) {}
793 OptionMap(
const OptionMap&);
794 OptionMap& operator=(OptionMap);
795 std::vector<Option> getOptions()
const {
796 return toStdVector<Option>(options.getAlias(), len);
799 OptionMap(Option*, int32_t);
800 virtual ~OptionMap();
805 bool checkDuplicates =
true;
812 friend inline void swap(Builder& m1, Builder& m2)
noexcept {
815 swap(m1.options, m2.options);
816 swap(m1.checkDuplicates, m2.checkDuplicates);
819 Builder(
const Builder&) =
delete;
820 Builder& operator=(Builder)
noexcept;
824 friend class message2::Serializer;
827 LocalArray<Option> options;
840 namespace data_model {
864 const FunctionName& getFunctionName()
const;
873 std::vector<Option> getOptions()
const {
874 return options.getOptions();
887 friend class Operator;
889 OptionMap::Builder options;
959 Operator(
const Operator&
other)
noexcept;
968 friend inline void swap(Operator&
o1, Operator&
o2)
noexcept {
971 swap(
o1.name,
o2.name);
972 swap(
o1.options,
o2.options);
980 Operator& operator=(Operator)
noexcept;
997 friend class Binding;
998 friend class Builder;
999 friend class message2::Checker;
1001 friend class message2::Serializer;
1006 const OptionMap& getOptionsInternal()
const;
1023#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1024template class U_I18N_API std::optional<icu::message2::data_model::Operator>;
1031 namespace data_model {
1033 typedef enum UMarkupType {
1096 std::vector<Option>
getOptions()
const {
return options.getOptions(); }
1135 OptionMap::Builder options;
1136 OptionMap::Builder attributes;
1137 UMarkupType type = UMARKUP_COUNT;
1243 friend class message2::Serializer;
1249 const OptionMap& getOptionsInternal()
const {
return options; }
1250 const OptionMap& getAttributesInternal()
const {
return attributes; }
1337 bool hasOperand =
false;
1338 bool hasOperator =
false;
1341 OptionMap::Builder attributes;
1426 swap(
e1.rator,
e2.rator);
1427 swap(
e1.rand,
e2.rand);
1428 swap(
e1.attributes,
e2.attributes);
1460 friend class message2::Serializer;
1479 std::optional<Operator> rator;
1482 const OptionMap& getAttributesInternal()
const {
return attributes; }
1493#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1494template class U_I18N_API LocalPointerBase<message2::data_model::Expression>;
1495template class U_I18N_API LocalArray<message2::data_model::Expression>;
1500 namespace data_model {
1525 UBool isText()
const {
return std::holds_alternative<UnicodeString>(piece); }
1585 swap(
p1.piece,
p2.piece);
1647 friend class Pattern;
1649 std::variant<UnicodeString, Expression, Markup> piece;
1660#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1667 namespace data_model {
1680 friend class PatternPart;
1693 Iterator begin()
const {
1694 return Iterator(
this, 0);
1705 Iterator end()
const {
1706 return Iterator(
this, len);
1719 friend class Pattern;
1809 friend inline void swap(Pattern&
p1, Pattern&
p2)
noexcept {
1812 swap(
p1.bogus,
p2.bogus);
1813 swap(
p1.len,
p2.len);
1814 swap(
p1.parts,
p2.parts);
1822 Pattern(
const Pattern&
other);
1829 Pattern& operator=(Pattern)
noexcept;
1849 using iterator_category = std::forward_iterator_tag;
1850 using difference_type = std::ptrdiff_t;
1851 using value_type = std::variant<UnicodeString, Expression, Markup>;
1852 using pointer = value_type*;
1853 using reference =
const value_type&;
1855 friend class Pattern;
1871 return patternContents(
part);
1890 friend class Builder;
1892 friend class message2::Serializer;
1905 static void initParts(Pattern&,
const Pattern&);
1930 static const std::variant<UnicodeString, Expression, Markup>&
1931 patternContents(
const PatternPart& p) {
return p.piece; }
1954 const Pattern& getPattern()
const {
return p; }
1963 const SelectorKeys& getKeys()
const {
return k; }
1975 Variant(
const SelectorKeys& keys, Pattern&& pattern) : k(keys), p(std::move(pattern)) {}
1984 friend inline void swap(Variant& v1, Variant& v2)
noexcept {
1996 Variant& operator=(Variant other)
noexcept;
2004 Variant() =
default;
2011 Variant(
const Variant&);
2025 namespace data_model {
2046 const Expression& getValue()
const;
2055 const VariableName& getVariable()
const {
return var; }
2070 static Binding input(UnicodeString&& variableName, Expression&& rhs,
UErrorCode& errorCode);
2078 UBool isLocal()
const {
return local; }
2088 Binding(
const VariableName& v, Expression&& e) : var(v), expr(std::move(e)), local(true), annotation(nullptr) {}
2097 friend inline void swap(Binding& b1, Binding& b2)
noexcept {
2100 swap(b1.var, b2.var);
2101 swap(b1.expr, b2.expr);
2102 swap(b1.local, b2.local);
2103 b1.updateAnnotation();
2104 b2.updateAnnotation();
2112 Binding(
const Binding& other);
2119 Binding& operator=(Binding)
noexcept;
2127 Binding() : local(true) {}
2136 friend class message2::Checker;
2137 friend class message2::MessageFormatter;
2138 friend class message2::Parser;
2139 friend class message2::Serializer;
2150 const Operator* annotation =
nullptr;
2152 const OptionMap& getOptionsInternal()
const;
2154 bool hasAnnotation()
const {
return !local && (annotation !=
nullptr); }
2155 void updateAnnotation();
2166#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
2167template class U_I18N_API LocalPointerBase<message2::data_model::Variant>;
2168template class U_I18N_API LocalPointerBase<message2::data_model::Binding>;
2169template class U_I18N_API LocalArray<message2::data_model::Variant>;
2170template class U_I18N_API LocalArray<message2::data_model::Binding>;
2175 using namespace data_model;
2182 #ifndef U_IN_DOXYGEN
2183 class Matcher :
public UObject {
2185 Matcher& operator=(Matcher);
2186 Matcher(
const Matcher&);
2195 friend inline void swap(Matcher& m1, Matcher& m2)
noexcept {
2206 swap(m1.selectors, m2.selectors);
2207 swap(m1.numSelectors, m2.numSelectors);
2208 swap(m1.variants, m2.variants);
2209 swap(m1.numVariants, m2.numVariants);
2214 friend class MFDataModel;
2216 Matcher(VariableName* ss, int32_t ns, Variant* vs, int32_t nv);
2225 LocalArray<VariableName> selectors;
2227 int32_t numSelectors = 0;
2229 LocalArray<Variant> variants;
2231 int32_t numVariants = 0;
2244#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
2245#if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
2246template class U_I18N_API std::_Variant_storage_<false, icu::message2::Matcher,icu::message2::data_model::Pattern>;
2248template class U_I18N_API std::variant<icu::message2::Matcher,icu::message2::data_model::Pattern>;
2274 class U_I18N_API MFDataModel :
public UMemory {
2318 std::vector<Binding> getLocalVariables()
const {
2319 std::vector<Binding> result;
2321 return toStdVector<Binding>(bindings.getAlias(), bindingsLen);
2333 std::vector<VariableName> getSelectors()
const {
2334 if (std::holds_alternative<Pattern>(body)) {
2337 const Matcher* match = std::get_if<Matcher>(&body);
2339 return toStdVector<VariableName>(match->selectors.getAlias(), match->numSelectors);
2349 std::vector<Variant> getVariants()
const {
2351 if (std::holds_alternative<Pattern>(body)) {
2354 const Matcher* match = std::get_if<Matcher>(&body);
2356 return toStdVector<Variant>(match->variants.getAlias(), match->numVariants);
2368 const Pattern& getPattern()
const;
2395 friend inline void swap(MFDataModel& m1, MFDataModel& m2)
noexcept {
2406 swap(m1.body, m2.body);
2407 swap(m1.bindings, m2.bindings);
2408 swap(m1.bindingsLen, m2.bindingsLen);
2416 MFDataModel& operator=(MFDataModel)
noexcept;
2423 MFDataModel(
const MFDataModel& other);
2430 virtual ~MFDataModel();
2441 friend class MFDataModel;
2445 bool hasPattern =
true;
2446 bool hasSelectors =
false;
2550 friend class Checker;
2552 friend class Serializer;
2556 bool hasPattern()
const {
return std::holds_alternative<Pattern>(body); }
2562 std::variant<Matcher, Pattern> body;
2568 const Binding* getLocalVariablesInternal()
const;
2570 const Variant* getVariantsInternal()
const;
2572 int32_t numSelectors()
const {
2573 const Matcher* matcher = std::get_if<Matcher>(&body);
2574 return (matcher ==
nullptr ? 0 : matcher->numSelectors);
2577 const Matcher* matcher = std::get_if<Matcher>(&body);
2578 return (matcher ==
nullptr ? 0 : matcher->numVariants);
2582 void initBindings(
const Binding*);
2584 MFDataModel(
const Builder& builder,
UErrorCode&)
noexcept;
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
"Smart pointer" base class; do not use directly: use LocalPointer etc.
UMemory is the common ICU base class.
UObject is the common ICU "boilerplate" class.
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
The mutable MFDataModel::Builder class allows the data model to be constructed incrementally.
Builder(UErrorCode &status)
Default constructor.
Builder & addSelector(VariableName &&selector, UErrorCode &errorCode)
Adds a selector variable.
Builder & addBinding(Binding &&b, UErrorCode &status)
Adds a binding, There must not already be a binding with the same name.
MFDataModel build(UErrorCode &status) const noexcept
Constructs a new immutable data model.
Builder & addVariant(SelectorKeys &&keys, Pattern &&pattern, UErrorCode &errorCode) noexcept
Adds a single variant.
Builder & setPattern(Pattern &&pattern)
Sets the body of the message as a pattern.
virtual ~Builder()
Destructor.
The mutable Expression::Builder class allows the operator to be constructed incrementally.
Builder & addAttribute(const UnicodeString &key, Operand &&value, UErrorCode &status)
Adds a single attribute.
Expression build(UErrorCode &status)
Constructs a new immutable Expression using the operand and operator that were previously set.
Builder & setOperand(Operand &&rAnd)
Sets the operand of this expression.
virtual ~Builder()
Destructor.
Builder & setOperator(Operator &&rAtor)
Sets the operator of this expression.
Builder(UErrorCode &status)
Default constructor.
The Expression class corresponds to the expression nonterminal in the MessageFormat 2 grammar and the...
const Operand & getOperand() const
Accesses the operand of this expression.
UBool isFunctionCall() const
Checks if this expression has a function annotation (with or without an operand).
Expression & operator=(Expression) noexcept
Assignment operator.
UBool isStandaloneAnnotation() const
Checks if this expression is an annotation with no operand.
std::vector< Option > getAttributes() const
Gets the attributes of this expression.
virtual ~Expression()
Destructor.
Expression()
Default constructor.
Expression(const Expression &other)
Copy constructor.
const Operator * getOperator(UErrorCode &status) const
Accesses the function annotating this expression.
friend void swap(Expression &e1, Expression &e2) noexcept
Non-member swap function.
The Literal class corresponds to the literal nonterminal in the MessageFormat 2 grammar,...
UBool isQuoted() const
Determines if this literal appeared as a quoted literal in the message.
const UnicodeString & unquoted() const
Returns the parsed string contents of this literal.
friend void swap(Literal &l1, Literal &l2) noexcept
Non-member swap function.
Literal(UBool q, const UnicodeString &s)
Literal constructor.
UnicodeString quoted() const
Returns the quoted representation of this literal (enclosed in '|' characters)
Literal()=default
Default constructor.
bool operator<(const Literal &other) const
Less than operator.
Literal(const Literal &other)
Copy constructor.
Literal & operator=(Literal) noexcept
Assignment operator.
bool operator==(const Literal &other) const
Equality operator.
virtual ~Literal()
Destructor.
The mutable Markup::Builder class allows the markup to be constructed incrementally.
Builder & setStandalone()
Sets this to be a standalone markup.
Builder & addOption(const UnicodeString &key, Operand &&value, UErrorCode &status)
Adds a single option.
Builder(UErrorCode &status)
Default constructor.
Builder & addAttribute(const UnicodeString &key, Operand &&value, UErrorCode &status)
Adds a single attribute.
Builder & setClose()
Sets this to be an closing markup.
virtual ~Builder()
Destructor.
Builder & setName(const UnicodeString &n)
Sets the name of this markup.
Markup build(UErrorCode &status)
Constructs a new immutable Markup using the name and type and (optionally) options and attributes tha...
Builder & setOpen()
Sets this to be an opening markup.
The Markup class corresponds to the markup nonterminal in the MessageFormat 2 grammar and the markup ...
Markup()
Default constructor.
std::vector< Option > getAttributes() const
Gets the attributes of this markup.
UBool isOpen() const
Checks if this markup is an opening tag.
std::vector< Option > getOptions() const
Gets the options of this markup.
UBool isClose() const
Checks if this markup is an closing tag.
virtual ~Markup()
Destructor.
const UnicodeString & getName() const
Gets the name of this markup.
UBool isStandalone() const
Checks if this markup is an standalone tag.
The mutable Operator::Builder class allows the operator to be constructed incrementally.
Builder & setFunctionName(FunctionName &&func)
Sets this operator to be a function annotation and sets its name to func.
Operator build(UErrorCode &status)
Constructs a new immutable Operator using the function name and options that were previously set.
Builder & addOption(const UnicodeString &key, Operand &&value, UErrorCode &status) noexcept
Sets this operator to be a function annotation and adds a single option.
Builder(UErrorCode &status)
Default constructor.
virtual ~Builder()
Destructor.
An Option pairs an option name with an Operand.
Option(const Option &other)
Copy constructor.
virtual ~Option()
Destructor.
Option()
Default constructor.
Option & operator=(Option other) noexcept
Assignment operator.
Option(const UnicodeString &n, Operand &&r)
Constructor.
const UnicodeString & getName() const
Accesses the left-hand side of the option.
const Operand & getValue() const
Accesses the right-hand side of the option.
friend void swap(Option &o1, Option &o2) noexcept
Non-member swap function.
A PatternPart is a single element (text or expression) in a Pattern.
UBool isExpression() const
Checks if the part is an expression part.
PatternPart(const UnicodeString &t)
Text part constructor.
const Expression & contents() const
Accesses the expression of the part.
PatternPart(Expression &&e)
Expression part constructor.
const UnicodeString & asText() const
Accesses the text contents of the part.
PatternPart()=default
Default constructor.
PatternPart(Markup &&m)
Markup part constructor.
UBool isMarkup() const
Checks if the part is a markup part.
UBool isText() const
Checks if the part is a text part.
const Markup & asMarkup() const
Accesses the expression of the part.
PatternPart(const PatternPart &other)
Copy constructor.
PatternPart & operator=(PatternPart) noexcept
Assignment operator.
friend void swap(PatternPart &p1, PatternPart &p2) noexcept
Non-member swap function.
virtual ~PatternPart()
Destructor.
The mutable Pattern::Builder class allows the pattern to be constructed one part at a time.
Builder & add(UnicodeString &&part, UErrorCode &status) noexcept
Adds a single text part to the pattern.
virtual ~Builder()
Destructor.
Builder & add(Markup &&part, UErrorCode &status) noexcept
Adds a single markup part to the pattern.
Pattern build(UErrorCode &status) const noexcept
Constructs a new immutable Pattern using the list of parts set with previous add() calls.
Builder(UErrorCode &status)
Default constructor.
Builder & add(Expression &&part, UErrorCode &status) noexcept
Adds a single expression part to the pattern.
The mutable SelectorKeys::Builder class allows the key list to be constructed one key at a time.
Builder & add(Key &&key, UErrorCode &status) noexcept
Adds a single key to the list.
virtual ~Builder()
Destructor.
Builder(UErrorCode &status)
Default constructor.
SelectorKeys build(UErrorCode &status) const
Constructs a new immutable SelectorKeys using the list of keys set with previous add() calls.
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
The Pattern::Iterator class provides an iterator over the formattable parts of a pattern.
reference operator*() const
Dereference operator (gets the element at the current iterator position)
Iterator operator++()
Increment operator (advances to the next iterator position)
int8_t UBool
The ICU boolean type, a signed-byte integer.
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.