2 *******************************************************************************
4 * Copyright (C) 2002-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
13 #include "unicode/uobject.h"
14 #include "unicode/unistr.h"
19 * Base class for 'pure' C++ implementations of uenum api. Adds a
20 * method that returns the next UnicodeString since in C++ this can
21 * be a common storage format for strings.
23 * <p>The model is that the enumeration is over strings maintained by
24 * a 'service.' At any point, the service might change, invalidating
25 * the enumerator (though this is expected to be rare). The iterator
26 * returns an error if this has occurred. Lack of the error is no
27 * guarantee that the service didn't change immediately after the
28 * call, so the returned string still might not be 'valid' on
31 * <p>Strings may take the form of const char*, const UChar*, or const
32 * UnicodeString*. The type you get is determine by the variant of
33 * 'next' that you call. In general the StringEnumeration is
34 * optimized for one of these types, but all StringEnumerations can
35 * return all types. Returned strings are each terminated with a NUL.
36 * Depending on the service data, they might also include embedded NUL
37 * characters, so API is provided to optionally return the true
38 * length, counting the embedded NULs but not counting the terminating
41 * <p>The pointers returned by next, unext, and snext become invalid
42 * upon any subsequent call to the enumeration's destructor, next,
43 * unext, snext, or reset.</p>
45 * ICU 2.8 adds some default implementations and helper functions
50 class U_COMMON_API StringEnumeration
: public UObject
{
56 virtual ~StringEnumeration();
59 * Clone this object, an instance of a subclass of StringEnumeration.
60 * Clones can be used concurrently in multiple threads.
61 * If a subclass does not implement clone(), or if an error occurs,
62 * then NULL is returned.
63 * The clone functions in all subclasses return a base class pointer
64 * because some compilers do not support covariant (same-as-this)
65 * return types; cast to the appropriate subclass if necessary.
66 * The caller must delete the clone.
68 * @return a clone of this object
70 * @see getDynamicClassID
73 virtual StringEnumeration
*clone() const;
76 * <p>Return the number of elements that the iterator traverses. If
77 * the iterator is out of sync with its service, status is set to
78 * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
80 * <p>The return value will not change except possibly as a result of
81 * a subsequent call to reset, or if the iterator becomes out of sync.</p>
83 * <p>This is a convenience function. It can end up being very
84 * expensive as all the items might have to be pre-fetched
85 * (depending on the storage format of the data being
88 * @param status the error code.
89 * @return number of elements in the iterator.
92 virtual int32_t count(UErrorCode
& status
) const = 0;
95 * <p>Returns the next element as a NUL-terminated char*. If there
96 * are no more elements, returns NULL. If the resultLength pointer
97 * is not NULL, the length of the string (not counting the
98 * terminating NUL) is returned at that address. If an error
99 * status is returned, the value at resultLength is undefined.</p>
101 * <p>The returned pointer is owned by this iterator and must not be
102 * deleted by the caller. The pointer is valid until the next call
103 * to next, unext, snext, reset, or the enumerator's destructor.</p>
105 * <p>If the iterator is out of sync with its service, status is set
106 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
108 * <p>If the native service string is a UChar* string, it is
109 * converted to char* with the invariant converter. If the
110 * conversion fails (because a character cannot be converted) then
111 * status is set to U_INVARIANT_CONVERSION_ERROR and the return
112 * value is undefined (though not NULL).</p>
114 * Starting with ICU 2.8, the default implementation calls snext()
115 * and handles the conversion.
117 * @param status the error code.
118 * @param resultLength a pointer to receive the length, can be NULL.
119 * @return a pointer to the string, or NULL.
123 virtual const char* next(int32_t *resultLength
, UErrorCode
& status
);
126 * <p>Returns the next element as a NUL-terminated UChar*. If there
127 * are no more elements, returns NULL. If the resultLength pointer
128 * is not NULL, the length of the string (not counting the
129 * terminating NUL) is returned at that address. If an error
130 * status is returned, the value at resultLength is undefined.</p>
132 * <p>The returned pointer is owned by this iterator and must not be
133 * deleted by the caller. The pointer is valid until the next call
134 * to next, unext, snext, reset, or the enumerator's destructor.</p>
136 * <p>If the iterator is out of sync with its service, status is set
137 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
139 * Starting with ICU 2.8, the default implementation calls snext()
140 * and handles the conversion.
142 * @param status the error code.
143 * @param resultLength a ponter to receive the length, can be NULL.
144 * @return a pointer to the string, or NULL.
148 virtual const UChar
* unext(int32_t *resultLength
, UErrorCode
& status
);
151 * <p>Returns the next element a UnicodeString*. If there are no
152 * more elements, returns NULL.</p>
154 * <p>The returned pointer is owned by this iterator and must not be
155 * deleted by the caller. The pointer is valid until the next call
156 * to next, unext, snext, reset, or the enumerator's destructor.</p>
158 * <p>If the iterator is out of sync with its service, status is set
159 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
161 * @param status the error code.
162 * @return a pointer to the string, or NULL.
166 virtual const UnicodeString
* snext(UErrorCode
& status
) = 0;
169 * <p>Resets the iterator. This re-establishes sync with the
170 * service and rewinds the iterator to start at the first
173 * <p>Previous pointers returned by next, unext, or snext become
174 * invalid, and the value returned by count might change.</p>
176 * @param status the error code.
180 virtual void reset(UErrorCode
& status
) = 0;
184 * UnicodeString field for use with default implementations and subclasses.
187 UnicodeString unistr
;
189 * char * default buffer for use with default implementations and subclasses.
192 char charsBuffer
[32];
194 * char * buffer for use with default implementations and subclasses.
195 * Allocated in constructor and in ensureCharsCapacity().
200 * Capacity of chars, for use with default implementations and subclasses.
203 int32_t charsCapacity
;
206 * Default constructor for use with default implementations and subclasses.
212 * Ensures that chars is at least as large as the requested capacity.
213 * For use with default implementations and subclasses.
215 * @param capacity Requested capacity.
216 * @param status ICU in/out error code.
219 void ensureCharsCapacity(int32_t capacity
, UErrorCode
&status
);
222 * Converts s to Unicode and sets unistr to the result.
223 * For use with default implementations and subclasses,
224 * especially for implementations of snext() in terms of next().
225 * This is provided with a helper function instead of a default implementation
226 * of snext() to avoid potential infinite loops between next() and snext().
230 * const UnicodeString* snext(UErrorCode& status) {
231 * int32_t resultLength=0;
232 * const char *s=next(&resultLength, status);
233 * return setChars(s, resultLength, status);
237 * @param s String to be converted to Unicode.
238 * @param length Length of the string.
239 * @param status ICU in/out error code.
240 * @return A pointer to unistr.
243 UnicodeString
*setChars(const char *s
, int32_t length
, UErrorCode
&status
);