strenum.h 10.0 KB

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