1 // Copyright (C) 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"
41 * <code>FieldPosition</code> is a simple class used by <code>Format</code>
42 * and its subclasses to identify fields in formatted output. Fields are
43 * identified by constants, whose names typically end with <code>_FIELD</code>,
44 * defined in the various subclasses of <code>Format</code>. See
45 * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for
49 * <code>FieldPosition</code> keeps track of the position of the
50 * field within the formatted output with two indices: the index
51 * of the first character of the field and the index of the last
52 * character of the field.
55 * One version of the <code>format</code> method in the various
56 * <code>Format</code> classes requires a <code>FieldPosition</code>
57 * object as an argument. You use this <code>format</code> method
58 * to perform partial formatting or to get information about the
59 * formatted output (such as the position of a field).
61 * The FieldPosition class is not intended for public subclassing.
64 * Below is an example of using <code>FieldPosition</code> to aid
65 * alignment of an array of formatted floating-point numbers on
66 * their decimal points:
69 * double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789,
70 * 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
71 * int dNumSize = (int)(sizeof(doubleNum)/sizeof(double));
73 * UErrorCode status = U_ZERO_ERROR;
74 * DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
75 * fmt->setDecimalSeparatorAlwaysShown(true);
77 * const int tempLen = 20;
80 * for (int i=0; i<dNumSize; i++) {
81 * FieldPosition pos(NumberFormat::INTEGER_FIELD);
83 * char fmtText[tempLen];
84 * ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
85 * for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces
86 * temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0';
87 * cout << temp << fmtText << endl;
93 * The code will generate the following output:
108 class U_I18N_API FieldPosition : public UObject {
111 * DONT_CARE may be specified as the field to indicate that the
112 * caller doesn't need to specify a field.
115 enum { DONT_CARE = -1 };
118 * Creates a FieldPosition object with a non-specified field.
122 : UObject(), fField(DONT_CARE), fBeginIndex(0), fEndIndex(0) {}
125 * Creates a FieldPosition object for the given field. Fields are
126 * identified by constants, whose names typically end with _FIELD,
127 * in the various subclasses of Format.
129 * @see NumberFormat#INTEGER_FIELD
130 * @see NumberFormat#FRACTION_FIELD
131 * @see DateFormat#YEAR_FIELD
132 * @see DateFormat#MONTH_FIELD
135 FieldPosition(int32_t field)
136 : UObject(), fField(field), fBeginIndex(0), fEndIndex(0) {}
140 * @param copy the object to be copied from.
143 FieldPosition(const FieldPosition& copy)
144 : UObject(copy), fField(copy.fField), fBeginIndex(copy.fBeginIndex), fEndIndex(copy.fEndIndex) {}
150 virtual ~FieldPosition();
153 * Assignment operator
154 * @param copy the object to be copied from.
157 FieldPosition& operator=(const FieldPosition& copy);
161 * @param that the object to be compared with.
162 * @return TRUE if the two field positions are equal, FALSE otherwise.
165 UBool operator==(const FieldPosition& that) const;
169 * @param that the object to be compared with.
170 * @return TRUE if the two field positions are not equal, FALSE otherwise.
173 UBool operator!=(const FieldPosition& that) const;
177 * Clones can be used concurrently in multiple threads.
178 * If an error occurs, then NULL is returned.
179 * The caller must delete the clone.
181 * @return a clone of this object
183 * @see getDynamicClassID
186 FieldPosition *clone() const;
189 * Retrieve the field identifier.
190 * @return the field identifier.
193 int32_t getField(void) const { return fField; }
196 * Retrieve the index of the first character in the requested field.
197 * @return the index of the first character in the requested field.
200 int32_t getBeginIndex(void) const { return fBeginIndex; }
203 * Retrieve the index of the character following the last character in the
205 * @return the index of the character following the last character in the
209 int32_t getEndIndex(void) const { return fEndIndex; }
213 * @param f the new value of the field.
216 void setField(int32_t f) { fField = f; }
219 * Set the begin index. For use by subclasses of Format.
220 * @param bi the new value of the begin index
223 void setBeginIndex(int32_t bi) { fBeginIndex = bi; }
226 * Set the end index. For use by subclasses of Format.
227 * @param ei the new value of the end index
230 void setEndIndex(int32_t ei) { fEndIndex = ei; }
233 * ICU "poor man's RTTI", returns a UClassID for the actual class.
237 virtual UClassID getDynamicClassID() const;
240 * ICU "poor man's RTTI", returns a UClassID for this class.
244 static UClassID U_EXPORT2 getStaticClassID();
248 * Input: Desired field to determine start and end offsets for.
249 * The meaning depends on the subclass of Format.
254 * Output: Start offset of field in text.
255 * If the field does not occur in the text, 0 is returned.
260 * Output: End offset of field in text.
261 * If the field does not occur in the text, 0 is returned.
266 inline FieldPosition&
267 FieldPosition::operator=(const FieldPosition& copy)
269 fField = copy.fField;
270 fEndIndex = copy.fEndIndex;
271 fBeginIndex = copy.fBeginIndex;
276 FieldPosition::operator==(const FieldPosition& copy) const
278 return (fField == copy.fField &&
279 fEndIndex == copy.fEndIndex &&
280 fBeginIndex == copy.fBeginIndex);
284 FieldPosition::operator!=(const FieldPosition& copy) const
286 return !operator==(copy);
291 #endif /* #if !UCONFIG_NO_FORMATTING */