type_cpp.cpp revision a8a2dc0ad258c1390c5d55b302774df357450caf
1a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley/*
2a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * Copyright (C) 2015, The Android Open Source Project
3a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *
4a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * Licensed under the Apache License, Version 2.0 (the "License");
5a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * you may not use this file except in compliance with the License.
6a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * You may obtain a copy of the License at
7a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *
8a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *     http://www.apache.org/licenses/LICENSE-2.0
9a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley *
10a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * Unless required by applicable law or agreed to in writing, software
11a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * distributed under the License is distributed on an "AS IS" BASIS,
12a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * See the License for the specific language governing permissions and
14a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley * limitations under the License.
15a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley */
16a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
17a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley#include "type_cpp.h"
18a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
19a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley#include "logging.h"
20a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
21a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyusing std::string;
22a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyusing std::unique_ptr;
23a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
24a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileynamespace android {
25a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileynamespace aidl {
26a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileynamespace cpp {
27a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
28a8a2dc0ad258c1390c5d55b302774df357450cafChristopher WileyType::Type(const string& aidl_type,
29a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley           const string& cpp_type,
30a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley           const string& read_method,
31a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley           const string& write_method)
32a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley    : aidl_type_(aidl_type),
33a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      cpp_type_(cpp_type),
34a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      parcel_read_method_(read_method),
35a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      parcel_write_method_(write_method) {
36a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
37a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
38a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileybool Type::CanBeArray() const { return false; }
39a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileybool Type::CanBeOutParameter() const { return false; }
40a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileybool Type::CanWriteToParcel() const { return false; }
41a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyconst string& Type::AidlType() const { return aidl_type_; }
42a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyconst string& Type::CppType() const { return cpp_type_; }
43a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyconst string& Type::ReadFromParcelMethod() const { return parcel_read_method_; }
44a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyconst string& Type::WriteToParcelMethod() const { return parcel_write_method_; }
45a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
46a8a2dc0ad258c1390c5d55b302774df357450cafChristopher WileyTypeNamespace::TypeNamespace() {
47a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // Note that the Java equivalent of the byte type actually calls methods
48a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // like write/readByte.  However, those are in Java, and underneath, they
49a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // write an int.
50a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  types_.emplace_back(
51a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      new Type("byte", "int8_t", "readInt32", "writeInt32"));
52a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
53a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // TODO(wiley): Implement boolean, which is an int + conversion logic.
54a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  types_.emplace_back(
55a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      new Type("int", "int32_t", "readInt32", "writeInt32"));
56a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  types_.emplace_back(
57a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      new Type("long", "int64_t", "readInt64", "writeInt64"));
58a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  types_.emplace_back(
59a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      new Type("float", "float", "readFloat", "writeFloat"));
60a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  types_.emplace_back(
61a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      new Type("double", "double", "readDouble", "writeDouble"));
62a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
63a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
64a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileybool TypeNamespace::AddParcelableType(const user_data_type* p,
65a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley                                      const string& filename) {
66a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // TODO Support parcelables b/23600712
67a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  LOG(ERROR) << "Passing parcelables in unimplemented in C++ generation.";
68a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  return false;
69a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
70a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
71a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileybool TypeNamespace::AddBinderType(const interface_type* b,
72a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley                                  const string& filename) {
73a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // TODO Support passing binders b/24470875
74a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  LOG(ERROR) << "Passing binders is unimplemented in C++ generation.";
75a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  return false;
76a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
77a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
78a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileybool TypeNamespace::AddContainerType(const string& type_name) {
79a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  // TODO Support container types b/24470786
80a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  LOG(ERROR) << "Passing container is unimplemented in C++ generation.";
81a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  return false;
82a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
83a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
84a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyconst Type* TypeNamespace::Find(const string& type_name) const {
85a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  Type* ret = nullptr;
86a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  for (const unique_ptr<Type>& type : types_) {
87a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley    if (type->AidlType() == type_name) {
88a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      ret = type.get();
89a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley      break;
90a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley    }
91a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  }
92a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
93a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  return ret;
94a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
95a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
96a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wileyconst ValidatableType* TypeNamespace::GetValidatableType(
97a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley    const string& type_name) const {
98a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley  return Find(type_name);
99a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}
100a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley
101a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}  // namespace cpp
102a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}  // namespace aidl
103a8a2dc0ad258c1390c5d55b302774df357450cafChristopher Wiley}  // namespace android
104