Debian Codegraph @

intl/icu/source/i18n/unicode/messageformat2_data_model.h

raw
1 // © 2024 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3
4 #include "unicode/utypes.h"
5
6 #ifndef MESSAGEFORMAT_DATA_MODEL_H
7 #define MESSAGEFORMAT_DATA_MODEL_H
8
9 #if U_SHOW_CPLUSPLUS_API
10
11 #if !UCONFIG_NO_NORMALIZATION
12
13 #if !UCONFIG_NO_FORMATTING
14
15 #if !UCONFIG_NO_MF2
16
17 #include "unicode/localpointer.h"
18 #include "unicode/messageformat2_data_model_names.h"
19
20 #ifndef U_HIDE_DEPRECATED_API
21
22 #include <algorithm>
23 #include <cstddef>
24 #include <iterator>
25 #include <optional>
26 #include <variant>
27 #include <vector>
28
29 U_NAMESPACE_BEGIN
30
31 class UVector;
32
33 // Helpers
34
35 // Note: this _must_ be declared `inline` or else gcc will generate code
36 // for its instantiations, which needs to be avoided because it returns
37 // a std::vector
38 template<typename T>
39 static 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]);
43 }
44 return result;
45 }
46
47 #if defined(U_REAL_MSVC)
48 #pragma warning(push)
49 // Ignore warning 4251 as these templates are instantiated later in this file,
50 // after the classes used to instantiate them have been defined.
51 #pragma warning(disable: 4251)
52 #endif
53
54 namespace message2 {
55 class Checker;
56 class MFDataModel;
57 class MessageFormatter;
58 class Parser;
59 class Serializer;
60
61
62 namespace data_model {
63 class Binding;
64 class Literal;
65 class Operator;
66
67 /**
68 * The `Literal` class corresponds to the `literal` nonterminal in the MessageFormat 2 grammar,
69 * https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf and the
70 * `Literal` interface defined in
71 * // https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#expressions
72 *
73 * `Literal` is immutable, copyable and movable.
74 *
75 * @internal ICU 75 technology preview
76 * @deprecated This API is for technology preview only.
77 */
78 class U_I18N_API Literal : public UObject {
79 public:
80 /**
81 * Returns the quoted representation of this literal (enclosed in '|' characters)
82 *
83 * @return A string representation of the literal enclosed in quote characters.
84 *
85 * @internal ICU 75 technology preview
86 * @deprecated This API is for technology preview only.
87 */
88 UnicodeString quoted() const;
89 /**
90 * Returns the parsed string contents of this literal.
91 *
92 * @return A string representation of this literal.
93 *
94 * @internal ICU 75 technology preview
95 * @deprecated This API is for technology preview only.
96 */
97 const UnicodeString& unquoted() const;
98 /**
99 * Determines if this literal appeared as a quoted literal in the message.
100 *
101 * @return true if and only if this literal appeared as a quoted literal in the
102 * message.
103 *
104 * @internal ICU 75 technology preview
105 * @deprecated This API is for technology preview only.
106 */
107 UBool isQuoted() const { return thisIsQuoted; }
108 /**
109 * Literal constructor.
110 *
111 * @param q True if and only if this literal was parsed with the `quoted` nonterminal
112 * (appeared enclosed in '|' characters in the message text).
113 * @param s The string contents of this literal; escape sequences are assumed to have
114 * been interpreted already.
115 *
116 * @internal ICU 75 technology preview
117 * @deprecated This API is for technology preview only.
118 */
119 Literal(UBool q, const UnicodeString& s) : thisIsQuoted(q), contents(s) {}
120 /**
121 * Copy constructor.
122 *
123 * @internal ICU 75 technology preview
124 * @deprecated This API is for technology preview only.
125 */
126 Literal(const Literal& other) : thisIsQuoted(other.thisIsQuoted), contents(other.contents) {}
127 /**
128 * Non-member swap function.
129 * @param l1 will get l2's contents
130 * @param l2 will get l1's contents
131 *
132 * @internal ICU 75 technology preview
133 * @deprecated This API is for technology preview only.
134 */
135 friend inline void swap(Literal& l1, Literal& l2) noexcept {
136 using std::swap;
137
138 swap(l1.thisIsQuoted, l2.thisIsQuoted);
139 swap(l1.contents, l2.contents);
140 }
141 /**
142 * Assignment operator.
143 *
144 * @internal ICU 75 technology preview
145 * @deprecated This API is for technology preview only.
146 */
147 Literal& operator=(Literal) noexcept;
148 /**
149 * Default constructor.
150 * Puts the Literal into a valid but undefined state.
151 *
152 * @internal ICU 75 technology preview
153 * @deprecated This API is for technology preview only.
154 */
155 Literal() = default;
156 /**
157 * Less than operator. Compares `this.stringContents()` with
158 * `other.stringContents()`. This method is used in representing
159 * the mapping from key lists to patterns in a message with variants,
160 * and is not expected to be useful otherwise.
161 *
162 * @param other The Literal to compare to this one.
163 * @return true if the parsed string corresponding to this `Literal`
164 * is less than the parsed string corresponding to the other `Literal`
165 * (according to `UnicodeString`'s less-than operator).
166 * Returns false otherwise.
167 *
168 * @internal ICU 75 technology preview
169 * @deprecated This API is for technology preview only.
170 */
171 bool operator<(const Literal& other) const;
172 /**
173 * Equality operator. Compares `this.stringContents()` with
174 * `other.stringContents()`. This method is used in representing
175 * the mapping from key lists to patterns in a message with variants,
176 * and is not expected to be useful otherwise.
177 *
178 * @param other The Literal to compare to this one.
179 * @return true if the parsed string corresponding to this `Literal`
180 * equals the parsed string corresponding to the other `Literal`
181 * (according to `UnicodeString`'s equality operator).
182 * Returns false otherwise.
183 *
184 * @internal ICU 75 technology preview
185 * @deprecated This API is for technology preview only.
186 */
187 bool operator==(const Literal& other) const;
188 /**
189 * Destructor.
190 *
191 * @internal ICU 75 technology preview
192 * @deprecated This API is for technology preview only.
193 */
194 virtual ~Literal();
195
196 private:
197 /* const */ bool thisIsQuoted = false;
198 /* const */ UnicodeString contents;
199 };
200 } // namespace data_model
201 } // namespace message2
202
203 /// @cond DOXYGEN_IGNORE
204 // Export an explicit template instantiation of the LocalPointer that is used as a
205 // data member of various MFDataModel classes.
206 // (When building DLLs for Windows this is required.)
207 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
208 // for similar examples.)
209 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
210 template class U_I18N_API LocalPointerBase<message2::data_model::Literal>;
211 template class U_I18N_API LocalArray<message2::data_model::Literal>;
212 #endif
213 #if defined(U_REAL_MSVC)
214 #pragma warning(pop)
215 #endif
216 /// @endcond
217
218 U_NAMESPACE_END
219
220 /// @cond DOXYGEN_IGNORE
221 // Export an explicit template instantiation of the std::variants and std::optionals
222 // that are used as a data member of various MFDataModel classes.
223 // (When building DLLs for Windows this is required.)
224 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
225 // for similar examples.)
226 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
227 #if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
228 struct U_I18N_API std::_Nontrivial_dummy_type;
229 template class U_I18N_API std::_Variant_storage_<false, icu::UnicodeString, icu::message2::data_model::Literal>;
230 #endif
231 template class U_I18N_API std::variant<icu::UnicodeString, icu::message2::data_model::Literal>;
232 template class U_I18N_API std::optional<std::variant<icu::UnicodeString, icu::message2::data_model::Literal>>;
233 template class U_I18N_API std::optional<icu::message2::data_model::Literal>;
234 #endif
235 /// @endcond
236
237 U_NAMESPACE_BEGIN
238
239 namespace message2 {
240 namespace data_model {
241
242 /**
243 * The `Operand` class corresponds to the `operand` nonterminal in the MessageFormat 2 grammar,
244 * https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf .
245 * It represents a `Literal | VariableRef` -- see the `operand?` field of the `FunctionRef`
246 * interface defined at:
247 * https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#expressions
248 * with the difference that it can also represent a null operand (the absent operand in an
249 * `annotation` with no operand).
250 *
251 * `Operand` is immutable and is copyable and movable.
252 *
253 * @internal ICU 75 technology preview
254 * @deprecated This API is for technology preview only.
255 */
256 class U_I18N_API Operand : public UObject {
257 public:
258 /**
259 * Determines if this operand represents a variable.
260 *
261 * @return True if and only if the operand is a variable.
262 *
263 * @internal ICU 75 technology preview
264 * @deprecated This API is for technology preview only.
265 */
266 UBool isVariable() const;
267 /**
268 * Determines if this operand represents a literal.
269 *
270 * @return True if and only if the operand is a literal.
271 *
272 * @internal ICU 75 technology preview
273 * @deprecated This API is for technology preview only.
274 */
275 UBool isLiteral() const;
276 /**
277 * Determines if this operand is the null operand.
278 *
279 * @return True if and only if the operand is the null operand.
280 *
281 * @internal ICU 75 technology preview
282 * @deprecated This API is for technology preview only.
283 */
284 virtual UBool isNull() const;
285 /**
286 * Returns a reference to this operand's variable name.
287 * Precondition: isVariable()
288 *
289 * @return A reference to the name of the variable
290 *
291 * @internal ICU 75 technology preview
292 * @deprecated This API is for technology preview only.
293 */
294 const UnicodeString& asVariable() const;
295 /**
296 * Returns a reference to this operand's literal contents.
297 * Precondition: isLiteral()
298 *
299 * @return A reference to the literal
300 *
301 * @internal ICU 75 technology preview
302 * @deprecated This API is for technology preview only.
303 */
304 const Literal& asLiteral() const;
305 /**
306 * Default constructor.
307 * Creates a null Operand.
308 *
309 * @internal ICU 75 technology preview
310 * @deprecated This API is for technology preview only.
311 */
312 Operand() : contents(std::nullopt) {}
313 /**
314 * Variable operand constructor.
315 *
316 * @param v The variable name; an operand corresponding
317 * to a reference to `v` is returned.
318 *
319 * @internal ICU 75 technology preview
320 * @deprecated This API is for technology preview only.
321 */
322 explicit Operand(const UnicodeString& v) : contents(VariableName(v)) {}
323 /**
324 * Literal operand constructor.
325 *
326 * @param l The literal to use for this operand; an operand
327 * corresponding to `l` is returned.
328 *
329 * @internal ICU 75 technology preview
330 * @deprecated This API is for technology preview only.
331 */
332 explicit Operand(const Literal& l) : contents(l) {}
333 /**
334 * Non-member swap function.
335 * @param o1 will get o2's contents
336 * @param o2 will get o1's contents
337 *
338 * @internal ICU 75 technology preview
339 * @deprecated This API is for technology preview only.
340 */
341 friend inline void swap(Operand& o1, Operand& o2) noexcept {
342 using std::swap;
343 (void) o1;
344 (void) o2;
345 swap(o1.contents, o2.contents);
346 }
347 /**
348 * Assignment operator.
349 *
350 * @internal ICU 75 technology preview
351 * @deprecated This API is for technology preview only.
352 */
353 virtual Operand& operator=(Operand) noexcept;
354 /**
355 * Copy constructor.
356 *
357 * @internal ICU 75 technology preview
358 * @deprecated This API is for technology preview only.
359 */
360 Operand(const Operand&);
361 /**
362 * Destructor.
363 *
364 * @internal ICU 75 technology preview
365 * @deprecated This API is for technology preview only.
366 */
367 virtual ~Operand();
368 private:
369 std::optional<std::variant<VariableName, Literal>> contents;
370 }; // class Operand
371
372 /**
373 * The `Key` class corresponds to the `key` nonterminal in the MessageFormat 2 grammar,
374 * https://github.com/unicode-org/message-format-wg/blob/main/spec/message.abnf .
375 * It also corresponds to
376 * the `Literal | CatchallKey` that is the
377 * element type of the `keys` array in the `Variant` interface
378 * defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#messages
379 *
380 * A key is either a literal or the wildcard symbol (represented in messages as '*')
381 *
382 * `Key` is immutable, copyable and movable.
383 *
384 * @internal ICU 75 technology preview
385 * @deprecated This API is for technology preview only.
386 */
387 class U_I18N_API Key : public UObject {
388 public:
389 /**
390 * Determines if this is a wildcard key
391 *
392 * @return True if and only if this is the wildcard key
393 *
394 * @internal ICU 75 technology preview
395 * @deprecated This API is for technology preview only.
396 */
397 UBool isWildcard() const { return !contents.has_value(); }
398 /**
399 * Returns the contents of this key as a literal.
400 * Precondition: !isWildcard()
401 *
402 * @return The literal contents of the key
403 *
404 * @internal ICU 75 technology preview
405 * @deprecated This API is for technology preview only.
406 */
407 const Literal& asLiteral() const;
408 /**
409 * Copy constructor.
410 *
411 * @internal ICU 75 technology preview
412 * @deprecated This API is for technology preview only.
413 */
414 Key(const Key& other) : contents(other.contents) {}
415 /**
416 * Wildcard constructor; constructs a Key representing the
417 * catchall or wildcard key, '*'.
418 *
419 * @internal ICU 75 technology preview
420 * @deprecated This API is for technology preview only.
421 */
422 Key() : contents(std::nullopt) {}
423 /**
424 * Literal key constructor.
425 *
426 * @param lit A Literal to use for this key. The result matches the
427 * literal `lit`.
428 *
429 * @internal ICU 75 technology preview
430 * @deprecated This API is for technology preview only.
431 */
432 explicit Key(const Literal& lit) : contents(lit) {}
433 /**
434 * Non-member swap function.
435 * @param k1 will get k2's contents
436 * @param k2 will get k1's contents
437 *
438 * @internal ICU 75 technology preview
439 * @deprecated This API is for technology preview only.
440 */
441 friend inline void swap(Key& k1, Key& k2) noexcept {
442 using std::swap;
443
444 swap(k1.contents, k2.contents);
445 }
446 /**
447 * Assignment operator
448 *
449 * @internal ICU 75 technology preview
450 * @deprecated This API is for technology preview only.
451 */
452 Key& operator=(Key) noexcept;
453 /**
454 * Less than operator. Compares the literal of `this` with the literal of `other`.
455 * This method is used in representing the mapping from key lists to patterns
456 * in a message with variants, and is not expected to be useful otherwise.
457 *
458 * @param other The Key to compare to this one.
459 * @return true if the two `Key`s are not wildcards and if `this.asLiteral()`
460 * < `other.asLiteral()`.
461 * Returns false otherwise.
462 *
463 * @internal ICU 75 technology preview
464 * @deprecated This API is for technology preview only.
465 */
466 bool operator<(const Key& other) const;
467 /**
468 * Equality operator. Compares the literal of `this` with the literal of `other`.
469 * This method is used in representing the mapping from key lists to patterns
470 * in a message with variants, and is not expected to be useful otherwise.
471 *
472 * @param other The Key to compare to this one.
473 * @return true if either both `Key`s are wildcards, or `this.asLiteral()`
474 * == `other.asLiteral()`.
475 * Returns false otherwise.
476 *
477 * @internal ICU 75 technology preview
478 * @deprecated This API is for technology preview only.
479 */
480 bool operator==(const Key& other) const;
481 /**
482 * Destructor.
483 *
484 * @internal ICU 75 technology preview
485 * @deprecated This API is for technology preview only.
486 */
487 virtual ~Key();
488 private:
489 /* const */ std::optional<Literal> contents;
490 }; // class Key
491 } // namespace data_model
492 } // namespace message2
493
494 /// @cond DOXYGEN_IGNORE
495 // Export an explicit template instantiation of the LocalPointer that is used as a
496 // data member of various MFDataModel classes.
497 // (When building DLLs for Windows this is required.)
498 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
499 // for similar examples.)
500 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
501 template class U_I18N_API LocalPointerBase<message2::data_model::Key>;
502 template class U_I18N_API LocalArray<message2::data_model::Key>;
503 #endif
504 /// @endcond
505
506 namespace message2 {
507 namespace data_model {
508 /**
509 * The `SelectorKeys` class represents the key list for a single variant.
510 * It corresponds to the `keys` array in the `Variant` interface
511 * defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#messages
512 *
513 * `SelectorKeys` is immutable, copyable and movable.
514 *
515 * @internal ICU 75 technology preview
516 * @deprecated This API is for technology preview only.
517 */
518 class U_I18N_API SelectorKeys : public UObject {
519 public:
520 /**
521 * Returns the underlying list of keys.
522 *
523 * @return The list of keys for this variant.
524 * Returns an empty list if allocating this `SelectorKeys`
525 * object previously failed.
526 *
527 * @internal ICU 75 technology preview
528 * @deprecated This API is for technology preview only.
529 */
530 std::vector<Key> getKeys() const {
531 return toStdVector<Key>(keys.getAlias(), len);
532 }
533 /**
534 * The mutable `SelectorKeys::Builder` class allows the key list to be constructed
535 * one key at a time.
536 *
537 * Builder is not copyable or movable.
538 *
539 * @internal ICU 75 technology preview
540 * @deprecated This API is for technology preview only.
541 */
542 class U_I18N_API Builder : public UMemory {
543 private:
544 friend class SelectorKeys;
545 UVector* keys; // This is a raw pointer and not a LocalPointer<UVector> to avoid undefined behavior warnings,
546 // since UVector is forward-declared
547 // The vector owns its elements
548 public:
549 /**
550 * Adds a single key to the list.
551 *
552 * @param key The key to be added. Passed by move
553 * @param status Input/output error code
554 * @return A reference to the builder.
555 *
556 * @internal ICU 75 technology preview
557 * @deprecated This API is for technology preview only.
558 */
559 Builder& add(Key&& key, UErrorCode& status) noexcept;
560 /**
561 * Constructs a new immutable `SelectorKeys` using the list of keys
562 * set with previous `add()` calls.
563 *
564 * The builder object (`this`) can still be used after calling `build()`.
565 *
566 * @param status Input/output error code
567 * @return The new SelectorKeys object
568 *
569 * @internal ICU 75 technology preview
570 * @deprecated This API is for technology preview only.
571 */
572 SelectorKeys build(UErrorCode& status) const;
573 /**
574 * Default constructor.
575 * Returns a Builder with an empty list of keys.
576 *
577 * @param status Input/output error code
578 *
579 * @internal ICU 75 technology preview
580 * @deprecated This API is for technology preview only.
581 */
582 Builder(UErrorCode& status);
583 /**
584 * Destructor.
585 *
586 * @internal ICU 75 technology preview
587 * @deprecated This API is for technology preview only.
588 */
589 virtual ~Builder();
590 Builder(const Builder&) = delete;
591 Builder& operator=(const Builder&) = delete;
592 Builder(Builder&&) = delete;
593 Builder& operator=(Builder&&) = delete;
594 }; // class SelectorKeys::Builder
595 /**
596 * Less than operator. Compares the two key lists lexicographically.
597 * This method makes it possible for a `SelectorKeys` to be used as a map
598 * key, which allows variants to be represented as a map. It is not expected
599 * to be useful otherwise.
600 *
601 * @param other The SelectorKeys to compare to this one.
602 * @return true if `this` is less than `other`, comparing the two key lists
603 * lexicographically.
604 * Returns false otherwise.
605 *
606 * @internal ICU 75 technology preview
607 * @deprecated This API is for technology preview only.
608 */
609 bool operator<(const SelectorKeys& other) const;
610 /**
611 * Default constructor.
612 * Puts the SelectorKeys into a valid but undefined state.
613 *
614 * @internal ICU 75 technology preview
615 * @deprecated This API is for technology preview only.
616 */
617 SelectorKeys() : len(0) {}
618 /**
619 * Non-member swap function.
620 * @param s1 will get s2's contents
621 * @param s2 will get s1's contents
622 *
623 * @internal ICU 75 technology preview
624 * @deprecated This API is for technology preview only.
625 */
626 friend inline void swap(SelectorKeys& s1, SelectorKeys& s2) noexcept {
627 using std::swap;
628
629 swap(s1.len, s2.len);
630 swap(s1.keys, s2.keys);
631 }
632 /**
633 * Copy constructor.
634 *
635 * @internal ICU 75 technology preview
636 * @deprecated This API is for technology preview only.
637 */
638 SelectorKeys(const SelectorKeys& other);
639 /**
640 * Assignment operator.
641 *
642 * @internal ICU 75 technology preview
643 * @deprecated This API is for technology preview only.
644 */
645 SelectorKeys& operator=(SelectorKeys other) noexcept;
646 /**
647 * Destructor.
648 *
649 * @internal ICU 75 technology preview
650 * @deprecated This API is for technology preview only.
651 */
652 virtual ~SelectorKeys();
653 private:
654 friend class Builder;
655 friend class message2::Checker;
656 friend class message2::MessageFormatter;
657 friend class message2::Serializer;
658
659 /* const */ LocalArray<Key> keys;
660 /* const */ int32_t len;
661
662 const Key* getKeysInternal() const;
663 SelectorKeys(const UVector& ks, UErrorCode& status);
664 }; // class SelectorKeys
665
666
667 } // namespace data_model
668
669
670 namespace data_model {
671 class Operator;
672
673 /**
674 * An `Option` pairs an option name with an Operand.
675 *
676 * `Option` is immutable, copyable and movable.
677 *
678 * @internal ICU 75 technology preview
679 * @deprecated This API is for technology preview only.
680 */
681 class U_I18N_API Option : public UObject {
682 public:
683 /**
684 * Accesses the right-hand side of the option.
685 *
686 * @return A reference to the operand.
687 *
688 * @internal ICU 75 technology preview
689 * @deprecated This API is for technology preview only.
690 */
691 const Operand& getValue() const { return rand; }
692 /**
693 * Accesses the left-hand side of the option.
694 *
695 * @return A reference to the option name.
696 *
697 * @internal ICU 75 technology preview
698 * @deprecated This API is for technology preview only.
699 */
700 const UnicodeString& getName() const { return name; }
701 /**
702 * Constructor. Returns an `Option` representing the
703 * named option "name=rand".
704 *
705 * @param n The name of the option.
706 * @param r The value of the option.
707 *
708 * @internal ICU 75 technology preview
709 * @deprecated This API is for technology preview only.
710 */
711 Option(const UnicodeString& n, Operand&& r) : name(n), rand(std::move(r)) {}
712 /**
713 * Default constructor.
714 * Returns an Option in a valid but undefined state.
715 *
716 * @internal ICU 75 technology preview
717 * @deprecated This API is for technology preview only.
718 */
719 Option() {}
720 /**
721 * Non-member swap function.
722 * @param o1 will get o2's contents
723 * @param o2 will get o1's contents
724 *
725 * @internal ICU 75 technology preview
726 * @deprecated This API is for technology preview only.
727 */
728 friend inline void swap(Option& o1, Option& o2) noexcept {
729 using std::swap;
730
731 swap(o1.name, o2.name);
732 swap(o1.rand, o2.rand);
733 }
734 /**
735 * Copy constructor.
736 *
737 * @internal ICU 75 technology preview
738 * @deprecated This API is for technology preview only.
739 */
740 Option(const Option& other);
741 /**
742 * Assignment operator
743 *
744 * @internal ICU 75 technology preview
745 * @deprecated This API is for technology preview only.
746 */
747 Option& operator=(Option other) noexcept;
748 /**
749 * Destructor.
750 *
751 * @internal ICU 75 technology preview
752 * @deprecated This API is for technology preview only.
753 */
754 virtual ~Option();
755 private:
756 /* const */ UnicodeString name;
757 /* const */ Operand rand;
758 }; // class Option
759 } // namespace data_model
760 } // namespace message2
761
762 /// @cond DOXYGEN_IGNORE
763 // Export an explicit template instantiation of the LocalPointer that is used as a
764 // data member of various MFDataModel classes.
765 // (When building DLLs for Windows this is required.)
766 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
767 // for similar examples.)
768 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
769 template class U_I18N_API LocalPointerBase<message2::data_model::Option>;
770 template class U_I18N_API LocalArray<message2::data_model::Option>;
771 #endif
772 /// @endcond
773
774 namespace message2 {
775 namespace data_model {
776 // Internal only
777 #ifndef U_IN_DOXYGEN
778 // Options
779 // This is a wrapper class around a vector of options that provides lookup operations
780 class U_I18N_API OptionMap : public UObject {
781 public:
782 int32_t size() const;
783 // Needs to take an error code b/c an earlier copy might have failed
784 const Option& getOption(int32_t, UErrorCode&) const;
785 friend inline void swap(OptionMap& m1, OptionMap& m2) noexcept {
786 using std::swap;
787
788 swap(m1.bogus, m2.bogus);
789 swap(m1.options, m2.options);
790 swap(m1.len, m2.len);
791 }
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);
797 }
798 OptionMap(const UVector&, UErrorCode&);
799 OptionMap(Option*, int32_t);
800 virtual ~OptionMap();
801
802 class U_I18N_API Builder : public UObject {
803 private:
804 UVector* options;
805 bool checkDuplicates = true;
806 public:
807 Builder& add(Option&& opt, UErrorCode&);
808 Builder(UErrorCode&);
809 static Builder attributes(UErrorCode&);
810 // As this class is private, build() is destructive
811 OptionMap build(UErrorCode&);
812 friend inline void swap(Builder& m1, Builder& m2) noexcept {
813 using std::swap;
814
815 swap(m1.options, m2.options);
816 swap(m1.checkDuplicates, m2.checkDuplicates);
817 }
818 Builder(Builder&&);
819 Builder(const Builder&) = delete;
820 Builder& operator=(Builder) noexcept;
821 virtual ~Builder();
822 }; // class OptionMap::Builder
823 private:
824 friend class message2::Serializer;
825
826 bool bogus = false;
827 LocalArray<Option> options;
828 int32_t len;
829 }; // class OptionMap
830 #endif
831
832 } // namespace data_model
833 } // namespace message2
834
835 U_NAMESPACE_END
836
837 U_NAMESPACE_BEGIN
838
839 namespace message2 {
840 namespace data_model {
841 /**
842 * The `Operator` class corresponds to the `FunctionRef` type in the
843 * `Expression` interface defined in
844 * https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
845 *
846 * It represents the annotation that an expression can have: a function name paired
847 * with a map from option names to operands (possibly empty).
848 *
849 * `Operator` is immutable, copyable and movable.
850 *
851 * @internal ICU 75 technology preview
852 * @deprecated This API is for technology preview only.
853 */
854 class U_I18N_API Operator : public UObject {
855 public:
856 /**
857 * Accesses the function name.
858 *
859 * @return The function name of this operator.
860 *
861 * @internal ICU 75 technology preview
862 * @deprecated This API is for technology preview only.
863 */
864 const FunctionName& getFunctionName() const;
865 /**
866 * Accesses function options.
867 *
868 * @return A vector of function options for this operator.
869 *
870 * @internal ICU 75 technology preview
871 * @deprecated This API is for technology preview only.
872 */
873 std::vector<Option> getOptions() const {
874 return options.getOptions();
875 }
876 /**
877 * The mutable `Operator::Builder` class allows the operator to be constructed
878 * incrementally.
879 *
880 * Builder is not copyable or movable.
881 *
882 * @internal ICU 75 technology preview
883 * @deprecated This API is for technology preview only.
884 */
885 class U_I18N_API Builder : public UMemory {
886 private:
887 friend class Operator;
888 FunctionName functionName;
889 OptionMap::Builder options;
890 public:
891 /**
892 * Sets this operator to be a function annotation and sets its name
893 * to `func`.
894 *
895 * @param func The function name.
896 * @return A reference to the builder.
897 *
898 * @internal ICU 75 technology preview
899 * @deprecated This API is for technology preview only.
900 */
901 Builder& setFunctionName(FunctionName&& func);
902 /**
903 * Sets this operator to be a function annotation and adds a
904 * single option.
905 *
906 * @param key The name of the option.
907 * @param value The value (right-hand side) of the option.
908 * @param status Input/output error code.
909 * @return A reference to the builder.
910 *
911 * @internal ICU 75 technology preview
912 * @deprecated This API is for technology preview only.
913 */
914 Builder& addOption(const UnicodeString &key, Operand&& value, UErrorCode& status) noexcept;
915 /**
916 * Constructs a new immutable `Operator` using the
917 * function name and options that were previously set.
918 *
919 * The builder object (`this`) can still be used after calling `build()`.
920 *
921 * The `build()` method is non-const for internal implementation reasons,
922 * but is observably const.
923 *
924 * @param status Input/output error code.
925 * @return The new Operator
926 *
927 * @internal ICU 75 technology preview
928 * @deprecated This API is for technology preview only.
929 */
930 Operator build(UErrorCode& status);
931 /**
932 * Default constructor.
933 * Returns a Builder with no function name or options set.
934 *
935 * @param status Input/output error code.
936 *
937 * @internal ICU 75 technology preview
938 * @deprecated This API is for technology preview only.
939 */
940 Builder(UErrorCode& status);
941 /**
942 * Destructor.
943 *
944 * @internal ICU 75 technology preview
945 * @deprecated This API is for technology preview only.
946 */
947 virtual ~Builder();
948 Builder(const Builder&) = delete;
949 Builder& operator=(const Builder&) = delete;
950 Builder(Builder&&) = delete;
951 Builder& operator=(Builder&&) = delete;
952 }; // class Operator::Builder
953 /**
954 * Copy constructor.
955 *
956 * @internal ICU 75 technology preview
957 * @deprecated This API is for technology preview only.
958 */
959 Operator(const Operator& other) noexcept;
960 /**
961 * Non-member swap function.
962 * @param o1 will get o2's contents
963 * @param o2 will get o1's contents
964 *
965 * @internal ICU 75 technology preview
966 * @deprecated This API is for technology preview only.
967 */
968 friend inline void swap(Operator& o1, Operator& o2) noexcept {
969 using std::swap;
970
971 swap(o1.name, o2.name);
972 swap(o1.options, o2.options);
973 }
974 /**
975 * Assignment operator.
976 *
977 * @internal ICU 75 technology preview
978 * @deprecated This API is for technology preview only.
979 */
980 Operator& operator=(Operator) noexcept;
981 /**
982 * Default constructor.
983 * Puts the Operator into a valid but undefined state.
984 *
985 * @internal ICU 75 technology preview
986 * @deprecated This API is for technology preview only.
987 */
988 Operator() {}
989 /**
990 * Destructor.
991 *
992 * @internal ICU 75 technology preview
993 * @deprecated This API is for technology preview only.
994 */
995 virtual ~Operator();
996 private:
997 friend class Binding;
998 friend class Builder;
999 friend class message2::Checker;
1000 friend class message2::MessageFormatter;
1001 friend class message2::Serializer;
1002
1003 // Function call constructor
1004 Operator(const FunctionName& f, const UVector& options, UErrorCode&);
1005
1006 const OptionMap& getOptionsInternal() const;
1007 Operator(const FunctionName&, const OptionMap&);
1008
1009 /* const */ FunctionName name;
1010 /* const */ OptionMap options;
1011 }; // class Operator
1012 } // namespace data_model
1013 } // namespace message2
1014
1015 U_NAMESPACE_END
1016
1017 /// @cond DOXYGEN_IGNORE
1018 // Export an explicit template instantiation of the std::optional that is used as a
1019 // data member of various MFDataModel classes.
1020 // (When building DLLs for Windows this is required.)
1021 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
1022 // for similar examples.)
1023 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1024 template class U_I18N_API std::optional<icu::message2::data_model::Operator>;
1025 #endif
1026 /// @endcond
1027
1028 U_NAMESPACE_BEGIN
1029
1030 namespace message2 {
1031 namespace data_model {
1032 // Internal only
1033 typedef enum UMarkupType {
1034 UMARKUP_OPEN = 0,
1035 UMARKUP_CLOSE,
1036 UMARKUP_STANDALONE,
1037 UMARKUP_COUNT
1038 } UMarkupType;
1039
1040 /**
1041 * The `Markup` class corresponds to the `markup` nonterminal in the MessageFormat 2
1042 * grammar and the `markup` interface defined in
1043 * https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model/message.json
1044 *
1045 * `Markup` is immutable, copyable and movable.
1046 *
1047 * @internal ICU 75 technology preview
1048 * @deprecated This API is for technology preview only.
1049 */
1050 class U_I18N_API Markup : public UObject {
1051 public:
1052 /**
1053 * Checks if this markup is an opening tag.
1054 *
1055 * @return True if and only if this represents an opening tag.
1056 *
1057 * @internal ICU 75 technology preview
1058 * @deprecated This API is for technology preview only.
1059 */
1060 UBool isOpen() const { return (type == UMARKUP_OPEN); }
1061 /**
1062 * Checks if this markup is an closing tag.
1063 *
1064 * @return True if and only if this represents an closing tag.
1065 *
1066 * @internal ICU 75 technology preview
1067 * @deprecated This API is for technology preview only.
1068 */
1069 UBool isClose() const { return (type == UMARKUP_CLOSE); }
1070 /**
1071 * Checks if this markup is an standalone tag.
1072 *
1073 * @return True if and only if this represents a standalone tag.
1074 *
1075 * @internal ICU 75 technology preview
1076 * @deprecated This API is for technology preview only.
1077 */
1078 UBool isStandalone() const { return (type == UMARKUP_STANDALONE); }
1079 /**
1080 * Gets the name of this markup
1081 *
1082 * @return A reference to the string identifying the markup
1083 *
1084 * @internal ICU 75 technology preview
1085 * @deprecated This API is for technology preview only.
1086 */
1087 const UnicodeString& getName() const { return name; }
1088 /**
1089 * Gets the options of this markup
1090 *
1091 * @return A reference to the string identifying the markup
1092 *
1093 * @internal ICU 75 technology preview
1094 * @deprecated This API is for technology preview only.
1095 */
1096 std::vector<Option> getOptions() const { return options.getOptions(); }
1097 /**
1098 * Gets the attributes of this markup
1099 *
1100 * @return A vector of attributes
1101 *
1102 * @internal ICU 75 technology preview
1103 * @deprecated This API is for technology preview only.
1104 */
1105 std::vector<Option> getAttributes() const { return attributes.getOptions(); }
1106 /**
1107 * Default constructor.
1108 * Puts the Markup into a valid but undefined state.
1109 *
1110 * @internal ICU 75 technology preview
1111 * @deprecated This API is for technology preview only.
1112 */
1113 Markup() {}
1114 /**
1115 * Destructor.
1116 *
1117 * @internal ICU 75 technology preview
1118 * @deprecated This API is for technology preview only.
1119 */
1120 virtual ~Markup();
1121 /**
1122 * The mutable `Markup::Builder` class allows the markup to be constructed
1123 * incrementally.
1124 *
1125 * Builder is not copyable or movable.
1126 *
1127 * @internal ICU 75 technology preview
1128 * @deprecated This API is for technology preview only.
1129 */
1130 class U_I18N_API Builder : public UMemory {
1131 private:
1132 friend class Markup;
1133
1134 UnicodeString name;
1135 OptionMap::Builder options;
1136 OptionMap::Builder attributes;
1137 UMarkupType type = UMARKUP_COUNT;
1138 public:
1139 /**
1140 * Sets the name of this markup.
1141 *
1142 * @param n A string representing the name.
1143 * @return A reference to the builder.
1144 *
1145 * @internal ICU 75 technology preview
1146 * @deprecated This API is for technology preview only.
1147 */
1148 Builder& setName(const UnicodeString& n) { name = n; return *this; }
1149 /**
1150 * Sets this to be an opening markup.
1151 *
1152 * @return A reference to the builder.
1153 *
1154 * @internal ICU 75 technology preview
1155 * @deprecated This API is for technology preview only.
1156 */
1157 Builder& setOpen() { type = UMARKUP_OPEN; return *this; }
1158 /**
1159 * Sets this to be an closing markup.
1160 *
1161 * @return A reference to the builder.
1162 *
1163 * @internal ICU 75 technology preview
1164 * @deprecated This API is for technology preview only.
1165 */
1166 Builder& setClose() { type = UMARKUP_CLOSE; return *this; }
1167 /**
1168 * Sets this to be a standalone markup.
1169 *
1170 * @return A reference to the builder.
1171 *
1172 * @internal ICU 75 technology preview
1173 * @deprecated This API is for technology preview only.
1174 */
1175 Builder& setStandalone() { type = UMARKUP_STANDALONE; return *this; }
1176 /**
1177 * Adds a single option.
1178 *
1179 * @param key The name of the option.
1180 * @param value The value (right-hand side) of the option.
1181 * @param status Input/output error code.
1182 * @return A reference to the builder.
1183 *
1184 * @internal ICU 75 technology preview
1185 * @deprecated This API is for technology preview only.
1186 */
1187 Builder& addOption(const UnicodeString &key, Operand&& value, UErrorCode& status);
1188 /**
1189 * Adds a single attribute.
1190 *
1191 * @param key The name of the attribute.
1192 * @param value The value (right-hand side) of the attribute.
1193 * @param status Input/output error code.
1194 * @return A reference to the builder.
1195 *
1196 * @internal ICU 75 technology preview
1197 * @deprecated This API is for technology preview only.
1198 */
1199 Builder& addAttribute(const UnicodeString &key, Operand&& value, UErrorCode& status);
1200 /**
1201 * Constructs a new immutable `Markup` using the name and type
1202 * and (optionally) options and attributes that were previously set.
1203 * If `setName()` and at least one of `setOpen()`, `setClose()`, and `setStandalone()`
1204 * were not previously called,
1205 * then `status` is set to U_INVALID_STATE_ERROR.
1206 *
1207 * The builder object (`this`) can still be used after calling `build()`.
1208 * The `build()` method is non-const for internal implementation reasons,
1209 * but is observably const.
1210 *
1211 * @param status Input/output error code.
1212 * @return The new Markup.
1213 *
1214 * @internal ICU 75 technology preview
1215 * @deprecated This API is for technology preview only.
1216 */
1217 Markup build(UErrorCode& status);
1218 /**
1219 * Default constructor.
1220 * Returns a Builder with no name, type, options, or attributes set.
1221 *
1222 * @param status Input/output error code.
1223 *
1224 * @internal ICU 75 technology preview
1225 * @deprecated This API is for technology preview only.
1226 */
1227 Builder(UErrorCode& status);
1228 /**
1229 * Destructor.
1230 *
1231 * @internal ICU 75 technology preview
1232 * @deprecated This API is for technology preview only.
1233 */
1234 virtual ~Builder();
1235 Builder(const Builder&) = delete;
1236 Builder& operator=(const Builder&) = delete;
1237 Builder(Builder&&) = delete;
1238 Builder& operator=(Builder&&) = delete;
1239 }; // class Markup::Builder
1240
1241 private:
1242 friend class Builder;
1243 friend class message2::Serializer;
1244
1245 UMarkupType type;
1246 UnicodeString name;
1247 OptionMap options;
1248 OptionMap attributes;
1249 const OptionMap& getOptionsInternal() const { return options; }
1250 const OptionMap& getAttributesInternal() const { return attributes; }
1251 Markup(UMarkupType, UnicodeString, OptionMap&&, OptionMap&&);
1252 }; // class Markup
1253
1254 /**
1255 * The `Expression` class corresponds to the `expression` nonterminal in the MessageFormat 2
1256 * grammar and the `Expression` interface defined in
1257 * https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
1258 *
1259 * It represents either an operand with no annotation; an annotation with no operand;
1260 * or an operand annotated with an annotation.
1261 *
1262 * `Expression` is immutable, copyable and movable.
1263 *
1264 * @internal ICU 75 technology preview
1265 * @deprecated This API is for technology preview only.
1266 */
1267 class U_I18N_API Expression : public UObject {
1268 public:
1269 /**
1270 * Checks if this expression is an annotation
1271 * with no operand.
1272 *
1273 * @return True if and only if the expression has
1274 * an annotation and has no operand.
1275 *
1276 * @internal ICU 75 technology preview
1277 * @deprecated This API is for technology preview only.
1278 */
1279 UBool isStandaloneAnnotation() const;
1280 /**
1281 * Checks if this expression has a function
1282 * annotation (with or without an operand).
1283 *
1284 * @return True if and only if the expression has an annotation
1285 * that is a function.
1286 *
1287 * @internal ICU 75 technology preview
1288 * @deprecated This API is for technology preview only.
1289 */
1290 UBool isFunctionCall() const;
1291 /**
1292 * Accesses the function
1293 * annotating this expression.
1294 * If !(isFunctionCall()), sets
1295 * `status` to U_INVALID_STATE_ERROR.
1296 *
1297 * @param status Input/output error code.
1298 * @return A non-owned pointer to the operator of this expression,
1299 * which does not outlive the expression.
1300 *
1301 * @internal ICU 75 technology preview
1302 * @deprecated This API is for technology preview only.
1303 */
1304 const Operator* getOperator(UErrorCode& status) const;
1305 /**
1306 * Accesses the operand of this expression.
1307 *
1308 * @return A reference to the operand of this expression,
1309 * which may be the null operand.
1310 *
1311 * @internal ICU 75 technology preview
1312 * @deprecated This API is for technology preview only.
1313 */
1314 const Operand& getOperand() const;
1315 /**
1316 * Gets the attributes of this expression
1317 *
1318 * @return A vector of attributes
1319 *
1320 * @internal ICU 75 technology preview
1321 * @deprecated This API is for technology preview only.
1322 */
1323 std::vector<Option> getAttributes() const { return attributes.getOptions(); }
1324 /**
1325 * The mutable `Expression::Builder` class allows the operator to be constructed
1326 * incrementally.
1327 *
1328 * Builder is not copyable or movable.
1329 *
1330 * @internal ICU 75 technology preview
1331 * @deprecated This API is for technology preview only.
1332 */
1333 class U_I18N_API Builder : public UMemory {
1334 private:
1335 friend class Expression;
1336
1337 bool hasOperand = false;
1338 bool hasOperator = false;
1339 Operand rand;
1340 Operator rator;
1341 OptionMap::Builder attributes;
1342 public:
1343 /**
1344 * Sets the operand of this expression.
1345 *
1346 * @param rAnd The operand to set. Passed by move.
1347 * @return A reference to the builder.
1348 *
1349 * @internal ICU 75 technology preview
1350 * @deprecated This API is for technology preview only.
1351 */
1352 Builder& setOperand(Operand&& rAnd);
1353 /**
1354 * Sets the operator of this expression.
1355 *
1356 * @param rAtor The operator to set. Passed by move.
1357 * @return A reference to the builder.
1358 *
1359 * @internal ICU 75 technology preview
1360 * @deprecated This API is for technology preview only.
1361 */
1362 Builder& setOperator(Operator&& rAtor);
1363 /**
1364 * Adds a single attribute.
1365 *
1366 * @param key The name of the attribute.
1367 * @param value The value (right-hand side) of the attribute.
1368 * @param status Input/output error code.
1369 * @return A reference to the builder.
1370 *
1371 * @internal ICU 75 technology preview
1372 * @deprecated This API is for technology preview only.
1373 */
1374 Builder& addAttribute(const UnicodeString &key, Operand&& value, UErrorCode& status);
1375 /**
1376 * Constructs a new immutable `Expression` using the operand and operator that
1377 * were previously set. If neither `setOperand()` nor `setOperator()` was
1378 * previously called, or if `setOperand()` was called with the null operand
1379 * and `setOperator()` was never called, then `status` is set to
1380 * U_INVALID_STATE_ERROR.
1381 *
1382 * The builder object (`this`) can still be used after calling `build()`.
1383 * The `build()` method is non-const for internal implementation reasons,
1384 * but is observably const.
1385
1386 * @param status Input/output error code.
1387 * @return The new Expression.
1388 *
1389 * @internal ICU 75 technology preview
1390 * @deprecated This API is for technology preview only.
1391 */
1392 Expression build(UErrorCode& status);
1393 /**
1394 * Default constructor.
1395 * Returns a Builder with no operator or operand set.
1396 *
1397 * @param status Input/output error code.
1398 *
1399 * @internal ICU 75 technology preview
1400 * @deprecated This API is for technology preview only.
1401 */
1402 Builder(UErrorCode& status);
1403 /**
1404 * Destructor.
1405 *
1406 * @internal ICU 75 technology preview
1407 * @deprecated This API is for technology preview only.
1408 */
1409 virtual ~Builder();
1410 Builder(const Builder&) = delete;
1411 Builder& operator=(const Builder&) = delete;
1412 Builder(Builder&&) = delete;
1413 Builder& operator=(Builder&&) = delete;
1414 }; // class Expression::Builder
1415 /**
1416 * Non-member swap function.
1417 * @param e1 will get e2's contents
1418 * @param e2 will get e1's contents
1419 *
1420 * @internal ICU 75 technology preview
1421 * @deprecated This API is for technology preview only.
1422 */
1423 friend inline void swap(Expression& e1, Expression& e2) noexcept {
1424 using std::swap;
1425
1426 swap(e1.rator, e2.rator);
1427 swap(e1.rand, e2.rand);
1428 swap(e1.attributes, e2.attributes);
1429 }
1430 /**
1431 * Copy constructor.
1432 *
1433 * @internal ICU 75 technology preview
1434 * @deprecated This API is for technology preview only.
1435 */
1436 Expression(const Expression& other);
1437 /**
1438 * Assignment operator.
1439 *
1440 * @internal ICU 75 technology preview
1441 * @deprecated This API is for technology preview only.
1442 */
1443 Expression& operator=(Expression) noexcept;
1444 /**
1445 * Default constructor.
1446 * Puts the Expression into a valid but undefined state.
1447 *
1448 * @internal ICU 75 technology preview
1449 * @deprecated This API is for technology preview only.
1450 */
1451 Expression();
1452 /**
1453 * Destructor.
1454 *
1455 * @internal ICU 75 technology preview
1456 * @deprecated This API is for technology preview only.
1457 */
1458 virtual ~Expression();
1459 private:
1460 friend class message2::Serializer;
1461
1462 /*
1463 Internally, an expression is represented as the application of an optional operator to an operand.
1464 The operand is always present; for function calls with no operand, it's represented
1465 as an operand for which `isNull()` is true.
1466
1467 Operator | Operand
1468 --------------------------------
1469 { |42| :fun opt=value } => (FunctionName=fun, | Literal(quoted=true, contents="42")
1470 options={opt: value})
1471 { abcd } => null | Literal(quoted=false, contents="abcd")
1472 { : fun opt=value } => (FunctionName=fun,
1473 options={opt: value}) | NullOperand()
1474 */
1475
1476 Expression(const Operator &rAtor, const Operand &rAnd, const OptionMap& attrs) : rator(rAtor), rand(rAnd), attributes(attrs) {}
1477 Expression(const Operand &rAnd, const OptionMap& attrs) : rator(std::nullopt), rand(Operand(rAnd)), attributes(attrs) {}
1478 Expression(const Operator &rAtor, const OptionMap& attrs) : rator(rAtor), rand(), attributes(attrs) {}
1479 /* const */ std::optional<Operator> rator;
1480 /* const */ Operand rand;
1481 /* const */ OptionMap attributes;
1482 const OptionMap& getAttributesInternal() const { return attributes; }
1483 }; // class Expression
1484 } // namespace data_model
1485 } // namespace message2
1486
1487 /// @cond DOXYGEN_IGNORE
1488 // Export an explicit template instantiation of the LocalPointer that is used as a
1489 // data member of various MFDataModel classes.
1490 // (When building DLLs for Windows this is required.)
1491 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
1492 // for similar examples.)
1493 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1494 template class U_I18N_API LocalPointerBase<message2::data_model::Expression>;
1495 template class U_I18N_API LocalArray<message2::data_model::Expression>;
1496 #endif
1497 /// @endcond
1498
1499 namespace message2 {
1500 namespace data_model {
1501
1502 class Pattern;
1503
1504 // Despite the comments, `PatternPart` is internal-only
1505 /**
1506 * A `PatternPart` is a single element (text or expression) in a `Pattern`.
1507 * It corresponds to the `body` field of the `Pattern` interface
1508 * defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
1509 *
1510 * `PatternPart` is immutable, copyable and movable.
1511 *
1512 * @internal ICU 75 technology preview
1513 * @deprecated This API is for technology preview only.
1514 */
1515 class PatternPart : public UObject {
1516 public:
1517 /**
1518 * Checks if the part is a text part.
1519 *
1520 * @return True if and only if this is a text part.
1521 *
1522 * @internal ICU 75 technology preview
1523 * @deprecated This API is for technology preview only.
1524 */
1525 UBool isText() const { return std::holds_alternative<UnicodeString>(piece); }
1526 /**
1527 * Checks if the part is a markup part.
1528 *
1529 * @return True if and only if this is a markup part.
1530 *
1531 * @internal ICU 75 technology preview
1532 * @deprecated This API is for technology preview only.
1533 */
1534 UBool isMarkup() const { return std::holds_alternative<Markup>(piece); }
1535 /**
1536 * Checks if the part is an expression part.
1537 *
1538 * @return True if and only if this is an expression part.
1539 *
1540 * @internal ICU 75 technology preview
1541 * @deprecated This API is for technology preview only.
1542 */
1543 UBool isExpression() const { return std::holds_alternative<Expression>(piece); }
1544 /**
1545 * Accesses the expression of the part.
1546 * Precondition: isExpression()
1547 *
1548 * @return A reference to the part's underlying expression.
1549 *
1550 * @internal ICU 75 technology preview
1551 * @deprecated This API is for technology preview only.
1552 */
1553 const Expression& contents() const;
1554 /**
1555 * Accesses the expression of the part.
1556 * Precondition: isMarkup()
1557 *
1558 * @return A reference to the part's underlying expression.
1559 *
1560 * @internal ICU 75 technology preview
1561 * @deprecated This API is for technology preview only.
1562 */
1563 const Markup& asMarkup() const;
1564 /**
1565 * Accesses the text contents of the part.
1566 * Precondition: isText()
1567 *
1568 * @return A reference to a string representing the part's text..
1569 *
1570 * @internal ICU 75 technology preview
1571 * @deprecated This API is for technology preview only.
1572 */
1573 const UnicodeString& asText() const;
1574 /**
1575 * Non-member swap function.
1576 * @param p1 will get p2's contents
1577 * @param p2 will get p1's contents
1578 *
1579 * @internal ICU 75 technology preview
1580 * @deprecated This API is for technology preview only.
1581 */
1582 friend inline void swap(PatternPart& p1, PatternPart& p2) noexcept {
1583 using std::swap;
1584
1585 swap(p1.piece, p2.piece);
1586 }
1587 /**
1588 * Copy constructor.
1589 *
1590 * @internal ICU 75 technology preview
1591 * @deprecated This API is for technology preview only.
1592 */
1593 PatternPart(const PatternPart& other);
1594 /**
1595 * Assignment operator.
1596 *
1597 * @internal ICU 75 technology preview
1598 * @deprecated This API is for technology preview only.
1599 */
1600 PatternPart& operator=(PatternPart) noexcept;
1601 /**
1602 * Destructor.
1603 *
1604 * @internal ICU 75 technology preview
1605 * @deprecated This API is for technology preview only.
1606 */
1607 virtual ~PatternPart();
1608 /**
1609 * Text part constructor. Returns a text pattern part
1610 * with text `t`.
1611 *
1612 * @param t A text string.
1613 *
1614 * @internal ICU 75 technology preview
1615 * @deprecated This API is for technology preview only.
1616 */
1617 explicit PatternPart(const UnicodeString& t) : piece(t) {}
1618 /**
1619 * Expression part constructor. Returns an Expression pattern
1620 * part with expression `e`.
1621 *
1622 * @param e An Expression.
1623 *
1624 * @internal ICU 75 technology preview
1625 * @deprecated This API is for technology preview only.
1626 */
1627 explicit PatternPart(Expression&& e) : piece(e) {}
1628 /**
1629 * Markup part constructor. Returns a Markup pattern
1630 * part with markup `m`
1631 *
1632 * @param m A Markup.
1633 *
1634 * @internal ICU 75 technology preview
1635 * @deprecated This API is for technology preview only.
1636 */
1637 explicit PatternPart(Markup&& m) : piece(m) {}
1638 /**
1639 * Default constructor.
1640 * Puts the PatternPart into a valid but undefined state.
1641 *
1642 * @internal ICU 75 technology preview
1643 * @deprecated This API is for technology preview only.
1644 */
1645 PatternPart() = default;
1646 private:
1647 friend class Pattern;
1648
1649 std::variant<UnicodeString, Expression, Markup> piece;
1650 }; // class PatternPart
1651 } // namespace data_model
1652 } // namespace message2
1653
1654 /// @cond DOXYGEN_IGNORE
1655 // Export an explicit template instantiation of the LocalPointer that is used as a
1656 // data member of various MFDataModel classes.
1657 // (When building DLLs for Windows this is required.)
1658 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
1659 // for similar examples.)
1660 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
1661 template class U_I18N_API LocalPointerBase<message2::data_model::PatternPart>;
1662 template class U_I18N_API LocalArray<message2::data_model::PatternPart>;
1663 #endif
1664 /// @endcond
1665
1666 namespace message2 {
1667 namespace data_model {
1668 /**
1669 * A `Pattern` is a sequence of formattable parts.
1670 * It corresponds to the `Pattern` interface
1671 * defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#patterns
1672 *
1673 * `Pattern` is immutable, copyable and movable.
1674 *
1675 * @internal ICU 75 technology preview
1676 * @deprecated This API is for technology preview only.
1677 */
1678 class U_I18N_API Pattern : public UObject {
1679 private:
1680 friend class PatternPart;
1681
1682 public:
1683 struct Iterator;
1684 /**
1685 * Returns the parts of this pattern
1686 *
1687 * @return A forward iterator of variants. Each element is either a string (text part)
1688 * or an expression part.
1689 *
1690 * @internal ICU 75 technology preview
1691 * @deprecated This API is for technology preview only.
1692 */
1693 Iterator begin() const {
1694 return Iterator(this, 0);
1695 }
1696 /**
1697 * Returns a special value to mark the end of iteration
1698 *
1699 * @return A forward iterator of variants. This should only be used for comparisons
1700 * against an iterator returned by incrementing begin().
1701 *
1702 * @internal ICU 75 technology preview
1703 * @deprecated This API is for technology preview only.
1704 */
1705 Iterator end() const {
1706 return Iterator(this, len);
1707 }
1708 /**
1709 * The mutable `Pattern::Builder` class allows the pattern to be
1710 * constructed one part at a time.
1711 *
1712 * Builder is not copyable or movable.
1713 *
1714 * @internal ICU 75 technology preview
1715 * @deprecated This API is for technology preview only.
1716 */
1717 class U_I18N_API Builder : public UMemory {
1718 private:
1719 friend class Pattern;
1720
1721 UVector* parts; // Not a LocalPointer for the same reason as in `SelectorKeys::Builder`
1722
1723 public:
1724 /**
1725 * Adds a single expression part to the pattern.
1726 *
1727 * @param part The part to be added (passed by move)
1728 * @param status Input/output error code.
1729 * @return A reference to the builder.
1730 *
1731 * @internal ICU 75 technology preview
1732 * @deprecated This API is for technology preview only.
1733 */
1734 Builder& add(Expression&& part, UErrorCode& status) noexcept;
1735 /**
1736 * Adds a single markup part to the pattern.
1737 *
1738 * @param part The part to be added (passed by move)
1739 * @param status Input/output error code.
1740 * @return A reference to the builder.
1741 *
1742 * @internal ICU 75 technology preview
1743 * @deprecated This API is for technology preview only.
1744 */
1745 Builder& add(Markup&& part, UErrorCode& status) noexcept;
1746 /**
1747 * Adds a single text part to the pattern. Copies `part`.
1748 *
1749 * @param part The part to be added (passed by move)
1750 * @param status Input/output error code.
1751 * @return A reference to the builder.
1752 *
1753 * @internal ICU 75 technology preview
1754 * @deprecated This API is for technology preview only.
1755 */
1756 Builder& add(UnicodeString&& part, UErrorCode& status) noexcept;
1757 /**
1758 * Constructs a new immutable `Pattern` using the list of parts
1759 * set with previous `add()` calls.
1760 *
1761 * The builder object (`this`) can still be used after calling `build()`.
1762 *
1763 * @param status Input/output error code.
1764 * @return The pattern object
1765 *
1766 * @internal ICU 75 technology preview
1767 * @deprecated This API is for technology preview only.
1768 */
1769 Pattern build(UErrorCode& status) const noexcept;
1770 /**
1771 * Default constructor.
1772 * Returns a Builder with an empty sequence of PatternParts.
1773 *
1774 * @param status Input/output error code
1775 *
1776 * @internal ICU 75 technology preview
1777 * @deprecated This API is for technology preview only.
1778 */
1779 Builder(UErrorCode& status);
1780 /**
1781 * Destructor.
1782 *
1783 * @internal ICU 75 technology preview
1784 * @deprecated This API is for technology preview only.
1785 */
1786 virtual ~Builder();
1787 Builder(const Builder&) = delete;
1788 Builder& operator=(const Builder&) = delete;
1789 Builder(Builder&&) = delete;
1790 Builder& operator=(Builder&&) = delete;
1791 }; // class Pattern::Builder
1792
1793 /**
1794 * Default constructor.
1795 * Puts the Pattern into a valid but undefined state.
1796 *
1797 * @internal ICU 75 technology preview
1798 * @deprecated This API is for technology preview only.
1799 */
1800 Pattern() : parts(LocalArray<PatternPart>()) {}
1801 /**
1802 * Non-member swap function.
1803 * @param p1 will get p2's contents
1804 * @param p2 will get p1's contents
1805 *
1806 * @internal ICU 75 technology preview
1807 * @deprecated This API is for technology preview only.
1808 */
1809 friend inline void swap(Pattern& p1, Pattern& p2) noexcept {
1810 using std::swap;
1811
1812 swap(p1.bogus, p2.bogus);
1813 swap(p1.len, p2.len);
1814 swap(p1.parts, p2.parts);
1815 }
1816 /**
1817 * Copy constructor.
1818 *
1819 * @internal ICU 75 technology preview
1820 * @deprecated This API is for technology preview only.
1821 */
1822 Pattern(const Pattern& other);
1823 /**
1824 * Assignment operator
1825 *
1826 * @internal ICU 75 technology preview
1827 * @deprecated This API is for technology preview only.
1828 */
1829 Pattern& operator=(Pattern) noexcept;
1830 /**
1831 * Destructor.
1832 *
1833 * @internal ICU 75 technology preview
1834 * @deprecated This API is for technology preview only.
1835 */
1836 virtual ~Pattern();
1837
1838 /**
1839 * The `Pattern::Iterator` class provides an iterator over the formattable
1840 * parts of a pattern.
1841 *
1842 * `Pattern::Iterator` is mutable and is not copyable or movable.
1843 *
1844 * @internal ICU 75 technology preview
1845 * @deprecated This API is for technology preview only.
1846 */
1847 struct U_I18N_API Iterator {
1848 private:
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&;
1854
1855 friend class Pattern;
1856 Iterator(const Pattern* p, int32_t i) : pos(i), pat(p) {}
1857 friend bool operator== (const Iterator& a, const Iterator& b) { return (a.pat == b.pat && a.pos == b.pos); }
1858
1859 int32_t pos;
1860 const Pattern* pat;
1861
1862 public:
1863 /**
1864 * Dereference operator (gets the element at the current iterator position)
1865 *
1866 * @internal ICU 75 technology preview
1867 * @deprecated This API is for technology preview only.
1868 */
1869 reference operator*() const {
1870 const PatternPart& part = pat->parts[pos];
1871 return patternContents(part);
1872 }
1873 /**
1874 * Increment operator (advances to the next iterator position)
1875 *
1876 * @internal ICU 75 technology preview
1877 * @deprecated This API is for technology preview only.
1878 */
1879 Iterator operator++() { pos++; return *this; }
1880 /**
1881 * Inequality comparison operator (used for comparing an iterator to the result of end())
1882 *
1883 * @internal ICU 75 technology preview
1884 * @deprecated This API is for technology preview only.
1885 */
1886 friend bool operator!= (const Iterator& a, const Iterator& b) { return !(a == b); }
1887 }; // struct Iterator
1888
1889 private:
1890 friend class Builder;
1891 friend class message2::MessageFormatter;
1892 friend class message2::Serializer;
1893
1894 // Set to true if a copy constructor fails;
1895 // needed in order to distinguish an uninitialized
1896 // Pattern from a 0-length pattern
1897 bool bogus = false;
1898
1899 // Possibly-empty array of parts
1900 int32_t len = 0;
1901 LocalArray<PatternPart> parts;
1902
1903 Pattern(const UVector& parts, UErrorCode& status);
1904 // Helper
1905 static void initParts(Pattern&, const Pattern&);
1906
1907 /**
1908 * Returns the size.
1909 *
1910 * @return The number of parts in the pattern.
1911 *
1912 * @internal ICU 75 technology preview
1913 * @deprecated This API is for technology preview only.
1914 */
1915 int32_t numParts() const;
1916 /**
1917 * Returns the `i`th part in the pattern.
1918 * Precondition: i < numParts()
1919 *
1920 * @param i Index of the part being accessed.
1921 * @return A reference to the part at index `i`.
1922 *
1923 * @internal ICU 75 technology preview
1924 * @deprecated This API is for technology preview only.
1925 */
1926 const PatternPart& getPart(int32_t i) const;
1927
1928 // Gets around not being able to declare Pattern::Iterator as a friend
1929 // in PatternPart
1930 static const std::variant<UnicodeString, Expression, Markup>&
1931 patternContents(const PatternPart& p) { return p.piece; }
1932 }; // class Pattern
1933
1934 /**
1935 * A `Variant` pairs a list of keys with a pattern
1936 * It corresponds to the `Variant` interface
1937 * defined in https://github.com/unicode-org/message-format-wg/tree/main/spec/data-model
1938 *
1939 * `Variant` is immutable, copyable and movable.
1940 *
1941 * @internal ICU 75 technology preview
1942 * @deprecated This API is for technology preview only.
1943 */
1944 class U_I18N_API Variant : public UObject {
1945 public:
1946 /**
1947 * Accesses the pattern of the variant.
1948 *
1949 * @return A reference to the pattern.
1950 *
1951 * @internal ICU 75 technology preview
1952 * @deprecated This API is for technology preview only.
1953 */
1954 const Pattern& getPattern() const { return p; }
1955 /**
1956 * Accesses the keys of the variant.
1957 *
1958 * @return A reference to the keys.
1959 *
1960 * @internal ICU 75 technology preview
1961 * @deprecated This API is for technology preview only.
1962 */
1963 const SelectorKeys& getKeys() const { return k; }
1964 /**
1965 * Constructor. Returns a variant that formats to `pattern`
1966 * when `keys` match the selector expressions in the enclosing
1967 * `match` construct.
1968 *
1969 * @param keys A reference to a `SelectorKeys`.
1970 * @param pattern A pattern (passed by move)
1971 *
1972 * @internal ICU 75 technology preview
1973 * @deprecated This API is for technology preview only.
1974 */
1975 Variant(const SelectorKeys& keys, Pattern&& pattern) : k(keys), p(std::move(pattern)) {}
1976 /**
1977 * Non-member swap function.
1978 * @param v1 will get v2's contents
1979 * @param v2 will get v1's contents
1980 *
1981 * @internal ICU 75 technology preview
1982 * @deprecated This API is for technology preview only.
1983 */
1984 friend inline void swap(Variant& v1, Variant& v2) noexcept {
1985 using std::swap;
1986
1987 swap(v1.k, v2.k);
1988 swap(v1.p, v2.p);
1989 }
1990 /**
1991 * Assignment operator
1992 *
1993 * @internal ICU 75 technology preview
1994 * @deprecated This API is for technology preview only.
1995 */
1996 Variant& operator=(Variant other) noexcept;
1997 /**
1998 * Default constructor.
1999 * Returns a Variant in a valid but undefined state.
2000 *
2001 * @internal ICU 75 technology preview
2002 * @deprecated This API is for technology preview only.
2003 */
2004 Variant() = default;
2005 /**
2006 * Copy constructor.
2007 *
2008 * @internal ICU 75 technology preview
2009 * @deprecated This API is for technology preview only.
2010 */
2011 Variant(const Variant&);
2012 /**
2013 * Destructor.
2014 *
2015 * @internal ICU 75 technology preview
2016 * @deprecated This API is for technology preview only.
2017 */
2018 virtual ~Variant();
2019 private:
2020 /* const */ SelectorKeys k;
2021 /* const */ Pattern p;
2022 }; // class Variant
2023 } // namespace data_model
2024
2025 namespace data_model {
2026 /**
2027 * A `Binding` pairs a variable name with an expression.
2028 * It corresponds to the `Declaration` interface
2029 * defined in https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md#messages
2030 *
2031 * `Binding` is immutable and copyable. It is not movable.
2032 *
2033 * @internal ICU 75 technology preview
2034 * @deprecated This API is for technology preview only.
2035 */
2036 class U_I18N_API Binding : public UObject {
2037 public:
2038 /**
2039 * Accesses the right-hand side of a binding.
2040 *
2041 * @return A reference to the expression.
2042 *
2043 * @internal ICU 75 technology preview
2044 * @deprecated This API is for technology preview only.
2045 */
2046 const Expression& getValue() const;
2047 /**
2048 * Accesses the left-hand side of the binding.
2049 *
2050 * @return A reference to the variable name.
2051 *
2052 * @internal ICU 75 technology preview
2053 * @deprecated This API is for technology preview only.
2054 */
2055 const VariableName& getVariable() const { return var; }
2056 /**
2057 * Constructor for input binding.
2058 *
2059 * @param variableName The variable name (left-hand side) of the binding. Passed by move.
2060 * @param rhs The right-hand side of the input binding. Passed by move.
2061 * `rhs` must have an operand that is a variable reference to `variableName`.
2062 * If `rhs` has an operator, it must be a function call.
2063 * If either of these properties is violated, `errorCode` is set to
2064 * U_INVALID_STATE_ERROR.
2065 * @param errorCode Input/output error code
2066 *
2067 * @internal ICU 75 technology preview
2068 * @deprecated This API is for technology preview only.
2069 */
2070 static Binding input(UnicodeString&& variableName, Expression&& rhs, UErrorCode& errorCode);
2071 /**
2072 * Returns true if and only if this binding represents a local declaration.
2073 * Otherwise, it's an input declaration.
2074 *
2075 * @return True if this binding represents a variable and expression;
2076 * false if it represents a variable plus an annotation.
2077 */
2078 UBool isLocal() const { return local; }
2079 /**
2080 * Constructor.
2081 *
2082 * @param v A variable name.
2083 * @param e An expression.
2084 *
2085 * @internal ICU 75 technology preview
2086 * @deprecated This API is for technology preview only.
2087 */
2088 Binding(const VariableName& v, Expression&& e) : var(v), expr(std::move(e)), local(true), annotation(nullptr) {}
2089 /**
2090 * Non-member swap function.
2091 * @param b1 will get b2's contents
2092 * @param b2 will get b1's contents
2093 *
2094 * @internal ICU 75 technology preview
2095 * @deprecated This API is for technology preview only.
2096 */
2097 friend inline void swap(Binding& b1, Binding& b2) noexcept {
2098 using std::swap;
2099
2100 swap(b1.var, b2.var);
2101 swap(b1.expr, b2.expr);
2102 swap(b1.local, b2.local);
2103 b1.updateAnnotation();
2104 b2.updateAnnotation();
2105 }
2106 /**
2107 * Copy constructor.
2108 *
2109 * @internal ICU 75 technology preview
2110 * @deprecated This API is for technology preview only.
2111 */
2112 Binding(const Binding& other);
2113 /**
2114 * Copy assignment operator
2115 *
2116 * @internal ICU 75 technology preview
2117 * @deprecated This API is for technology preview only.
2118 */
2119 Binding& operator=(Binding) noexcept;
2120 /**
2121 * Default constructor.
2122 * Puts the Binding into a valid but undefined state.
2123 *
2124 * @internal ICU 75 technology preview
2125 * @deprecated This API is for technology preview only.
2126 */
2127 Binding() : local(true) {}
2128 /**
2129 * Destructor.
2130 *
2131 * @internal ICU 75 technology preview
2132 * @deprecated This API is for technology preview only.
2133 */
2134 virtual ~Binding();
2135 private:
2136 friend class message2::Checker;
2137 friend class message2::MessageFormatter;
2138 friend class message2::Parser;
2139 friend class message2::Serializer;
2140
2141 /* const */ VariableName var;
2142 /* const */ Expression expr;
2143 /* const */ bool local;
2144
2145 // The following field is always nullptr for a local
2146 // declaration, and possibly nullptr for an .input declaration
2147 // If non-null, the referent is a member of `expr` so
2148 // its lifetime is the same as the lifetime of the enclosing Binding
2149 // (as long as there's no mutation)
2150 const Operator* annotation = nullptr;
2151
2152 const OptionMap& getOptionsInternal() const;
2153
2154 bool hasAnnotation() const { return !local && (annotation != nullptr); }
2155 void updateAnnotation();
2156 }; // class Binding
2157 } // namespace data_model
2158 } // namespace message2
2159
2160 /// @cond DOXYGEN_IGNORE
2161 // Export an explicit template instantiation of the LocalPointer that is used as a
2162 // data member of various MFDataModel classes.
2163 // (When building DLLs for Windows this is required.)
2164 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
2165 // for similar examples.)
2166 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
2167 template class U_I18N_API LocalPointerBase<message2::data_model::Variant>;
2168 template class U_I18N_API LocalPointerBase<message2::data_model::Binding>;
2169 template class U_I18N_API LocalArray<message2::data_model::Variant>;
2170 template class U_I18N_API LocalArray<message2::data_model::Binding>;
2171 #endif
2172 /// @endcond
2173
2174 namespace message2 {
2175 using namespace data_model;
2176
2177
2178 // Internal only
2179
2180 class MFDataModel;
2181
2182 #ifndef U_IN_DOXYGEN
2183 class Matcher : public UObject {
2184 public:
2185 Matcher& operator=(Matcher);
2186 Matcher(const Matcher&);
2187 /**
2188 * Non-member swap function.
2189 * @param m1 will get m2's contents
2190 * @param m2 will get m1's contents
2191 *
2192 * @internal ICU 75 technology preview
2193 * @deprecated This API is for technology preview only.
2194 */
2195 friend inline void swap(Matcher& m1, Matcher& m2) noexcept {
2196 using std::swap;
2197
2198 if (m1.bogus) {
2199 m2.bogus = true;
2200 return;
2201 }
2202 if (m2.bogus) {
2203 m1.bogus = true;
2204 return;
2205 }
2206 swap(m1.selectors, m2.selectors);
2207 swap(m1.numSelectors, m2.numSelectors);
2208 swap(m1.variants, m2.variants);
2209 swap(m1.numVariants, m2.numVariants);
2210 }
2211 virtual ~Matcher();
2212 private:
2213
2214 friend class MFDataModel;
2215
2216 Matcher(VariableName* ss, int32_t ns, Variant* vs, int32_t nv);
2217 Matcher() {}
2218
2219 // A Matcher may have numSelectors=0 and numVariants=0
2220 // (this is a data model error, but it's representable).
2221 // So we have to keep a separate flag to track failed copies.
2222 bool bogus = false;
2223
2224 // The variables that are being matched on.
2225 LocalArray<VariableName> selectors;
2226 // The number of selectors
2227 int32_t numSelectors = 0;
2228 // The list of `when` clauses (case arms).
2229 LocalArray<Variant> variants;
2230 // The number of variants
2231 int32_t numVariants = 0;
2232 }; // class Matcher
2233 #endif
2234 } // namespace message2
2235
2236 U_NAMESPACE_END
2237
2238 /// @cond DOXYGEN_IGNORE
2239 // Export an explicit template instantiation of the std::variant that is used as a
2240 // data member of various MFDataModel classes.
2241 // (When building DLLs for Windows this is required.)
2242 // (See measunit_impl.h, datefmt.h, collationiterator.h, erarules.h and others
2243 // for similar examples.)
2244 #if U_PF_WINDOWS <= U_PLATFORM && U_PLATFORM <= U_PF_CYGWIN
2245 #if defined(U_REAL_MSVC) && defined(_MSVC_STL_VERSION)
2246 template class U_I18N_API std::_Variant_storage_<false, icu::message2::Matcher,icu::message2::data_model::Pattern>;
2247 #endif
2248 template class U_I18N_API std::variant<icu::message2::Matcher,icu::message2::data_model::Pattern>;
2249 #endif
2250 /// @endcond
2251
2252 U_NAMESPACE_BEGIN
2253
2254 namespace message2 {
2255 // -----------------------------------------------------------------------
2256 // Public MFDataModel class
2257
2258 /**
2259 *
2260 * The `MFDataModel` class describes a parsed representation of the text of a message.
2261 * This representation is public as higher-level APIs for messages will need to know its public
2262 * interface: for example, to re-instantiate a parsed message with different values for imported
2263 variables.
2264 *
2265 * The MFDataModel API implements <a target="github"
2266 href="https://github.com/unicode-org/message-format-wg/blob/main/spec/data-model.md">the
2267 * specification of the abstract syntax (data model representation)</a> for MessageFormat.
2268 *
2269 * `MFDataModel` is immutable, copyable and movable.
2270 *
2271 * @internal ICU 75 technology preview
2272 * @deprecated This API is for technology preview only.
2273 */
2274 class U_I18N_API MFDataModel : public UMemory {
2275 /*
2276 Classes that represent nodes in the data model are nested inside the
2277 `MFDataModel` class.
2278
2279 Classes such as `Expression`, `Pattern` and `VariantMap` are immutable and
2280 are constructed using the builder pattern.
2281
2282 Most classes representing nodes have copy constructors. This is because builders
2283 contain immutable data that must be copied when calling `build()`, since the builder
2284 could go out of scope before the immutable result of the builder does. Copying is
2285 also necessary to prevent unexpected mutation if intermediate builders are saved
2286 and mutated again after calling `build()`.
2287
2288 The copy constructors perform a deep copy, for example by copying the entire
2289 list of options for an `Operator` (and copying the entire underlying vector.)
2290 Some internal fields should be `const`, but are declared as non-`const` to make
2291 the copy constructor simpler to implement. (These are noted throughout.) In
2292 other words, those fields are `const` except during the execution of a copy
2293 constructor.
2294
2295 On the other hand, intermediate `Builder` methods that return a `Builder&`
2296 mutate the state of the builder, so in code like:
2297
2298 Expression::Builder& exprBuilder = Expression::builder()-> setOperand(foo);
2299 Expression::Builder& exprBuilder2 = exprBuilder.setOperator(bar);
2300
2301 the call to `setOperator()` would mutate `exprBuilder`, since `exprBuilder`
2302 and `exprBuilder2` are references to the same object.
2303
2304 An alternate choice would be to make `build()` destructive, so that copying would
2305 be unnecessary. Or, both copying and moving variants of `build()` could be
2306 provided. Copying variants of the intermediate `Builder` methods could be
2307 provided as well, if this proved useful.
2308 */
2309 public:
2310 /**
2311 * Accesses the local variable declarations for this data model.
2312 *
2313 * @return A vector of bindings for local variables.
2314 *
2315 * @internal ICU 75 technology preview
2316 * @deprecated This API is for technology preview only.
2317 */
2318 std::vector<Binding> getLocalVariables() const {
2319 std::vector<Binding> result;
2320 if (!bogus) {
2321 return toStdVector<Binding>(bindings.getAlias(), bindingsLen);
2322 }
2323 return {};
2324 }
2325 /**
2326 * Accesses the selectors. Returns an empty vector if this is a pattern message.
2327 *
2328 * @return A vector of selectors.
2329 *
2330 * @internal ICU 75 technology preview
2331 * @deprecated This API is for technology preview only.
2332 */
2333 std::vector<VariableName> getSelectors() const {
2334 if (std::holds_alternative<Pattern>(body)) {
2335 return {};
2336 }
2337 const Matcher* match = std::get_if<Matcher>(&body);
2338 // match must be non-null, given the previous check
2339 return toStdVector<VariableName>(match->selectors.getAlias(), match->numSelectors);
2340 }
2341 /**
2342 * Accesses the variants. Returns an empty vector if this is a pattern message.
2343 *
2344 * @return A vector of variants.
2345 *
2346 * @internal ICU 75 technology preview
2347 * @deprecated This API is for technology preview only.
2348 */
2349 std::vector<Variant> getVariants() const {
2350 // Return empty vector if no variants
2351 if (std::holds_alternative<Pattern>(body)) {
2352 return {};
2353 }
2354 const Matcher* match = std::get_if<Matcher>(&body);
2355 // match must be non-null, given the previous check
2356 return toStdVector<Variant>(match->variants.getAlias(), match->numVariants);
2357 return {};
2358 }
2359 /**
2360 * Accesses the pattern (in a message without selectors).
2361 * Returns a reference to an empty pattern if the message has selectors.
2362 *
2363 * @return A reference to the pattern.
2364 *
2365 * @internal ICU 75 technology preview
2366 * @deprecated This API is for technology preview only.
2367 */
2368 const Pattern& getPattern() const;
2369
2370 /**
2371 * The mutable `MFDataModel::Builder` class allows the data model to be
2372 * constructed incrementally.
2373 *
2374 * @internal ICU 75 technology preview
2375 * @deprecated This API is for technology preview only.
2376 */
2377 class U_I18N_API Builder;
2378
2379 /**
2380 * Default constructor.
2381 * Puts the MFDataModel into a valid but undefined state.
2382 *
2383 * @internal ICU 75 technology preview
2384 * @deprecated This API is for technology preview only.
2385 */
2386 MFDataModel();
2387 /**
2388 * Non-member swap function.
2389 * @param m1 will get m2's contents
2390 * @param m2 will get m1's contents
2391 *
2392 * @internal ICU 75 technology preview
2393 * @deprecated This API is for technology preview only.
2394 */
2395 friend inline void swap(MFDataModel& m1, MFDataModel& m2) noexcept {
2396 using std::swap;
2397
2398 if (m1.bogus) {
2399 m2.bogus = true;
2400 return;
2401 }
2402 if (m2.bogus) {
2403 m1.bogus = true;
2404 return;
2405 }
2406 swap(m1.body, m2.body);
2407 swap(m1.bindings, m2.bindings);
2408 swap(m1.bindingsLen, m2.bindingsLen);
2409 }
2410 /**
2411 * Assignment operator
2412 *
2413 * @internal ICU 75 technology preview
2414 * @deprecated This API is for technology preview only.
2415 */
2416 MFDataModel& operator=(MFDataModel) noexcept;
2417 /**
2418 * Copy constructor.
2419 *
2420 * @internal ICU 75 technology preview
2421 * @deprecated This API is for technology preview only.
2422 */
2423 MFDataModel(const MFDataModel& other);
2424 /**
2425 * Destructor.
2426 *
2427 * @internal ICU 75 technology preview
2428 * @deprecated This API is for technology preview only.
2429 */
2430 virtual ~MFDataModel();
2431
2432 /**
2433 * The mutable `MFDataModel::Builder` class allows the data model to be
2434 * constructed incrementally. Builder is not copyable or movable.
2435 *
2436 * @internal ICU 75 technology preview
2437 * @deprecated This API is for technology preview only.
2438 */
2439 class U_I18N_API Builder : public UMemory {
2440 private:
2441 friend class MFDataModel;
2442
2443 void checkDuplicate(const VariableName&, UErrorCode&) const;
2444 void buildSelectorsMessage(UErrorCode&);
2445 bool hasPattern = true;
2446 bool hasSelectors = false;
2447 Pattern pattern;
2448 // The following members are not LocalPointers for the same reason as in SelectorKeys::Builder
2449 UVector* selectors = nullptr;
2450 UVector* variants = nullptr;
2451 UVector* bindings = nullptr;
2452 public:
2453 /**
2454 * Adds a binding, There must not already be a binding
2455 * with the same name.
2456 *
2457 * @param b The binding. Passed by move.
2458 * @param status Input/output error code. Set to U_DUPLICATE_DECLARATION_ERROR
2459 * if `addBinding()` was previously called with a binding
2460 * with the same variable name as `b`.
2461 *
2462 * @internal ICU 75 technology preview
2463 * @deprecated This API is for technology preview only.
2464 */
2465 Builder& addBinding(Binding&& b, UErrorCode& status);
2466 /**
2467 * Adds a selector variable.
2468 * If a pattern was previously set, clears the pattern.
2469 *
2470 * @param selector Variable to add as a selector. Passed by move.
2471 * @param errorCode Input/output error code
2472 * @return A reference to the builder.
2473 *
2474 * @internal ICU 75 technology preview
2475 * @deprecated This API is for technology preview only.
2476 */
2477 Builder& addSelector(VariableName&& selector, UErrorCode& errorCode);
2478 /**
2479 * Adds a single variant.
2480 * If a pattern was previously set using `setPattern()`, clears the pattern.
2481 *
2482 * @param keys Keys for the variant. Passed by move.
2483 * @param pattern Pattern for the variant. Passed by move.
2484 * @param errorCode Input/output error code
2485 * @return A reference to the builder.
2486 *
2487 * @internal ICU 75 technology preview
2488 * @deprecated This API is for technology preview only.
2489 */
2490 Builder& addVariant(SelectorKeys&& keys, Pattern&& pattern, UErrorCode& errorCode) noexcept;
2491 /**
2492 * Sets the body of the message as a pattern.
2493 * If selectors and/or variants were previously set, clears them.
2494 *
2495 * @param pattern Pattern to represent the body of the message.
2496 * Passed by move.
2497 * @return A reference to the builder.
2498 *
2499 * @internal ICU 75 technology preview
2500 * @deprecated This API is for technology preview only.
2501 */
2502 Builder& setPattern(Pattern&& pattern);
2503 /**
2504 * Constructs a new immutable data model.
2505 * If `setPattern()` has not been called and if `addSelector()` and
2506 * `addVariant()` were not each called at least once,
2507 * `status` is set to `U_INVALID_STATE_ERROR`.
2508 * If `addSelector()` was called and `addVariant()` was never called,
2509 * or vice versa, then `status` is set to U_INVALID_STATE_ERROR.
2510 * Otherwise, either a Pattern or Selectors message is constructed
2511 * based on the pattern that was previously set, or selectors and variants
2512 * that were previously set.
2513 *
2514 * The builder object (`this`) can still be used after calling `build()`.
2515 *
2516 * @param status Input/output error code.
2517 * @return The new MFDataModel
2518 *
2519 * @internal ICU 75 technology preview
2520 * @deprecated This API is for technology preview only.
2521 */
2522 MFDataModel build(UErrorCode& status) const noexcept;
2523 /**
2524 * Default constructor.
2525 * Returns a Builder with no pattern or selectors set.
2526 * Either `setPattern()` or both `addSelector()` and
2527 * `addVariant()` must be called before calling `build()`
2528 * on the resulting builder.
2529 *
2530 * @param status Input/output error code.
2531 *
2532 * @internal ICU 75 technology preview
2533 * @deprecated This API is for technology preview only.
2534 */
2535 Builder(UErrorCode& status);
2536 /**
2537 * Destructor.
2538 *
2539 * @internal ICU 75 technology preview
2540 * @deprecated This API is for technology preview only.
2541 */
2542 virtual ~Builder();
2543 Builder(const Builder&) = delete;
2544 Builder& operator=(const Builder&) = delete;
2545 Builder(Builder&&) = delete;
2546 Builder& operator=(Builder&&) = delete;
2547 }; // class Builder
2548
2549 private:
2550 friend class Checker;
2551 friend class MessageFormatter;
2552 friend class Serializer;
2553
2554 Pattern empty; // Provided so that `getPattern()` can return a result
2555 // if called on a selectors message
2556 bool hasPattern() const { return std::holds_alternative<Pattern>(body); }
2557
2558 bool bogus = false; // Set if a copy constructor fails
2559
2560 // A message body is either a matcher (selector list and variant list),
2561 // or a single pattern
2562 std::variant<Matcher, Pattern> body;
2563
2564 // Bindings for local variables
2565 /* const */ LocalArray<Binding> bindings;
2566 int32_t bindingsLen = 0;
2567
2568 const Binding* getLocalVariablesInternal() const;
2569 const VariableName* getSelectorsInternal() const;
2570 const Variant* getVariantsInternal() const;
2571
2572 int32_t numSelectors() const {
2573 const Matcher* matcher = std::get_if<Matcher>(&body);
2574 return (matcher == nullptr ? 0 : matcher->numSelectors);
2575 }
2576 int32_t numVariants() const {
2577 const Matcher* matcher = std::get_if<Matcher>(&body);
2578 return (matcher == nullptr ? 0 : matcher->numVariants);
2579 }
2580
2581 // Helper
2582 void initBindings(const Binding*);
2583
2584 MFDataModel(const Builder& builder, UErrorCode&) noexcept;
2585 }; // class MFDataModel
2586
2587 } // namespace message2
2588
2589 U_NAMESPACE_END
2590
2591 #endif // U_HIDE_DEPRECATED_API
2592
2593 #endif /* #if !UCONFIG_NO_MF2 */
2594
2595 #endif /* #if !UCONFIG_NO_FORMATTING */
2596
2597 #endif /* #if !UCONFIG_NO_NORMALIZATION */
2598
2599 #endif /* U_SHOW_CPLUSPLUS_API */
2600
2601 #endif // MESSAGEFORMAT_DATA_MODEL_H
2602
2603 // eof
2604