1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
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
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
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.
16 *
17 */
18
19 #ifndef GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
20 #define GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
21
22 #include <map>
23 #include "src/compiler/config.h"
24 #include "src/compiler/generator_helpers.h"
25
26 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
27
28 namespace grpc_objective_c_generator {
29
30 using ::grpc::protobuf::FileDescriptor;
31 using ::grpc::protobuf::ServiceDescriptor;
32 using ::std::string;
33
MessageHeaderName(const FileDescriptor * file)34 inline string MessageHeaderName(const FileDescriptor* file) {
35 return google::protobuf::compiler::objectivec::FilePath(file) + ".pbobjc.h";
36 }
37
ServiceClassName(const ServiceDescriptor * service)38 inline string ServiceClassName(const ServiceDescriptor* service) {
39 const FileDescriptor* file = service->file();
40 string prefix = file->options().objc_class_prefix();
41 return prefix + service->name();
42 }
43
LocalImport(const::std::string & import)44 inline ::std::string LocalImport(const ::std::string& import) {
45 return ::std::string("#import \"" + import + "\"\n");
46 }
47
FrameworkImport(const::std::string & import,const::std::string & framework)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");
56 }
57
SystemImport(const::std::string & import)58 inline ::std::string SystemImport(const ::std::string& import) {
59 return ::std::string("#import <" + import + ">\n");
60 }
61
PreprocConditional(::std::string symbol,bool invert)62 inline ::std::string PreprocConditional(::std::string symbol, bool invert) {
63 return invert ? "!defined(" + symbol + ") || !" + symbol
64 : "defined(" + symbol + ") && " + symbol;
65 }
66
PreprocIf(const::std::string & symbol,const::std::string & if_true)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");
71 }
72
PreprocIfNot(const::std::string & symbol,const::std::string & if_true)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");
77 }
78
PreprocIfElse(const::std::string & symbol,const::std::string & if_true,const::std::string & if_false)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");
84 }
85
PreprocIfNotElse(const::std::string & symbol,const::std::string & if_true,const::std::string & if_false)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");
91 }
92
93 } // namespace grpc_objective_c_generator
94 #endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
95