18#ifndef FLATBUFFERS_REFLECTION_H_
19#define FLATBUFFERS_REFLECTION_H_
26#include "flatbuffers/reflection_generated.h"
37 return t >= reflection::UType && t <= reflection::Double;
41 return t >= reflection::UType && t <= reflection::ULong;
43inline bool IsFloat(reflection::BaseType t)
45 return t == reflection::Float || t == reflection::Double;
47inline bool IsLong(reflection::BaseType t)
49 return t == reflection::Long || t == reflection::ULong;
56 static size_t sizes[] = {
79 static_assert(
sizeof(sizes) /
sizeof(
size_t) == reflection::MaxBaseType + 1,
80 "Size of sizes[] array does not match the count of BaseType "
82 return sizes[base_type];
88 const reflection::Schema &schema)
90 if (base_type == reflection::Obj && schema.objects()->Get(type_index)->is_struct())
92 return schema.objects()->Get(type_index)->bytesize();
101inline Table *
GetAnyRoot(uint8_t *flatbuf) {
return GetMutableRoot<Table>(flatbuf); }
102inline const Table *
GetAnyRoot(
const uint8_t *flatbuf) {
return GetRoot<Table>(flatbuf); }
108 return static_cast<T
>(field.default_integer());
115 return static_cast<T
>(field.default_real());
119template <
typename T> T
GetFieldI(
const Table &table,
const reflection::Field &field)
122 return table.GetField<T>(field.offset(),
static_cast<T
>(field.default_integer()));
126template <
typename T> T
GetFieldF(
const Table &table,
const reflection::Field &field)
129 return table.GetField<T>(field.offset(),
static_cast<T
>(field.default_real()));
136 return table.GetPointer<
const String *>(field.offset());
143 sizeof(T) ==
GetTypeSize(field.type()->element()));
144 return table.GetPointer<
Vector<T> *>(field.offset());
152 return table.GetPointer<
VectorOfAny *>(field.offset());
156inline Table *
GetFieldT(
const Table &table,
const reflection::Field &field)
159 field.type()->base_type() == reflection::Union);
160 return table.GetPointer<Table *>(field.offset());
164inline const Struct *
GetFieldStruct(
const Table &table,
const reflection::Field &field)
169 return table.GetStruct<
const Struct *>(field.offset());
173inline const Struct *
GetFieldStruct(
const Struct &structure,
const reflection::Field &field)
176 return structure.GetStruct<
const Struct *>(field.offset());
190 const reflection::Schema *schema,
int type_index);
193inline int64_t
GetAnyFieldI(
const Table &table,
const reflection::Field &field)
195 auto field_ptr = table.GetAddressOf(field.offset());
196 return field_ptr ?
GetAnyValueI(field.type()->base_type(), field_ptr) : field.default_integer();
200inline double GetAnyFieldF(
const Table &table,
const reflection::Field &field)
202 auto field_ptr = table.GetAddressOf(field.offset());
203 return field_ptr ?
GetAnyValueF(field.type()->base_type(), field_ptr) : field.default_real();
209inline std::string
GetAnyFieldS(
const Table &table,
const reflection::Field &field,
210 const reflection::Schema *schema)
212 auto field_ptr = table.GetAddressOf(field.offset());
214 ?
GetAnyValueS(field.type()->base_type(), field_ptr, schema, field.type()->index())
219inline int64_t
GetAnyFieldI(
const Struct &st,
const reflection::Field &field)
221 return GetAnyValueI(field.type()->base_type(), st.GetAddressOf(field.offset()));
225inline double GetAnyFieldF(
const Struct &st,
const reflection::Field &field)
227 return GetAnyValueF(field.type()->base_type(), st.GetAddressOf(field.offset()));
231inline std::string
GetAnyFieldS(
const Struct &st,
const reflection::Field &field)
233 return GetAnyValueS(field.type()->base_type(), st.GetAddressOf(field.offset()),
nullptr, -1);
260 auto elem_ptr = vec->
Data() +
sizeof(uoffset_t) * i;
261 return reinterpret_cast<T *
>(elem_ptr + ReadScalar<uoffset_t>(elem_ptr));
272 return reinterpret_cast<T *
>(vec->
Data() + elem_size * i);
278 return reinterpret_cast<T *
>(table.GetAddressOf(field.offset()));
284 return reinterpret_cast<T *
>(st.GetAddressOf(field.offset()));
290template <
typename T>
bool SetField(Table *table,
const reflection::Field &field, T val)
292 reflection::BaseType type = field.type()->base_type();
301 def = GetFieldDefaultI<T>(field);
306 def = GetFieldDefaultF<T>(field);
308 return table->SetField(field.offset(), val, def);
320inline bool SetAnyFieldI(Table *table,
const reflection::Field &field, int64_t val)
322 auto field_ptr = table->GetAddressOf(field.offset());
324 return val == GetFieldDefaultI<int64_t>(field);
325 SetAnyValueI(field.type()->base_type(), field_ptr, val);
330inline bool SetAnyFieldF(Table *table,
const reflection::Field &field,
double val)
332 auto field_ptr = table->GetAddressOf(field.offset());
334 return val == GetFieldDefaultF<double>(field);
335 SetAnyValueF(field.type()->base_type(), field_ptr, val);
340inline bool SetAnyFieldS(Table *table,
const reflection::Field &field,
const char *val)
342 auto field_ptr = table->GetAddressOf(field.offset());
345 SetAnyValueS(field.type()->base_type(), field_ptr, val);
350inline void SetAnyFieldI(Struct *st,
const reflection::Field &field, int64_t val)
352 SetAnyValueI(field.type()->base_type(), st->GetAddressOf(field.offset()), val);
356inline void SetAnyFieldF(Struct *st,
const reflection::Field &field,
double val)
358 SetAnyValueF(field.type()->base_type(), st->GetAddressOf(field.offset()), val);
362inline void SetAnyFieldS(Struct *st,
const reflection::Field &field,
const char *val)
364 SetAnyValueS(field.type()->base_type(), st->GetAddressOf(field.offset()), val);
396 : offset_(reinterpret_cast<uint8_t *>(ptr) -
411 std::vector<U> &vec_;
423inline const reflection::Object &
GetUnionType(
const reflection::Schema &schema,
424 const reflection::Object &parent,
425 const reflection::Field &unionfield,
428 auto enumdef = schema.enums()->Get(unionfield.type()->index());
433 auto union_type = GetFieldI<uint8_t>(table, *type_field);
434 auto enumval = enumdef->values()->LookupByKey(union_type);
435 return *enumval->object();
444 std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table =
nullptr);
453 std::vector<uint8_t> *flatbuf,
454 const reflection::Object *root_table =
nullptr);
458 std::vector<uint8_t> *flatbuf,
const reflection::Object *root_table =
nullptr)
460 auto delta_elem =
static_cast<int>(newsize) -
static_cast<int>(vec->
size());
463 static_cast<uoffset_t
>(
sizeof(T)), flatbuf, root_table);
465 for (
int i = 0; i < delta_elem; i++)
467 auto loc = newelems + i *
sizeof(T);
471 WriteScalar(loc, val);
475 *
reinterpret_cast<T *
>(loc) = val;
488const uint8_t *
AddFlatBuffer(std::vector<uint8_t> &flatbuf,
const uint8_t *newbuf,
size_t newlen);
490inline bool SetFieldT(Table *table,
const reflection::Field &field,
const uint8_t *val)
493 return table->SetPointer(field.offset(), val);
508 const reflection::Object &objectdef,
const Table &table,
509 bool use_string_pooling =
false);
515bool Verify(
const reflection::Schema &schema,
const reflection::Object &root,
const uint8_t *buf,
516 size_t length, uoffset_t max_depth = 64, uoffset_t max_tables = 1000000);
#define FLATBUFFERS_ASSERT
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
const uint8_t * Data() const
pointer_inside_vector(T *ptr, std::vector< U > &vec)
const uint8_t * AddFlatBuffer(std::vector< uint8_t > &flatbuf, const uint8_t *newbuf, size_t newlen)
T GetFieldDefaultI(const reflection::Field &field)
bool IsInteger(BaseType t)
T * GetAnyFieldAddressOf(const Table &table, const reflection::Field &field)
bool IsScalar(BaseType t)
int64_t GetAnyValueI(reflection::BaseType type, const uint8_t *data)
const String * GetFieldS(const Table &table, const reflection::Field &field)
std::string GetAnyVectorElemS(const VectorOfAny *vec, reflection::BaseType elem_type, size_t i)
std::string GetAnyValueS(reflection::BaseType type, const uint8_t *data, const reflection::Schema *schema, int type_index)
void SetAnyValueI(reflection::BaseType type, uint8_t *data, int64_t val)
T * GetAnyVectorElemPointer(const VectorOfAny *vec, size_t i)
const char * UnionTypeFieldSuffix()
bool Verify(const reflection::Schema &schema, const reflection::Object &root, const uint8_t *buf, size_t length, uoffset_t max_depth=64, uoffset_t max_tables=1000000)
Table * GetFieldT(const Table &table, const reflection::Field &field)
uint8_t * ResizeAnyVector(const reflection::Schema &schema, uoffset_t newsize, const VectorOfAny *vec, uoffset_t num_elems, uoffset_t elem_size, std::vector< uint8_t > *flatbuf, const reflection::Object *root_table=nullptr)
T GetFieldDefaultF(const reflection::Field &field)
bool SetFieldT(Table *table, const reflection::Field &field, const uint8_t *val)
int64_t GetAnyFieldI(const Table &table, const reflection::Field &field)
bool SetField(Table *table, const reflection::Field &field, T val)
T * GetAnyVectorElemAddressOf(const VectorOfAny *vec, size_t i, size_t elem_size)
bool SetAnyFieldS(Table *table, const reflection::Field &field, const char *val)
void SetAnyValueS(reflection::BaseType type, uint8_t *data, const char *val)
Offset< const Table * > CopyTable(FlatBufferBuilder &fbb, const reflection::Schema &schema, const reflection::Object &objectdef, const Table &table, bool use_string_pooling=false)
T GetFieldF(const Table &table, const reflection::Field &field)
void SetString(const reflection::Schema &schema, const std::string &val, const String *str, std::vector< uint8_t > *flatbuf, const reflection::Object *root_table=nullptr)
double GetAnyFieldF(const Table &table, const reflection::Field &field)
Vector< T > * GetFieldV(const Table &table, const reflection::Field &field)
double GetAnyValueF(reflection::BaseType type, const uint8_t *data)
bool SetAnyFieldF(Table *table, const reflection::Field &field, double val)
void SetAnyValueF(reflection::BaseType type, uint8_t *data, double val)
size_t GetTypeSize(reflection::BaseType base_type)
void ResizeVector(const reflection::Schema &schema, uoffset_t newsize, T val, const Vector< T > *vec, std::vector< uint8_t > *flatbuf, const reflection::Object *root_table=nullptr)
const Struct * GetFieldStruct(const Table &table, const reflection::Field &field)
bool SetAnyFieldI(Table *table, const reflection::Field &field, int64_t val)
T * vector_data(std::vector< T > &vector)
void SetAnyVectorElemI(VectorOfAny *vec, reflection::BaseType elem_type, size_t i, int64_t val)
VectorOfAny * GetFieldAnyV(const Table &table, const reflection::Field &field)
const reflection::Object & GetUnionType(const reflection::Schema &schema, const reflection::Object &parent, const reflection::Field &unionfield, const Table &table)
double GetAnyVectorElemF(const VectorOfAny *vec, reflection::BaseType elem_type, size_t i)
int64_t GetAnyVectorElemI(const VectorOfAny *vec, reflection::BaseType elem_type, size_t i)
size_t GetTypeSizeInline(reflection::BaseType base_type, int type_index, const reflection::Schema &schema)
T GetFieldI(const Table &table, const reflection::Field &field)
void SetAnyVectorElemF(VectorOfAny *vec, reflection::BaseType elem_type, size_t i, double val)
std::string GetAnyFieldS(const Table &table, const reflection::Field &field, const reflection::Schema *schema)
const T * data(const std::vector< T, Alloc > &v)
pointer_inside_vector< T, U > piv(T *ptr, std::vector< U > &vec)
Table * GetAnyRoot(uint8_t *flatbuf)
void SetAnyVectorElemS(VectorOfAny *vec, reflection::BaseType elem_type, size_t i, const char *val)
uint64_t num_elems(const nnfw_tensorinfo *tensor_info)
Get the total number of elements in nnfw_tensorinfo->dims.