1 /*
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 #pragma once
18 
19 #include <string>
20 #include <type_traits>
21 
22 #include "aidl_language.h"
23 
24 // This is used to help generate code targetting C++ (the language) whether using the libbinder or
25 // libbinder_ndk backend.
26 
27 namespace android {
28 namespace aidl {
29 namespace cpp {
30 
31 // These roughly correspond to the various class names in the C++ hierarchy:
32 enum class ClassNames {
33   BASE,          // Foo (not a real class, but useful in some circumstances).
34   CLIENT,        // BpFoo
35   SERVER,        // BnFoo
36   INTERFACE,     // IFoo
37   DEFAULT_IMPL,  // IFooDefault
38   RAW,           // (as shown in the file)
39 };
40 
41 string ClassName(const AidlDefinedType& defined_type, ClassNames type);
42 
43 // Generate the relative path to a header file.  If |use_os_sep| we'll use the
44 // operating system specific path separator rather than C++'s expected '/' when
45 // including headers.
46 std::string HeaderFile(const AidlDefinedType& defined_type, ClassNames class_type,
47                        bool use_os_sep = true);
48 
49 void EnterNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
50 void LeaveNamespace(CodeWriter& out, const AidlDefinedType& defined_type);
51 
52 string BuildVarName(const AidlArgument& a);
53 const string GenLogBeforeExecute(const string className, const AidlMethod& method, bool isServer,
54                                  bool isNdk);
55 const string GenLogAfterExecute(const string className, const AidlInterface& interface,
56                                 const AidlMethod& method, const string& statusVarName,
57                                 const string& returnVarName, bool isServer, bool isNdk);
58 
59 template <typename T, typename = std::enable_if_t<std::is_copy_constructible_v<T>>>
Append(std::vector<T> as,const std::vector<T> & bs)60 std::vector<T> Append(std::vector<T> as, const std::vector<T>& bs) {
61   as.insert(as.end(), bs.begin(), bs.end());
62   return as;
63 }
64 
65 template <typename T>
Append(std::vector<T> && as,std::vector<T> && bs)66 std::vector<T> Append(std::vector<T>&& as, std::vector<T>&& bs) {
67   std::vector<T> appended = std::move(as);
68   std::copy(std::move_iterator(bs.begin()), std::move_iterator(bs.end()),
69             std::back_inserter(appended));
70   return appended;
71 }
72 
73 std::string GenerateEnumValues(const AidlEnumDeclaration& enum_decl,
74                                const std::vector<std::string>& enclosing_namespaces_of_enum_decl);
75 
76 }  // namespace cpp
77 }  // namespace aidl
78 }  // namespace android
79