1 // Copyright (C) 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
4 *******************************************************************************
6 * Copyright (C) 2002-2012, International Business Machines
7 * Corporation and others. All Rights Reserved.
9 *******************************************************************************
15 #include "unicode/uobject.h"
16 #include "unicode/unistr.h"
20 * \brief C++ API: String Enumeration
26 * Base class for 'pure' C++ implementations of uenum api. Adds a
27 * method that returns the next UnicodeString since in C++ this can
28 * be a common storage format for strings.
30 * <p>The model is that the enumeration is over strings maintained by
31 * a 'service.' At any point, the service might change, invalidating
32 * the enumerator (though this is expected to be rare). The iterator
33 * returns an error if this has occurred. Lack of the error is no
34 * guarantee that the service didn't change immediately after the
35 * call, so the returned string still might not be 'valid' on
38 * <p>Strings may take the form of const char*, const UChar*, or const
39 * UnicodeString*. The type you get is determine by the variant of
40 * 'next' that you call. In general the StringEnumeration is
41 * optimized for one of these types, but all StringEnumerations can
42 * return all types. Returned strings are each terminated with a NUL.
43 * Depending on the service data, they might also include embedded NUL
44 * characters, so API is provided to optionally return the true
45 * length, counting the embedded NULs but not counting the terminating
48 * <p>The pointers returned by next, unext, and snext become invalid
49 * upon any subsequent call to the enumeration's destructor, next,
50 * unext, snext, or reset.</p>
52 * ICU 2.8 adds some default implementations and helper functions
57 class U_COMMON_API StringEnumeration : public UObject {
63 virtual ~StringEnumeration();
66 * Clone this object, an instance of a subclass of StringEnumeration.
67 * Clones can be used concurrently in multiple threads.
68 * If a subclass does not implement clone(), or if an error occurs,
69 * then NULL is returned.
70 * The clone functions in all subclasses return a base class pointer
71 * because some compilers do not support covariant (same-as-this)
72 * return types; cast to the appropriate subclass if necessary.
73 * The caller must delete the clone.
75 * @return a clone of this object
77 * @see getDynamicClassID
80 virtual StringEnumeration *clone() const;
83 * <p>Return the number of elements that the iterator traverses. If
84 * the iterator is out of sync with its service, status is set to
85 * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
87 * <p>The return value will not change except possibly as a result of
88 * a subsequent call to reset, or if the iterator becomes out of sync.</p>
90 * <p>This is a convenience function. It can end up being very
91 * expensive as all the items might have to be pre-fetched
92 * (depending on the storage format of the data being
95 * @param status the error code.
96 * @return number of elements in the iterator.
99 virtual int32_t count(UErrorCode& status) const = 0;
102 * <p>Returns the next element as a NUL-terminated char*. If there
103 * are no more elements, returns NULL. If the resultLength pointer
104 * is not NULL, the length of the string (not counting the
105 * terminating NUL) is returned at that address. If an error
106 * status is returned, the value at resultLength is undefined.</p>
108 * <p>The returned pointer is owned by this iterator and must not be
109 * deleted by the caller. The pointer is valid until the next call
110 * to next, unext, snext, reset, or the enumerator's destructor.</p>
112 * <p>If the iterator is out of sync with its service, status is set
113 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
115 * <p>If the native service string is a UChar* string, it is
116 * converted to char* with the invariant converter. If the
117 * conversion fails (because a character cannot be converted) then
118 * status is set to U_INVARIANT_CONVERSION_ERROR and the return
119 * value is undefined (though not NULL).</p>
121 * Starting with ICU 2.8, the default implementation calls snext()
122 * and handles the conversion.
123 * Either next() or snext() must be implemented differently by a subclass.
125 * @param status the error code.
126 * @param resultLength a pointer to receive the length, can be NULL.
127 * @return a pointer to the string, or NULL.
131 virtual const char* next(int32_t *resultLength, UErrorCode& status);
134 * <p>Returns the next element as a NUL-terminated UChar*. If there
135 * are no more elements, returns NULL. If the resultLength pointer
136 * is not NULL, the length of the string (not counting the
137 * terminating NUL) is returned at that address. If an error
138 * status is returned, the value at resultLength is undefined.</p>
140 * <p>The returned pointer is owned by this iterator and must not be
141 * deleted by the caller. The pointer is valid until the next call
142 * to next, unext, snext, reset, or the enumerator's destructor.</p>
144 * <p>If the iterator is out of sync with its service, status is set
145 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
147 * Starting with ICU 2.8, the default implementation calls snext()
148 * and handles the conversion.
150 * @param status the error code.
151 * @param resultLength a ponter to receive the length, can be NULL.
152 * @return a pointer to the string, or NULL.
156 virtual const UChar* unext(int32_t *resultLength, UErrorCode& status);
159 * <p>Returns the next element a UnicodeString*. If there are no
160 * more elements, returns NULL.</p>
162 * <p>The returned pointer is owned by this iterator and must not be
163 * deleted by the caller. The pointer is valid until the next call
164 * to next, unext, snext, reset, or the enumerator's destructor.</p>
166 * <p>If the iterator is out of sync with its service, status is set
167 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
169 * Starting with ICU 2.8, the default implementation calls next()
170 * and handles the conversion.
171 * Either next() or snext() must be implemented differently by a subclass.
173 * @param status the error code.
174 * @return a pointer to the string, or NULL.
178 virtual const UnicodeString* snext(UErrorCode& status);
181 * <p>Resets the iterator. This re-establishes sync with the
182 * service and rewinds the iterator to start at the first
185 * <p>Previous pointers returned by next, unext, or snext become
186 * invalid, and the value returned by count might change.</p>
188 * @param status the error code.
192 virtual void reset(UErrorCode& status) = 0;
195 * Compares this enumeration to other to check if both are equal
197 * @param that The other string enumeration to compare this object to
198 * @return TRUE if the enumerations are equal. FALSE if not.
201 virtual UBool operator==(const StringEnumeration& that)const;
203 * Compares this enumeration to other to check if both are not equal
205 * @param that The other string enumeration to compare this object to
206 * @return TRUE if the enumerations are equal. FALSE if not.
209 virtual UBool operator!=(const StringEnumeration& that)const;
213 * UnicodeString field for use with default implementations and subclasses.
216 UnicodeString unistr;
218 * char * default buffer for use with default implementations and subclasses.
221 char charsBuffer[32];
223 * char * buffer for use with default implementations and subclasses.
224 * Allocated in constructor and in ensureCharsCapacity().
229 * Capacity of chars, for use with default implementations and subclasses.
232 int32_t charsCapacity;
235 * Default constructor for use with default implementations and subclasses.
241 * Ensures that chars is at least as large as the requested capacity.
242 * For use with default implementations and subclasses.
244 * @param capacity Requested capacity.
245 * @param status ICU in/out error code.
248 void ensureCharsCapacity(int32_t capacity, UErrorCode &status);
251 * Converts s to Unicode and sets unistr to the result.
252 * For use with default implementations and subclasses,
253 * especially for implementations of snext() in terms of next().
254 * This is provided with a helper function instead of a default implementation
255 * of snext() to avoid potential infinite loops between next() and snext().
259 * const UnicodeString* snext(UErrorCode& status) {
260 * int32_t resultLength=0;
261 * const char *s=next(&resultLength, status);
262 * return setChars(s, resultLength, status);
266 * @param s String to be converted to Unicode.
267 * @param length Length of the string.
268 * @param status ICU in/out error code.
269 * @return A pointer to unistr.
272 UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);