blob: da0d97127bddc620f5360b42f27517f44adfaa58 [file] [log] [blame]
Andreas Huber1aec3972016-08-26 09:26:32 -07001/*
2 * Copyright (C) 2016 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
Andreas Huberc9410c72016-07-28 12:18:40 -070017#include "StringType.h"
18
Iliyan Malcheva72e0d22016-09-09 11:03:08 -070019#include <hidl-util/Formatter.h>
Andreas Huberc9410c72016-07-28 12:18:40 -070020
21namespace android {
22
23StringType::StringType() {}
24
Steven Moreland979e0992016-09-07 09:18:08 -070025void StringType::addNamedTypesToSet(std::set<const FQName> &) const {
26 // do nothing
27}
28
29std::string StringType::getCppType(StorageMode mode,
30 std::string *extra,
31 bool specifyNamespaces) const {
Andreas Huber881227d2016-08-02 14:20:21 -070032 extra->clear();
33
Steven Moreland979e0992016-09-07 09:18:08 -070034 const std::string base =
35 std::string(specifyNamespaces ? "::android::hardware::" : "")
36 + "hidl_string";
Andreas Huber881227d2016-08-02 14:20:21 -070037
38 switch (mode) {
39 case StorageMode_Stack:
40 return base;
41
42 case StorageMode_Argument:
43 return "const " + base + "&";
44
45 case StorageMode_Result:
46 return "const " + base + "*";
47 }
48}
49
Andreas Huber4c865b72016-09-14 15:26:27 -070050std::string StringType::getJavaType(
51 std::string *extra, bool /* forInitializer */) const {
52 extra->clear();
Andreas Huber2831d512016-08-15 09:33:47 -070053 return "String";
54}
55
56std::string StringType::getJavaSuffix() const {
57 return "String";
58}
59
Andreas Huber881227d2016-08-02 14:20:21 -070060void StringType::emitReaderWriter(
61 Formatter &out,
62 const std::string &name,
63 const std::string &parcelObj,
64 bool parcelObjIsPointer,
65 bool isReader,
66 ErrorMode mode) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -070067 const std::string parentName = "_hidl_" + name + "_parent";
Andreas Huber881227d2016-08-02 14:20:21 -070068 out << "size_t " << parentName << ";\n\n";
69
70 const std::string parcelObjDeref =
71 parcelObj + (parcelObjIsPointer ? "->" : ".");
72
73 if (isReader) {
74 out << name
Andreas Huber8a82ff72016-08-04 10:29:39 -070075 << " = (const ::android::hardware::hidl_string *)"
Andreas Huber881227d2016-08-02 14:20:21 -070076 << parcelObjDeref
77 << "readBuffer("
78 << "&"
79 << parentName
80 << ");\n";
81
82 out << "if ("
83 << name
84 << " == nullptr) {\n";
85
86 out.indent();
87
Iliyan Malchev549e2592016-08-10 08:59:12 -070088 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070089 handleError2(out, mode);
90
91 out.unindent();
92 out << "}\n\n";
93 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070094 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -070095 << parcelObjDeref
96 << "writeBuffer(&"
97 << name
98 << ", sizeof("
99 << name
100 << "), &"
101 << parentName
102 << ");\n";
103
104 handleError(out, mode);
105 }
106
107 emitReaderWriterEmbedded(
108 out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700109 0 /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700110 name,
111 isReader /* nameIsPointer */,
112 parcelObj,
113 parcelObjIsPointer,
114 isReader,
115 mode,
116 parentName,
117 "0 /* parentOffset */");
118}
119
120void StringType::emitReaderWriterEmbedded(
121 Formatter &out,
Andreas Huberf9d49f12016-09-12 14:58:36 -0700122 size_t /* depth */,
Andreas Huber881227d2016-08-02 14:20:21 -0700123 const std::string &name,
124 bool nameIsPointer,
125 const std::string &parcelObj,
126 bool parcelObjIsPointer,
127 bool isReader,
128 ErrorMode mode,
129 const std::string &parentName,
130 const std::string &offsetText) const {
131 emitReaderWriterEmbeddedForTypeName(
132 out,
133 name,
134 nameIsPointer,
135 parcelObj,
136 parcelObjIsPointer,
137 isReader,
138 mode,
139 parentName,
140 offsetText,
Andreas Huber8a82ff72016-08-04 10:29:39 -0700141 "::android::hardware::hidl_string",
Andreas Huber881227d2016-08-02 14:20:21 -0700142 "" /* childName */);
143}
144
Andreas Huber85eabdb2016-08-25 11:24:49 -0700145void StringType::emitJavaFieldInitializer(
146 Formatter &out, const std::string &fieldName) const {
147 out << "String "
148 << fieldName
149 << " = new String();\n";
150}
151
152void StringType::emitJavaFieldReaderWriter(
153 Formatter &out,
Andreas Huber4c865b72016-09-14 15:26:27 -0700154 size_t /* depth */,
Andreas Huber709b62d2016-09-19 11:21:18 -0700155 const std::string &parcelName,
Andreas Huber85eabdb2016-08-25 11:24:49 -0700156 const std::string &blobName,
157 const std::string &fieldName,
158 const std::string &offset,
159 bool isReader) const {
160 if (isReader) {
Andreas Huber709b62d2016-09-19 11:21:18 -0700161 out << "\n"
162 << parcelName
163 << ".readEmbeddedBuffer(\n";
Andreas Huber85eabdb2016-08-25 11:24:49 -0700164
165 out.indent();
166 out.indent();
167
168 out << blobName
169 << ".handle(),\n"
170 << offset
171 << " + 0 /* offsetof(hidl_string, mBuffer) */);\n\n";
172
173 out.unindent();
174 out.unindent();
175
176 out << fieldName
177 << " = "
178 << blobName
179 << ".getString("
180 << offset
181 << ");\n";
182
183 return;
184 }
185
186 out << blobName
187 << ".putString("
188 << offset
189 << ", "
190 << fieldName
191 << ");\n";
192}
193
Andreas Huber881227d2016-08-02 14:20:21 -0700194bool StringType::needsEmbeddedReadWrite() const {
195 return true;
196}
197
198bool StringType::resultNeedsDeref() const {
199 return true;
200}
201
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700202status_t StringType::emitVtsTypeDeclarations(Formatter &out) const {
203 out << "type: TYPE_STRING\n";
204 return OK;
205}
206
Andreas Huber85eabdb2016-08-25 11:24:49 -0700207void StringType::getAlignmentAndSize(size_t *align, size_t *size) const {
208 *align = 8; // hidl_string
209 *size = 24;
210}
211
Andreas Huberc9410c72016-07-28 12:18:40 -0700212} // namespace android
213