Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 1 | #ifndef AOS_FLATBUFFER_UTILS_ |
| 2 | #define AOS_FLATBUFFER_UTILS_ |
| 3 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 4 | #include <optional> |
| 5 | #include <string_view> |
| 6 | |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 7 | #include "flatbuffers/flatbuffers.h" |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 8 | #include "flatbuffers/reflection_generated.h" |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 9 | |
| 10 | namespace aos { |
| 11 | |
| 12 | // Returns a human readable description of the type. |
| 13 | inline const char *ElementaryTypeName( |
| 14 | const flatbuffers::ElementaryType elementary_type) { |
| 15 | return flatbuffers::ElementaryTypeNames()[elementary_type] + 3; |
| 16 | } |
| 17 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 18 | // 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. |
| 23 | class FlatbufferType final { |
| 24 | public: |
| 25 | // Implicit on purpose, to allow freely creating a FlatbufferType. |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 26 | FlatbufferType(const flatbuffers::TypeTable *type_table); |
| 27 | FlatbufferType(const reflection::Schema *schema); |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 28 | |
| 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 Kuszmaul | 768c468 | 2023-10-12 21:07:16 -0700 | [diff] [blame] | 42 | // 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 Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 48 | // 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 Kuszmaul | 768c468 | 2023-10-12 21:07:16 -0700 | [diff] [blame] | 89 | size_t InlineSize() const; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 90 | |
| 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 Kuszmaul | 768c468 | 2023-10-12 21:07:16 -0700 | [diff] [blame] | 101 | // 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 Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 111 | private: |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 112 | explicit FlatbufferType(const reflection::Schema *schema, |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 113 | const reflection::Object *object); |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 114 | explicit FlatbufferType(const reflection::Schema *schema, |
Austin Schuh | 6bdcc37 | 2024-06-27 14:49:11 -0700 | [diff] [blame] | 115 | const reflection::Enum *fb_enum); |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 116 | |
| 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 Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 122 | const flatbuffers::TypeTable *type_table_ = nullptr; |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 123 | const reflection::Schema *schema_ = nullptr; |
| 124 | const reflection::Object *object_ = nullptr; |
| 125 | const reflection::Enum *enum_ = nullptr; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 126 | }; |
| 127 | |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 128 | } // namespace aos |
| 129 | |
| 130 | #endif // AOS_FLATBUFFER_UTILS_ |