2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 * Copyright 2017 Google Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #ifndef FLATBUFFERS_MINIREFLECT_H_
19 #define FLATBUFFERS_MINIREFLECT_H_
21 #include "flatbuffers/flatbuffers.h"
22 #include "flatbuffers/util.h"
27 // Utilities that can be used with the "mini reflection" tables present
28 // in generated code with --reflect-types (only types) or --reflect-names
30 // This allows basic reflection functionality such as pretty-printing
31 // that does not require the use of the schema parser or loading of binary
32 // schema files at runtime (reflection.h).
34 // For any of the functions below that take `const TypeTable *`, you pass
35 // `FooTypeTable()` if the type of the root is `Foo`.
37 // First, a generic iterator that can be used by multiple algorithms.
39 struct IterationVisitor
41 // These mark the scope of a table or struct.
42 virtual void StartSequence() {}
43 virtual void EndSequence() {}
44 // Called for each field regardless of whether it is present or not.
45 // If not present, val == nullptr. set_idx is the index of all set fields.
46 virtual void Field(size_t /*field_idx*/, size_t /*set_idx*/, ElementaryType /*type*/,
47 bool /*is_vector*/, const TypeTable * /*type_table*/, const char * /*name*/,
48 const uint8_t * /*val*/)
51 // Called for a value that is actually present, after a field, or as part
53 virtual void UType(uint8_t, const char *) {}
54 virtual void Bool(bool) {}
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) {}
62 virtual void ULong(uint64_t) {}
63 virtual void Float(float) {}
64 virtual void Double(double) {}
65 virtual void String(const String *) {}
66 virtual void Unknown(const uint8_t *) {} // From a future version.
67 // These mark the scope of a vector.
68 virtual void StartVector() {}
69 virtual void EndVector() {}
70 virtual void Element(size_t /*i*/, ElementaryType /*type*/, const TypeTable * /*type_table*/,
71 const uint8_t * /*val*/)
74 virtual ~IterationVisitor() {}
77 inline 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]);
107 FLATBUFFERS_ASSERT(false);
111 FLATBUFFERS_ASSERT(false);
116 inline 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);
125 return -1; // Unknown enum value.
128 template <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];
140 void IterateObject(const uint8_t *obj, const TypeTable *type_table, IterationVisitor *visitor);
142 inline void IterateValue(ElementaryType type, const uint8_t *val, const TypeTable *type_table,
143 const uint8_t *prev_val, soffset_t vector_index, IterationVisitor *visitor)
149 auto tval = ReadScalar<uint8_t>(val);
150 visitor->UType(tval, EnumName(tval, type_table));
155 visitor->Bool(ReadScalar<uint8_t>(val) != 0);
160 auto tval = ReadScalar<int8_t>(val);
161 visitor->Char(tval, EnumName(tval, type_table));
166 auto tval = ReadScalar<uint8_t>(val);
167 visitor->UChar(tval, EnumName(tval, type_table));
172 auto tval = ReadScalar<int16_t>(val);
173 visitor->Short(tval, EnumName(tval, type_table));
178 auto tval = ReadScalar<uint16_t>(val);
179 visitor->UShort(tval, EnumName(tval, type_table));
184 auto tval = ReadScalar<int32_t>(val);
185 visitor->Int(tval, EnumName(tval, type_table));
190 auto tval = ReadScalar<uint32_t>(val);
191 visitor->UInt(tval, EnumName(tval, type_table));
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);
217 visitor->String(reinterpret_cast<const String *>(val));
222 switch (type_table->st)
225 val += ReadScalar<uoffset_t>(val);
226 IterateObject(val, type_table, visitor);
229 IterateObject(val, type_table, visitor);
233 val += ReadScalar<uoffset_t>(val);
234 FLATBUFFERS_ASSERT(prev_val);
235 auto union_type = *prev_val; // Always a uint8_t.
236 if (vector_index >= 0)
238 auto type_vec = reinterpret_cast<const Vector<uint8_t> *>(prev_val);
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]();
250 IterateObject(val, ref, visitor);
254 visitor->String(reinterpret_cast<const String *>(val));
257 visitor->Unknown(val);
262 visitor->Unknown(val);
267 FLATBUFFERS_ASSERT(false);
274 visitor->Unknown(val);
280 inline void IterateObject(const uint8_t *obj, const TypeTable *type_table,
281 IterationVisitor *visitor)
283 visitor->StartSequence();
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(
303 FieldIndexToOffset(static_cast<voffset_t>(i)));
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)
319 // variable length vector
320 val += ReadScalar<uoffset_t>(val);
321 auto vec = reinterpret_cast<const Vector<uint8_t> *>(val);
322 elem_ptr = vec->Data();
327 // otherwise fixed size array
328 size = type_table->array_sizes[array_idx];
331 visitor->StartVector();
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);
336 elem_ptr += InlineSize(type, ref);
338 visitor->EndVector();
342 IterateValue(type, val, ref, prev_val, -1, visitor);
347 visitor->EndSequence();
350 inline void IterateFlatBuffer(const uint8_t *buffer, const TypeTable *type_table,
351 IterationVisitor *callback)
353 IterateObject(GetRoot<uint8_t>(buffer), type_table, callback);
356 // Outputting a Flatbuffer to a string. Tries to conform as close to JSON /
357 // the output generated by idl_gen_text.cpp.
359 struct ToStringVisitor : public IterationVisitor
366 bool vector_delimited;
367 ToStringVisitor(std::string delimiter, bool quotes, std::string indent, bool vdelimited = true)
368 : d(delimiter), q(quotes), in(indent), indent_level(0), vector_delimited(vdelimited)
371 ToStringVisitor(std::string delimiter)
372 : d(delimiter), q(false), in(""), indent_level(0), vector_delimited(true)
378 for (size_t i = 0; i < indent_level; i++)
397 void Field(size_t /*field_idx*/, size_t set_idx, ElementaryType /*type*/, bool /*is_vector*/,
398 const TypeTable * /*type_table*/, const char *name, const uint8_t *val)
418 template <typename T> void Named(T x, const char *name)
433 void UType(uint8_t x, const char *name) { Named(x, name); }
434 void Bool(bool x) { s += x ? "true" : "false"; }
435 void Char(int8_t x, const char *name) { Named(x, name); }
436 void UChar(uint8_t x, const char *name) { Named(x, name); }
437 void Short(int16_t x, const char *name) { Named(x, name); }
438 void UShort(uint16_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); }
441 void Long(int64_t x) { s += NumToString(x); }
442 void ULong(uint64_t x) { s += NumToString(x); }
443 void Float(float x) { s += NumToString(x); }
444 void Double(double x) { s += NumToString(x); }
445 void String(const struct String *str)
447 EscapeString(str->c_str(), str->size(), &s, true, false);
449 void Unknown(const uint8_t *) { s += "(?)"; }
453 if (vector_delimited)
466 if (vector_delimited)
478 void Element(size_t i, ElementaryType /*type*/, const TypeTable * /*type_table*/,
479 const uint8_t * /*val*/)
484 if (vector_delimited)
497 inline std::string FlatBufferToString(const uint8_t *buffer, const TypeTable *type_table,
498 bool multi_line = false, bool vector_delimited = true)
500 ToStringVisitor tostring_visitor(multi_line ? "\n" : " ", false, "", vector_delimited);
501 IterateFlatBuffer(buffer, type_table, &tostring_visitor);
502 return tostring_visitor.s;
505 } // namespace flatbuffers
507 #endif // FLATBUFFERS_MINIREFLECT_H_