protobuf 2.8.0.beta9 → 2.8.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (204) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +0 -1
  3. data/{UPGRADING.md → CHANGES.md} +20 -12
  4. data/LICENSE.txt +14 -0
  5. data/README.md +60 -74
  6. data/bin/protoc-gen-ruby +26 -0
  7. data/bin/rprotoc +4 -58
  8. data/lib/protobuf.rb +15 -13
  9. data/lib/protobuf/code_generator.rb +41 -0
  10. data/lib/protobuf/descriptors/google/protobuf/compiler/plugin.pb.rb +51 -0
  11. data/lib/protobuf/descriptors/google/protobuf/descriptor.pb.rb +248 -0
  12. data/lib/protobuf/field/base_field.rb +0 -1
  13. data/lib/protobuf/field/bytes_field.rb +1 -1
  14. data/lib/protobuf/field/message_field.rb +2 -4
  15. data/lib/protobuf/field/varint_field.rb +4 -7
  16. data/lib/protobuf/generators/base.rb +71 -0
  17. data/lib/protobuf/generators/enum_generator.rb +31 -0
  18. data/lib/protobuf/generators/extension_generator.rb +28 -0
  19. data/lib/protobuf/generators/field_generator.rb +132 -0
  20. data/lib/protobuf/generators/file_generator.rb +140 -0
  21. data/lib/protobuf/generators/group_generator.rb +113 -0
  22. data/lib/protobuf/generators/message_generator.rb +99 -0
  23. data/lib/protobuf/generators/printable.rb +161 -0
  24. data/lib/protobuf/generators/service_generator.rb +27 -0
  25. data/lib/protobuf/lifecycle.rb +7 -3
  26. data/lib/protobuf/message.rb +51 -34
  27. data/lib/protobuf/rpc/connectors/common.rb +4 -6
  28. data/lib/protobuf/rpc/server.rb +1 -1
  29. data/lib/protobuf/rpc/servers/zmq/server.rb +2 -2
  30. data/lib/protobuf/rpc/servers/zmq/worker.rb +2 -2
  31. data/lib/protobuf/rpc/service.rb +3 -3
  32. data/lib/protobuf/rpc/service_directory.rb +24 -12
  33. data/lib/protobuf/version.rb +1 -1
  34. data/proto/google/protobuf/compiler/plugin.proto +147 -0
  35. data/proto/google/protobuf/descriptor.proto +620 -0
  36. data/protobuf.gemspec +12 -21
  37. data/spec/bin/protoc-gen-ruby_spec.rb +18 -0
  38. data/spec/data/data.bin +3 -0
  39. data/{test/data/unk.png → spec/data/types.bin} +0 -0
  40. data/spec/encoding/all_types_spec.rb +91 -0
  41. data/spec/encoding/extreme_values_spec.rb +0 -0
  42. data/spec/lib/protobuf/code_generator_spec.rb +60 -0
  43. data/spec/lib/protobuf/generators/base_spec.rb +87 -0
  44. data/spec/lib/protobuf/generators/enum_generator_spec.rb +45 -0
  45. data/spec/lib/protobuf/generators/extension_generator_spec.rb +43 -0
  46. data/spec/lib/protobuf/generators/field_generator_spec.rb +99 -0
  47. data/spec/lib/protobuf/generators/file_generator_spec.rb +29 -0
  48. data/spec/lib/protobuf/generators/message_generator_spec.rb +0 -0
  49. data/spec/lib/protobuf/generators/service_generator_spec.rb +43 -0
  50. data/spec/lib/protobuf/lifecycle_spec.rb +31 -1
  51. data/spec/lib/protobuf/logger_spec.rb +5 -0
  52. data/spec/lib/protobuf/message_spec.rb +22 -8
  53. data/spec/lib/protobuf/rpc/connectors/common_spec.rb +1 -1
  54. data/spec/lib/protobuf/rpc/service_directory_spec.rb +45 -7
  55. data/spec/lib/protobuf/rpc/service_spec.rb +3 -3
  56. data/spec/spec_helper.rb +15 -16
  57. data/spec/support/test/all_types.data.bin +0 -0
  58. data/spec/support/test/all_types.data.txt +119 -0
  59. data/spec/support/test/defaults.pb.rb +25 -0
  60. data/spec/support/test/defaults.proto +9 -0
  61. data/spec/support/test/enum.pb.rb +10 -8
  62. data/spec/support/test/extended.pb.rb +5 -3
  63. data/spec/support/test/extreme_values.data.bin +0 -0
  64. data/spec/support/test/google_unittest.pb.rb +543 -0
  65. data/spec/support/test/google_unittest.proto +713 -0
  66. data/spec/support/test/google_unittest_import.pb.rb +37 -0
  67. data/{ext/protobuf-2.4.1/src/google/protobuf/io/package_info.h → spec/support/test/google_unittest_import.proto} +27 -17
  68. data/spec/support/test/google_unittest_import_public.pb.rb +8 -0
  69. data/{ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream_inl.h → spec/support/test/google_unittest_import_public.proto} +5 -31
  70. data/spec/support/test/multi_field_extensions.pb.rb +29 -7
  71. data/spec/support/test/multi_field_extensions.proto +12 -2
  72. data/spec/support/test/resource.pb.rb +20 -16
  73. metadata +149 -236
  74. data/examples/addressbook.pb.rb +0 -55
  75. data/examples/addressbook.proto +0 -24
  76. data/examples/reading_a_message.rb +0 -32
  77. data/examples/writing_a_message.rb +0 -46
  78. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/code_generator.h +0 -142
  79. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/command_line_interface.h +0 -318
  80. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum.h +0 -99
  81. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_enum_field.h +0 -103
  82. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_extension.h +0 -85
  83. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_field.h +0 -167
  84. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_file.h +0 -98
  85. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_generator.h +0 -72
  86. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_helpers.h +0 -159
  87. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message.h +0 -170
  88. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_message_field.h +0 -102
  89. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_primitive_field.h +0 -103
  90. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_service.h +0 -118
  91. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_string_field.h +0 -104
  92. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/cpp/cpp_test_bad_identifiers.pb.h +0 -2721
  93. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/importer.h +0 -303
  94. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum.h +0 -84
  95. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_enum_field.h +0 -121
  96. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_extension.h +0 -77
  97. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_field.h +0 -108
  98. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_file.h +0 -101
  99. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_generator.h +0 -72
  100. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_helpers.h +0 -213
  101. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message.h +0 -109
  102. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_message_field.h +0 -134
  103. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_primitive_field.h +0 -121
  104. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_service.h +0 -113
  105. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/java/java_string_field.h +0 -120
  106. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/mock_code_generator.h +0 -113
  107. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/package_info.h +0 -64
  108. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/parser.h +0 -434
  109. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.h +0 -73
  110. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/plugin.pb.h +0 -790
  111. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/python/python_generator.h +0 -156
  112. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/subprocess.h +0 -108
  113. data/ext/protobuf-2.4.1/src/google/protobuf/compiler/zip_writer.h +0 -93
  114. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.h +0 -1367
  115. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor.pb.h +0 -5223
  116. data/ext/protobuf-2.4.1/src/google/protobuf/descriptor_database.h +0 -366
  117. data/ext/protobuf-2.4.1/src/google/protobuf/dynamic_message.h +0 -136
  118. data/ext/protobuf-2.4.1/src/google/protobuf/extension_set.h +0 -904
  119. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_reflection.h +0 -424
  120. data/ext/protobuf-2.4.1/src/google/protobuf/generated_message_util.h +0 -82
  121. data/ext/protobuf-2.4.1/src/google/protobuf/io/coded_stream.h +0 -1102
  122. data/ext/protobuf-2.4.1/src/google/protobuf/io/gzip_stream.h +0 -207
  123. data/ext/protobuf-2.4.1/src/google/protobuf/io/printer.h +0 -136
  124. data/ext/protobuf-2.4.1/src/google/protobuf/io/tokenizer.h +0 -313
  125. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream.h +0 -238
  126. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl.h +0 -357
  127. data/ext/protobuf-2.4.1/src/google/protobuf/io/zero_copy_stream_impl_lite.h +0 -340
  128. data/ext/protobuf-2.4.1/src/google/protobuf/message.h +0 -692
  129. data/ext/protobuf-2.4.1/src/google/protobuf/message_lite.h +0 -239
  130. data/ext/protobuf-2.4.1/src/google/protobuf/package_info.h +0 -64
  131. data/ext/protobuf-2.4.1/src/google/protobuf/reflection_ops.h +0 -80
  132. data/ext/protobuf-2.4.1/src/google/protobuf/repeated_field.h +0 -1295
  133. data/ext/protobuf-2.4.1/src/google/protobuf/service.h +0 -291
  134. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/common.h +0 -1211
  135. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/hash.h +0 -220
  136. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/map-util.h +0 -119
  137. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/once.h +0 -123
  138. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/stl_util-inl.h +0 -121
  139. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/strutil.h +0 -457
  140. data/ext/protobuf-2.4.1/src/google/protobuf/stubs/substitute.h +0 -170
  141. data/ext/protobuf-2.4.1/src/google/protobuf/test_util.h +0 -174
  142. data/ext/protobuf-2.4.1/src/google/protobuf/test_util_lite.h +0 -101
  143. data/ext/protobuf-2.4.1/src/google/protobuf/testing/file.h +0 -83
  144. data/ext/protobuf-2.4.1/src/google/protobuf/testing/googletest.h +0 -98
  145. data/ext/protobuf-2.4.1/src/google/protobuf/text_format.h +0 -285
  146. data/ext/protobuf-2.4.1/src/google/protobuf/unittest.pb.h +0 -11915
  147. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_custom_options.pb.h +0 -2895
  148. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_embed_optimize_for.pb.h +0 -211
  149. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_empty.pb.h +0 -56
  150. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import.pb.h +0 -188
  151. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_import_lite.pb.h +0 -151
  152. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite.pb.h +0 -4752
  153. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_lite_imports_nonlite.pb.h +0 -150
  154. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_mset.pb.h +0 -816
  155. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_no_generic_services.pb.h +0 -197
  156. data/ext/protobuf-2.4.1/src/google/protobuf/unittest_optimize_for.pb.h +0 -403
  157. data/ext/protobuf-2.4.1/src/google/protobuf/unknown_field_set.h +0 -268
  158. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format.h +0 -304
  159. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite.h +0 -620
  160. data/ext/protobuf-2.4.1/src/google/protobuf/wire_format_lite_inl.h +0 -774
  161. data/ext/ruby_generator/Makefile +0 -10
  162. data/ext/ruby_generator/RubyGenerator.cpp +0 -544
  163. data/ext/ruby_generator/RubyGenerator.h +0 -206
  164. data/ext/ruby_generator/extconf.rb +0 -35
  165. data/test/data/data_source.py +0 -14
  166. data/test/data/types_source.py +0 -22
  167. data/test/proto/addressbook.pb.rb +0 -66
  168. data/test/proto/addressbook.proto +0 -33
  169. data/test/proto/addressbook_base.pb.rb +0 -58
  170. data/test/proto/addressbook_base.proto +0 -26
  171. data/test/proto/addressbook_ext.pb.rb +0 -20
  172. data/test/proto/addressbook_ext.proto +0 -6
  173. data/test/proto/collision.pb.rb +0 -17
  174. data/test/proto/collision.proto +0 -5
  175. data/test/proto/ext_collision.pb.rb +0 -24
  176. data/test/proto/ext_collision.proto +0 -8
  177. data/test/proto/ext_range.pb.rb +0 -22
  178. data/test/proto/ext_range.proto +0 -7
  179. data/test/proto/float_default.proto +0 -10
  180. data/test/proto/lowercase.pb.rb +0 -30
  181. data/test/proto/lowercase.proto +0 -9
  182. data/test/proto/merge.pb.rb +0 -39
  183. data/test/proto/merge.proto +0 -15
  184. data/test/proto/nested.pb.rb +0 -30
  185. data/test/proto/nested.proto +0 -9
  186. data/test/proto/optional_field.pb.rb +0 -35
  187. data/test/proto/optional_field.proto +0 -12
  188. data/test/proto/packed.pb.rb +0 -22
  189. data/test/proto/packed.proto +0 -6
  190. data/test/proto/rpc.proto +0 -6
  191. data/test/proto/types.pb.rb +0 -84
  192. data/test/proto/types.proto +0 -37
  193. data/test/test_addressbook.rb +0 -56
  194. data/test/test_enum_value.rb +0 -41
  195. data/test/test_extension.rb +0 -36
  196. data/test/test_lowercase.rb +0 -11
  197. data/test/test_message.rb +0 -128
  198. data/test/test_optional_field.rb +0 -103
  199. data/test/test_packed_field.rb +0 -40
  200. data/test/test_parse.rb +0 -15
  201. data/test/test_repeated_types.rb +0 -132
  202. data/test/test_serialize.rb +0 -61
  203. data/test/test_standard_message.rb +0 -96
  204. data/test/test_types.rb +0 -226
@@ -1,238 +0,0 @@
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
-
31
- // Author: kenton@google.com (Kenton Varda)
32
- // Based on original Protocol Buffers design by
33
- // Sanjay Ghemawat, Jeff Dean, and others.
34
- //
35
- // This file contains the ZeroCopyInputStream and ZeroCopyOutputStream
36
- // interfaces, which represent abstract I/O streams to and from which
37
- // protocol buffers can be read and written. For a few simple
38
- // implementations of these interfaces, see zero_copy_stream_impl.h.
39
- //
40
- // These interfaces are different from classic I/O streams in that they
41
- // try to minimize the amount of data copying that needs to be done.
42
- // To accomplish this, responsibility for allocating buffers is moved to
43
- // the stream object, rather than being the responsibility of the caller.
44
- // So, the stream can return a buffer which actually points directly into
45
- // the final data structure where the bytes are to be stored, and the caller
46
- // can interact directly with that buffer, eliminating an intermediate copy
47
- // operation.
48
- //
49
- // As an example, consider the common case in which you are reading bytes
50
- // from an array that is already in memory (or perhaps an mmap()ed file).
51
- // With classic I/O streams, you would do something like:
52
- // char buffer[BUFFER_SIZE];
53
- // input->Read(buffer, BUFFER_SIZE);
54
- // DoSomething(buffer, BUFFER_SIZE);
55
- // Then, the stream basically just calls memcpy() to copy the data from
56
- // the array into your buffer. With a ZeroCopyInputStream, you would do
57
- // this instead:
58
- // const void* buffer;
59
- // int size;
60
- // input->Next(&buffer, &size);
61
- // DoSomething(buffer, size);
62
- // Here, no copy is performed. The input stream returns a pointer directly
63
- // into the backing array, and the caller ends up reading directly from it.
64
- //
65
- // If you want to be able to read the old-fashion way, you can create
66
- // a CodedInputStream or CodedOutputStream wrapping these objects and use
67
- // their ReadRaw()/WriteRaw() methods. These will, of course, add a copy
68
- // step, but Coded*Stream will handle buffering so at least it will be
69
- // reasonably efficient.
70
- //
71
- // ZeroCopyInputStream example:
72
- // // Read in a file and print its contents to stdout.
73
- // int fd = open("myfile", O_RDONLY);
74
- // ZeroCopyInputStream* input = new FileInputStream(fd);
75
- //
76
- // const void* buffer;
77
- // int size;
78
- // while (input->Next(&buffer, &size)) {
79
- // cout.write(buffer, size);
80
- // }
81
- //
82
- // delete input;
83
- // close(fd);
84
- //
85
- // ZeroCopyOutputStream example:
86
- // // Copy the contents of "infile" to "outfile", using plain read() for
87
- // // "infile" but a ZeroCopyOutputStream for "outfile".
88
- // int infd = open("infile", O_RDONLY);
89
- // int outfd = open("outfile", O_WRONLY);
90
- // ZeroCopyOutputStream* output = new FileOutputStream(outfd);
91
- //
92
- // void* buffer;
93
- // int size;
94
- // while (output->Next(&buffer, &size)) {
95
- // int bytes = read(infd, buffer, size);
96
- // if (bytes < size) {
97
- // // Reached EOF.
98
- // output->BackUp(size - bytes);
99
- // break;
100
- // }
101
- // }
102
- //
103
- // delete output;
104
- // close(infd);
105
- // close(outfd);
106
-
107
- #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__
108
- #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__
109
-
110
- #include <string>
111
- #include <google/protobuf/stubs/common.h>
112
-
113
- namespace google {
114
-
115
- namespace protobuf {
116
- namespace io {
117
-
118
- // Defined in this file.
119
- class ZeroCopyInputStream;
120
- class ZeroCopyOutputStream;
121
-
122
- // Abstract interface similar to an input stream but designed to minimize
123
- // copying.
124
- class LIBPROTOBUF_EXPORT ZeroCopyInputStream {
125
- public:
126
- inline ZeroCopyInputStream() {}
127
- virtual ~ZeroCopyInputStream();
128
-
129
- // Obtains a chunk of data from the stream.
130
- //
131
- // Preconditions:
132
- // * "size" and "data" are not NULL.
133
- //
134
- // Postconditions:
135
- // * If the returned value is false, there is no more data to return or
136
- // an error occurred. All errors are permanent.
137
- // * Otherwise, "size" points to the actual number of bytes read and "data"
138
- // points to a pointer to a buffer containing these bytes.
139
- // * Ownership of this buffer remains with the stream, and the buffer
140
- // remains valid only until some other method of the stream is called
141
- // or the stream is destroyed.
142
- // * It is legal for the returned buffer to have zero size, as long
143
- // as repeatedly calling Next() eventually yields a buffer with non-zero
144
- // size.
145
- virtual bool Next(const void** data, int* size) = 0;
146
-
147
- // Backs up a number of bytes, so that the next call to Next() returns
148
- // data again that was already returned by the last call to Next(). This
149
- // is useful when writing procedures that are only supposed to read up
150
- // to a certain point in the input, then return. If Next() returns a
151
- // buffer that goes beyond what you wanted to read, you can use BackUp()
152
- // to return to the point where you intended to finish.
153
- //
154
- // Preconditions:
155
- // * The last method called must have been Next().
156
- // * count must be less than or equal to the size of the last buffer
157
- // returned by Next().
158
- //
159
- // Postconditions:
160
- // * The last "count" bytes of the last buffer returned by Next() will be
161
- // pushed back into the stream. Subsequent calls to Next() will return
162
- // the same data again before producing new data.
163
- virtual void BackUp(int count) = 0;
164
-
165
- // Skips a number of bytes. Returns false if the end of the stream is
166
- // reached or some input error occurred. In the end-of-stream case, the
167
- // stream is advanced to the end of the stream (so ByteCount() will return
168
- // the total size of the stream).
169
- virtual bool Skip(int count) = 0;
170
-
171
- // Returns the total number of bytes read since this object was created.
172
- virtual int64 ByteCount() const = 0;
173
-
174
-
175
- private:
176
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyInputStream);
177
- };
178
-
179
- // Abstract interface similar to an output stream but designed to minimize
180
- // copying.
181
- class LIBPROTOBUF_EXPORT ZeroCopyOutputStream {
182
- public:
183
- inline ZeroCopyOutputStream() {}
184
- virtual ~ZeroCopyOutputStream();
185
-
186
- // Obtains a buffer into which data can be written. Any data written
187
- // into this buffer will eventually (maybe instantly, maybe later on)
188
- // be written to the output.
189
- //
190
- // Preconditions:
191
- // * "size" and "data" are not NULL.
192
- //
193
- // Postconditions:
194
- // * If the returned value is false, an error occurred. All errors are
195
- // permanent.
196
- // * Otherwise, "size" points to the actual number of bytes in the buffer
197
- // and "data" points to the buffer.
198
- // * Ownership of this buffer remains with the stream, and the buffer
199
- // remains valid only until some other method of the stream is called
200
- // or the stream is destroyed.
201
- // * Any data which the caller stores in this buffer will eventually be
202
- // written to the output (unless BackUp() is called).
203
- // * It is legal for the returned buffer to have zero size, as long
204
- // as repeatedly calling Next() eventually yields a buffer with non-zero
205
- // size.
206
- virtual bool Next(void** data, int* size) = 0;
207
-
208
- // Backs up a number of bytes, so that the end of the last buffer returned
209
- // by Next() is not actually written. This is needed when you finish
210
- // writing all the data you want to write, but the last buffer was bigger
211
- // than you needed. You don't want to write a bunch of garbage after the
212
- // end of your data, so you use BackUp() to back up.
213
- //
214
- // Preconditions:
215
- // * The last method called must have been Next().
216
- // * count must be less than or equal to the size of the last buffer
217
- // returned by Next().
218
- // * The caller must not have written anything to the last "count" bytes
219
- // of that buffer.
220
- //
221
- // Postconditions:
222
- // * The last "count" bytes of the last buffer returned by Next() will be
223
- // ignored.
224
- virtual void BackUp(int count) = 0;
225
-
226
- // Returns the total number of bytes written since this object was created.
227
- virtual int64 ByteCount() const = 0;
228
-
229
-
230
- private:
231
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ZeroCopyOutputStream);
232
- };
233
-
234
- } // namespace io
235
- } // namespace protobuf
236
-
237
- } // namespace google
238
- #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_H__
@@ -1,357 +0,0 @@
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
-
31
- // Author: kenton@google.com (Kenton Varda)
32
- // Based on original Protocol Buffers design by
33
- // Sanjay Ghemawat, Jeff Dean, and others.
34
- //
35
- // This file contains common implementations of the interfaces defined in
36
- // zero_copy_stream.h which are only included in the full (non-lite)
37
- // protobuf library. These implementations include Unix file descriptors
38
- // and C++ iostreams. See also: zero_copy_stream_impl_lite.h
39
-
40
- #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__
41
- #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__
42
-
43
- #include <string>
44
- #include <iosfwd>
45
- #include <google/protobuf/io/zero_copy_stream.h>
46
- #include <google/protobuf/io/zero_copy_stream_impl_lite.h>
47
- #include <google/protobuf/stubs/common.h>
48
-
49
-
50
- namespace google {
51
- namespace protobuf {
52
- namespace io {
53
-
54
-
55
- // ===================================================================
56
-
57
- // A ZeroCopyInputStream which reads from a file descriptor.
58
- //
59
- // FileInputStream is preferred over using an ifstream with IstreamInputStream.
60
- // The latter will introduce an extra layer of buffering, harming performance.
61
- // Also, it's conceivable that FileInputStream could someday be enhanced
62
- // to use zero-copy file descriptors on OSs which support them.
63
- class LIBPROTOBUF_EXPORT FileInputStream : public ZeroCopyInputStream {
64
- public:
65
- // Creates a stream that reads from the given Unix file descriptor.
66
- // If a block_size is given, it specifies the number of bytes that
67
- // should be read and returned with each call to Next(). Otherwise,
68
- // a reasonable default is used.
69
- explicit FileInputStream(int file_descriptor, int block_size = -1);
70
- ~FileInputStream();
71
-
72
- // Flushes any buffers and closes the underlying file. Returns false if
73
- // an error occurs during the process; use GetErrno() to examine the error.
74
- // Even if an error occurs, the file descriptor is closed when this returns.
75
- bool Close();
76
-
77
- // By default, the file descriptor is not closed when the stream is
78
- // destroyed. Call SetCloseOnDelete(true) to change that. WARNING:
79
- // This leaves no way for the caller to detect if close() fails. If
80
- // detecting close() errors is important to you, you should arrange
81
- // to close the descriptor yourself.
82
- void SetCloseOnDelete(bool value) { copying_input_.SetCloseOnDelete(value); }
83
-
84
- // If an I/O error has occurred on this file descriptor, this is the
85
- // errno from that error. Otherwise, this is zero. Once an error
86
- // occurs, the stream is broken and all subsequent operations will
87
- // fail.
88
- int GetErrno() { return copying_input_.GetErrno(); }
89
-
90
- // implements ZeroCopyInputStream ----------------------------------
91
- bool Next(const void** data, int* size);
92
- void BackUp(int count);
93
- bool Skip(int count);
94
- int64 ByteCount() const;
95
-
96
- private:
97
- class LIBPROTOBUF_EXPORT CopyingFileInputStream : public CopyingInputStream {
98
- public:
99
- CopyingFileInputStream(int file_descriptor);
100
- ~CopyingFileInputStream();
101
-
102
- bool Close();
103
- void SetCloseOnDelete(bool value) { close_on_delete_ = value; }
104
- int GetErrno() { return errno_; }
105
-
106
- // implements CopyingInputStream ---------------------------------
107
- int Read(void* buffer, int size);
108
- int Skip(int count);
109
-
110
- private:
111
- // The file descriptor.
112
- const int file_;
113
- bool close_on_delete_;
114
- bool is_closed_;
115
-
116
- // The errno of the I/O error, if one has occurred. Otherwise, zero.
117
- int errno_;
118
-
119
- // Did we try to seek once and fail? If so, we assume this file descriptor
120
- // doesn't support seeking and won't try again.
121
- bool previous_seek_failed_;
122
-
123
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingFileInputStream);
124
- };
125
-
126
- CopyingFileInputStream copying_input_;
127
- CopyingInputStreamAdaptor impl_;
128
-
129
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileInputStream);
130
- };
131
-
132
- // ===================================================================
133
-
134
- // A ZeroCopyOutputStream which writes to a file descriptor.
135
- //
136
- // FileOutputStream is preferred over using an ofstream with
137
- // OstreamOutputStream. The latter will introduce an extra layer of buffering,
138
- // harming performance. Also, it's conceivable that FileOutputStream could
139
- // someday be enhanced to use zero-copy file descriptors on OSs which
140
- // support them.
141
- class LIBPROTOBUF_EXPORT FileOutputStream : public ZeroCopyOutputStream {
142
- public:
143
- // Creates a stream that writes to the given Unix file descriptor.
144
- // If a block_size is given, it specifies the size of the buffers
145
- // that should be returned by Next(). Otherwise, a reasonable default
146
- // is used.
147
- explicit FileOutputStream(int file_descriptor, int block_size = -1);
148
- ~FileOutputStream();
149
-
150
- // Flushes any buffers and closes the underlying file. Returns false if
151
- // an error occurs during the process; use GetErrno() to examine the error.
152
- // Even if an error occurs, the file descriptor is closed when this returns.
153
- bool Close();
154
-
155
- // Flushes FileOutputStream's buffers but does not close the
156
- // underlying file. No special measures are taken to ensure that
157
- // underlying operating system file object is synchronized to disk.
158
- bool Flush();
159
-
160
- // By default, the file descriptor is not closed when the stream is
161
- // destroyed. Call SetCloseOnDelete(true) to change that. WARNING:
162
- // This leaves no way for the caller to detect if close() fails. If
163
- // detecting close() errors is important to you, you should arrange
164
- // to close the descriptor yourself.
165
- void SetCloseOnDelete(bool value) { copying_output_.SetCloseOnDelete(value); }
166
-
167
- // If an I/O error has occurred on this file descriptor, this is the
168
- // errno from that error. Otherwise, this is zero. Once an error
169
- // occurs, the stream is broken and all subsequent operations will
170
- // fail.
171
- int GetErrno() { return copying_output_.GetErrno(); }
172
-
173
- // implements ZeroCopyOutputStream ---------------------------------
174
- bool Next(void** data, int* size);
175
- void BackUp(int count);
176
- int64 ByteCount() const;
177
-
178
- private:
179
- class LIBPROTOBUF_EXPORT CopyingFileOutputStream : public CopyingOutputStream {
180
- public:
181
- CopyingFileOutputStream(int file_descriptor);
182
- ~CopyingFileOutputStream();
183
-
184
- bool Close();
185
- void SetCloseOnDelete(bool value) { close_on_delete_ = value; }
186
- int GetErrno() { return errno_; }
187
-
188
- // implements CopyingOutputStream --------------------------------
189
- bool Write(const void* buffer, int size);
190
-
191
- private:
192
- // The file descriptor.
193
- const int file_;
194
- bool close_on_delete_;
195
- bool is_closed_;
196
-
197
- // The errno of the I/O error, if one has occurred. Otherwise, zero.
198
- int errno_;
199
-
200
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingFileOutputStream);
201
- };
202
-
203
- CopyingFileOutputStream copying_output_;
204
- CopyingOutputStreamAdaptor impl_;
205
-
206
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileOutputStream);
207
- };
208
-
209
- // ===================================================================
210
-
211
- // A ZeroCopyInputStream which reads from a C++ istream.
212
- //
213
- // Note that for reading files (or anything represented by a file descriptor),
214
- // FileInputStream is more efficient.
215
- class LIBPROTOBUF_EXPORT IstreamInputStream : public ZeroCopyInputStream {
216
- public:
217
- // Creates a stream that reads from the given C++ istream.
218
- // If a block_size is given, it specifies the number of bytes that
219
- // should be read and returned with each call to Next(). Otherwise,
220
- // a reasonable default is used.
221
- explicit IstreamInputStream(istream* stream, int block_size = -1);
222
- ~IstreamInputStream();
223
-
224
- // implements ZeroCopyInputStream ----------------------------------
225
- bool Next(const void** data, int* size);
226
- void BackUp(int count);
227
- bool Skip(int count);
228
- int64 ByteCount() const;
229
-
230
- private:
231
- class LIBPROTOBUF_EXPORT CopyingIstreamInputStream : public CopyingInputStream {
232
- public:
233
- CopyingIstreamInputStream(istream* input);
234
- ~CopyingIstreamInputStream();
235
-
236
- // implements CopyingInputStream ---------------------------------
237
- int Read(void* buffer, int size);
238
- // (We use the default implementation of Skip().)
239
-
240
- private:
241
- // The stream.
242
- istream* input_;
243
-
244
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingIstreamInputStream);
245
- };
246
-
247
- CopyingIstreamInputStream copying_input_;
248
- CopyingInputStreamAdaptor impl_;
249
-
250
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(IstreamInputStream);
251
- };
252
-
253
- // ===================================================================
254
-
255
- // A ZeroCopyOutputStream which writes to a C++ ostream.
256
- //
257
- // Note that for writing files (or anything represented by a file descriptor),
258
- // FileOutputStream is more efficient.
259
- class LIBPROTOBUF_EXPORT OstreamOutputStream : public ZeroCopyOutputStream {
260
- public:
261
- // Creates a stream that writes to the given C++ ostream.
262
- // If a block_size is given, it specifies the size of the buffers
263
- // that should be returned by Next(). Otherwise, a reasonable default
264
- // is used.
265
- explicit OstreamOutputStream(ostream* stream, int block_size = -1);
266
- ~OstreamOutputStream();
267
-
268
- // implements ZeroCopyOutputStream ---------------------------------
269
- bool Next(void** data, int* size);
270
- void BackUp(int count);
271
- int64 ByteCount() const;
272
-
273
- private:
274
- class LIBPROTOBUF_EXPORT CopyingOstreamOutputStream : public CopyingOutputStream {
275
- public:
276
- CopyingOstreamOutputStream(ostream* output);
277
- ~CopyingOstreamOutputStream();
278
-
279
- // implements CopyingOutputStream --------------------------------
280
- bool Write(const void* buffer, int size);
281
-
282
- private:
283
- // The stream.
284
- ostream* output_;
285
-
286
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOstreamOutputStream);
287
- };
288
-
289
- CopyingOstreamOutputStream copying_output_;
290
- CopyingOutputStreamAdaptor impl_;
291
-
292
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OstreamOutputStream);
293
- };
294
-
295
- // ===================================================================
296
-
297
- // A ZeroCopyInputStream which reads from several other streams in sequence.
298
- // ConcatenatingInputStream is unable to distinguish between end-of-stream
299
- // and read errors in the underlying streams, so it assumes any errors mean
300
- // end-of-stream. So, if the underlying streams fail for any other reason,
301
- // ConcatenatingInputStream may do odd things. It is suggested that you do
302
- // not use ConcatenatingInputStream on streams that might produce read errors
303
- // other than end-of-stream.
304
- class LIBPROTOBUF_EXPORT ConcatenatingInputStream : public ZeroCopyInputStream {
305
- public:
306
- // All streams passed in as well as the array itself must remain valid
307
- // until the ConcatenatingInputStream is destroyed.
308
- ConcatenatingInputStream(ZeroCopyInputStream* const streams[], int count);
309
- ~ConcatenatingInputStream();
310
-
311
- // implements ZeroCopyInputStream ----------------------------------
312
- bool Next(const void** data, int* size);
313
- void BackUp(int count);
314
- bool Skip(int count);
315
- int64 ByteCount() const;
316
-
317
-
318
- private:
319
- // As streams are retired, streams_ is incremented and count_ is
320
- // decremented.
321
- ZeroCopyInputStream* const* streams_;
322
- int stream_count_;
323
- int64 bytes_retired_; // Bytes read from previous streams.
324
-
325
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ConcatenatingInputStream);
326
- };
327
-
328
- // ===================================================================
329
-
330
- // A ZeroCopyInputStream which wraps some other stream and limits it to
331
- // a particular byte count.
332
- class LIBPROTOBUF_EXPORT LimitingInputStream : public ZeroCopyInputStream {
333
- public:
334
- LimitingInputStream(ZeroCopyInputStream* input, int64 limit);
335
- ~LimitingInputStream();
336
-
337
- // implements ZeroCopyInputStream ----------------------------------
338
- bool Next(const void** data, int* size);
339
- void BackUp(int count);
340
- bool Skip(int count);
341
- int64 ByteCount() const;
342
-
343
-
344
- private:
345
- ZeroCopyInputStream* input_;
346
- int64 limit_; // Decreases as we go, becomes negative if we overshoot.
347
-
348
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(LimitingInputStream);
349
- };
350
-
351
- // ===================================================================
352
-
353
- } // namespace io
354
- } // namespace protobuf
355
-
356
- } // namespace google
357
- #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_H__