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

Side by Side Diff: third_party/grpc/src/compiler/cpp_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 cpp gRPC service interface out of Protobuf IDL.
35 //
36
37 #include <memory>
38
39 #include "src/compiler/config.h"
40
41 #include "src/compiler/cpp_generator.h"
42 #include "src/compiler/cpp_generator_helpers.h"
43
44 class CppGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
45 public:
46 CppGrpcGenerator() {}
47 virtual ~CppGrpcGenerator() {}
48
49 virtual bool Generate(const grpc::protobuf::FileDescriptor *file,
50 const grpc::string &parameter,
51 grpc::protobuf::compiler::GeneratorContext *context,
52 grpc::string *error) const {
53 if (file->options().cc_generic_services()) {
54 *error =
55 "cpp grpc proto compiler plugin does not work with generic "
56 "services. To generate cpp grpc APIs, please set \""
57 "cc_generic_service = false\".";
58 return false;
59 }
60
61 grpc_cpp_generator::Parameters generator_parameters;
62
63 if (!parameter.empty()) {
64 std::vector<grpc::string> parameters_list =
65 grpc_generator::tokenize(parameter, ",");
66 for (auto parameter_string = parameters_list.begin();
67 parameter_string != parameters_list.end();
68 parameter_string++) {
69 std::vector<grpc::string> param =
70 grpc_generator::tokenize(*parameter_string, "=");
71 if (param[0] == "services_namespace") {
72 generator_parameters.services_namespace = param[1];
73 } else {
74 *error = grpc::string("Unknown parameter: ") + *parameter_string;
75 return false;
76 }
77 }
78 }
79
80 grpc::string file_name = grpc_generator::StripProto(file->name());
81
82 grpc::string header_code =
83 grpc_cpp_generator::GetHeaderPrologue(file, generator_parameters) +
84 grpc_cpp_generator::GetHeaderIncludes(file, generator_parameters) +
85 grpc_cpp_generator::GetHeaderServices(file, generator_parameters) +
86 grpc_cpp_generator::GetHeaderEpilogue(file, generator_parameters);
87 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> header_output(
88 context->Open(file_name + ".grpc.pb.h"));
89 grpc::protobuf::io::CodedOutputStream header_coded_out(
90 header_output.get());
91 header_coded_out.WriteRaw(header_code.data(), header_code.size());
92
93 grpc::string source_code =
94 grpc_cpp_generator::GetSourcePrologue(file, generator_parameters) +
95 grpc_cpp_generator::GetSourceIncludes(file, generator_parameters) +
96 grpc_cpp_generator::GetSourceServices(file, generator_parameters) +
97 grpc_cpp_generator::GetSourceEpilogue(file, generator_parameters);
98 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> source_output(
99 context->Open(file_name + ".grpc.pb.cc"));
100 grpc::protobuf::io::CodedOutputStream source_coded_out(
101 source_output.get());
102 source_coded_out.WriteRaw(source_code.data(), source_code.size());
103
104 return true;
105 }
106
107 private:
108 // Insert the given code into the given file at the given insertion point.
109 void Insert(grpc::protobuf::compiler::GeneratorContext *context,
110 const grpc::string &filename, const grpc::string &insertion_point,
111 const grpc::string &code) const {
112 std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
113 context->OpenForInsert(filename, insertion_point));
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 CppGrpcGenerator generator;
121 return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
122 }
OLDNEW
« no previous file with comments | « third_party/grpc/src/compiler/cpp_generator_helpers.h ('k') | third_party/grpc/src/compiler/csharp_generator.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698