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
29namespace android {
30namespace aidl {
31namespace cpp {
32
33class 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
49  bool CanBeOutParameter() const override { return false; }
50  bool CanWriteToParcel() const override;
51
52  const Type* ArrayType() const override { return array_type_.get(); }
53  const Type* NullableType() const override { return nullable_type_.get(); }
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 {
63    for (const std::string& header : headers_) {
64      if (!header.empty()) {
65        headers->insert(header);
66      }
67    }
68  }
69  virtual bool IsCppPrimitive() const { return false; }
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
90class TypeNamespace : public ::android::aidl::LanguageTypeNamespace<Type> {
91 public:
92  TypeNamespace() = default;
93  virtual ~TypeNamespace() = default;
94
95  void Init() override;
96  bool AddParcelableType(const AidlParcelable& p,
97                         const std::string& filename) override;
98  bool AddBinderType(const AidlInterface& b,
99                     const std::string& filename) override;
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
104  bool IsValidPackage(const std::string& package) const override;
105  const ValidatableType* GetArgType(const AidlArgument& a,
106                             int arg_index,
107                             const std::string& filename,
108                             const AidlInterface& interface) const override;
109
110  const Type* VoidType() const { return void_type_; }
111  const Type* IBinderType() const { return ibinder_type_; }
112
113 private:
114  Type* void_type_ = nullptr;
115  Type* string_type_ = nullptr;
116  Type* ibinder_type_ = nullptr;
117
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_
126