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