Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 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> |
Christopher Wiley | 8a65797 | 2015-10-28 16:03:45 -0700 | [diff] [blame] | 22 | #include <set> |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 23 | #include <vector> |
| 24 | |
Elliott Hughes | 0a62067 | 2015-12-04 13:53:18 -0800 | [diff] [blame] | 25 | #include <android-base/macros.h> |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 26 | |
| 27 | #include "type_namespace.h" |
| 28 | |
| 29 | namespace android { |
| 30 | namespace aidl { |
| 31 | namespace cpp { |
| 32 | |
| 33 | class Type : public ValidatableType { |
| 34 | public: |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 35 | Type(int kind, // from ValidatableType |
| 36 | const std::string& package, |
Casey Dahlin | ce776cf | 2015-10-15 18:45:54 -0700 | [diff] [blame] | 37 | const std::string& aidl_type, |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 38 | const std::vector<std::string>& header, |
Casey Dahlin | b096661 | 2015-10-19 16:35:26 -0700 | [diff] [blame] | 39 | const std::string& cpp_type, |
| 40 | const std::string& read_method, |
| 41 | const std::string& write_method, |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 42 | Type* array_type = nullptr, |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 43 | Type* nullable_type = nullptr, |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 44 | const std::string& src_file_name = "", |
| 45 | int line = -1); |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 46 | virtual ~Type() = default; |
| 47 | |
| 48 | // overrides of ValidatableType |
Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 49 | bool CanBeOutParameter() const override { return false; } |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 50 | bool CanWriteToParcel() const override; |
| 51 | |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 52 | const Type* ArrayType() const override { return array_type_.get(); } |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 53 | const Type* NullableType() const override { return nullable_type_.get(); } |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 54 | std::string CppType() const { return cpp_type_; } |
| 55 | const std::string& ReadFromParcelMethod() const { |
| 56 | return parcel_read_method_; |
| 57 | } |
| 58 | const std::string& WriteToParcelMethod() const { |
| 59 | return parcel_write_method_; |
| 60 | } |
| 61 | |
| 62 | void GetHeaders(std::set<std::string>* headers) const { |
Chih-Hung Hsieh | f05cc26 | 2016-07-27 11:42:51 -0700 | [diff] [blame] | 63 | for (const std::string& header : headers_) { |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 64 | if (!header.empty()) { |
| 65 | headers->insert(header); |
| 66 | } |
| 67 | } |
| 68 | } |
Christopher Wiley | b8e49a4 | 2015-10-27 12:55:18 -0700 | [diff] [blame] | 69 | virtual bool IsCppPrimitive() const { return false; } |
Casey Dahlin | 389781f | 2015-10-22 13:13:21 -0700 | [diff] [blame] | 70 | virtual std::string WriteCast(const std::string& value) const { |
| 71 | return value; |
| 72 | } |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 73 | |
| 74 | private: |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 75 | // |headers| are the headers we must include to use this type |
| 76 | const std::vector<std::string> headers_; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 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_; |
Casey Dahlin | a2f77c4 | 2015-12-01 18:26:02 -0800 | [diff] [blame] | 83 | |
| 84 | const std::unique_ptr<Type> array_type_; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 85 | const std::unique_ptr<Type> nullable_type_; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 86 | |
| 87 | DISALLOW_COPY_AND_ASSIGN(Type); |
| 88 | }; // class Type |
| 89 | |
Christopher Wiley | 09af469 | 2015-10-30 11:48:25 -0700 | [diff] [blame] | 90 | class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> { |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 91 | public: |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 92 | TypeNamespace() = default; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 93 | virtual ~TypeNamespace() = default; |
| 94 | |
Christopher Wiley | 5679952 | 2015-10-31 10:17:04 -0700 | [diff] [blame] | 95 | void Init() override; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 96 | bool AddParcelableType(const AidlParcelable& p, |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 97 | const std::string& filename) override; |
Casey Dahlin | c1f39b4 | 2015-11-24 10:34:34 -0800 | [diff] [blame] | 98 | bool AddBinderType(const AidlInterface& b, |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 99 | const std::string& filename) override; |
Christopher Wiley | cd8e897 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 100 | bool AddListType(const std::string& type_name) override; |
| 101 | bool AddMapType(const std::string& key_type_name, |
| 102 | const std::string& value_type_name) override; |
| 103 | |
Christopher Wiley | b656a3b | 2015-10-16 11:11:09 -0700 | [diff] [blame] | 104 | bool IsValidPackage(const std::string& package) const override; |
Casey Dahlin | 57dbe24 | 2015-12-04 11:44:02 -0800 | [diff] [blame] | 105 | const ValidatableType* GetArgType(const AidlArgument& a, |
| 106 | int arg_index, |
Casey Dahlin | c5afb40 | 2016-03-01 13:54:05 -0800 | [diff] [blame] | 107 | const std::string& filename, |
| 108 | const AidlInterface& interface) const override; |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 109 | |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 110 | const Type* VoidType() const { return void_type_; } |
Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 111 | const Type* IBinderType() const { return ibinder_type_; } |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 112 | |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 113 | private: |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 114 | Type* void_type_ = nullptr; |
Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 115 | Type* string_type_ = nullptr; |
Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 116 | Type* ibinder_type_ = nullptr; |
Christopher Wiley | ad33927 | 2015-10-05 19:11:58 -0700 | [diff] [blame] | 117 | |
Christopher Wiley | a8a2dc0 | 2015-09-28 16:35:31 -0700 | [diff] [blame] | 118 | DISALLOW_COPY_AND_ASSIGN(TypeNamespace); |
| 119 | }; // class TypeNamespace |
| 120 | |
| 121 | } // namespace cpp |
| 122 | } // namespace aidl |
| 123 | } // namespace android |
| 124 | |
| 125 | #endif // AIDL_TYPE_NAMESPACE_CPP_H_ |