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