displayoptions.h 7.1 KB

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