sl@0
|
1 |
/*
|
sl@0
|
2 |
*******************************************************************************
|
sl@0
|
3 |
*
|
sl@0
|
4 |
* Copyright (C) 2002-2005, International Business Machines
|
sl@0
|
5 |
* Corporation and others. All Rights Reserved.
|
sl@0
|
6 |
*
|
sl@0
|
7 |
*******************************************************************************
|
sl@0
|
8 |
*/
|
sl@0
|
9 |
|
sl@0
|
10 |
#ifndef STRENUM_H
|
sl@0
|
11 |
#define STRENUM_H
|
sl@0
|
12 |
|
sl@0
|
13 |
#include "unicode/uobject.h"
|
sl@0
|
14 |
#include "unicode/unistr.h"
|
sl@0
|
15 |
|
sl@0
|
16 |
/**
|
sl@0
|
17 |
* \file
|
sl@0
|
18 |
* \brief C++ API: String Enumeration
|
sl@0
|
19 |
*/
|
sl@0
|
20 |
|
sl@0
|
21 |
U_NAMESPACE_BEGIN
|
sl@0
|
22 |
|
sl@0
|
23 |
/**
|
sl@0
|
24 |
* Base class for 'pure' C++ implementations of uenum api. Adds a
|
sl@0
|
25 |
* method that returns the next UnicodeString since in C++ this can
|
sl@0
|
26 |
* be a common storage format for strings.
|
sl@0
|
27 |
*
|
sl@0
|
28 |
* <p>The model is that the enumeration is over strings maintained by
|
sl@0
|
29 |
* a 'service.' At any point, the service might change, invalidating
|
sl@0
|
30 |
* the enumerator (though this is expected to be rare). The iterator
|
sl@0
|
31 |
* returns an error if this has occurred. Lack of the error is no
|
sl@0
|
32 |
* guarantee that the service didn't change immediately after the
|
sl@0
|
33 |
* call, so the returned string still might not be 'valid' on
|
sl@0
|
34 |
* subsequent use.</p>
|
sl@0
|
35 |
*
|
sl@0
|
36 |
* <p>Strings may take the form of const char*, const UChar*, or const
|
sl@0
|
37 |
* UnicodeString*. The type you get is determine by the variant of
|
sl@0
|
38 |
* 'next' that you call. In general the StringEnumeration is
|
sl@0
|
39 |
* optimized for one of these types, but all StringEnumerations can
|
sl@0
|
40 |
* return all types. Returned strings are each terminated with a NUL.
|
sl@0
|
41 |
* Depending on the service data, they might also include embedded NUL
|
sl@0
|
42 |
* characters, so API is provided to optionally return the true
|
sl@0
|
43 |
* length, counting the embedded NULs but not counting the terminating
|
sl@0
|
44 |
* NUL.</p>
|
sl@0
|
45 |
*
|
sl@0
|
46 |
* <p>The pointers returned by next, unext, and snext become invalid
|
sl@0
|
47 |
* upon any subsequent call to the enumeration's destructor, next,
|
sl@0
|
48 |
* unext, snext, or reset.</p>
|
sl@0
|
49 |
*
|
sl@0
|
50 |
* ICU 2.8 adds some default implementations and helper functions
|
sl@0
|
51 |
* for subclasses.
|
sl@0
|
52 |
*
|
sl@0
|
53 |
* @stable ICU 2.4
|
sl@0
|
54 |
*/
|
sl@0
|
55 |
class U_COMMON_API StringEnumeration : public UObject {
|
sl@0
|
56 |
public:
|
sl@0
|
57 |
/**
|
sl@0
|
58 |
* Destructor.
|
sl@0
|
59 |
* @stable ICU 2.4
|
sl@0
|
60 |
*/
|
sl@0
|
61 |
virtual ~StringEnumeration();
|
sl@0
|
62 |
|
sl@0
|
63 |
/**
|
sl@0
|
64 |
* Clone this object, an instance of a subclass of StringEnumeration.
|
sl@0
|
65 |
* Clones can be used concurrently in multiple threads.
|
sl@0
|
66 |
* If a subclass does not implement clone(), or if an error occurs,
|
sl@0
|
67 |
* then NULL is returned.
|
sl@0
|
68 |
* The clone functions in all subclasses return a base class pointer
|
sl@0
|
69 |
* because some compilers do not support covariant (same-as-this)
|
sl@0
|
70 |
* return types; cast to the appropriate subclass if necessary.
|
sl@0
|
71 |
* The caller must delete the clone.
|
sl@0
|
72 |
*
|
sl@0
|
73 |
* @return a clone of this object
|
sl@0
|
74 |
*
|
sl@0
|
75 |
* @see getDynamicClassID
|
sl@0
|
76 |
* @stable ICU 2.8
|
sl@0
|
77 |
*/
|
sl@0
|
78 |
virtual StringEnumeration *clone() const;
|
sl@0
|
79 |
|
sl@0
|
80 |
/**
|
sl@0
|
81 |
* <p>Return the number of elements that the iterator traverses. If
|
sl@0
|
82 |
* the iterator is out of sync with its service, status is set to
|
sl@0
|
83 |
* U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
|
sl@0
|
84 |
*
|
sl@0
|
85 |
* <p>The return value will not change except possibly as a result of
|
sl@0
|
86 |
* a subsequent call to reset, or if the iterator becomes out of sync.</p>
|
sl@0
|
87 |
*
|
sl@0
|
88 |
* <p>This is a convenience function. It can end up being very
|
sl@0
|
89 |
* expensive as all the items might have to be pre-fetched
|
sl@0
|
90 |
* (depending on the storage format of the data being
|
sl@0
|
91 |
* traversed).</p>
|
sl@0
|
92 |
*
|
sl@0
|
93 |
* @param status the error code.
|
sl@0
|
94 |
* @return number of elements in the iterator.
|
sl@0
|
95 |
*
|
sl@0
|
96 |
* @stable ICU 2.4 */
|
sl@0
|
97 |
virtual int32_t count(UErrorCode& status) const = 0;
|
sl@0
|
98 |
|
sl@0
|
99 |
/**
|
sl@0
|
100 |
* <p>Returns the next element as a NUL-terminated char*. If there
|
sl@0
|
101 |
* are no more elements, returns NULL. If the resultLength pointer
|
sl@0
|
102 |
* is not NULL, the length of the string (not counting the
|
sl@0
|
103 |
* terminating NUL) is returned at that address. If an error
|
sl@0
|
104 |
* status is returned, the value at resultLength is undefined.</p>
|
sl@0
|
105 |
*
|
sl@0
|
106 |
* <p>The returned pointer is owned by this iterator and must not be
|
sl@0
|
107 |
* deleted by the caller. The pointer is valid until the next call
|
sl@0
|
108 |
* to next, unext, snext, reset, or the enumerator's destructor.</p>
|
sl@0
|
109 |
*
|
sl@0
|
110 |
* <p>If the iterator is out of sync with its service, status is set
|
sl@0
|
111 |
* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
|
sl@0
|
112 |
*
|
sl@0
|
113 |
* <p>If the native service string is a UChar* string, it is
|
sl@0
|
114 |
* converted to char* with the invariant converter. If the
|
sl@0
|
115 |
* conversion fails (because a character cannot be converted) then
|
sl@0
|
116 |
* status is set to U_INVARIANT_CONVERSION_ERROR and the return
|
sl@0
|
117 |
* value is undefined (though not NULL).</p>
|
sl@0
|
118 |
*
|
sl@0
|
119 |
* Starting with ICU 2.8, the default implementation calls snext()
|
sl@0
|
120 |
* and handles the conversion.
|
sl@0
|
121 |
*
|
sl@0
|
122 |
* @param status the error code.
|
sl@0
|
123 |
* @param resultLength a pointer to receive the length, can be NULL.
|
sl@0
|
124 |
* @return a pointer to the string, or NULL.
|
sl@0
|
125 |
*
|
sl@0
|
126 |
* @stable ICU 2.4
|
sl@0
|
127 |
*/
|
sl@0
|
128 |
virtual const char* next(int32_t *resultLength, UErrorCode& status);
|
sl@0
|
129 |
|
sl@0
|
130 |
/**
|
sl@0
|
131 |
* <p>Returns the next element as a NUL-terminated UChar*. If there
|
sl@0
|
132 |
* are no more elements, returns NULL. If the resultLength pointer
|
sl@0
|
133 |
* is not NULL, the length of the string (not counting the
|
sl@0
|
134 |
* terminating NUL) is returned at that address. If an error
|
sl@0
|
135 |
* status is returned, the value at resultLength is undefined.</p>
|
sl@0
|
136 |
*
|
sl@0
|
137 |
* <p>The returned pointer is owned by this iterator and must not be
|
sl@0
|
138 |
* deleted by the caller. The pointer is valid until the next call
|
sl@0
|
139 |
* to next, unext, snext, reset, or the enumerator's destructor.</p>
|
sl@0
|
140 |
*
|
sl@0
|
141 |
* <p>If the iterator is out of sync with its service, status is set
|
sl@0
|
142 |
* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
|
sl@0
|
143 |
*
|
sl@0
|
144 |
* Starting with ICU 2.8, the default implementation calls snext()
|
sl@0
|
145 |
* and handles the conversion.
|
sl@0
|
146 |
*
|
sl@0
|
147 |
* @param status the error code.
|
sl@0
|
148 |
* @param resultLength a ponter to receive the length, can be NULL.
|
sl@0
|
149 |
* @return a pointer to the string, or NULL.
|
sl@0
|
150 |
*
|
sl@0
|
151 |
* @stable ICU 2.4
|
sl@0
|
152 |
*/
|
sl@0
|
153 |
virtual const UChar* unext(int32_t *resultLength, UErrorCode& status);
|
sl@0
|
154 |
|
sl@0
|
155 |
/**
|
sl@0
|
156 |
* <p>Returns the next element a UnicodeString*. If there are no
|
sl@0
|
157 |
* more elements, returns NULL.</p>
|
sl@0
|
158 |
*
|
sl@0
|
159 |
* <p>The returned pointer is owned by this iterator and must not be
|
sl@0
|
160 |
* deleted by the caller. The pointer is valid until the next call
|
sl@0
|
161 |
* to next, unext, snext, reset, or the enumerator's destructor.</p>
|
sl@0
|
162 |
*
|
sl@0
|
163 |
* <p>If the iterator is out of sync with its service, status is set
|
sl@0
|
164 |
* to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
|
sl@0
|
165 |
*
|
sl@0
|
166 |
* @param status the error code.
|
sl@0
|
167 |
* @return a pointer to the string, or NULL.
|
sl@0
|
168 |
*
|
sl@0
|
169 |
* @stable ICU 2.4
|
sl@0
|
170 |
*/
|
sl@0
|
171 |
virtual const UnicodeString* snext(UErrorCode& status) = 0;
|
sl@0
|
172 |
|
sl@0
|
173 |
/**
|
sl@0
|
174 |
* <p>Resets the iterator. This re-establishes sync with the
|
sl@0
|
175 |
* service and rewinds the iterator to start at the first
|
sl@0
|
176 |
* element.</p>
|
sl@0
|
177 |
*
|
sl@0
|
178 |
* <p>Previous pointers returned by next, unext, or snext become
|
sl@0
|
179 |
* invalid, and the value returned by count might change.</p>
|
sl@0
|
180 |
*
|
sl@0
|
181 |
* @param status the error code.
|
sl@0
|
182 |
*
|
sl@0
|
183 |
* @stable ICU 2.4
|
sl@0
|
184 |
*/
|
sl@0
|
185 |
virtual void reset(UErrorCode& status) = 0;
|
sl@0
|
186 |
|
sl@0
|
187 |
protected:
|
sl@0
|
188 |
/**
|
sl@0
|
189 |
* UnicodeString field for use with default implementations and subclasses.
|
sl@0
|
190 |
* @stable ICU 2.8
|
sl@0
|
191 |
*/
|
sl@0
|
192 |
UnicodeString unistr;
|
sl@0
|
193 |
/**
|
sl@0
|
194 |
* char * default buffer for use with default implementations and subclasses.
|
sl@0
|
195 |
* @stable ICU 2.8
|
sl@0
|
196 |
*/
|
sl@0
|
197 |
char charsBuffer[32];
|
sl@0
|
198 |
/**
|
sl@0
|
199 |
* char * buffer for use with default implementations and subclasses.
|
sl@0
|
200 |
* Allocated in constructor and in ensureCharsCapacity().
|
sl@0
|
201 |
* @stable ICU 2.8
|
sl@0
|
202 |
*/
|
sl@0
|
203 |
char *chars;
|
sl@0
|
204 |
/**
|
sl@0
|
205 |
* Capacity of chars, for use with default implementations and subclasses.
|
sl@0
|
206 |
* @stable ICU 2.8
|
sl@0
|
207 |
*/
|
sl@0
|
208 |
int32_t charsCapacity;
|
sl@0
|
209 |
|
sl@0
|
210 |
/**
|
sl@0
|
211 |
* Default constructor for use with default implementations and subclasses.
|
sl@0
|
212 |
* @stable ICU 2.8
|
sl@0
|
213 |
*/
|
sl@0
|
214 |
StringEnumeration();
|
sl@0
|
215 |
|
sl@0
|
216 |
/**
|
sl@0
|
217 |
* Ensures that chars is at least as large as the requested capacity.
|
sl@0
|
218 |
* For use with default implementations and subclasses.
|
sl@0
|
219 |
*
|
sl@0
|
220 |
* @param capacity Requested capacity.
|
sl@0
|
221 |
* @param status ICU in/out error code.
|
sl@0
|
222 |
* @stable ICU 2.8
|
sl@0
|
223 |
*/
|
sl@0
|
224 |
void ensureCharsCapacity(int32_t capacity, UErrorCode &status);
|
sl@0
|
225 |
|
sl@0
|
226 |
/**
|
sl@0
|
227 |
* Converts s to Unicode and sets unistr to the result.
|
sl@0
|
228 |
* For use with default implementations and subclasses,
|
sl@0
|
229 |
* especially for implementations of snext() in terms of next().
|
sl@0
|
230 |
* This is provided with a helper function instead of a default implementation
|
sl@0
|
231 |
* of snext() to avoid potential infinite loops between next() and snext().
|
sl@0
|
232 |
*
|
sl@0
|
233 |
* For example:
|
sl@0
|
234 |
* \code
|
sl@0
|
235 |
* const UnicodeString* snext(UErrorCode& status) {
|
sl@0
|
236 |
* int32_t resultLength=0;
|
sl@0
|
237 |
* const char *s=next(&resultLength, status);
|
sl@0
|
238 |
* return setChars(s, resultLength, status);
|
sl@0
|
239 |
* }
|
sl@0
|
240 |
* \endcode
|
sl@0
|
241 |
*
|
sl@0
|
242 |
* @param s String to be converted to Unicode.
|
sl@0
|
243 |
* @param length Length of the string.
|
sl@0
|
244 |
* @param status ICU in/out error code.
|
sl@0
|
245 |
* @return A pointer to unistr.
|
sl@0
|
246 |
* @stable ICU 2.8
|
sl@0
|
247 |
*/
|
sl@0
|
248 |
UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);
|
sl@0
|
249 |
};
|
sl@0
|
250 |
|
sl@0
|
251 |
U_NAMESPACE_END
|
sl@0
|
252 |
|
sl@0
|
253 |
/* STRENUM_H */
|
sl@0
|
254 |
#endif
|