]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/strenum.h
ICU-6.2.8.tar.gz
[apple/icu.git] / icuSources / common / unicode / strenum.h
1 /*
2 *******************************************************************************
3 *
4 * Copyright (C) 2002-2004, International Business Machines
5 * Corporation and others. All Rights Reserved.
6 *
7 *******************************************************************************
8 */
9
10 #ifndef STRENUM_H
11 #define STRENUM_H
12
13 #include "unicode/uobject.h"
14 #include "unicode/unistr.h"
15
16 U_NAMESPACE_BEGIN
17
18 /**
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.
22 *
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
29 * subsequent use.</p>
30 *
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
39 * NUL.</p>
40 *
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>
44 *
45 * ICU 2.8 adds some default implementations and helper functions
46 * for subclasses.
47 *
48 * @stable ICU 2.4
49 */
50 class U_COMMON_API StringEnumeration : public UObject {
51 public:
52 /**
53 * Destructor.
54 * @stable ICU 2.4
55 */
56 virtual ~StringEnumeration();
57
58 /**
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.
67 *
68 * @return a clone of this object
69 *
70 * @see getDynamicClassID
71 * @draft ICU 2.8
72 */
73 virtual StringEnumeration *clone() const;
74
75 /**
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>
79 *
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>
82 *
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
86 * traversed).</p>
87 *
88 * @param status the error code.
89 * @return number of elements in the iterator.
90 *
91 * @stable ICU 2.4 */
92 virtual int32_t count(UErrorCode& status) const = 0;
93
94 /**
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>
100 *
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>
104 *
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>
107 *
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>
113 *
114 * Starting with ICU 2.8, the default implementation calls snext()
115 * and handles the conversion.
116 *
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.
120 *
121 * @stable ICU 2.4
122 */
123 virtual const char* next(int32_t *resultLength, UErrorCode& status);
124
125 /**
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>
131 *
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>
135 *
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>
138 *
139 * Starting with ICU 2.8, the default implementation calls snext()
140 * and handles the conversion.
141 *
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.
145 *
146 * @stable ICU 2.4
147 */
148 virtual const UChar* unext(int32_t *resultLength, UErrorCode& status);
149
150 /**
151 * <p>Returns the next element a UnicodeString*. If there are no
152 * more elements, returns NULL.</p>
153 *
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>
157 *
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>
160 *
161 * @param status the error code.
162 * @return a pointer to the string, or NULL.
163 *
164 * @stable ICU 2.4
165 */
166 virtual const UnicodeString* snext(UErrorCode& status) = 0;
167
168 /**
169 * <p>Resets the iterator. This re-establishes sync with the
170 * service and rewinds the iterator to start at the first
171 * element.</p>
172 *
173 * <p>Previous pointers returned by next, unext, or snext become
174 * invalid, and the value returned by count might change.</p>
175 *
176 * @param status the error code.
177 *
178 * @stable ICU 2.4
179 */
180 virtual void reset(UErrorCode& status) = 0;
181
182 protected:
183 /**
184 * UnicodeString field for use with default implementations and subclasses.
185 * @draft ICU 2.8
186 */
187 UnicodeString unistr;
188 /**
189 * char * default buffer for use with default implementations and subclasses.
190 * @draft ICU 2.8
191 */
192 char charsBuffer[32];
193 /**
194 * char * buffer for use with default implementations and subclasses.
195 * Allocated in constructor and in ensureCharsCapacity().
196 * @draft ICU 2.8
197 */
198 char *chars;
199 /**
200 * Capacity of chars, for use with default implementations and subclasses.
201 * @draft ICU 2.8
202 */
203 int32_t charsCapacity;
204
205 /**
206 * Default constructor for use with default implementations and subclasses.
207 * @draft ICU 2.8
208 */
209 StringEnumeration();
210
211 /**
212 * Ensures that chars is at least as large as the requested capacity.
213 * For use with default implementations and subclasses.
214 *
215 * @param capacity Requested capacity.
216 * @param status ICU in/out error code.
217 * @draft ICU 2.8
218 */
219 void ensureCharsCapacity(int32_t capacity, UErrorCode &status);
220
221 /**
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().
227 *
228 * For example:
229 * \code
230 * const UnicodeString* snext(UErrorCode& status) {
231 * int32_t resultLength=0;
232 * const char *s=next(&resultLength, status);
233 * return setChars(s, resultLength, status);
234 * }
235 * \endcode
236 *
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.
241 * @draft ICU 2.8
242 */
243 UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);
244 };
245
246 U_NAMESPACE_END
247
248 /* STRENUM_H */
249 #endif