]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/unicode/fieldpos.h
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 ********************************************************************************
5 * Copyright (C) 1997-2006, International Business Machines
6 * Corporation and others. All Rights Reserved.
7 ********************************************************************************
11 * Modification History:
13 * Date Name Description
14 * 02/25/97 aliu Converted from java.
15 * 03/17/97 clhuang Updated per Format implementation.
16 * 07/17/98 stephen Added default/copy ctors, and operators =, ==, !=
17 ********************************************************************************
20 // *****************************************************************************
21 // This file was generated from the java source file FieldPosition.java
22 // *****************************************************************************
27 #include "unicode/utypes.h"
31 * \brief C++ API: FieldPosition identifies the fields in a formatted output.
34 #if !UCONFIG_NO_FORMATTING
36 #include "unicode/uobject.h"
38 #if U_SHOW_CPLUSPLUS_API
42 * <code>FieldPosition</code> is a simple class used by <code>Format</code>
43 * and its subclasses to identify fields in formatted output. Fields are
44 * identified by constants, whose names typically end with <code>_FIELD</code>,
45 * defined in the various subclasses of <code>Format</code>. See
46 * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
50 * <code>FieldPosition</code> keeps track of the position of the
51 * field within the formatted output with two indices: the index
52 * of the first character of the field and the index of the last
53 * character of the field.
56 * One version of the <code>format</code> method in the various
57 * <code>Format</code> classes requires a <code>FieldPosition</code>
58 * object as an argument. You use this <code>format</code> method
59 * to perform partial formatting or to get information about the
60 * formatted output (such as the position of a field).
62 * The FieldPosition class is not intended for public subclassing.
65 * Below is an example of using <code>FieldPosition</code> to aid
66 * alignment of an array of formatted floating-point numbers on
67 * their decimal points:
70 * double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789,
71 * 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
72 * int dNumSize = (int)(sizeof(doubleNum)/sizeof(double));
74 * UErrorCode status = U_ZERO_ERROR;
75 * DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
76 * fmt->setDecimalSeparatorAlwaysShown(true);
78 * const int tempLen = 20;
81 * for (int i=0; i<dNumSize; i++) {
82 * FieldPosition pos(NumberFormat::INTEGER_FIELD);
84 * char fmtText[tempLen];
85 * ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
86 * for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces
87 * temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0';
88 * cout << temp << fmtText << endl;
94 * The code will generate the following output:
109 class U_I18N_API FieldPosition
: public UObject
{
112 * DONT_CARE may be specified as the field to indicate that the
113 * caller doesn't need to specify a field.
116 enum { DONT_CARE
= -1 };
119 * Creates a FieldPosition object with a non-specified field.
123 : UObject(), fField(DONT_CARE
), fBeginIndex(0), fEndIndex(0) {}
126 * Creates a FieldPosition object for the given field. Fields are
127 * identified by constants, whose names typically end with _FIELD,
128 * in the various subclasses of Format.
130 * @see NumberFormat#INTEGER_FIELD
131 * @see NumberFormat#FRACTION_FIELD
132 * @see DateFormat#YEAR_FIELD
133 * @see DateFormat#MONTH_FIELD
136 FieldPosition(int32_t field
)
137 : UObject(), fField(field
), fBeginIndex(0), fEndIndex(0) {}
141 * @param copy the object to be copied from.
144 FieldPosition(const FieldPosition
& copy
)
145 : UObject(copy
), fField(copy
.fField
), fBeginIndex(copy
.fBeginIndex
), fEndIndex(copy
.fEndIndex
) {}
151 virtual ~FieldPosition();
154 * Assignment operator
155 * @param copy the object to be copied from.
158 FieldPosition
& operator=(const FieldPosition
& copy
);
162 * @param that the object to be compared with.
163 * @return TRUE if the two field positions are equal, FALSE otherwise.
166 UBool
operator==(const FieldPosition
& that
) const;
170 * @param that the object to be compared with.
171 * @return TRUE if the two field positions are not equal, FALSE otherwise.
174 UBool
operator!=(const FieldPosition
& that
) const;
178 * Clones can be used concurrently in multiple threads.
179 * If an error occurs, then NULL is returned.
180 * The caller must delete the clone.
182 * @return a clone of this object
184 * @see getDynamicClassID
187 FieldPosition
*clone() const;
190 * Retrieve the field identifier.
191 * @return the field identifier.
194 int32_t getField(void) const { return fField
; }
197 * Retrieve the index of the first character in the requested field.
198 * @return the index of the first character in the requested field.
201 int32_t getBeginIndex(void) const { return fBeginIndex
; }
204 * Retrieve the index of the character following the last character in the
206 * @return the index of the character following the last character in the
210 int32_t getEndIndex(void) const { return fEndIndex
; }
214 * @param f the new value of the field.
217 void setField(int32_t f
) { fField
= f
; }
220 * Set the begin index. For use by subclasses of Format.
221 * @param bi the new value of the begin index
224 void setBeginIndex(int32_t bi
) { fBeginIndex
= bi
; }
227 * Set the end index. For use by subclasses of Format.
228 * @param ei the new value of the end index
231 void setEndIndex(int32_t ei
) { fEndIndex
= ei
; }
234 * ICU "poor man's RTTI", returns a UClassID for the actual class.
238 virtual UClassID
getDynamicClassID() const;
241 * ICU "poor man's RTTI", returns a UClassID for this class.
245 static UClassID U_EXPORT2
getStaticClassID();
249 * Input: Desired field to determine start and end offsets for.
250 * The meaning depends on the subclass of Format.
255 * Output: Start offset of field in text.
256 * If the field does not occur in the text, 0 is returned.
261 * Output: End offset of field in text.
262 * If the field does not occur in the text, 0 is returned.
267 inline FieldPosition
&
268 FieldPosition::operator=(const FieldPosition
& copy
)
270 fField
= copy
.fField
;
271 fEndIndex
= copy
.fEndIndex
;
272 fBeginIndex
= copy
.fBeginIndex
;
277 FieldPosition::operator==(const FieldPosition
& copy
) const
279 return (fField
== copy
.fField
&&
280 fEndIndex
== copy
.fEndIndex
&&
281 fBeginIndex
== copy
.fBeginIndex
);
285 FieldPosition::operator!=(const FieldPosition
& copy
) const
287 return !operator==(copy
);
291 #endif // U_SHOW_CPLUSPLUS_API
293 #endif /* #if !UCONFIG_NO_FORMATTING */