fieldpos.h 8.6 KB

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