1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker /* 4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************** 5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 1997-2006, International Business Machines 6*0e209d39SAndroid Build Coastguard Worker * Corporation and others. All Rights Reserved. 7*0e209d39SAndroid Build Coastguard Worker ******************************************************************************** 8*0e209d39SAndroid Build Coastguard Worker * 9*0e209d39SAndroid Build Coastguard Worker * File FIELDPOS.H 10*0e209d39SAndroid Build Coastguard Worker * 11*0e209d39SAndroid Build Coastguard Worker * Modification History: 12*0e209d39SAndroid Build Coastguard Worker * 13*0e209d39SAndroid Build Coastguard Worker * Date Name Description 14*0e209d39SAndroid Build Coastguard Worker * 02/25/97 aliu Converted from java. 15*0e209d39SAndroid Build Coastguard Worker * 03/17/97 clhuang Updated per Format implementation. 16*0e209d39SAndroid Build Coastguard Worker * 07/17/98 stephen Added default/copy ctors, and operators =, ==, != 17*0e209d39SAndroid Build Coastguard Worker ******************************************************************************** 18*0e209d39SAndroid Build Coastguard Worker */ 19*0e209d39SAndroid Build Coastguard Worker 20*0e209d39SAndroid Build Coastguard Worker // ***************************************************************************** 21*0e209d39SAndroid Build Coastguard Worker // This file was generated from the java source file FieldPosition.java 22*0e209d39SAndroid Build Coastguard Worker // ***************************************************************************** 23*0e209d39SAndroid Build Coastguard Worker 24*0e209d39SAndroid Build Coastguard Worker #ifndef FIELDPOS_H 25*0e209d39SAndroid Build Coastguard Worker #define FIELDPOS_H 26*0e209d39SAndroid Build Coastguard Worker 27*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 28*0e209d39SAndroid Build Coastguard Worker 29*0e209d39SAndroid Build Coastguard Worker #if U_SHOW_CPLUSPLUS_API 30*0e209d39SAndroid Build Coastguard Worker 31*0e209d39SAndroid Build Coastguard Worker /** 32*0e209d39SAndroid Build Coastguard Worker * \file 33*0e209d39SAndroid Build Coastguard Worker * \brief C++ API: FieldPosition identifies the fields in a formatted output. 34*0e209d39SAndroid Build Coastguard Worker */ 35*0e209d39SAndroid Build Coastguard Worker 36*0e209d39SAndroid Build Coastguard Worker #if !UCONFIG_NO_FORMATTING 37*0e209d39SAndroid Build Coastguard Worker 38*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h" 39*0e209d39SAndroid Build Coastguard Worker 40*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 41*0e209d39SAndroid Build Coastguard Worker 42*0e209d39SAndroid Build Coastguard Worker /** 43*0e209d39SAndroid Build Coastguard Worker * <code>FieldPosition</code> is a simple class used by <code>Format</code> 44*0e209d39SAndroid Build Coastguard Worker * and its subclasses to identify fields in formatted output. Fields are 45*0e209d39SAndroid Build Coastguard Worker * identified by constants, whose names typically end with <code>_FIELD</code>, 46*0e209d39SAndroid Build Coastguard Worker * defined in the various subclasses of <code>Format</code>. See 47*0e209d39SAndroid Build Coastguard Worker * <code>ERA_FIELD</code> and its friends in <code>DateFormat</code> for 48*0e209d39SAndroid Build Coastguard Worker * an example. 49*0e209d39SAndroid Build Coastguard Worker * 50*0e209d39SAndroid Build Coastguard Worker * <p> 51*0e209d39SAndroid Build Coastguard Worker * <code>FieldPosition</code> keeps track of the position of the 52*0e209d39SAndroid Build Coastguard Worker * field within the formatted output with two indices: the index 53*0e209d39SAndroid Build Coastguard Worker * of the first character of the field and the index of the last 54*0e209d39SAndroid Build Coastguard Worker * character of the field. 55*0e209d39SAndroid Build Coastguard Worker * 56*0e209d39SAndroid Build Coastguard Worker * <p> 57*0e209d39SAndroid Build Coastguard Worker * One version of the <code>format</code> method in the various 58*0e209d39SAndroid Build Coastguard Worker * <code>Format</code> classes requires a <code>FieldPosition</code> 59*0e209d39SAndroid Build Coastguard Worker * object as an argument. You use this <code>format</code> method 60*0e209d39SAndroid Build Coastguard Worker * to perform partial formatting or to get information about the 61*0e209d39SAndroid Build Coastguard Worker * formatted output (such as the position of a field). 62*0e209d39SAndroid Build Coastguard Worker * 63*0e209d39SAndroid Build Coastguard Worker * The FieldPosition class is not intended for public subclassing. 64*0e209d39SAndroid Build Coastguard Worker * 65*0e209d39SAndroid Build Coastguard Worker * <p> 66*0e209d39SAndroid Build Coastguard Worker * Below is an example of using <code>FieldPosition</code> to aid 67*0e209d39SAndroid Build Coastguard Worker * alignment of an array of formatted floating-point numbers on 68*0e209d39SAndroid Build Coastguard Worker * their decimal points: 69*0e209d39SAndroid Build Coastguard Worker * <pre> 70*0e209d39SAndroid Build Coastguard Worker * \code 71*0e209d39SAndroid Build Coastguard Worker * double doubleNum[] = {123456789.0, -12345678.9, 1234567.89, -123456.789, 72*0e209d39SAndroid Build Coastguard Worker * 12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789}; 73*0e209d39SAndroid Build Coastguard Worker * int dNumSize = (int)(sizeof(doubleNum)/sizeof(double)); 74*0e209d39SAndroid Build Coastguard Worker * 75*0e209d39SAndroid Build Coastguard Worker * UErrorCode status = U_ZERO_ERROR; 76*0e209d39SAndroid Build Coastguard Worker * DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status); 77*0e209d39SAndroid Build Coastguard Worker * fmt->setDecimalSeparatorAlwaysShown(true); 78*0e209d39SAndroid Build Coastguard Worker * 79*0e209d39SAndroid Build Coastguard Worker * const int tempLen = 20; 80*0e209d39SAndroid Build Coastguard Worker * char temp[tempLen]; 81*0e209d39SAndroid Build Coastguard Worker * 82*0e209d39SAndroid Build Coastguard Worker * for (int i=0; i<dNumSize; i++) { 83*0e209d39SAndroid Build Coastguard Worker * FieldPosition pos(NumberFormat::INTEGER_FIELD); 84*0e209d39SAndroid Build Coastguard Worker * UnicodeString buf; 85*0e209d39SAndroid Build Coastguard Worker * char fmtText[tempLen]; 86*0e209d39SAndroid Build Coastguard Worker * ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText); 87*0e209d39SAndroid Build Coastguard Worker * for (int j=0; j<tempLen; j++) temp[j] = ' '; // clear with spaces 88*0e209d39SAndroid Build Coastguard Worker * temp[__min(tempLen, tempLen-pos.getEndIndex())] = '\0'; 89*0e209d39SAndroid Build Coastguard Worker * cout << temp << fmtText << endl; 90*0e209d39SAndroid Build Coastguard Worker * } 91*0e209d39SAndroid Build Coastguard Worker * delete fmt; 92*0e209d39SAndroid Build Coastguard Worker * \endcode 93*0e209d39SAndroid Build Coastguard Worker * </pre> 94*0e209d39SAndroid Build Coastguard Worker * <p> 95*0e209d39SAndroid Build Coastguard Worker * The code will generate the following output: 96*0e209d39SAndroid Build Coastguard Worker * <pre> 97*0e209d39SAndroid Build Coastguard Worker * \code 98*0e209d39SAndroid Build Coastguard Worker * 123,456,789.000 99*0e209d39SAndroid Build Coastguard Worker * -12,345,678.900 100*0e209d39SAndroid Build Coastguard Worker * 1,234,567.880 101*0e209d39SAndroid Build Coastguard Worker * -123,456.789 102*0e209d39SAndroid Build Coastguard Worker * 12,345.678 103*0e209d39SAndroid Build Coastguard Worker * -1,234.567 104*0e209d39SAndroid Build Coastguard Worker * 123.456 105*0e209d39SAndroid Build Coastguard Worker * -12.345 106*0e209d39SAndroid Build Coastguard Worker * 1.234 107*0e209d39SAndroid Build Coastguard Worker * \endcode 108*0e209d39SAndroid Build Coastguard Worker * </pre> 109*0e209d39SAndroid Build Coastguard Worker */ 110*0e209d39SAndroid Build Coastguard Worker class U_I18N_API FieldPosition : public UObject { 111*0e209d39SAndroid Build Coastguard Worker public: 112*0e209d39SAndroid Build Coastguard Worker /** 113*0e209d39SAndroid Build Coastguard Worker * DONT_CARE may be specified as the field to indicate that the 114*0e209d39SAndroid Build Coastguard Worker * caller doesn't need to specify a field. 115*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 116*0e209d39SAndroid Build Coastguard Worker */ 117*0e209d39SAndroid Build Coastguard Worker enum { DONT_CARE = -1 }; 118*0e209d39SAndroid Build Coastguard Worker 119*0e209d39SAndroid Build Coastguard Worker /** 120*0e209d39SAndroid Build Coastguard Worker * Creates a FieldPosition object with a non-specified field. 121*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 122*0e209d39SAndroid Build Coastguard Worker */ FieldPosition()123*0e209d39SAndroid Build Coastguard Worker FieldPosition() 124*0e209d39SAndroid Build Coastguard Worker : UObject(), fField(DONT_CARE), fBeginIndex(0), fEndIndex(0) {} 125*0e209d39SAndroid Build Coastguard Worker 126*0e209d39SAndroid Build Coastguard Worker /** 127*0e209d39SAndroid Build Coastguard Worker * Creates a FieldPosition object for the given field. Fields are 128*0e209d39SAndroid Build Coastguard Worker * identified by constants, whose names typically end with _FIELD, 129*0e209d39SAndroid Build Coastguard Worker * in the various subclasses of Format. 130*0e209d39SAndroid Build Coastguard Worker * 131*0e209d39SAndroid Build Coastguard Worker * @see NumberFormat#INTEGER_FIELD 132*0e209d39SAndroid Build Coastguard Worker * @see NumberFormat#FRACTION_FIELD 133*0e209d39SAndroid Build Coastguard Worker * @see DateFormat#YEAR_FIELD 134*0e209d39SAndroid Build Coastguard Worker * @see DateFormat#MONTH_FIELD 135*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 136*0e209d39SAndroid Build Coastguard Worker */ FieldPosition(int32_t field)137*0e209d39SAndroid Build Coastguard Worker FieldPosition(int32_t field) 138*0e209d39SAndroid Build Coastguard Worker : UObject(), fField(field), fBeginIndex(0), fEndIndex(0) {} 139*0e209d39SAndroid Build Coastguard Worker 140*0e209d39SAndroid Build Coastguard Worker /** 141*0e209d39SAndroid Build Coastguard Worker * Copy constructor 142*0e209d39SAndroid Build Coastguard Worker * @param copy the object to be copied from. 143*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 144*0e209d39SAndroid Build Coastguard Worker */ FieldPosition(const FieldPosition & copy)145*0e209d39SAndroid Build Coastguard Worker FieldPosition(const FieldPosition& copy) 146*0e209d39SAndroid Build Coastguard Worker : UObject(copy), fField(copy.fField), fBeginIndex(copy.fBeginIndex), fEndIndex(copy.fEndIndex) {} 147*0e209d39SAndroid Build Coastguard Worker 148*0e209d39SAndroid Build Coastguard Worker /** 149*0e209d39SAndroid Build Coastguard Worker * Destructor 150*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 151*0e209d39SAndroid Build Coastguard Worker */ 152*0e209d39SAndroid Build Coastguard Worker virtual ~FieldPosition(); 153*0e209d39SAndroid Build Coastguard Worker 154*0e209d39SAndroid Build Coastguard Worker /** 155*0e209d39SAndroid Build Coastguard Worker * Assignment operator 156*0e209d39SAndroid Build Coastguard Worker * @param copy the object to be copied from. 157*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 158*0e209d39SAndroid Build Coastguard Worker */ 159*0e209d39SAndroid Build Coastguard Worker FieldPosition& operator=(const FieldPosition& copy); 160*0e209d39SAndroid Build Coastguard Worker 161*0e209d39SAndroid Build Coastguard Worker /** 162*0e209d39SAndroid Build Coastguard Worker * Equality operator. 163*0e209d39SAndroid Build Coastguard Worker * @param that the object to be compared with. 164*0e209d39SAndroid Build Coastguard Worker * @return true if the two field positions are equal, false otherwise. 165*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 166*0e209d39SAndroid Build Coastguard Worker */ 167*0e209d39SAndroid Build Coastguard Worker bool operator==(const FieldPosition& that) const; 168*0e209d39SAndroid Build Coastguard Worker 169*0e209d39SAndroid Build Coastguard Worker /** 170*0e209d39SAndroid Build Coastguard Worker * Equality operator. 171*0e209d39SAndroid Build Coastguard Worker * @param that the object to be compared with. 172*0e209d39SAndroid Build Coastguard Worker * @return true if the two field positions are not equal, false otherwise. 173*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 174*0e209d39SAndroid Build Coastguard Worker */ 175*0e209d39SAndroid Build Coastguard Worker bool operator!=(const FieldPosition& that) const; 176*0e209d39SAndroid Build Coastguard Worker 177*0e209d39SAndroid Build Coastguard Worker /** 178*0e209d39SAndroid Build Coastguard Worker * Clone this object. 179*0e209d39SAndroid Build Coastguard Worker * Clones can be used concurrently in multiple threads. 180*0e209d39SAndroid Build Coastguard Worker * If an error occurs, then nullptr is returned. 181*0e209d39SAndroid Build Coastguard Worker * The caller must delete the clone. 182*0e209d39SAndroid Build Coastguard Worker * 183*0e209d39SAndroid Build Coastguard Worker * @return a clone of this object 184*0e209d39SAndroid Build Coastguard Worker * 185*0e209d39SAndroid Build Coastguard Worker * @see getDynamicClassID 186*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.8 187*0e209d39SAndroid Build Coastguard Worker */ 188*0e209d39SAndroid Build Coastguard Worker FieldPosition *clone() const; 189*0e209d39SAndroid Build Coastguard Worker 190*0e209d39SAndroid Build Coastguard Worker /** 191*0e209d39SAndroid Build Coastguard Worker * Retrieve the field identifier. 192*0e209d39SAndroid Build Coastguard Worker * @return the field identifier. 193*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 194*0e209d39SAndroid Build Coastguard Worker */ getField()195*0e209d39SAndroid Build Coastguard Worker int32_t getField() const { return fField; } 196*0e209d39SAndroid Build Coastguard Worker 197*0e209d39SAndroid Build Coastguard Worker /** 198*0e209d39SAndroid Build Coastguard Worker * Retrieve the index of the first character in the requested field. 199*0e209d39SAndroid Build Coastguard Worker * @return the index of the first character in the requested field. 200*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 201*0e209d39SAndroid Build Coastguard Worker */ getBeginIndex()202*0e209d39SAndroid Build Coastguard Worker int32_t getBeginIndex() const { return fBeginIndex; } 203*0e209d39SAndroid Build Coastguard Worker 204*0e209d39SAndroid Build Coastguard Worker /** 205*0e209d39SAndroid Build Coastguard Worker * Retrieve the index of the character following the last character in the 206*0e209d39SAndroid Build Coastguard Worker * requested field. 207*0e209d39SAndroid Build Coastguard Worker * @return the index of the character following the last character in the 208*0e209d39SAndroid Build Coastguard Worker * requested field. 209*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 210*0e209d39SAndroid Build Coastguard Worker */ getEndIndex()211*0e209d39SAndroid Build Coastguard Worker int32_t getEndIndex() const { return fEndIndex; } 212*0e209d39SAndroid Build Coastguard Worker 213*0e209d39SAndroid Build Coastguard Worker /** 214*0e209d39SAndroid Build Coastguard Worker * Set the field. 215*0e209d39SAndroid Build Coastguard Worker * @param f the new value of the field. 216*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 217*0e209d39SAndroid Build Coastguard Worker */ setField(int32_t f)218*0e209d39SAndroid Build Coastguard Worker void setField(int32_t f) { fField = f; } 219*0e209d39SAndroid Build Coastguard Worker 220*0e209d39SAndroid Build Coastguard Worker /** 221*0e209d39SAndroid Build Coastguard Worker * Set the begin index. For use by subclasses of Format. 222*0e209d39SAndroid Build Coastguard Worker * @param bi the new value of the begin index 223*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 224*0e209d39SAndroid Build Coastguard Worker */ setBeginIndex(int32_t bi)225*0e209d39SAndroid Build Coastguard Worker void setBeginIndex(int32_t bi) { fBeginIndex = bi; } 226*0e209d39SAndroid Build Coastguard Worker 227*0e209d39SAndroid Build Coastguard Worker /** 228*0e209d39SAndroid Build Coastguard Worker * Set the end index. For use by subclasses of Format. 229*0e209d39SAndroid Build Coastguard Worker * @param ei the new value of the end index 230*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.0 231*0e209d39SAndroid Build Coastguard Worker */ setEndIndex(int32_t ei)232*0e209d39SAndroid Build Coastguard Worker void setEndIndex(int32_t ei) { fEndIndex = ei; } 233*0e209d39SAndroid Build Coastguard Worker 234*0e209d39SAndroid Build Coastguard Worker /** 235*0e209d39SAndroid Build Coastguard Worker * ICU "poor man's RTTI", returns a UClassID for the actual class. 236*0e209d39SAndroid Build Coastguard Worker * 237*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.2 238*0e209d39SAndroid Build Coastguard Worker */ 239*0e209d39SAndroid Build Coastguard Worker virtual UClassID getDynamicClassID() const override; 240*0e209d39SAndroid Build Coastguard Worker 241*0e209d39SAndroid Build Coastguard Worker /** 242*0e209d39SAndroid Build Coastguard Worker * ICU "poor man's RTTI", returns a UClassID for this class. 243*0e209d39SAndroid Build Coastguard Worker * 244*0e209d39SAndroid Build Coastguard Worker * @stable ICU 2.2 245*0e209d39SAndroid Build Coastguard Worker */ 246*0e209d39SAndroid Build Coastguard Worker static UClassID U_EXPORT2 getStaticClassID(); 247*0e209d39SAndroid Build Coastguard Worker 248*0e209d39SAndroid Build Coastguard Worker private: 249*0e209d39SAndroid Build Coastguard Worker /** 250*0e209d39SAndroid Build Coastguard Worker * Input: Desired field to determine start and end offsets for. 251*0e209d39SAndroid Build Coastguard Worker * The meaning depends on the subclass of Format. 252*0e209d39SAndroid Build Coastguard Worker */ 253*0e209d39SAndroid Build Coastguard Worker int32_t fField; 254*0e209d39SAndroid Build Coastguard Worker 255*0e209d39SAndroid Build Coastguard Worker /** 256*0e209d39SAndroid Build Coastguard Worker * Output: Start offset of field in text. 257*0e209d39SAndroid Build Coastguard Worker * If the field does not occur in the text, 0 is returned. 258*0e209d39SAndroid Build Coastguard Worker */ 259*0e209d39SAndroid Build Coastguard Worker int32_t fBeginIndex; 260*0e209d39SAndroid Build Coastguard Worker 261*0e209d39SAndroid Build Coastguard Worker /** 262*0e209d39SAndroid Build Coastguard Worker * Output: End offset of field in text. 263*0e209d39SAndroid Build Coastguard Worker * If the field does not occur in the text, 0 is returned. 264*0e209d39SAndroid Build Coastguard Worker */ 265*0e209d39SAndroid Build Coastguard Worker int32_t fEndIndex; 266*0e209d39SAndroid Build Coastguard Worker }; 267*0e209d39SAndroid Build Coastguard Worker 268*0e209d39SAndroid Build Coastguard Worker inline FieldPosition& 269*0e209d39SAndroid Build Coastguard Worker FieldPosition::operator=(const FieldPosition& copy) 270*0e209d39SAndroid Build Coastguard Worker { 271*0e209d39SAndroid Build Coastguard Worker fField = copy.fField; 272*0e209d39SAndroid Build Coastguard Worker fEndIndex = copy.fEndIndex; 273*0e209d39SAndroid Build Coastguard Worker fBeginIndex = copy.fBeginIndex; 274*0e209d39SAndroid Build Coastguard Worker return *this; 275*0e209d39SAndroid Build Coastguard Worker } 276*0e209d39SAndroid Build Coastguard Worker 277*0e209d39SAndroid Build Coastguard Worker inline bool 278*0e209d39SAndroid Build Coastguard Worker FieldPosition::operator==(const FieldPosition& copy) const 279*0e209d39SAndroid Build Coastguard Worker { 280*0e209d39SAndroid Build Coastguard Worker return (fField == copy.fField && 281*0e209d39SAndroid Build Coastguard Worker fEndIndex == copy.fEndIndex && 282*0e209d39SAndroid Build Coastguard Worker fBeginIndex == copy.fBeginIndex); 283*0e209d39SAndroid Build Coastguard Worker } 284*0e209d39SAndroid Build Coastguard Worker 285*0e209d39SAndroid Build Coastguard Worker inline bool 286*0e209d39SAndroid Build Coastguard Worker FieldPosition::operator!=(const FieldPosition& copy) const 287*0e209d39SAndroid Build Coastguard Worker { 288*0e209d39SAndroid Build Coastguard Worker return !operator==(copy); 289*0e209d39SAndroid Build Coastguard Worker } 290*0e209d39SAndroid Build Coastguard Worker 291*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 292*0e209d39SAndroid Build Coastguard Worker 293*0e209d39SAndroid Build Coastguard Worker #endif /* #if !UCONFIG_NO_FORMATTING */ 294*0e209d39SAndroid Build Coastguard Worker 295*0e209d39SAndroid Build Coastguard Worker #endif /* U_SHOW_CPLUSPLUS_API */ 296*0e209d39SAndroid Build Coastguard Worker 297*0e209d39SAndroid Build Coastguard Worker #endif // _FIELDPOS 298*0e209d39SAndroid Build Coastguard Worker //eof 299