1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "ipc_generator.h"
17 #include "ipc_generator_impl.h"
18
IpcGenerator()19 IpcGenerator::IpcGenerator() {}
20
~IpcGenerator()21 IpcGenerator::~IpcGenerator() {}
22
Generate(const google::protobuf::FileDescriptor * file,const std::string & parameter,google::protobuf::compiler::GeneratorContext * context,std::string * error) const23 bool IpcGenerator::Generate(const google::protobuf::FileDescriptor* file,
24 const std::string& parameter,
25 google::protobuf::compiler::GeneratorContext* context,
26 std::string* error) const
27 {
28 auto pcsp = std::make_shared<IpcGeneratorImpl>();
29 std::string base_name = pcsp->SetNames(file->name(), file->package());
30
31 for (int i = 0; i < file->service_count(); i++) {
32 const google::protobuf::ServiceDescriptor* service = file->service(i);
33 pcsp->AddService(service->name());
34 for (int j = 0; j < service->method_count(); j++) {
35 const google::protobuf::MethodDescriptor* method = service->method(j);
36 const google::protobuf::Descriptor* inputType = method->input_type();
37 const google::protobuf::Descriptor* outputType = method->output_type();
38 pcsp->AddServiceMethod(service->name(), method->name(), inputType->name(), outputType->name());
39 }
40 }
41
42 std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> header_output(context->Open(base_name + ".ipc.h"));
43 std::unique_ptr<::google::protobuf::io::ZeroCopyOutputStream> source_output(context->Open(base_name + ".ipc.cc"));
44
45 ::google::protobuf::io::CodedOutputStream header_out(header_output.get());
46 ::google::protobuf::io::CodedOutputStream source_out(source_output.get());
47
48 std::string header_str = pcsp->GenHeader();
49 header_out.WriteString(header_str);
50
51 std::string source_str = pcsp->GenSource();
52 source_out.WriteString(source_str);
53
54 return true;
55 }