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