fieldpos.h 8.5 KB

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