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" |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 9 | #include "glog/logging.h" |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 10 | |
| 11 | namespace aos { |
| 12 | |
| 13 | // Returns a human readable description of the type. |
| 14 | inline const char *ElementaryTypeName( |
| 15 | const flatbuffers::ElementaryType elementary_type) { |
| 16 | return flatbuffers::ElementaryTypeNames()[elementary_type] + 3; |
| 17 | } |
| 18 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 19 | // 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. |
| 24 | class 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 Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 29 | FlatbufferType(const reflection::Schema *schema) |
| 30 | : schema_(CHECK_NOTNULL(schema)), |
| 31 | object_(DCHECK_NOTNULL(schema->root_table())) {} |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 32 | |
| 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 Kuszmaul | 768c468 | 2023-10-12 21:07:16 -0700 | [diff] [blame] | 46 | // 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 Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 52 | // 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 Kuszmaul | 768c468 | 2023-10-12 21:07:16 -0700 | [diff] [blame] | 93 | size_t InlineSize() const; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 94 | |
| 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 Kuszmaul | 768c468 | 2023-10-12 21:07:16 -0700 | [diff] [blame] | 105 | // 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 Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 115 | private: |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 116 | 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 Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 128 | const flatbuffers::TypeTable *type_table_ = nullptr; |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 129 | const reflection::Schema *schema_ = nullptr; |
| 130 | const reflection::Object *object_ = nullptr; |
| 131 | const reflection::Enum *enum_ = nullptr; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 132 | }; |
| 133 | |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 134 | } // namespace aos |
| 135 | |
| 136 | #endif // AOS_FLATBUFFER_UTILS_ |