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 | |
| 46 | // Returns whether the given field is a sequence (table, struct, or union). |
| 47 | // |
| 48 | // Only valid for sequences (tables, structs, or unions). |
| 49 | bool FieldIsSequence(int index) const; |
| 50 | |
| 51 | // Returns whether the given field is an enum. |
| 52 | // |
| 53 | // Only valid for sequences (tables, structs, or unions). |
| 54 | bool FieldIsEnum(int index) const; |
| 55 | |
| 56 | // Returns the value for a given enumerator. |
| 57 | // |
| 58 | // Only valid for enums. |
| 59 | std::optional<int64_t> EnumValue(std::string_view name) const; |
| 60 | |
| 61 | // Returns whether the given field is either a vector (in a table) or an array |
| 62 | // (in a struct). |
| 63 | // |
| 64 | // Only valid for sequences (tables, structs, or unions). |
| 65 | bool FieldIsRepeating(int index) const; |
| 66 | |
| 67 | // Returns the field index in a table given the name, or -1 if the name is not |
| 68 | // found. |
| 69 | // |
| 70 | // Only valid for sequences (tables, structs, or unions). |
| 71 | int FieldIndex(std::string_view field_name) const; |
| 72 | |
| 73 | // Returns the name for a field. |
| 74 | // |
| 75 | // Only valid for sequences (tables, structs, or unions). |
| 76 | std::string_view FieldName(int index) const; |
| 77 | |
| 78 | // Returns the type of a field. |
| 79 | // |
| 80 | // Only valid for sequences (tables, structs, or unions). |
| 81 | flatbuffers::ElementaryType FieldElementaryType(int index) const; |
| 82 | |
| 83 | // See flatbuffers::InlineSize for details. |
| 84 | // |
| 85 | // Only valid for sequences (tables, structs, or unions). |
| 86 | size_t FieldInlineSize(int index) const; |
| 87 | |
| 88 | // Returns the total number of fields. |
| 89 | // |
| 90 | // Only valid for sequences (tables, structs, or unions). |
| 91 | int NumberFields() const; |
| 92 | |
| 93 | // Returns the type for a field. |
| 94 | // |
| 95 | // Only valid for sequences (tables, structs, or unions). |
| 96 | FlatbufferType FieldType(int index) const; |
| 97 | |
| 98 | private: |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 99 | explicit FlatbufferType(const reflection::Schema *schema, |
| 100 | const reflection::Object *object) |
| 101 | : schema_(DCHECK_NOTNULL(schema)), object_(DCHECK_NOTNULL(object)) {} |
| 102 | explicit FlatbufferType(const reflection::Schema *schema, |
| 103 | const reflection::Enum *fb_enum) |
| 104 | : schema_(DCHECK_NOTNULL(schema)), enum_(DCHECK_NOTNULL(fb_enum)) {} |
| 105 | |
| 106 | const reflection::Type *ReflectionType(int index) const; |
| 107 | const reflection::Field *ReflectionObjectField(int index) const; |
| 108 | const reflection::EnumVal *ReflectionEnumValue(int index) const; |
| 109 | reflection::BaseType ReflectionElementBaseType(int index) const; |
| 110 | |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 111 | const flatbuffers::TypeTable *type_table_ = nullptr; |
Brian Silverman | c5105ab | 2021-02-10 17:55:38 -0800 | [diff] [blame] | 112 | const reflection::Schema *schema_ = nullptr; |
| 113 | const reflection::Object *object_ = nullptr; |
| 114 | const reflection::Enum *enum_ = nullptr; |
Brian Silverman | cf4fb66 | 2021-02-10 17:54:53 -0800 | [diff] [blame] | 115 | }; |
| 116 | |
Austin Schuh | 43c6a35 | 2019-09-30 22:22:10 -0700 | [diff] [blame] | 117 | } // namespace aos |
| 118 | |
| 119 | #endif // AOS_FLATBUFFER_UTILS_ |