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)
56 class MessageFormatter;
61 namespace data_model {
126 Literal(
const Literal& other) : thisIsQuoted(other.thisIsQuoted), contents(other.contents) {}
138 swap(l1.thisIsQuoted, l2.thisIsQuoted);
139 swap(l1.contents, l2.contents);
197 bool thisIsQuoted =
false;
304 swap(o1.contents, o2.contents);
328 std::optional<std::variant<VariableName, Literal>> contents;
403 swap(k1.contents, k2.contents);
448 std::optional<Literal> contents;
474 return toStdVector<Key>(keys.getAlias(), len);
487 friend class SelectorKeys;
572 swap(s1.len, s2.len);
573 swap(s1.keys, s2.keys);
598 friend class message2::Checker;
600 friend class message2::Serializer;
605 const Key* getKeysInternal()
const;
669 swap(o1.name, o2.name);
670 swap(o1.rand, o2.rand);
707 U_I18N_API friend inline void swap(OptionMap& m1, OptionMap& m2)
noexcept {
710 swap(m1.bogus, m2.bogus);
711 swap(m1.options, m2.options);
712 swap(m1.len, m2.len);
717 U_I18N_API std::vector<Option> getOptions()
const {
718 return toStdVector<Option>(options.getAlias(), len);
727 bool checkDuplicates =
true;
734 friend inline void swap(Builder& m1, Builder& m2)
noexcept {
737 swap(m1.options, m2.options);
738 swap(m1.checkDuplicates, m2.checkDuplicates);
741 Builder(
const Builder&) =
delete;
742 Builder& operator=(Builder)
noexcept;
746 friend class message2::MessageFormatter;
747 friend class message2::Serializer;
750 LocalArray<Option> options;
788 return options.getOptions();
801 friend class Operator;
802 FunctionName functionName;
803 OptionMap::Builder options;
885 swap(o1.name, o2.name);
886 swap(o1.options, o2.options);
911 friend class Binding;
913 friend class message2::Checker;
915 friend class message2::Serializer;
920 const OptionMap& getOptionsInternal()
const;
921 Operator(
const FunctionName&,
const OptionMap&);
928 typedef enum UMarkupType {
991 std::vector<Option>
getOptions()
const {
return options.getOptions(); }
1027 friend class Markup;
1030 OptionMap::Builder options;
1031 OptionMap::Builder attributes;
1032 UMarkupType type = UMARKUP_COUNT;
1139 friend class message2::Serializer;
1144 OptionMap attributes;
1145 const OptionMap& getOptionsInternal()
const {
return options; }
1146 const OptionMap& getAttributesInternal()
const {
return attributes; }
1231 friend class Expression;
1233 bool hasOperand =
false;
1234 bool hasOperator =
false;
1237 OptionMap::Builder attributes;
1322 swap(e1.rator, e2.rator);
1323 swap(e1.rand, e2.rand);
1324 swap(e1.attributes, e2.attributes);
1356 friend class message2::Serializer;
1372 Expression(
const Operator &rAtor,
const Operand &rAnd,
const OptionMap& attrs) : rator(rAtor), rand(rAnd), attributes(attrs) {}
1373 Expression(
const Operand &rAnd,
const OptionMap& attrs) : rator(std::nullopt), rand(
Operand(rAnd)), attributes(attrs) {}
1374 Expression(
const Operator &rAtor,
const OptionMap& attrs) : rator(rAtor), rand(), attributes(attrs) {}
1375 std::optional<Operator> rator;
1377 OptionMap attributes;
1378 const OptionMap& getAttributesInternal()
const {
return attributes; }
1464 swap(p1.piece, p2.piece);
1526 friend class Pattern;
1528 std::variant<UnicodeString, Expression, Markup> piece;
1543 friend class PatternPart;
1546 #ifndef U_IN_DOXYGEN
1584 friend class Pattern;
1677 swap(p1.bogus, p2.bogus);
1678 swap(p1.len, p2.len);
1679 swap(p1.parts, p2.parts);
1714 using iterator_category = std::forward_iterator_tag;
1715 using difference_type = std::ptrdiff_t;
1716 using value_type = std::variant<UnicodeString, Expression, Markup>;
1717 using pointer = value_type*;
1718 using reference =
const value_type&;
1720 friend class Pattern;
1721 Iterator(
const Pattern* p, int32_t i) : pos(i), pat(p) {}
1722 friend bool operator== (
const Iterator& a,
const Iterator& b) {
return (a.pat == b.pat && a.pos == b.pos); }
1735 const PatternPart& part = pat->parts[pos];
1736 return patternContents(part);
1751 friend bool operator!= (
const Iterator& a,
const Iterator& b) {
return !(a == b); }
1755 friend class Builder;
1757 friend class message2::Serializer;
1780 int32_t numParts()
const;
1795 static const std::variant<UnicodeString, Expression, Markup>&
1796 patternContents(
const PatternPart& p) {
return p.piece; }
1953 Binding(
const VariableName& v,
Expression&& e) : var(v), expr(std::move(e)), local(true), annotation(nullptr) {}
1965 swap(b1.var, b2.var);
1966 swap(b1.expr, b2.expr);
1967 swap(b1.local, b2.local);
1968 b1.updateAnnotation();
1969 b2.updateAnnotation();
2001 friend class message2::Checker;
2003 friend class message2::Parser;
2004 friend class message2::Serializer;
2015 const Operator* annotation =
nullptr;
2017 const OptionMap& getOptionsInternal()
const;
2019 bool hasAnnotation()
const {
return !local && (annotation !=
nullptr); }
2020 void updateAnnotation();
2025 #ifndef U_IN_DOXYGEN
2038 U_I18N_API friend inline void swap(Matcher& m1, Matcher& m2)
noexcept {
2049 swap(m1.selectors, m2.selectors);
2050 swap(m1.numSelectors, m2.numSelectors);
2051 swap(m1.variants, m2.variants);
2052 swap(m1.numVariants, m2.numVariants);
2057 friend class MFDataModel;
2059 Matcher(VariableName* ss, int32_t ns, Variant* vs, int32_t nv);
2068 LocalArray<VariableName> selectors;
2070 int32_t numSelectors = 0;
2072 LocalArray<Variant> variants;
2074 int32_t numVariants = 0;
2142 std::vector<Binding> result;
2144 return toStdVector<Binding>(bindings.getAlias(), bindingsLen);
2157 if (std::holds_alternative<Pattern>(body)) {
2160 const Matcher* match = std::get_if<Matcher>(&body);
2162 return toStdVector<VariableName>(match->selectors.getAlias(), match->numSelectors);
2174 if (std::holds_alternative<Pattern>(body)) {
2177 const Matcher* match = std::get_if<Matcher>(&body);
2179 return toStdVector<Variant>(match->variants.getAlias(), match->numVariants);
2227 swap(m1.body, m2.body);
2228 swap(m1.bindings, m2.bindings);
2229 swap(m1.bindingsLen, m2.bindingsLen);
2262 friend class MFDataModel;
2264 void checkDuplicate(
const VariableName&,
UErrorCode&)
const;
2266 bool hasPattern =
true;
2267 bool hasSelectors =
false;
2270 UVector* selectors =
nullptr;
2271 UVector* variants =
nullptr;
2272 UVector* bindings =
nullptr;
2371 friend class message2::Checker;
2373 friend class message2::Serializer;
2377 bool hasPattern()
const {
return std::holds_alternative<Pattern>(body); }
2383 std::variant<Matcher, Pattern> body;
2387 int32_t bindingsLen = 0;
2389 const Binding* getLocalVariablesInternal()
const;
2390 const VariableName* getSelectorsInternal()
const;
2391 const Variant* getVariantsInternal()
const;
2393 int32_t numSelectors()
const {
2394 const Matcher* matcher = std::get_if<Matcher>(&body);
2395 return (matcher ==
nullptr ? 0 : matcher->numSelectors);
2397 int32_t numVariants()
const {
2398 const Matcher* matcher = std::get_if<Matcher>(&body);
2399 return (matcher ==
nullptr ? 0 : matcher->numVariants);
2403 void initBindings(
const Binding*);
2405 MFDataModel(
const Builder& builder,
UErrorCode&)
noexcept;
2412#if defined(U_REAL_MSVC)
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
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...
A Binding pairs a variable name with an expression.
UBool isLocal() const
Returns true if and only if this binding represents a local declaration.
Binding(const Binding &other)
Copy constructor.
const VariableName & getVariable() const
Accesses the left-hand side of the binding.
Binding()
Default constructor.
Binding & operator=(Binding) noexcept
Copy assignment operator.
static Binding input(UnicodeString &&variableName, Expression &&rhs, UErrorCode &errorCode)
Constructor for input binding.
const Expression & getValue() const
Accesses the right-hand side of a binding.
Binding(const VariableName &v, Expression &&e)
Constructor.
virtual ~Binding()
Destructor.
friend void swap(Binding &b1, Binding &b2) noexcept
Non-member swap function.
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...
U_I18N_API std::vector< Option > getAttributes() const
Gets the attributes of this expression.
U_I18N_API Expression()
Default constructor.
U_I18N_API friend void swap(Expression &e1, Expression &e2) noexcept
Non-member swap function.
U_I18N_API const Operator * getOperator(UErrorCode &status) const
Accesses the function annotating this expression.
U_I18N_API const Operand & getOperand() const
Accesses the operand of this expression.
U_I18N_API Expression & operator=(Expression) noexcept
Assignment operator.
virtual U_I18N_API ~Expression()
Destructor.
U_I18N_API UBool isStandaloneAnnotation() const
Checks if this expression is an annotation with no operand.
U_I18N_API UBool isFunctionCall() const
Checks if this expression has a function annotation (with or without an operand).
U_I18N_API Expression(const Expression &other)
Copy constructor.
The Key class corresponds to the key nonterminal in the MessageFormat 2 grammar, https://github....
U_I18N_API const Literal & asLiteral() const
Returns the contents of this key as a literal.
U_I18N_API bool operator==(const Key &other) const
Equality operator.
U_I18N_API bool operator<(const Key &other) const
Less than operator.
virtual U_I18N_API ~Key()
Destructor.
U_I18N_API UBool isWildcard() const
Determines if this is a wildcard key.
U_I18N_API Key(const Literal &lit)
Literal key constructor.
U_I18N_API Key(const Key &other)
Copy constructor.
U_I18N_API friend void swap(Key &k1, Key &k2) noexcept
Non-member swap function.
U_I18N_API Key & operator=(Key) noexcept
Assignment operator.
U_I18N_API Key()
Wildcard constructor; constructs a Key representing the catchall or wildcard key, '*'.
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 MFDataModel::Builder class allows the data model to be constructed incrementally.
Builder & addVariant(SelectorKeys &&keys, Pattern &&pattern, UErrorCode &errorCode) noexcept
Adds a single variant.
Builder(UErrorCode &status)
Default constructor.
Builder & addSelector(VariableName &&selector, UErrorCode &errorCode)
Adds a selector variable.
MFDataModel build(UErrorCode &status) const noexcept
Constructs a new immutable data model.
Builder & setPattern(Pattern &&pattern)
Sets the body of the message as a pattern.
Builder & addBinding(Binding &&b, UErrorCode &status)
Adds a binding, There must not already be a binding with the same name.
virtual ~Builder()
Destructor.
U_I18N_API std::vector< Binding > getLocalVariables() const
Accesses the local variable declarations for this data model.
U_I18N_API MFDataModel(const MFDataModel &other)
Copy constructor.
U_I18N_API MFDataModel & operator=(MFDataModel) noexcept
Assignment operator.
virtual U_I18N_API ~MFDataModel()
Destructor.
U_I18N_API friend void swap(MFDataModel &m1, MFDataModel &m2) noexcept
Non-member swap function.
U_I18N_API std::vector< Variant > getVariants() const
Accesses the variants.
U_I18N_API std::vector< VariableName > getSelectors() const
Accesses the selectors.
U_I18N_API MFDataModel()
Default constructor.
U_I18N_API const Pattern & getPattern() const
Accesses the pattern (in a message without selectors).
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 Operand class corresponds to the operand nonterminal in the MessageFormat 2 grammar,...
virtual U_I18N_API UBool isNull() const
Determines if this operand is the null operand.
U_I18N_API const UnicodeString & asVariable() const
Returns a reference to this operand's variable name.
U_I18N_API const Literal & asLiteral() const
Returns a reference to this operand's literal contents.
U_I18N_API Operand(const Operand &)
Copy constructor.
U_I18N_API UBool isLiteral() const
Determines if this operand represents a literal.
U_I18N_API friend void swap(Operand &o1, Operand &o2) noexcept
Non-member swap function.
U_I18N_API Operand(const UnicodeString &v)
Variable operand constructor.
U_I18N_API UBool isVariable() const
Determines if this operand represents a variable.
U_I18N_API Operand(const Literal &l)
Literal operand constructor.
virtual U_I18N_API Operand & operator=(Operand) noexcept
Assignment operator.
virtual U_I18N_API ~Operand()
Destructor.
U_I18N_API Operand()
Default constructor.
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.
The Operator class corresponds to the FunctionRef type in the Expression interface defined in https:/...
std::vector< Option > getOptions() const
Accesses function options.
Operator(const Operator &other) noexcept
Copy constructor.
virtual ~Operator()
Destructor.
Operator()
Default constructor.
const FunctionName & getFunctionName() const
Accesses the function name.
friend void swap(Operator &o1, Operator &o2) noexcept
Non-member swap function.
Operator & operator=(Operator) noexcept
Assignment operator.
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.
U_I18N_API UBool isMarkup() const
Checks if the part is a markup part.
U_I18N_API PatternPart(const PatternPart &other)
Copy constructor.
virtual U_I18N_API ~PatternPart()
Destructor.
U_I18N_API const UnicodeString & asText() const
Accesses the text contents of the part.
U_I18N_API UBool isExpression() const
Checks if the part is an expression part.
U_I18N_API PatternPart(const UnicodeString &t)
Text part constructor.
U_I18N_API friend void swap(PatternPart &p1, PatternPart &p2) noexcept
Non-member swap function.
U_I18N_API const Expression & contents() const
Accesses the expression of the part.
U_I18N_API PatternPart(Markup &&m)
Markup part constructor.
U_I18N_API const Markup & asMarkup() const
Accesses the expression of the part.
U_I18N_API UBool isText() const
Checks if the part is a text part.
U_I18N_API PatternPart()=default
Default constructor.
U_I18N_API PatternPart(Expression &&e)
Expression part constructor.
U_I18N_API PatternPart & operator=(PatternPart) noexcept
Assignment operator.
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.
A Pattern is a sequence of formattable parts.
U_I18N_API Iterator begin() const
Returns the parts of this pattern.
U_I18N_API friend void swap(Pattern &p1, Pattern &p2) noexcept
Non-member swap function.
U_I18N_API Pattern & operator=(Pattern) noexcept
Assignment operator.
virtual U_I18N_API ~Pattern()
Destructor.
U_I18N_API Iterator end() const
Returns a special value to mark the end of iteration.
U_I18N_API Pattern(const Pattern &other)
Copy constructor.
U_I18N_API Pattern()
Default constructor.
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.
The SelectorKeys class represents the key list for a single variant.
U_I18N_API SelectorKeys(const SelectorKeys &other)
Copy constructor.
U_I18N_API friend void swap(SelectorKeys &s1, SelectorKeys &s2) noexcept
Non-member swap function.
U_I18N_API SelectorKeys()
Default constructor.
U_I18N_API bool operator<(const SelectorKeys &other) const
Less than operator.
U_I18N_API std::vector< Key > getKeys() const
Returns the underlying list of keys.
U_I18N_API SelectorKeys & operator=(SelectorKeys other) noexcept
Assignment operator.
virtual U_I18N_API ~SelectorKeys()
Destructor.
A Variant pairs a list of keys with a pattern It corresponds to the Variant interface defined in http...
Variant(const Variant &)
Copy constructor.
Variant & operator=(Variant other) noexcept
Assignment operator.
Variant(const SelectorKeys &keys, Pattern &&pattern)
Constructor.
virtual ~Variant()
Destructor.
const Pattern & getPattern() const
Accesses the pattern of the variant.
friend void swap(Variant &v1, Variant &v2) noexcept
Non-member swap function.
const SelectorKeys & getKeys() const
Accesses the keys of the variant.
Variant()=default
Default constructor.
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
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_CLASS
Set to export library symbols from inside the i18n library, and to import them from outside,...
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.