annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/unicode/fmtable.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 // © 2016 and later: Unicode, Inc. and others.
jpayne@69 2 // License & terms of use: http://www.unicode.org/copyright.html
jpayne@69 3 /*
jpayne@69 4 ********************************************************************************
jpayne@69 5 * Copyright (C) 1997-2014, International Business Machines
jpayne@69 6 * Corporation and others. All Rights Reserved.
jpayne@69 7 ********************************************************************************
jpayne@69 8 *
jpayne@69 9 * File FMTABLE.H
jpayne@69 10 *
jpayne@69 11 * Modification History:
jpayne@69 12 *
jpayne@69 13 * Date Name Description
jpayne@69 14 * 02/29/97 aliu Creation.
jpayne@69 15 ********************************************************************************
jpayne@69 16 */
jpayne@69 17 #ifndef FMTABLE_H
jpayne@69 18 #define FMTABLE_H
jpayne@69 19
jpayne@69 20 #include "unicode/utypes.h"
jpayne@69 21
jpayne@69 22 #if U_SHOW_CPLUSPLUS_API
jpayne@69 23
jpayne@69 24 /**
jpayne@69 25 * \file
jpayne@69 26 * \brief C++ API: Formattable is a thin wrapper for primitive types used for formatting and parsing
jpayne@69 27 */
jpayne@69 28
jpayne@69 29 #if !UCONFIG_NO_FORMATTING
jpayne@69 30
jpayne@69 31 #include "unicode/unistr.h"
jpayne@69 32 #include "unicode/stringpiece.h"
jpayne@69 33 #include "unicode/uformattable.h"
jpayne@69 34
jpayne@69 35 U_NAMESPACE_BEGIN
jpayne@69 36
jpayne@69 37 class CharString;
jpayne@69 38 namespace number {
jpayne@69 39 namespace impl {
jpayne@69 40 class DecimalQuantity;
jpayne@69 41 }
jpayne@69 42 }
jpayne@69 43
jpayne@69 44 /**
jpayne@69 45 * Formattable objects can be passed to the Format class or
jpayne@69 46 * its subclasses for formatting. Formattable is a thin wrapper
jpayne@69 47 * class which interconverts between the primitive numeric types
jpayne@69 48 * (double, long, etc.) as well as UDate and UnicodeString.
jpayne@69 49 *
jpayne@69 50 * <p>Internally, a Formattable object is a union of primitive types.
jpayne@69 51 * As such, it can only store one flavor of data at a time. To
jpayne@69 52 * determine what flavor of data it contains, use the getType method.
jpayne@69 53 *
jpayne@69 54 * <p>As of ICU 3.0, Formattable may also wrap a UObject pointer,
jpayne@69 55 * which it owns. This allows an instance of any ICU class to be
jpayne@69 56 * encapsulated in a Formattable. For legacy reasons and for
jpayne@69 57 * efficiency, primitive numeric types are still stored directly
jpayne@69 58 * within a Formattable.
jpayne@69 59 *
jpayne@69 60 * <p>The Formattable class is not suitable for subclassing.
jpayne@69 61 *
jpayne@69 62 * <p>See UFormattable for a C wrapper.
jpayne@69 63 */
jpayne@69 64 class U_I18N_API Formattable : public UObject {
jpayne@69 65 public:
jpayne@69 66 /**
jpayne@69 67 * This enum is only used to let callers distinguish between
jpayne@69 68 * the Formattable(UDate) constructor and the Formattable(double)
jpayne@69 69 * constructor; the compiler cannot distinguish the signatures,
jpayne@69 70 * since UDate is currently typedefed to be either double or long.
jpayne@69 71 * If UDate is changed later to be a bonafide class
jpayne@69 72 * or struct, then we no longer need this enum.
jpayne@69 73 * @stable ICU 2.4
jpayne@69 74 */
jpayne@69 75 enum ISDATE { kIsDate };
jpayne@69 76
jpayne@69 77 /**
jpayne@69 78 * Default constructor
jpayne@69 79 * @stable ICU 2.4
jpayne@69 80 */
jpayne@69 81 Formattable(); // Type kLong, value 0
jpayne@69 82
jpayne@69 83 /**
jpayne@69 84 * Creates a Formattable object with a UDate instance.
jpayne@69 85 * @param d the UDate instance.
jpayne@69 86 * @param flag the flag to indicate this is a date. Always set it to kIsDate
jpayne@69 87 * @stable ICU 2.0
jpayne@69 88 */
jpayne@69 89 Formattable(UDate d, ISDATE flag);
jpayne@69 90
jpayne@69 91 /**
jpayne@69 92 * Creates a Formattable object with a double number.
jpayne@69 93 * @param d the double number.
jpayne@69 94 * @stable ICU 2.0
jpayne@69 95 */
jpayne@69 96 Formattable(double d);
jpayne@69 97
jpayne@69 98 /**
jpayne@69 99 * Creates a Formattable object with a long number.
jpayne@69 100 * @param l the long number.
jpayne@69 101 * @stable ICU 2.0
jpayne@69 102 */
jpayne@69 103 Formattable(int32_t l);
jpayne@69 104
jpayne@69 105 /**
jpayne@69 106 * Creates a Formattable object with an int64_t number
jpayne@69 107 * @param ll the int64_t number.
jpayne@69 108 * @stable ICU 2.8
jpayne@69 109 */
jpayne@69 110 Formattable(int64_t ll);
jpayne@69 111
jpayne@69 112 #if !UCONFIG_NO_CONVERSION
jpayne@69 113 /**
jpayne@69 114 * Creates a Formattable object with a char string pointer.
jpayne@69 115 * Assumes that the char string is null terminated.
jpayne@69 116 * @param strToCopy the char string.
jpayne@69 117 * @stable ICU 2.0
jpayne@69 118 */
jpayne@69 119 Formattable(const char* strToCopy);
jpayne@69 120 #endif
jpayne@69 121
jpayne@69 122 /**
jpayne@69 123 * Creates a Formattable object of an appropriate numeric type from a
jpayne@69 124 * a decimal number in string form. The Formattable will retain the
jpayne@69 125 * full precision of the input in decimal format, even when it exceeds
jpayne@69 126 * what can be represented by a double or int64_t.
jpayne@69 127 *
jpayne@69 128 * @param number the unformatted (not localized) string representation
jpayne@69 129 * of the Decimal number.
jpayne@69 130 * @param status the error code. Possible errors include U_INVALID_FORMAT_ERROR
jpayne@69 131 * if the format of the string does not conform to that of a
jpayne@69 132 * decimal number.
jpayne@69 133 * @stable ICU 4.4
jpayne@69 134 */
jpayne@69 135 Formattable(StringPiece number, UErrorCode &status);
jpayne@69 136
jpayne@69 137 /**
jpayne@69 138 * Creates a Formattable object with a UnicodeString object to copy from.
jpayne@69 139 * @param strToCopy the UnicodeString string.
jpayne@69 140 * @stable ICU 2.0
jpayne@69 141 */
jpayne@69 142 Formattable(const UnicodeString& strToCopy);
jpayne@69 143
jpayne@69 144 /**
jpayne@69 145 * Creates a Formattable object with a UnicodeString object to adopt from.
jpayne@69 146 * @param strToAdopt the UnicodeString string.
jpayne@69 147 * @stable ICU 2.0
jpayne@69 148 */
jpayne@69 149 Formattable(UnicodeString* strToAdopt);
jpayne@69 150
jpayne@69 151 /**
jpayne@69 152 * Creates a Formattable object with an array of Formattable objects.
jpayne@69 153 * @param arrayToCopy the Formattable object array.
jpayne@69 154 * @param count the array count.
jpayne@69 155 * @stable ICU 2.0
jpayne@69 156 */
jpayne@69 157 Formattable(const Formattable* arrayToCopy, int32_t count);
jpayne@69 158
jpayne@69 159 /**
jpayne@69 160 * Creates a Formattable object that adopts the given UObject.
jpayne@69 161 * @param objectToAdopt the UObject to set this object to
jpayne@69 162 * @stable ICU 3.0
jpayne@69 163 */
jpayne@69 164 Formattable(UObject* objectToAdopt);
jpayne@69 165
jpayne@69 166 /**
jpayne@69 167 * Copy constructor.
jpayne@69 168 * @stable ICU 2.0
jpayne@69 169 */
jpayne@69 170 Formattable(const Formattable&);
jpayne@69 171
jpayne@69 172 /**
jpayne@69 173 * Assignment operator.
jpayne@69 174 * @param rhs The Formattable object to copy into this object.
jpayne@69 175 * @stable ICU 2.0
jpayne@69 176 */
jpayne@69 177 Formattable& operator=(const Formattable &rhs);
jpayne@69 178
jpayne@69 179 /**
jpayne@69 180 * Equality comparison.
jpayne@69 181 * @param other the object to be compared with.
jpayne@69 182 * @return TRUE if other are equal to this, FALSE otherwise.
jpayne@69 183 * @stable ICU 2.0
jpayne@69 184 */
jpayne@69 185 UBool operator==(const Formattable &other) const;
jpayne@69 186
jpayne@69 187 /**
jpayne@69 188 * Equality operator.
jpayne@69 189 * @param other the object to be compared with.
jpayne@69 190 * @return TRUE if other are unequal to this, FALSE otherwise.
jpayne@69 191 * @stable ICU 2.0
jpayne@69 192 */
jpayne@69 193 UBool operator!=(const Formattable& other) const
jpayne@69 194 { return !operator==(other); }
jpayne@69 195
jpayne@69 196 /**
jpayne@69 197 * Destructor.
jpayne@69 198 * @stable ICU 2.0
jpayne@69 199 */
jpayne@69 200 virtual ~Formattable();
jpayne@69 201
jpayne@69 202 /**
jpayne@69 203 * Clone this object.
jpayne@69 204 * Clones can be used concurrently in multiple threads.
jpayne@69 205 * If an error occurs, then NULL is returned.
jpayne@69 206 * The caller must delete the clone.
jpayne@69 207 *
jpayne@69 208 * @return a clone of this object
jpayne@69 209 *
jpayne@69 210 * @see getDynamicClassID
jpayne@69 211 * @stable ICU 2.8
jpayne@69 212 */
jpayne@69 213 Formattable *clone() const;
jpayne@69 214
jpayne@69 215 /**
jpayne@69 216 * Selector for flavor of data type contained within a
jpayne@69 217 * Formattable object. Formattable is a union of several
jpayne@69 218 * different types, and at any time contains exactly one type.
jpayne@69 219 * @stable ICU 2.4
jpayne@69 220 */
jpayne@69 221 enum Type {
jpayne@69 222 /**
jpayne@69 223 * Selector indicating a UDate value. Use getDate to retrieve
jpayne@69 224 * the value.
jpayne@69 225 * @stable ICU 2.4
jpayne@69 226 */
jpayne@69 227 kDate,
jpayne@69 228
jpayne@69 229 /**
jpayne@69 230 * Selector indicating a double value. Use getDouble to
jpayne@69 231 * retrieve the value.
jpayne@69 232 * @stable ICU 2.4
jpayne@69 233 */
jpayne@69 234 kDouble,
jpayne@69 235
jpayne@69 236 /**
jpayne@69 237 * Selector indicating a 32-bit integer value. Use getLong to
jpayne@69 238 * retrieve the value.
jpayne@69 239 * @stable ICU 2.4
jpayne@69 240 */
jpayne@69 241 kLong,
jpayne@69 242
jpayne@69 243 /**
jpayne@69 244 * Selector indicating a UnicodeString value. Use getString
jpayne@69 245 * to retrieve the value.
jpayne@69 246 * @stable ICU 2.4
jpayne@69 247 */
jpayne@69 248 kString,
jpayne@69 249
jpayne@69 250 /**
jpayne@69 251 * Selector indicating an array of Formattables. Use getArray
jpayne@69 252 * to retrieve the value.
jpayne@69 253 * @stable ICU 2.4
jpayne@69 254 */
jpayne@69 255 kArray,
jpayne@69 256
jpayne@69 257 /**
jpayne@69 258 * Selector indicating a 64-bit integer value. Use getInt64
jpayne@69 259 * to retrieve the value.
jpayne@69 260 * @stable ICU 2.8
jpayne@69 261 */
jpayne@69 262 kInt64,
jpayne@69 263
jpayne@69 264 /**
jpayne@69 265 * Selector indicating a UObject value. Use getObject to
jpayne@69 266 * retrieve the value.
jpayne@69 267 * @stable ICU 3.0
jpayne@69 268 */
jpayne@69 269 kObject
jpayne@69 270 };
jpayne@69 271
jpayne@69 272 /**
jpayne@69 273 * Gets the data type of this Formattable object.
jpayne@69 274 * @return the data type of this Formattable object.
jpayne@69 275 * @stable ICU 2.0
jpayne@69 276 */
jpayne@69 277 Type getType(void) const;
jpayne@69 278
jpayne@69 279 /**
jpayne@69 280 * Returns TRUE if the data type of this Formattable object
jpayne@69 281 * is kDouble, kLong, or kInt64
jpayne@69 282 * @return TRUE if this is a pure numeric object
jpayne@69 283 * @stable ICU 3.0
jpayne@69 284 */
jpayne@69 285 UBool isNumeric() const;
jpayne@69 286
jpayne@69 287 /**
jpayne@69 288 * Gets the double value of this object. If this object is not of type
jpayne@69 289 * kDouble then the result is undefined.
jpayne@69 290 * @return the double value of this object.
jpayne@69 291 * @stable ICU 2.0
jpayne@69 292 */
jpayne@69 293 double getDouble(void) const { return fValue.fDouble; }
jpayne@69 294
jpayne@69 295 /**
jpayne@69 296 * Gets the double value of this object. If this object is of type
jpayne@69 297 * long, int64 or Decimal Number then a conversion is peformed, with
jpayne@69 298 * possible loss of precision. If the type is kObject and the
jpayne@69 299 * object is a Measure, then the result of
jpayne@69 300 * getNumber().getDouble(status) is returned. If this object is
jpayne@69 301 * neither a numeric type nor a Measure, then 0 is returned and
jpayne@69 302 * the status is set to U_INVALID_FORMAT_ERROR.
jpayne@69 303 * @param status the error code
jpayne@69 304 * @return the double value of this object.
jpayne@69 305 * @stable ICU 3.0
jpayne@69 306 */
jpayne@69 307 double getDouble(UErrorCode& status) const;
jpayne@69 308
jpayne@69 309 /**
jpayne@69 310 * Gets the long value of this object. If this object is not of type
jpayne@69 311 * kLong then the result is undefined.
jpayne@69 312 * @return the long value of this object.
jpayne@69 313 * @stable ICU 2.0
jpayne@69 314 */
jpayne@69 315 int32_t getLong(void) const { return (int32_t)fValue.fInt64; }
jpayne@69 316
jpayne@69 317 /**
jpayne@69 318 * Gets the long value of this object. If the magnitude is too
jpayne@69 319 * large to fit in a long, then the maximum or minimum long value,
jpayne@69 320 * as appropriate, is returned and the status is set to
jpayne@69 321 * U_INVALID_FORMAT_ERROR. If this object is of type kInt64 and
jpayne@69 322 * it fits within a long, then no precision is lost. If it is of
jpayne@69 323 * type kDouble, then a conversion is peformed, with
jpayne@69 324 * truncation of any fractional part. If the type is kObject and
jpayne@69 325 * the object is a Measure, then the result of
jpayne@69 326 * getNumber().getLong(status) is returned. If this object is
jpayne@69 327 * neither a numeric type nor a Measure, then 0 is returned and
jpayne@69 328 * the status is set to U_INVALID_FORMAT_ERROR.
jpayne@69 329 * @param status the error code
jpayne@69 330 * @return the long value of this object.
jpayne@69 331 * @stable ICU 3.0
jpayne@69 332 */
jpayne@69 333 int32_t getLong(UErrorCode& status) const;
jpayne@69 334
jpayne@69 335 /**
jpayne@69 336 * Gets the int64 value of this object. If this object is not of type
jpayne@69 337 * kInt64 then the result is undefined.
jpayne@69 338 * @return the int64 value of this object.
jpayne@69 339 * @stable ICU 2.8
jpayne@69 340 */
jpayne@69 341 int64_t getInt64(void) const { return fValue.fInt64; }
jpayne@69 342
jpayne@69 343 /**
jpayne@69 344 * Gets the int64 value of this object. If this object is of a numeric
jpayne@69 345 * type and the magnitude is too large to fit in an int64, then
jpayne@69 346 * the maximum or minimum int64 value, as appropriate, is returned
jpayne@69 347 * and the status is set to U_INVALID_FORMAT_ERROR. If the
jpayne@69 348 * magnitude fits in an int64, then a casting conversion is
jpayne@69 349 * peformed, with truncation of any fractional part. If the type
jpayne@69 350 * is kObject and the object is a Measure, then the result of
jpayne@69 351 * getNumber().getDouble(status) is returned. If this object is
jpayne@69 352 * neither a numeric type nor a Measure, then 0 is returned and
jpayne@69 353 * the status is set to U_INVALID_FORMAT_ERROR.
jpayne@69 354 * @param status the error code
jpayne@69 355 * @return the int64 value of this object.
jpayne@69 356 * @stable ICU 3.0
jpayne@69 357 */
jpayne@69 358 int64_t getInt64(UErrorCode& status) const;
jpayne@69 359
jpayne@69 360 /**
jpayne@69 361 * Gets the Date value of this object. If this object is not of type
jpayne@69 362 * kDate then the result is undefined.
jpayne@69 363 * @return the Date value of this object.
jpayne@69 364 * @stable ICU 2.0
jpayne@69 365 */
jpayne@69 366 UDate getDate() const { return fValue.fDate; }
jpayne@69 367
jpayne@69 368 /**
jpayne@69 369 * Gets the Date value of this object. If the type is not a date,
jpayne@69 370 * status is set to U_INVALID_FORMAT_ERROR and the return value is
jpayne@69 371 * undefined.
jpayne@69 372 * @param status the error code.
jpayne@69 373 * @return the Date value of this object.
jpayne@69 374 * @stable ICU 3.0
jpayne@69 375 */
jpayne@69 376 UDate getDate(UErrorCode& status) const;
jpayne@69 377
jpayne@69 378 /**
jpayne@69 379 * Gets the string value of this object. If this object is not of type
jpayne@69 380 * kString then the result is undefined.
jpayne@69 381 * @param result Output param to receive the Date value of this object.
jpayne@69 382 * @return A reference to 'result'.
jpayne@69 383 * @stable ICU 2.0
jpayne@69 384 */
jpayne@69 385 UnicodeString& getString(UnicodeString& result) const
jpayne@69 386 { result=*fValue.fString; return result; }
jpayne@69 387
jpayne@69 388 /**
jpayne@69 389 * Gets the string value of this object. If the type is not a
jpayne@69 390 * string, status is set to U_INVALID_FORMAT_ERROR and a bogus
jpayne@69 391 * string is returned.
jpayne@69 392 * @param result Output param to receive the Date value of this object.
jpayne@69 393 * @param status the error code.
jpayne@69 394 * @return A reference to 'result'.
jpayne@69 395 * @stable ICU 3.0
jpayne@69 396 */
jpayne@69 397 UnicodeString& getString(UnicodeString& result, UErrorCode& status) const;
jpayne@69 398
jpayne@69 399 /**
jpayne@69 400 * Gets a const reference to the string value of this object. If
jpayne@69 401 * this object is not of type kString then the result is
jpayne@69 402 * undefined.
jpayne@69 403 * @return a const reference to the string value of this object.
jpayne@69 404 * @stable ICU 2.0
jpayne@69 405 */
jpayne@69 406 inline const UnicodeString& getString(void) const;
jpayne@69 407
jpayne@69 408 /**
jpayne@69 409 * Gets a const reference to the string value of this object. If
jpayne@69 410 * the type is not a string, status is set to
jpayne@69 411 * U_INVALID_FORMAT_ERROR and the result is a bogus string.
jpayne@69 412 * @param status the error code.
jpayne@69 413 * @return a const reference to the string value of this object.
jpayne@69 414 * @stable ICU 3.0
jpayne@69 415 */
jpayne@69 416 const UnicodeString& getString(UErrorCode& status) const;
jpayne@69 417
jpayne@69 418 /**
jpayne@69 419 * Gets a reference to the string value of this object. If this
jpayne@69 420 * object is not of type kString then the result is undefined.
jpayne@69 421 * @return a reference to the string value of this object.
jpayne@69 422 * @stable ICU 2.0
jpayne@69 423 */
jpayne@69 424 inline UnicodeString& getString(void);
jpayne@69 425
jpayne@69 426 /**
jpayne@69 427 * Gets a reference to the string value of this object. If the
jpayne@69 428 * type is not a string, status is set to U_INVALID_FORMAT_ERROR
jpayne@69 429 * and the result is a bogus string.
jpayne@69 430 * @param status the error code.
jpayne@69 431 * @return a reference to the string value of this object.
jpayne@69 432 * @stable ICU 3.0
jpayne@69 433 */
jpayne@69 434 UnicodeString& getString(UErrorCode& status);
jpayne@69 435
jpayne@69 436 /**
jpayne@69 437 * Gets the array value and count of this object. If this object
jpayne@69 438 * is not of type kArray then the result is undefined.
jpayne@69 439 * @param count fill-in with the count of this object.
jpayne@69 440 * @return the array value of this object.
jpayne@69 441 * @stable ICU 2.0
jpayne@69 442 */
jpayne@69 443 const Formattable* getArray(int32_t& count) const
jpayne@69 444 { count=fValue.fArrayAndCount.fCount; return fValue.fArrayAndCount.fArray; }
jpayne@69 445
jpayne@69 446 /**
jpayne@69 447 * Gets the array value and count of this object. If the type is
jpayne@69 448 * not an array, status is set to U_INVALID_FORMAT_ERROR, count is
jpayne@69 449 * set to 0, and the result is NULL.
jpayne@69 450 * @param count fill-in with the count of this object.
jpayne@69 451 * @param status the error code.
jpayne@69 452 * @return the array value of this object.
jpayne@69 453 * @stable ICU 3.0
jpayne@69 454 */
jpayne@69 455 const Formattable* getArray(int32_t& count, UErrorCode& status) const;
jpayne@69 456
jpayne@69 457 /**
jpayne@69 458 * Accesses the specified element in the array value of this
jpayne@69 459 * Formattable object. If this object is not of type kArray then
jpayne@69 460 * the result is undefined.
jpayne@69 461 * @param index the specified index.
jpayne@69 462 * @return the accessed element in the array.
jpayne@69 463 * @stable ICU 2.0
jpayne@69 464 */
jpayne@69 465 Formattable& operator[](int32_t index) { return fValue.fArrayAndCount.fArray[index]; }
jpayne@69 466
jpayne@69 467 /**
jpayne@69 468 * Returns a pointer to the UObject contained within this
jpayne@69 469 * formattable, or NULL if this object does not contain a UObject.
jpayne@69 470 * @return a UObject pointer, or NULL
jpayne@69 471 * @stable ICU 3.0
jpayne@69 472 */
jpayne@69 473 const UObject* getObject() const;
jpayne@69 474
jpayne@69 475 /**
jpayne@69 476 * Returns a numeric string representation of the number contained within this
jpayne@69 477 * formattable, or NULL if this object does not contain numeric type.
jpayne@69 478 * For values obtained by parsing, the returned decimal number retains
jpayne@69 479 * the full precision and range of the original input, unconstrained by
jpayne@69 480 * the limits of a double floating point or a 64 bit int.
jpayne@69 481 *
jpayne@69 482 * This function is not thread safe, and therfore is not declared const,
jpayne@69 483 * even though it is logically const.
jpayne@69 484 *
jpayne@69 485 * Possible errors include U_MEMORY_ALLOCATION_ERROR, and
jpayne@69 486 * U_INVALID_STATE if the formattable object has not been set to
jpayne@69 487 * a numeric type.
jpayne@69 488 *
jpayne@69 489 * @param status the error code.
jpayne@69 490 * @return the unformatted string representation of a number.
jpayne@69 491 * @stable ICU 4.4
jpayne@69 492 */
jpayne@69 493 StringPiece getDecimalNumber(UErrorCode &status);
jpayne@69 494
jpayne@69 495 /**
jpayne@69 496 * Sets the double value of this object and changes the type to
jpayne@69 497 * kDouble.
jpayne@69 498 * @param d the new double value to be set.
jpayne@69 499 * @stable ICU 2.0
jpayne@69 500 */
jpayne@69 501 void setDouble(double d);
jpayne@69 502
jpayne@69 503 /**
jpayne@69 504 * Sets the long value of this object and changes the type to
jpayne@69 505 * kLong.
jpayne@69 506 * @param l the new long value to be set.
jpayne@69 507 * @stable ICU 2.0
jpayne@69 508 */
jpayne@69 509 void setLong(int32_t l);
jpayne@69 510
jpayne@69 511 /**
jpayne@69 512 * Sets the int64 value of this object and changes the type to
jpayne@69 513 * kInt64.
jpayne@69 514 * @param ll the new int64 value to be set.
jpayne@69 515 * @stable ICU 2.8
jpayne@69 516 */
jpayne@69 517 void setInt64(int64_t ll);
jpayne@69 518
jpayne@69 519 /**
jpayne@69 520 * Sets the Date value of this object and changes the type to
jpayne@69 521 * kDate.
jpayne@69 522 * @param d the new Date value to be set.
jpayne@69 523 * @stable ICU 2.0
jpayne@69 524 */
jpayne@69 525 void setDate(UDate d);
jpayne@69 526
jpayne@69 527 /**
jpayne@69 528 * Sets the string value of this object and changes the type to
jpayne@69 529 * kString.
jpayne@69 530 * @param stringToCopy the new string value to be set.
jpayne@69 531 * @stable ICU 2.0
jpayne@69 532 */
jpayne@69 533 void setString(const UnicodeString& stringToCopy);
jpayne@69 534
jpayne@69 535 /**
jpayne@69 536 * Sets the array value and count of this object and changes the
jpayne@69 537 * type to kArray.
jpayne@69 538 * @param array the array value.
jpayne@69 539 * @param count the number of array elements to be copied.
jpayne@69 540 * @stable ICU 2.0
jpayne@69 541 */
jpayne@69 542 void setArray(const Formattable* array, int32_t count);
jpayne@69 543
jpayne@69 544 /**
jpayne@69 545 * Sets and adopts the string value and count of this object and
jpayne@69 546 * changes the type to kArray.
jpayne@69 547 * @param stringToAdopt the new string value to be adopted.
jpayne@69 548 * @stable ICU 2.0
jpayne@69 549 */
jpayne@69 550 void adoptString(UnicodeString* stringToAdopt);
jpayne@69 551
jpayne@69 552 /**
jpayne@69 553 * Sets and adopts the array value and count of this object and
jpayne@69 554 * changes the type to kArray.
jpayne@69 555 * @stable ICU 2.0
jpayne@69 556 */
jpayne@69 557 void adoptArray(Formattable* array, int32_t count);
jpayne@69 558
jpayne@69 559 /**
jpayne@69 560 * Sets and adopts the UObject value of this object and changes
jpayne@69 561 * the type to kObject. After this call, the caller must not
jpayne@69 562 * delete the given object.
jpayne@69 563 * @param objectToAdopt the UObject value to be adopted
jpayne@69 564 * @stable ICU 3.0
jpayne@69 565 */
jpayne@69 566 void adoptObject(UObject* objectToAdopt);
jpayne@69 567
jpayne@69 568 /**
jpayne@69 569 * Sets the the numeric value from a decimal number string, and changes
jpayne@69 570 * the type to to a numeric type appropriate for the number.
jpayne@69 571 * The syntax of the number is a "numeric string"
jpayne@69 572 * as defined in the Decimal Arithmetic Specification, available at
jpayne@69 573 * http://speleotrove.com/decimal
jpayne@69 574 * The full precision and range of the input number will be retained,
jpayne@69 575 * even when it exceeds what can be represented by a double or an int64.
jpayne@69 576 *
jpayne@69 577 * @param numberString a string representation of the unformatted decimal number.
jpayne@69 578 * @param status the error code. Set to U_INVALID_FORMAT_ERROR if the
jpayne@69 579 * incoming string is not a valid decimal number.
jpayne@69 580 * @stable ICU 4.4
jpayne@69 581 */
jpayne@69 582 void setDecimalNumber(StringPiece numberString,
jpayne@69 583 UErrorCode &status);
jpayne@69 584
jpayne@69 585 /**
jpayne@69 586 * ICU "poor man's RTTI", returns a UClassID for the actual class.
jpayne@69 587 *
jpayne@69 588 * @stable ICU 2.2
jpayne@69 589 */
jpayne@69 590 virtual UClassID getDynamicClassID() const;
jpayne@69 591
jpayne@69 592 /**
jpayne@69 593 * ICU "poor man's RTTI", returns a UClassID for this class.
jpayne@69 594 *
jpayne@69 595 * @stable ICU 2.2
jpayne@69 596 */
jpayne@69 597 static UClassID U_EXPORT2 getStaticClassID();
jpayne@69 598
jpayne@69 599 /**
jpayne@69 600 * Convert the UFormattable to a Formattable. Internally, this is a reinterpret_cast.
jpayne@69 601 * @param fmt a valid UFormattable
jpayne@69 602 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
jpayne@69 603 * UFormattable, and so is only valid while the original argument remains in scope.
jpayne@69 604 * @stable ICU 52
jpayne@69 605 */
jpayne@69 606 static inline Formattable *fromUFormattable(UFormattable *fmt);
jpayne@69 607
jpayne@69 608 /**
jpayne@69 609 * Convert the const UFormattable to a const Formattable. Internally, this is a reinterpret_cast.
jpayne@69 610 * @param fmt a valid UFormattable
jpayne@69 611 * @return the UFormattable as a Formattable object pointer. This is an alias to the original
jpayne@69 612 * UFormattable, and so is only valid while the original argument remains in scope.
jpayne@69 613 * @stable ICU 52
jpayne@69 614 */
jpayne@69 615 static inline const Formattable *fromUFormattable(const UFormattable *fmt);
jpayne@69 616
jpayne@69 617 /**
jpayne@69 618 * Convert this object pointer to a UFormattable.
jpayne@69 619 * @return this object as a UFormattable pointer. This is an alias to this object,
jpayne@69 620 * and so is only valid while this object remains in scope.
jpayne@69 621 * @stable ICU 52
jpayne@69 622 */
jpayne@69 623 inline UFormattable *toUFormattable();
jpayne@69 624
jpayne@69 625 /**
jpayne@69 626 * Convert this object pointer to a UFormattable.
jpayne@69 627 * @return this object as a UFormattable pointer. This is an alias to this object,
jpayne@69 628 * and so is only valid while this object remains in scope.
jpayne@69 629 * @stable ICU 52
jpayne@69 630 */
jpayne@69 631 inline const UFormattable *toUFormattable() const;
jpayne@69 632
jpayne@69 633 #ifndef U_HIDE_DEPRECATED_API
jpayne@69 634 /**
jpayne@69 635 * Deprecated variant of getLong(UErrorCode&).
jpayne@69 636 * @param status the error code
jpayne@69 637 * @return the long value of this object.
jpayne@69 638 * @deprecated ICU 3.0 use getLong(UErrorCode&) instead
jpayne@69 639 */
jpayne@69 640 inline int32_t getLong(UErrorCode* status) const;
jpayne@69 641 #endif /* U_HIDE_DEPRECATED_API */
jpayne@69 642
jpayne@69 643 #ifndef U_HIDE_INTERNAL_API
jpayne@69 644 /**
jpayne@69 645 * Internal function, do not use.
jpayne@69 646 * TODO: figure out how to make this be non-public.
jpayne@69 647 * NumberFormat::format(Formattable, ...
jpayne@69 648 * needs to get at the DecimalQuantity, if it exists, for
jpayne@69 649 * big decimal formatting.
jpayne@69 650 * @internal
jpayne@69 651 */
jpayne@69 652 number::impl::DecimalQuantity *getDecimalQuantity() const { return fDecimalQuantity;}
jpayne@69 653
jpayne@69 654 /**
jpayne@69 655 * Export the value of this Formattable to a DecimalQuantity.
jpayne@69 656 * @internal
jpayne@69 657 */
jpayne@69 658 void populateDecimalQuantity(number::impl::DecimalQuantity& output, UErrorCode& status) const;
jpayne@69 659
jpayne@69 660 /**
jpayne@69 661 * Adopt, and set value from, a DecimalQuantity
jpayne@69 662 * Internal Function, do not use.
jpayne@69 663 * @param dq the DecimalQuantity to be adopted
jpayne@69 664 * @internal
jpayne@69 665 */
jpayne@69 666 void adoptDecimalQuantity(number::impl::DecimalQuantity *dq);
jpayne@69 667
jpayne@69 668 /**
jpayne@69 669 * Internal function to return the CharString pointer.
jpayne@69 670 * @param status error code
jpayne@69 671 * @return pointer to the CharString - may become invalid if the object is modified
jpayne@69 672 * @internal
jpayne@69 673 */
jpayne@69 674 CharString *internalGetCharString(UErrorCode &status);
jpayne@69 675
jpayne@69 676 #endif /* U_HIDE_INTERNAL_API */
jpayne@69 677
jpayne@69 678 private:
jpayne@69 679 /**
jpayne@69 680 * Cleans up the memory for unwanted values. For example, the adopted
jpayne@69 681 * string or array objects.
jpayne@69 682 */
jpayne@69 683 void dispose(void);
jpayne@69 684
jpayne@69 685 /**
jpayne@69 686 * Common initialization, for use by constructors.
jpayne@69 687 */
jpayne@69 688 void init();
jpayne@69 689
jpayne@69 690 UnicodeString* getBogus() const;
jpayne@69 691
jpayne@69 692 union {
jpayne@69 693 UObject* fObject;
jpayne@69 694 UnicodeString* fString;
jpayne@69 695 double fDouble;
jpayne@69 696 int64_t fInt64;
jpayne@69 697 UDate fDate;
jpayne@69 698 struct {
jpayne@69 699 Formattable* fArray;
jpayne@69 700 int32_t fCount;
jpayne@69 701 } fArrayAndCount;
jpayne@69 702 } fValue;
jpayne@69 703
jpayne@69 704 CharString *fDecimalStr;
jpayne@69 705
jpayne@69 706 number::impl::DecimalQuantity *fDecimalQuantity;
jpayne@69 707
jpayne@69 708 Type fType;
jpayne@69 709 UnicodeString fBogus; // Bogus string when it's needed.
jpayne@69 710 };
jpayne@69 711
jpayne@69 712 inline UDate Formattable::getDate(UErrorCode& status) const {
jpayne@69 713 if (fType != kDate) {
jpayne@69 714 if (U_SUCCESS(status)) {
jpayne@69 715 status = U_INVALID_FORMAT_ERROR;
jpayne@69 716 }
jpayne@69 717 return 0;
jpayne@69 718 }
jpayne@69 719 return fValue.fDate;
jpayne@69 720 }
jpayne@69 721
jpayne@69 722 inline const UnicodeString& Formattable::getString(void) const {
jpayne@69 723 return *fValue.fString;
jpayne@69 724 }
jpayne@69 725
jpayne@69 726 inline UnicodeString& Formattable::getString(void) {
jpayne@69 727 return *fValue.fString;
jpayne@69 728 }
jpayne@69 729
jpayne@69 730 #ifndef U_HIDE_DEPRECATED_API
jpayne@69 731 inline int32_t Formattable::getLong(UErrorCode* status) const {
jpayne@69 732 return getLong(*status);
jpayne@69 733 }
jpayne@69 734 #endif /* U_HIDE_DEPRECATED_API */
jpayne@69 735
jpayne@69 736 inline UFormattable* Formattable::toUFormattable() {
jpayne@69 737 return reinterpret_cast<UFormattable*>(this);
jpayne@69 738 }
jpayne@69 739
jpayne@69 740 inline const UFormattable* Formattable::toUFormattable() const {
jpayne@69 741 return reinterpret_cast<const UFormattable*>(this);
jpayne@69 742 }
jpayne@69 743
jpayne@69 744 inline Formattable* Formattable::fromUFormattable(UFormattable *fmt) {
jpayne@69 745 return reinterpret_cast<Formattable *>(fmt);
jpayne@69 746 }
jpayne@69 747
jpayne@69 748 inline const Formattable* Formattable::fromUFormattable(const UFormattable *fmt) {
jpayne@69 749 return reinterpret_cast<const Formattable *>(fmt);
jpayne@69 750 }
jpayne@69 751
jpayne@69 752 U_NAMESPACE_END
jpayne@69 753
jpayne@69 754 #endif /* #if !UCONFIG_NO_FORMATTING */
jpayne@69 755
jpayne@69 756 #endif /* U_SHOW_CPLUSPLUS_API */
jpayne@69 757
jpayne@69 758 #endif //_FMTABLE
jpayne@69 759 //eof