blob: 0287b6dbc13d440626bc5b747111cc5b85e33963 [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.
27 FlatbufferType(const flatbuffers::TypeTable *type_table)
28 : type_table_(CHECK_NOTNULL(type_table)) {}
Brian Silvermanc5105ab2021-02-10 17:55:38 -080029 FlatbufferType(const reflection::Schema *schema)
30 : schema_(CHECK_NOTNULL(schema)),
31 object_(DCHECK_NOTNULL(schema->root_table())) {}
Brian Silvermancf4fb662021-02-10 17:54:53 -080032
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 Silvermanc5105ab2021-02-10 17:55:38 -080099 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 Silvermancf4fb662021-02-10 17:54:53 -0800111 const flatbuffers::TypeTable *type_table_ = nullptr;
Brian Silvermanc5105ab2021-02-10 17:55:38 -0800112 const reflection::Schema *schema_ = nullptr;
113 const reflection::Object *object_ = nullptr;
114 const reflection::Enum *enum_ = nullptr;
Brian Silvermancf4fb662021-02-10 17:54:53 -0800115};
116
Austin Schuh43c6a352019-09-30 22:22:10 -0700117} // namespace aos
118
119#endif // AOS_FLATBUFFER_UTILS_