• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // © 2022 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 
4 #ifndef __DISPLAYOPTIONS_H__
5 #define __DISPLAYOPTIONS_H__
6 
7 #include "unicode/utypes.h"
8 
9 #if U_SHOW_CPLUSPLUS_API
10 
11 #if !UCONFIG_NO_FORMATTING
12 
13 /**
14  * \file
15  * \brief C++ API: Display options class
16  *
17  * This class is designed as a more modern version of the UDisplayContext mechanism.
18  */
19 
20 #include "unicode/udisplayoptions.h"
21 #include "unicode/uversion.h"
22 
23 U_NAMESPACE_BEGIN
24 
25 #ifndef U_HIDE_DRAFT_API
26 
27 /**
28  * Represents all the display options that are supported by CLDR such as grammatical case, noun
29  * class, ... etc. It currently supports enums, but may be extended in the future to have other
30  * types of data. It replaces a DisplayContext[] as a method parameter.
31  *
32  * NOTE: This class is Immutable, and uses a Builder interface.
33  *
34  * For example:
35  * ```
36  * DisplayOptions x =
37  *     DisplayOptions::builder().
38  *         .setGrammaticalCase(UDISPOPT_GRAMMATICAL_CASE_DATIVE)
39  *         .setPluralCategory(UDISPOPT_PLURAL_CATEGORY_FEW)
40  *         .build();
41  * ```
42  *
43  * @draft ICU 72
44  */
45 class U_I18N_API DisplayOptions {
46 public:
47     /**
48      * Responsible for building `DisplayOptions`.
49      *
50      * @draft ICU 72
51      */
52     class U_I18N_API Builder {
53     public:
54         /**
55          * Sets the grammatical case.
56          *
57          * @param grammaticalCase The grammatical case.
58          * @return Builder
59          * @draft ICU 72
60          */
setGrammaticalCase(UDisplayOptionsGrammaticalCase optGrammaticalCase)61         Builder &setGrammaticalCase(UDisplayOptionsGrammaticalCase optGrammaticalCase) {
62             this->grammaticalCase = optGrammaticalCase;
63             return *this;
64         }
65 
66         /**
67          * Sets the noun class.
68          *
69          * @param nounClass The noun class.
70          * @return Builder
71          * @draft ICU 72
72          */
setNounClass(UDisplayOptionsNounClass optNounClass)73         Builder &setNounClass(UDisplayOptionsNounClass optNounClass) {
74             this->nounClass = optNounClass;
75             return *this;
76         }
77 
78         /**
79          * Sets the plural category.
80          *
81          * @param pluralCategory The plural category.
82          * @return Builder
83          * @draft ICU 72
84          */
setPluralCategory(UDisplayOptionsPluralCategory optPluralCategory)85         Builder &setPluralCategory(UDisplayOptionsPluralCategory optPluralCategory) {
86             this->pluralCategory = optPluralCategory;
87             return *this;
88         }
89 
90         /**
91          * Sets the capitalization.
92          *
93          * @param capitalization The capitalization.
94          * @return Builder
95          * @draft ICU 72
96          */
setCapitalization(UDisplayOptionsCapitalization optCapitalization)97         Builder &setCapitalization(UDisplayOptionsCapitalization optCapitalization) {
98             this->capitalization = optCapitalization;
99             return *this;
100         }
101 
102         /**
103          * Sets the dialect handling.
104          *
105          * @param nameStyle The name style.
106          * @return Builder
107          * @draft ICU 72
108          */
setNameStyle(UDisplayOptionsNameStyle optNameStyle)109         Builder &setNameStyle(UDisplayOptionsNameStyle optNameStyle) {
110             this->nameStyle = optNameStyle;
111             return *this;
112         }
113 
114         /**
115          * Sets the display length.
116          *
117          * @param displayLength The display length.
118          * @return Builder
119          * @draft ICU 72
120          */
setDisplayLength(UDisplayOptionsDisplayLength optDisplayLength)121         Builder &setDisplayLength(UDisplayOptionsDisplayLength optDisplayLength) {
122             this->displayLength = optDisplayLength;
123             return *this;
124         }
125 
126         /**
127          * Sets the substitute handling.
128          *
129          * @param substituteHandling The substitute handling.
130          * @return Builder
131          * @draft ICU 72
132          */
setSubstituteHandling(UDisplayOptionsSubstituteHandling optSubstituteHandling)133         Builder &setSubstituteHandling(UDisplayOptionsSubstituteHandling optSubstituteHandling) {
134             this->substituteHandling = optSubstituteHandling;
135             return *this;
136         }
137 
138         /**
139          * Builds the display options.
140          *
141          * @return DisplayOptions
142          * @draft ICU 72
143          */
build()144         DisplayOptions build() { return DisplayOptions(*this); }
145 
146     private:
147         friend DisplayOptions;
148 
149         Builder();
150         Builder(const DisplayOptions &displayOptions);
151 
152         UDisplayOptionsGrammaticalCase grammaticalCase;
153         UDisplayOptionsNounClass nounClass;
154         UDisplayOptionsPluralCategory pluralCategory;
155         UDisplayOptionsCapitalization capitalization;
156         UDisplayOptionsNameStyle nameStyle;
157         UDisplayOptionsDisplayLength displayLength;
158         UDisplayOptionsSubstituteHandling substituteHandling;
159     };
160 
161     /**
162      * Creates a builder with the `UNDEFINED` values for all the parameters.
163      *
164      * @return Builder
165      * @draft ICU 72
166      */
167     static Builder builder();
168     /**
169      * Creates a builder with the same parameters from this object.
170      *
171      * @return Builder
172      * @draft ICU 72
173      */
174     Builder copyToBuilder() const;
175     /**
176      * Gets the grammatical case.
177      *
178      * @return UDisplayOptionsGrammaticalCase
179      * @draft ICU 72
180      */
getGrammaticalCase()181     UDisplayOptionsGrammaticalCase getGrammaticalCase() const { return grammaticalCase; }
182 
183     /**
184      * Gets the noun class.
185      *
186      * @return UDisplayOptionsNounClass
187      * @draft ICU 72
188      */
getNounClass()189     UDisplayOptionsNounClass getNounClass() const { return nounClass; }
190 
191     /**
192      * Gets the plural category.
193      *
194      * @return UDisplayOptionsPluralCategory
195      * @draft ICU 72
196      */
getPluralCategory()197     UDisplayOptionsPluralCategory getPluralCategory() const { return pluralCategory; }
198 
199     /**
200      * Gets the capitalization.
201      *
202      * @return UDisplayOptionsCapitalization
203      * @draft ICU 72
204      */
getCapitalization()205     UDisplayOptionsCapitalization getCapitalization() const { return capitalization; }
206 
207     /**
208      * Gets the dialect handling.
209      *
210      * @return UDisplayOptionsNameStyle
211      * @draft ICU 72
212      */
getNameStyle()213     UDisplayOptionsNameStyle getNameStyle() const { return nameStyle; }
214 
215     /**
216      * Gets the display length.
217      *
218      * @return UDisplayOptionsDisplayLength
219      * @draft ICU 72
220      */
getDisplayLength()221     UDisplayOptionsDisplayLength getDisplayLength() const { return displayLength; }
222 
223     /**
224      * Gets the substitute handling.
225      *
226      * @return UDisplayOptionsSubstituteHandling
227      * @draft ICU 72
228      */
getSubstituteHandling()229     UDisplayOptionsSubstituteHandling getSubstituteHandling() const { return substituteHandling; }
230 
231     /**
232      * Copies the DisplayOptions.
233      *
234      * @param other The options to copy.
235      * @draft ICU 72
236      */
237     DisplayOptions &operator=(const DisplayOptions &other) = default;
238 
239     /**
240      * Moves the DisplayOptions.
241      *
242      * @param other The options to move from.
243      * @draft ICU 72
244      */
245     DisplayOptions &operator=(DisplayOptions &&other) noexcept = default;
246 
247     /**
248      * Copies the DisplayOptions.
249      *
250      * @param other The options to copy.
251      * @draft ICU 72
252      */
253     DisplayOptions(const DisplayOptions &other) = default;
254 
255 private:
256     DisplayOptions(const Builder &builder);
257     UDisplayOptionsGrammaticalCase grammaticalCase;
258     UDisplayOptionsNounClass nounClass;
259     UDisplayOptionsPluralCategory pluralCategory;
260     UDisplayOptionsCapitalization capitalization;
261     UDisplayOptionsNameStyle nameStyle;
262     UDisplayOptionsDisplayLength displayLength;
263     UDisplayOptionsSubstituteHandling substituteHandling;
264 };
265 
266 #endif // U_HIDE_DRAFT_API
267 
268 U_NAMESPACE_END
269 
270 #endif /* #if !UCONFIG_NO_FORMATTING */
271 
272 #endif /* U_SHOW_CPLUSPLUS_API */
273 
274 #endif // __DISPLAYOPTIONS_H__
275