1 /* 2 * Copyright (C) 2015, 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 #ifndef AIDL_TYPE_CPP_H_ 18 #define AIDL_TYPE_CPP_H_ 19 20 #include <memory> 21 #include <string> 22 #include <set> 23 #include <vector> 24 25 #include <android-base/macros.h> 26 27 #include "type_namespace.h" 28 29 namespace android { 30 namespace aidl { 31 namespace cpp { 32 33 class Type : public ValidatableType { 34 public: 35 Type(int kind, // from ValidatableType 36 const std::string& package, 37 const std::string& aidl_type, 38 const std::vector<std::string>& header, 39 const std::string& cpp_type, 40 const std::string& read_method, 41 const std::string& write_method, 42 Type* array_type = nullptr, 43 Type* nullable_type = nullptr, 44 const std::string& src_file_name = "", 45 int line = -1); 46 virtual ~Type() = default; 47 48 // overrides of ValidatableType CanBeOutParameter()49 bool CanBeOutParameter() const override { return false; } 50 bool CanWriteToParcel() const override; 51 ArrayType()52 const Type* ArrayType() const override { return array_type_.get(); } NullableType()53 const Type* NullableType() const override { return nullable_type_.get(); } CppType()54 std::string CppType() const { return cpp_type_; } ReadFromParcelMethod()55 const std::string& ReadFromParcelMethod() const { 56 return parcel_read_method_; 57 } WriteToParcelMethod()58 const std::string& WriteToParcelMethod() const { 59 return parcel_write_method_; 60 } 61 GetHeaders(std::set<std::string> * headers)62 void GetHeaders(std::set<std::string>* headers) const { 63 for (std::string header : headers_) { 64 if (!header.empty()) { 65 headers->insert(header); 66 } 67 } 68 } IsCppPrimitive()69 virtual bool IsCppPrimitive() const { return false; } WriteCast(const std::string & value)70 virtual std::string WriteCast(const std::string& value) const { 71 return value; 72 } 73 74 private: 75 // |headers| are the headers we must include to use this type 76 const std::vector<std::string> headers_; 77 // |aidl_type| is what we find in the yacc generated AST (e.g. "int"). 78 const std::string aidl_type_; 79 // |cpp_type| is what we use in the generated C++ code (e.g. "int32_t"). 80 const std::string cpp_type_; 81 const std::string parcel_read_method_; 82 const std::string parcel_write_method_; 83 84 const std::unique_ptr<Type> array_type_; 85 const std::unique_ptr<Type> nullable_type_; 86 87 DISALLOW_COPY_AND_ASSIGN(Type); 88 }; // class Type 89 90 class ArrayType : public Type { 91 public: 92 ArrayType(int kind, // from ValidatableType 93 const std::string& package, 94 const std::string& aidl_type, 95 const std::vector<std::string>& header, 96 const std::string& cpp_type, 97 const std::string& read_method, 98 const std::string& write_method, 99 Type* array_type = nullptr, 100 Type* nullable_type = nullptr, 101 const std::string& src_file_name = "", 102 int line = -1) Type(kind,package,aidl_type,header,cpp_type,read_method,write_method,array_type,nullable_type,src_file_name,line)103 : Type(kind, package, aidl_type, header, cpp_type, read_method, 104 write_method, array_type, nullable_type, src_file_name, line) {} 105 CanBeOutParameter()106 bool CanBeOutParameter() const override { return true; } 107 108 virtual ~ArrayType() = default; 109 }; 110 111 class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> { 112 public: 113 TypeNamespace() = default; 114 virtual ~TypeNamespace() = default; 115 116 void Init() override; 117 bool AddParcelableType(const AidlParcelable& p, 118 const std::string& filename) override; 119 bool AddBinderType(const AidlInterface& b, 120 const std::string& filename) override; 121 bool AddListType(const std::string& type_name) override; 122 bool AddMapType(const std::string& key_type_name, 123 const std::string& value_type_name) override; 124 125 bool IsValidPackage(const std::string& package) const override; 126 const ValidatableType* GetArgType(const AidlArgument& a, 127 int arg_index, 128 const std::string& filename) const override; 129 VoidType()130 const Type* VoidType() const { return void_type_; } IBinderType()131 const Type* IBinderType() const { return ibinder_type_; } 132 133 private: 134 Type* void_type_ = nullptr; 135 Type* string_type_ = nullptr; 136 Type* ibinder_type_ = nullptr; 137 138 DISALLOW_COPY_AND_ASSIGN(TypeNamespace); 139 }; // class TypeNamespace 140 141 } // namespace cpp 142 } // namespace aidl 143 } // namespace android 144 145 #endif // AIDL_TYPE_NAMESPACE_CPP_H_ 146