Skip to content

Commit 9b01c9d

Browse files
Add printer class to wrap protobuf IO classes (googleapis#12)
* Add printer class * Use printer, add comments * Address PR comments
1 parent 8570c8b commit 9b01c9d

File tree

3 files changed

+65
-11
lines changed

3 files changed

+65
-11
lines changed

generator/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ cc_library(
2121
srcs = [
2222
"gapic_generator.cc",
2323
"internal/gapic_utils.h",
24+
"internal/printer.h",
2425
],
2526
hdrs = [
2627
"gapic_generator.h",

generator/gapic_generator.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "absl/strings/str_replace.h"
2222
#include "generator/gapic_generator.h"
2323
#include "internal/gapic_utils.h"
24+
#include "internal/printer.h"
2425

2526
#include "google/api/client.pb.h"
2627

@@ -69,7 +70,7 @@ std::vector<std::string> BuildNamespaces(
6970
void PrintMethods(
7071
pb::ServiceDescriptor const* service,
7172
std::map<std::string, std::string> vars,
72-
pb::io::Printer* p,
73+
internal::Printer& p,
7374
char const* tmplt) {
7475
for (int i = 0; i < service->method_count(); i++) {
7576
const pb::MethodDescriptor* method = service->method(i);
@@ -81,7 +82,7 @@ void PrintMethods(
8182
bool GenerateClientHeaderFile(
8283
pb::ServiceDescriptor const* service,
8384
std::map<std::string, std::string> const& vars,
84-
pb::io::Printer* p,
85+
internal::Printer& p,
8586
std::string * /* error */) {
8687
auto includes = BuildHeaderIncludes(service);
8788
auto namespaces = BuildNamespaces(service);
@@ -121,7 +122,7 @@ bool GenerateClientHeaderFile(
121122
bool GenerateClientCCFile(
122123
pb::ServiceDescriptor const* service,
123124
std::map<std::string, std::string> const& vars,
124-
pb::io::Printer* p,
125+
internal::Printer& p,
125126
std::string * /* error */) {
126127
auto includes = BuildCCIncludes(service);
127128
auto namespaces = BuildNamespaces(service);
@@ -174,18 +175,14 @@ bool GapicGenerator::Generate(pb::FileDescriptor const* file,
174175
service->full_name());
175176

176177
std::string header_file_path = absl::StrCat(service_file_path, ".gapic.h");
177-
std::unique_ptr<pb::io::ZeroCopyOutputStream> header_output(
178-
generator_context->Open(header_file_path));
179-
pb::io::Printer header_printer(header_output.get(), '$', NULL);
180-
if (!GenerateClientHeaderFile(service, vars, &header_printer, error)) {
178+
internal::Printer header_printer(generator_context, header_file_path);
179+
if (!GenerateClientHeaderFile(service, vars, header_printer, error)) {
181180
return false;
182181
}
183182

184183
std::string cc_file_path = absl::StrCat(service_file_path, ".gapic.cc");
185-
std::unique_ptr<pb::io::ZeroCopyOutputStream> cc_output(
186-
generator_context->Open(cc_file_path));
187-
pb::io::Printer cc_printer(cc_output.get(), '$', NULL);
188-
if (!GenerateClientCCFile(service, vars, &cc_printer, error)) {
184+
internal::Printer cc_printer(generator_context, cc_file_path);
185+
if (!GenerateClientCCFile(service, vars, cc_printer, error)) {
189186
return false;
190187
}
191188
}

generator/internal/printer.h

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2019 Google Inc. All rights reserved
2+
//
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+
#ifndef GOOGLE_API_CODEGEN_INTERNAL_PRINTER_H_
15+
#define GOOGLE_API_CODEGEN_INTERNAL_PRINTER_H_
16+
17+
#include <string>
18+
#include "src/google/protobuf/compiler/code_generator.h"
19+
#include "src/google/protobuf/io/printer.h"
20+
21+
namespace pb = google::protobuf;
22+
23+
namespace google {
24+
namespace api {
25+
namespace codegen {
26+
namespace internal {
27+
28+
/**
29+
* Wrapper around a google::protobuf::io::ZeroCopyOutputStream and a
30+
* google::protobuf::io::Printer object so that they can be used for
31+
* code generation.
32+
*/
33+
class Printer {
34+
public:
35+
Printer(pb::compiler::GeneratorContext* generator_context,
36+
std::string const& file_name)
37+
: output_(generator_context->Open(file_name)),
38+
printer_(output_.get(), '$', NULL) {}
39+
40+
pb::io::Printer* operator->() & { return &printer_; }
41+
pb::io::Printer const* operator->() const& { return &printer_; }
42+
43+
Printer(Printer const&) = delete;
44+
Printer& operator=(Printer const&) = delete;
45+
46+
private:
47+
std::unique_ptr<pb::io::ZeroCopyOutputStream> output_;
48+
pb::io::Printer printer_;
49+
};
50+
51+
} // internal
52+
} // codegen
53+
} // api
54+
} // google
55+
56+
#endif // GOOGLE_API_CODEGEN_INTERNAL_PRINTER_H_

0 commit comments

Comments
 (0)