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