Skip to content

Commit 4924c99

Browse files
committed
Add conditionals for Protobuf 22+ API changes
1 parent c05239e commit 4924c99

4 files changed

Lines changed: 68 additions & 1 deletion

File tree

protoc/CppFileGenerator.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,13 @@ void FileGenerator::GenerateImplementation(Printer *printer) {
130130
"#include \"$file$.pb.h\"\n"
131131
"\n"
132132
"#include <google/protobuf/descriptor.h> // NOLINT(build/include)\n"
133+
#if GOOGLE_PROTOBUF_VERSION < 4022000
133134
"#include <google/protobuf/stubs/once.h>\n"
135+
#else
136+
"#include <absl/base/call_once.h>\n"
137+
"#include <absl/log/absl_check.h>\n"
138+
"#include <absl/log/absl_log.h>\n"
139+
#endif
134140
"\n"
135141
"#include \"common/rpc/RpcChannel.h\"\n"
136142
"#include \"common/rpc/RpcController.h\"\n"
@@ -215,7 +221,11 @@ void FileGenerator::GenerateBuildDescriptors(Printer* printer) {
215221
" \"$filename$\");\n"
216222
// Note that this GOOGLE_CHECK is necessary to prevent a warning about
217223
// "file" being unused when compiling an empty .proto file.
224+
#if GOOGLE_PROTOBUF_VERSION < 4022000
218225
"GOOGLE_CHECK(file != NULL);\n",
226+
#else
227+
"ABSL_CHECK(file != NULL);\n",
228+
#endif
219229
"filename", m_file->name());
220230

221231
for (int i = 0; i < m_file->service_count(); i++) {
@@ -248,7 +258,7 @@ void FileGenerator::GenerateBuildDescriptors(Printer* printer) {
248258
"assigndescriptorsname", GlobalAssignDescriptorsName(m_output_name));
249259

250260
printer->Print("} // namespace\n");
251-
#else
261+
#elif GOOGLE_PROTOBUF_VERSION < 4022000
252262
printer->Print(
253263
"namespace {\n"
254264
"\n"
@@ -260,6 +270,17 @@ void FileGenerator::GenerateBuildDescriptors(Printer* printer) {
260270
"\n",
261271
"assigndescriptorsname", GlobalAssignDescriptorsName(m_output_name));
262272
printer->Print("} // namespace\n");
273+
#else
274+
printer->Print(
275+
"namespace {\n"
276+
"\n"
277+
"inline void protobuf_AssignDescriptorsOnce() {\n"
278+
" static ::absl::once_flag once;\n"
279+
" ::absl::call_once(once, &$assigndescriptorsname$);\n"
280+
"}\n"
281+
"\n",
282+
"assigndescriptorsname", GlobalAssignDescriptorsName(m_output_name));
283+
printer->Print("} // namespace\n");
263284
#endif
264285
}
265286
}

protoc/ServiceGenerator.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,12 @@ void ServiceGenerator::GenerateInterface(Printer* printer) {
130130
printer->Print(vars_,
131131
"\n"
132132
" private:\n"
133+
#if GOOGLE_PROTOBUF_VERSION < 4022000
133134
" GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$);\n"
135+
#else
136+
" $classname$(const $classname$&) = delete;\n"
137+
" $classname$& operator=(const $classname$&) = delete;\n"
138+
#endif
134139
"};\n"
135140
"\n");
136141
}
@@ -178,7 +183,12 @@ void ServiceGenerator::GenerateStubDefinition(Printer* printer) {
178183
" private:\n"
179184
" ola::rpc::RpcChannel* channel_;\n"
180185
" bool owns_channel_;\n"
186+
#if GOOGLE_PROTOBUF_VERSION < 4022000
181187
" GOOGLE_DISALLOW_EVIL_CONSTRUCTORS($classname$_Stub);\n"
188+
#else
189+
" $classname$_Stub(const $classname$_Stub&) = delete;\n"
190+
" $classname$_Stub& operator=(const $classname$_Stub&) = delete;\n"
191+
#endif
182192
"};\n"
183193
"\n");
184194
}
@@ -291,7 +301,11 @@ void ServiceGenerator::GenerateCallMethod(Printer* printer) {
291301
" const ::google::protobuf::Message* request,\n"
292302
" ::google::protobuf::Message* response,\n"
293303
" ola::rpc::RpcService::CompletionCallback* done) {\n"
304+
#if GOOGLE_PROTOBUF_VERSION < 4022000
294305
" GOOGLE_DCHECK_EQ(method->service(), $classname$_descriptor_);\n"
306+
#else
307+
" ABSL_DCHECK_EQ(method->service(), $classname$_descriptor_);\n"
308+
#endif
295309
" switch (method->index()) {\n");
296310

297311
for (int i = 0; i < descriptor_->method_count(); i++) {
@@ -308,17 +322,28 @@ void ServiceGenerator::GenerateCallMethod(Printer* printer) {
308322
" case $index$:\n"
309323
" $name$(\n"
310324
" controller,\n"
325+
#if GOOGLE_PROTOBUF_VERSION < 4022000
311326
" ::google::protobuf::down_cast<\n"
312327
" const $input_type$*>(request),\n"
313328
" ::google::protobuf::down_cast<\n"
314329
" $output_type$*>(response),\n"
330+
#else
331+
" ::google::protobuf::internal::DownCast<\n"
332+
" const $input_type$*>(request),\n"
333+
" ::google::protobuf::internal::DownCast<\n"
334+
" $output_type$*>(response),\n"
335+
#endif
315336
" done);\n"
316337
" break;\n");
317338
}
318339

319340
printer->Print(vars_,
320341
" default:\n"
342+
#if GOOGLE_PROTOBUF_VERSION < 4022000
321343
" GOOGLE_LOG(FATAL) << \"Bad method index; this should never "
344+
#else
345+
" ABSL_LOG(FATAL) << \"Bad method index; this should never "
346+
#endif
322347
"happen.\";\n"
323348
" break;\n"
324349
" }\n"
@@ -339,7 +364,11 @@ void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
339364

340365
printer->Print(vars_,
341366
" const ::google::protobuf::MethodDescriptor* method) const {\n"
367+
#if GOOGLE_PROTOBUF_VERSION < 4022000
342368
" GOOGLE_DCHECK_EQ(method->service(), descriptor());\n"
369+
#else
370+
" ABSL_DCHECK_EQ(method->service(), descriptor());\n"
371+
#endif
343372
" switch (method->index()) {\n");
344373

345374
for (int i = 0; i < descriptor_->method_count(); i++) {
@@ -358,7 +387,11 @@ void ServiceGenerator::GenerateGetPrototype(RequestOrResponse which,
358387

359388
printer->Print(vars_,
360389
" default:\n"
390+
#if GOOGLE_PROTOBUF_VERSION < 4022000
361391
" GOOGLE_LOG(FATAL) << \"Bad method index; this should never happen."
392+
#else
393+
" ABSL_LOG(FATAL) << \"Bad method index; this should never happen."
394+
#endif
362395
"\";\n"
363396
" return *static_cast< ::google::protobuf::Message*>(NULL);\n"
364397
" }\n"

protoc/ServiceGenerator.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ class ServiceGenerator {
114114
const ServiceDescriptor* descriptor_;
115115
std::map<string, string> vars_;
116116

117+
#if GOOGLE_PROTOBUF_VERSION < 4022000
117118
GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceGenerator);
119+
#else
120+
ServiceGenerator(const ServiceGenerator&) = delete;
121+
ServiceGenerator& operator=(const ServiceGenerator&) = delete;
122+
#endif
118123
};
119124

120125
} // namespace ola

protoc/StrUtil.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@
5656
#include <google/protobuf/stubs/stl_util.h>
5757
#endif // HAVE_GOOGLE_PROTOBUF_STUBS_STL_UTIL_H
5858

59+
#if GOOGLE_PROTOBUF_VERSION >= 4022000
60+
#include <absl/log/absl_check.h>
61+
#include <absl/log/absl_log.h>
62+
#define GOOGLE_CHECK ABSL_CHECK
63+
#define GOOGLE_DCHECK_LT ABSL_CHECK_LT
64+
#define GOOGLE_LOG_IF(LEVEL, VECTOR) ABSL_LOG_IF(LEVEL, VECTOR)
65+
#endif
66+
5967
#ifdef _WIN32
6068
// MSVC has only _snprintf, not snprintf.
6169
//

0 commit comments

Comments
 (0)