ICU 58.2 58.2

- Main Page
- Related Pages
- Modules
- Namespaces
- Data Structures
- Files

- File List
- Globals

bytestream.h
Go to the documentation of this file.
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
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
40 #include "unicode/utypes.h"
41 #include "unicode/uobject.h"
42 #include "unicode/std_string.h"
43
44 U_NAMESPACE_BEGIN
45
50 class U_COMMON_API ByteSink : public UMemory {
51 public:
56 ByteSink() { }
61 virtual ~ByteSink();
62
69 virtual void Append(const char* bytes, int32_t n) = 0;
70
113 virtual char* GetAppendBuffer(int32_t min_capacity,
114 int32_t desired_capacity_hint,
115 char* scratch, int32_t scratch_capacity,
116 int32_t* result_capacity);
117
126 virtual void Flush();
127
128 private:
129 ByteSink(const ByteSink &); // copy constructor not implemented
130 ByteSink &operator=(const ByteSink &); // assignment operator not implemented
131 };
132
133 // -------------------------------------------------------------
134 // Some standard implementations
135
145 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
146 public:
153 CheckedArrayByteSink(char* outbuf, int32_t capacity);
158 virtual ~CheckedArrayByteSink();
167 virtual CheckedArrayByteSink& Reset();
174 virtual void Append(const char* bytes, int32_t n);
189 virtual char* GetAppendBuffer(int32_t min_capacity,
190 int32_t desired_capacity_hint,
191 char* scratch, int32_t scratch_capacity,
192 int32_t* result_capacity);
198 int32_t NumberOfBytesWritten() const { return size_; }
205 UBool Overflowed() const { return overflowed_; }
213 int32_t NumberOfBytesAppended() const { return appended_; }
214 private:
215 char* outbuf_;
216 const int32_t capacity_;
217 int32_t size_;
218 int32_t appended_;
219 UBool overflowed_;
220 CheckedArrayByteSink();
221 CheckedArrayByteSink(const CheckedArrayByteSink &);
222 CheckedArrayByteSink &operator=(const CheckedArrayByteSink &);
223 };
224
225 #if U_HAVE_STD_STRING
226
232 template<typename StringClass>
233 class StringByteSink : public ByteSink {
234 public:
240 StringByteSink(StringClass* dest) : dest_(dest) { }
247 virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
248 private:
249 StringClass* dest_;
250 StringByteSink();
251 StringByteSink(const StringByteSink &);
252 StringByteSink &operator=(const StringByteSink &);
253 };
254
255 #endif
256
257 U_NAMESPACE_END
258
259 #endif // __BYTESTREAM_H__
icu::ByteSink::GetAppendBuffer
virtual char * GetAppendBuffer(int32_t min_capacity, int32_t desired_capacity_hint, char *scratch, int32_t scratch_capacity, int32_t *result_capacity)
Returns a writable buffer for appending and writes the buffer&#39;s capacity to *result_capacity.
icu::StringByteSink::StringByteSink
StringByteSink(StringClass *dest)
Constructs a ByteSink that will append bytes to the dest string.
Definition: bytestream.h:240
icu::CheckedArrayByteSink::NumberOfBytesWritten
int32_t NumberOfBytesWritten() const
Returns the number of bytes actually written to the sink.
Definition: bytestream.h:198
icu::StringByteSink::Append
virtual void Append(const char *data, int32_t n)
Append "bytes[0,n-1]" to this.
Definition: bytestream.h:247
icu::CheckedArrayByteSink::Overflowed
UBool Overflowed() const
Returns true if any bytes were discarded, i.e., if there was an attempt to write more than &#39;capacity&#39;...
Definition: bytestream.h:205
icu::ByteSink::ByteSink
ByteSink()
Default constructor.
Definition: bytestream.h:56
icu::ByteSink
A ByteSink can be filled with bytes.
Definition: bytestream.h:50
icu::ByteSink::Append
virtual void Append(const char *bytes, int32_t n)=0
Append "bytes[0,n-1]" to this.
icu::CheckedArrayByteSink
Implementation of ByteSink that writes to a flat byte array, with bounds-checking: This sink will not...
Definition: bytestream.h:145
U_NAMESPACE_BEGIN
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:131
std_string.h
C++ API: Central ICU header for including the C++ standard <string> header and for related definition...
uobject.h
C++ API: Common ICU base class UObject.
U_NAMESPACE_END
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API.
Definition: uversion.h:132
icu::CheckedArrayByteSink::NumberOfBytesAppended
int32_t NumberOfBytesAppended() const
Returns the number of bytes appended to the sink.
Definition: bytestream.h:213
utypes.h
Basic definitions for ICU, for both C and C++ APIs.
icu::StringByteSink
Implementation of ByteSink that writes to a "string".
Definition: bytestream.h:233
U_COMMON_API
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:359
icu::UMemory
UMemory is the common ICU base class.
Definition: uobject.h:112
UBool
int8_t UBool
The ICU boolean type.
Definition: umachine.h:259