2 *******************************************************************************
4 * Copyright (C) 2002-2012, International Business Machines
5 * Corporation and others. All Rights Reserved.
7 *******************************************************************************
13 #include "unicode/uobject.h"
14 #include "unicode/unistr.h"
18 * \brief C++ API: String Enumeration
24 * Base class for 'pure' C++ implementations of uenum api. Adds a
25 * method that returns the next UnicodeString since in C++ this can
26 * be a common storage format for strings.
28 * <p>The model is that the enumeration is over strings maintained by
29 * a 'service.' At any point, the service might change, invalidating
30 * the enumerator (though this is expected to be rare). The iterator
31 * returns an error if this has occurred. Lack of the error is no
32 * guarantee that the service didn't change immediately after the
33 * call, so the returned string still might not be 'valid' on
36 * <p>Strings may take the form of const char*, const UChar*, or const
37 * UnicodeString*. The type you get is determine by the variant of
38 * 'next' that you call. In general the StringEnumeration is
39 * optimized for one of these types, but all StringEnumerations can
40 * return all types. Returned strings are each terminated with a NUL.
41 * Depending on the service data, they might also include embedded NUL
42 * characters, so API is provided to optionally return the true
43 * length, counting the embedded NULs but not counting the terminating
46 * <p>The pointers returned by next, unext, and snext become invalid
47 * upon any subsequent call to the enumeration's destructor, next,
48 * unext, snext, or reset.</p>
50 * ICU 2.8 adds some default implementations and helper functions
55 class U_COMMON_API StringEnumeration
: public UObject
{
61 virtual ~StringEnumeration();
64 * Clone this object, an instance of a subclass of StringEnumeration.
65 * Clones can be used concurrently in multiple threads.
66 * If a subclass does not implement clone(), or if an error occurs,
67 * then NULL is returned.
68 * The clone functions in all subclasses return a base class pointer
69 * because some compilers do not support covariant (same-as-this)
70 * return types; cast to the appropriate subclass if necessary.
71 * The caller must delete the clone.
73 * @return a clone of this object
75 * @see getDynamicClassID
78 virtual StringEnumeration
*clone() const;
81 * <p>Return the number of elements that the iterator traverses. If
82 * the iterator is out of sync with its service, status is set to
83 * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
85 * <p>The return value will not change except possibly as a result of
86 * a subsequent call to reset, or if the iterator becomes out of sync.</p>
88 * <p>This is a convenience function. It can end up being very
89 * expensive as all the items might have to be pre-fetched
90 * (depending on the storage format of the data being
93 * @param status the error code.
94 * @return number of elements in the iterator.
97 virtual int32_t count(UErrorCode
& status
) const = 0;
100 * <p>Returns the next element as a NUL-terminated char*. If there
101 * are no more elements, returns NULL. If the resultLength pointer
102 * is not NULL, the length of the string (not counting the
103 * terminating NUL) is returned at that address. If an error
104 * status is returned, the value at resultLength is undefined.</p>
106 * <p>The returned pointer is owned by this iterator and must not be
107 * deleted by the caller. The pointer is valid until the next call
108 * to next, unext, snext, reset, or the enumerator's destructor.</p>
110 * <p>If the iterator is out of sync with its service, status is set
111 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
113 * <p>If the native service string is a UChar* string, it is
114 * converted to char* with the invariant converter. If the
115 * conversion fails (because a character cannot be converted) then
116 * status is set to U_INVARIANT_CONVERSION_ERROR and the return
117 * value is undefined (though not NULL).</p>
119 * Starting with ICU 2.8, the default implementation calls snext()
120 * and handles the conversion.
121 * Either next() or snext() must be implemented differently by a subclass.
123 * @param status the error code.
124 * @param resultLength a pointer to receive the length, can be NULL.
125 * @return a pointer to the string, or NULL.
129 virtual const char* next(int32_t *resultLength
, UErrorCode
& status
);
132 * <p>Returns the next element as a NUL-terminated UChar*. If there
133 * are no more elements, returns NULL. If the resultLength pointer
134 * is not NULL, the length of the string (not counting the
135 * terminating NUL) is returned at that address. If an error
136 * status is returned, the value at resultLength is undefined.</p>
138 * <p>The returned pointer is owned by this iterator and must not be
139 * deleted by the caller. The pointer is valid until the next call
140 * to next, unext, snext, reset, or the enumerator's destructor.</p>
142 * <p>If the iterator is out of sync with its service, status is set
143 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
145 * Starting with ICU 2.8, the default implementation calls snext()
146 * and handles the conversion.
148 * @param status the error code.
149 * @param resultLength a ponter to receive the length, can be NULL.
150 * @return a pointer to the string, or NULL.
154 virtual const UChar
* unext(int32_t *resultLength
, UErrorCode
& status
);
157 * <p>Returns the next element a UnicodeString*. If there are no
158 * more elements, returns NULL.</p>
160 * <p>The returned pointer is owned by this iterator and must not be
161 * deleted by the caller. The pointer is valid until the next call
162 * to next, unext, snext, reset, or the enumerator's destructor.</p>
164 * <p>If the iterator is out of sync with its service, status is set
165 * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
167 * Starting with ICU 2.8, the default implementation calls next()
168 * and handles the conversion.
169 * Either next() or snext() must be implemented differently by a subclass.
171 * @param status the error code.
172 * @return a pointer to the string, or NULL.
176 virtual const UnicodeString
* snext(UErrorCode
& status
);
179 * <p>Resets the iterator. This re-establishes sync with the
180 * service and rewinds the iterator to start at the first
183 * <p>Previous pointers returned by next, unext, or snext become
184 * invalid, and the value returned by count might change.</p>
186 * @param status the error code.
190 virtual void reset(UErrorCode
& status
) = 0;
193 * Compares this enumeration to other to check if both are equal
195 * @param that The other string enumeration to compare this object to
196 * @return TRUE if the enumerations are equal. FALSE if not.
199 virtual UBool
operator==(const StringEnumeration
& that
)const;
201 * Compares this enumeration to other to check if both are not equal
203 * @param that The other string enumeration to compare this object to
204 * @return TRUE if the enumerations are equal. FALSE if not.
207 virtual UBool
operator!=(const StringEnumeration
& that
)const;
211 * UnicodeString field for use with default implementations and subclasses.
214 UnicodeString unistr
;
216 * char * default buffer for use with default implementations and subclasses.
219 char charsBuffer
[32];
221 * char * buffer for use with default implementations and subclasses.
222 * Allocated in constructor and in ensureCharsCapacity().
227 * Capacity of chars, for use with default implementations and subclasses.
230 int32_t charsCapacity
;
233 * Default constructor for use with default implementations and subclasses.
239 * Ensures that chars is at least as large as the requested capacity.
240 * For use with default implementations and subclasses.
242 * @param capacity Requested capacity.
243 * @param status ICU in/out error code.
246 void ensureCharsCapacity(int32_t capacity
, UErrorCode
&status
);
249 * Converts s to Unicode and sets unistr to the result.
250 * For use with default implementations and subclasses,
251 * especially for implementations of snext() in terms of next().
252 * This is provided with a helper function instead of a default implementation
253 * of snext() to avoid potential infinite loops between next() and snext().
257 * const UnicodeString* snext(UErrorCode& status) {
258 * int32_t resultLength=0;
259 * const char *s=next(&resultLength, status);
260 * return setChars(s, resultLength, status);
264 * @param s String to be converted to Unicode.
265 * @param length Length of the string.
266 * @param status ICU in/out error code.
267 * @return A pointer to unistr.
270 UnicodeString
*setChars(const char *s
, int32_t length
, UErrorCode
&status
);