ICU 4.2.1
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bytestream.h
Go to the documentation of this file.
1 // Copyright (C) 2009, 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 
38 #include "unicode/utypes.h"
39 #include "unicode/uobject.h"
40 #include "unicode/std_string.h"
41 
43 
48 class U_COMMON_API ByteSink : public UMemory {
49 public:
54  ByteSink() { }
59  virtual ~ByteSink() { }
60 
67  virtual void Append(const char* bytes, int32_t n) = 0;
68 
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 
123  virtual void Flush();
124 
125 private:
126  ByteSink(const ByteSink &); // copy constructor not implemented
127  ByteSink &operator=(const ByteSink &); // assignment operator not implemented
128 };
129 
130 // -------------------------------------------------------------
131 // Some standard implementations
132 
143 public:
150  CheckedArrayByteSink(char* outbuf, int32_t capacity);
157  virtual void Append(const char* bytes, int32_t n);
172  virtual char* GetAppendBuffer(int32_t min_capacity,
173  int32_t desired_capacity_hint,
174  char* scratch, int32_t scratch_capacity,
175  int32_t* result_capacity);
181  int32_t NumberOfBytesWritten() const { return size_; }
188  UBool Overflowed() const { return overflowed_; }
189 private:
190  char* outbuf_;
191  const int32_t capacity_;
192  int32_t size_;
193  bool overflowed_;
196  CheckedArrayByteSink &operator=(const CheckedArrayByteSink &);
197 };
198 
199 #if U_HAVE_STD_STRING
200 
206 template<typename StringClass>
207 class StringByteSink : public ByteSink {
208  public:
214  StringByteSink(StringClass* dest) : dest_(dest) { }
221  virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
222  private:
223  StringClass* dest_;
224  StringByteSink();
225  StringByteSink(const StringByteSink &);
226  StringByteSink &operator=(const StringByteSink &);
227 };
228 
229 #endif
230 
232 
233 #endif // __BYTESTREAM_H__
A ByteSink can be filled with bytes.
Definition: bytestream.h:48
int32_t NumberOfBytesWritten() const
Returns the number of bytes actually written to the sink.
Definition: bytestream.h:181
Implementation of ByteSink that writes to a flat byte array, with bounds-checking: This sink will not...
Definition: bytestream.h:142
virtual ~ByteSink()
Virtual destructor.
Definition: bytestream.h:59
ByteSink()
Default constructor.
Definition: bytestream.h:54
#define U_NAMESPACE_BEGIN
This is used to begin a declaration of a public ICU C++ API.
Definition: uversion.h:183
C++ API: Central ICU header for including the C++ standard &lt;string&gt; header and for related definition...
UMemory is the common ICU base class.
Definition: uobject.h:98
C++ API: Common ICU base class UObject.
#define U_NAMESPACE_END
This is used to end a declaration of a public ICU C++ API If the compiler doesn&#39;t support namespaces...
Definition: uversion.h:184
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.
virtual void Append(const char *bytes, int32_t n)=0
Append &quot;bytes[0,n-1]&quot; to this.
Basic definitions for ICU, for both C and C++ APIs.
#define U_COMMON_API
Set to export library symbols from inside the common library, and to import them from outside...
Definition: utypes.h:474
signed int int32_t
Define 64 bit limits.
Definition: pwin32.h:143
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:188
int8_t UBool
The ICU boolean type.
Definition: umachine.h:208