xref: /aosp_15_r20/external/grpc-grpc-java/compiler/src/java_plugin/cpp/java_generator.h (revision e07d83d3ffcef9ecfc9f7f475418ec639ff0e5fe)
1 /*
2  * Copyright 2019 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef NET_GRPC_COMPILER_JAVA_GENERATOR_H_
18 #define NET_GRPC_COMPILER_JAVA_GENERATOR_H_
19 
20 #include <stdlib.h>  // for abort()
21 #include <iostream>
22 #include <string>
23 
24 #include <google/protobuf/io/zero_copy_stream.h>
25 #include <google/protobuf/descriptor.h>
26 
27 class LogHelper {
28   std::ostream* os;
29 
30  public:
LogHelper(std::ostream * os)31   LogHelper(std::ostream* os) : os(os) {}
~LogHelper()32   [[ noreturn ]] ~LogHelper() {
33     *os << std::endl;
34     ::abort();
35   }
get_os()36   std::ostream& get_os() {
37     return *os;
38   }
39 };
40 
41 // Abort the program after logging the message if the given condition is not
42 // true. Otherwise, do nothing.
43 #define GRPC_CODEGEN_CHECK(x) !(x) && LogHelper(&std::cerr).get_os() \
44                              << "CHECK FAILED: " << __FILE__ << ":" \
45                              << __LINE__ << ": "
46 
47 // Abort the program after logging the message.
48 #define GRPC_CODEGEN_FAIL GRPC_CODEGEN_CHECK(false)
49 
50 namespace java_grpc_generator {
51 
52 namespace impl {
53 namespace protobuf = google::protobuf;
54 } // namespace impl
55 
56 enum ProtoFlavor {
57   NORMAL, LITE
58 };
59 
60 // Returns the package name of the gRPC services defined in the given file.
61 std::string ServiceJavaPackage(const impl::protobuf::FileDescriptor* file);
62 
63 // Returns the name of the outer class that wraps in all the generated code for
64 // the given service.
65 std::string ServiceClassName(const impl::protobuf::ServiceDescriptor* service);
66 
67 // Writes the generated service interface into the given ZeroCopyOutputStream
68 void GenerateService(const impl::protobuf::ServiceDescriptor* service,
69                      impl::protobuf::io::ZeroCopyOutputStream* out,
70                      ProtoFlavor flavor,
71                      bool disable_version);
72 
73 }  // namespace java_grpc_generator
74 
75 #endif  // NET_GRPC_COMPILER_JAVA_GENERATOR_H_
76