Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/native/common/unicode/strenum.h
38827 views
/*1*******************************************************************************2*3* Copyright (C) 2002-2012, International Business Machines4* Corporation and others. All Rights Reserved.5*6*******************************************************************************7*/89#ifndef STRENUM_H10#define STRENUM_H1112#include "unicode/uobject.h"13#include "unicode/unistr.h"1415/**16* \file17* \brief C++ API: String Enumeration18*/1920U_NAMESPACE_BEGIN2122/**23* Base class for 'pure' C++ implementations of uenum api. Adds a24* method that returns the next UnicodeString since in C++ this can25* be a common storage format for strings.26*27* <p>The model is that the enumeration is over strings maintained by28* a 'service.' At any point, the service might change, invalidating29* the enumerator (though this is expected to be rare). The iterator30* returns an error if this has occurred. Lack of the error is no31* guarantee that the service didn't change immediately after the32* call, so the returned string still might not be 'valid' on33* subsequent use.</p>34*35* <p>Strings may take the form of const char*, const UChar*, or const36* UnicodeString*. The type you get is determine by the variant of37* 'next' that you call. In general the StringEnumeration is38* optimized for one of these types, but all StringEnumerations can39* return all types. Returned strings are each terminated with a NUL.40* Depending on the service data, they might also include embedded NUL41* characters, so API is provided to optionally return the true42* length, counting the embedded NULs but not counting the terminating43* NUL.</p>44*45* <p>The pointers returned by next, unext, and snext become invalid46* upon any subsequent call to the enumeration's destructor, next,47* unext, snext, or reset.</p>48*49* ICU 2.8 adds some default implementations and helper functions50* for subclasses.51*52* @stable ICU 2.453*/54class U_COMMON_API StringEnumeration : public UObject {55public:56/**57* Destructor.58* @stable ICU 2.459*/60virtual ~StringEnumeration();6162/**63* Clone this object, an instance of a subclass of StringEnumeration.64* Clones can be used concurrently in multiple threads.65* If a subclass does not implement clone(), or if an error occurs,66* then NULL is returned.67* The clone functions in all subclasses return a base class pointer68* because some compilers do not support covariant (same-as-this)69* return types; cast to the appropriate subclass if necessary.70* The caller must delete the clone.71*72* @return a clone of this object73*74* @see getDynamicClassID75* @stable ICU 2.876*/77virtual StringEnumeration *clone() const;7879/**80* <p>Return the number of elements that the iterator traverses. If81* the iterator is out of sync with its service, status is set to82* U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>83*84* <p>The return value will not change except possibly as a result of85* a subsequent call to reset, or if the iterator becomes out of sync.</p>86*87* <p>This is a convenience function. It can end up being very88* expensive as all the items might have to be pre-fetched89* (depending on the storage format of the data being90* traversed).</p>91*92* @param status the error code.93* @return number of elements in the iterator.94*95* @stable ICU 2.4 */96virtual int32_t count(UErrorCode& status) const = 0;9798/**99* <p>Returns the next element as a NUL-terminated char*. If there100* are no more elements, returns NULL. If the resultLength pointer101* is not NULL, the length of the string (not counting the102* terminating NUL) is returned at that address. If an error103* status is returned, the value at resultLength is undefined.</p>104*105* <p>The returned pointer is owned by this iterator and must not be106* deleted by the caller. The pointer is valid until the next call107* to next, unext, snext, reset, or the enumerator's destructor.</p>108*109* <p>If the iterator is out of sync with its service, status is set110* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>111*112* <p>If the native service string is a UChar* string, it is113* converted to char* with the invariant converter. If the114* conversion fails (because a character cannot be converted) then115* status is set to U_INVARIANT_CONVERSION_ERROR and the return116* value is undefined (though not NULL).</p>117*118* Starting with ICU 2.8, the default implementation calls snext()119* and handles the conversion.120* Either next() or snext() must be implemented differently by a subclass.121*122* @param status the error code.123* @param resultLength a pointer to receive the length, can be NULL.124* @return a pointer to the string, or NULL.125*126* @stable ICU 2.4127*/128virtual const char* next(int32_t *resultLength, UErrorCode& status);129130/**131* <p>Returns the next element as a NUL-terminated UChar*. If there132* are no more elements, returns NULL. If the resultLength pointer133* is not NULL, the length of the string (not counting the134* terminating NUL) is returned at that address. If an error135* status is returned, the value at resultLength is undefined.</p>136*137* <p>The returned pointer is owned by this iterator and must not be138* deleted by the caller. The pointer is valid until the next call139* to next, unext, snext, reset, or the enumerator's destructor.</p>140*141* <p>If the iterator is out of sync with its service, status is set142* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>143*144* Starting with ICU 2.8, the default implementation calls snext()145* and handles the conversion.146*147* @param status the error code.148* @param resultLength a ponter to receive the length, can be NULL.149* @return a pointer to the string, or NULL.150*151* @stable ICU 2.4152*/153virtual const UChar* unext(int32_t *resultLength, UErrorCode& status);154155/**156* <p>Returns the next element a UnicodeString*. If there are no157* more elements, returns NULL.</p>158*159* <p>The returned pointer is owned by this iterator and must not be160* deleted by the caller. The pointer is valid until the next call161* to next, unext, snext, reset, or the enumerator's destructor.</p>162*163* <p>If the iterator is out of sync with its service, status is set164* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>165*166* Starting with ICU 2.8, the default implementation calls next()167* and handles the conversion.168* Either next() or snext() must be implemented differently by a subclass.169*170* @param status the error code.171* @return a pointer to the string, or NULL.172*173* @stable ICU 2.4174*/175virtual const UnicodeString* snext(UErrorCode& status);176177/**178* <p>Resets the iterator. This re-establishes sync with the179* service and rewinds the iterator to start at the first180* element.</p>181*182* <p>Previous pointers returned by next, unext, or snext become183* invalid, and the value returned by count might change.</p>184*185* @param status the error code.186*187* @stable ICU 2.4188*/189virtual void reset(UErrorCode& status) = 0;190191/**192* Compares this enumeration to other to check if both are equal193*194* @param that The other string enumeration to compare this object to195* @return TRUE if the enumerations are equal. FALSE if not.196* @stable ICU 3.6197*/198virtual UBool operator==(const StringEnumeration& that)const;199/**200* Compares this enumeration to other to check if both are not equal201*202* @param that The other string enumeration to compare this object to203* @return TRUE if the enumerations are equal. FALSE if not.204* @stable ICU 3.6205*/206virtual UBool operator!=(const StringEnumeration& that)const;207208protected:209/**210* UnicodeString field for use with default implementations and subclasses.211* @stable ICU 2.8212*/213UnicodeString unistr;214/**215* char * default buffer for use with default implementations and subclasses.216* @stable ICU 2.8217*/218char charsBuffer[32];219/**220* char * buffer for use with default implementations and subclasses.221* Allocated in constructor and in ensureCharsCapacity().222* @stable ICU 2.8223*/224char *chars;225/**226* Capacity of chars, for use with default implementations and subclasses.227* @stable ICU 2.8228*/229int32_t charsCapacity;230231/**232* Default constructor for use with default implementations and subclasses.233* @stable ICU 2.8234*/235StringEnumeration();236237/**238* Ensures that chars is at least as large as the requested capacity.239* For use with default implementations and subclasses.240*241* @param capacity Requested capacity.242* @param status ICU in/out error code.243* @stable ICU 2.8244*/245void ensureCharsCapacity(int32_t capacity, UErrorCode &status);246247/**248* Converts s to Unicode and sets unistr to the result.249* For use with default implementations and subclasses,250* especially for implementations of snext() in terms of next().251* This is provided with a helper function instead of a default implementation252* of snext() to avoid potential infinite loops between next() and snext().253*254* For example:255* \code256* const UnicodeString* snext(UErrorCode& status) {257* int32_t resultLength=0;258* const char *s=next(&resultLength, status);259* return setChars(s, resultLength, status);260* }261* \endcode262*263* @param s String to be converted to Unicode.264* @param length Length of the string.265* @param status ICU in/out error code.266* @return A pointer to unistr.267* @stable ICU 2.8268*/269UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);270};271272U_NAMESPACE_END273274/* STRENUM_H */275#endif276277278