]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/unicode/bytestream.h
ICU-491.11.1.tar.gz
[apple/icu.git] / icuSources / common / unicode / bytestream.h
1 // Copyright (C) 2009-2012, 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 * Destructor.
154 * @stable ICU 4.2
155 */
156 virtual ~CheckedArrayByteSink();
157 /**
158 * Returns the sink to its original state, without modifying the buffer.
159 * Useful for reusing both the buffer and the sink for multiple streams.
160 * Resets the state to NumberOfBytesWritten()=NumberOfBytesAppended()=0
161 * and Overflowed()=FALSE.
162 * @return *this
163 * @stable ICU 4.6
164 */
165 virtual CheckedArrayByteSink& Reset();
166 /**
167 * Append "bytes[0,n-1]" to this.
168 * @param bytes the pointer to the bytes
169 * @param n the number of bytes; must be non-negative
170 * @stable ICU 4.2
171 */
172 virtual void Append(const char* bytes, int32_t n);
173 /**
174 * Returns a writable buffer for appending and writes the buffer's capacity to
175 * *result_capacity. For details see the base class documentation.
176 * @param min_capacity required minimum capacity of the returned buffer;
177 * must be non-negative
178 * @param desired_capacity_hint desired capacity of the returned buffer;
179 * must be non-negative
180 * @param scratch default caller-owned buffer
181 * @param scratch_capacity capacity of the scratch buffer
182 * @param result_capacity pointer to an integer which will be set to the
183 * capacity of the returned buffer
184 * @return a buffer with *result_capacity>=min_capacity
185 * @stable ICU 4.2
186 */
187 virtual char* GetAppendBuffer(int32_t min_capacity,
188 int32_t desired_capacity_hint,
189 char* scratch, int32_t scratch_capacity,
190 int32_t* result_capacity);
191 /**
192 * Returns the number of bytes actually written to the sink.
193 * @return number of bytes written to the buffer
194 * @stable ICU 4.2
195 */
196 int32_t NumberOfBytesWritten() const { return size_; }
197 /**
198 * Returns true if any bytes were discarded, i.e., if there was an
199 * attempt to write more than 'capacity' bytes.
200 * @return TRUE if more than 'capacity' bytes were Append()ed
201 * @stable ICU 4.2
202 */
203 UBool Overflowed() const { return overflowed_; }
204 /**
205 * Returns the number of bytes appended to the sink.
206 * If Overflowed() then NumberOfBytesAppended()>NumberOfBytesWritten()
207 * else they return the same number.
208 * @return number of bytes written to the buffer
209 * @stable ICU 4.6
210 */
211 int32_t NumberOfBytesAppended() const { return appended_; }
212 private:
213 char* outbuf_;
214 const int32_t capacity_;
215 int32_t size_;
216 int32_t appended_;
217 UBool overflowed_;
218 CheckedArrayByteSink(); ///< default constructor not implemented
219 CheckedArrayByteSink(const CheckedArrayByteSink &); ///< copy constructor not implemented
220 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &); ///< assignment operator not implemented
221 };
222
223 #if U_HAVE_STD_STRING
224
225 /**
226 * Implementation of ByteSink that writes to a "string".
227 * The StringClass is usually instantiated with a std::string.
228 * @stable ICU 4.2
229 */
230 template<typename StringClass>
231 class StringByteSink : public ByteSink {
232 public:
233 /**
234 * Constructs a ByteSink that will append bytes to the dest string.
235 * @param dest pointer to string object to append to
236 * @stable ICU 4.2
237 */
238 StringByteSink(StringClass* dest) : dest_(dest) { }
239 /**
240 * Append "bytes[0,n-1]" to this.
241 * @param data the pointer to the bytes
242 * @param n the number of bytes; must be non-negative
243 * @stable ICU 4.2
244 */
245 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
246 private:
247 StringClass* dest_;
248 StringByteSink(); ///< default constructor not implemented
249 StringByteSink(const StringByteSink &); ///< copy constructor not implemented
250 StringByteSink &operator=(const StringByteSink &); ///< assignment operator not implemented
251 };
252
253 #endif
254
255 U_NAMESPACE_END
256
257 #endif // __BYTESTREAM_H__