Path: blob/jdk8u272-b10-aarch32-20201026/jdk/src/share/native/common/unicode/strenum.h
48773 views
// © 2016 and later: Unicode, Inc. and others.1// License & terms of use: http://www.unicode.org/copyright.html2/*3*******************************************************************************4*5* Copyright (C) 2002-2012, International Business Machines6* Corporation and others. All Rights Reserved.7*8*******************************************************************************9*/1011#ifndef STRENUM_H12#define STRENUM_H1314#include "unicode/uobject.h"15#include "unicode/unistr.h"1617/**18* \file19* \brief C++ API: String Enumeration20*/2122U_NAMESPACE_BEGIN2324/**25* Base class for 'pure' C++ implementations of uenum api. Adds a26* method that returns the next UnicodeString since in C++ this can27* be a common storage format for strings.28*29* <p>The model is that the enumeration is over strings maintained by30* a 'service.' At any point, the service might change, invalidating31* the enumerator (though this is expected to be rare). The iterator32* returns an error if this has occurred. Lack of the error is no33* guarantee that the service didn't change immediately after the34* call, so the returned string still might not be 'valid' on35* subsequent use.</p>36*37* <p>Strings may take the form of const char*, const char16_t*, or const38* UnicodeString*. The type you get is determine by the variant of39* 'next' that you call. In general the StringEnumeration is40* optimized for one of these types, but all StringEnumerations can41* return all types. Returned strings are each terminated with a NUL.42* Depending on the service data, they might also include embedded NUL43* characters, so API is provided to optionally return the true44* length, counting the embedded NULs but not counting the terminating45* NUL.</p>46*47* <p>The pointers returned by next, unext, and snext become invalid48* upon any subsequent call to the enumeration's destructor, next,49* unext, snext, or reset.</p>50*51* ICU 2.8 adds some default implementations and helper functions52* for subclasses.53*54* @stable ICU 2.455*/56class U_COMMON_API StringEnumeration : public UObject {57public:58/**59* Destructor.60* @stable ICU 2.461*/62virtual ~StringEnumeration();6364/**65* Clone this object, an instance of a subclass of StringEnumeration.66* Clones can be used concurrently in multiple threads.67* If a subclass does not implement clone(), or if an error occurs,68* then NULL is returned.69* The clone functions in all subclasses return a base class pointer70* because some compilers do not support covariant (same-as-this)71* return types; cast to the appropriate subclass if necessary.72* The caller must delete the clone.73*74* @return a clone of this object75*76* @see getDynamicClassID77* @stable ICU 2.878*/79virtual StringEnumeration *clone() const;8081/**82* <p>Return the number of elements that the iterator traverses. If83* the iterator is out of sync with its service, status is set to84* U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>85*86* <p>The return value will not change except possibly as a result of87* a subsequent call to reset, or if the iterator becomes out of sync.</p>88*89* <p>This is a convenience function. It can end up being very90* expensive as all the items might have to be pre-fetched91* (depending on the storage format of the data being92* traversed).</p>93*94* @param status the error code.95* @return number of elements in the iterator.96*97* @stable ICU 2.4 */98virtual int32_t count(UErrorCode& status) const = 0;99100/**101* <p>Returns the next element as a NUL-terminated char*. If there102* are no more elements, returns NULL. If the resultLength pointer103* is not NULL, the length of the string (not counting the104* terminating NUL) is returned at that address. If an error105* status is returned, the value at resultLength is undefined.</p>106*107* <p>The returned pointer is owned by this iterator and must not be108* deleted by the caller. The pointer is valid until the next call109* to next, unext, snext, reset, or the enumerator's destructor.</p>110*111* <p>If the iterator is out of sync with its service, status is set112* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>113*114* <p>If the native service string is a char16_t* string, it is115* converted to char* with the invariant converter. If the116* conversion fails (because a character cannot be converted) then117* status is set to U_INVARIANT_CONVERSION_ERROR and the return118* value is undefined (though not NULL).</p>119*120* Starting with ICU 2.8, the default implementation calls snext()121* and handles the conversion.122* Either next() or snext() must be implemented differently by a subclass.123*124* @param status the error code.125* @param resultLength a pointer to receive the length, can be NULL.126* @return a pointer to the string, or NULL.127*128* @stable ICU 2.4129*/130virtual const char* next(int32_t *resultLength, UErrorCode& status);131132/**133* <p>Returns the next element as a NUL-terminated char16_t*. If there134* are no more elements, returns NULL. If the resultLength pointer135* is not NULL, the length of the string (not counting the136* terminating NUL) is returned at that address. If an error137* status is returned, the value at resultLength is undefined.</p>138*139* <p>The returned pointer is owned by this iterator and must not be140* deleted by the caller. The pointer is valid until the next call141* to next, unext, snext, reset, or the enumerator's destructor.</p>142*143* <p>If the iterator is out of sync with its service, status is set144* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>145*146* Starting with ICU 2.8, the default implementation calls snext()147* and handles the conversion.148*149* @param status the error code.150* @param resultLength a ponter to receive the length, can be NULL.151* @return a pointer to the string, or NULL.152*153* @stable ICU 2.4154*/155virtual const char16_t* unext(int32_t *resultLength, UErrorCode& status);156157/**158* <p>Returns the next element a UnicodeString*. If there are no159* more elements, returns NULL.</p>160*161* <p>The returned pointer is owned by this iterator and must not be162* deleted by the caller. The pointer is valid until the next call163* to next, unext, snext, reset, or the enumerator's destructor.</p>164*165* <p>If the iterator is out of sync with its service, status is set166* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>167*168* Starting with ICU 2.8, the default implementation calls next()169* and handles the conversion.170* Either next() or snext() must be implemented differently by a subclass.171*172* @param status the error code.173* @return a pointer to the string, or NULL.174*175* @stable ICU 2.4176*/177virtual const UnicodeString* snext(UErrorCode& status);178179/**180* <p>Resets the iterator. This re-establishes sync with the181* service and rewinds the iterator to start at the first182* element.</p>183*184* <p>Previous pointers returned by next, unext, or snext become185* invalid, and the value returned by count might change.</p>186*187* @param status the error code.188*189* @stable ICU 2.4190*/191virtual void reset(UErrorCode& status) = 0;192193/**194* Compares this enumeration to other to check if both are equal195*196* @param that The other string enumeration to compare this object to197* @return TRUE if the enumerations are equal. FALSE if not.198* @stable ICU 3.6199*/200virtual UBool operator==(const StringEnumeration& that)const;201/**202* Compares this enumeration to other to check if both are not equal203*204* @param that The other string enumeration to compare this object to205* @return TRUE if the enumerations are equal. FALSE if not.206* @stable ICU 3.6207*/208virtual UBool operator!=(const StringEnumeration& that)const;209210protected:211/**212* UnicodeString field for use with default implementations and subclasses.213* @stable ICU 2.8214*/215UnicodeString unistr;216/**217* char * default buffer for use with default implementations and subclasses.218* @stable ICU 2.8219*/220char charsBuffer[32];221/**222* char * buffer for use with default implementations and subclasses.223* Allocated in constructor and in ensureCharsCapacity().224* @stable ICU 2.8225*/226char *chars;227/**228* Capacity of chars, for use with default implementations and subclasses.229* @stable ICU 2.8230*/231int32_t charsCapacity;232233/**234* Default constructor for use with default implementations and subclasses.235* @stable ICU 2.8236*/237StringEnumeration();238239/**240* Ensures that chars is at least as large as the requested capacity.241* For use with default implementations and subclasses.242*243* @param capacity Requested capacity.244* @param status ICU in/out error code.245* @stable ICU 2.8246*/247void ensureCharsCapacity(int32_t capacity, UErrorCode &status);248249/**250* Converts s to Unicode and sets unistr to the result.251* For use with default implementations and subclasses,252* especially for implementations of snext() in terms of next().253* This is provided with a helper function instead of a default implementation254* of snext() to avoid potential infinite loops between next() and snext().255*256* For example:257* \code258* const UnicodeString* snext(UErrorCode& status) {259* int32_t resultLength=0;260* const char *s=next(&resultLength, status);261* return setChars(s, resultLength, status);262* }263* \endcode264*265* @param s String to be converted to Unicode.266* @param length Length of the string.267* @param status ICU in/out error code.268* @return A pointer to unistr.269* @stable ICU 2.8270*/271UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);272};273274U_NAMESPACE_END275276/* STRENUM_H */277#endif278279280