3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 #ifndef GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
20 #define GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
23 #include "src/compiler/config.h"
24 #include "src/compiler/generator_helpers.h"
26 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
28 namespace grpc_objective_c_generator {
30 using ::grpc::protobuf::FileDescriptor;
31 using ::grpc::protobuf::ServiceDescriptor;
34 inline string MessageHeaderName(const FileDescriptor* file) {
35 return google::protobuf::compiler::objectivec::FilePath(file) + ".pbobjc.h";
38 inline string ServiceClassName(const ServiceDescriptor* service) {
39 const FileDescriptor* file = service->file();
40 string prefix = google::protobuf::compiler::objectivec::FileClassPrefix(file);
41 return prefix + service->name();
44 inline ::std::string LocalImport(const ::std::string& import) {
45 return ::std::string("#import \"" + import + "\"\n");
48 inline ::std::string FrameworkImport(const ::std::string& import,
49 const ::std::string& framework) {
50 // Flattens the directory structure: grab the file name only
51 std::size_t pos = import.rfind("/");
52 // If pos is npos, pos + 1 is 0, which gives us the entire string,
53 // so there's no need to check that
54 ::std::string filename = import.substr(pos + 1, import.size() - (pos + 1));
55 return ::std::string("#import <" + framework + "/" + filename + ">\n");
58 inline ::std::string SystemImport(const ::std::string& import) {
59 return ::std::string("#import <" + import + ">\n");
62 inline ::std::string PreprocConditional(::std::string symbol, bool invert) {
63 return invert ? "!defined(" + symbol + ") || !" + symbol
64 : "defined(" + symbol + ") && " + symbol;
67 inline ::std::string PreprocIf(const ::std::string& symbol,
68 const ::std::string& if_true) {
69 return ::std::string("#if " + PreprocConditional(symbol, false) + "\n" +
70 if_true + "#endif\n");
73 inline ::std::string PreprocIfNot(const ::std::string& symbol,
74 const ::std::string& if_true) {
75 return ::std::string("#if " + PreprocConditional(symbol, true) + "\n" +
76 if_true + "#endif\n");
79 inline ::std::string PreprocIfElse(const ::std::string& symbol,
80 const ::std::string& if_true,
81 const ::std::string& if_false) {
82 return ::std::string("#if " + PreprocConditional(symbol, false) + "\n" +
83 if_true + "#else\n" + if_false + "#endif\n");
86 inline ::std::string PreprocIfNotElse(const ::std::string& symbol,
87 const ::std::string& if_true,
88 const ::std::string& if_false) {
89 return ::std::string("#if " + PreprocConditional(symbol, true) + "\n" +
90 if_true + "#else\n" + if_false + "#endif\n");
93 } // namespace grpc_objective_c_generator
94 #endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H