Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1373)

Side by Side Diff: third_party/grpc/src/compiler/objective_c_plugin.cc

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 /*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
34 // Generates Objective C gRPC service interface out of Protobuf IDL.
35
36 #include <memory>
37
38 #include "src/compiler/config.h"
39 #include "src/compiler/objective_c_generator.h"
40 #include "src/compiler/objective_c_generator_helpers.h"
41
42 class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
43 public:
44 ObjectiveCGrpcGenerator() {}
45 virtual ~ObjectiveCGrpcGenerator() {}
46
47 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
48 const ::grpc::string &parameter,
49 grpc::protobuf::compiler::GeneratorContext *context,
50 ::grpc::string *error) const {
51
52 if (file->service_count() == 0) {
53 // No services. Do nothing.
54 return true;
55 }
56
57 ::grpc::string file_name = grpc_generator::FileNameInUpperCamel(file);
58 ::grpc::string prefix = file->options().objc_class_prefix();
59
60 {
61 // Generate .pbrpc.h
62
63 ::grpc::string imports = ::grpc::string("#import \"") + file_name +
64 ".pbobjc.h\"\n\n"
65 "#import <ProtoRPC/ProtoService.h>\n"
66 "#import <RxLibrary/GRXWriteable.h>\n"
67 "#import <RxLibrary/GRXWriter.h>\n";
68
69 // TODO(jcanizales): Instead forward-declare the input and output types
70 // and import the files in the .pbrpc.m
71 ::grpc::string proto_imports;
72 for (int i = 0; i < file->dependency_count(); i++) {
73 ::grpc::string header = grpc_objective_c_generator::MessageHeaderName(
74 file->dependency(i));
75 proto_imports += ::grpc::string("#import \"") + header + "\"\n";
76 }
77
78 ::grpc::string declarations;
79 for (int i = 0; i < file->service_count(); i++) {
80 const grpc::protobuf::ServiceDescriptor *service = file->service(i);
81 declarations += grpc_objective_c_generator::GetHeader(service);
82 }
83
84 Write(context, file_name + ".pbrpc.h",
85 imports + '\n' + proto_imports + '\n' + declarations);
86 }
87
88 {
89 // Generate .pbrpc.m
90
91 ::grpc::string imports = ::grpc::string("#import \"") + file_name +
92 ".pbrpc.h\"\n\n"
93 "#import <ProtoRPC/ProtoRPC.h>\n"
94 "#import <RxLibrary/GRXWriter+Immediate.h>\n";
95
96 ::grpc::string definitions;
97 for (int i = 0; i < file->service_count(); i++) {
98 const grpc::protobuf::ServiceDescriptor *service = file->service(i);
99 definitions += grpc_objective_c_generator::GetSource(service);
100 }
101
102 Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions);
103 }
104
105 return true;
106 }
107
108 private:
109 // Write the given code into the given file.
110 void Write(grpc::protobuf::compiler::GeneratorContext *context,
111 const ::grpc::string &filename, const ::grpc::string &code) const {
112 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
113 context->Open(filename));
114 grpc::protobuf::io::CodedOutputStream coded_out(output.get());
115 coded_out.WriteRaw(code.data(), code.size());
116 }
117 };
118
119 int main(int argc, char *argv[]) {
120 ObjectiveCGrpcGenerator generator;
121 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
122 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/compiler/objective_c_generator_helpers.h ('k') | third_party/grpc/src/compiler/python_generator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698