ICU 56.1  56.1
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
bytestream.h
Go to the documentation of this file.
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 
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 
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 
144 public:
151  CheckedArrayByteSink(char* outbuf, int32_t capacity);
156  virtual ~CheckedArrayByteSink();
165  virtual CheckedArrayByteSink& Reset();
172  virtual void Append(const char* bytes, int32_t n);
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);
196  int32_t NumberOfBytesWritten() const { return size_; }
203  UBool Overflowed() const { return overflowed_; }
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_;
220  CheckedArrayByteSink &operator=(const CheckedArrayByteSink &);
221 };
222 
223 #if U_HAVE_STD_STRING
224 
230 template<typename StringClass>
231 class StringByteSink : public ByteSink {
232  public:
238  StringByteSink(StringClass* dest) : dest_(dest) { }
245  virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
246  private:
247  StringClass* dest_;
248  StringByteSink();
249  StringByteSink(const StringByteSink &);
250  StringByteSink &operator=(const StringByteSink &);
251 };
252 
253 #endif
254 
256 
257 #endif // __BYTESTREAM_H__