]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fieldpos.h
2 ********************************************************************************
3 * Copyright (C) 1997-2005, 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 ********************************************************************************
18 // *****************************************************************************
19 // This file was generated from the java source file FieldPosition.java
20 // *****************************************************************************
25 #include "unicode/utypes.h"
29 * \brief C++ API: FieldPosition identifies the fields in a formatted output.
32 #if !UCONFIG_NO_FORMATTING
34 #include "unicode/uobject.h"
39 * <code>FieldPosition</code> is a simple class used by <code>Format</code>
40 * and its subclasses to identify fields in formatted output. Fields are
41 * identified by constants, whose names typically end with <code>_FIELD</code>,
42 * defined in the various subclasses of <code>Format</code>. See
43 * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
47 * <code>FieldPosition</code> keeps track of the position of the
48 * field within the formatted output with two indices: the index
49 * of the first character of the field and the index of the last
50 * character of the field.
53 * One version of the <code>format</code> method in the various
54 * <code>Format</code> classes requires a <code>FieldPosition</code>
55 * object as an argument. You use this <code>format</code> method
56 * to perform partial formatting or to get information about the
57 * formatted output (such as the position of a field).
59 * The FieldPosition class is not suitable for subclassing.
62 * Below is an example of using <code>FieldPosition</code> to aid
63 * alignment of an array of formatted floating-point numbers on
64 * their decimal points:
67 * double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789,
68 * 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
69 * int dNumSize = (int)(sizeof(doubleNum)/sizeof(double));
71 * UErrorCode status = U_ZERO_ERROR;
72 * DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
73 * fmt->setDecimalSeparatorAlwaysShown(true);
75 * const int tempLen = 20;
78 * for (int i=0; i<dNumSize; i++) {
79 * FieldPosition pos(NumberFormat::INTEGER_FIELD);
81 * char fmtText[tempLen];
82 * ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
83 * for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces
84 * temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0';
85 * cout << temp << fmtText << endl;
91 * The code will generate the following output:
106 class U_I18N_API FieldPosition
: public UObject
{
109 * DONT_CARE may be specified as the field to indicate that the
110 * caller doesn't need to specify a field. Do not subclass.
112 enum { DONT_CARE
= -1 };
115 * Creates a FieldPosition object with a non-specified field.
119 : UObject(), fField(DONT_CARE
), fBeginIndex(0), fEndIndex(0) {}
122 * Creates a FieldPosition object for the given field. Fields are
123 * identified by constants, whose names typically end with _FIELD,
124 * in the various subclasses of Format.
126 * @see NumberFormat#INTEGER_FIELD
127 * @see NumberFormat#FRACTION_FIELD
128 * @see DateFormat#YEAR_FIELD
129 * @see DateFormat#MONTH_FIELD
132 FieldPosition(int32_t field
)
133 : UObject(), fField(field
), fBeginIndex(0), fEndIndex(0) {}
137 * @param copy the object to be copied from.
140 FieldPosition(const FieldPosition
& copy
)
141 : UObject(copy
), fField(copy
.fField
), fBeginIndex(copy
.fBeginIndex
), fEndIndex(copy
.fEndIndex
) {}
147 virtual ~FieldPosition();
150 * Assignment operator
151 * @param copy the object to be copied from.
154 FieldPosition
& operator=(const FieldPosition
& copy
);
158 * @param that the object to be compared with.
159 * @return TRUE if the two field positions are equal, FALSE otherwise.
162 UBool
operator==(const FieldPosition
& that
) const;
166 * @param that the object to be compared with.
167 * @return TRUE if the two field positions are not equal, FALSE otherwise.
170 UBool
operator!=(const FieldPosition
& that
) const;
174 * Clones can be used concurrently in multiple threads.
175 * If an error occurs, then NULL is returned.
176 * The caller must delete the clone.
178 * @return a clone of this object
180 * @see getDynamicClassID
183 FieldPosition
*clone() const;
186 * Retrieve the field identifier.
187 * @return the field identifier.
190 int32_t getField(void) const { return fField
; }
193 * Retrieve the index of the first character in the requested field.
194 * @return the index of the first character in the requested field.
197 int32_t getBeginIndex(void) const { return fBeginIndex
; }
200 * Retrieve the index of the character following the last character in the
202 * @return the index of the character following the last character in the
206 int32_t getEndIndex(void) const { return fEndIndex
; }
210 * @param f the new value of the field.
213 void setField(int32_t f
) { fField
= f
; }
216 * Set the begin index. For use by subclasses of Format.
217 * @param bi the new value of the begin index
220 void setBeginIndex(int32_t bi
) { fBeginIndex
= bi
; }
223 * Set the end index. For use by subclasses of Format.
224 * @param ei the new value of the end index
227 void setEndIndex(int32_t ei
) { fEndIndex
= ei
; }
230 * ICU "poor man's RTTI", returns a UClassID for the actual class.
234 virtual UClassID
getDynamicClassID() const;
237 * ICU "poor man's RTTI", returns a UClassID for this class.
241 static UClassID U_EXPORT2
getStaticClassID();
245 * Input: Desired field to determine start and end offsets for.
246 * The meaning depends on the subclass of Format.
251 * Output: Start offset of field in text.
252 * If the field does not occur in the text, 0 is returned.
257 * Output: End offset of field in text.
258 * If the field does not occur in the text, 0 is returned.
263 inline FieldPosition
&
264 FieldPosition::operator=(const FieldPosition
& copy
)
266 fField
= copy
.fField
;
267 fEndIndex
= copy
.fEndIndex
;
268 fBeginIndex
= copy
.fBeginIndex
;
273 FieldPosition::operator==(const FieldPosition
& copy
) const
275 if( fField
!= copy
.fField
||
276 fEndIndex
!= copy
.fEndIndex
||
277 fBeginIndex
!= copy
.fBeginIndex
)
284 FieldPosition::operator!=(const FieldPosition
& copy
) const
286 return !operator==(copy
);
291 #endif /* #if !UCONFIG_NO_FORMATTING */