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