fieldpos.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. // © 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. /*
  4. ********************************************************************************
  5. * Copyright (C) 1997-2006, International Business Machines
  6. * Corporation and others. All Rights Reserved.
  7. ********************************************************************************
  8. *
  9. * File FIELDPOS.H
  10. *
  11. * Modification History:
  12. *
  13. * Date Name Description
  14. * 02/25/97 aliu Converted from java.
  15. * 03/17/97 clhuang Updated per Format implementation.
  16. * 07/17/98 stephen Added default/copy ctors, and operators =, ==, !=
  17. ********************************************************************************
  18. */
  19. // *****************************************************************************
  20. // This file was generated from the java source file FieldPosition.java
  21. // *****************************************************************************
  22. #ifndef FIELDPOS_H
  23. #define FIELDPOS_H
  24. #include "unicode/utypes.h"
  25. #if U_SHOW_CPLUSPLUS_API
  26. /**
  27. * \file
  28. * \brief C++ API: FieldPosition identifies the fields in a formatted output.
  29. */
  30. #if !UCONFIG_NO_FORMATTING
  31. #include "unicode/uobject.h"
  32. U_NAMESPACE_BEGIN
  33. /**
  34. * <code>FieldPosition</code> is a simple class used by <code>Format</code>
  35. * and its subclasses to identify fields in formatted output. Fields are
  36. * identified by constants, whose names typically end with <code>_FIELD</code>,
  37. * defined in the various subclasses of <code>Format</code>. See
  38. * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
  39. * an example.
  40. *
  41. * <p>
  42. * <code>FieldPosition</code> keeps track of the position of the
  43. * field within the formatted output with two indices: the index
  44. * of the first character of the field and the index of the last
  45. * character of the field.
  46. *
  47. * <p>
  48. * One version of the <code>format</code> method in the various
  49. * <code>Format</code> classes requires a <code>FieldPosition</code>
  50. * object as an argument. You use this <code>format</code> method
  51. * to perform partial formatting or to get information about the
  52. * formatted output (such as the position of a field).
  53. *
  54. * The FieldPosition class is not intended for public subclassing.
  55. *
  56. * <p>
  57. * Below is an example of using <code>FieldPosition</code> to aid
  58. * alignment of an array of formatted floating-point numbers on
  59. * their decimal points:
  60. * <pre>
  61. * \code
  62. * double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789,
  63. * 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
  64. * int dNumSize = (int)(sizeof(doubleNum)/sizeof(double));
  65. *
  66. * UErrorCode status = U_ZERO_ERROR;
  67. * DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
  68. * fmt->setDecimalSeparatorAlwaysShown(true);
  69. *
  70. * const int tempLen = 20;
  71. * char temp[tempLen];
  72. *
  73. * for (int i=0; i<dNumSize; i++) {
  74. * FieldPosition pos(NumberFormat::INTEGER_FIELD);
  75. * UnicodeString buf;
  76. * char fmtText[tempLen];
  77. * ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
  78. * for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces
  79. * temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0';
  80. * cout << temp << fmtText << endl;
  81. * }
  82. * delete fmt;
  83. * \endcode
  84. * </pre>
  85. * <p>
  86. * The code will generate the following output:
  87. * <pre>
  88. * \code
  89. * 123,456,789.000
  90. * -12,345,678.900
  91. * 1,234,567.880
  92. * -123,456.789
  93. * 12,345.678
  94. * -1,234.567
  95. * 123.456
  96. * -12.345
  97. * 1.234
  98. * \endcode
  99. * </pre>
  100. */
  101. class U_I18N_API FieldPosition : public UObject {
  102. public:
  103. /**
  104. * DONT_CARE may be specified as the field to indicate that the
  105. * caller doesn't need to specify a field.
  106. * @stable ICU 2.0
  107. */
  108. enum { DONT_CARE = -1 };
  109. /**
  110. * Creates a FieldPosition object with a non-specified field.
  111. * @stable ICU 2.0
  112. */
  113. FieldPosition()
  114. : UObject(), fField(DONT_CARE), fBeginIndex(0), fEndIndex(0) {}
  115. /**
  116. * Creates a FieldPosition object for the given field. Fields are
  117. * identified by constants, whose names typically end with _FIELD,
  118. * in the various subclasses of Format.
  119. *
  120. * @see NumberFormat#INTEGER_FIELD
  121. * @see NumberFormat#FRACTION_FIELD
  122. * @see DateFormat#YEAR_FIELD
  123. * @see DateFormat#MONTH_FIELD
  124. * @stable ICU 2.0
  125. */
  126. FieldPosition(int32_t field)
  127. : UObject(), fField(field), fBeginIndex(0), fEndIndex(0) {}
  128. /**
  129. * Copy constructor
  130. * @param copy the object to be copied from.
  131. * @stable ICU 2.0
  132. */
  133. FieldPosition(const FieldPosition& copy)
  134. : UObject(copy), fField(copy.fField), fBeginIndex(copy.fBeginIndex), fEndIndex(copy.fEndIndex) {}
  135. /**
  136. * Destructor
  137. * @stable ICU 2.0
  138. */
  139. virtual ~FieldPosition();
  140. /**
  141. * Assignment operator
  142. * @param copy the object to be copied from.
  143. * @stable ICU 2.0
  144. */
  145. FieldPosition& operator=(const FieldPosition& copy);
  146. /**
  147. * Equality operator.
  148. * @param that the object to be compared with.
  149. * @return true if the two field positions are equal, false otherwise.
  150. * @stable ICU 2.0
  151. */
  152. bool operator==(const FieldPosition& that) const;
  153. /**
  154. * Equality operator.
  155. * @param that the object to be compared with.
  156. * @return true if the two field positions are not equal, false otherwise.
  157. * @stable ICU 2.0
  158. */
  159. bool operator!=(const FieldPosition& that) const;
  160. /**
  161. * Clone this object.
  162. * Clones can be used concurrently in multiple threads.
  163. * If an error occurs, then nullptr is returned.
  164. * The caller must delete the clone.
  165. *
  166. * @return a clone of this object
  167. *
  168. * @see getDynamicClassID
  169. * @stable ICU 2.8
  170. */
  171. FieldPosition *clone() const;
  172. /**
  173. * Retrieve the field identifier.
  174. * @return the field identifier.
  175. * @stable ICU 2.0
  176. */
  177. int32_t getField() const { return fField; }
  178. /**
  179. * Retrieve the index of the first character in the requested field.
  180. * @return the index of the first character in the requested field.
  181. * @stable ICU 2.0
  182. */
  183. int32_t getBeginIndex() const { return fBeginIndex; }
  184. /**
  185. * Retrieve the index of the character following the last character in the
  186. * requested field.
  187. * @return the index of the character following the last character in the
  188. * requested field.
  189. * @stable ICU 2.0
  190. */
  191. int32_t getEndIndex() const { return fEndIndex; }
  192. /**
  193. * Set the field.
  194. * @param f the new value of the field.
  195. * @stable ICU 2.0
  196. */
  197. void setField(int32_t f) { fField = f; }
  198. /**
  199. * Set the begin index. For use by subclasses of Format.
  200. * @param bi the new value of the begin index
  201. * @stable ICU 2.0
  202. */
  203. void setBeginIndex(int32_t bi) { fBeginIndex = bi; }
  204. /**
  205. * Set the end index. For use by subclasses of Format.
  206. * @param ei the new value of the end index
  207. * @stable ICU 2.0
  208. */
  209. void setEndIndex(int32_t ei) { fEndIndex = ei; }
  210. /**
  211. * ICU "poor man's RTTI", returns a UClassID for the actual class.
  212. *
  213. * @stable ICU 2.2
  214. */
  215. virtual UClassID getDynamicClassID() const override;
  216. /**
  217. * ICU "poor man's RTTI", returns a UClassID for this class.
  218. *
  219. * @stable ICU 2.2
  220. */
  221. static UClassID U_EXPORT2 getStaticClassID();
  222. private:
  223. /**
  224. * Input: Desired field to determine start and end offsets for.
  225. * The meaning depends on the subclass of Format.
  226. */
  227. int32_t fField;
  228. /**
  229. * Output: Start offset of field in text.
  230. * If the field does not occur in the text, 0 is returned.
  231. */
  232. int32_t fBeginIndex;
  233. /**
  234. * Output: End offset of field in text.
  235. * If the field does not occur in the text, 0 is returned.
  236. */
  237. int32_t fEndIndex;
  238. };
  239. inline FieldPosition&
  240. FieldPosition::operator=(const FieldPosition& copy)
  241. {
  242. fField = copy.fField;
  243. fEndIndex = copy.fEndIndex;
  244. fBeginIndex = copy.fBeginIndex;
  245. return *this;
  246. }
  247. inline bool
  248. FieldPosition::operator==(const FieldPosition& copy) const
  249. {
  250. return (fField == copy.fField &&
  251. fEndIndex == copy.fEndIndex &&
  252. fBeginIndex == copy.fBeginIndex);
  253. }
  254. inline bool
  255. FieldPosition::operator!=(const FieldPosition& copy) const
  256. {
  257. return !operator==(copy);
  258. }
  259. U_NAMESPACE_END
  260. #endif /* #if !UCONFIG_NO_FORMATTING */
  261. #endif /* U_SHOW_CPLUSPLUS_API */
  262. #endif // _FIELDPOS
  263. //eof