18#ifndef FLATBUFFERS_MINIREFLECT_H_
19#define FLATBUFFERS_MINIREFLECT_H_
46 virtual void Field(
size_t ,
size_t , ElementaryType ,
47 bool ,
const TypeTable * ,
const char * ,
53 virtual void UType(uint8_t,
const char *) {}
55 virtual void Char(int8_t,
const char *) {}
56 virtual void UChar(uint8_t,
const char *) {}
57 virtual void Short(int16_t,
const char *) {}
58 virtual void UShort(uint16_t,
const char *) {}
59 virtual void Int(int32_t,
const char *) {}
60 virtual void UInt(uint32_t,
const char *) {}
61 virtual void Long(int64_t) {}
70 virtual void Element(
size_t , ElementaryType ,
const TypeTable * ,
77inline size_t InlineSize(ElementaryType type,
const TypeTable *type_table)
99 switch (type_table->st)
105 return static_cast<size_t>(type_table->values[type_table->num_elems]);
116inline int64_t
LookupEnum(int64_t enum_val,
const int64_t *values,
size_t num_values)
120 for (
size_t i = 0; i < num_values; i++)
122 if (enum_val == values[i])
123 return static_cast<int64_t
>(i);
128template <
typename T>
const char *
EnumName(T tval,
const TypeTable *type_table)
130 if (!type_table || !type_table->names)
132 auto i =
LookupEnum(
static_cast<int64_t
>(tval), type_table->values, type_table->num_elems);
133 if (i >= 0 && i <
static_cast<int64_t
>(type_table->num_elems))
135 return type_table->names[i];
140void IterateObject(
const uint8_t *obj,
const TypeTable *type_table, IterationVisitor *visitor);
142inline void IterateValue(ElementaryType type,
const uint8_t *val,
const TypeTable *type_table,
149 auto tval = ReadScalar<uint8_t>(val);
155 visitor->
Bool(ReadScalar<uint8_t>(val) != 0);
160 auto tval = ReadScalar<int8_t>(val);
166 auto tval = ReadScalar<uint8_t>(val);
172 auto tval = ReadScalar<int16_t>(val);
178 auto tval = ReadScalar<uint16_t>(val);
184 auto tval = ReadScalar<int32_t>(val);
190 auto tval = ReadScalar<uint32_t>(val);
196 visitor->
Long(ReadScalar<int64_t>(val));
201 visitor->
ULong(ReadScalar<uint64_t>(val));
206 visitor->
Float(ReadScalar<float>(val));
211 visitor->
Double(ReadScalar<double>(val));
216 val += ReadScalar<uoffset_t>(val);
222 switch (type_table->st)
225 val += ReadScalar<uoffset_t>(val);
233 val += ReadScalar<uoffset_t>(val);
235 auto union_type = *prev_val;
236 if (vector_index >= 0)
239 union_type = type_vec->
Get(
static_cast<uoffset_t
>(vector_index));
241 auto type_code_idx =
LookupEnum(union_type, type_table->values, type_table->num_elems);
242 if (type_code_idx >= 0 && type_code_idx <
static_cast<int32_t
>(type_table->num_elems))
244 auto type_code = type_table->type_codes[type_code_idx];
245 switch (type_code.base_type)
249 auto ref = type_table->type_refs[type_code.sequence_ref]();
284 const uint8_t *prev_val =
nullptr;
286 size_t array_idx = 0;
287 for (
size_t i = 0; i < type_table->num_elems; i++)
289 auto type_code = type_table->type_codes[i];
290 auto type =
static_cast<ElementaryType
>(type_code.base_type);
291 auto is_repeating = type_code.is_repeating != 0;
292 auto ref_idx = type_code.sequence_ref;
293 const TypeTable *ref =
nullptr;
296 ref = type_table->type_refs[ref_idx]();
298 auto name = type_table->names ? type_table->names[i] :
nullptr;
299 const uint8_t *val =
nullptr;
300 if (type_table->st == ST_TABLE)
302 val =
reinterpret_cast<const Table *
>(obj)->GetAddressOf(
307 val = obj + type_table->values[i];
309 visitor->
Field(i, set_idx, type, is_repeating, ref, name, val);
317 if (type_table->st == ST_TABLE)
320 val += ReadScalar<uoffset_t>(val);
322 elem_ptr = vec->
Data();
328 size = type_table->array_sizes[array_idx];
332 for (
size_t j = 0; j <
size; j++)
334 visitor->
Element(j, type, ref, elem_ptr);
335 IterateValue(type, elem_ptr, ref, prev_val,
static_cast<soffset_t
>(j), visitor);
353 IterateObject(GetRoot<uint8_t>(buffer), type_table, callback);
367 ToStringVisitor(std::string delimiter,
bool quotes, std::string indent,
bool vdelimited =
true)
397 void Field(
size_t ,
size_t set_idx, ElementaryType ,
bool ,
398 const TypeTable * ,
const char *name,
const uint8_t *val)
418 template <
typename T>
void Named(T x,
const char *name)
434 void Bool(
bool x) {
s += x ?
"true" :
"false"; }
435 void Char(int8_t x,
const char *name) {
Named(x, name); }
439 void Int(int32_t x,
const char *name) {
Named(x, name); }
440 void UInt(uint32_t x,
const char *name) {
Named(x, name); }
478 void Element(
size_t i, ElementaryType ,
const TypeTable * ,
498 bool multi_line =
false,
bool vector_delimited =
true)
500 ToStringVisitor tostring_visitor(multi_line ?
"\n" :
" ",
false,
"", vector_delimited);
502 return tostring_visitor.
s;
#define FLATBUFFERS_ASSERT
return_type Get(uoffset_t i) const
const uint8_t * Data() const
int64_t LookupEnum(int64_t enum_val, const int64_t *values, size_t num_values)
std::string NumToString(T t)
std::string FlatBufferToString(const uint8_t *buffer, const TypeTable *type_table, bool multi_line=false, bool vector_delimited=true)
void IterateValue(ElementaryType type, const uint8_t *val, const TypeTable *type_table, const uint8_t *prev_val, soffset_t vector_index, IterationVisitor *visitor)
bool EscapeString(const char *s, size_t length, std::string *_text, bool allow_non_utf8, bool natural_utf8)
void IterateObject(const uint8_t *obj, const TypeTable *type_table, IterationVisitor *visitor)
const char * EnumName(T tval, const TypeTable *type_table)
size_t InlineSize(const Type &type)
voffset_t FieldIndexToOffset(voffset_t field_id)
void IterateFlatBuffer(const uint8_t *buffer, const TypeTable *type_table, IterationVisitor *callback)
virtual void Long(int64_t)
virtual ~IterationVisitor()
virtual void Int(int32_t, const char *)
virtual void Char(int8_t, const char *)
virtual void UType(uint8_t, const char *)
virtual void UShort(uint16_t, const char *)
virtual void Field(size_t, size_t, ElementaryType, bool, const TypeTable *, const char *, const uint8_t *)
virtual void Float(float)
virtual void Element(size_t, ElementaryType, const TypeTable *, const uint8_t *)
virtual void StartSequence()
virtual void UInt(uint32_t, const char *)
virtual void Double(double)
virtual void ULong(uint64_t)
virtual void Unknown(const uint8_t *)
virtual void EndSequence()
virtual void String(const String *)
virtual void StartVector()
virtual void Short(int16_t, const char *)
virtual void UChar(uint8_t, const char *)
void Char(int8_t x, const char *name)
void UType(uint8_t x, const char *name)
void Named(T x, const char *name)
ToStringVisitor(std::string delimiter, bool quotes, std::string indent, bool vdelimited=true)
void UShort(uint16_t x, const char *name)
void Unknown(const uint8_t *)
void UInt(uint32_t x, const char *name)
void UChar(uint8_t x, const char *name)
void Short(int16_t x, const char *name)
void Element(size_t i, ElementaryType, const TypeTable *, const uint8_t *)
void Int(int32_t x, const char *name)
void Field(size_t, size_t set_idx, ElementaryType, bool, const TypeTable *, const char *name, const uint8_t *val)
void String(const struct String *str)
ToStringVisitor(std::string delimiter)