blob: 003f14160d5119381ef3c4117df71a9bc7059312 [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.
Austin Schuh6bdcc372024-06-27 14:49:11 -070027 FlatbufferType(const flatbuffers::TypeTable *type_table);
28 FlatbufferType(const reflection::Schema *schema);
Brian Silvermancf4fb662021-02-10 17:54:53 -080029
30 // This is deliberately copyable, for ease of memory management. It is cheap
31 // to pass by value.
32 FlatbufferType(const FlatbufferType &) = default;
33 FlatbufferType(FlatbufferType &&) = default;
34 FlatbufferType &operator=(const FlatbufferType &) = default;
35 FlatbufferType &operator=(FlatbufferType &&) = default;
36
37 // Returns whether this type is a sequence (table, struct, or union).
38 bool IsSequence() const;
39
40 // Returns whether this type is an enum.
41 bool IsEnum() const;
42
James Kuszmaul768c4682023-10-12 21:07:16 -070043 // Returns whether this type is a struct.
44 bool IsStruct() const;
45
46 // Returns whether this type is a table.
47 bool IsTable() const;
48
Brian Silvermancf4fb662021-02-10 17:54:53 -080049 // Returns whether the given field is a sequence (table, struct, or union).
50 //
51 // Only valid for sequences (tables, structs, or unions).
52 bool FieldIsSequence(int index) const;
53
54 // Returns whether the given field is an enum.
55 //
56 // Only valid for sequences (tables, structs, or unions).
57 bool FieldIsEnum(int index) const;
58
59 // Returns the value for a given enumerator.
60 //
61 // Only valid for enums.
62 std::optional<int64_t> EnumValue(std::string_view name) const;
63
64 // Returns whether the given field is either a vector (in a table) or an array
65 // (in a struct).
66 //
67 // Only valid for sequences (tables, structs, or unions).
68 bool FieldIsRepeating(int index) const;
69
70 // Returns the field index in a table given the name, or -1 if the name is not
71 // found.
72 //
73 // Only valid for sequences (tables, structs, or unions).
74 int FieldIndex(std::string_view field_name) const;
75
76 // Returns the name for a field.
77 //
78 // Only valid for sequences (tables, structs, or unions).
79 std::string_view FieldName(int index) const;
80
81 // Returns the type of a field.
82 //
83 // Only valid for sequences (tables, structs, or unions).
84 flatbuffers::ElementaryType FieldElementaryType(int index) const;
85
86 // See flatbuffers::InlineSize for details.
87 //
88 // Only valid for sequences (tables, structs, or unions).
89 size_t FieldInlineSize(int index) const;
James Kuszmaul768c4682023-10-12 21:07:16 -070090 size_t InlineSize() const;
Brian Silvermancf4fb662021-02-10 17:54:53 -080091
92 // Returns the total number of fields.
93 //
94 // Only valid for sequences (tables, structs, or unions).
95 int NumberFields() const;
96
97 // Returns the type for a field.
98 //
99 // Only valid for sequences (tables, structs, or unions).
100 FlatbufferType FieldType(int index) const;
101
James Kuszmaul768c4682023-10-12 21:07:16 -0700102 // Returns the offset of the specified field within the struct.
103 //
104 // Only valid for structs.
105 size_t StructFieldOffset(int index) const;
106
107 // Returns the required alignment for this type.
108 size_t Alignment() const;
109 // The required alignment of the inline data for the specified field.
110 size_t FieldInlineAlignment(size_t field_index) const;
111
Brian Silvermancf4fb662021-02-10 17:54:53 -0800112 private:
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800113 explicit FlatbufferType(const reflection::Schema *schema,
Austin Schuh6bdcc372024-06-27 14:49:11 -0700114 const reflection::Object *object);
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800115 explicit FlatbufferType(const reflection::Schema *schema,
Austin Schuh6bdcc372024-06-27 14:49:11 -0700116 const reflection::Enum *fb_enum);
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800117
118 const reflection::Type *ReflectionType(int index) const;
119 const reflection::Field *ReflectionObjectField(int index) const;
120 const reflection::EnumVal *ReflectionEnumValue(int index) const;
121 reflection::BaseType ReflectionElementBaseType(int index) const;
122
Brian Silvermancf4fb662021-02-10 17:54:53 -0800123 const flatbuffers::TypeTable *type_table_ = nullptr;
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800124 const reflection::Schema *schema_ = nullptr;
125 const reflection::Object *object_ = nullptr;
126 const reflection::Enum *enum_ = nullptr;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800127};
128
Austin Schuh43c6a352019-09-30 22:22:10 -0700129} // namespace aos
130
131#endif // AOS_FLATBUFFER_UTILS_