BWAPI
Undermind/proxy/cpp/include/google/protobuf/io/zero_copy_stream_impl_lite.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // Author: kenton@google.com (Kenton Varda)
00032 //  Based on original Protocol Buffers design by
00033 //  Sanjay Ghemawat, Jeff Dean, and others.
00034 //
00035 // This file contains common implementations of the interfaces defined in
00036 // zero_copy_stream.h which are included in the "lite" protobuf library.
00037 // These implementations cover I/O on raw arrays and strings, as well as
00038 // adaptors which make it easy to implement streams based on traditional
00039 // streams.  Of course, many users will probably want to write their own
00040 // implementations of these interfaces specific to the particular I/O
00041 // abstractions they prefer to use, but these should cover the most common
00042 // cases.
00043 
00044 #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
00045 #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
00046 
00047 #include <string>
00048 #include <iosfwd>
00049 #include <google/protobuf/io/zero_copy_stream.h>
00050 #include <google/protobuf/stubs/common.h>
00051 
00052 
00053 namespace google {
00054 namespace protobuf {
00055 namespace io {
00056 
00057 // ===================================================================
00058 
00059 // A ZeroCopyInputStream backed by an in-memory array of bytes.
00060 class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
00061  public:
00062   // Create an InputStream that returns the bytes pointed to by "data".
00063   // "data" remains the property of the caller but must remain valid until
00064   // the stream is destroyed.  If a block_size is given, calls to Next()
00065   // will return data blocks no larger than the given size.  Otherwise, the
00066   // first call to Next() returns the entire array.  block_size is mainly
00067   // useful for testing; in production you would probably never want to set
00068   // it.
00069   ArrayInputStream(const void* data, int size, int block_size = -1);
00070   ~ArrayInputStream();
00071 
00072   // implements ZeroCopyInputStream ----------------------------------
00073   bool Next(const void** data, int* size);
00074   void BackUp(int count);
00075   bool Skip(int count);
00076   int64 ByteCount() const;
00077 
00078 
00079  private:
00080   const uint8* const data_;  // The byte array.
00081   const int size_;           // Total size of the array.
00082   const int block_size_;     // How many bytes to return at a time.
00083 
00084   int position_;
00085   int last_returned_size_;   // How many bytes we returned last time Next()
00086                              // was called (used for error checking only).
00087 
00088   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream);
00089 };
00090 
00091 // ===================================================================
00092 
00093 // A ZeroCopyOutputStream backed by an in-memory array of bytes.
00094 class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
00095  public:
00096   // Create an OutputStream that writes to the bytes pointed to by "data".
00097   // "data" remains the property of the caller but must remain valid until
00098   // the stream is destroyed.  If a block_size is given, calls to Next()
00099   // will return data blocks no larger than the given size.  Otherwise, the
00100   // first call to Next() returns the entire array.  block_size is mainly
00101   // useful for testing; in production you would probably never want to set
00102   // it.
00103   ArrayOutputStream(void* data, int size, int block_size = -1);
00104   ~ArrayOutputStream();
00105 
00106   // implements ZeroCopyOutputStream ---------------------------------
00107   bool Next(void** data, int* size);
00108   void BackUp(int count);
00109   int64 ByteCount() const;
00110 
00111  private:
00112   uint8* const data_;        // The byte array.
00113   const int size_;           // Total size of the array.
00114   const int block_size_;     // How many bytes to return at a time.
00115 
00116   int position_;
00117   int last_returned_size_;   // How many bytes we returned last time Next()
00118                              // was called (used for error checking only).
00119 
00120   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream);
00121 };
00122 
00123 // ===================================================================
00124 
00125 // A ZeroCopyOutputStream which appends bytes to a string.
00126 class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
00127  public:
00128   // Create a StringOutputStream which appends bytes to the given string.
00129   // The string remains property of the caller, but it MUST NOT be accessed
00130   // in any way until the stream is destroyed.
00131   //
00132   // Hint:  If you call target->reserve(n) before creating the stream,
00133   //   the first call to Next() will return at least n bytes of buffer
00134   //   space.
00135   explicit StringOutputStream(string* target);
00136   ~StringOutputStream();
00137 
00138   // implements ZeroCopyOutputStream ---------------------------------
00139   bool Next(void** data, int* size);
00140   void BackUp(int count);
00141   int64 ByteCount() const;
00142 
00143  private:
00144   static const int kMinimumSize = 16;
00145 
00146   string* target_;
00147 
00148   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream);
00149 };
00150 
00151 // Note:  There is no StringInputStream.  Instead, just create an
00152 // ArrayInputStream as follows:
00153 //   ArrayInputStream input(str.data(), str.size());
00154 
00155 // ===================================================================
00156 
00157 // A generic traditional input stream interface.
00158 //
00159 // Lots of traditional input streams (e.g. file descriptors, C stdio
00160 // streams, and C++ iostreams) expose an interface where every read
00161 // involves copying bytes into a buffer.  If you want to take such an
00162 // interface and make a ZeroCopyInputStream based on it, simply implement
00163 // CopyingInputStream and then use CopyingInputStreamAdaptor.
00164 //
00165 // CopyingInputStream implementations should avoid buffering if possible.
00166 // CopyingInputStreamAdaptor does its own buffering and will read data
00167 // in large blocks.
00168 class LIBPROTOBUF_EXPORT CopyingInputStream {
00169  public:
00170   virtual ~CopyingInputStream();
00171 
00172   // Reads up to "size" bytes into the given buffer.  Returns the number of
00173   // bytes read.  Read() waits until at least one byte is available, or
00174   // returns zero if no bytes will ever become available (EOF), or -1 if a
00175   // permanent read error occurred.
00176   virtual int Read(void* buffer, int size) = 0;
00177 
00178   // Skips the next "count" bytes of input.  Returns the number of bytes
00179   // actually skipped.  This will always be exactly equal to "count" unless
00180   // EOF was reached or a permanent read error occurred.
00181   //
00182   // The default implementation just repeatedly calls Read() into a scratch
00183   // buffer.
00184   virtual int Skip(int count);
00185 };
00186 
00187 // A ZeroCopyInputStream which reads from a CopyingInputStream.  This is
00188 // useful for implementing ZeroCopyInputStreams that read from traditional
00189 // streams.  Note that this class is not really zero-copy.
00190 //
00191 // If you want to read from file descriptors or C++ istreams, this is
00192 // already implemented for you:  use FileInputStream or IstreamInputStream
00193 // respectively.
00194 class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
00195  public:
00196   // Creates a stream that reads from the given CopyingInputStream.
00197   // If a block_size is given, it specifies the number of bytes that
00198   // should be read and returned with each call to Next().  Otherwise,
00199   // a reasonable default is used.  The caller retains ownership of
00200   // copying_stream unless SetOwnsCopyingStream(true) is called.
00201   explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
00202                                      int block_size = -1);
00203   ~CopyingInputStreamAdaptor();
00204 
00205   // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
00206   // delete the underlying CopyingInputStream when it is destroyed.
00207   void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
00208 
00209   // implements ZeroCopyInputStream ----------------------------------
00210   bool Next(const void** data, int* size);
00211   void BackUp(int count);
00212   bool Skip(int count);
00213   int64 ByteCount() const;
00214 
00215  private:
00216   // Insures that buffer_ is not NULL.
00217   void AllocateBufferIfNeeded();
00218   // Frees the buffer and resets buffer_used_.
00219   void FreeBuffer();
00220 
00221   // The underlying copying stream.
00222   CopyingInputStream* copying_stream_;
00223   bool owns_copying_stream_;
00224 
00225   // True if we have seen a permenant error from the underlying stream.
00226   bool failed_;
00227 
00228   // The current position of copying_stream_, relative to the point where
00229   // we started reading.
00230   int64 position_;
00231 
00232   // Data is read into this buffer.  It may be NULL if no buffer is currently
00233   // in use.  Otherwise, it points to an array of size buffer_size_.
00234   scoped_array<uint8> buffer_;
00235   const int buffer_size_;
00236 
00237   // Number of valid bytes currently in the buffer (i.e. the size last
00238   // returned by Next()).  0 <= buffer_used_ <= buffer_size_.
00239   int buffer_used_;
00240 
00241   // Number of bytes in the buffer which were backed up over by a call to
00242   // BackUp().  These need to be returned again.
00243   // 0 <= backup_bytes_ <= buffer_used_
00244   int backup_bytes_;
00245 
00246   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor);
00247 };
00248 
00249 // ===================================================================
00250 
00251 // A generic traditional output stream interface.
00252 //
00253 // Lots of traditional output streams (e.g. file descriptors, C stdio
00254 // streams, and C++ iostreams) expose an interface where every write
00255 // involves copying bytes from a buffer.  If you want to take such an
00256 // interface and make a ZeroCopyOutputStream based on it, simply implement
00257 // CopyingOutputStream and then use CopyingOutputStreamAdaptor.
00258 //
00259 // CopyingOutputStream implementations should avoid buffering if possible.
00260 // CopyingOutputStreamAdaptor does its own buffering and will write data
00261 // in large blocks.
00262 class LIBPROTOBUF_EXPORT CopyingOutputStream {
00263  public:
00264   virtual ~CopyingOutputStream();
00265 
00266   // Writes "size" bytes from the given buffer to the output.  Returns true
00267   // if successful, false on a write error.
00268   virtual bool Write(const void* buffer, int size) = 0;
00269 };
00270 
00271 // A ZeroCopyOutputStream which writes to a CopyingOutputStream.  This is
00272 // useful for implementing ZeroCopyOutputStreams that write to traditional
00273 // streams.  Note that this class is not really zero-copy.
00274 //
00275 // If you want to write to file descriptors or C++ ostreams, this is
00276 // already implemented for you:  use FileOutputStream or OstreamOutputStream
00277 // respectively.
00278 class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
00279  public:
00280   // Creates a stream that writes to the given Unix file descriptor.
00281   // If a block_size is given, it specifies the size of the buffers
00282   // that should be returned by Next().  Otherwise, a reasonable default
00283   // is used.
00284   explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
00285                                       int block_size = -1);
00286   ~CopyingOutputStreamAdaptor();
00287 
00288   // Writes all pending data to the underlying stream.  Returns false if a
00289   // write error occurred on the underlying stream.  (The underlying
00290   // stream itself is not necessarily flushed.)
00291   bool Flush();
00292 
00293   // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
00294   // delete the underlying CopyingOutputStream when it is destroyed.
00295   void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
00296 
00297   // implements ZeroCopyOutputStream ---------------------------------
00298   bool Next(void** data, int* size);
00299   void BackUp(int count);
00300   int64 ByteCount() const;
00301 
00302  private:
00303   // Write the current buffer, if it is present.
00304   bool WriteBuffer();
00305   // Insures that buffer_ is not NULL.
00306   void AllocateBufferIfNeeded();
00307   // Frees the buffer.
00308   void FreeBuffer();
00309 
00310   // The underlying copying stream.
00311   CopyingOutputStream* copying_stream_;
00312   bool owns_copying_stream_;
00313 
00314   // True if we have seen a permenant error from the underlying stream.
00315   bool failed_;
00316 
00317   // The current position of copying_stream_, relative to the point where
00318   // we started writing.
00319   int64 position_;
00320 
00321   // Data is written from this buffer.  It may be NULL if no buffer is
00322   // currently in use.  Otherwise, it points to an array of size buffer_size_.
00323   scoped_array<uint8> buffer_;
00324   const int buffer_size_;
00325 
00326   // Number of valid bytes currently in the buffer (i.e. the size last
00327   // returned by Next()).  When BackUp() is called, we just reduce this.
00328   // 0 <= buffer_used_ <= buffer_size_.
00329   int buffer_used_;
00330 
00331   GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor);
00332 };
00333 
00334 // ===================================================================
00335 
00336 }  // namespace io
00337 }  // namespace protobuf
00338 
00339 }  // namespace google
00340 #endif  // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines