displayoptions.h 7.1 KB

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