blob: 90a4c86b842ac351bac5e92bd1472dc8eb6c1553 [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"
Austin Schuh43c6a352019-09-30 22:22:10 -07009
10namespace aos {
11
12// Returns a human readable description of the type.
13inline const char *ElementaryTypeName(
14 const flatbuffers::ElementaryType elementary_type) {
15 return flatbuffers::ElementaryTypeNames()[elementary_type] + 3;
16}
17
Brian Silvermancf4fb662021-02-10 17:54:53 -080018// A least-common-denominator API for a TypeTable or a Schema.
19//
20// An instance may represent an enum or a sequence (table, struct, or union).
21// Schemas have objects for the individual fields, but these are not exposed as
22// FlatbufferType instances.
23class FlatbufferType final {
24 public:
25 // Implicit on purpose, to allow freely creating a FlatbufferType.
Austin Schuh6bdcc372024-06-27 14:49:11 -070026 FlatbufferType(const flatbuffers::TypeTable *type_table);
27 FlatbufferType(const reflection::Schema *schema);
Brian Silvermancf4fb662021-02-10 17:54:53 -080028
29 // This is deliberately copyable, for ease of memory management. It is cheap
30 // to pass by value.
31 FlatbufferType(const FlatbufferType &) = default;
32 FlatbufferType(FlatbufferType &&) = default;
33 FlatbufferType &operator=(const FlatbufferType &) = default;
34 FlatbufferType &operator=(FlatbufferType &&) = default;
35
36 // Returns whether this type is a sequence (table, struct, or union).
37 bool IsSequence() const;
38
39 // Returns whether this type is an enum.
40 bool IsEnum() const;
41
James Kuszmaul768c4682023-10-12 21:07:16 -070042 // Returns whether this type is a struct.
43 bool IsStruct() const;
44
45 // Returns whether this type is a table.
46 bool IsTable() const;
47
Brian Silvermancf4fb662021-02-10 17:54:53 -080048 // Returns whether the given field is a sequence (table, struct, or union).
49 //
50 // Only valid for sequences (tables, structs, or unions).
51 bool FieldIsSequence(int index) const;
52
53 // Returns whether the given field is an enum.
54 //
55 // Only valid for sequences (tables, structs, or unions).
56 bool FieldIsEnum(int index) const;
57
58 // Returns the value for a given enumerator.
59 //
60 // Only valid for enums.
61 std::optional<int64_t> EnumValue(std::string_view name) const;
62
63 // Returns whether the given field is either a vector (in a table) or an array
64 // (in a struct).
65 //
66 // Only valid for sequences (tables, structs, or unions).
67 bool FieldIsRepeating(int index) const;
68
69 // Returns the field index in a table given the name, or -1 if the name is not
70 // found.
71 //
72 // Only valid for sequences (tables, structs, or unions).
73 int FieldIndex(std::string_view field_name) const;
74
75 // Returns the name for a field.
76 //
77 // Only valid for sequences (tables, structs, or unions).
78 std::string_view FieldName(int index) const;
79
80 // Returns the type of a field.
81 //
82 // Only valid for sequences (tables, structs, or unions).
83 flatbuffers::ElementaryType FieldElementaryType(int index) const;
84
85 // See flatbuffers::InlineSize for details.
86 //
87 // Only valid for sequences (tables, structs, or unions).
88 size_t FieldInlineSize(int index) const;
James Kuszmaul768c4682023-10-12 21:07:16 -070089 size_t InlineSize() const;
Brian Silvermancf4fb662021-02-10 17:54:53 -080090
91 // Returns the total number of fields.
92 //
93 // Only valid for sequences (tables, structs, or unions).
94 int NumberFields() const;
95
96 // Returns the type for a field.
97 //
98 // Only valid for sequences (tables, structs, or unions).
99 FlatbufferType FieldType(int index) const;
100
James Kuszmaul768c4682023-10-12 21:07:16 -0700101 // Returns the offset of the specified field within the struct.
102 //
103 // Only valid for structs.
104 size_t StructFieldOffset(int index) const;
105
106 // Returns the required alignment for this type.
107 size_t Alignment() const;
108 // The required alignment of the inline data for the specified field.
109 size_t FieldInlineAlignment(size_t field_index) const;
110
Brian Silvermancf4fb662021-02-10 17:54:53 -0800111 private:
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800112 explicit FlatbufferType(const reflection::Schema *schema,
Austin Schuh6bdcc372024-06-27 14:49:11 -0700113 const reflection::Object *object);
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800114 explicit FlatbufferType(const reflection::Schema *schema,
Austin Schuh6bdcc372024-06-27 14:49:11 -0700115 const reflection::Enum *fb_enum);
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800116
117 const reflection::Type *ReflectionType(int index) const;
118 const reflection::Field *ReflectionObjectField(int index) const;
119 const reflection::EnumVal *ReflectionEnumValue(int index) const;
120 reflection::BaseType ReflectionElementBaseType(int index) const;
121
Brian Silvermancf4fb662021-02-10 17:54:53 -0800122 const flatbuffers::TypeTable *type_table_ = nullptr;
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800123 const reflection::Schema *schema_ = nullptr;
124 const reflection::Object *object_ = nullptr;
125 const reflection::Enum *enum_ = nullptr;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800126};
127
Austin Schuh43c6a352019-09-30 22:22:10 -0700128} // namespace aos
129
130#endif // AOS_FLATBUFFER_UTILS_