blob: 0a6a4a90431b1704514039e26707346dfbc271eb [file] [log] [blame]
Christopher Wileya8a2dc02015-09-28 16:35:31 -07001/*
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 Wiley8a657972015-10-28 16:03:45 -070022#include <set>
Christopher Wileya8a2dc02015-09-28 16:35:31 -070023#include <vector>
24
Elliott Hughes0a620672015-12-04 13:53:18 -080025#include <android-base/macros.h>
Christopher Wileya8a2dc02015-09-28 16:35:31 -070026
27#include "type_namespace.h"
28
29namespace android {
30namespace aidl {
31namespace cpp {
32
33class Type : public ValidatableType {
34 public:
Christopher Wiley09af4692015-10-30 11:48:25 -070035 Type(int kind, // from ValidatableType
36 const std::string& package,
Casey Dahlince776cf2015-10-15 18:45:54 -070037 const std::string& aidl_type,
Casey Dahlina2f77c42015-12-01 18:26:02 -080038 const std::vector<std::string>& header,
Casey Dahlinb0966612015-10-19 16:35:26 -070039 const std::string& cpp_type,
40 const std::string& read_method,
41 const std::string& write_method,
Casey Dahlina2f77c42015-12-01 18:26:02 -080042 Type* array_type = nullptr,
Casey Dahlin57dbe242015-12-04 11:44:02 -080043 Type* nullable_type = nullptr,
Christopher Wiley09af4692015-10-30 11:48:25 -070044 const std::string& src_file_name = "",
45 int line = -1);
Christopher Wileya8a2dc02015-09-28 16:35:31 -070046 virtual ~Type() = default;
47
48 // overrides of ValidatableType
Christopher Wiley56c9bf32015-10-30 10:41:12 -070049 bool CanBeOutParameter() const override { return false; }
Christopher Wileya8a2dc02015-09-28 16:35:31 -070050 bool CanWriteToParcel() const override;
51
Casey Dahlina2f77c42015-12-01 18:26:02 -080052 const Type* ArrayType() const override { return array_type_.get(); }
Casey Dahlin57dbe242015-12-04 11:44:02 -080053 const Type* NullableType() const override { return nullable_type_.get(); }
Casey Dahlina2f77c42015-12-01 18:26:02 -080054 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 Hsiehf05cc262016-07-27 11:42:51 -070063 for (const std::string& header : headers_) {
Casey Dahlina2f77c42015-12-01 18:26:02 -080064 if (!header.empty()) {
65 headers->insert(header);
66 }
67 }
68 }
Christopher Wileyb8e49a42015-10-27 12:55:18 -070069 virtual bool IsCppPrimitive() const { return false; }
Casey Dahlin389781f2015-10-22 13:13:21 -070070 virtual std::string WriteCast(const std::string& value) const {
71 return value;
72 }
Christopher Wileya8a2dc02015-09-28 16:35:31 -070073
74 private:
Casey Dahlina2f77c42015-12-01 18:26:02 -080075 // |headers| are the headers we must include to use this type
76 const std::vector<std::string> headers_;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070077 // |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 Dahlina2f77c42015-12-01 18:26:02 -080083
84 const std::unique_ptr<Type> array_type_;
Casey Dahlin57dbe242015-12-04 11:44:02 -080085 const std::unique_ptr<Type> nullable_type_;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070086
87 DISALLOW_COPY_AND_ASSIGN(Type);
88}; // class Type
89
Christopher Wiley09af4692015-10-30 11:48:25 -070090class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> {
Christopher Wileya8a2dc02015-09-28 16:35:31 -070091 public:
Christopher Wiley56799522015-10-31 10:17:04 -070092 TypeNamespace() = default;
Christopher Wileya8a2dc02015-09-28 16:35:31 -070093 virtual ~TypeNamespace() = default;
94
Christopher Wiley56799522015-10-31 10:17:04 -070095 void Init() override;
Casey Dahlinc1f39b42015-11-24 10:34:34 -080096 bool AddParcelableType(const AidlParcelable& p,
Christopher Wileya8a2dc02015-09-28 16:35:31 -070097 const std::string& filename) override;
Casey Dahlinc1f39b42015-11-24 10:34:34 -080098 bool AddBinderType(const AidlInterface& b,
Christopher Wileya8a2dc02015-09-28 16:35:31 -070099 const std::string& filename) override;
Christopher Wileycd8e8972015-10-26 10:24:35 -0700100 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 Wileyb656a3b2015-10-16 11:11:09 -0700104 bool IsValidPackage(const std::string& package) const override;
Casey Dahlin57dbe242015-12-04 11:44:02 -0800105 const ValidatableType* GetArgType(const AidlArgument& a,
106 int arg_index,
Casey Dahlinc5afb402016-03-01 13:54:05 -0800107 const std::string& filename,
108 const AidlInterface& interface) const override;
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700109
Christopher Wileyad339272015-10-05 19:11:58 -0700110 const Type* VoidType() const { return void_type_; }
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800111 const Type* IBinderType() const { return ibinder_type_; }
Christopher Wileyad339272015-10-05 19:11:58 -0700112
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700113 private:
Christopher Wileyad339272015-10-05 19:11:58 -0700114 Type* void_type_ = nullptr;
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700115 Type* string_type_ = nullptr;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800116 Type* ibinder_type_ = nullptr;
Christopher Wileyad339272015-10-05 19:11:58 -0700117
Christopher Wileya8a2dc02015-09-28 16:35:31 -0700118 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_