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,291 +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
- // DEPRECATED: This module declares the abstract interfaces underlying proto2
36
- // RPC services. These are intented to be independent of any particular RPC
37
- // implementation, so that proto2 services can be used on top of a variety
38
- // of implementations. Starting with version 2.3.0, RPC implementations should
39
- // not try to build on these, but should instead provide code generator plugins
40
- // which generate code specific to the particular RPC implementation. This way
41
- // the generated code can be more appropriate for the implementation in use
42
- // and can avoid unnecessary layers of indirection.
43
- //
44
- //
45
- // When you use the protocol compiler to compile a service definition, it
46
- // generates two classes: An abstract interface for the service (with
47
- // methods matching the service definition) and a "stub" implementation.
48
- // A stub is just a type-safe wrapper around an RpcChannel which emulates a
49
- // local implementation of the service.
50
- //
51
- // For example, the service definition:
52
- // service MyService {
53
- // rpc Foo(MyRequest) returns(MyResponse);
54
- // }
55
- // will generate abstract interface "MyService" and class "MyService::Stub".
56
- // You could implement a MyService as follows:
57
- // class MyServiceImpl : public MyService {
58
- // public:
59
- // MyServiceImpl() {}
60
- // ~MyServiceImpl() {}
61
- //
62
- // // implements MyService ---------------------------------------
63
- //
64
- // void Foo(google::protobuf::RpcController* controller,
65
- // const MyRequest* request,
66
- // MyResponse* response,
67
- // Closure* done) {
68
- // // ... read request and fill in response ...
69
- // done->Run();
70
- // }
71
- // };
72
- // You would then register an instance of MyServiceImpl with your RPC server
73
- // implementation. (How to do that depends on the implementation.)
74
- //
75
- // To call a remote MyServiceImpl, first you need an RpcChannel connected to it.
76
- // How to construct a channel depends, again, on your RPC implementation.
77
- // Here we use a hypothentical "MyRpcChannel" as an example:
78
- // MyRpcChannel channel("rpc:hostname:1234/myservice");
79
- // MyRpcController controller;
80
- // MyServiceImpl::Stub stub(&channel);
81
- // FooRequest request;
82
- // FooRespnose response;
83
- //
84
- // // ... fill in request ...
85
- //
86
- // stub.Foo(&controller, request, &response, NewCallback(HandleResponse));
87
- //
88
- // On Thread-Safety:
89
- //
90
- // Different RPC implementations may make different guarantees about what
91
- // threads they may run callbacks on, and what threads the application is
92
- // allowed to use to call the RPC system. Portable software should be ready
93
- // for callbacks to be called on any thread, but should not try to call the
94
- // RPC system from any thread except for the ones on which it received the
95
- // callbacks. Realistically, though, simple software will probably want to
96
- // use a single-threaded RPC system while high-end software will want to
97
- // use multiple threads. RPC implementations should provide multiple
98
- // choices.
99
-
100
- #ifndef GOOGLE_PROTOBUF_SERVICE_H__
101
- #define GOOGLE_PROTOBUF_SERVICE_H__
102
-
103
- #include <string>
104
- #include <google/protobuf/stubs/common.h>
105
-
106
- namespace google {
107
- namespace protobuf {
108
-
109
- // Defined in this file.
110
- class Service;
111
- class RpcController;
112
- class RpcChannel;
113
-
114
- // Defined in other files.
115
- class Descriptor; // descriptor.h
116
- class ServiceDescriptor; // descriptor.h
117
- class MethodDescriptor; // descriptor.h
118
- class Message; // message.h
119
-
120
- // Abstract base interface for protocol-buffer-based RPC services. Services
121
- // themselves are abstract interfaces (implemented either by servers or as
122
- // stubs), but they subclass this base interface. The methods of this
123
- // interface can be used to call the methods of the Service without knowing
124
- // its exact type at compile time (analogous to Reflection).
125
- class LIBPROTOBUF_EXPORT Service {
126
- public:
127
- inline Service() {}
128
- virtual ~Service();
129
-
130
- // When constructing a stub, you may pass STUB_OWNS_CHANNEL as the second
131
- // parameter to the constructor to tell it to delete its RpcChannel when
132
- // destroyed.
133
- enum ChannelOwnership {
134
- STUB_OWNS_CHANNEL,
135
- STUB_DOESNT_OWN_CHANNEL
136
- };
137
-
138
- // Get the ServiceDescriptor describing this service and its methods.
139
- virtual const ServiceDescriptor* GetDescriptor() = 0;
140
-
141
- // Call a method of the service specified by MethodDescriptor. This is
142
- // normally implemented as a simple switch() that calls the standard
143
- // definitions of the service's methods.
144
- //
145
- // Preconditions:
146
- // * method->service() == GetDescriptor()
147
- // * request and response are of the exact same classes as the objects
148
- // returned by GetRequestPrototype(method) and
149
- // GetResponsePrototype(method).
150
- // * After the call has started, the request must not be modified and the
151
- // response must not be accessed at all until "done" is called.
152
- // * "controller" is of the correct type for the RPC implementation being
153
- // used by this Service. For stubs, the "correct type" depends on the
154
- // RpcChannel which the stub is using. Server-side Service
155
- // implementations are expected to accept whatever type of RpcController
156
- // the server-side RPC implementation uses.
157
- //
158
- // Postconditions:
159
- // * "done" will be called when the method is complete. This may be
160
- // before CallMethod() returns or it may be at some point in the future.
161
- // * If the RPC succeeded, "response" contains the response returned by
162
- // the server.
163
- // * If the RPC failed, "response"'s contents are undefined. The
164
- // RpcController can be queried to determine if an error occurred and
165
- // possibly to get more information about the error.
166
- virtual void CallMethod(const MethodDescriptor* method,
167
- RpcController* controller,
168
- const Message* request,
169
- Message* response,
170
- Closure* done) = 0;
171
-
172
- // CallMethod() requires that the request and response passed in are of a
173
- // particular subclass of Message. GetRequestPrototype() and
174
- // GetResponsePrototype() get the default instances of these required types.
175
- // You can then call Message::New() on these instances to construct mutable
176
- // objects which you can then pass to CallMethod().
177
- //
178
- // Example:
179
- // const MethodDescriptor* method =
180
- // service->GetDescriptor()->FindMethodByName("Foo");
181
- // Message* request = stub->GetRequestPrototype (method)->New();
182
- // Message* response = stub->GetResponsePrototype(method)->New();
183
- // request->ParseFromString(input);
184
- // service->CallMethod(method, *request, response, callback);
185
- virtual const Message& GetRequestPrototype(
186
- const MethodDescriptor* method) const = 0;
187
- virtual const Message& GetResponsePrototype(
188
- const MethodDescriptor* method) const = 0;
189
-
190
- private:
191
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Service);
192
- };
193
-
194
- // An RpcController mediates a single method call. The primary purpose of
195
- // the controller is to provide a way to manipulate settings specific to the
196
- // RPC implementation and to find out about RPC-level errors.
197
- //
198
- // The methods provided by the RpcController interface are intended to be a
199
- // "least common denominator" set of features which we expect all
200
- // implementations to support. Specific implementations may provide more
201
- // advanced features (e.g. deadline propagation).
202
- class LIBPROTOBUF_EXPORT RpcController {
203
- public:
204
- inline RpcController() {}
205
- virtual ~RpcController();
206
-
207
- // Client-side methods ---------------------------------------------
208
- // These calls may be made from the client side only. Their results
209
- // are undefined on the server side (may crash).
210
-
211
- // Resets the RpcController to its initial state so that it may be reused in
212
- // a new call. Must not be called while an RPC is in progress.
213
- virtual void Reset() = 0;
214
-
215
- // After a call has finished, returns true if the call failed. The possible
216
- // reasons for failure depend on the RPC implementation. Failed() must not
217
- // be called before a call has finished. If Failed() returns true, the
218
- // contents of the response message are undefined.
219
- virtual bool Failed() const = 0;
220
-
221
- // If Failed() is true, returns a human-readable description of the error.
222
- virtual string ErrorText() const = 0;
223
-
224
- // Advises the RPC system that the caller desires that the RPC call be
225
- // canceled. The RPC system may cancel it immediately, may wait awhile and
226
- // then cancel it, or may not even cancel the call at all. If the call is
227
- // canceled, the "done" callback will still be called and the RpcController
228
- // will indicate that the call failed at that time.
229
- virtual void StartCancel() = 0;
230
-
231
- // Server-side methods ---------------------------------------------
232
- // These calls may be made from the server side only. Their results
233
- // are undefined on the client side (may crash).
234
-
235
- // Causes Failed() to return true on the client side. "reason" will be
236
- // incorporated into the message returned by ErrorText(). If you find
237
- // you need to return machine-readable information about failures, you
238
- // should incorporate it into your response protocol buffer and should
239
- // NOT call SetFailed().
240
- virtual void SetFailed(const string& reason) = 0;
241
-
242
- // If true, indicates that the client canceled the RPC, so the server may
243
- // as well give up on replying to it. The server should still call the
244
- // final "done" callback.
245
- virtual bool IsCanceled() const = 0;
246
-
247
- // Asks that the given callback be called when the RPC is canceled. The
248
- // callback will always be called exactly once. If the RPC completes without
249
- // being canceled, the callback will be called after completion. If the RPC
250
- // has already been canceled when NotifyOnCancel() is called, the callback
251
- // will be called immediately.
252
- //
253
- // NotifyOnCancel() must be called no more than once per request.
254
- virtual void NotifyOnCancel(Closure* callback) = 0;
255
-
256
- private:
257
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcController);
258
- };
259
-
260
- // Abstract interface for an RPC channel. An RpcChannel represents a
261
- // communication line to a Service which can be used to call that Service's
262
- // methods. The Service may be running on another machine. Normally, you
263
- // should not call an RpcChannel directly, but instead construct a stub Service
264
- // wrapping it. Example:
265
- // RpcChannel* channel = new MyRpcChannel("remotehost.example.com:1234");
266
- // MyService* service = new MyService::Stub(channel);
267
- // service->MyMethod(request, &response, callback);
268
- class LIBPROTOBUF_EXPORT RpcChannel {
269
- public:
270
- inline RpcChannel() {}
271
- virtual ~RpcChannel();
272
-
273
- // Call the given method of the remote service. The signature of this
274
- // procedure looks the same as Service::CallMethod(), but the requirements
275
- // are less strict in one important way: the request and response objects
276
- // need not be of any specific class as long as their descriptors are
277
- // method->input_type() and method->output_type().
278
- virtual void CallMethod(const MethodDescriptor* method,
279
- RpcController* controller,
280
- const Message* request,
281
- Message* response,
282
- Closure* done) = 0;
283
-
284
- private:
285
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RpcChannel);
286
- };
287
-
288
- } // namespace protobuf
289
-
290
- } // namespace google
291
- #endif // GOOGLE_PROTOBUF_SERVICE_H__
@@ -1,1211 +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) and others
32
- //
33
- // Contains basic types and utilities used by the rest of the library.
34
-
35
- #ifndef GOOGLE_PROTOBUF_COMMON_H__
36
- #define GOOGLE_PROTOBUF_COMMON_H__
37
-
38
- #include <assert.h>
39
- #include <stdlib.h>
40
- #include <cstddef>
41
- #include <string>
42
- #include <string.h>
43
- #if defined(__osf__)
44
- // Tru64 lacks stdint.h, but has inttypes.h which defines a superset of
45
- // what stdint.h would define.
46
- #include <inttypes.h>
47
- #elif !defined(_MSC_VER)
48
- #include <stdint.h>
49
- #endif
50
-
51
- #if defined(_MSC_VER) && defined(_CPPUNWIND)
52
- #define PROTOBUF_USE_EXCEPTIONS
53
- #elif defined(__EXCEPTIONS)
54
- #define PROTOBUF_USE_EXCEPTIONS
55
- #endif
56
- #ifdef PROTOBUF_USE_EXCEPTIONS
57
- #include <exception>
58
- #endif
59
-
60
- #if defined(_WIN32) && defined(GetMessage)
61
- // Allow GetMessage to be used as a valid method name in protobuf classes.
62
- // windows.h defines GetMessage() as a macro. Let's re-define it as an inline
63
- // function. The inline function should be equivalent for C++ users.
64
- inline BOOL GetMessage_Win32(
65
- LPMSG lpMsg, HWND hWnd,
66
- UINT wMsgFilterMin, UINT wMsgFilterMax) {
67
- return GetMessage(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
68
- }
69
- #undef GetMessage
70
- inline BOOL GetMessage(
71
- LPMSG lpMsg, HWND hWnd,
72
- UINT wMsgFilterMin, UINT wMsgFilterMax) {
73
- return GetMessage_Win32(lpMsg, hWnd, wMsgFilterMin, wMsgFilterMax);
74
- }
75
- #endif
76
-
77
-
78
- namespace std {}
79
-
80
- namespace google {
81
- namespace protobuf {
82
-
83
- #undef GOOGLE_DISALLOW_EVIL_CONSTRUCTORS
84
- #define GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TypeName) \
85
- TypeName(const TypeName&); \
86
- void operator=(const TypeName&)
87
-
88
- #if defined(_MSC_VER) && defined(PROTOBUF_USE_DLLS)
89
- #ifdef LIBPROTOBUF_EXPORTS
90
- #define LIBPROTOBUF_EXPORT __declspec(dllexport)
91
- #else
92
- #define LIBPROTOBUF_EXPORT __declspec(dllimport)
93
- #endif
94
- #ifdef LIBPROTOC_EXPORTS
95
- #define LIBPROTOC_EXPORT __declspec(dllexport)
96
- #else
97
- #define LIBPROTOC_EXPORT __declspec(dllimport)
98
- #endif
99
- #else
100
- #define LIBPROTOBUF_EXPORT
101
- #define LIBPROTOC_EXPORT
102
- #endif
103
-
104
- namespace internal {
105
-
106
- // Some of these constants are macros rather than const ints so that they can
107
- // be used in #if directives.
108
-
109
- // The current version, represented as a single integer to make comparison
110
- // easier: major * 10^6 + minor * 10^3 + micro
111
- #define GOOGLE_PROTOBUF_VERSION 2004001
112
-
113
- // The minimum library version which works with the current version of the
114
- // headers.
115
- #define GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION 2004000
116
-
117
- // The minimum header version which works with the current version of
118
- // the library. This constant should only be used by protoc's C++ code
119
- // generator.
120
- static const int kMinHeaderVersionForLibrary = 2004000;
121
-
122
- // The minimum protoc version which works with the current version of the
123
- // headers.
124
- #define GOOGLE_PROTOBUF_MIN_PROTOC_VERSION 2004000
125
-
126
- // The minimum header version which works with the current version of
127
- // protoc. This constant should only be used in VerifyVersion().
128
- static const int kMinHeaderVersionForProtoc = 2004000;
129
-
130
- // Verifies that the headers and libraries are compatible. Use the macro
131
- // below to call this.
132
- void LIBPROTOBUF_EXPORT VerifyVersion(int headerVersion, int minLibraryVersion,
133
- const char* filename);
134
-
135
- // Converts a numeric version number to a string.
136
- std::string LIBPROTOBUF_EXPORT VersionString(int version);
137
-
138
- } // namespace internal
139
-
140
- // Place this macro in your main() function (or somewhere before you attempt
141
- // to use the protobuf library) to verify that the version you link against
142
- // matches the headers you compiled against. If a version mismatch is
143
- // detected, the process will abort.
144
- #define GOOGLE_PROTOBUF_VERIFY_VERSION \
145
- ::google::protobuf::internal::VerifyVersion( \
146
- GOOGLE_PROTOBUF_VERSION, GOOGLE_PROTOBUF_MIN_LIBRARY_VERSION, \
147
- __FILE__)
148
-
149
- // ===================================================================
150
- // from google3/base/port.h
151
-
152
- typedef unsigned int uint;
153
-
154
- #ifdef _MSC_VER
155
- typedef __int8 int8;
156
- typedef __int16 int16;
157
- typedef __int32 int32;
158
- typedef __int64 int64;
159
-
160
- typedef unsigned __int8 uint8;
161
- typedef unsigned __int16 uint16;
162
- typedef unsigned __int32 uint32;
163
- typedef unsigned __int64 uint64;
164
- #else
165
- typedef int8_t int8;
166
- typedef int16_t int16;
167
- typedef int32_t int32;
168
- typedef int64_t int64;
169
-
170
- typedef uint8_t uint8;
171
- typedef uint16_t uint16;
172
- typedef uint32_t uint32;
173
- typedef uint64_t uint64;
174
- #endif
175
-
176
- // long long macros to be used because gcc and vc++ use different suffixes,
177
- // and different size specifiers in format strings
178
- #undef GOOGLE_LONGLONG
179
- #undef GOOGLE_ULONGLONG
180
- #undef GOOGLE_LL_FORMAT
181
-
182
- #ifdef _MSC_VER
183
- #define GOOGLE_LONGLONG(x) x##I64
184
- #define GOOGLE_ULONGLONG(x) x##UI64
185
- #define GOOGLE_LL_FORMAT "I64" // As in printf("%I64d", ...)
186
- #else
187
- #define GOOGLE_LONGLONG(x) x##LL
188
- #define GOOGLE_ULONGLONG(x) x##ULL
189
- #define GOOGLE_LL_FORMAT "ll" // As in "%lld". Note that "q" is poor form also.
190
- #endif
191
-
192
- static const int32 kint32max = 0x7FFFFFFF;
193
- static const int32 kint32min = -kint32max - 1;
194
- static const int64 kint64max = GOOGLE_LONGLONG(0x7FFFFFFFFFFFFFFF);
195
- static const int64 kint64min = -kint64max - 1;
196
- static const uint32 kuint32max = 0xFFFFFFFFu;
197
- static const uint64 kuint64max = GOOGLE_ULONGLONG(0xFFFFFFFFFFFFFFFF);
198
-
199
- // -------------------------------------------------------------------
200
- // Annotations: Some parts of the code have been annotated in ways that might
201
- // be useful to some compilers or tools, but are not supported universally.
202
- // You can #define these annotations yourself if the default implementation
203
- // is not right for you.
204
-
205
- #ifndef GOOGLE_ATTRIBUTE_ALWAYS_INLINE
206
- #if defined(__GNUC__) && (__GNUC__ > 3 ||(__GNUC__ == 3 && __GNUC_MINOR__ >= 1))
207
- // For functions we want to force inline.
208
- // Introduced in gcc 3.1.
209
- #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE __attribute__ ((always_inline))
210
- #else
211
- // Other compilers will have to figure it out for themselves.
212
- #define GOOGLE_ATTRIBUTE_ALWAYS_INLINE
213
- #endif
214
- #endif
215
-
216
- #ifndef GOOGLE_ATTRIBUTE_DEPRECATED
217
- #ifdef __GNUC__
218
- // If the method/variable/type is used anywhere, produce a warning.
219
- #define GOOGLE_ATTRIBUTE_DEPRECATED __attribute__((deprecated))
220
- #else
221
- #define GOOGLE_ATTRIBUTE_DEPRECATED
222
- #endif
223
- #endif
224
-
225
- #ifndef GOOGLE_PREDICT_TRUE
226
- #ifdef __GNUC__
227
- // Provided at least since GCC 3.0.
228
- #define GOOGLE_PREDICT_TRUE(x) (__builtin_expect(!!(x), 1))
229
- #else
230
- #define GOOGLE_PREDICT_TRUE
231
- #endif
232
- #endif
233
-
234
- // Delimits a block of code which may write to memory which is simultaneously
235
- // written by other threads, but which has been determined to be thread-safe
236
- // (e.g. because it is an idempotent write).
237
- #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN
238
- #define GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN()
239
- #endif
240
- #ifndef GOOGLE_SAFE_CONCURRENT_WRITES_END
241
- #define GOOGLE_SAFE_CONCURRENT_WRITES_END()
242
- #endif
243
-
244
- // ===================================================================
245
- // from google3/base/basictypes.h
246
-
247
- // The GOOGLE_ARRAYSIZE(arr) macro returns the # of elements in an array arr.
248
- // The expression is a compile-time constant, and therefore can be
249
- // used in defining new arrays, for example.
250
- //
251
- // GOOGLE_ARRAYSIZE catches a few type errors. If you see a compiler error
252
- //
253
- // "warning: division by zero in ..."
254
- //
255
- // when using GOOGLE_ARRAYSIZE, you are (wrongfully) giving it a pointer.
256
- // You should only use GOOGLE_ARRAYSIZE on statically allocated arrays.
257
- //
258
- // The following comments are on the implementation details, and can
259
- // be ignored by the users.
260
- //
261
- // ARRAYSIZE(arr) works by inspecting sizeof(arr) (the # of bytes in
262
- // the array) and sizeof(*(arr)) (the # of bytes in one array
263
- // element). If the former is divisible by the latter, perhaps arr is
264
- // indeed an array, in which case the division result is the # of
265
- // elements in the array. Otherwise, arr cannot possibly be an array,
266
- // and we generate a compiler error to prevent the code from
267
- // compiling.
268
- //
269
- // Since the size of bool is implementation-defined, we need to cast
270
- // !(sizeof(a) & sizeof(*(a))) to size_t in order to ensure the final
271
- // result has type size_t.
272
- //
273
- // This macro is not perfect as it wrongfully accepts certain
274
- // pointers, namely where the pointer size is divisible by the pointee
275
- // size. Since all our code has to go through a 32-bit compiler,
276
- // where a pointer is 4 bytes, this means all pointers to a type whose
277
- // size is 3 or greater than 4 will be (righteously) rejected.
278
- //
279
- // Kudos to Jorg Brown for this simple and elegant implementation.
280
-
281
- #undef GOOGLE_ARRAYSIZE
282
- #define GOOGLE_ARRAYSIZE(a) \
283
- ((sizeof(a) / sizeof(*(a))) / \
284
- static_cast<size_t>(!(sizeof(a) % sizeof(*(a)))))
285
-
286
- namespace internal {
287
-
288
- // Use implicit_cast as a safe version of static_cast or const_cast
289
- // for upcasting in the type hierarchy (i.e. casting a pointer to Foo
290
- // to a pointer to SuperclassOfFoo or casting a pointer to Foo to
291
- // a const pointer to Foo).
292
- // When you use implicit_cast, the compiler checks that the cast is safe.
293
- // Such explicit implicit_casts are necessary in surprisingly many
294
- // situations where C++ demands an exact type match instead of an
295
- // argument type convertable to a target type.
296
- //
297
- // The From type can be inferred, so the preferred syntax for using
298
- // implicit_cast is the same as for static_cast etc.:
299
- //
300
- // implicit_cast<ToType>(expr)
301
- //
302
- // implicit_cast would have been part of the C++ standard library,
303
- // but the proposal was submitted too late. It will probably make
304
- // its way into the language in the future.
305
- template<typename To, typename From>
306
- inline To implicit_cast(From const &f) {
307
- return f;
308
- }
309
-
310
- // When you upcast (that is, cast a pointer from type Foo to type
311
- // SuperclassOfFoo), it's fine to use implicit_cast<>, since upcasts
312
- // always succeed. When you downcast (that is, cast a pointer from
313
- // type Foo to type SubclassOfFoo), static_cast<> isn't safe, because
314
- // how do you know the pointer is really of type SubclassOfFoo? It
315
- // could be a bare Foo, or of type DifferentSubclassOfFoo. Thus,
316
- // when you downcast, you should use this macro. In debug mode, we
317
- // use dynamic_cast<> to double-check the downcast is legal (we die
318
- // if it's not). In normal mode, we do the efficient static_cast<>
319
- // instead. Thus, it's important to test in debug mode to make sure
320
- // the cast is legal!
321
- // This is the only place in the code we should use dynamic_cast<>.
322
- // In particular, you SHOULDN'T be using dynamic_cast<> in order to
323
- // do RTTI (eg code like this:
324
- // if (dynamic_cast<Subclass1>(foo)) HandleASubclass1Object(foo);
325
- // if (dynamic_cast<Subclass2>(foo)) HandleASubclass2Object(foo);
326
- // You should design the code some other way not to need this.
327
-
328
- template<typename To, typename From> // use like this: down_cast<T*>(foo);
329
- inline To down_cast(From* f) { // so we only accept pointers
330
- // Ensures that To is a sub-type of From *. This test is here only
331
- // for compile-time type checking, and has no overhead in an
332
- // optimized build at run-time, as it will be optimized away
333
- // completely.
334
- if (false) {
335
- implicit_cast<From*, To>(0);
336
- }
337
-
338
- #if !defined(NDEBUG) && !defined(GOOGLE_PROTOBUF_NO_RTTI)
339
- assert(f == NULL || dynamic_cast<To>(f) != NULL); // RTTI: debug mode only!
340
- #endif
341
- return static_cast<To>(f);
342
- }
343
-
344
- } // namespace internal
345
-
346
- // We made these internal so that they would show up as such in the docs,
347
- // but we don't want to stick "internal::" in front of them everywhere.
348
- using internal::implicit_cast;
349
- using internal::down_cast;
350
-
351
- // The COMPILE_ASSERT macro can be used to verify that a compile time
352
- // expression is true. For example, you could use it to verify the
353
- // size of a static array:
354
- //
355
- // COMPILE_ASSERT(ARRAYSIZE(content_type_names) == CONTENT_NUM_TYPES,
356
- // content_type_names_incorrect_size);
357
- //
358
- // or to make sure a struct is smaller than a certain size:
359
- //
360
- // COMPILE_ASSERT(sizeof(foo) < 128, foo_too_large);
361
- //
362
- // The second argument to the macro is the name of the variable. If
363
- // the expression is false, most compilers will issue a warning/error
364
- // containing the name of the variable.
365
-
366
- namespace internal {
367
-
368
- template <bool>
369
- struct CompileAssert {
370
- };
371
-
372
- } // namespace internal
373
-
374
- #undef GOOGLE_COMPILE_ASSERT
375
- #define GOOGLE_COMPILE_ASSERT(expr, msg) \
376
- typedef ::google::protobuf::internal::CompileAssert<(bool(expr))> \
377
- msg[bool(expr) ? 1 : -1]
378
-
379
-
380
- // Implementation details of COMPILE_ASSERT:
381
- //
382
- // - COMPILE_ASSERT works by defining an array type that has -1
383
- // elements (and thus is invalid) when the expression is false.
384
- //
385
- // - The simpler definition
386
- //
387
- // #define COMPILE_ASSERT(expr, msg) typedef char msg[(expr) ? 1 : -1]
388
- //
389
- // does not work, as gcc supports variable-length arrays whose sizes
390
- // are determined at run-time (this is gcc's extension and not part
391
- // of the C++ standard). As a result, gcc fails to reject the
392
- // following code with the simple definition:
393
- //
394
- // int foo;
395
- // COMPILE_ASSERT(foo, msg); // not supposed to compile as foo is
396
- // // not a compile-time constant.
397
- //
398
- // - By using the type CompileAssert<(bool(expr))>, we ensures that
399
- // expr is a compile-time constant. (Template arguments must be
400
- // determined at compile-time.)
401
- //
402
- // - The outter parentheses in CompileAssert<(bool(expr))> are necessary
403
- // to work around a bug in gcc 3.4.4 and 4.0.1. If we had written
404
- //
405
- // CompileAssert<bool(expr)>
406
- //
407
- // instead, these compilers will refuse to compile
408
- //
409
- // COMPILE_ASSERT(5 > 0, some_message);
410
- //
411
- // (They seem to think the ">" in "5 > 0" marks the end of the
412
- // template argument list.)
413
- //
414
- // - The array size is (bool(expr) ? 1 : -1), instead of simply
415
- //
416
- // ((expr) ? 1 : -1).
417
- //
418
- // This is to avoid running into a bug in MS VC 7.1, which
419
- // causes ((0.0) ? 1 : -1) to incorrectly evaluate to 1.
420
-
421
- // ===================================================================
422
- // from google3/base/scoped_ptr.h
423
-
424
- namespace internal {
425
-
426
- // This is an implementation designed to match the anticipated future TR2
427
- // implementation of the scoped_ptr class, and its closely-related brethren,
428
- // scoped_array, scoped_ptr_malloc, and make_scoped_ptr.
429
-
430
- template <class C> class scoped_ptr;
431
- template <class C> class scoped_array;
432
-
433
- // A scoped_ptr<T> is like a T*, except that the destructor of scoped_ptr<T>
434
- // automatically deletes the pointer it holds (if any).
435
- // That is, scoped_ptr<T> owns the T object that it points to.
436
- // Like a T*, a scoped_ptr<T> may hold either NULL or a pointer to a T object.
437
- //
438
- // The size of a scoped_ptr is small:
439
- // sizeof(scoped_ptr<C>) == sizeof(C*)
440
- template <class C>
441
- class scoped_ptr {
442
- public:
443
-
444
- // The element type
445
- typedef C element_type;
446
-
447
- // Constructor. Defaults to intializing with NULL.
448
- // There is no way to create an uninitialized scoped_ptr.
449
- // The input parameter must be allocated with new.
450
- explicit scoped_ptr(C* p = NULL) : ptr_(p) { }
451
-
452
- // Destructor. If there is a C object, delete it.
453
- // We don't need to test ptr_ == NULL because C++ does that for us.
454
- ~scoped_ptr() {
455
- enum { type_must_be_complete = sizeof(C) };
456
- delete ptr_;
457
- }
458
-
459
- // Reset. Deletes the current owned object, if any.
460
- // Then takes ownership of a new object, if given.
461
- // this->reset(this->get()) works.
462
- void reset(C* p = NULL) {
463
- if (p != ptr_) {
464
- enum { type_must_be_complete = sizeof(C) };
465
- delete ptr_;
466
- ptr_ = p;
467
- }
468
- }
469
-
470
- // Accessors to get the owned object.
471
- // operator* and operator-> will assert() if there is no current object.
472
- C& operator*() const {
473
- assert(ptr_ != NULL);
474
- return *ptr_;
475
- }
476
- C* operator->() const {
477
- assert(ptr_ != NULL);
478
- return ptr_;
479
- }
480
- C* get() const { return ptr_; }
481
-
482
- // Comparison operators.
483
- // These return whether two scoped_ptr refer to the same object, not just to
484
- // two different but equal objects.
485
- bool operator==(C* p) const { return ptr_ == p; }
486
- bool operator!=(C* p) const { return ptr_ != p; }
487
-
488
- // Swap two scoped pointers.
489
- void swap(scoped_ptr& p2) {
490
- C* tmp = ptr_;
491
- ptr_ = p2.ptr_;
492
- p2.ptr_ = tmp;
493
- }
494
-
495
- // Release a pointer.
496
- // The return value is the current pointer held by this object.
497
- // If this object holds a NULL pointer, the return value is NULL.
498
- // After this operation, this object will hold a NULL pointer,
499
- // and will not own the object any more.
500
- C* release() {
501
- C* retVal = ptr_;
502
- ptr_ = NULL;
503
- return retVal;
504
- }
505
-
506
- private:
507
- C* ptr_;
508
-
509
- // Forbid comparison of scoped_ptr types. If C2 != C, it totally doesn't
510
- // make sense, and if C2 == C, it still doesn't make sense because you should
511
- // never have the same object owned by two different scoped_ptrs.
512
- template <class C2> bool operator==(scoped_ptr<C2> const& p2) const;
513
- template <class C2> bool operator!=(scoped_ptr<C2> const& p2) const;
514
-
515
- // Disallow evil constructors
516
- scoped_ptr(const scoped_ptr&);
517
- void operator=(const scoped_ptr&);
518
- };
519
-
520
- // scoped_array<C> is like scoped_ptr<C>, except that the caller must allocate
521
- // with new [] and the destructor deletes objects with delete [].
522
- //
523
- // As with scoped_ptr<C>, a scoped_array<C> either points to an object
524
- // or is NULL. A scoped_array<C> owns the object that it points to.
525
- //
526
- // Size: sizeof(scoped_array<C>) == sizeof(C*)
527
- template <class C>
528
- class scoped_array {
529
- public:
530
-
531
- // The element type
532
- typedef C element_type;
533
-
534
- // Constructor. Defaults to intializing with NULL.
535
- // There is no way to create an uninitialized scoped_array.
536
- // The input parameter must be allocated with new [].
537
- explicit scoped_array(C* p = NULL) : array_(p) { }
538
-
539
- // Destructor. If there is a C object, delete it.
540
- // We don't need to test ptr_ == NULL because C++ does that for us.
541
- ~scoped_array() {
542
- enum { type_must_be_complete = sizeof(C) };
543
- delete[] array_;
544
- }
545
-
546
- // Reset. Deletes the current owned object, if any.
547
- // Then takes ownership of a new object, if given.
548
- // this->reset(this->get()) works.
549
- void reset(C* p = NULL) {
550
- if (p != array_) {
551
- enum { type_must_be_complete = sizeof(C) };
552
- delete[] array_;
553
- array_ = p;
554
- }
555
- }
556
-
557
- // Get one element of the current object.
558
- // Will assert() if there is no current object, or index i is negative.
559
- C& operator[](std::ptrdiff_t i) const {
560
- assert(i >= 0);
561
- assert(array_ != NULL);
562
- return array_[i];
563
- }
564
-
565
- // Get a pointer to the zeroth element of the current object.
566
- // If there is no current object, return NULL.
567
- C* get() const {
568
- return array_;
569
- }
570
-
571
- // Comparison operators.
572
- // These return whether two scoped_array refer to the same object, not just to
573
- // two different but equal objects.
574
- bool operator==(C* p) const { return array_ == p; }
575
- bool operator!=(C* p) const { return array_ != p; }
576
-
577
- // Swap two scoped arrays.
578
- void swap(scoped_array& p2) {
579
- C* tmp = array_;
580
- array_ = p2.array_;
581
- p2.array_ = tmp;
582
- }
583
-
584
- // Release an array.
585
- // The return value is the current pointer held by this object.
586
- // If this object holds a NULL pointer, the return value is NULL.
587
- // After this operation, this object will hold a NULL pointer,
588
- // and will not own the object any more.
589
- C* release() {
590
- C* retVal = array_;
591
- array_ = NULL;
592
- return retVal;
593
- }
594
-
595
- private:
596
- C* array_;
597
-
598
- // Forbid comparison of different scoped_array types.
599
- template <class C2> bool operator==(scoped_array<C2> const& p2) const;
600
- template <class C2> bool operator!=(scoped_array<C2> const& p2) const;
601
-
602
- // Disallow evil constructors
603
- scoped_array(const scoped_array&);
604
- void operator=(const scoped_array&);
605
- };
606
-
607
- } // namespace internal
608
-
609
- // We made these internal so that they would show up as such in the docs,
610
- // but we don't want to stick "internal::" in front of them everywhere.
611
- using internal::scoped_ptr;
612
- using internal::scoped_array;
613
-
614
- // ===================================================================
615
- // emulates google3/base/logging.h
616
-
617
- enum LogLevel {
618
- LOGLEVEL_INFO, // Informational. This is never actually used by
619
- // libprotobuf.
620
- LOGLEVEL_WARNING, // Warns about issues that, although not technically a
621
- // problem now, could cause problems in the future. For
622
- // example, a // warning will be printed when parsing a
623
- // message that is near the message size limit.
624
- LOGLEVEL_ERROR, // An error occurred which should never happen during
625
- // normal use.
626
- LOGLEVEL_FATAL, // An error occurred from which the library cannot
627
- // recover. This usually indicates a programming error
628
- // in the code which calls the library, especially when
629
- // compiled in debug mode.
630
-
631
- #ifdef NDEBUG
632
- LOGLEVEL_DFATAL = LOGLEVEL_ERROR
633
- #else
634
- LOGLEVEL_DFATAL = LOGLEVEL_FATAL
635
- #endif
636
- };
637
-
638
- namespace internal {
639
-
640
- class LogFinisher;
641
-
642
- class LIBPROTOBUF_EXPORT LogMessage {
643
- public:
644
- LogMessage(LogLevel level, const char* filename, int line);
645
- ~LogMessage();
646
-
647
- LogMessage& operator<<(const std::string& value);
648
- LogMessage& operator<<(const char* value);
649
- LogMessage& operator<<(char value);
650
- LogMessage& operator<<(int value);
651
- LogMessage& operator<<(uint value);
652
- LogMessage& operator<<(long value);
653
- LogMessage& operator<<(unsigned long value);
654
- LogMessage& operator<<(double value);
655
-
656
- private:
657
- friend class LogFinisher;
658
- void Finish();
659
-
660
- LogLevel level_;
661
- const char* filename_;
662
- int line_;
663
- std::string message_;
664
- };
665
-
666
- // Used to make the entire "LOG(BLAH) << etc." expression have a void return
667
- // type and print a newline after each message.
668
- class LIBPROTOBUF_EXPORT LogFinisher {
669
- public:
670
- void operator=(LogMessage& other);
671
- };
672
-
673
- } // namespace internal
674
-
675
- // Undef everything in case we're being mixed with some other Google library
676
- // which already defined them itself. Presumably all Google libraries will
677
- // support the same syntax for these so it should not be a big deal if they
678
- // end up using our definitions instead.
679
- #undef GOOGLE_LOG
680
- #undef GOOGLE_LOG_IF
681
-
682
- #undef GOOGLE_CHECK
683
- #undef GOOGLE_CHECK_EQ
684
- #undef GOOGLE_CHECK_NE
685
- #undef GOOGLE_CHECK_LT
686
- #undef GOOGLE_CHECK_LE
687
- #undef GOOGLE_CHECK_GT
688
- #undef GOOGLE_CHECK_GE
689
-
690
- #undef GOOGLE_DLOG
691
- #undef GOOGLE_DCHECK
692
- #undef GOOGLE_DCHECK_EQ
693
- #undef GOOGLE_DCHECK_NE
694
- #undef GOOGLE_DCHECK_LT
695
- #undef GOOGLE_DCHECK_LE
696
- #undef GOOGLE_DCHECK_GT
697
- #undef GOOGLE_DCHECK_GE
698
-
699
- #define GOOGLE_LOG(LEVEL) \
700
- ::google::protobuf::internal::LogFinisher() = \
701
- ::google::protobuf::internal::LogMessage( \
702
- ::google::protobuf::LOGLEVEL_##LEVEL, __FILE__, __LINE__)
703
- #define GOOGLE_LOG_IF(LEVEL, CONDITION) \
704
- !(CONDITION) ? (void)0 : GOOGLE_LOG(LEVEL)
705
-
706
- #define GOOGLE_CHECK(EXPRESSION) \
707
- GOOGLE_LOG_IF(FATAL, !(EXPRESSION)) << "CHECK failed: " #EXPRESSION ": "
708
- #define GOOGLE_CHECK_EQ(A, B) GOOGLE_CHECK((A) == (B))
709
- #define GOOGLE_CHECK_NE(A, B) GOOGLE_CHECK((A) != (B))
710
- #define GOOGLE_CHECK_LT(A, B) GOOGLE_CHECK((A) < (B))
711
- #define GOOGLE_CHECK_LE(A, B) GOOGLE_CHECK((A) <= (B))
712
- #define GOOGLE_CHECK_GT(A, B) GOOGLE_CHECK((A) > (B))
713
- #define GOOGLE_CHECK_GE(A, B) GOOGLE_CHECK((A) >= (B))
714
-
715
- #ifdef NDEBUG
716
-
717
- #define GOOGLE_DLOG GOOGLE_LOG_IF(INFO, false)
718
-
719
- #define GOOGLE_DCHECK(EXPRESSION) while(false) GOOGLE_CHECK(EXPRESSION)
720
- #define GOOGLE_DCHECK_EQ(A, B) GOOGLE_DCHECK((A) == (B))
721
- #define GOOGLE_DCHECK_NE(A, B) GOOGLE_DCHECK((A) != (B))
722
- #define GOOGLE_DCHECK_LT(A, B) GOOGLE_DCHECK((A) < (B))
723
- #define GOOGLE_DCHECK_LE(A, B) GOOGLE_DCHECK((A) <= (B))
724
- #define GOOGLE_DCHECK_GT(A, B) GOOGLE_DCHECK((A) > (B))
725
- #define GOOGLE_DCHECK_GE(A, B) GOOGLE_DCHECK((A) >= (B))
726
-
727
- #else // NDEBUG
728
-
729
- #define GOOGLE_DLOG GOOGLE_LOG
730
-
731
- #define GOOGLE_DCHECK GOOGLE_CHECK
732
- #define GOOGLE_DCHECK_EQ GOOGLE_CHECK_EQ
733
- #define GOOGLE_DCHECK_NE GOOGLE_CHECK_NE
734
- #define GOOGLE_DCHECK_LT GOOGLE_CHECK_LT
735
- #define GOOGLE_DCHECK_LE GOOGLE_CHECK_LE
736
- #define GOOGLE_DCHECK_GT GOOGLE_CHECK_GT
737
- #define GOOGLE_DCHECK_GE GOOGLE_CHECK_GE
738
-
739
- #endif // !NDEBUG
740
-
741
- typedef void LogHandler(LogLevel level, const char* filename, int line,
742
- const std::string& message);
743
-
744
- // The protobuf library sometimes writes warning and error messages to
745
- // stderr. These messages are primarily useful for developers, but may
746
- // also help end users figure out a problem. If you would prefer that
747
- // these messages be sent somewhere other than stderr, call SetLogHandler()
748
- // to set your own handler. This returns the old handler. Set the handler
749
- // to NULL to ignore log messages (but see also LogSilencer, below).
750
- //
751
- // Obviously, SetLogHandler is not thread-safe. You should only call it
752
- // at initialization time, and probably not from library code. If you
753
- // simply want to suppress log messages temporarily (e.g. because you
754
- // have some code that tends to trigger them frequently and you know
755
- // the warnings are not important to you), use the LogSilencer class
756
- // below.
757
- LIBPROTOBUF_EXPORT LogHandler* SetLogHandler(LogHandler* new_func);
758
-
759
- // Create a LogSilencer if you want to temporarily suppress all log
760
- // messages. As long as any LogSilencer objects exist, non-fatal
761
- // log messages will be discarded (the current LogHandler will *not*
762
- // be called). Constructing a LogSilencer is thread-safe. You may
763
- // accidentally suppress log messages occurring in another thread, but
764
- // since messages are generally for debugging purposes only, this isn't
765
- // a big deal. If you want to intercept log messages, use SetLogHandler().
766
- class LIBPROTOBUF_EXPORT LogSilencer {
767
- public:
768
- LogSilencer();
769
- ~LogSilencer();
770
- };
771
-
772
- // ===================================================================
773
- // emulates google3/base/callback.h
774
-
775
- // Abstract interface for a callback. When calling an RPC, you must provide
776
- // a Closure to call when the procedure completes. See the Service interface
777
- // in service.h.
778
- //
779
- // To automatically construct a Closure which calls a particular function or
780
- // method with a particular set of parameters, use the NewCallback() function.
781
- // Example:
782
- // void FooDone(const FooResponse* response) {
783
- // ...
784
- // }
785
- //
786
- // void CallFoo() {
787
- // ...
788
- // // When done, call FooDone() and pass it a pointer to the response.
789
- // Closure* callback = NewCallback(&FooDone, response);
790
- // // Make the call.
791
- // service->Foo(controller, request, response, callback);
792
- // }
793
- //
794
- // Example that calls a method:
795
- // class Handler {
796
- // public:
797
- // ...
798
- //
799
- // void FooDone(const FooResponse* response) {
800
- // ...
801
- // }
802
- //
803
- // void CallFoo() {
804
- // ...
805
- // // When done, call FooDone() and pass it a pointer to the response.
806
- // Closure* callback = NewCallback(this, &Handler::FooDone, response);
807
- // // Make the call.
808
- // service->Foo(controller, request, response, callback);
809
- // }
810
- // };
811
- //
812
- // Currently NewCallback() supports binding zero, one, or two arguments.
813
- //
814
- // Callbacks created with NewCallback() automatically delete themselves when
815
- // executed. They should be used when a callback is to be called exactly
816
- // once (usually the case with RPC callbacks). If a callback may be called
817
- // a different number of times (including zero), create it with
818
- // NewPermanentCallback() instead. You are then responsible for deleting the
819
- // callback (using the "delete" keyword as normal).
820
- //
821
- // Note that NewCallback() is a bit touchy regarding argument types. Generally,
822
- // the values you provide for the parameter bindings must exactly match the
823
- // types accepted by the callback function. For example:
824
- // void Foo(string s);
825
- // NewCallback(&Foo, "foo"); // WON'T WORK: const char* != string
826
- // NewCallback(&Foo, string("foo")); // WORKS
827
- // Also note that the arguments cannot be references:
828
- // void Foo(const string& s);
829
- // string my_str;
830
- // NewCallback(&Foo, my_str); // WON'T WORK: Can't use referecnes.
831
- // However, correctly-typed pointers will work just fine.
832
- class LIBPROTOBUF_EXPORT Closure {
833
- public:
834
- Closure() {}
835
- virtual ~Closure();
836
-
837
- virtual void Run() = 0;
838
-
839
- private:
840
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Closure);
841
- };
842
-
843
- namespace internal {
844
-
845
- class LIBPROTOBUF_EXPORT FunctionClosure0 : public Closure {
846
- public:
847
- typedef void (*FunctionType)();
848
-
849
- FunctionClosure0(FunctionType function, bool self_deleting)
850
- : function_(function), self_deleting_(self_deleting) {}
851
- ~FunctionClosure0();
852
-
853
- void Run() {
854
- bool needs_delete = self_deleting_; // read in case callback deletes
855
- function_();
856
- if (needs_delete) delete this;
857
- }
858
-
859
- private:
860
- FunctionType function_;
861
- bool self_deleting_;
862
- };
863
-
864
- template <typename Class>
865
- class MethodClosure0 : public Closure {
866
- public:
867
- typedef void (Class::*MethodType)();
868
-
869
- MethodClosure0(Class* object, MethodType method, bool self_deleting)
870
- : object_(object), method_(method), self_deleting_(self_deleting) {}
871
- ~MethodClosure0() {}
872
-
873
- void Run() {
874
- bool needs_delete = self_deleting_; // read in case callback deletes
875
- (object_->*method_)();
876
- if (needs_delete) delete this;
877
- }
878
-
879
- private:
880
- Class* object_;
881
- MethodType method_;
882
- bool self_deleting_;
883
- };
884
-
885
- template <typename Arg1>
886
- class FunctionClosure1 : public Closure {
887
- public:
888
- typedef void (*FunctionType)(Arg1 arg1);
889
-
890
- FunctionClosure1(FunctionType function, bool self_deleting,
891
- Arg1 arg1)
892
- : function_(function), self_deleting_(self_deleting),
893
- arg1_(arg1) {}
894
- ~FunctionClosure1() {}
895
-
896
- void Run() {
897
- bool needs_delete = self_deleting_; // read in case callback deletes
898
- function_(arg1_);
899
- if (needs_delete) delete this;
900
- }
901
-
902
- private:
903
- FunctionType function_;
904
- bool self_deleting_;
905
- Arg1 arg1_;
906
- };
907
-
908
- template <typename Class, typename Arg1>
909
- class MethodClosure1 : public Closure {
910
- public:
911
- typedef void (Class::*MethodType)(Arg1 arg1);
912
-
913
- MethodClosure1(Class* object, MethodType method, bool self_deleting,
914
- Arg1 arg1)
915
- : object_(object), method_(method), self_deleting_(self_deleting),
916
- arg1_(arg1) {}
917
- ~MethodClosure1() {}
918
-
919
- void Run() {
920
- bool needs_delete = self_deleting_; // read in case callback deletes
921
- (object_->*method_)(arg1_);
922
- if (needs_delete) delete this;
923
- }
924
-
925
- private:
926
- Class* object_;
927
- MethodType method_;
928
- bool self_deleting_;
929
- Arg1 arg1_;
930
- };
931
-
932
- template <typename Arg1, typename Arg2>
933
- class FunctionClosure2 : public Closure {
934
- public:
935
- typedef void (*FunctionType)(Arg1 arg1, Arg2 arg2);
936
-
937
- FunctionClosure2(FunctionType function, bool self_deleting,
938
- Arg1 arg1, Arg2 arg2)
939
- : function_(function), self_deleting_(self_deleting),
940
- arg1_(arg1), arg2_(arg2) {}
941
- ~FunctionClosure2() {}
942
-
943
- void Run() {
944
- bool needs_delete = self_deleting_; // read in case callback deletes
945
- function_(arg1_, arg2_);
946
- if (needs_delete) delete this;
947
- }
948
-
949
- private:
950
- FunctionType function_;
951
- bool self_deleting_;
952
- Arg1 arg1_;
953
- Arg2 arg2_;
954
- };
955
-
956
- template <typename Class, typename Arg1, typename Arg2>
957
- class MethodClosure2 : public Closure {
958
- public:
959
- typedef void (Class::*MethodType)(Arg1 arg1, Arg2 arg2);
960
-
961
- MethodClosure2(Class* object, MethodType method, bool self_deleting,
962
- Arg1 arg1, Arg2 arg2)
963
- : object_(object), method_(method), self_deleting_(self_deleting),
964
- arg1_(arg1), arg2_(arg2) {}
965
- ~MethodClosure2() {}
966
-
967
- void Run() {
968
- bool needs_delete = self_deleting_; // read in case callback deletes
969
- (object_->*method_)(arg1_, arg2_);
970
- if (needs_delete) delete this;
971
- }
972
-
973
- private:
974
- Class* object_;
975
- MethodType method_;
976
- bool self_deleting_;
977
- Arg1 arg1_;
978
- Arg2 arg2_;
979
- };
980
-
981
- } // namespace internal
982
-
983
- // See Closure.
984
- inline Closure* NewCallback(void (*function)()) {
985
- return new internal::FunctionClosure0(function, true);
986
- }
987
-
988
- // See Closure.
989
- inline Closure* NewPermanentCallback(void (*function)()) {
990
- return new internal::FunctionClosure0(function, false);
991
- }
992
-
993
- // See Closure.
994
- template <typename Class>
995
- inline Closure* NewCallback(Class* object, void (Class::*method)()) {
996
- return new internal::MethodClosure0<Class>(object, method, true);
997
- }
998
-
999
- // See Closure.
1000
- template <typename Class>
1001
- inline Closure* NewPermanentCallback(Class* object, void (Class::*method)()) {
1002
- return new internal::MethodClosure0<Class>(object, method, false);
1003
- }
1004
-
1005
- // See Closure.
1006
- template <typename Arg1>
1007
- inline Closure* NewCallback(void (*function)(Arg1),
1008
- Arg1 arg1) {
1009
- return new internal::FunctionClosure1<Arg1>(function, true, arg1);
1010
- }
1011
-
1012
- // See Closure.
1013
- template <typename Arg1>
1014
- inline Closure* NewPermanentCallback(void (*function)(Arg1),
1015
- Arg1 arg1) {
1016
- return new internal::FunctionClosure1<Arg1>(function, false, arg1);
1017
- }
1018
-
1019
- // See Closure.
1020
- template <typename Class, typename Arg1>
1021
- inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1),
1022
- Arg1 arg1) {
1023
- return new internal::MethodClosure1<Class, Arg1>(object, method, true, arg1);
1024
- }
1025
-
1026
- // See Closure.
1027
- template <typename Class, typename Arg1>
1028
- inline Closure* NewPermanentCallback(Class* object, void (Class::*method)(Arg1),
1029
- Arg1 arg1) {
1030
- return new internal::MethodClosure1<Class, Arg1>(object, method, false, arg1);
1031
- }
1032
-
1033
- // See Closure.
1034
- template <typename Arg1, typename Arg2>
1035
- inline Closure* NewCallback(void (*function)(Arg1, Arg2),
1036
- Arg1 arg1, Arg2 arg2) {
1037
- return new internal::FunctionClosure2<Arg1, Arg2>(
1038
- function, true, arg1, arg2);
1039
- }
1040
-
1041
- // See Closure.
1042
- template <typename Arg1, typename Arg2>
1043
- inline Closure* NewPermanentCallback(void (*function)(Arg1, Arg2),
1044
- Arg1 arg1, Arg2 arg2) {
1045
- return new internal::FunctionClosure2<Arg1, Arg2>(
1046
- function, false, arg1, arg2);
1047
- }
1048
-
1049
- // See Closure.
1050
- template <typename Class, typename Arg1, typename Arg2>
1051
- inline Closure* NewCallback(Class* object, void (Class::*method)(Arg1, Arg2),
1052
- Arg1 arg1, Arg2 arg2) {
1053
- return new internal::MethodClosure2<Class, Arg1, Arg2>(
1054
- object, method, true, arg1, arg2);
1055
- }
1056
-
1057
- // See Closure.
1058
- template <typename Class, typename Arg1, typename Arg2>
1059
- inline Closure* NewPermanentCallback(
1060
- Class* object, void (Class::*method)(Arg1, Arg2),
1061
- Arg1 arg1, Arg2 arg2) {
1062
- return new internal::MethodClosure2<Class, Arg1, Arg2>(
1063
- object, method, false, arg1, arg2);
1064
- }
1065
-
1066
- // A function which does nothing. Useful for creating no-op callbacks, e.g.:
1067
- // Closure* nothing = NewCallback(&DoNothing);
1068
- void LIBPROTOBUF_EXPORT DoNothing();
1069
-
1070
- // ===================================================================
1071
- // emulates google3/base/mutex.h
1072
-
1073
- namespace internal {
1074
-
1075
- // A Mutex is a non-reentrant (aka non-recursive) mutex. At most one thread T
1076
- // may hold a mutex at a given time. If T attempts to Lock() the same Mutex
1077
- // while holding it, T will deadlock.
1078
- class LIBPROTOBUF_EXPORT Mutex {
1079
- public:
1080
- // Create a Mutex that is not held by anybody.
1081
- Mutex();
1082
-
1083
- // Destructor
1084
- ~Mutex();
1085
-
1086
- // Block if necessary until this Mutex is free, then acquire it exclusively.
1087
- void Lock();
1088
-
1089
- // Release this Mutex. Caller must hold it exclusively.
1090
- void Unlock();
1091
-
1092
- // Crash if this Mutex is not held exclusively by this thread.
1093
- // May fail to crash when it should; will never crash when it should not.
1094
- void AssertHeld();
1095
-
1096
- private:
1097
- struct Internal;
1098
- Internal* mInternal;
1099
-
1100
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Mutex);
1101
- };
1102
-
1103
- // MutexLock(mu) acquires mu when constructed and releases it when destroyed.
1104
- class LIBPROTOBUF_EXPORT MutexLock {
1105
- public:
1106
- explicit MutexLock(Mutex *mu) : mu_(mu) { this->mu_->Lock(); }
1107
- ~MutexLock() { this->mu_->Unlock(); }
1108
- private:
1109
- Mutex *const mu_;
1110
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLock);
1111
- };
1112
-
1113
- // TODO(kenton): Implement these? Hard to implement portably.
1114
- typedef MutexLock ReaderMutexLock;
1115
- typedef MutexLock WriterMutexLock;
1116
-
1117
- // MutexLockMaybe is like MutexLock, but is a no-op when mu is NULL.
1118
- class LIBPROTOBUF_EXPORT MutexLockMaybe {
1119
- public:
1120
- explicit MutexLockMaybe(Mutex *mu) :
1121
- mu_(mu) { if (this->mu_ != NULL) { this->mu_->Lock(); } }
1122
- ~MutexLockMaybe() { if (this->mu_ != NULL) { this->mu_->Unlock(); } }
1123
- private:
1124
- Mutex *const mu_;
1125
- GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MutexLockMaybe);
1126
- };
1127
-
1128
- } // namespace internal
1129
-
1130
- // We made these internal so that they would show up as such in the docs,
1131
- // but we don't want to stick "internal::" in front of them everywhere.
1132
- using internal::Mutex;
1133
- using internal::MutexLock;
1134
- using internal::ReaderMutexLock;
1135
- using internal::WriterMutexLock;
1136
- using internal::MutexLockMaybe;
1137
-
1138
- // ===================================================================
1139
- // from google3/base/type_traits.h
1140
-
1141
- namespace internal {
1142
-
1143
- // Specified by TR1 [4.7.4] Pointer modifications.
1144
- template<typename T> struct remove_pointer { typedef T type; };
1145
- template<typename T> struct remove_pointer<T*> { typedef T type; };
1146
- template<typename T> struct remove_pointer<T* const> { typedef T type; };
1147
- template<typename T> struct remove_pointer<T* volatile> { typedef T type; };
1148
- template<typename T> struct remove_pointer<T* const volatile> {
1149
- typedef T type; };
1150
-
1151
- // ===================================================================
1152
-
1153
- // Checks if the buffer contains structurally-valid UTF-8. Implemented in
1154
- // structurally_valid.cc.
1155
- LIBPROTOBUF_EXPORT bool IsStructurallyValidUTF8(const char* buf, int len);
1156
-
1157
- } // namespace internal
1158
-
1159
- // ===================================================================
1160
- // Shutdown support.
1161
-
1162
- // Shut down the entire protocol buffers library, deleting all static-duration
1163
- // objects allocated by the library or by generated .pb.cc files.
1164
- //
1165
- // There are two reasons you might want to call this:
1166
- // * You use a draconian definition of "memory leak" in which you expect
1167
- // every single malloc() to have a corresponding free(), even for objects
1168
- // which live until program exit.
1169
- // * You are writing a dynamically-loaded library which needs to clean up
1170
- // after itself when the library is unloaded.
1171
- //
1172
- // It is safe to call this multiple times. However, it is not safe to use
1173
- // any other part of the protocol buffers library after
1174
- // ShutdownProtobufLibrary() has been called.
1175
- LIBPROTOBUF_EXPORT void ShutdownProtobufLibrary();
1176
-
1177
- namespace internal {
1178
-
1179
- // Register a function to be called when ShutdownProtocolBuffers() is called.
1180
- LIBPROTOBUF_EXPORT void OnShutdown(void (*func)());
1181
-
1182
- } // namespace internal
1183
-
1184
- #ifdef PROTOBUF_USE_EXCEPTIONS
1185
- class FatalException : public std::exception {
1186
- public:
1187
- FatalException(const char* filename, int line, const std::string& message)
1188
- : filename_(filename), line_(line), message_(message) {}
1189
- virtual ~FatalException() throw();
1190
-
1191
- virtual const char* what() const throw();
1192
-
1193
- const char* filename() const { return filename_; }
1194
- int line() const { return line_; }
1195
- const std::string& message() const { return message_; }
1196
-
1197
- private:
1198
- const char* filename_;
1199
- const int line_;
1200
- const std::string message_;
1201
- };
1202
- #endif
1203
-
1204
- // This is at the end of the file instead of the beginning to work around a bug
1205
- // in some versions of MSVC.
1206
- using namespace std; // Don't do this at home, kids.
1207
-
1208
- } // namespace protobuf
1209
- } // namespace google
1210
-
1211
- #endif // GOOGLE_PROTOBUF_COMMON_H__