blob: b0615df1ebd2490e71a663e7a895e159ef46e48a [file] [log] [blame]
Austin Schuh43c6a352019-09-30 22:22:10 -07001#include "aos/flatbuffer_utils.h"
Brian Silvermancf4fb662021-02-10 17:54:53 -08002
3#include "flatbuffers/minireflect.h"
4#include "glog/logging.h"
5
6namespace aos {
7
8bool FlatbufferType::IsSequence() const {
9 if (type_table_) {
10 return type_table_->st != flatbuffers::ST_ENUM;
11 }
12 LOG(FATAL) << "Unimplemented";
13}
14
15bool FlatbufferType::IsEnum() const {
16 if (type_table_) {
17 return type_table_->st == flatbuffers::ST_ENUM;
18 }
19 LOG(FATAL) << "Unimplemented";
20}
21
22bool FlatbufferType::FieldIsSequence(int index) const {
23 DCHECK(IsSequence());
24 if (type_table_) {
25 DCHECK_LT(static_cast<size_t>(index), type_table_->num_elems);
26 const flatbuffers::TypeCode &type_code = type_table_->type_codes[index];
27 if (type_code.base_type != flatbuffers::ET_SEQUENCE) {
28 return false;
29 }
30 DCHECK(FieldType(index).IsSequence());
31 return true;
32 }
33 LOG(FATAL) << "Unimplemented";
34}
35
36bool FlatbufferType::FieldIsEnum(int index) const {
37 DCHECK(IsSequence());
38 if (type_table_) {
39 DCHECK_LT(static_cast<size_t>(index), type_table_->num_elems);
40 const flatbuffers::TypeCode &type_code = type_table_->type_codes[index];
41 if (type_code.base_type == flatbuffers::ET_SEQUENCE) {
42 return false;
43 }
44 if (type_code.sequence_ref == -1) {
45 // Not an enum.
46 return false;
47 }
48 DCHECK(FieldType(index).IsEnum());
49 return true;
50 }
51 LOG(FATAL) << "Unimplemented";
52}
53
54std::optional<int64_t> FlatbufferType::EnumValue(std::string_view name) const {
55 DCHECK(IsEnum());
56 if (type_table_) {
57 for (size_t i = 0; i < type_table_->num_elems; ++i) {
58 if (name == type_table_->names[i]) {
59 if (type_table_->values) {
60 return type_table_->values[i];
61 } else {
62 return i;
63 }
64 }
65 }
66 return std::nullopt;
67 }
68 LOG(FATAL) << "Unimplemented";
69}
70
71bool FlatbufferType::FieldIsRepeating(int index) const {
72 DCHECK(IsSequence());
73 if (type_table_) {
74 DCHECK_LT(static_cast<size_t>(index), type_table_->num_elems);
75 const flatbuffers::TypeCode &type_code = type_table_->type_codes[index];
76 return type_code.is_repeating;
77 }
78 LOG(FATAL) << "Unimplemented";
79}
80
81int FlatbufferType::FieldIndex(std::string_view field_name) const {
82 DCHECK(IsSequence());
83 if (type_table_) {
84 for (size_t i = 0; i < type_table_->num_elems; ++i) {
85 if (field_name == std::string_view(type_table_->names[i])) {
86 return i;
87 }
88 }
89 return -1;
90 }
91 LOG(FATAL) << "Unimplemented";
92}
93
94std::string_view FlatbufferType::FieldName(int index) const {
95 DCHECK(IsSequence());
96 if (type_table_) {
97 DCHECK_LT(static_cast<size_t>(index), type_table_->num_elems);
98 return type_table_->names[index];
99 }
100 LOG(FATAL) << "Unimplemented";
101}
102
103flatbuffers::ElementaryType FlatbufferType::FieldElementaryType(
104 int index) const {
105 DCHECK(IsSequence());
106 if (type_table_) {
107 DCHECK_LT(static_cast<size_t>(index), type_table_->num_elems);
108 const flatbuffers::TypeCode &type_code = type_table_->type_codes[index];
109 return static_cast<flatbuffers::ElementaryType>(type_code.base_type);
110 }
111 LOG(FATAL) << "Unimplemented";
112}
113
114size_t FlatbufferType::FieldInlineSize(int index) const {
115 DCHECK(IsSequence());
116 if (type_table_) {
117 return flatbuffers::InlineSize(FieldElementaryType(index), type_table_);
118 }
119 LOG(FATAL) << "Unimplemented";
120}
121
122int FlatbufferType::NumberFields() const {
123 DCHECK(IsSequence());
124 if (type_table_) {
125 return type_table_->num_elems;
126 }
127 LOG(FATAL) << "Unimplemented";
128}
129
130FlatbufferType FlatbufferType::FieldType(int index) const {
131 DCHECK(IsSequence());
132 if (type_table_) {
133 DCHECK_LT(static_cast<size_t>(index), type_table_->num_elems);
134 const flatbuffers::TypeCode &type_code = type_table_->type_codes[index];
135 CHECK_GE(type_code.sequence_ref, 0);
136 // type_refs can be shorter than num_elems, but not longer, so this is still
137 // a valid sanity check.
138 DCHECK_LT(static_cast<size_t>(type_code.sequence_ref),
139 type_table_->num_elems);
140 const flatbuffers::TypeFunction type_function =
141 type_table_->type_refs[type_code.sequence_ref];
142 return FlatbufferType(type_function());
143 }
144 LOG(FATAL) << "Unimplemented";
145}
146
147} // namespace aos