PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/simulator/libsimulator/lib/protobuf-lite/google/protobuf/io/zero_copy_stream_impl_lite.h

https://github.com/dumganhar/cocos2d-x
C Header | 340 lines | 113 code | 57 blank | 170 comment | 0 complexity | e618771e139938cf70862e0776700455 MD5 | raw file
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // This file contains common implementations of the interfaces defined in
  35. // zero_copy_stream.h which are included in the "lite" protobuf library.
  36. // These implementations cover I/O on raw arrays and strings, as well as
  37. // adaptors which make it easy to implement streams based on traditional
  38. // streams. Of course, many users will probably want to write their own
  39. // implementations of these interfaces specific to the particular I/O
  40. // abstractions they prefer to use, but these should cover the most common
  41. // cases.
  42. #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
  43. #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
  44. #include <string>
  45. #include <iosfwd>
  46. #include <google/protobuf/io/zero_copy_stream.h>
  47. #include <google/protobuf/stubs/common.h>
  48. namespace google {
  49. namespace protobuf {
  50. namespace io {
  51. // ===================================================================
  52. // A ZeroCopyInputStream backed by an in-memory array of bytes.
  53. class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
  54. public:
  55. // Create an InputStream that returns the bytes pointed to by "data".
  56. // "data" remains the property of the caller but must remain valid until
  57. // the stream is destroyed. If a block_size is given, calls to Next()
  58. // will return data blocks no larger than the given size. Otherwise, the
  59. // first call to Next() returns the entire array. block_size is mainly
  60. // useful for testing; in production you would probably never want to set
  61. // it.
  62. ArrayInputStream(const void* data, int size, int block_size = -1);
  63. ~ArrayInputStream();
  64. // implements ZeroCopyInputStream ----------------------------------
  65. bool Next(const void** data, int* size);
  66. void BackUp(int count);
  67. bool Skip(int count);
  68. int64 ByteCount() const;
  69. private:
  70. const uint8* const data_; // The byte array.
  71. const int size_; // Total size of the array.
  72. const int block_size_; // How many bytes to return at a time.
  73. int position_;
  74. int last_returned_size_; // How many bytes we returned last time Next()
  75. // was called (used for error checking only).
  76. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream);
  77. };
  78. // ===================================================================
  79. // A ZeroCopyOutputStream backed by an in-memory array of bytes.
  80. class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
  81. public:
  82. // Create an OutputStream that writes to the bytes pointed to by "data".
  83. // "data" remains the property of the caller but must remain valid until
  84. // the stream is destroyed. If a block_size is given, calls to Next()
  85. // will return data blocks no larger than the given size. Otherwise, the
  86. // first call to Next() returns the entire array. block_size is mainly
  87. // useful for testing; in production you would probably never want to set
  88. // it.
  89. ArrayOutputStream(void* data, int size, int block_size = -1);
  90. ~ArrayOutputStream();
  91. // implements ZeroCopyOutputStream ---------------------------------
  92. bool Next(void** data, int* size);
  93. void BackUp(int count);
  94. int64 ByteCount() const;
  95. private:
  96. uint8* const data_; // The byte array.
  97. const int size_; // Total size of the array.
  98. const int block_size_; // How many bytes to return at a time.
  99. int position_;
  100. int last_returned_size_; // How many bytes we returned last time Next()
  101. // was called (used for error checking only).
  102. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream);
  103. };
  104. // ===================================================================
  105. // A ZeroCopyOutputStream which appends bytes to a string.
  106. class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
  107. public:
  108. // Create a StringOutputStream which appends bytes to the given string.
  109. // The string remains property of the caller, but it MUST NOT be accessed
  110. // in any way until the stream is destroyed.
  111. //
  112. // Hint: If you call target->reserve(n) before creating the stream,
  113. // the first call to Next() will return at least n bytes of buffer
  114. // space.
  115. explicit StringOutputStream(string* target);
  116. ~StringOutputStream();
  117. // implements ZeroCopyOutputStream ---------------------------------
  118. bool Next(void** data, int* size);
  119. void BackUp(int count);
  120. int64 ByteCount() const;
  121. private:
  122. static const int kMinimumSize = 16;
  123. string* target_;
  124. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream);
  125. };
  126. // Note: There is no StringInputStream. Instead, just create an
  127. // ArrayInputStream as follows:
  128. // ArrayInputStream input(str.data(), str.size());
  129. // ===================================================================
  130. // A generic traditional input stream interface.
  131. //
  132. // Lots of traditional input streams (e.g. file descriptors, C stdio
  133. // streams, and C++ iostreams) expose an interface where every read
  134. // involves copying bytes into a buffer. If you want to take such an
  135. // interface and make a ZeroCopyInputStream based on it, simply implement
  136. // CopyingInputStream and then use CopyingInputStreamAdaptor.
  137. //
  138. // CopyingInputStream implementations should avoid buffering if possible.
  139. // CopyingInputStreamAdaptor does its own buffering and will read data
  140. // in large blocks.
  141. class LIBPROTOBUF_EXPORT CopyingInputStream {
  142. public:
  143. virtual ~CopyingInputStream();
  144. // Reads up to "size" bytes into the given buffer. Returns the number of
  145. // bytes read. Read() waits until at least one byte is available, or
  146. // returns zero if no bytes will ever become available (EOF), or -1 if a
  147. // permanent read error occurred.
  148. virtual int Read(void* buffer, int size) = 0;
  149. // Skips the next "count" bytes of input. Returns the number of bytes
  150. // actually skipped. This will always be exactly equal to "count" unless
  151. // EOF was reached or a permanent read error occurred.
  152. //
  153. // The default implementation just repeatedly calls Read() into a scratch
  154. // buffer.
  155. virtual int Skip(int count);
  156. };
  157. // A ZeroCopyInputStream which reads from a CopyingInputStream. This is
  158. // useful for implementing ZeroCopyInputStreams that read from traditional
  159. // streams. Note that this class is not really zero-copy.
  160. //
  161. // If you want to read from file descriptors or C++ istreams, this is
  162. // already implemented for you: use FileInputStream or IstreamInputStream
  163. // respectively.
  164. class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
  165. public:
  166. // Creates a stream that reads from the given CopyingInputStream.
  167. // If a block_size is given, it specifies the number of bytes that
  168. // should be read and returned with each call to Next(). Otherwise,
  169. // a reasonable default is used. The caller retains ownership of
  170. // copying_stream unless SetOwnsCopyingStream(true) is called.
  171. explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
  172. int block_size = -1);
  173. ~CopyingInputStreamAdaptor();
  174. // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
  175. // delete the underlying CopyingInputStream when it is destroyed.
  176. void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
  177. // implements ZeroCopyInputStream ----------------------------------
  178. bool Next(const void** data, int* size);
  179. void BackUp(int count);
  180. bool Skip(int count);
  181. int64 ByteCount() const;
  182. private:
  183. // Insures that buffer_ is not NULL.
  184. void AllocateBufferIfNeeded();
  185. // Frees the buffer and resets buffer_used_.
  186. void FreeBuffer();
  187. // The underlying copying stream.
  188. CopyingInputStream* copying_stream_;
  189. bool owns_copying_stream_;
  190. // True if we have seen a permenant error from the underlying stream.
  191. bool failed_;
  192. // The current position of copying_stream_, relative to the point where
  193. // we started reading.
  194. int64 position_;
  195. // Data is read into this buffer. It may be NULL if no buffer is currently
  196. // in use. Otherwise, it points to an array of size buffer_size_.
  197. scoped_array<uint8> buffer_;
  198. const int buffer_size_;
  199. // Number of valid bytes currently in the buffer (i.e. the size last
  200. // returned by Next()). 0 <= buffer_used_ <= buffer_size_.
  201. int buffer_used_;
  202. // Number of bytes in the buffer which were backed up over by a call to
  203. // BackUp(). These need to be returned again.
  204. // 0 <= backup_bytes_ <= buffer_used_
  205. int backup_bytes_;
  206. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor);
  207. };
  208. // ===================================================================
  209. // A generic traditional output stream interface.
  210. //
  211. // Lots of traditional output streams (e.g. file descriptors, C stdio
  212. // streams, and C++ iostreams) expose an interface where every write
  213. // involves copying bytes from a buffer. If you want to take such an
  214. // interface and make a ZeroCopyOutputStream based on it, simply implement
  215. // CopyingOutputStream and then use CopyingOutputStreamAdaptor.
  216. //
  217. // CopyingOutputStream implementations should avoid buffering if possible.
  218. // CopyingOutputStreamAdaptor does its own buffering and will write data
  219. // in large blocks.
  220. class LIBPROTOBUF_EXPORT CopyingOutputStream {
  221. public:
  222. virtual ~CopyingOutputStream();
  223. // Writes "size" bytes from the given buffer to the output. Returns true
  224. // if successful, false on a write error.
  225. virtual bool Write(const void* buffer, int size) = 0;
  226. };
  227. // A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is
  228. // useful for implementing ZeroCopyOutputStreams that write to traditional
  229. // streams. Note that this class is not really zero-copy.
  230. //
  231. // If you want to write to file descriptors or C++ ostreams, this is
  232. // already implemented for you: use FileOutputStream or OstreamOutputStream
  233. // respectively.
  234. class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
  235. public:
  236. // Creates a stream that writes to the given Unix file descriptor.
  237. // If a block_size is given, it specifies the size of the buffers
  238. // that should be returned by Next(). Otherwise, a reasonable default
  239. // is used.
  240. explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
  241. int block_size = -1);
  242. ~CopyingOutputStreamAdaptor();
  243. // Writes all pending data to the underlying stream. Returns false if a
  244. // write error occurred on the underlying stream. (The underlying
  245. // stream itself is not necessarily flushed.)
  246. bool Flush();
  247. // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
  248. // delete the underlying CopyingOutputStream when it is destroyed.
  249. void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
  250. // implements ZeroCopyOutputStream ---------------------------------
  251. bool Next(void** data, int* size);
  252. void BackUp(int count);
  253. int64 ByteCount() const;
  254. private:
  255. // Write the current buffer, if it is present.
  256. bool WriteBuffer();
  257. // Insures that buffer_ is not NULL.
  258. void AllocateBufferIfNeeded();
  259. // Frees the buffer.
  260. void FreeBuffer();
  261. // The underlying copying stream.
  262. CopyingOutputStream* copying_stream_;
  263. bool owns_copying_stream_;
  264. // True if we have seen a permenant error from the underlying stream.
  265. bool failed_;
  266. // The current position of copying_stream_, relative to the point where
  267. // we started writing.
  268. int64 position_;
  269. // Data is written from this buffer. It may be NULL if no buffer is
  270. // currently in use. Otherwise, it points to an array of size buffer_size_.
  271. scoped_array<uint8> buffer_;
  272. const int buffer_size_;
  273. // Number of valid bytes currently in the buffer (i.e. the size last
  274. // returned by Next()). When BackUp() is called, we just reduce this.
  275. // 0 <= buffer_used_ <= buffer_size_.
  276. int buffer_used_;
  277. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor);
  278. };
  279. // ===================================================================
  280. } // namespace io
  281. } // namespace protobuf
  282. } // namespace google
  283. #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__