sl@0: /* sl@0: ******************************************************************************* sl@0: * sl@0: * Copyright (C) 2002-2005, International Business Machines sl@0: * Corporation and others. All Rights Reserved. sl@0: * sl@0: ******************************************************************************* sl@0: */ sl@0: sl@0: #ifndef STRENUM_H sl@0: #define STRENUM_H sl@0: sl@0: #include "unicode/uobject.h" sl@0: #include "unicode/unistr.h" sl@0: sl@0: /** sl@0: * \file sl@0: * \brief C++ API: String Enumeration sl@0: */ sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /** sl@0: * Base class for 'pure' C++ implementations of uenum api. Adds a sl@0: * method that returns the next UnicodeString since in C++ this can sl@0: * be a common storage format for strings. sl@0: * sl@0: *
The model is that the enumeration is over strings maintained by sl@0: * a 'service.' At any point, the service might change, invalidating sl@0: * the enumerator (though this is expected to be rare). The iterator sl@0: * returns an error if this has occurred. Lack of the error is no sl@0: * guarantee that the service didn't change immediately after the sl@0: * call, so the returned string still might not be 'valid' on sl@0: * subsequent use.
sl@0: * sl@0: *Strings may take the form of const char*, const UChar*, or const sl@0: * UnicodeString*. The type you get is determine by the variant of sl@0: * 'next' that you call. In general the StringEnumeration is sl@0: * optimized for one of these types, but all StringEnumerations can sl@0: * return all types. Returned strings are each terminated with a NUL. sl@0: * Depending on the service data, they might also include embedded NUL sl@0: * characters, so API is provided to optionally return the true sl@0: * length, counting the embedded NULs but not counting the terminating sl@0: * NUL.
sl@0: * sl@0: *The pointers returned by next, unext, and snext become invalid sl@0: * upon any subsequent call to the enumeration's destructor, next, sl@0: * unext, snext, or reset.
sl@0: * sl@0: * ICU 2.8 adds some default implementations and helper functions sl@0: * for subclasses. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: class U_COMMON_API StringEnumeration : public UObject { sl@0: public: sl@0: /** sl@0: * Destructor. sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual ~StringEnumeration(); sl@0: sl@0: /** sl@0: * Clone this object, an instance of a subclass of StringEnumeration. sl@0: * Clones can be used concurrently in multiple threads. sl@0: * If a subclass does not implement clone(), or if an error occurs, sl@0: * then NULL is returned. sl@0: * The clone functions in all subclasses return a base class pointer sl@0: * because some compilers do not support covariant (same-as-this) sl@0: * return types; cast to the appropriate subclass if necessary. sl@0: * The caller must delete the clone. sl@0: * sl@0: * @return a clone of this object sl@0: * sl@0: * @see getDynamicClassID sl@0: * @stable ICU 2.8 sl@0: */ sl@0: virtual StringEnumeration *clone() const; sl@0: sl@0: /** sl@0: *Return the number of elements that the iterator traverses. If sl@0: * the iterator is out of sync with its service, status is set to sl@0: * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.
sl@0: * sl@0: *The return value will not change except possibly as a result of sl@0: * a subsequent call to reset, or if the iterator becomes out of sync.
sl@0: * sl@0: *This is a convenience function. It can end up being very sl@0: * expensive as all the items might have to be pre-fetched sl@0: * (depending on the storage format of the data being sl@0: * traversed).
sl@0: * sl@0: * @param status the error code. sl@0: * @return number of elements in the iterator. sl@0: * sl@0: * @stable ICU 2.4 */ sl@0: virtual int32_t count(UErrorCode& status) const = 0; sl@0: sl@0: /** sl@0: *Returns the next element as a NUL-terminated char*. If there sl@0: * are no more elements, returns NULL. If the resultLength pointer sl@0: * is not NULL, the length of the string (not counting the sl@0: * terminating NUL) is returned at that address. If an error sl@0: * status is returned, the value at resultLength is undefined.
sl@0: * sl@0: *The returned pointer is owned by this iterator and must not be sl@0: * deleted by the caller. The pointer is valid until the next call sl@0: * to next, unext, snext, reset, or the enumerator's destructor.
sl@0: * sl@0: *If the iterator is out of sync with its service, status is set sl@0: * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.
sl@0: * sl@0: *If the native service string is a UChar* string, it is sl@0: * converted to char* with the invariant converter. If the sl@0: * conversion fails (because a character cannot be converted) then sl@0: * status is set to U_INVARIANT_CONVERSION_ERROR and the return sl@0: * value is undefined (though not NULL).
sl@0: * sl@0: * Starting with ICU 2.8, the default implementation calls snext() sl@0: * and handles the conversion. sl@0: * sl@0: * @param status the error code. sl@0: * @param resultLength a pointer to receive the length, can be NULL. sl@0: * @return a pointer to the string, or NULL. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual const char* next(int32_t *resultLength, UErrorCode& status); sl@0: sl@0: /** sl@0: *Returns the next element as a NUL-terminated UChar*. If there sl@0: * are no more elements, returns NULL. If the resultLength pointer sl@0: * is not NULL, the length of the string (not counting the sl@0: * terminating NUL) is returned at that address. If an error sl@0: * status is returned, the value at resultLength is undefined.
sl@0: * sl@0: *The returned pointer is owned by this iterator and must not be sl@0: * deleted by the caller. The pointer is valid until the next call sl@0: * to next, unext, snext, reset, or the enumerator's destructor.
sl@0: * sl@0: *If the iterator is out of sync with its service, status is set sl@0: * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.
sl@0: * sl@0: * Starting with ICU 2.8, the default implementation calls snext() sl@0: * and handles the conversion. sl@0: * sl@0: * @param status the error code. sl@0: * @param resultLength a ponter to receive the length, can be NULL. sl@0: * @return a pointer to the string, or NULL. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual const UChar* unext(int32_t *resultLength, UErrorCode& status); sl@0: sl@0: /** sl@0: *Returns the next element a UnicodeString*. If there are no sl@0: * more elements, returns NULL.
sl@0: * sl@0: *The returned pointer is owned by this iterator and must not be sl@0: * deleted by the caller. The pointer is valid until the next call sl@0: * to next, unext, snext, reset, or the enumerator's destructor.
sl@0: * sl@0: *If the iterator is out of sync with its service, status is set sl@0: * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.
sl@0: * sl@0: * @param status the error code. sl@0: * @return a pointer to the string, or NULL. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual const UnicodeString* snext(UErrorCode& status) = 0; sl@0: sl@0: /** sl@0: *Resets the iterator. This re-establishes sync with the sl@0: * service and rewinds the iterator to start at the first sl@0: * element.
sl@0: * sl@0: *Previous pointers returned by next, unext, or snext become sl@0: * invalid, and the value returned by count might change.
sl@0: * sl@0: * @param status the error code. sl@0: * sl@0: * @stable ICU 2.4 sl@0: */ sl@0: virtual void reset(UErrorCode& status) = 0; sl@0: sl@0: protected: sl@0: /** sl@0: * UnicodeString field for use with default implementations and subclasses. sl@0: * @stable ICU 2.8 sl@0: */ sl@0: UnicodeString unistr; sl@0: /** sl@0: * char * default buffer for use with default implementations and subclasses. sl@0: * @stable ICU 2.8 sl@0: */ sl@0: char charsBuffer[32]; sl@0: /** sl@0: * char * buffer for use with default implementations and subclasses. sl@0: * Allocated in constructor and in ensureCharsCapacity(). sl@0: * @stable ICU 2.8 sl@0: */ sl@0: char *chars; sl@0: /** sl@0: * Capacity of chars, for use with default implementations and subclasses. sl@0: * @stable ICU 2.8 sl@0: */ sl@0: int32_t charsCapacity; sl@0: sl@0: /** sl@0: * Default constructor for use with default implementations and subclasses. sl@0: * @stable ICU 2.8 sl@0: */ sl@0: StringEnumeration(); sl@0: sl@0: /** sl@0: * Ensures that chars is at least as large as the requested capacity. sl@0: * For use with default implementations and subclasses. sl@0: * sl@0: * @param capacity Requested capacity. sl@0: * @param status ICU in/out error code. sl@0: * @stable ICU 2.8 sl@0: */ sl@0: void ensureCharsCapacity(int32_t capacity, UErrorCode &status); sl@0: sl@0: /** sl@0: * Converts s to Unicode and sets unistr to the result. sl@0: * For use with default implementations and subclasses, sl@0: * especially for implementations of snext() in terms of next(). sl@0: * This is provided with a helper function instead of a default implementation sl@0: * of snext() to avoid potential infinite loops between next() and snext(). sl@0: * sl@0: * For example: sl@0: * \code sl@0: * const UnicodeString* snext(UErrorCode& status) { sl@0: * int32_t resultLength=0; sl@0: * const char *s=next(&resultLength, status); sl@0: * return setChars(s, resultLength, status); sl@0: * } sl@0: * \endcode sl@0: * sl@0: * @param s String to be converted to Unicode. sl@0: * @param length Length of the string. sl@0: * @param status ICU in/out error code. sl@0: * @return A pointer to unistr. sl@0: * @stable ICU 2.8 sl@0: */ sl@0: UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status); sl@0: }; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: /* STRENUM_H */ sl@0: #endif