1 /* 2 * Copyright (C) 2018 The Android Open Source Project 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 // Utility functions for working with FlatBuffers. 18 19 #ifndef LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_REFLECTION_H_ 20 #define LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_REFLECTION_H_ 21 22 #include "utils/flatbuffers/flatbuffers_generated.h" 23 #include "utils/optional.h" 24 #include "utils/strings/stringpiece.h" 25 #include "utils/variant.h" 26 #include "flatbuffers/reflection.h" 27 #include "flatbuffers/reflection_generated.h" 28 29 namespace libtextclassifier3 { 30 31 template <typename T> 32 struct flatbuffers_base_type { 33 static const reflection::BaseType value; 34 }; 35 36 template <typename T> 37 inline const reflection::BaseType flatbuffers_base_type<T>::value = 38 reflection::None; 39 40 template <> 41 inline const reflection::BaseType flatbuffers_base_type<bool>::value = 42 reflection::Bool; 43 44 template <> 45 inline const reflection::BaseType flatbuffers_base_type<int8>::value = 46 reflection::Byte; 47 48 template <> 49 inline const reflection::BaseType flatbuffers_base_type<uint8>::value = 50 reflection::UByte; 51 52 template <> 53 inline const reflection::BaseType flatbuffers_base_type<int16>::value = 54 reflection::Short; 55 56 template <> 57 inline const reflection::BaseType flatbuffers_base_type<uint16>::value = 58 reflection::UShort; 59 60 template <> 61 inline const reflection::BaseType flatbuffers_base_type<int32>::value = 62 reflection::Int; 63 64 template <> 65 inline const reflection::BaseType flatbuffers_base_type<uint32>::value = 66 reflection::UInt; 67 68 template <> 69 inline const reflection::BaseType flatbuffers_base_type<int64>::value = 70 reflection::Long; 71 72 template <> 73 inline const reflection::BaseType flatbuffers_base_type<uint64>::value = 74 reflection::ULong; 75 76 template <> 77 inline const reflection::BaseType flatbuffers_base_type<float>::value = 78 reflection::Float; 79 80 template <> 81 inline const reflection::BaseType flatbuffers_base_type<double>::value = 82 reflection::Double; 83 84 template <> 85 inline const reflection::BaseType flatbuffers_base_type<StringPiece>::value = 86 reflection::String; 87 88 template <reflection::BaseType> 89 struct flatbuffers_cpp_type; 90 91 template <> 92 struct flatbuffers_cpp_type<reflection::BaseType::Bool> { 93 using value = bool; 94 }; 95 96 template <> 97 struct flatbuffers_cpp_type<reflection::BaseType::Byte> { 98 using value = int8; 99 }; 100 101 template <> 102 struct flatbuffers_cpp_type<reflection::BaseType::UByte> { 103 using value = uint8; 104 }; 105 106 template <> 107 struct flatbuffers_cpp_type<reflection::BaseType::Short> { 108 using value = int16; 109 }; 110 111 template <> 112 struct flatbuffers_cpp_type<reflection::BaseType::UShort> { 113 using value = uint16; 114 }; 115 116 template <> 117 struct flatbuffers_cpp_type<reflection::BaseType::Int> { 118 using value = int32; 119 }; 120 121 template <> 122 struct flatbuffers_cpp_type<reflection::BaseType::UInt> { 123 using value = uint32; 124 }; 125 126 template <> 127 struct flatbuffers_cpp_type<reflection::BaseType::Long> { 128 using value = int64; 129 }; 130 131 template <> 132 struct flatbuffers_cpp_type<reflection::BaseType::ULong> { 133 using value = uint64; 134 }; 135 136 template <> 137 struct flatbuffers_cpp_type<reflection::BaseType::Float> { 138 using value = float; 139 }; 140 141 template <> 142 struct flatbuffers_cpp_type<reflection::BaseType::Double> { 143 using value = double; 144 }; 145 146 // Gets the field information for a field name, returns nullptr if the 147 // field was not defined. 148 const reflection::Field* GetFieldOrNull(const reflection::Object* type, 149 const StringPiece field_name); 150 151 // Gets the field information for a field offet, returns nullptr if no field was 152 // defined with the given offset. 153 const reflection::Field* GetFieldOrNull(const reflection::Object* type, 154 const int field_offset); 155 156 // Gets a field by name or offset, returns nullptr if no field was found. 157 const reflection::Field* GetFieldOrNull(const reflection::Object* type, 158 const StringPiece field_name, 159 const int field_offset); 160 161 // Gets a field by a field spec, either by name or offset. Returns nullptr if no 162 // such field was found. 163 const reflection::Field* GetFieldOrNull(const reflection::Object* type, 164 const FlatbufferField* field); 165 166 const reflection::Field* GetFieldOrNull(const reflection::Object* type, 167 const FlatbufferFieldT* field); 168 169 // Gets the type information for the given type name or nullptr if not 170 // specified. 171 const reflection::Object* TypeForName(const reflection::Schema* schema, 172 const StringPiece type_name); 173 174 // Gets the type id for a type name. 175 Optional<int> TypeIdForName(const reflection::Schema* schema, 176 const StringPiece type_name); 177 178 // Gets the type id for a type. 179 Optional<int> TypeIdForObject(const reflection::Schema* schema, 180 const reflection::Object* type); 181 182 // Resolves field lookups by name to the concrete field offsets. 183 bool SwapFieldNamesForOffsetsInPath(const reflection::Schema* schema, 184 FlatbufferFieldPathT* path); 185 186 // Checks whether a type denotes an enum. 187 inline bool IsEnum(const reflection::Type* type) { 188 return flatbuffers::IsInteger(type->base_type()) && type->index() >= 0; 189 } 190 191 // Parses an enum value. 192 Variant ParseEnumValue(const reflection::Schema* schema, 193 const reflection::Type* type, StringPiece value); 194 195 } // namespace libtextclassifier3 196 197 #endif // LIBTEXTCLASSIFIER_UTILS_FLATBUFFERS_REFLECTION_H_ 198