strenum.h 10 KB

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