blob: 827a0b4f93b424f0fb1750a40b59c112dee7eee2 [file] [log] [blame]
Steven Morelandb0057e72018-08-27 01:44:11 -07001/*
2 * Copyright (C) 2018, The Android Open Source Project
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#include "aidl_to_cpp_common.h"
18
19#include "os.h"
20
21namespace android {
22namespace aidl {
23namespace cpp {
24
25string ClassName(const AidlDefinedType& defined_type, ClassNames type) {
26 string c_name = defined_type.GetName();
27
28 if (c_name.length() >= 2 && c_name[0] == 'I' && isupper(c_name[1])) c_name = c_name.substr(1);
29
30 switch (type) {
31 case ClassNames::CLIENT:
32 c_name = "Bp" + c_name;
33 break;
34 case ClassNames::SERVER:
35 c_name = "Bn" + c_name;
36 break;
37 case ClassNames::INTERFACE:
38 c_name = "I" + c_name;
39 break;
40 case ClassNames::DEFAULT_IMPL:
41 c_name = "I" + c_name + "Default";
42 break;
43 case ClassNames::BASE:
44 break;
45 }
46 return c_name;
47}
48
49std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
50 bool use_os_sep) {
51 std::string file_path = defined_type.GetPackage();
52 for (char& c : file_path) {
53 if (c == '.') {
54 c = (use_os_sep) ? OS_PATH_SEPARATOR : '/';
55 }
56 }
57 if (!file_path.empty()) {
58 file_path += (use_os_sep) ? OS_PATH_SEPARATOR : '/';
59 }
60 file_path += ClassName(defined_type, class_type);
61 file_path += ".h";
62
63 return file_path;
64}
65
66void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
67 const std::vector<std::string> packages = defined_type.GetSplitPackage();
68 for (const std::string& package : packages) {
69 out << "namespace " << package << " {\n";
70 }
71}
72void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type) {
73 const std::vector<std::string> packages = defined_type.GetSplitPackage();
74 for (auto it = packages.rbegin(); it != packages.rend(); ++it) {
75 out << "} // namespace " << *it << "\n";
76 }
77}
78
79} // namespace cpp
80} // namespace aidl
81} // namespace android