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,340 +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 included in the "lite" protobuf library.
37
- // These implementations cover I/O on raw arrays and strings, as well as
38
- // adaptors which make it easy to implement streams based on traditional
39
- // streams. Of course, many users will probably want to write their own
40
- // implementations of these interfaces specific to the particular I/O
41
- // abstractions they prefer to use, but these should cover the most common
42
- // cases.
43
-
44
- #ifndef GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
45
- #define GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
46
-
47
- #include <string>
48
- #include <iosfwd>
49
- #include <google/protobuf/io/zero_copy_stream.h>
50
- #include <google/protobuf/stubs/common.h>
51
-
52
-
53
- namespace google {
54
- namespace protobuf {
55
- namespace io {
56
-
57
- // ===================================================================
58
-
59
- // A ZeroCopyInputStream backed by an in-memory array of bytes.
60
- class LIBPROTOBUF_EXPORT ArrayInputStream : public ZeroCopyInputStream {
61
- public:
62
- // Create an InputStream that returns the bytes pointed to by "data".
63
- // "data" remains the property of the caller but must remain valid until
64
- // the stream is destroyed. If a block_size is given, calls to Next()
65
- // will return data blocks no larger than the given size. Otherwise, the
66
- // first call to Next() returns the entire array. block_size is mainly
67
- // useful for testing; in production you would probably never want to set
68
- // it.
69
- ArrayInputStream(const void* data, int size, int block_size = -1);
70
- ~ArrayInputStream();
71
-
72
- // implements ZeroCopyInputStream ----------------------------------
73
- bool Next(const void** data, int* size);
74
- void BackUp(int count);
75
- bool Skip(int count);
76
- int64 ByteCount() const;
77
-
78
-
79
- private:
80
- const uint8* const data_; // The byte array.
81
- const int size_; // Total size of the array.
82
- const int block_size_; // How many bytes to return at a time.
83
-
84
- int position_;
85
- int last_returned_size_; // How many bytes we returned last time Next()
86
- // was called (used for error checking only).
87
-
88
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayInputStream);
89
- };
90
-
91
- // ===================================================================
92
-
93
- // A ZeroCopyOutputStream backed by an in-memory array of bytes.
94
- class LIBPROTOBUF_EXPORT ArrayOutputStream : public ZeroCopyOutputStream {
95
- public:
96
- // Create an OutputStream that writes to the bytes pointed to by "data".
97
- // "data" remains the property of the caller but must remain valid until
98
- // the stream is destroyed. If a block_size is given, calls to Next()
99
- // will return data blocks no larger than the given size. Otherwise, the
100
- // first call to Next() returns the entire array. block_size is mainly
101
- // useful for testing; in production you would probably never want to set
102
- // it.
103
- ArrayOutputStream(void* data, int size, int block_size = -1);
104
- ~ArrayOutputStream();
105
-
106
- // implements ZeroCopyOutputStream ---------------------------------
107
- bool Next(void** data, int* size);
108
- void BackUp(int count);
109
- int64 ByteCount() const;
110
-
111
- private:
112
- uint8* const data_; // The byte array.
113
- const int size_; // Total size of the array.
114
- const int block_size_; // How many bytes to return at a time.
115
-
116
- int position_;
117
- int last_returned_size_; // How many bytes we returned last time Next()
118
- // was called (used for error checking only).
119
-
120
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ArrayOutputStream);
121
- };
122
-
123
- // ===================================================================
124
-
125
- // A ZeroCopyOutputStream which appends bytes to a string.
126
- class LIBPROTOBUF_EXPORT StringOutputStream : public ZeroCopyOutputStream {
127
- public:
128
- // Create a StringOutputStream which appends bytes to the given string.
129
- // The string remains property of the caller, but it MUST NOT be accessed
130
- // in any way until the stream is destroyed.
131
- //
132
- // Hint: If you call target->reserve(n) before creating the stream,
133
- // the first call to Next() will return at least n bytes of buffer
134
- // space.
135
- explicit StringOutputStream(string* target);
136
- ~StringOutputStream();
137
-
138
- // implements ZeroCopyOutputStream ---------------------------------
139
- bool Next(void** data, int* size);
140
- void BackUp(int count);
141
- int64 ByteCount() const;
142
-
143
- private:
144
- static const int kMinimumSize = 16;
145
-
146
- string* target_;
147
-
148
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(StringOutputStream);
149
- };
150
-
151
- // Note: There is no StringInputStream. Instead, just create an
152
- // ArrayInputStream as follows:
153
- // ArrayInputStream input(str.data(), str.size());
154
-
155
- // ===================================================================
156
-
157
- // A generic traditional input stream interface.
158
- //
159
- // Lots of traditional input streams (e.g. file descriptors, C stdio
160
- // streams, and C++ iostreams) expose an interface where every read
161
- // involves copying bytes into a buffer. If you want to take such an
162
- // interface and make a ZeroCopyInputStream based on it, simply implement
163
- // CopyingInputStream and then use CopyingInputStreamAdaptor.
164
- //
165
- // CopyingInputStream implementations should avoid buffering if possible.
166
- // CopyingInputStreamAdaptor does its own buffering and will read data
167
- // in large blocks.
168
- class LIBPROTOBUF_EXPORT CopyingInputStream {
169
- public:
170
- virtual ~CopyingInputStream();
171
-
172
- // Reads up to "size" bytes into the given buffer. Returns the number of
173
- // bytes read. Read() waits until at least one byte is available, or
174
- // returns zero if no bytes will ever become available (EOF), or -1 if a
175
- // permanent read error occurred.
176
- virtual int Read(void* buffer, int size) = 0;
177
-
178
- // Skips the next "count" bytes of input. Returns the number of bytes
179
- // actually skipped. This will always be exactly equal to "count" unless
180
- // EOF was reached or a permanent read error occurred.
181
- //
182
- // The default implementation just repeatedly calls Read() into a scratch
183
- // buffer.
184
- virtual int Skip(int count);
185
- };
186
-
187
- // A ZeroCopyInputStream which reads from a CopyingInputStream. This is
188
- // useful for implementing ZeroCopyInputStreams that read from traditional
189
- // streams. Note that this class is not really zero-copy.
190
- //
191
- // If you want to read from file descriptors or C++ istreams, this is
192
- // already implemented for you: use FileInputStream or IstreamInputStream
193
- // respectively.
194
- class LIBPROTOBUF_EXPORT CopyingInputStreamAdaptor : public ZeroCopyInputStream {
195
- public:
196
- // Creates a stream that reads from the given CopyingInputStream.
197
- // If a block_size is given, it specifies the number of bytes that
198
- // should be read and returned with each call to Next(). Otherwise,
199
- // a reasonable default is used. The caller retains ownership of
200
- // copying_stream unless SetOwnsCopyingStream(true) is called.
201
- explicit CopyingInputStreamAdaptor(CopyingInputStream* copying_stream,
202
- int block_size = -1);
203
- ~CopyingInputStreamAdaptor();
204
-
205
- // Call SetOwnsCopyingStream(true) to tell the CopyingInputStreamAdaptor to
206
- // delete the underlying CopyingInputStream when it is destroyed.
207
- void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
208
-
209
- // implements ZeroCopyInputStream ----------------------------------
210
- bool Next(const void** data, int* size);
211
- void BackUp(int count);
212
- bool Skip(int count);
213
- int64 ByteCount() const;
214
-
215
- private:
216
- // Insures that buffer_ is not NULL.
217
- void AllocateBufferIfNeeded();
218
- // Frees the buffer and resets buffer_used_.
219
- void FreeBuffer();
220
-
221
- // The underlying copying stream.
222
- CopyingInputStream* copying_stream_;
223
- bool owns_copying_stream_;
224
-
225
- // True if we have seen a permenant error from the underlying stream.
226
- bool failed_;
227
-
228
- // The current position of copying_stream_, relative to the point where
229
- // we started reading.
230
- int64 position_;
231
-
232
- // Data is read into this buffer. It may be NULL if no buffer is currently
233
- // in use. Otherwise, it points to an array of size buffer_size_.
234
- scoped_array<uint8> buffer_;
235
- const int buffer_size_;
236
-
237
- // Number of valid bytes currently in the buffer (i.e. the size last
238
- // returned by Next()). 0 <= buffer_used_ <= buffer_size_.
239
- int buffer_used_;
240
-
241
- // Number of bytes in the buffer which were backed up over by a call to
242
- // BackUp(). These need to be returned again.
243
- // 0 <= backup_bytes_ <= buffer_used_
244
- int backup_bytes_;
245
-
246
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingInputStreamAdaptor);
247
- };
248
-
249
- // ===================================================================
250
-
251
- // A generic traditional output stream interface.
252
- //
253
- // Lots of traditional output streams (e.g. file descriptors, C stdio
254
- // streams, and C++ iostreams) expose an interface where every write
255
- // involves copying bytes from a buffer. If you want to take such an
256
- // interface and make a ZeroCopyOutputStream based on it, simply implement
257
- // CopyingOutputStream and then use CopyingOutputStreamAdaptor.
258
- //
259
- // CopyingOutputStream implementations should avoid buffering if possible.
260
- // CopyingOutputStreamAdaptor does its own buffering and will write data
261
- // in large blocks.
262
- class LIBPROTOBUF_EXPORT CopyingOutputStream {
263
- public:
264
- virtual ~CopyingOutputStream();
265
-
266
- // Writes "size" bytes from the given buffer to the output. Returns true
267
- // if successful, false on a write error.
268
- virtual bool Write(const void* buffer, int size) = 0;
269
- };
270
-
271
- // A ZeroCopyOutputStream which writes to a CopyingOutputStream. This is
272
- // useful for implementing ZeroCopyOutputStreams that write to traditional
273
- // streams. Note that this class is not really zero-copy.
274
- //
275
- // If you want to write to file descriptors or C++ ostreams, this is
276
- // already implemented for you: use FileOutputStream or OstreamOutputStream
277
- // respectively.
278
- class LIBPROTOBUF_EXPORT CopyingOutputStreamAdaptor : public ZeroCopyOutputStream {
279
- public:
280
- // Creates a stream that writes to the given Unix file descriptor.
281
- // If a block_size is given, it specifies the size of the buffers
282
- // that should be returned by Next(). Otherwise, a reasonable default
283
- // is used.
284
- explicit CopyingOutputStreamAdaptor(CopyingOutputStream* copying_stream,
285
- int block_size = -1);
286
- ~CopyingOutputStreamAdaptor();
287
-
288
- // Writes all pending data to the underlying stream. Returns false if a
289
- // write error occurred on the underlying stream. (The underlying
290
- // stream itself is not necessarily flushed.)
291
- bool Flush();
292
-
293
- // Call SetOwnsCopyingStream(true) to tell the CopyingOutputStreamAdaptor to
294
- // delete the underlying CopyingOutputStream when it is destroyed.
295
- void SetOwnsCopyingStream(bool value) { owns_copying_stream_ = value; }
296
-
297
- // implements ZeroCopyOutputStream ---------------------------------
298
- bool Next(void** data, int* size);
299
- void BackUp(int count);
300
- int64 ByteCount() const;
301
-
302
- private:
303
- // Write the current buffer, if it is present.
304
- bool WriteBuffer();
305
- // Insures that buffer_ is not NULL.
306
- void AllocateBufferIfNeeded();
307
- // Frees the buffer.
308
- void FreeBuffer();
309
-
310
- // The underlying copying stream.
311
- CopyingOutputStream* copying_stream_;
312
- bool owns_copying_stream_;
313
-
314
- // True if we have seen a permenant error from the underlying stream.
315
- bool failed_;
316
-
317
- // The current position of copying_stream_, relative to the point where
318
- // we started writing.
319
- int64 position_;
320
-
321
- // Data is written from this buffer. It may be NULL if no buffer is
322
- // currently in use. Otherwise, it points to an array of size buffer_size_.
323
- scoped_array<uint8> buffer_;
324
- const int buffer_size_;
325
-
326
- // Number of valid bytes currently in the buffer (i.e. the size last
327
- // returned by Next()). When BackUp() is called, we just reduce this.
328
- // 0 <= buffer_used_ <= buffer_size_.
329
- int buffer_used_;
330
-
331
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CopyingOutputStreamAdaptor);
332
- };
333
-
334
- // ===================================================================
335
-
336
- } // namespace io
337
- } // namespace protobuf
338
-
339
- } // namespace google
340
- #endif // GOOGLE_PROTOBUF_IO_ZERO_COPY_STREAM_IMPL_LITE_H__
@@ -1,692 +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
- // Defines Message, the abstract interface implemented by non-lite
36
- // protocol message objects. Although it's possible to implement this
37
- // interface manually, most users will use the protocol compiler to
38
- // generate implementations.
39
- //
40
- // Example usage:
41
- //
42
- // Say you have a message defined as:
43
- //
44
- // message Foo {
45
- // optional string text = 1;
46
- // repeated int32 numbers = 2;
47
- // }
48
- //
49
- // Then, if you used the protocol compiler to generate a class from the above
50
- // definition, you could use it like so:
51
- //
52
- // string data; // Will store a serialized version of the message.
53
- //
54
- // {
55
- // // Create a message and serialize it.
56
- // Foo foo;
57
- // foo.set_text("Hello World!");
58
- // foo.add_numbers(1);
59
- // foo.add_numbers(5);
60
- // foo.add_numbers(42);
61
- //
62
- // foo.SerializeToString(&data);
63
- // }
64
- //
65
- // {
66
- // // Parse the serialized message and check that it contains the
67
- // // correct data.
68
- // Foo foo;
69
- // foo.ParseFromString(data);
70
- //
71
- // assert(foo.text() == "Hello World!");
72
- // assert(foo.numbers_size() == 3);
73
- // assert(foo.numbers(0) == 1);
74
- // assert(foo.numbers(1) == 5);
75
- // assert(foo.numbers(2) == 42);
76
- // }
77
- //
78
- // {
79
- // // Same as the last block, but do it dynamically via the Message
80
- // // reflection interface.
81
- // Message* foo = new Foo;
82
- // Descriptor* descriptor = foo->GetDescriptor();
83
- //
84
- // // Get the descriptors for the fields we're interested in and verify
85
- // // their types.
86
- // FieldDescriptor* text_field = descriptor->FindFieldByName("text");
87
- // assert(text_field != NULL);
88
- // assert(text_field->type() == FieldDescriptor::TYPE_STRING);
89
- // assert(text_field->label() == FieldDescriptor::TYPE_OPTIONAL);
90
- // FieldDescriptor* numbers_field = descriptor->FindFieldByName("numbers");
91
- // assert(numbers_field != NULL);
92
- // assert(numbers_field->type() == FieldDescriptor::TYPE_INT32);
93
- // assert(numbers_field->label() == FieldDescriptor::TYPE_REPEATED);
94
- //
95
- // // Parse the message.
96
- // foo->ParseFromString(data);
97
- //
98
- // // Use the reflection interface to examine the contents.
99
- // const Reflection* reflection = foo->GetReflection();
100
- // assert(reflection->GetString(foo, text_field) == "Hello World!");
101
- // assert(reflection->FieldSize(foo, numbers_field) == 3);
102
- // assert(reflection->GetRepeatedInt32(foo, numbers_field, 0) == 1);
103
- // assert(reflection->GetRepeatedInt32(foo, numbers_field, 1) == 5);
104
- // assert(reflection->GetRepeatedInt32(foo, numbers_field, 2) == 42);
105
- //
106
- // delete foo;
107
- // }
108
-
109
- #ifndef GOOGLE_PROTOBUF_MESSAGE_H__
110
- #define GOOGLE_PROTOBUF_MESSAGE_H__
111
-
112
- #include <vector>
113
- #include <string>
114
-
115
- #ifdef __DECCXX
116
- // HP C++'s iosfwd doesn't work.
117
- #include <iostream>
118
- #else
119
- #include <iosfwd>
120
- #endif
121
-
122
- #include <google/protobuf/message_lite.h>
123
-
124
- #include <google/protobuf/stubs/common.h>
125
-
126
-
127
- namespace google {
128
- namespace protobuf {
129
-
130
- // Defined in this file.
131
- class Message;
132
- class Reflection;
133
- class MessageFactory;
134
-
135
- // Defined in other files.
136
- class Descriptor; // descriptor.h
137
- class FieldDescriptor; // descriptor.h
138
- class EnumDescriptor; // descriptor.h
139
- class EnumValueDescriptor; // descriptor.h
140
- namespace io {
141
- class ZeroCopyInputStream; // zero_copy_stream.h
142
- class ZeroCopyOutputStream; // zero_copy_stream.h
143
- class CodedInputStream; // coded_stream.h
144
- class CodedOutputStream; // coded_stream.h
145
- }
146
- class UnknownFieldSet; // unknown_field_set.h
147
-
148
- // A container to hold message metadata.
149
- struct Metadata {
150
- const Descriptor* descriptor;
151
- const Reflection* reflection;
152
- };
153
-
154
- // Returns the EnumDescriptor for enum type E, which must be a
155
- // proto-declared enum type. Code generated by the protocol compiler
156
- // will include specializations of this template for each enum type declared.
157
- template <typename E>
158
- const EnumDescriptor* GetEnumDescriptor();
159
-
160
- // Abstract interface for protocol messages.
161
- //
162
- // See also MessageLite, which contains most every-day operations. Message
163
- // adds descriptors and reflection on top of that.
164
- //
165
- // The methods of this class that are virtual but not pure-virtual have
166
- // default implementations based on reflection. Message classes which are
167
- // optimized for speed will want to override these with faster implementations,
168
- // but classes optimized for code size may be happy with keeping them. See
169
- // the optimize_for option in descriptor.proto.
170
- class LIBPROTOBUF_EXPORT Message : public MessageLite {
171
- public:
172
- inline Message() {}
173
- virtual ~Message();
174
-
175
- // Basic Operations ------------------------------------------------
176
-
177
- // Construct a new instance of the same type. Ownership is passed to the
178
- // caller. (This is also defined in MessageLite, but is defined again here
179
- // for return-type covariance.)
180
- virtual Message* New() const = 0;
181
-
182
- // Make this message into a copy of the given message. The given message
183
- // must have the same descriptor, but need not necessarily be the same class.
184
- // By default this is just implemented as "Clear(); MergeFrom(from);".
185
- virtual void CopyFrom(const Message& from);
186
-
187
- // Merge the fields from the given message into this message. Singular
188
- // fields will be overwritten, except for embedded messages which will
189
- // be merged. Repeated fields will be concatenated. The given message
190
- // must be of the same type as this message (i.e. the exact same class).
191
- virtual void MergeFrom(const Message& from);
192
-
193
- // Verifies that IsInitialized() returns true. GOOGLE_CHECK-fails otherwise, with
194
- // a nice error message.
195
- void CheckInitialized() const;
196
-
197
- // Slowly build a list of all required fields that are not set.
198
- // This is much, much slower than IsInitialized() as it is implemented
199
- // purely via reflection. Generally, you should not call this unless you
200
- // have already determined that an error exists by calling IsInitialized().
201
- void FindInitializationErrors(vector<string>* errors) const;
202
-
203
- // Like FindInitializationErrors, but joins all the strings, delimited by
204
- // commas, and returns them.
205
- string InitializationErrorString() const;
206
-
207
- // Clears all unknown fields from this message and all embedded messages.
208
- // Normally, if unknown tag numbers are encountered when parsing a message,
209
- // the tag and value are stored in the message's UnknownFieldSet and
210
- // then written back out when the message is serialized. This allows servers
211
- // which simply route messages to other servers to pass through messages
212
- // that have new field definitions which they don't yet know about. However,
213
- // this behavior can have security implications. To avoid it, call this
214
- // method after parsing.
215
- //
216
- // See Reflection::GetUnknownFields() for more on unknown fields.
217
- virtual void DiscardUnknownFields();
218
-
219
- // Computes (an estimate of) the total number of bytes currently used for
220
- // storing the message in memory. The default implementation calls the
221
- // Reflection object's SpaceUsed() method.
222
- virtual int SpaceUsed() const;
223
-
224
- // Debugging & Testing----------------------------------------------
225
-
226
- // Generates a human readable form of this message, useful for debugging
227
- // and other purposes.
228
- string DebugString() const;
229
- // Like DebugString(), but with less whitespace.
230
- string ShortDebugString() const;
231
- // Like DebugString(), but do not escape UTF-8 byte sequences.
232
- string Utf8DebugString() const;
233
- // Convenience function useful in GDB. Prints DebugString() to stdout.
234
- void PrintDebugString() const;
235
-
236
- // Heavy I/O -------------------------------------------------------
237
- // Additional parsing and serialization methods not implemented by
238
- // MessageLite because they are not supported by the lite library.
239
-
240
- // Parse a protocol buffer from a file descriptor. If successful, the entire
241
- // input will be consumed.
242
- bool ParseFromFileDescriptor(int file_descriptor);
243
- // Like ParseFromFileDescriptor(), but accepts messages that are missing
244
- // required fields.
245
- bool ParsePartialFromFileDescriptor(int file_descriptor);
246
- // Parse a protocol buffer from a C++ istream. If successful, the entire
247
- // input will be consumed.
248
- bool ParseFromIstream(istream* input);
249
- // Like ParseFromIstream(), but accepts messages that are missing
250
- // required fields.
251
- bool ParsePartialFromIstream(istream* input);
252
-
253
- // Serialize the message and write it to the given file descriptor. All
254
- // required fields must be set.
255
- bool SerializeToFileDescriptor(int file_descriptor) const;
256
- // Like SerializeToFileDescriptor(), but allows missing required fields.
257
- bool SerializePartialToFileDescriptor(int file_descriptor) const;
258
- // Serialize the message and write it to the given C++ ostream. All
259
- // required fields must be set.
260
- bool SerializeToOstream(ostream* output) const;
261
- // Like SerializeToOstream(), but allows missing required fields.
262
- bool SerializePartialToOstream(ostream* output) const;
263
-
264
-
265
- // Reflection-based methods ----------------------------------------
266
- // These methods are pure-virtual in MessageLite, but Message provides
267
- // reflection-based default implementations.
268
-
269
- virtual string GetTypeName() const;
270
- virtual void Clear();
271
- virtual bool IsInitialized() const;
272
- virtual void CheckTypeAndMergeFrom(const MessageLite& other);
273
- virtual bool MergePartialFromCodedStream(io::CodedInputStream* input);
274
- virtual int ByteSize() const;
275
- virtual void SerializeWithCachedSizes(io::CodedOutputStream* output) const;
276
-
277
- private:
278
- // This is called only by the default implementation of ByteSize(), to
279
- // update the cached size. If you override ByteSize(), you do not need
280
- // to override this. If you do not override ByteSize(), you MUST override
281
- // this; the default implementation will crash.
282
- //
283
- // The method is private because subclasses should never call it; only
284
- // override it. Yes, C++ lets you do that. Crazy, huh?
285
- virtual void SetCachedSize(int size) const;
286
-
287
- public:
288
-
289
- // Introspection ---------------------------------------------------
290
-
291
- // Typedef for backwards-compatibility.
292
- typedef google::protobuf::Reflection Reflection;
293
-
294
- // Get a Descriptor for this message's type. This describes what
295
- // fields the message contains, the types of those fields, etc.
296
- const Descriptor* GetDescriptor() const { return GetMetadata().descriptor; }
297
-
298
- // Get the Reflection interface for this Message, which can be used to
299
- // read and modify the fields of the Message dynamically (in other words,
300
- // without knowing the message type at compile time). This object remains
301
- // property of the Message.
302
- //
303
- // This method remains virtual in case a subclass does not implement
304
- // reflection and wants to override the default behavior.
305
- virtual const Reflection* GetReflection() const {
306
- return GetMetadata().reflection;
307
- }
308
-
309
- protected:
310
- // Get a struct containing the metadata for the Message. Most subclasses only
311
- // need to implement this method, rather than the GetDescriptor() and
312
- // GetReflection() wrappers.
313
- virtual Metadata GetMetadata() const = 0;
314
-
315
-
316
- private:
317
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Message);
318
- };
319
-
320
- // This interface contains methods that can be used to dynamically access
321
- // and modify the fields of a protocol message. Their semantics are
322
- // similar to the accessors the protocol compiler generates.
323
- //
324
- // To get the Reflection for a given Message, call Message::GetReflection().
325
- //
326
- // This interface is separate from Message only for efficiency reasons;
327
- // the vast majority of implementations of Message will share the same
328
- // implementation of Reflection (GeneratedMessageReflection,
329
- // defined in generated_message.h), and all Messages of a particular class
330
- // should share the same Reflection object (though you should not rely on
331
- // the latter fact).
332
- //
333
- // There are several ways that these methods can be used incorrectly. For
334
- // example, any of the following conditions will lead to undefined
335
- // results (probably assertion failures):
336
- // - The FieldDescriptor is not a field of this message type.
337
- // - The method called is not appropriate for the field's type. For
338
- // each field type in FieldDescriptor::TYPE_*, there is only one
339
- // Get*() method, one Set*() method, and one Add*() method that is
340
- // valid for that type. It should be obvious which (except maybe
341
- // for TYPE_BYTES, which are represented using strings in C++).
342
- // - A Get*() or Set*() method for singular fields is called on a repeated
343
- // field.
344
- // - GetRepeated*(), SetRepeated*(), or Add*() is called on a non-repeated
345
- // field.
346
- // - The Message object passed to any method is not of the right type for
347
- // this Reflection object (i.e. message.GetReflection() != reflection).
348
- //
349
- // You might wonder why there is not any abstract representation for a field
350
- // of arbitrary type. E.g., why isn't there just a "GetField()" method that
351
- // returns "const Field&", where "Field" is some class with accessors like
352
- // "GetInt32Value()". The problem is that someone would have to deal with
353
- // allocating these Field objects. For generated message classes, having to
354
- // allocate space for an additional object to wrap every field would at least
355
- // double the message's memory footprint, probably worse. Allocating the
356
- // objects on-demand, on the other hand, would be expensive and prone to
357
- // memory leaks. So, instead we ended up with this flat interface.
358
- //
359
- // TODO(kenton): Create a utility class which callers can use to read and
360
- // write fields from a Reflection without paying attention to the type.
361
- class LIBPROTOBUF_EXPORT Reflection {
362
- public:
363
- // TODO(kenton): Remove parameter.
364
- inline Reflection() {}
365
- virtual ~Reflection();
366
-
367
- // Get the UnknownFieldSet for the message. This contains fields which
368
- // were seen when the Message was parsed but were not recognized according
369
- // to the Message's definition.
370
- virtual const UnknownFieldSet& GetUnknownFields(
371
- const Message& message) const = 0;
372
- // Get a mutable pointer to the UnknownFieldSet for the message. This
373
- // contains fields which were seen when the Message was parsed but were not
374
- // recognized according to the Message's definition.
375
- virtual UnknownFieldSet* MutableUnknownFields(Message* message) const = 0;
376
-
377
- // Estimate the amount of memory used by the message object.
378
- virtual int SpaceUsed(const Message& message) const = 0;
379
-
380
- // Check if the given non-repeated field is set.
381
- virtual bool HasField(const Message& message,
382
- const FieldDescriptor* field) const = 0;
383
-
384
- // Get the number of elements of a repeated field.
385
- virtual int FieldSize(const Message& message,
386
- const FieldDescriptor* field) const = 0;
387
-
388
- // Clear the value of a field, so that HasField() returns false or
389
- // FieldSize() returns zero.
390
- virtual void ClearField(Message* message,
391
- const FieldDescriptor* field) const = 0;
392
-
393
- // Remove the last element of a repeated field.
394
- // We don't provide a way to remove any element other than the last
395
- // because it invites inefficient use, such as O(n^2) filtering loops
396
- // that should have been O(n). If you want to remove an element other
397
- // than the last, the best way to do it is to re-arrange the elements
398
- // (using Swap()) so that the one you want removed is at the end, then
399
- // call RemoveLast().
400
- virtual void RemoveLast(Message* message,
401
- const FieldDescriptor* field) const = 0;
402
-
403
- // Swap the complete contents of two messages.
404
- virtual void Swap(Message* message1, Message* message2) const = 0;
405
-
406
- // Swap two elements of a repeated field.
407
- virtual void SwapElements(Message* message,
408
- const FieldDescriptor* field,
409
- int index1,
410
- int index2) const = 0;
411
-
412
- // List all fields of the message which are currently set. This includes
413
- // extensions. Singular fields will only be listed if HasField(field) would
414
- // return true and repeated fields will only be listed if FieldSize(field)
415
- // would return non-zero. Fields (both normal fields and extension fields)
416
- // will be listed ordered by field number.
417
- virtual void ListFields(const Message& message,
418
- vector<const FieldDescriptor*>* output) const = 0;
419
-
420
- // Singular field getters ------------------------------------------
421
- // These get the value of a non-repeated field. They return the default
422
- // value for fields that aren't set.
423
-
424
- virtual int32 GetInt32 (const Message& message,
425
- const FieldDescriptor* field) const = 0;
426
- virtual int64 GetInt64 (const Message& message,
427
- const FieldDescriptor* field) const = 0;
428
- virtual uint32 GetUInt32(const Message& message,
429
- const FieldDescriptor* field) const = 0;
430
- virtual uint64 GetUInt64(const Message& message,
431
- const FieldDescriptor* field) const = 0;
432
- virtual float GetFloat (const Message& message,
433
- const FieldDescriptor* field) const = 0;
434
- virtual double GetDouble(const Message& message,
435
- const FieldDescriptor* field) const = 0;
436
- virtual bool GetBool (const Message& message,
437
- const FieldDescriptor* field) const = 0;
438
- virtual string GetString(const Message& message,
439
- const FieldDescriptor* field) const = 0;
440
- virtual const EnumValueDescriptor* GetEnum(
441
- const Message& message, const FieldDescriptor* field) const = 0;
442
- // See MutableMessage() for the meaning of the "factory" parameter.
443
- virtual const Message& GetMessage(const Message& message,
444
- const FieldDescriptor* field,
445
- MessageFactory* factory = NULL) const = 0;
446
-
447
- // Get a string value without copying, if possible.
448
- //
449
- // GetString() necessarily returns a copy of the string. This can be
450
- // inefficient when the string is already stored in a string object in the
451
- // underlying message. GetStringReference() will return a reference to the
452
- // underlying string in this case. Otherwise, it will copy the string into
453
- // *scratch and return that.
454
- //
455
- // Note: It is perfectly reasonable and useful to write code like:
456
- // str = reflection->GetStringReference(field, &str);
457
- // This line would ensure that only one copy of the string is made
458
- // regardless of the field's underlying representation. When initializing
459
- // a newly-constructed string, though, it's just as fast and more readable
460
- // to use code like:
461
- // string str = reflection->GetString(field);
462
- virtual const string& GetStringReference(const Message& message,
463
- const FieldDescriptor* field,
464
- string* scratch) const = 0;
465
-
466
-
467
- // Singular field mutators -----------------------------------------
468
- // These mutate the value of a non-repeated field.
469
-
470
- virtual void SetInt32 (Message* message,
471
- const FieldDescriptor* field, int32 value) const = 0;
472
- virtual void SetInt64 (Message* message,
473
- const FieldDescriptor* field, int64 value) const = 0;
474
- virtual void SetUInt32(Message* message,
475
- const FieldDescriptor* field, uint32 value) const = 0;
476
- virtual void SetUInt64(Message* message,
477
- const FieldDescriptor* field, uint64 value) const = 0;
478
- virtual void SetFloat (Message* message,
479
- const FieldDescriptor* field, float value) const = 0;
480
- virtual void SetDouble(Message* message,
481
- const FieldDescriptor* field, double value) const = 0;
482
- virtual void SetBool (Message* message,
483
- const FieldDescriptor* field, bool value) const = 0;
484
- virtual void SetString(Message* message,
485
- const FieldDescriptor* field,
486
- const string& value) const = 0;
487
- virtual void SetEnum (Message* message,
488
- const FieldDescriptor* field,
489
- const EnumValueDescriptor* value) const = 0;
490
- // Get a mutable pointer to a field with a message type. If a MessageFactory
491
- // is provided, it will be used to construct instances of the sub-message;
492
- // otherwise, the default factory is used. If the field is an extension that
493
- // does not live in the same pool as the containing message's descriptor (e.g.
494
- // it lives in an overlay pool), then a MessageFactory must be provided.
495
- // If you have no idea what that meant, then you probably don't need to worry
496
- // about it (don't provide a MessageFactory). WARNING: If the
497
- // FieldDescriptor is for a compiled-in extension, then
498
- // factory->GetPrototype(field->message_type() MUST return an instance of the
499
- // compiled-in class for this type, NOT DynamicMessage.
500
- virtual Message* MutableMessage(Message* message,
501
- const FieldDescriptor* field,
502
- MessageFactory* factory = NULL) const = 0;
503
-
504
-
505
- // Repeated field getters ------------------------------------------
506
- // These get the value of one element of a repeated field.
507
-
508
- virtual int32 GetRepeatedInt32 (const Message& message,
509
- const FieldDescriptor* field,
510
- int index) const = 0;
511
- virtual int64 GetRepeatedInt64 (const Message& message,
512
- const FieldDescriptor* field,
513
- int index) const = 0;
514
- virtual uint32 GetRepeatedUInt32(const Message& message,
515
- const FieldDescriptor* field,
516
- int index) const = 0;
517
- virtual uint64 GetRepeatedUInt64(const Message& message,
518
- const FieldDescriptor* field,
519
- int index) const = 0;
520
- virtual float GetRepeatedFloat (const Message& message,
521
- const FieldDescriptor* field,
522
- int index) const = 0;
523
- virtual double GetRepeatedDouble(const Message& message,
524
- const FieldDescriptor* field,
525
- int index) const = 0;
526
- virtual bool GetRepeatedBool (const Message& message,
527
- const FieldDescriptor* field,
528
- int index) const = 0;
529
- virtual string GetRepeatedString(const Message& message,
530
- const FieldDescriptor* field,
531
- int index) const = 0;
532
- virtual const EnumValueDescriptor* GetRepeatedEnum(
533
- const Message& message,
534
- const FieldDescriptor* field, int index) const = 0;
535
- virtual const Message& GetRepeatedMessage(
536
- const Message& message,
537
- const FieldDescriptor* field, int index) const = 0;
538
-
539
- // See GetStringReference(), above.
540
- virtual const string& GetRepeatedStringReference(
541
- const Message& message, const FieldDescriptor* field,
542
- int index, string* scratch) const = 0;
543
-
544
-
545
- // Repeated field mutators -----------------------------------------
546
- // These mutate the value of one element of a repeated field.
547
-
548
- virtual void SetRepeatedInt32 (Message* message,
549
- const FieldDescriptor* field,
550
- int index, int32 value) const = 0;
551
- virtual void SetRepeatedInt64 (Message* message,
552
- const FieldDescriptor* field,
553
- int index, int64 value) const = 0;
554
- virtual void SetRepeatedUInt32(Message* message,
555
- const FieldDescriptor* field,
556
- int index, uint32 value) const = 0;
557
- virtual void SetRepeatedUInt64(Message* message,
558
- const FieldDescriptor* field,
559
- int index, uint64 value) const = 0;
560
- virtual void SetRepeatedFloat (Message* message,
561
- const FieldDescriptor* field,
562
- int index, float value) const = 0;
563
- virtual void SetRepeatedDouble(Message* message,
564
- const FieldDescriptor* field,
565
- int index, double value) const = 0;
566
- virtual void SetRepeatedBool (Message* message,
567
- const FieldDescriptor* field,
568
- int index, bool value) const = 0;
569
- virtual void SetRepeatedString(Message* message,
570
- const FieldDescriptor* field,
571
- int index, const string& value) const = 0;
572
- virtual void SetRepeatedEnum(Message* message,
573
- const FieldDescriptor* field, int index,
574
- const EnumValueDescriptor* value) const = 0;
575
- // Get a mutable pointer to an element of a repeated field with a message
576
- // type.
577
- virtual Message* MutableRepeatedMessage(
578
- Message* message, const FieldDescriptor* field, int index) const = 0;
579
-
580
-
581
- // Repeated field adders -------------------------------------------
582
- // These add an element to a repeated field.
583
-
584
- virtual void AddInt32 (Message* message,
585
- const FieldDescriptor* field, int32 value) const = 0;
586
- virtual void AddInt64 (Message* message,
587
- const FieldDescriptor* field, int64 value) const = 0;
588
- virtual void AddUInt32(Message* message,
589
- const FieldDescriptor* field, uint32 value) const = 0;
590
- virtual void AddUInt64(Message* message,
591
- const FieldDescriptor* field, uint64 value) const = 0;
592
- virtual void AddFloat (Message* message,
593
- const FieldDescriptor* field, float value) const = 0;
594
- virtual void AddDouble(Message* message,
595
- const FieldDescriptor* field, double value) const = 0;
596
- virtual void AddBool (Message* message,
597
- const FieldDescriptor* field, bool value) const = 0;
598
- virtual void AddString(Message* message,
599
- const FieldDescriptor* field,
600
- const string& value) const = 0;
601
- virtual void AddEnum (Message* message,
602
- const FieldDescriptor* field,
603
- const EnumValueDescriptor* value) const = 0;
604
- // See MutableMessage() for comments on the "factory" parameter.
605
- virtual Message* AddMessage(Message* message,
606
- const FieldDescriptor* field,
607
- MessageFactory* factory = NULL) const = 0;
608
-
609
-
610
- // Extensions ------------------------------------------------------
611
-
612
- // Try to find an extension of this message type by fully-qualified field
613
- // name. Returns NULL if no extension is known for this name or number.
614
- virtual const FieldDescriptor* FindKnownExtensionByName(
615
- const string& name) const = 0;
616
-
617
- // Try to find an extension of this message type by field number.
618
- // Returns NULL if no extension is known for this name or number.
619
- virtual const FieldDescriptor* FindKnownExtensionByNumber(
620
- int number) const = 0;
621
-
622
- private:
623
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Reflection);
624
- };
625
-
626
- // Abstract interface for a factory for message objects.
627
- class LIBPROTOBUF_EXPORT MessageFactory {
628
- public:
629
- inline MessageFactory() {}
630
- virtual ~MessageFactory();
631
-
632
- // Given a Descriptor, gets or constructs the default (prototype) Message
633
- // of that type. You can then call that message's New() method to construct
634
- // a mutable message of that type.
635
- //
636
- // Calling this method twice with the same Descriptor returns the same
637
- // object. The returned object remains property of the factory. Also, any
638
- // objects created by calling the prototype's New() method share some data
639
- // with the prototype, so these must be destoyed before the MessageFactory
640
- // is destroyed.
641
- //
642
- // The given descriptor must outlive the returned message, and hence must
643
- // outlive the MessageFactory.
644
- //
645
- // Some implementations do not support all types. GetPrototype() will
646
- // return NULL if the descriptor passed in is not supported.
647
- //
648
- // This method may or may not be thread-safe depending on the implementation.
649
- // Each implementation should document its own degree thread-safety.
650
- virtual const Message* GetPrototype(const Descriptor* type) = 0;
651
-
652
- // Gets a MessageFactory which supports all generated, compiled-in messages.
653
- // In other words, for any compiled-in type FooMessage, the following is true:
654
- // MessageFactory::generated_factory()->GetPrototype(
655
- // FooMessage::descriptor()) == FooMessage::default_instance()
656
- // This factory supports all types which are found in
657
- // DescriptorPool::generated_pool(). If given a descriptor from any other
658
- // pool, GetPrototype() will return NULL. (You can also check if a
659
- // descriptor is for a generated message by checking if
660
- // descriptor->file()->pool() == DescriptorPool::generated_pool().)
661
- //
662
- // This factory is 100% thread-safe; calling GetPrototype() does not modify
663
- // any shared data.
664
- //
665
- // This factory is a singleton. The caller must not delete the object.
666
- static MessageFactory* generated_factory();
667
-
668
- // For internal use only: Registers a .proto file at static initialization
669
- // time, to be placed in generated_factory. The first time GetPrototype()
670
- // is called with a descriptor from this file, |register_messages| will be
671
- // called, with the file name as the parameter. It must call
672
- // InternalRegisterGeneratedMessage() (below) to register each message type
673
- // in the file. This strange mechanism is necessary because descriptors are
674
- // built lazily, so we can't register types by their descriptor until we
675
- // know that the descriptor exists. |filename| must be a permanent string.
676
- static void InternalRegisterGeneratedFile(
677
- const char* filename, void (*register_messages)(const string&));
678
-
679
- // For internal use only: Registers a message type. Called only by the
680
- // functions which are registered with InternalRegisterGeneratedFile(),
681
- // above.
682
- static void InternalRegisterGeneratedMessage(const Descriptor* descriptor,
683
- const Message* prototype);
684
-
685
- private:
686
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MessageFactory);
687
- };
688
-
689
- } // namespace protobuf
690
-
691
- } // namespace google
692
- #endif // GOOGLE_PROTOBUF_MESSAGE_H__