blob: 506c796322899249f47495b330b978acf23f5d3b [file] [log] [blame]
Andreas Huberc9410c72016-07-28 12:18:40 -07001#include "EnumType.h"
2
3#include "Formatter.h"
Andreas Huber881227d2016-08-02 14:20:21 -07004#include "ScalarType.h"
Andreas Huberc9410c72016-07-28 12:18:40 -07005
6namespace android {
7
8EnumValue::EnumValue(const char *name, const char *value)
9 : mName(name),
10 mValue(value) {
11}
12
13void EnumValue::dump(Formatter &out) const {
14 out << mName;
15
16 if (mValue) {
17 out << " = " << mValue;
18 }
19
20 out << ",";
21}
22
Andreas Huber881227d2016-08-02 14:20:21 -070023std::string EnumValue::name() const {
24 return mName;
25}
26
27const char *EnumValue::value() const {
28 return mValue;
29}
30
Andreas Huberc9410c72016-07-28 12:18:40 -070031EnumType::EnumType(
Andreas Huber881227d2016-08-02 14:20:21 -070032 const char *name, std::vector<EnumValue *> *values, Type *storageType)
Andreas Huberc9410c72016-07-28 12:18:40 -070033 : NamedType(name),
34 mValues(values),
Andreas Huber881227d2016-08-02 14:20:21 -070035 mStorageType(
36 storageType != NULL
37 ? storageType
38 : new ScalarType(ScalarType::KIND_INT32)) {
Andreas Huberc9410c72016-07-28 12:18:40 -070039}
40
41void EnumType::dump(Formatter &out) const {
42 out << "enum " << name() << " ";
43
44 if (mStorageType) {
45 out << ": ";
46 mStorageType->dump(out);
47 out << " ";
48 }
49
50 out << "{\n";
51 out.indent();
52
53 for (size_t i = 0; i < mValues->size(); ++i) {
Andreas Huber881227d2016-08-02 14:20:21 -070054 (*mValues)[i]->dump(out);
Andreas Huberc9410c72016-07-28 12:18:40 -070055 out << "\n";
56 }
57
58 out.unindent();
59
60 out << "};\n\n";
61}
62
Andreas Huber881227d2016-08-02 14:20:21 -070063std::string EnumType::getCppType(StorageMode, std::string *extra) const {
64 extra->clear();
65
66 return name();
67}
68
69void EnumType::emitReaderWriter(
70 Formatter &out,
71 const std::string &name,
72 const std::string &parcelObj,
73 bool parcelObjIsPointer,
74 bool isReader,
75 ErrorMode mode) const {
76 mStorageType->emitReaderWriter(
77 out, name, parcelObj, parcelObjIsPointer, isReader, mode);
78}
79
80status_t EnumType::emitTypeDeclarations(Formatter &out) const {
81 std::string extra;
82
83 out << "enum class "
84 << name()
85 << " : "
86 << mStorageType->getCppType(&extra)
87 << " {\n";
88
89 out.indent();
90
91 for (const auto &entry : *mValues) {
92 out << entry->name();
93
94 const char *value = entry->value();
95 if (value != NULL) {
96 out << " = " << value;
97 }
98
99 out << ",\n";
100 }
101
102 out.unindent();
103 out << "};\n\n";
104
105 return OK;
106}
107
Andreas Huberc9410c72016-07-28 12:18:40 -0700108} // namespace android
109