]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fieldpos.h
2 ********************************************************************************
3 * Copyright (C) 1997-2003, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 ********************************************************************************
9 * Modification History:
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 ********************************************************************************
17 // *****************************************************************************
18 // This file was generated from the java source file FieldPosition.java
19 // *****************************************************************************
24 #include "unicode/utypes.h"
26 #if !UCONFIG_NO_FORMATTING
28 #include "unicode/uobject.h"
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
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.
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).
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:
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));
63 * UErrorCode status = U_ZERO_ERROR;
64 * DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
65 * fmt->setDecimalSeparatorAlwaysShown(true);
67 * const int tempLen = 20;
70 * for (int i=0; i<dNumSize; i++) {
71 * FieldPosition pos(NumberFormat::INTEGER_FIELD);
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;
83 * The code will generate the following output:
98 class U_I18N_API FieldPosition
: public UObject
{
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.
104 enum { DONT_CARE
= -1 };
107 * Creates a FieldPosition object with a non-specified field.
111 : UObject(), fField(DONT_CARE
), fBeginIndex(0), fEndIndex(0) {}
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.
118 * @see NumberFormat#INTEGER_FIELD
119 * @see NumberFormat#FRACTION_FIELD
120 * @see DateFormat#YEAR_FIELD
121 * @see DateFormat#MONTH_FIELD
124 FieldPosition(int32_t field
)
125 : UObject(), fField(field
), fBeginIndex(0), fEndIndex(0) {}
129 * @param copy the object to be copied from.
132 FieldPosition(const FieldPosition
& copy
)
133 : UObject(copy
), fField(copy
.fField
), fBeginIndex(copy
.fBeginIndex
), fEndIndex(copy
.fEndIndex
) {}
142 * Assignment operator
143 * @param copy the object to be copied from.
146 FieldPosition
& operator=(const FieldPosition
& copy
);
150 * @param that the object to be compared with.
151 * @return TRUE if the two field positions are equal, FALSE otherwise.
154 UBool
operator==(const FieldPosition
& that
) const;
158 * @param that the object to be compared with.
159 * @return TRUE if the two field positions are not equal, FALSE otherwise.
162 UBool
operator!=(const FieldPosition
& that
) const;
165 * Retrieve the field identifier.
166 * @return the field identifier.
169 int32_t getField(void) const { return fField
; }
172 * Retrieve the index of the first character in the requested field.
173 * @return the index of the first character in the requested field.
176 int32_t getBeginIndex(void) const { return fBeginIndex
; }
179 * Retrieve the index of the character following the last character in the
181 * @return the index of the character following the last character in the
185 int32_t getEndIndex(void) const { return fEndIndex
; }
189 * @param f the new value of the field.
192 void setField(int32_t f
) { fField
= f
; }
195 * Set the begin index. For use by subclasses of Format.
196 * @param bi the new value of the begin index
199 void setBeginIndex(int32_t bi
) { fBeginIndex
= bi
; }
202 * Set the end index. For use by subclasses of Format.
203 * @param ei the new value of the end index
206 void setEndIndex(int32_t ei
) { fEndIndex
= ei
; }
209 * ICU "poor man's RTTI", returns a UClassID for the actual class.
213 virtual inline UClassID
getDynamicClassID() const;
216 * ICU "poor man's RTTI", returns a UClassID for this class.
220 static inline UClassID
getStaticClassID();
224 * Input: Desired field to determine start and end offsets for.
225 * The meaning depends on the subclass of Format.
230 * Output: Start offset of field in text.
231 * If the field does not occur in the text, 0 is returned.
236 * Output: End offset of field in text.
237 * If the field does not occur in the text, 0 is returned.
242 * The address of this static class variable serves as this class's ID
243 * for ICU "poor man's RTTI".
245 static const char fgClassID
;
248 inline UClassID
FieldPosition::getStaticClassID()
249 { return (UClassID
)&fgClassID
; }
251 inline UClassID
FieldPosition::getDynamicClassID() const
252 { return FieldPosition::getStaticClassID(); }
254 inline FieldPosition
&
255 FieldPosition::operator=(const FieldPosition
& copy
)
257 fField
= copy
.fField
;
258 fEndIndex
= copy
.fEndIndex
;
259 fBeginIndex
= copy
.fBeginIndex
;
264 FieldPosition::operator==(const FieldPosition
& copy
) const
266 if( fField
!= copy
.fField
||
267 fEndIndex
!= copy
.fEndIndex
||
268 fBeginIndex
!= copy
.fBeginIndex
)
275 FieldPosition::operator!=(const FieldPosition
& copy
) const
277 return !operator==(copy
);
282 #endif /* #if !UCONFIG_NO_FORMATTING */