blob: b15734b4498f99fd21e77c3050689445762baec2 [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "ScalarType.h"
2
3#include "Formatter.h"
4
5namespace android {
6
7ScalarType::ScalarType(Kind kind)
8 : mKind(kind) {
9}
10
Andreas Huber737080b2016-08-02 15:38:04 -070011const ScalarType *ScalarType::resolveToScalarType() const {
12 return this;
13}
14
Andreas Huber8d3ac0c2016-08-04 14:49:23 -070015bool ScalarType::isValidEnumStorageType() const {
16 // Only integer types.
17 return mKind >= KIND_INT8 && mKind <= KIND_UINT64;
18}
19
Andreas Huber881227d2016-08-02 14:20:21 -070020std::string ScalarType::getCppType(StorageMode, std::string *extra) const {
21 static const char *const kName[] = {
22 "char",
23 "bool",
24 "void *",
25 "int8_t",
26 "uint8_t",
27 "int16_t",
28 "uint16_t",
29 "int32_t",
30 "uint32_t",
31 "int64_t",
32 "uint64_t",
33 "float",
34 "double"
35 };
36
37 extra->clear();
38
39 return kName[mKind];
40}
41
42void ScalarType::emitReaderWriter(
43 Formatter &out,
44 const std::string &name,
45 const std::string &parcelObj,
46 bool parcelObjIsPointer,
47 bool isReader,
48 ErrorMode mode) const {
Andreas Huber737080b2016-08-02 15:38:04 -070049 emitReaderWriterWithCast(
50 out,
51 name,
52 parcelObj,
53 parcelObjIsPointer,
54 isReader,
55 mode,
56 false /* needsCast */);
57}
58
59void ScalarType::emitReaderWriterWithCast(
60 Formatter &out,
61 const std::string &name,
62 const std::string &parcelObj,
63 bool parcelObjIsPointer,
64 bool isReader,
65 ErrorMode mode,
66 bool needsCast) const {
Andreas Huber881227d2016-08-02 14:20:21 -070067 static const char *const kSuffix[] = {
68 "Uint8",
69 "Uint8",
70 "Pointer",
71 "Int8",
72 "Uint8",
73 "Int16",
74 "Uint16",
75 "Int32",
76 "Uint32",
77 "Int64",
78 "Uint64",
79 "Float",
80 "Double"
81 };
82
83 const std::string parcelObjDeref =
84 parcelObj + (parcelObjIsPointer ? "->" : ".");
85
Iliyan Malchev549e2592016-08-10 08:59:12 -070086 out << "_hidl_err = "
Andreas Huber881227d2016-08-02 14:20:21 -070087 << parcelObjDeref
88 << (isReader ? "read" : "write")
89 << kSuffix[mKind]
90 << "(";
91
Andreas Huber737080b2016-08-02 15:38:04 -070092 if (needsCast) {
93 std::string extra;
94
95 out << "("
96 << Type::getCppType(&extra)
97 << (isReader ? " *)" : ")");
98 }
99
Andreas Huber881227d2016-08-02 14:20:21 -0700100 if (isReader) {
101 out << "&";
102 }
103
104 out << name
105 << ");\n";
106
107 handleError(out, mode);
108}
109
Andreas Huberc9410c72016-07-28 12:18:40 -0700110} // namespace android
111