blob: ef51cd2a3d351cbe2e378675c44afeee6efe09a9 [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
19#include "Formatter.h"
20
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 Huber2831d512016-08-15 09:33:47 -070050std::string StringType::getJavaType() const {
51 return "String";
52}
53
54std::string StringType::getJavaSuffix() const {
55 return "String";
56}
57
Andreas Huber881227d2016-08-02 14:20:21 -070058void StringType::emitReaderWriter(
59 Formatter &out,
60 const std::string &name,
61 const std::string &parcelObj,
62 bool parcelObjIsPointer,
63 bool isReader,
64 ErrorMode mode) const {
Iliyan Malchev549e2592016-08-10 08:59:12 -070065 const std::string parentName = "_hidl_" + name + "_parent";
Andreas Huber881227d2016-08-02 14:20:21 -070066 out << "size_t " << parentName << ";\n\n";
67
68 const std::string parcelObjDeref =
69 parcelObj + (parcelObjIsPointer ? "->" : ".");
70
71 if (isReader) {
72 out << name
Andreas Huber8a82ff72016-08-04 10:29:39 -070073 << " = (const ::android::hardware::hidl_string *)"
Andreas Huber881227d2016-08-02 14:20:21 -070074 << parcelObjDeref
75 << "readBuffer("
76 << "&"
77 << parentName
78 << ");\n";
79
80 out << "if ("
81 << name
82 << " == nullptr) {\n";
83
84 out.indent();
85
Iliyan Malchev549e2592016-08-10 08:59:12 -070086 out << "_hidl_err = ::android::UNKNOWN_ERROR;\n";
Andreas Huber881227d2016-08-02 14:20:21 -070087 handleError2(out, mode);
88
89 out.unindent();
90 out << "}\n\n";
91 } else {
Iliyan Malchev549e2592016-08-10 08:59:12 -070092 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -070093 << parcelObjDeref
94 << "writeBuffer(&"
95 << name
96 << ", sizeof("
97 << name
98 << "), &"
99 << parentName
100 << ");\n";
101
102 handleError(out, mode);
103 }
104
105 emitReaderWriterEmbedded(
106 out,
107 name,
108 isReader /* nameIsPointer */,
109 parcelObj,
110 parcelObjIsPointer,
111 isReader,
112 mode,
113 parentName,
114 "0 /* parentOffset */");
115}
116
117void StringType::emitReaderWriterEmbedded(
118 Formatter &out,
119 const std::string &name,
120 bool nameIsPointer,
121 const std::string &parcelObj,
122 bool parcelObjIsPointer,
123 bool isReader,
124 ErrorMode mode,
125 const std::string &parentName,
126 const std::string &offsetText) const {
127 emitReaderWriterEmbeddedForTypeName(
128 out,
129 name,
130 nameIsPointer,
131 parcelObj,
132 parcelObjIsPointer,
133 isReader,
134 mode,
135 parentName,
136 offsetText,
Andreas Huber8a82ff72016-08-04 10:29:39 -0700137 "::android::hardware::hidl_string",
Andreas Huber881227d2016-08-02 14:20:21 -0700138 "" /* childName */);
139}
140
Andreas Huber85eabdb2016-08-25 11:24:49 -0700141void StringType::emitJavaFieldInitializer(
142 Formatter &out, const std::string &fieldName) const {
143 out << "String "
144 << fieldName
145 << " = new String();\n";
146}
147
148void StringType::emitJavaFieldReaderWriter(
149 Formatter &out,
150 const std::string &blobName,
151 const std::string &fieldName,
152 const std::string &offset,
153 bool isReader) const {
154 if (isReader) {
155 out << "\nparcel.readEmbeddedBuffer(\n";
156
157 out.indent();
158 out.indent();
159
160 out << blobName
161 << ".handle(),\n"
162 << offset
163 << " + 0 /* offsetof(hidl_string, mBuffer) */);\n\n";
164
165 out.unindent();
166 out.unindent();
167
168 out << fieldName
169 << " = "
170 << blobName
171 << ".getString("
172 << offset
173 << ");\n";
174
175 return;
176 }
177
178 out << blobName
179 << ".putString("
180 << offset
181 << ", "
182 << fieldName
183 << ");\n";
184}
185
Andreas Huber881227d2016-08-02 14:20:21 -0700186bool StringType::needsEmbeddedReadWrite() const {
187 return true;
188}
189
190bool StringType::resultNeedsDeref() const {
191 return true;
192}
193
Zhuoyao Zhang5158db42016-08-10 10:25:20 -0700194status_t StringType::emitVtsTypeDeclarations(Formatter &out) const {
195 out << "type: TYPE_STRING\n";
196 return OK;
197}
198
Andreas Huber85eabdb2016-08-25 11:24:49 -0700199void StringType::getAlignmentAndSize(size_t *align, size_t *size) const {
200 *align = 8; // hidl_string
201 *size = 24;
202}
203
Andreas Huberc9410c72016-07-28 12:18:40 -0700204} // namespace android
205