6#ifndef MESSAGEFORMAT_DATA_MODEL_H
7#define MESSAGEFORMAT_DATA_MODEL_H
9#if U_SHOW_CPLUSPLUS_API
11#if !UCONFIG_NO_FORMATTING
16#include "unicode/messageformat2_data_model_names.h"
18#ifndef U_HIDE_DEPRECATED_API
37static inline std::vector<T> toStdVector(
const T* arr, int32_t len) {
38 std::vector<T> result;
39 for (int32_t i = 0; i < len; i++) {
40 result.push_back(arr[i]);
45#if defined(U_REAL_MSVC)
49#pragma warning(disable: 4251)
55 class MessageFormatter;
60 namespace data_model {
136 swap(
l1.thisIsQuoted,
l2.thisIsQuoted);
137 swap(
l1.contents,
l2.contents);
195 bool thisIsQuoted =
false;
207#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
211#if defined(U_REAL_MSVC)
224#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
225#if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
227template class U_I18N_API std::_Variant_storage_<false, icu::UnicodeString, icu::message2::data_model::Literal>;
229template class U_I18N_API std::variant<icu::UnicodeString, icu::message2::data_model::Literal>;
230template class U_I18N_API std::optional<std::variant<icu::UnicodeString, icu::message2::data_model::Literal>>;
231template class U_I18N_API std::optional<icu::message2::data_model::Literal>;
238 namespace data_model {
264 UBool isVariable()
const;
273 UBool isLiteral()
const;
282 virtual UBool isNull()
const;
302 const Literal& asLiteral()
const;
320 explicit Operand(
const UnicodeString& v) : contents(VariableName(v)) {}
330 explicit Operand(
const Literal& l) : contents(l) {}
339 friend inline void swap(Operand& o1, Operand& o2)
noexcept {
343 swap(o1.contents, o2.contents);
351 virtual Operand& operator=(Operand)
noexcept;
358 Operand(
const Operand&);
367 std::optional<std::variant<VariableName, Literal>> contents;
395 UBool isWildcard()
const {
return !contents.has_value(); }
405 const Literal& asLiteral()
const;
412 Key(
const Key& other) : contents(other.contents) {}
420 Key() : contents(std::nullopt) {}
430 explicit Key(
const Literal& lit) : contents(lit) {}
439 friend inline void swap(Key& k1, Key& k2)
noexcept {
442 swap(k1.contents, k2.contents);
450 Key& operator=(Key)
noexcept;
464 bool operator<(
const Key& other)
const;
487 std::optional<Literal> contents;
498#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
499template class U_I18N_API LocalPointerBase<message2::data_model::Key>;
500template class U_I18N_API LocalArray<message2::data_model::Key>;
505 namespace data_model {
516 class U_I18N_API SelectorKeys :
public UObject {
528 std::vector<Key> getKeys()
const {
529 return toStdVector<Key>(keys.getAlias(), len);
542 friend class SelectorKeys;
615 SelectorKeys() : len(0) {}
624 friend inline void swap(SelectorKeys&
s1, SelectorKeys&
s2)
noexcept {
627 swap(
s1.len,
s2.len);
628 swap(
s1.keys,
s2.keys);
636 SelectorKeys(
const SelectorKeys&
other);
643 SelectorKeys& operator=(SelectorKeys
other)
noexcept;
650 virtual ~SelectorKeys();
652 friend class Builder;
653 friend class message2::Checker;
655 friend class message2::Serializer;
660 const Key* getKeysInternal()
const;
668 namespace data_model {
729 swap(
o1.name,
o2.name);
730 swap(
o1.rand,
o2.rand);
766#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
773 namespace data_model {
786 swap(
m1.bogus,
m2.bogus);
787 swap(
m1.options,
m2.options);
788 swap(
m1.len,
m2.len);
790 OptionMap() : len(0) {}
791 OptionMap(
const OptionMap&);
792 OptionMap& operator=(OptionMap);
793 std::vector<Option> getOptions()
const {
794 return toStdVector<Option>(options.getAlias(), len);
797 OptionMap(Option*, int32_t);
798 virtual ~OptionMap();
803 bool checkDuplicates =
true;
810 friend inline void swap(Builder& m1, Builder& m2)
noexcept {
813 swap(m1.options, m2.options);
814 swap(m1.checkDuplicates, m2.checkDuplicates);
817 Builder(
const Builder&) =
delete;
818 Builder& operator=(Builder)
noexcept;
822 friend class message2::Serializer;
825 LocalArray<Option> options;
838 namespace data_model {
862 const FunctionName& getFunctionName()
const;
871 std::vector<Option> getOptions()
const {
872 return options.getOptions();
885 friend class Operator;
887 OptionMap::Builder options;
957 Operator(
const Operator&
other)
noexcept;
966 friend inline void swap(Operator&
o1, Operator&
o2)
noexcept {
969 swap(
o1.name,
o2.name);
970 swap(
o1.options,
o2.options);
978 Operator& operator=(Operator)
noexcept;
995 friend class Binding;
996 friend class Builder;
997 friend class message2::Checker;
999 friend class message2::Serializer;
1004 const OptionMap& getOptionsInternal()
const;
1021#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1022template class U_I18N_API std::optional<icu::message2::data_model::Operator>;
1029 namespace data_model {
1031 typedef enum UMarkupType {
1094 std::vector<Option>
getOptions()
const {
return options.getOptions(); }
1133 OptionMap::Builder options;
1134 OptionMap::Builder attributes;
1135 UMarkupType type = UMARKUP_COUNT;
1241 friend class message2::Serializer;
1247 const OptionMap& getOptionsInternal()
const {
return options; }
1248 const OptionMap& getAttributesInternal()
const {
return attributes; }
1335 bool hasOperand =
false;
1336 bool hasOperator =
false;
1339 OptionMap::Builder attributes;
1424 swap(
e1.rator,
e2.rator);
1425 swap(
e1.rand,
e2.rand);
1426 swap(
e1.attributes,
e2.attributes);
1458 friend class message2::Serializer;
1477 std::optional<Operator> rator;
1480 const OptionMap& getAttributesInternal()
const {
return attributes; }
1491#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1492template class U_I18N_API LocalPointerBase<message2::data_model::Expression>;
1493template class U_I18N_API LocalArray<message2::data_model::Expression>;
1498 namespace data_model {
1523 UBool isText()
const {
return std::holds_alternative<UnicodeString>(piece); }
1583 swap(
p1.piece,
p2.piece);
1645 friend class Pattern;
1647 std::variant<UnicodeString, Expression, Markup> piece;
1658#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1665 namespace data_model {
1678 friend class PatternPart;
1691 Iterator begin()
const {
1692 return Iterator(
this, 0);
1703 Iterator end()
const {
1704 return Iterator(
this, len);
1717 friend class Pattern;
1807 friend inline void swap(Pattern&
p1, Pattern&
p2)
noexcept {
1810 swap(
p1.bogus,
p2.bogus);
1811 swap(
p1.len,
p2.len);
1812 swap(
p1.parts,
p2.parts);
1820 Pattern(
const Pattern&
other);
1827 Pattern& operator=(Pattern)
noexcept;
1847 using iterator_category = std::forward_iterator_tag;
1848 using difference_type = std::ptrdiff_t;
1849 using value_type = std::variant<UnicodeString, Expression, Markup>;
1850 using pointer = value_type*;
1851 using reference =
const value_type&;
1853 friend class Pattern;
1869 return patternContents(
part);
1888 friend class Builder;
1890 friend class message2::Serializer;
1903 static void initParts(Pattern&,
const Pattern&);
1928 static const std::variant<UnicodeString, Expression, Markup>&
1929 patternContents(
const PatternPart& p) {
return p.piece; }
1952 const Pattern& getPattern()
const {
return p; }
1961 const SelectorKeys& getKeys()
const {
return k; }
1973 Variant(
const SelectorKeys& keys, Pattern&& pattern) : k(keys), p(std::move(pattern)) {}
1982 friend inline void swap(Variant& v1, Variant& v2)
noexcept {
1994 Variant& operator=(Variant other)
noexcept;
2002 Variant() =
default;
2009 Variant(
const Variant&);
2023 namespace data_model {
2044 const Expression& getValue()
const;
2053 const VariableName& getVariable()
const {
return var; }
2068 static Binding input(UnicodeString&& variableName, Expression&& rhs,
UErrorCode& errorCode);
2076 UBool isLocal()
const {
return local; }
2086 Binding(
const VariableName& v, Expression&& e) : var(v), expr(std::move(e)), local(true), annotation(nullptr) {}
2095 friend inline void swap(Binding& b1, Binding& b2)
noexcept {
2098 swap(b1.var, b2.var);
2099 swap(b1.expr, b2.expr);
2100 swap(b1.local, b2.local);
2101 b1.updateAnnotation();
2102 b2.updateAnnotation();
2110 Binding(
const Binding& other);
2117 Binding& operator=(Binding)
noexcept;
2125 Binding() : local(true) {}
2134 friend class message2::Checker;
2135 friend class message2::MessageFormatter;
2136 friend class message2::Parser;
2137 friend class message2::Serializer;
2148 const Operator* annotation =
nullptr;
2150 const OptionMap& getOptionsInternal()
const;
2152 bool hasAnnotation()
const {
return !local && (annotation !=
nullptr); }
2153 void updateAnnotation();
2164#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
2165template class U_I18N_API LocalPointerBase<message2::data_model::Variant>;
2166template class U_I18N_API LocalPointerBase<message2::data_model::Binding>;
2167template class U_I18N_API LocalArray<message2::data_model::Variant>;
2168template class U_I18N_API LocalArray<message2::data_model::Binding>;
2173 using namespace data_model;
2180 #ifndef U_IN_DOXYGEN
2181 class Matcher :
public UObject {
2183 Matcher& operator=(Matcher);
2184 Matcher(
const Matcher&);
2193 friend inline void swap(Matcher& m1, Matcher& m2)
noexcept {
2204 swap(m1.selectors, m2.selectors);
2205 swap(m1.numSelectors, m2.numSelectors);
2206 swap(m1.variants, m2.variants);
2207 swap(m1.numVariants, m2.numVariants);
2212 friend class MFDataModel;
2214 Matcher(Expression* ss, int32_t ns, Variant* vs, int32_t nv);
2223 LocalArray<Expression> selectors;
2225 int32_t numSelectors = 0;
2227 LocalArray<Variant> variants;
2229 int32_t numVariants = 0;
2242#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
2243#if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
2244template class U_I18N_API std::_Variant_storage_<false, icu::message2::Matcher,icu::message2::data_model::Pattern>;
2246template class U_I18N_API std::variant<icu::message2::Matcher,icu::message2::data_model::Pattern>;
2272 class U_I18N_API MFDataModel :
public UMemory {
2316 std::vector<Binding> getLocalVariables()
const {
2317 std::vector<Binding> result;
2319 return toStdVector<Binding>(bindings.getAlias(), bindingsLen);
2331 const std::vector<Expression> getSelectors()
const {
2332 if (std::holds_alternative<Pattern>(body)) {
2335 const Matcher* match = std::get_if<Matcher>(&body);
2337 return toStdVector<Expression>(match->selectors.getAlias(), match->numSelectors);
2347 std::vector<Variant> getVariants()
const {
2349 if (std::holds_alternative<Pattern>(body)) {
2352 const Matcher* match = std::get_if<Matcher>(&body);
2354 return toStdVector<Variant>(match->variants.getAlias(), match->numVariants);
2366 const Pattern& getPattern()
const;
2393 friend inline void swap(MFDataModel& m1, MFDataModel& m2)
noexcept {
2404 swap(m1.body, m2.body);
2405 swap(m1.bindings, m2.bindings);
2406 swap(m1.bindingsLen, m2.bindingsLen);
2414 MFDataModel& operator=(MFDataModel)
noexcept;
2421 MFDataModel(
const MFDataModel& other);
2428 virtual ~MFDataModel();
2439 friend class MFDataModel;
2443 bool hasPattern =
true;
2444 bool hasSelectors =
false;
2548 friend class Checker;
2550 friend class Serializer;
2554 bool hasPattern()
const {
return std::holds_alternative<Pattern>(body); }
2560 std::variant<Matcher, Pattern> body;
2566 const Binding* getLocalVariablesInternal()
const;
2567 const Expression* getSelectorsInternal()
const;
2568 const Variant* getVariantsInternal()
const;
2570 int32_t numSelectors()
const {
2571 const Matcher* matcher = std::get_if<Matcher>(&body);
2572 return (matcher ==
nullptr ? 0 : matcher->numSelectors);
2575 const Matcher* matcher = std::get_if<Matcher>(&body);
2576 return (matcher ==
nullptr ? 0 : matcher->numVariants);
2580 void initBindings(
const Binding*);
2582 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 & 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 & addSelector(Expression &&selector, UErrorCode &errorCode) noexcept
Adds a selector expression.
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.