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