bytestream.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. // Copyright (C) 2016 and later: Unicode, Inc. and others.
  2. // License & terms of use: http://www.unicode.org/copyright.html
  3. // Copyright (C) 2009-2012, International Business Machines
  4. // Corporation and others. All Rights Reserved.
  5. //
  6. // Copyright 2007 Google Inc. All Rights Reserved.
  7. // Author: sanjay@google.com (Sanjay Ghemawat)
  8. //
  9. // Abstract interface that consumes a sequence of bytes (ByteSink).
  10. //
  11. // Used so that we can write a single piece of code that can operate
  12. // on a variety of output string types.
  13. //
  14. // Various implementations of this interface are provided:
  15. // ByteSink:
  16. // CheckedArrayByteSink Write to a flat array, with bounds checking
  17. // StringByteSink Write to an STL string
  18. // This code is a contribution of Google code, and the style used here is
  19. // a compromise between the original Google code and the ICU coding guidelines.
  20. // For example, data types are ICU-ified (size_t,int->int32_t),
  21. // and API comments doxygen-ified, but function names and behavior are
  22. // as in the original, if possible.
  23. // Assertion-style error handling, not available in ICU, was changed to
  24. // parameter "pinning" similar to UnicodeString.
  25. //
  26. // In addition, this is only a partial port of the original Google code,
  27. // limited to what was needed so far. The (nearly) complete original code
  28. // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
  29. // (see ICU ticket 6765, r25517).
  30. #ifndef __BYTESTREAM_H__
  31. #define __BYTESTREAM_H__
  32. /**
  33. * \file
  34. * \brief C++ API: Interface for writing bytes, and implementation classes.
  35. */
  36. #include "unicode/utypes.h"
  37. #include "unicode/uobject.h"
  38. #include "unicode/std_string.h"
  39. U_NAMESPACE_BEGIN
  40. /**
  41. * A ByteSink can be filled with bytes.
  42. * @stable ICU 4.2
  43. */
  44. class U_COMMON_API ByteSink : public UMemory {
  45. public:
  46. /**
  47. * Default constructor.
  48. * @stable ICU 4.2
  49. */
  50. ByteSink() { }
  51. /**
  52. * Virtual destructor.
  53. * @stable ICU 4.2
  54. */
  55. virtual ~ByteSink();
  56. /**
  57. * Append "bytes[0,n-1]" to this.
  58. * @param bytes the pointer to the bytes
  59. * @param n the number of bytes; must be non-negative
  60. * @stable ICU 4.2
  61. */
  62. virtual void Append(const char* bytes, int32_t n) = 0;
  63. /**
  64. * Returns a writable buffer for appending and writes the buffer's capacity to
  65. * *result_capacity. Guarantees *result_capacity>=min_capacity.
  66. * May return a pointer to the caller-owned scratch buffer which must have
  67. * scratch_capacity>=min_capacity.
  68. * The returned buffer is only valid until the next operation
  69. * on this ByteSink.
  70. *
  71. * After writing at most *result_capacity bytes, call Append() with the
  72. * pointer returned from this function and the number of bytes written.
  73. * Many Append() implementations will avoid copying bytes if this function
  74. * returned an internal buffer.
  75. *
  76. * Partial usage example:
  77. * int32_t capacity;
  78. * char* buffer = sink->GetAppendBuffer(..., &capacity);
  79. * ... Write n bytes into buffer, with n <= capacity.
  80. * sink->Append(buffer, n);
  81. * In many implementations, that call to Append will avoid copying bytes.
  82. *
  83. * If the ByteSink allocates or reallocates an internal buffer, it should use
  84. * the desired_capacity_hint if appropriate.
  85. * If a caller cannot provide a reasonable guess at the desired capacity,
  86. * it should pass desired_capacity_hint=0.
  87. *
  88. * If a non-scratch buffer is returned, the caller may only pass
  89. * a prefix to it to Append().
  90. * That is, it is not correct to pass an interior pointer to Append().
  91. *
  92. * The default implementation always returns the scratch buffer.
  93. *
  94. * @param min_capacity required minimum capacity of the returned buffer;
  95. * must be non-negative
  96. * @param desired_capacity_hint desired capacity of the returned buffer;
  97. * must be non-negative
  98. * @param scratch default caller-owned buffer
  99. * @param scratch_capacity capacity of the scratch buffer
  100. * @param result_capacity pointer to an integer which will be set to the
  101. * capacity of the returned buffer
  102. * @return a buffer with *result_capacity>=min_capacity
  103. * @stable ICU 4.2
  104. */
  105. virtual char* GetAppendBuffer(int32_t min_capacity,
  106. int32_t desired_capacity_hint,
  107. char* scratch, int32_t scratch_capacity,
  108. int32_t* result_capacity);
  109. /**
  110. * Flush internal buffers.
  111. * Some byte sinks use internal buffers or provide buffering
  112. * and require calling Flush() at the end of the stream.
  113. * The ByteSink should be ready for further Append() calls after Flush().
  114. * The default implementation of Flush() does nothing.
  115. * @stable ICU 4.2
  116. */
  117. virtual void Flush();
  118. private:
  119. ByteSink(const ByteSink &); // copy constructor not implemented
  120. ByteSink &operator=(const ByteSink &); // assignment operator not implemented
  121. };
  122. // -------------------------------------------------------------
  123. // Some standard implementations
  124. /**
  125. * Implementation of ByteSink that writes to a flat byte array,
  126. * with bounds-checking:
  127. * This sink will not write more than capacity bytes to outbuf.
  128. * If more than capacity bytes are Append()ed, then excess bytes are ignored,
  129. * and Overflowed() will return true.
  130. * Overflow does not cause a runtime error.
  131. * @stable ICU 4.2
  132. */
  133. class U_COMMON_API CheckedArrayByteSink : public ByteSink {
  134. public:
  135. /**
  136. * Constructs a ByteSink that will write to outbuf[0..capacity-1].
  137. * @param outbuf buffer to write to
  138. * @param capacity size of the buffer
  139. * @stable ICU 4.2
  140. */
  141. CheckedArrayByteSink(char* outbuf, int32_t capacity);
  142. /**
  143. * Destructor.
  144. * @stable ICU 4.2
  145. */
  146. virtual ~CheckedArrayByteSink();
  147. /**
  148. * Returns the sink to its original state, without modifying the buffer.
  149. * Useful for reusing both the buffer and the sink for multiple streams.
  150. * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
  151. * and Overflowed()=FALSE.
  152. * @return *this
  153. * @stable ICU 4.6
  154. */
  155. virtual CheckedArrayByteSink& Reset();
  156. /**
  157. * Append "bytes[0,n-1]" to this.
  158. * @param bytes the pointer to the bytes
  159. * @param n the number of bytes; must be non-negative
  160. * @stable ICU 4.2
  161. */
  162. virtual void Append(const char* bytes, int32_t n);
  163. /**
  164. * Returns a writable buffer for appending and writes the buffer's capacity to
  165. * *result_capacity. For details see the base class documentation.
  166. * @param min_capacity required minimum capacity of the returned buffer;
  167. * must be non-negative
  168. * @param desired_capacity_hint desired capacity of the returned buffer;
  169. * must be non-negative
  170. * @param scratch default caller-owned buffer
  171. * @param scratch_capacity capacity of the scratch buffer
  172. * @param result_capacity pointer to an integer which will be set to the
  173. * capacity of the returned buffer
  174. * @return a buffer with *result_capacity>=min_capacity
  175. * @stable ICU 4.2
  176. */
  177. virtual char* GetAppendBuffer(int32_t min_capacity,
  178. int32_t desired_capacity_hint,
  179. char* scratch, int32_t scratch_capacity,
  180. int32_t* result_capacity);
  181. /**
  182. * Returns the number of bytes actually written to the sink.
  183. * @return number of bytes written to the buffer
  184. * @stable ICU 4.2
  185. */
  186. int32_t NumberOfBytesWritten() const { return size_; }
  187. /**
  188. * Returns true if any bytes were discarded, i.e., if there was an
  189. * attempt to write more than 'capacity' bytes.
  190. * @return TRUE if more than 'capacity' bytes were Append()ed
  191. * @stable ICU 4.2
  192. */
  193. UBool Overflowed() const { return overflowed_; }
  194. /**
  195. * Returns the number of bytes appended to the sink.
  196. * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
  197. * else they return the same number.
  198. * @return number of bytes written to the buffer
  199. * @stable ICU 4.6
  200. */
  201. int32_t NumberOfBytesAppended() const { return appended_; }
  202. private:
  203. char* outbuf_;
  204. const int32_t capacity_;
  205. int32_t size_;
  206. int32_t appended_;
  207. UBool overflowed_;
  208. CheckedArrayByteSink(); ///< default constructor not implemented
  209. CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
  210. CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented
  211. };
  212. #if U_HAVE_STD_STRING
  213. /**
  214. * Implementation of ByteSink that writes to a "string".
  215. * The StringClass is usually instantiated with a std::string.
  216. * @stable ICU 4.2
  217. */
  218. template<typename StringClass>
  219. class StringByteSink : public ByteSink {
  220. public:
  221. /**
  222. * Constructs a ByteSink that will append bytes to the dest string.
  223. * @param dest pointer to string object to append to
  224. * @stable ICU 4.2
  225. */
  226. StringByteSink(StringClass* dest) : dest_(dest) { }
  227. /**
  228. * Append "bytes[0,n-1]" to this.
  229. * @param data the pointer to the bytes
  230. * @param n the number of bytes; must be non-negative
  231. * @stable ICU 4.2
  232. */
  233. virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
  234. private:
  235. StringClass* dest_;
  236. StringByteSink(); ///< default constructor not implemented
  237. StringByteSink(const StringByteSink &); ///< copy constructor not implemented
  238. StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented
  239. };
  240. #endif
  241. U_NAMESPACE_END
  242. #endif // __BYTESTREAM_H__