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
28namespace grpc_objective_c_generator {
29
30using ::grpc::protobuf::FileDescriptor;
31using ::grpc::protobuf::ServiceDescriptor;
32using ::grpc::string;
33
34inline string MessageHeaderName(const FileDescriptor* file) {
35 return google::protobuf::compiler::objectivec::FilePath(file) + ".pbobjc.h";
36}
37
38inline 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
44inline ::grpc::string LocalImport(const ::grpc::string& import) {
45 return ::grpc::string("#import \"" + import + "\"\n");
46}
47
48inline ::grpc::string FrameworkImport(const ::grpc::string& import,
49 const ::grpc::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 ::grpc::string filename = import.substr(pos + 1, import.size() - (pos + 1));
55 return ::grpc::string("#import <" + framework + "/" + filename + ">\n");
56}
57
58inline ::grpc::string SystemImport(const ::grpc::string& import) {
59 return ::grpc::string("#import <" + import + ">\n");
60}
61
62inline ::grpc::string PreprocConditional(::grpc::string symbol, bool invert) {
63 return invert ? "!defined(" + symbol + ") || !" + symbol
64 : "defined(" + symbol + ") && " + symbol;
65}
66
67inline ::grpc::string PreprocIf(const ::grpc::string& symbol,
68 const ::grpc::string& if_true) {
69 return ::grpc::string("#if " + PreprocConditional(symbol, false) + "\n" +
70 if_true + "#endif\n");
71}
72
73inline ::grpc::string PreprocIfNot(const ::grpc::string& symbol,
74 const ::grpc::string& if_true) {
75 return ::grpc::string("#if " + PreprocConditional(symbol, true) + "\n" +
76 if_true + "#endif\n");
77}
78
79inline ::grpc::string PreprocIfElse(const ::grpc::string& symbol,
80 const ::grpc::string& if_true,
81 const ::grpc::string& if_false) {
82 return ::grpc::string("#if " + PreprocConditional(symbol, false) + "\n" +
83 if_true + "#else\n" + if_false + "#endif\n");
84}
85
86inline ::grpc::string PreprocIfNotElse(const ::grpc::string& symbol,
87 const ::grpc::string& if_true,
88 const ::grpc::string& if_false) {
89 return ::grpc::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