bytestream.h 9.1 KB

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