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

- Search for |

- File List
- Globals

# bytestream.h

Go to the documentation of this file.

    00001 // Copyright (C) 2009, International Business Machines
    00002 // Corporation and others. All Rights Reserved.
    00003 //
    00004 // Copyright 2007 Google Inc. All Rights Reserved.
    00005 // Author: sanjay@google.com (Sanjay Ghemawat)
    00006 //
    00007 // Abstract interface that consumes a sequence of bytes (ByteSink).
    00008 //
    00009 // Used so that we can write a single piece of code that can operate
    00010 // on a variety of output string types.
    00011 //
    00012 // Various implementations of this interface are provided:
    00013 //   ByteSink:
    00014 //      CheckedArrayByteSink    Write to a flat array, with bounds checking
    00015 //      StringByteSink          Write to an STL string
    00016
    00017 // This code is a contribution of Google code, and the style used here is
    00018 // a compromise between the original Google code and the ICU coding guidelines.
    00019 // For example, data types are ICU-ified (size_t,int->int32_t),
    00020 // and API comments doxygen-ified, but function names and behavior are
    00021 // as in the original, if possible.
    00022 // Assertion-style error handling, not available in ICU, was changed to
    00023 // parameter "pinning" similar to UnicodeString.
    00024 //
    00025 // In addition, this is only a partial port of the original Google code,
    00026 // limited to what was needed so far. The (nearly) complete original code
    00027 // is in the ICU svn repository at icuhtml/trunk/design/strings/contrib
    00028 // (see ICU ticket 6765, r25517).
    00029
    00030 #ifndef __BYTESTREAM_H__
    00031 #define __BYTESTREAM_H__
    00032
    00038 #include "unicode/utypes.h"
    00039 #include "unicode/uobject.h"
    00040 #include "unicode/std_string.h"
    00041
    00042 U_NAMESPACE_BEGIN
    00043
    00048 class U_COMMON_API ByteSink : public UMemory {
    00049 public:
    00054   ByteSink() { }
    00059   virtual ~ByteSink() { }
    00060
    00067   virtual void Append(const char* bytes, int32_t n) = 0;
    00068
    00111   virtual char* GetAppendBuffer(int32_t min_capacity,
    00112                                 int32_t desired_capacity_hint,
    00113                                 char* scratch, int32_t scratch_capacity,
    00114                                 int32_t* result_capacity);
    00115
    00123   virtual void Flush();
    00124
    00125 private:
    00126   ByteSink(const ByteSink &); // copy constructor not implemented
    00127   ByteSink &operator=(const ByteSink &); // assignment operator not implemented
    00128 };
    00129
    00130 // -------------------------------------------------------------
    00131 // Some standard implementations
    00132
    00142 class U_COMMON_API CheckedArrayByteSink : public ByteSink {
    00143 public:
    00150   CheckedArrayByteSink(char* outbuf, int32_t capacity);
    00157   virtual void Append(const char* bytes, int32_t n);
    00172   virtual char* GetAppendBuffer(int32_t min_capacity,
    00173                                 int32_t desired_capacity_hint,
    00174                                 char* scratch, int32_t scratch_capacity,
    00175                                 int32_t* result_capacity);
    00181   int32_t NumberOfBytesWritten() const { return size_; }
    00188   UBool Overflowed() const { return overflowed_; }
    00189 private:
    00190   char* outbuf_;
    00191   const int32_t capacity_;
    00192   int32_t size_;
    00193   bool overflowed_;
    00194   CheckedArrayByteSink();
    00195   CheckedArrayByteSink(const CheckedArrayByteSink &);
    00196   CheckedArrayByteSink &operator=(const CheckedArrayByteSink &);
    00197 };
    00198
    00199 #if U_HAVE_STD_STRING
    00200
    00206 template<typename StringClass>
    00207 class StringByteSink : public ByteSink {
    00208  public:
    00214   StringByteSink(StringClass* dest) : dest_(dest) { }
    00221   virtual void Append(const char* data, int32_t n) { dest_->append(data, n); }
    00222  private:
    00223   StringClass* dest_;
    00224   StringByteSink();
    00225   StringByteSink(const StringByteSink &);
    00226   StringByteSink &operator=(const StringByteSink &);
    00227 };
    00228
    00229 #endif
    00230
    00231 U_NAMESPACE_END
    00232
    00233 #endif  // __BYTESTREAM_H__