strenum.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. *******************************************************************************
  3. *
  4. * Copyright (C) 2002-2007, International Business Machines
  5. * Corporation and others. All Rights Reserved.
  6. *
  7. *******************************************************************************
  8. */
  9. #ifndef STRENUM_H
  10. #define STRENUM_H
  11. #include "unicode/uobject.h"
  12. #include "unicode/unistr.h"
  13. /**
  14. * \file
  15. * \brief C++ API: String Enumeration
  16. */
  17. U_NAMESPACE_BEGIN
  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. * Clone this object, an instance of a subclass of StringEnumeration.
  59. * Clones can be used concurrently in multiple threads.
  60. * If a subclass does not implement clone(), or if an error occurs,
  61. * then NULL is returned.
  62. * The clone functions in all subclasses return a base class pointer
  63. * because some compilers do not support covariant (same-as-this)
  64. * return types; cast to the appropriate subclass if necessary.
  65. * The caller must delete the clone.
  66. *
  67. * @return a clone of this object
  68. *
  69. * @see getDynamicClassID
  70. * @stable ICU 2.8
  71. */
  72. virtual StringEnumeration *clone() const;
  73. /**
  74. * <p>Return the number of elements that the iterator traverses. If
  75. * the iterator is out of sync with its service, status is set to
  76. * U_ENUM_OUT_OF_SYNC_ERROR, and the return value is zero.</p>
  77. *
  78. * <p>The return value will not change except possibly as a result of
  79. * a subsequent call to reset, or if the iterator becomes out of sync.</p>
  80. *
  81. * <p>This is a convenience function. It can end up being very
  82. * expensive as all the items might have to be pre-fetched
  83. * (depending on the storage format of the data being
  84. * traversed).</p>
  85. *
  86. * @param status the error code.
  87. * @return number of elements in the iterator.
  88. *
  89. * @stable ICU 2.4 */
  90. virtual int32_t count(UErrorCode& status) const = 0;
  91. /**
  92. * <p>Returns the next element as a NUL-terminated char*. If there
  93. * are no more elements, returns NULL. If the resultLength pointer
  94. * is not NULL, the length of the string (not counting the
  95. * terminating NUL) is returned at that address. If an error
  96. * status is returned, the value at resultLength is undefined.</p>
  97. *
  98. * <p>The returned pointer is owned by this iterator and must not be
  99. * deleted by the caller. The pointer is valid until the next call
  100. * to next, unext, snext, reset, or the enumerator's destructor.</p>
  101. *
  102. * <p>If the iterator is out of sync with its service, status is set
  103. * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
  104. *
  105. * <p>If the native service string is a UChar* string, it is
  106. * converted to char* with the invariant converter. If the
  107. * conversion fails (because a character cannot be converted) then
  108. * status is set to U_INVARIANT_CONVERSION_ERROR and the return
  109. * value is undefined (though not NULL).</p>
  110. *
  111. * Starting with ICU 2.8, the default implementation calls snext()
  112. * and handles the conversion.
  113. *
  114. * @param status the error code.
  115. * @param resultLength a pointer to receive the length, can be NULL.
  116. * @return a pointer to the string, or NULL.
  117. *
  118. * @stable ICU 2.4
  119. */
  120. virtual const char* next(int32_t *resultLength, UErrorCode& status);
  121. /**
  122. * <p>Returns the next element as a NUL-terminated UChar*. If there
  123. * are no more elements, returns NULL. If the resultLength pointer
  124. * is not NULL, the length of the string (not counting the
  125. * terminating NUL) is returned at that address. If an error
  126. * status is returned, the value at resultLength is undefined.</p>
  127. *
  128. * <p>The returned pointer is owned by this iterator and must not be
  129. * deleted by the caller. The pointer is valid until the next call
  130. * to next, unext, snext, reset, or the enumerator's destructor.</p>
  131. *
  132. * <p>If the iterator is out of sync with its service, status is set
  133. * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
  134. *
  135. * Starting with ICU 2.8, the default implementation calls snext()
  136. * and handles the conversion.
  137. *
  138. * @param status the error code.
  139. * @param resultLength a ponter to receive the length, can be NULL.
  140. * @return a pointer to the string, or NULL.
  141. *
  142. * @stable ICU 2.4
  143. */
  144. virtual const UChar* unext(int32_t *resultLength, UErrorCode& status);
  145. /**
  146. * <p>Returns the next element a UnicodeString*. If there are no
  147. * more elements, returns NULL.</p>
  148. *
  149. * <p>The returned pointer is owned by this iterator and must not be
  150. * deleted by the caller. The pointer is valid until the next call
  151. * to next, unext, snext, reset, or the enumerator's destructor.</p>
  152. *
  153. * <p>If the iterator is out of sync with its service, status is set
  154. * to U_ENUM_OUT_OF_SYNC_ERROR and NULL is returned.</p>
  155. *
  156. * @param status the error code.
  157. * @return a pointer to the string, or NULL.
  158. *
  159. * @stable ICU 2.4
  160. */
  161. virtual const UnicodeString* snext(UErrorCode& status) = 0;
  162. /**
  163. * <p>Resets the iterator. This re-establishes sync with the
  164. * service and rewinds the iterator to start at the first
  165. * element.</p>
  166. *
  167. * <p>Previous pointers returned by next, unext, or snext become
  168. * invalid, and the value returned by count might change.</p>
  169. *
  170. * @param status the error code.
  171. *
  172. * @stable ICU 2.4
  173. */
  174. virtual void reset(UErrorCode& status) = 0;
  175. /**
  176. * Compares this enumeration to other to check if both are equal
  177. *
  178. * @param that The other string enumeration to compare this object to
  179. * @return TRUE if the enumerations are equal. FALSE if not.
  180. * @stable ICU 3.6
  181. */
  182. virtual UBool operator==(const StringEnumeration& that)const;
  183. /**
  184. * Compares this enumeration to other to check if both are not equal
  185. *
  186. * @param that The other string enumeration to compare this object to
  187. * @return TRUE if the enumerations are equal. FALSE if not.
  188. * @stable ICU 3.6
  189. */
  190. virtual UBool operator!=(const StringEnumeration& that)const;
  191. protected:
  192. /**
  193. * UnicodeString field for use with default implementations and subclasses.
  194. * @stable ICU 2.8
  195. */
  196. UnicodeString unistr;
  197. /**
  198. * char * default buffer for use with default implementations and subclasses.
  199. * @stable ICU 2.8
  200. */
  201. char charsBuffer[32];
  202. /**
  203. * char * buffer for use with default implementations and subclasses.
  204. * Allocated in constructor and in ensureCharsCapacity().
  205. * @stable ICU 2.8
  206. */
  207. char *chars;
  208. /**
  209. * Capacity of chars, for use with default implementations and subclasses.
  210. * @stable ICU 2.8
  211. */
  212. int32_t charsCapacity;
  213. /**
  214. * Default constructor for use with default implementations and subclasses.
  215. * @stable ICU 2.8
  216. */
  217. StringEnumeration();
  218. /**
  219. * Ensures that chars is at least as large as the requested capacity.
  220. * For use with default implementations and subclasses.
  221. *
  222. * @param capacity Requested capacity.
  223. * @param status ICU in/out error code.
  224. * @stable ICU 2.8
  225. */
  226. void ensureCharsCapacity(int32_t capacity, UErrorCode &status);
  227. /**
  228. * Converts s to Unicode and sets unistr to the result.
  229. * For use with default implementations and subclasses,
  230. * especially for implementations of snext() in terms of next().
  231. * This is provided with a helper function instead of a default implementation
  232. * of snext() to avoid potential infinite loops between next() and snext().
  233. *
  234. * For example:
  235. * \code
  236. * const UnicodeString* snext(UErrorCode& status) {
  237. * int32_t resultLength=0;
  238. * const char *s=next(&resultLength, status);
  239. * return setChars(s, resultLength, status);
  240. * }
  241. * \endcode
  242. *
  243. * @param s String to be converted to Unicode.
  244. * @param length Length of the string.
  245. * @param status ICU in/out error code.
  246. * @return A pointer to unistr.
  247. * @stable ICU 2.8
  248. */
  249. UnicodeString *setChars(const char *s, int32_t length, UErrorCode &status);
  250. };
  251. U_NAMESPACE_END
  252. /* STRENUM_H */
  253. #endif