Debian Codegraph @

src/compiler/objective_c_generator_helpers.h

raw
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
24 #include <google/protobuf/compiler/objectivec/objectivec_helpers.h>
25
26 #include "src/compiler/config.h"
27 #include "src/compiler/generator_helpers.h"
28
29 namespace grpc_objective_c_generator {
30
31 using ::grpc::protobuf::FileDescriptor;
32 using ::grpc::protobuf::MethodDescriptor;
33 using ::grpc::protobuf::ServiceDescriptor;
34 using ::std::string;
35
36 inline string MessageHeaderName(const FileDescriptor* file) {
37 return google::protobuf::compiler::objectivec::FilePath(file) + ".pbobjc.h";
38 }
39
40 inline bool AsciiIsUpper(char c) { return c >= 'A' && c <= 'Z'; }
41
42 inline ::std::string ServiceClassName(const ServiceDescriptor* service) {
43 const FileDescriptor* file = service->file();
44 ::std::string prefix =
45 google::protobuf::compiler::objectivec::FileClassPrefix(file);
46 ::std::string class_name = service->name();
47 // We add the prefix in the cases where the string is missing a prefix.
48 // We define "missing a prefix" as where 'input':
49 // a) Doesn't start with the prefix or
50 // b) Isn't equivalent to the prefix or
51 // c) Has the prefix, but the letter after the prefix is lowercase
52 // This is the same semantics as the Objective-C protoc.
53 // https://github.com/protocolbuffers/protobuf/blob/c160ae52a91ca4c76936531d68cc846f8230dbb1/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc#L389
54 if (class_name.rfind(prefix, 0) == 0) {
55 if (class_name.length() == prefix.length() ||
56 !AsciiIsUpper(class_name[prefix.length()])) {
57 return prefix + class_name;
58 } else {
59 return class_name;
60 }
61 } else {
62 return prefix + class_name;
63 }
64 }
65
66 inline ::std::string LocalImport(const ::std::string& import) {
67 return ::std::string("#import \"" + import + "\"\n");
68 }
69
70 inline ::std::string FrameworkImport(const ::std::string& import,
71 const ::std::string& framework) {
72 // Flattens the directory structure: grab the file name only
73 std::size_t pos = import.rfind("/");
74 // If pos is npos, pos + 1 is 0, which gives us the entire string,
75 // so there's no need to check that
76 ::std::string filename = import.substr(pos + 1, import.size() - (pos + 1));
77 return ::std::string("#import <" + framework + "/" + filename + ">\n");
78 }
79
80 inline ::std::string SystemImport(const ::std::string& import) {
81 return ::std::string("#import <" + import + ">\n");
82 }
83
84 inline ::std::string PreprocConditional(::std::string symbol, bool invert) {
85 return invert ? "!defined(" + symbol + ") || !" + symbol
86 : "defined(" + symbol + ") && " + symbol;
87 }
88
89 inline ::std::string PreprocIf(const ::std::string& symbol,
90 const ::std::string& if_true) {
91 return ::std::string("#if " + PreprocConditional(symbol, false) + "\n" +
92 if_true + "#endif\n");
93 }
94
95 inline ::std::string PreprocIfNot(const ::std::string& symbol,
96 const ::std::string& if_true) {
97 return ::std::string("#if " + PreprocConditional(symbol, true) + "\n" +
98 if_true + "#endif\n");
99 }
100
101 inline ::std::string PreprocIfElse(const ::std::string& symbol,
102 const ::std::string& if_true,
103 const ::std::string& if_false) {
104 return ::std::string("#if " + PreprocConditional(symbol, false) + "\n" +
105 if_true + "#else\n" + if_false + "#endif\n");
106 }
107
108 inline ::std::string PreprocIfNotElse(const ::std::string& symbol,
109 const ::std::string& if_true,
110 const ::std::string& if_false) {
111 return ::std::string("#if " + PreprocConditional(symbol, true) + "\n" +
112 if_true + "#else\n" + if_false + "#endif\n");
113 }
114
115 inline bool ShouldIncludeMethod(const MethodDescriptor* method) {
116 #ifdef OBJC_SKIP_METHODS_WITHOUT_MESSAGE_PREFIX
117 return (method->input_type()->file()->options().has_objc_class_prefix() &&
118 method->output_type()->file()->options().has_objc_class_prefix());
119 #else
120 (void)method; // to silence the unused warning for method.
121 return true;
122 #endif
123 }
124
125 } // namespace grpc_objective_c_generator
126 #endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H