1 /* 2 * Copyright 2020 Google Inc. All rights reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 // This contains some utilities/examples for how to leverage the static reflec- 18 // tion features of tables and structs in the C++17 code generation to recur- 19 // sively produce a string representation of any Flatbuffer table or struct use 20 // compile-time iteration over the fields. Note that this code is completely 21 // generic in that it makes no reference to any particular Flatbuffer type. 22 23 #include <optional> 24 #include <string> 25 #include <type_traits> 26 #include <utility> 27 #include <vector> 28 29 #include "flatbuffers/flatbuffers.h" 30 #include "flatbuffers/util.h" 31 32 namespace cpp17 { 33 34 // User calls this; need to forward declare it since it is called recursively. 35 template<typename T> 36 std::optional<std::string> StringifyFlatbufferValue( 37 T &&val, const std::string &indent = ""); 38 39 namespace detail { 40 41 /******************************************************************************* 42 ** Metaprogramming helpers for detecting Flatbuffers Tables, Structs, & Vectors. 43 *******************************************************************************/ 44 template<typename FBS, typename = void> 45 struct is_flatbuffers_table_or_struct : std::false_type {}; 46 47 // We know it's a table or struct when it has a Traits subclass. 48 template<typename FBS> 49 struct is_flatbuffers_table_or_struct<FBS, std::void_t<typename FBS::Traits>> 50 : std::true_type {}; 51 52 template<typename FBS> 53 inline constexpr bool is_flatbuffers_table_or_struct_v = 54 is_flatbuffers_table_or_struct<FBS>::value; 55 56 template<typename T> struct is_flatbuffers_vector : std::false_type {}; 57 58 template<typename T> 59 struct is_flatbuffers_vector<flatbuffers::Vector<T>> : std::true_type {}; 60 61 template<typename T> 62 inline constexpr bool is_flatbuffers_vector_v = is_flatbuffers_vector<T>::value; 63 64 /******************************************************************************* 65 ** Compile-time Iteration & Recursive Stringification over Flatbuffers types. 66 *******************************************************************************/ 67 template<size_t Index, typename FBS> 68 std::string AddStringifiedField(const FBS &fbs, const std::string &indent) { 69 auto value_string = 70 StringifyFlatbufferValue(fbs.template get_field<Index>(), indent); 71 if (!value_string) { return ""; } 72 return indent + FBS::Traits::field_names[Index] + " = " + *value_string + 73 "\n"; 74 } 75 76 template<typename FBS, size_t... Indexes> 77 std::string StringifyTableOrStructImpl(const FBS &fbs, 78 const std::string &indent, 79 std::index_sequence<Indexes...>) { 80 // This line is where the compile-time iteration happens! 81 return (AddStringifiedField<Indexes>(fbs, indent) + ...); 82 } 83 84 template<typename FBS> 85 std::string StringifyTableOrStruct(const FBS &fbs, const std::string &indent) { 86 static constexpr size_t field_count = FBS::Traits::fields_number; 87 std::string out; 88 if constexpr (field_count > 0) { 89 out = std::string(FBS::Traits::fully_qualified_name) + "{\n" + 90 StringifyTableOrStructImpl(fbs, indent + " ", 91 std::make_index_sequence<field_count>{}) + 92 indent + '}'; 93 } 94 return out; 95 } 96 97 template<typename T> 98 std::string StringifyVector(const flatbuffers::Vector<T> &vec, 99 const std::string &indent) { 100 const auto prologue = indent + std::string(" "); 101 const auto epilogue = std::string(",\n"); 102 std::string text; 103 text += "[\n"; 104 for (auto it = vec.cbegin(), end = vec.cend(); it != end; ++it) { 105 text += prologue; 106 text += StringifyFlatbufferValue(*it).value_or("(field absent)"); 107 text += epilogue; 108 } 109 if (vec.cbegin() != vec.cend()) { 110 text.resize(text.size() - epilogue.size()); 111 } 112 text += '\n' + indent + ']'; 113 return text; 114 } 115 116 template<typename T> std::string StringifyArithmeticType(T val) { 117 return flatbuffers::NumToString(val); 118 } 119 120 } // namespace detail 121 122 /******************************************************************************* 123 ** Take any flatbuffer type (table, struct, Vector, int...) and stringify it. 124 *******************************************************************************/ 125 template<typename T> 126 std::optional<std::string> StringifyFlatbufferValue(T &&val, 127 const std::string &indent) { 128 constexpr bool is_pointer = std::is_pointer_v<std::remove_reference_t<T>>; 129 if constexpr (is_pointer) { 130 if (val == nullptr) return std::nullopt; // Field is absent. 131 } 132 using decayed = 133 std::decay_t<std::remove_pointer_t<std::remove_reference_t<T>>>; 134 135 // Is it a Flatbuffers Table or Struct? 136 if constexpr (detail::is_flatbuffers_table_or_struct_v<decayed>) { 137 // We have a nested table or struct; use recursion! 138 if constexpr (is_pointer) 139 return detail::StringifyTableOrStruct(*val, indent); 140 else 141 return detail::StringifyTableOrStruct(val, indent); 142 } 143 144 // Is it an 8-bit number? If so, print it like an int (not char). 145 else if constexpr (std::is_same_v<decayed, int8_t> || 146 std::is_same_v<decayed, uint8_t>) { 147 return detail::StringifyArithmeticType(static_cast<int>(val)); 148 } 149 150 // Is it an enum? If so, print it like an int, since Flatbuffers doesn't yet 151 // have type-based reflection for enums, so we can't print the enum's name :( 152 else if constexpr (std::is_enum_v<decayed>) { 153 return StringifyFlatbufferValue( 154 static_cast<std::underlying_type_t<decayed>>(val), indent); 155 } 156 157 // Is it an int, double, float, uint32_t, etc.? 158 else if constexpr (std::is_arithmetic_v<decayed>) { 159 return detail::StringifyArithmeticType(val); 160 } 161 162 // Is it a Flatbuffers string? 163 else if constexpr (std::is_same_v<decayed, flatbuffers::String>) { 164 return '"' + val->str() + '"'; 165 } 166 167 // Is it a Flatbuffers Vector? 168 else if constexpr (detail::is_flatbuffers_vector_v<decayed>) { 169 return detail::StringifyVector(*val, indent); 170 } 171 172 // Is it a void pointer? 173 else if constexpr (std::is_same_v<decayed, void>) { 174 // Can't format it. 175 return std::nullopt; 176 } 177 178 else { 179 // Not sure how to format this type, whatever it is. 180 static_assert(sizeof(T) != sizeof(T), 181 "Do not know how to format this type T (the compiler error " 182 "should tell you nearby what T is)."); 183 } 184 } 185 186 } // namespace cpp17 187