]> git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fieldpos.h
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / i18n / unicode / fieldpos.h
1 /*
2 ********************************************************************************
3 * Copyright (C) 1997-2003, 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
21 #ifndef FIELDPOS_H
22 #define FIELDPOS_H
23
24 #include "unicode/utypes.h"
25
26 #if !UCONFIG_NO_FORMATTING
27
28 #include "unicode/uobject.h"
29
30 U_NAMESPACE_BEGIN
31
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 * <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 /**
107 * Creates a FieldPosition object with a non-specified field.
108 * @stable ICU 2.0
109 */
110 FieldPosition()
111 : UObject(), fField(DONT_CARE), fBeginIndex(0), fEndIndex(0) {}
112
113 /**
114 * Creates a FieldPosition object for the given field. Fields are
115 * identified by constants, whose names typically end with _FIELD,
116 * in the various subclasses of Format.
117 *
118 * @see NumberFormat#INTEGER_FIELD
119 * @see NumberFormat#FRACTION_FIELD
120 * @see DateFormat#YEAR_FIELD
121 * @see DateFormat#MONTH_FIELD
122 * @stable ICU 2.0
123 */
124 FieldPosition(int32_t field)
125 : UObject(), fField(field), fBeginIndex(0), fEndIndex(0) {}
126
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 /**
136 * Destructor
137 * @stable ICU 2.0
138 */
139 ~FieldPosition() {}
140
141 /**
142 * Assignment operator
143 * @param copy the object to be copied from.
144 * @stable ICU 2.0
145 */
146 FieldPosition& operator=(const FieldPosition& copy);
147
148 /**
149 * Equality operator.
150 * @param that the object to be compared with.
151 * @return TRUE if the two field positions are equal, FALSE otherwise.
152 * @stable ICU 2.0
153 */
154 UBool operator==(const FieldPosition& that) const;
155
156 /**
157 * Equality operator.
158 * @param that the object to be compared with.
159 * @return TRUE if the two field positions are not equal, FALSE otherwise.
160 * @stable ICU 2.0
161 */
162 UBool operator!=(const FieldPosition& that) const;
163
164 /**
165 * Retrieve the field identifier.
166 * @return the field identifier.
167 * @stable ICU 2.0
168 */
169 int32_t getField(void) const { return fField; }
170
171 /**
172 * Retrieve the index of the first character in the requested field.
173 * @return the index of the first character in the requested field.
174 * @stable ICU 2.0
175 */
176 int32_t getBeginIndex(void) const { return fBeginIndex; }
177
178 /**
179 * Retrieve the index of the character following the last character in the
180 * requested field.
181 * @return the index of the character following the last character in the
182 * requested field.
183 * @stable ICU 2.0
184 */
185 int32_t getEndIndex(void) const { return fEndIndex; }
186
187 /**
188 * Set the field.
189 * @param f the new value of the field.
190 * @stable ICU 2.0
191 */
192 void setField(int32_t f) { fField = f; }
193
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 /**
202 * Set the end index. For use by subclasses of Format.
203 * @param ei the new value of the end index
204 * @stable ICU 2.0
205 */
206 void setEndIndex(int32_t ei) { fEndIndex = ei; }
207
208 /**
209 * ICU "poor man's RTTI", returns a UClassID for the actual class.
210 *
211 * @draft ICU 2.2
212 */
213 virtual inline UClassID getDynamicClassID() const;
214
215 /**
216 * ICU "poor man's RTTI", returns a UClassID for this class.
217 *
218 * @draft ICU 2.2
219 */
220 static inline UClassID getStaticClassID();
221
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 /**
230 * Output: Start offset of field in text.
231 * If the field does not occur in the text, 0 is returned.
232 */
233 int32_t fBeginIndex;
234
235 /**
236 * Output: End offset of field in text.
237 * If the field does not occur in the text, 0 is returned.
238 */
239 int32_t fEndIndex;
240
241 /**
242 * The address of this static class variable serves as this class's ID
243 * for ICU "poor man's RTTI".
244 */
245 static const char fgClassID;
246 };
247
248 inline UClassID FieldPosition::getStaticClassID()
249 { return (UClassID)&fgClassID; }
250
251 inline UClassID FieldPosition::getDynamicClassID() const
252 { return FieldPosition::getStaticClassID(); }
253
254 inline FieldPosition&
255 FieldPosition::operator=(const FieldPosition& copy)
256 {
257 fField = copy.fField;
258 fEndIndex = copy.fEndIndex;
259 fBeginIndex = copy.fBeginIndex;
260 return *this;
261 }
262
263 inline UBool
264 FieldPosition::operator==(const FieldPosition& copy) const
265 {
266 if( fField != copy.fField ||
267 fEndIndex != copy.fEndIndex ||
268 fBeginIndex != copy.fBeginIndex)
269 return FALSE;
270 else
271 return TRUE;
272 }
273
274 inline UBool
275 FieldPosition::operator!=(const FieldPosition& copy) const
276 {
277 return !operator==(copy);
278 }
279
280 U_NAMESPACE_END
281
282 #endif /* #if !UCONFIG_NO_FORMATTING */
283
284 #endif // _FIELDPOS
285 //eof