blob: aa22e6809815aa3cb3a097f7aa4d55448045afb1 [file] [log] [blame]
Austin Schuh43c6a352019-09-30 22:22:10 -07001#ifndef AOS_FLATBUFFER_UTILS_
2#define AOS_FLATBUFFER_UTILS_
3
Brian Silvermancf4fb662021-02-10 17:54:53 -08004#include <optional>
5#include <string_view>
6
Austin Schuh43c6a352019-09-30 22:22:10 -07007#include "flatbuffers/flatbuffers.h"
Brian Silvermanc5105ab2021-02-10 17:55:38 -08008#include "flatbuffers/reflection_generated.h"
Brian Silvermancf4fb662021-02-10 17:54:53 -08009#include "glog/logging.h"
Austin Schuh43c6a352019-09-30 22:22:10 -070010
11namespace aos {
12
13// Returns a human readable description of the type.
14inline const char *ElementaryTypeName(
15 const flatbuffers::ElementaryType elementary_type) {
16 return flatbuffers::ElementaryTypeNames()[elementary_type] + 3;
17}
18
Brian Silvermancf4fb662021-02-10 17:54:53 -080019// A least-common-denominator API for a TypeTable or a Schema.
20//
21// An instance may represent an enum or a sequence (table, struct, or union).
22// Schemas have objects for the individual fields, but these are not exposed as
23// FlatbufferType instances.
24class FlatbufferType final {
25 public:
26 // Implicit on purpose, to allow freely creating a FlatbufferType.
27 FlatbufferType(const flatbuffers::TypeTable *type_table)
28 : type_table_(CHECK_NOTNULL(type_table)) {}
Brian Silvermanc5105ab2021-02-10 17:55:38 -080029 FlatbufferType(const reflection::Schema *schema)
30 : schema_(CHECK_NOTNULL(schema)),
31 object_(DCHECK_NOTNULL(schema->root_table())) {}
Brian Silvermancf4fb662021-02-10 17:54:53 -080032
33 // This is deliberately copyable, for ease of memory management. It is cheap
34 // to pass by value.
35 FlatbufferType(const FlatbufferType &) = default;
36 FlatbufferType(FlatbufferType &&) = default;
37 FlatbufferType &operator=(const FlatbufferType &) = default;
38 FlatbufferType &operator=(FlatbufferType &&) = default;
39
40 // Returns whether this type is a sequence (table, struct, or union).
41 bool IsSequence() const;
42
43 // Returns whether this type is an enum.
44 bool IsEnum() const;
45
James Kuszmaul768c4682023-10-12 21:07:16 -070046 // Returns whether this type is a struct.
47 bool IsStruct() const;
48
49 // Returns whether this type is a table.
50 bool IsTable() const;
51
Brian Silvermancf4fb662021-02-10 17:54:53 -080052 // Returns whether the given field is a sequence (table, struct, or union).
53 //
54 // Only valid for sequences (tables, structs, or unions).
55 bool FieldIsSequence(int index) const;
56
57 // Returns whether the given field is an enum.
58 //
59 // Only valid for sequences (tables, structs, or unions).
60 bool FieldIsEnum(int index) const;
61
62 // Returns the value for a given enumerator.
63 //
64 // Only valid for enums.
65 std::optional<int64_t> EnumValue(std::string_view name) const;
66
67 // Returns whether the given field is either a vector (in a table) or an array
68 // (in a struct).
69 //
70 // Only valid for sequences (tables, structs, or unions).
71 bool FieldIsRepeating(int index) const;
72
73 // Returns the field index in a table given the name, or -1 if the name is not
74 // found.
75 //
76 // Only valid for sequences (tables, structs, or unions).
77 int FieldIndex(std::string_view field_name) const;
78
79 // Returns the name for a field.
80 //
81 // Only valid for sequences (tables, structs, or unions).
82 std::string_view FieldName(int index) const;
83
84 // Returns the type of a field.
85 //
86 // Only valid for sequences (tables, structs, or unions).
87 flatbuffers::ElementaryType FieldElementaryType(int index) const;
88
89 // See flatbuffers::InlineSize for details.
90 //
91 // Only valid for sequences (tables, structs, or unions).
92 size_t FieldInlineSize(int index) const;
James Kuszmaul768c4682023-10-12 21:07:16 -070093 size_t InlineSize() const;
Brian Silvermancf4fb662021-02-10 17:54:53 -080094
95 // Returns the total number of fields.
96 //
97 // Only valid for sequences (tables, structs, or unions).
98 int NumberFields() const;
99
100 // Returns the type for a field.
101 //
102 // Only valid for sequences (tables, structs, or unions).
103 FlatbufferType FieldType(int index) const;
104
James Kuszmaul768c4682023-10-12 21:07:16 -0700105 // Returns the offset of the specified field within the struct.
106 //
107 // Only valid for structs.
108 size_t StructFieldOffset(int index) const;
109
110 // Returns the required alignment for this type.
111 size_t Alignment() const;
112 // The required alignment of the inline data for the specified field.
113 size_t FieldInlineAlignment(size_t field_index) const;
114
Brian Silvermancf4fb662021-02-10 17:54:53 -0800115 private:
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800116 explicit FlatbufferType(const reflection::Schema *schema,
117 const reflection::Object *object)
118 : schema_(DCHECK_NOTNULL(schema)), object_(DCHECK_NOTNULL(object)) {}
119 explicit FlatbufferType(const reflection::Schema *schema,
120 const reflection::Enum *fb_enum)
121 : schema_(DCHECK_NOTNULL(schema)), enum_(DCHECK_NOTNULL(fb_enum)) {}
122
123 const reflection::Type *ReflectionType(int index) const;
124 const reflection::Field *ReflectionObjectField(int index) const;
125 const reflection::EnumVal *ReflectionEnumValue(int index) const;
126 reflection::BaseType ReflectionElementBaseType(int index) const;
127
Brian Silvermancf4fb662021-02-10 17:54:53 -0800128 const flatbuffers::TypeTable *type_table_ = nullptr;
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800129 const reflection::Schema *schema_ = nullptr;
130 const reflection::Object *object_ = nullptr;
131 const reflection::Enum *enum_ = nullptr;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800132};
133
Austin Schuh43c6a352019-09-30 22:22:10 -0700134} // namespace aos
135
136#endif // AOS_FLATBUFFER_UTILS_