messageformat2_data_model.h
// © 2024 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
#include "unicode/utypes.h"
#ifndef MESSAGEFORMAT_DATA_MODEL_H
#define MESSAGEFORMAT_DATA_MODEL_H
#if U_SHOW_CPLUSPLUS_API
#if !UCONFIG_NO_FORMATTING
#if !UCONFIG_NO_MF2
#include "unicode/localpointer.h"
#include "unicode/messageformat2_data_model_names.h"
#ifndef U_HIDE_DEPRECATED_API
#include <algorithm>
#include <cstddef>
#include <iterator>
#include <optional>
#include <variant>
#include <vector>
U_NAMESPACE_BEGIN
class UVector;
// Helpers
// Note: this _must_ be declared `inline` or else gcc will generate code
// for its instantiations, which needs to be avoided because it returns
// a std::vector
template<typename T>
static inline std::vector<T> toStdVector(const T* arr, int32_t len) {
std::vector<T> result;
for (int32_t i = 0; i < len; i++) {
result.push_back(arr[i]);
}
return result;
}
#if defined(U_REAL_MSVC)
#pragma warning(push)
// Ignore warning 4251 as these templates are instantiated later in this file,
// after the classes used to instantiate them have been defined.
#pragma warning(disable: 4251)
#endif
namespace message2 {
class Checker;
class MFDataModel;
class MessageFormatter;
class Parser;
class Serializer;
namespace data_model {
class Binding;
class Literal;
class Operator;
class U_I18N_API Literal : public UObject {
public:
UnicodeString quoted() const;
const UnicodeString& unquoted() const;
UBool isQuoted() const { return thisIsQuoted; }
Literal(UBool q, const UnicodeString& s) : thisIsQuoted(q), contents(s) {}
Literal(const Literal& other) : thisIsQuoted(other.thisIsQuoted), contents(other.contents) {}
friend inline void swap(Literal& l1, Literal& l2) noexcept {
using std::swap;
swap(l1.thisIsQuoted, l2.thisIsQuoted);
swap(l1.contents, l2.contents);
}
Literal& operator=(Literal) noexcept;
Literal() = default;
bool operator<(const Literal& other) const;
bool operator==(const Literal& other) const;
virtual ~Literal();
private:
/* const */ bool thisIsQuoted = false;
/* const */ UnicodeString contents;
};
} // namespace data_model
} // namespace message2
// Export an explicit template instantiation of the LocalPointer that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API LocalPointerBase<message2::data_model::Literal>;
template class U_I18N_API LocalArray<message2::data_model::Literal>;
#endif
#if defined(U_REAL_MSVC)
#pragma warning(pop)
#endif
U_NAMESPACE_END
// Export an explicit template instantiation of the std::variants and std::optionals
// that are used as a data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
#if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
struct U_I18N_API std::_Nontrivial_dummy_type;
template class U_I18N_API std::_Variant_storage_<false, icu::UnicodeString, icu::message2::data_model::Literal>;
#endif
template class U_I18N_API std::variant<icu::UnicodeString, icu::message2::data_model::Literal>;
template class U_I18N_API std::optional<std::variant<icu::UnicodeString, icu::message2::data_model::Literal>>;
template class U_I18N_API std::optional<icu::message2::data_model::Literal>;
#endif
U_NAMESPACE_BEGIN
namespace message2 {
namespace data_model {
class U_I18N_API Operand : public UObject {
public:
UBool isVariable() const;
UBool isLiteral() const;
virtual UBool isNull() const;
const UnicodeString& asVariable() const;
const Literal& asLiteral() const;
Operand() : contents(std::nullopt) {}
explicit Operand(const UnicodeString& v) : contents(VariableName(v)) {}
explicit Operand(const Literal& l) : contents(l) {}
friend inline void swap(Operand& o1, Operand& o2) noexcept {
using std::swap;
(void) o1;
(void) o2;
swap(o1.contents, o2.contents);
}
virtual Operand& operator=(Operand) noexcept;
Operand(const Operand&);
virtual ~Operand();
private:
std::optional<std::variant<VariableName, Literal>> contents;
}; // class Operand
class U_I18N_API Key : public UObject {
public:
UBool isWildcard() const { return !contents.has_value(); }
const Literal& asLiteral() const;
Key(const Key& other) : contents(other.contents) {}
Key() : contents(std::nullopt) {}
explicit Key(const Literal& lit) : contents(lit) {}
friend inline void swap(Key& k1, Key& k2) noexcept {
using std::swap;
swap(k1.contents, k2.contents);
}
Key& operator=(Key) noexcept;
bool operator<(const Key& other) const;
bool operator==(const Key& other) const;
virtual ~Key();
private:
/* const */ std::optional<Literal> contents;
}; // class Key
} // namespace data_model
} // namespace message2
// Export an explicit template instantiation of the LocalPointer that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API LocalPointerBase<message2::data_model::Key>;
template class U_I18N_API LocalArray<message2::data_model::Key>;
#endif
namespace message2 {
namespace data_model {
class U_I18N_API SelectorKeys : public UObject {
public:
std::vector<Key> getKeys() const {
return toStdVector<Key>(keys.getAlias(), len);
}
class U_I18N_API Builder : public UMemory {
private:
friend class SelectorKeys;
UVector* keys; // This is a raw pointer and not a LocalPointer<UVector> to avoid undefined behavior warnings,
// since UVector is forward-declared
// The vector owns its elements
public:
Builder& add(Key&& key, UErrorCode& status) noexcept;
SelectorKeys build(UErrorCode& status) const;
Builder(UErrorCode& status);
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class SelectorKeys::Builder
bool operator<(const SelectorKeys& other) const;
SelectorKeys() : len(0) {}
friend inline void swap(SelectorKeys& s1, SelectorKeys& s2) noexcept {
using std::swap;
swap(s1.len, s2.len);
swap(s1.keys, s2.keys);
}
SelectorKeys(const SelectorKeys& other);
SelectorKeys& operator=(SelectorKeys other) noexcept;
virtual ~SelectorKeys();
private:
friend class Builder;
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Serializer;
/* const */ LocalArray<Key> keys;
/* const */ int32_t len;
const Key* getKeysInternal() const;
SelectorKeys(const UVector& ks, UErrorCode& status);
}; // class SelectorKeys
} // namespace data_model
namespace data_model {
class Operator;
class U_I18N_API Option : public UObject {
public:
const Operand& getValue() const { return rand; }
const UnicodeString& getName() const { return name; }
Option(const UnicodeString& n, Operand&& r) : name(n), rand(std::move(r)) {}
Option() {}
friend inline void swap(Option& o1, Option& o2) noexcept {
using std::swap;
swap(o1.name, o2.name);
swap(o1.rand, o2.rand);
}
Option(const Option& other);
Option& operator=(Option other) noexcept;
virtual ~Option();
private:
/* const */ UnicodeString name;
/* const */ Operand rand;
}; // class Option
} // namespace data_model
} // namespace message2
// Export an explicit template instantiation of the LocalPointer that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API LocalPointerBase<message2::data_model::Option>;
template class U_I18N_API LocalArray<message2::data_model::Option>;
#endif
namespace message2 {
namespace data_model {
// Internal only
#ifndef U_IN_DOXYGEN
// Options
// This is a wrapper class around a vector of options that provides lookup operations
class U_I18N_API OptionMap : public UObject {
public:
int32_t size() const;
// Needs to take an error code b/c an earlier copy might have failed
const Option& getOption(int32_t, UErrorCode&) const;
friend inline void swap(OptionMap& m1, OptionMap& m2) noexcept {
using std::swap;
swap(m1.bogus, m2.bogus);
swap(m1.options, m2.options);
swap(m1.len, m2.len);
}
OptionMap() : len(0) {}
OptionMap(const OptionMap&);
OptionMap& operator=(OptionMap);
std::vector<Option> getOptions() const {
return toStdVector<Option>(options.getAlias(), len);
}
OptionMap(const UVector&, UErrorCode&);
OptionMap(Option*, int32_t);
virtual ~OptionMap();
class U_I18N_API Builder : public UObject {
private:
UVector* options;
bool checkDuplicates = true;
public:
Builder& add(Option&& opt, UErrorCode&);
Builder(UErrorCode&);
static Builder attributes(UErrorCode&);
// As this class is private, build() is destructive
OptionMap build(UErrorCode&);
friend inline void swap(Builder& m1, Builder& m2) noexcept {
using std::swap;
swap(m1.options, m2.options);
swap(m1.checkDuplicates, m2.checkDuplicates);
}
Builder(Builder&&);
Builder(const Builder&) = delete;
Builder& operator=(Builder) noexcept;
virtual ~Builder();
}; // class OptionMap::Builder
private:
friend class message2::Serializer;
bool bogus = false;
LocalArray<Option> options;
int32_t len;
}; // class OptionMap
#endif
} // namespace data_model
} // namespace message2
U_NAMESPACE_END
U_NAMESPACE_BEGIN
namespace message2 {
namespace data_model {
class U_I18N_API Operator : public UObject {
public:
const FunctionName& getFunctionName() const;
std::vector<Option> getOptions() const {
return options.getOptions();
}
class U_I18N_API Builder : public UMemory {
private:
friend class Operator;
FunctionName functionName;
OptionMap::Builder options;
public:
Builder& setFunctionName(FunctionName&& func);
Builder& addOption(const UnicodeString &key, Operand&& value, UErrorCode& status) noexcept;
Operator build(UErrorCode& status);
Builder(UErrorCode& status);
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Operator::Builder
Operator(const Operator& other) noexcept;
friend inline void swap(Operator& o1, Operator& o2) noexcept {
using std::swap;
swap(o1.name, o2.name);
swap(o1.options, o2.options);
}
Operator& operator=(Operator) noexcept;
Operator() {}
virtual ~Operator();
private:
friend class Binding;
friend class Builder;
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Serializer;
// Function call constructor
Operator(const FunctionName& f, const UVector& options, UErrorCode&);
const OptionMap& getOptionsInternal() const;
Operator(const FunctionName&, const OptionMap&);
/* const */ FunctionName name;
/* const */ OptionMap options;
}; // class Operator
} // namespace data_model
} // namespace message2
U_NAMESPACE_END
// Export an explicit template instantiation of the std::optional that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API std::optional<icu::message2::data_model::Operator>;
#endif
U_NAMESPACE_BEGIN
namespace message2 {
namespace data_model {
// Internal only
typedef enum UMarkupType {
UMARKUP_OPEN = 0,
UMARKUP_CLOSE,
UMARKUP_STANDALONE,
UMARKUP_COUNT
} UMarkupType;
class U_I18N_API Markup : public UObject {
public:
UBool isOpen() const { return (type == UMARKUP_OPEN); }
UBool isClose() const { return (type == UMARKUP_CLOSE); }
UBool isStandalone() const { return (type == UMARKUP_STANDALONE); }
const UnicodeString& getName() const { return name; }
std::vector<Option> getOptions() const { return options.getOptions(); }
std::vector<Option> getAttributes() const { return attributes.getOptions(); }
Markup() {}
virtual ~Markup();
class U_I18N_API Builder : public UMemory {
private:
friend class Markup;
UnicodeString name;
OptionMap::Builder options;
OptionMap::Builder attributes;
UMarkupType type = UMARKUP_COUNT;
public:
Builder& setName(const UnicodeString& n) { name = n; return *this; }
Builder& setOpen() { type = UMARKUP_OPEN; return *this; }
Builder& setClose() { type = UMARKUP_CLOSE; return *this; }
Builder& setStandalone() { type = UMARKUP_STANDALONE; return *this; }
Builder& addOption(const UnicodeString &key, Operand&& value, UErrorCode& status);
Builder& addAttribute(const UnicodeString &key, Operand&& value, UErrorCode& status);
Markup build(UErrorCode& status);
Builder(UErrorCode& status);
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Markup::Builder
private:
friend class Builder;
friend class message2::Serializer;
UMarkupType type;
UnicodeString name;
OptionMap options;
OptionMap attributes;
const OptionMap& getOptionsInternal() const { return options; }
const OptionMap& getAttributesInternal() const { return attributes; }
Markup(UMarkupType, UnicodeString, OptionMap&&, OptionMap&&);
}; // class Markup
class U_I18N_API Expression : public UObject {
public:
UBool isStandaloneAnnotation() const;
UBool isFunctionCall() const;
const Operator* getOperator(UErrorCode& status) const;
const Operand& getOperand() const;
std::vector<Option> getAttributes() const { return attributes.getOptions(); }
class U_I18N_API Builder : public UMemory {
private:
friend class Expression;
bool hasOperand = false;
bool hasOperator = false;
Operand rand;
Operator rator;
OptionMap::Builder attributes;
public:
Builder& setOperand(Operand&& rAnd);
Builder& setOperator(Operator&& rAtor);
Builder& addAttribute(const UnicodeString &key, Operand&& value, UErrorCode& status);
Expression build(UErrorCode& status);
Builder(UErrorCode& status);
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Expression::Builder
friend inline void swap(Expression& e1, Expression& e2) noexcept {
using std::swap;
swap(e1.rator, e2.rator);
swap(e1.rand, e2.rand);
swap(e1.attributes, e2.attributes);
}
Expression(const Expression& other);
Expression& operator=(Expression) noexcept;
Expression();
virtual ~Expression();
private:
friend class message2::Serializer;
/*
Internally, an expression is represented as the application of an optional operator to an operand.
The operand is always present; for function calls with no operand, it's represented
as an operand for which `isNull()` is true.
Operator | Operand
--------------------------------
{ |42| :fun opt=value } => (FunctionName=fun, | Literal(quoted=true, contents="42")
options={opt: value})
{ abcd } => null | Literal(quoted=false, contents="abcd")
{ : fun opt=value } => (FunctionName=fun,
options={opt: value}) | NullOperand()
*/
Expression(const Operator &rAtor, const Operand &rAnd, const OptionMap& attrs) : rator(rAtor), rand(rAnd), attributes(attrs) {}
Expression(const Operand &rAnd, const OptionMap& attrs) : rator(std::nullopt), rand(Operand(rAnd)), attributes(attrs) {}
Expression(const Operator &rAtor, const OptionMap& attrs) : rator(rAtor), rand(), attributes(attrs) {}
/* const */ std::optional<Operator> rator;
/* const */ Operand rand;
/* const */ OptionMap attributes;
const OptionMap& getAttributesInternal() const { return attributes; }
}; // class Expression
} // namespace data_model
} // namespace message2
// Export an explicit template instantiation of the LocalPointer that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API LocalPointerBase<message2::data_model::Expression>;
template class U_I18N_API LocalArray<message2::data_model::Expression>;
#endif
namespace message2 {
namespace data_model {
class Pattern;
// Despite the comments, `PatternPart` is internal-only
class PatternPart : public UObject {
public:
UBool isText() const { return std::holds_alternative<UnicodeString>(piece); }
UBool isMarkup() const { return std::holds_alternative<Markup>(piece); }
UBool isExpression() const { return std::holds_alternative<Expression>(piece); }
const Expression& contents() const;
const Markup& asMarkup() const;
const UnicodeString& asText() const;
friend inline void swap(PatternPart& p1, PatternPart& p2) noexcept {
using std::swap;
swap(p1.piece, p2.piece);
}
PatternPart(const PatternPart& other);
PatternPart& operator=(PatternPart) noexcept;
virtual ~PatternPart();
explicit PatternPart(const UnicodeString& t) : piece(t) {}
explicit PatternPart(Expression&& e) : piece(e) {}
explicit PatternPart(Markup&& m) : piece(m) {}
PatternPart() = default;
private:
friend class Pattern;
std::variant<UnicodeString, Expression, Markup> piece;
}; // class PatternPart
} // namespace data_model
} // namespace message2
// Export an explicit template instantiation of the LocalPointer that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API LocalPointerBase<message2::data_model::PatternPart>;
template class U_I18N_API LocalArray<message2::data_model::PatternPart>;
#endif
namespace message2 {
namespace data_model {
class U_I18N_API Pattern : public UObject {
private:
friend class PatternPart;
public:
struct Iterator;
Iterator begin() const {
return Iterator(this, 0);
}
Iterator end() const {
return Iterator(this, len);
}
class U_I18N_API Builder : public UMemory {
private:
friend class Pattern;
UVector* parts; // Not a LocalPointer for the same reason as in `SelectorKeys::Builder`
public:
Builder& add(Expression&& part, UErrorCode& status) noexcept;
Builder& add(Markup&& part, UErrorCode& status) noexcept;
Builder& add(UnicodeString&& part, UErrorCode& status) noexcept;
Pattern build(UErrorCode& status) const noexcept;
Builder(UErrorCode& status);
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Pattern::Builder
Pattern() : parts(LocalArray<PatternPart>()) {}
friend inline void swap(Pattern& p1, Pattern& p2) noexcept {
using std::swap;
swap(p1.bogus, p2.bogus);
swap(p1.len, p2.len);
swap(p1.parts, p2.parts);
}
Pattern(const Pattern& other);
Pattern& operator=(Pattern) noexcept;
virtual ~Pattern();
struct U_I18N_API Iterator {
private:
using iterator_category = std::forward_iterator_tag;
using difference_type = std::ptrdiff_t;
using value_type = std::variant<UnicodeString, Expression, Markup>;
using pointer = value_type*;
using reference = const value_type&;
friend class Pattern;
Iterator(const Pattern* p, int32_t i) : pos(i), pat(p) {}
friend bool operator== (const Iterator& a, const Iterator& b) { return (a.pat == b.pat && a.pos == b.pos); }
int32_t pos;
const Pattern* pat;
public:
reference operator*() const {
const PatternPart& part = pat->parts[pos];
return patternContents(part);
}
Iterator operator++() { pos++; return *this; }
friend bool operator!= (const Iterator& a, const Iterator& b) { return !(a == b); }
}; // struct Iterator
private:
friend class Builder;
friend class message2::MessageFormatter;
friend class message2::Serializer;
// Set to true if a copy constructor fails;
// needed in order to distinguish an uninitialized
// Pattern from a 0-length pattern
bool bogus = false;
// Possibly-empty array of parts
int32_t len = 0;
LocalArray<PatternPart> parts;
Pattern(const UVector& parts, UErrorCode& status);
// Helper
static void initParts(Pattern&, const Pattern&);
int32_t numParts() const;
const PatternPart& getPart(int32_t i) const;
// Gets around not being able to declare Pattern::Iterator as a friend
// in PatternPart
static const std::variant<UnicodeString, Expression, Markup>&
patternContents(const PatternPart& p) { return p.piece; }
}; // class Pattern
class U_I18N_API Variant : public UObject {
public:
const Pattern& getPattern() const { return p; }
const SelectorKeys& getKeys() const { return k; }
Variant(const SelectorKeys& keys, Pattern&& pattern) : k(keys), p(std::move(pattern)) {}
friend inline void swap(Variant& v1, Variant& v2) noexcept {
using std::swap;
swap(v1.k, v2.k);
swap(v1.p, v2.p);
}
Variant& operator=(Variant other) noexcept;
Variant() = default;
Variant(const Variant&);
virtual ~Variant();
private:
/* const */ SelectorKeys k;
/* const */ Pattern p;
}; // class Variant
} // namespace data_model
namespace data_model {
class U_I18N_API Binding : public UObject {
public:
const Expression& getValue() const;
const VariableName& getVariable() const { return var; }
static Binding input(UnicodeString&& variableName, Expression&& rhs, UErrorCode& errorCode);
UBool isLocal() const { return local; }
Binding(const VariableName& v, Expression&& e) : var(v), expr(std::move(e)), local(true), annotation(nullptr) {}
friend inline void swap(Binding& b1, Binding& b2) noexcept {
using std::swap;
swap(b1.var, b2.var);
swap(b1.expr, b2.expr);
swap(b1.local, b2.local);
b1.updateAnnotation();
b2.updateAnnotation();
}
Binding(const Binding& other);
Binding& operator=(Binding) noexcept;
Binding() : local(true) {}
virtual ~Binding();
private:
friend class message2::Checker;
friend class message2::MessageFormatter;
friend class message2::Parser;
friend class message2::Serializer;
/* const */ VariableName var;
/* const */ Expression expr;
/* const */ bool local;
// The following field is always nullptr for a local
// declaration, and possibly nullptr for an .input declaration
// If non-null, the referent is a member of `expr` so
// its lifetime is the same as the lifetime of the enclosing Binding
// (as long as there's no mutation)
const Operator* annotation = nullptr;
const OptionMap& getOptionsInternal() const;
bool hasAnnotation() const { return !local && (annotation != nullptr); }
void updateAnnotation();
}; // class Binding
} // namespace data_model
} // namespace message2
// Export an explicit template instantiation of the LocalPointer that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
template class U_I18N_API LocalPointerBase<message2::data_model::Variant>;
template class U_I18N_API LocalPointerBase<message2::data_model::Binding>;
template class U_I18N_API LocalArray<message2::data_model::Variant>;
template class U_I18N_API LocalArray<message2::data_model::Binding>;
#endif
namespace message2 {
using namespace data_model;
// Internal only
class MFDataModel;
#ifndef U_IN_DOXYGEN
class Matcher : public UObject {
public:
Matcher& operator=(Matcher);
Matcher(const Matcher&);
friend inline void swap(Matcher& m1, Matcher& m2) noexcept {
using std::swap;
if (m1.bogus) {
m2.bogus = true;
return;
}
if (m2.bogus) {
m1.bogus = true;
return;
}
swap(m1.selectors, m2.selectors);
swap(m1.numSelectors, m2.numSelectors);
swap(m1.variants, m2.variants);
swap(m1.numVariants, m2.numVariants);
}
virtual ~Matcher();
private:
friend class MFDataModel;
Matcher(Expression* ss, int32_t ns, Variant* vs, int32_t nv);
Matcher() {}
// A Matcher may have numSelectors=0 and numVariants=0
// (this is a data model error, but it's representable).
// So we have to keep a separate flag to track failed copies.
bool bogus = false;
// The expressions that are being matched on.
LocalArray<Expression> selectors;
// The number of selectors
int32_t numSelectors = 0;
// The list of `when` clauses (case arms).
LocalArray<Variant> variants;
// The number of variants
int32_t numVariants = 0;
}; // class Matcher
#endif
} // namespace message2
U_NAMESPACE_END
// Export an explicit template instantiation of the std::variant that is used as a
// data member of various MFDataModel classes.
// (When building DLLs for Windows this is required.)
// (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
// for similar examples.)
#if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
#if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
template class U_I18N_API std::_Variant_storage_<false, icu::message2::Matcher,icu::message2::data_model::Pattern>;
#endif
template class U_I18N_API std::variant<icu::message2::Matcher,icu::message2::data_model::Pattern>;
#endif
U_NAMESPACE_BEGIN
namespace message2 {
// -----------------------------------------------------------------------
// Public MFDataModel class
class U_I18N_API MFDataModel : public UMemory {
/*
Classes that represent nodes in the data model are nested inside the
`MFDataModel` class.
Classes such as `Expression`, `Pattern` and `VariantMap` are immutable and
are constructed using the builder pattern.
Most classes representing nodes have copy constructors. This is because builders
contain immutable data that must be copied when calling `build()`, since the builder
could go out of scope before the immutable result of the builder does. Copying is
also necessary to prevent unexpected mutation if intermediate builders are saved
and mutated again after calling `build()`.
The copy constructors perform a deep copy, for example by copying the entire
list of options for an `Operator` (and copying the entire underlying vector.)
Some internal fields should be `const`, but are declared as non-`const` to make
the copy constructor simpler to implement. (These are noted throughout.) In
other words, those fields are `const` except during the execution of a copy
constructor.
On the other hand, intermediate `Builder` methods that return a `Builder&`
mutate the state of the builder, so in code like:
Expression::Builder& exprBuilder = Expression::builder()-> setOperand(foo);
Expression::Builder& exprBuilder2 = exprBuilder.setOperator(bar);
the call to `setOperator()` would mutate `exprBuilder`, since `exprBuilder`
and `exprBuilder2` are references to the same object.
An alternate choice would be to make `build()` destructive, so that copying would
be unnecessary. Or, both copying and moving variants of `build()` could be
provided. Copying variants of the intermediate `Builder` methods could be
provided as well, if this proved useful.
*/
public:
std::vector<Binding> getLocalVariables() const {
std::vector<Binding> result;
if (!bogus) {
return toStdVector<Binding>(bindings.getAlias(), bindingsLen);
}
return {};
}
const std::vector<Expression> getSelectors() const {
if (std::holds_alternative<Pattern>(body)) {
return {};
}
const Matcher* match = std::get_if<Matcher>(&body);
// match must be non-null, given the previous check
return toStdVector<Expression>(match->selectors.getAlias(), match->numSelectors);
}
std::vector<Variant> getVariants() const {
// Return empty vector if no variants
if (std::holds_alternative<Pattern>(body)) {
return {};
}
const Matcher* match = std::get_if<Matcher>(&body);
// match must be non-null, given the previous check
return toStdVector<Variant>(match->variants.getAlias(), match->numVariants);
return {};
}
const Pattern& getPattern() const;
class U_I18N_API Builder;
MFDataModel();
friend inline void swap(MFDataModel& m1, MFDataModel& m2) noexcept {
using std::swap;
if (m1.bogus) {
m2.bogus = true;
return;
}
if (m2.bogus) {
m1.bogus = true;
return;
}
swap(m1.body, m2.body);
swap(m1.bindings, m2.bindings);
swap(m1.bindingsLen, m2.bindingsLen);
}
MFDataModel& operator=(MFDataModel) noexcept;
MFDataModel(const MFDataModel& other);
virtual ~MFDataModel();
class U_I18N_API Builder : public UMemory {
private:
friend class MFDataModel;
void checkDuplicate(const VariableName&, UErrorCode&) const;
void buildSelectorsMessage(UErrorCode&);
bool hasPattern = true;
bool hasSelectors = false;
Pattern pattern;
// The following members are not LocalPointers for the same reason as in SelectorKeys::Builder
UVector* selectors = nullptr;
UVector* variants = nullptr;
UVector* bindings = nullptr;
public:
Builder& addBinding(Binding&& b, UErrorCode& status);
Builder& addSelector(Expression&& selector, UErrorCode& errorCode) noexcept;
Builder& addVariant(SelectorKeys&& keys, Pattern&& pattern, UErrorCode& errorCode) noexcept;
Builder& setPattern(Pattern&& pattern);
MFDataModel build(UErrorCode& status) const noexcept;
Builder(UErrorCode& status);
virtual ~Builder();
Builder(const Builder&) = delete;
Builder& operator=(const Builder&) = delete;
Builder(Builder&&) = delete;
Builder& operator=(Builder&&) = delete;
}; // class Builder
private:
friend class Checker;
friend class MessageFormatter;
friend class Serializer;
Pattern empty; // Provided so that `getPattern()` can return a result
// if called on a selectors message
bool hasPattern() const { return std::holds_alternative<Pattern>(body); }
bool bogus = false; // Set if a copy constructor fails
// A message body is either a matcher (selector list and variant list),
// or a single pattern
std::variant<Matcher, Pattern> body;
// Bindings for local variables
/* const */ LocalArray<Binding> bindings;
int32_t bindingsLen = 0;
const Binding* getLocalVariablesInternal() const;
const Expression* getSelectorsInternal() const;
const Variant* getVariantsInternal() const;
int32_t numSelectors() const {
const Matcher* matcher = std::get_if<Matcher>(&body);
return (matcher == nullptr ? 0 : matcher->numSelectors);
}
int32_t numVariants() const {
const Matcher* matcher = std::get_if<Matcher>(&body);
return (matcher == nullptr ? 0 : matcher->numVariants);
}
// Helper
void initBindings(const Binding*);
MFDataModel(const Builder& builder, UErrorCode&) noexcept;
}; // class MFDataModel
} // namespace message2
U_NAMESPACE_END
#endif // U_HIDE_DEPRECATED_API
#endif /* #if !UCONFIG_NO_MF2 */
#endif /* #if !UCONFIG_NO_FORMATTING */
#endif /* U_SHOW_CPLUSPLUS_API */
#endif // MESSAGEFORMAT_DATA_MODEL_H
// eof
icu::LocalArray
"Smart pointer" class, deletes objects via the C++ array delete[] operator.
Definition localpointer.h:366
icu::LocalPointerBase
"Smart pointer" base class; do not use directly: use LocalPointer etc.
Definition localpointer.h:68
icu::UMemory
UMemory is the common ICU base class.
Definition uobject.h:115
icu::UObject
UObject is the common ICU "boilerplate" class.
Definition uobject.h:223
icu::UnicodeString
UnicodeString is a string class that stores Unicode characters directly and provides similar function...
Definition unistr.h:296
icu::message2::MFDataModel::Builder
The mutable MFDataModel::Builder class allows the data model to be constructed incrementally.
Definition messageformat2_data_model.h:2437
icu::message2::MFDataModel::Builder::Builder
Builder(UErrorCode &status)
Default constructor.
icu::message2::MFDataModel::Builder::addBinding
Builder & addBinding(Binding &&b, UErrorCode &status)
Adds a binding, There must not already be a binding with the same name.
icu::message2::MFDataModel::Builder::build
MFDataModel build(UErrorCode &status) const noexcept
Constructs a new immutable data model.
icu::message2::MFDataModel::Builder::addVariant
Builder & addVariant(SelectorKeys &&keys, Pattern &&pattern, UErrorCode &errorCode) noexcept
Adds a single variant.
icu::message2::MFDataModel::Builder::addSelector
Builder & addSelector(Expression &&selector, UErrorCode &errorCode) noexcept
Adds a selector expression.
icu::message2::MFDataModel::Builder::setPattern
Builder & setPattern(Pattern &&pattern)
Sets the body of the message as a pattern.
icu::message2::MFDataModel::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::MessageFormatter
Definition messageformat2.h:51
icu::message2::data_model::Expression::Builder
The mutable Expression::Builder class allows the operator to be constructed incrementally.
Definition messageformat2_data_model.h:1331
icu::message2::data_model::Expression::Builder::addAttribute
Builder & addAttribute(const UnicodeString &key, Operand &&value, UErrorCode &status)
Adds a single attribute.
icu::message2::data_model::Expression::Builder::build
Expression build(UErrorCode &status)
Constructs a new immutable Expression using the operand and operator that were previously set.
icu::message2::data_model::Expression::Builder::setOperand
Builder & setOperand(Operand &&rAnd)
Sets the operand of this expression.
icu::message2::data_model::Expression::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::data_model::Expression::Builder::setOperator
Builder & setOperator(Operator &&rAtor)
Sets the operator of this expression.
icu::message2::data_model::Expression::Builder::Builder
Builder(UErrorCode &status)
Default constructor.
icu::message2::data_model::Expression
The Expression class corresponds to the expression nonterminal in the MessageFormat 2 grammar and the...
Definition messageformat2_data_model.h:1265
icu::message2::data_model::Expression::getOperand
const Operand & getOperand() const
Accesses the operand of this expression.
icu::message2::data_model::Expression::isFunctionCall
UBool isFunctionCall() const
Checks if this expression has a function annotation (with or without an operand).
icu::message2::data_model::Expression::operator=
Expression & operator=(Expression) noexcept
Assignment operator.
icu::message2::data_model::Expression::isStandaloneAnnotation
UBool isStandaloneAnnotation() const
Checks if this expression is an annotation with no operand.
icu::message2::data_model::Expression::getAttributes
std::vector< Option > getAttributes() const
Gets the attributes of this expression.
Definition messageformat2_data_model.h:1321
icu::message2::data_model::Expression::~Expression
virtual ~Expression()
Destructor.
icu::message2::data_model::Expression::Expression
Expression()
Default constructor.
icu::message2::data_model::Expression::Expression
Expression(const Expression &other)
Copy constructor.
icu::message2::data_model::Expression::getOperator
const Operator * getOperator(UErrorCode &status) const
Accesses the function annotating this expression.
icu::message2::data_model::Expression::swap
friend void swap(Expression &e1, Expression &e2) noexcept
Non-member swap function.
Definition messageformat2_data_model.h:1421
icu::message2::data_model::Literal
The Literal class corresponds to the literal nonterminal in the MessageFormat 2 grammar,...
Definition messageformat2_data_model.h:76
icu::message2::data_model::Literal::isQuoted
UBool isQuoted() const
Determines if this literal appeared as a quoted literal in the message.
Definition messageformat2_data_model.h:105
icu::message2::data_model::Literal::unquoted
const UnicodeString & unquoted() const
Returns the parsed string contents of this literal.
icu::message2::data_model::Literal::swap
friend void swap(Literal &l1, Literal &l2) noexcept
Non-member swap function.
Definition messageformat2_data_model.h:133
icu::message2::data_model::Literal::Literal
Literal(UBool q, const UnicodeString &s)
Literal constructor.
Definition messageformat2_data_model.h:117
icu::message2::data_model::Literal::quoted
UnicodeString quoted() const
Returns the quoted representation of this literal (enclosed in '|' characters)
icu::message2::data_model::Literal::Literal
Literal()=default
Default constructor.
icu::message2::data_model::Literal::operator<
bool operator<(const Literal &other) const
Less than operator.
icu::message2::data_model::Literal::Literal
Literal(const Literal &other)
Copy constructor.
Definition messageformat2_data_model.h:124
icu::message2::data_model::Literal::operator=
Literal & operator=(Literal) noexcept
Assignment operator.
icu::message2::data_model::Literal::operator==
bool operator==(const Literal &other) const
Equality operator.
icu::message2::data_model::Literal::~Literal
virtual ~Literal()
Destructor.
icu::message2::data_model::Markup::Builder
The mutable Markup::Builder class allows the markup to be constructed incrementally.
Definition messageformat2_data_model.h:1128
icu::message2::data_model::Markup::Builder::setStandalone
Builder & setStandalone()
Sets this to be a standalone markup.
Definition messageformat2_data_model.h:1173
icu::message2::data_model::Markup::Builder::addOption
Builder & addOption(const UnicodeString &key, Operand &&value, UErrorCode &status)
Adds a single option.
icu::message2::data_model::Markup::Builder::Builder
Builder(UErrorCode &status)
Default constructor.
icu::message2::data_model::Markup::Builder::addAttribute
Builder & addAttribute(const UnicodeString &key, Operand &&value, UErrorCode &status)
Adds a single attribute.
icu::message2::data_model::Markup::Builder::setClose
Builder & setClose()
Sets this to be an closing markup.
Definition messageformat2_data_model.h:1164
icu::message2::data_model::Markup::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::data_model::Markup::Builder::setName
Builder & setName(const UnicodeString &n)
Sets the name of this markup.
Definition messageformat2_data_model.h:1146
icu::message2::data_model::Markup::Builder::build
Markup build(UErrorCode &status)
Constructs a new immutable Markup using the name and type and (optionally) options and attributes tha...
icu::message2::data_model::Markup::Builder::setOpen
Builder & setOpen()
Sets this to be an opening markup.
Definition messageformat2_data_model.h:1155
icu::message2::data_model::Markup
The Markup class corresponds to the markup nonterminal in the MessageFormat 2 grammar and the markup ...
Definition messageformat2_data_model.h:1048
icu::message2::data_model::Markup::Markup
Markup()
Default constructor.
Definition messageformat2_data_model.h:1111
icu::message2::data_model::Markup::getAttributes
std::vector< Option > getAttributes() const
Gets the attributes of this markup.
Definition messageformat2_data_model.h:1103
icu::message2::data_model::Markup::isOpen
UBool isOpen() const
Checks if this markup is an opening tag.
Definition messageformat2_data_model.h:1058
icu::message2::data_model::Markup::getOptions
std::vector< Option > getOptions() const
Gets the options of this markup.
Definition messageformat2_data_model.h:1094
icu::message2::data_model::Markup::isClose
UBool isClose() const
Checks if this markup is an closing tag.
Definition messageformat2_data_model.h:1067
icu::message2::data_model::Markup::~Markup
virtual ~Markup()
Destructor.
icu::message2::data_model::Markup::getName
const UnicodeString & getName() const
Gets the name of this markup.
Definition messageformat2_data_model.h:1085
icu::message2::data_model::Markup::isStandalone
UBool isStandalone() const
Checks if this markup is an standalone tag.
Definition messageformat2_data_model.h:1076
icu::message2::data_model::Operator::Builder
The mutable Operator::Builder class allows the operator to be constructed incrementally.
Definition messageformat2_data_model.h:883
icu::message2::data_model::Operator::Builder::setFunctionName
Builder & setFunctionName(FunctionName &&func)
Sets this operator to be a function annotation and sets its name to func.
icu::message2::data_model::Operator::Builder::build
Operator build(UErrorCode &status)
Constructs a new immutable Operator using the function name and options that were previously set.
icu::message2::data_model::Operator::Builder::addOption
Builder & addOption(const UnicodeString &key, Operand &&value, UErrorCode &status) noexcept
Sets this operator to be a function annotation and adds a single option.
icu::message2::data_model::Operator::Builder::Builder
Builder(UErrorCode &status)
Default constructor.
icu::message2::data_model::Operator::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::data_model::Option
An Option pairs an option name with an Operand.
Definition messageformat2_data_model.h:679
icu::message2::data_model::Option::Option
Option(const Option &other)
Copy constructor.
icu::message2::data_model::Option::~Option
virtual ~Option()
Destructor.
icu::message2::data_model::Option::Option
Option()
Default constructor.
Definition messageformat2_data_model.h:717
icu::message2::data_model::Option::operator=
Option & operator=(Option other) noexcept
Assignment operator.
icu::message2::data_model::Option::Option
Option(const UnicodeString &n, Operand &&r)
Constructor.
Definition messageformat2_data_model.h:709
icu::message2::data_model::Option::getName
const UnicodeString & getName() const
Accesses the left-hand side of the option.
Definition messageformat2_data_model.h:698
icu::message2::data_model::Option::getValue
const Operand & getValue() const
Accesses the right-hand side of the option.
Definition messageformat2_data_model.h:689
icu::message2::data_model::Option::swap
friend void swap(Option &o1, Option &o2) noexcept
Non-member swap function.
Definition messageformat2_data_model.h:726
icu::message2::data_model::PatternPart
A PatternPart is a single element (text or expression) in a Pattern.
Definition messageformat2_data_model.h:1513
icu::message2::data_model::PatternPart::isExpression
UBool isExpression() const
Checks if the part is an expression part.
Definition messageformat2_data_model.h:1541
icu::message2::data_model::PatternPart::PatternPart
PatternPart(const UnicodeString &t)
Text part constructor.
Definition messageformat2_data_model.h:1615
icu::message2::data_model::PatternPart::contents
const Expression & contents() const
Accesses the expression of the part.
icu::message2::data_model::PatternPart::PatternPart
PatternPart(Expression &&e)
Expression part constructor.
Definition messageformat2_data_model.h:1625
icu::message2::data_model::PatternPart::asText
const UnicodeString & asText() const
Accesses the text contents of the part.
icu::message2::data_model::PatternPart::PatternPart
PatternPart()=default
Default constructor.
icu::message2::data_model::PatternPart::PatternPart
PatternPart(Markup &&m)
Markup part constructor.
Definition messageformat2_data_model.h:1635
icu::message2::data_model::PatternPart::isMarkup
UBool isMarkup() const
Checks if the part is a markup part.
Definition messageformat2_data_model.h:1532
icu::message2::data_model::PatternPart::isText
UBool isText() const
Checks if the part is a text part.
Definition messageformat2_data_model.h:1523
icu::message2::data_model::PatternPart::asMarkup
const Markup & asMarkup() const
Accesses the expression of the part.
icu::message2::data_model::PatternPart::PatternPart
PatternPart(const PatternPart &other)
Copy constructor.
icu::message2::data_model::PatternPart::operator=
PatternPart & operator=(PatternPart) noexcept
Assignment operator.
icu::message2::data_model::PatternPart::swap
friend void swap(PatternPart &p1, PatternPart &p2) noexcept
Non-member swap function.
Definition messageformat2_data_model.h:1580
icu::message2::data_model::PatternPart::~PatternPart
virtual ~PatternPart()
Destructor.
icu::message2::data_model::Pattern::Builder
The mutable Pattern::Builder class allows the pattern to be constructed one part at a time.
Definition messageformat2_data_model.h:1715
icu::message2::data_model::Pattern::Builder::add
Builder & add(UnicodeString &&part, UErrorCode &status) noexcept
Adds a single text part to the pattern.
icu::message2::data_model::Pattern::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::data_model::Pattern::Builder::add
Builder & add(Markup &&part, UErrorCode &status) noexcept
Adds a single markup part to the pattern.
icu::message2::data_model::Pattern::Builder::build
Pattern build(UErrorCode &status) const noexcept
Constructs a new immutable Pattern using the list of parts set with previous add() calls.
icu::message2::data_model::Pattern::Builder::Builder
Builder(UErrorCode &status)
Default constructor.
icu::message2::data_model::Pattern::Builder::add
Builder & add(Expression &&part, UErrorCode &status) noexcept
Adds a single expression part to the pattern.
icu::message2::data_model::SelectorKeys::Builder
The mutable SelectorKeys::Builder class allows the key list to be constructed one key at a time.
Definition messageformat2_data_model.h:540
icu::message2::data_model::SelectorKeys::Builder::add
Builder & add(Key &&key, UErrorCode &status) noexcept
Adds a single key to the list.
icu::message2::data_model::SelectorKeys::Builder::~Builder
virtual ~Builder()
Destructor.
icu::message2::data_model::SelectorKeys::Builder::Builder
Builder(UErrorCode &status)
Default constructor.
icu::message2::data_model::SelectorKeys::Builder::build
SelectorKeys build(UErrorCode &status) const
Constructs a new immutable SelectorKeys using the list of keys set with previous add() calls.
localpointer.h
C++ API: "Smart pointers" for use with and in ICU4C C++ code.
icu::operator==
U_EXPORT UBool operator==(const StringPiece &x, const StringPiece &y)
Global operator == for StringPiece.
icu::message2::data_model::Pattern::Iterator
The Pattern::Iterator class provides an iterator over the formattable parts of a pattern.
Definition messageformat2_data_model.h:1845
icu::message2::data_model::Pattern::Iterator::operator*
reference operator*() const
Dereference operator (gets the element at the current iterator position)
Definition messageformat2_data_model.h:1867
icu::message2::data_model::Pattern::Iterator::operator++
Iterator operator++()
Increment operator (advances to the next iterator position)
Definition messageformat2_data_model.h:1877
UBool
int8_t UBool
The ICU boolean type, a signed-byte integer.
Definition umachine.h:247
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
UErrorCode
UErrorCode
Standard ICU4C error code type, a substitute for exceptions.
Definition utypes.h:430
U_I18N_API
#define U_I18N_API
Set to export library symbols from inside the i18n library, and to import them from outside.
Definition utypes.h:316