James Kuszmaul | dac091f | 2022-03-22 09:35:06 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2021 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 | #ifndef FLATBUFFERS_BFBS_GEN_H_ |
| 18 | #define FLATBUFFERS_BFBS_GEN_H_ |
| 19 | |
| 20 | #include <cstdint> |
| 21 | |
| 22 | #include "flatbuffers/bfbs_generator.h" |
| 23 | #include "flatbuffers/reflection_generated.h" |
| 24 | |
| 25 | namespace flatbuffers { |
| 26 | |
| 27 | void ForAllEnums( |
| 28 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Enum>> *enums, |
| 29 | std::function<void(const reflection::Enum *)> func) { |
| 30 | for (auto it = enums->cbegin(); it != enums->cend(); ++it) { func(*it); } |
| 31 | } |
| 32 | |
| 33 | void ForAllObjects( |
| 34 | const flatbuffers::Vector<flatbuffers::Offset<reflection::Object>> *objects, |
| 35 | std::function<void(const reflection::Object *)> func) { |
| 36 | for (auto it = objects->cbegin(); it != objects->cend(); ++it) { func(*it); } |
| 37 | } |
| 38 | |
| 39 | void ForAllEnumValues(const reflection::Enum *enum_def, |
| 40 | std::function<void(const reflection::EnumVal *)> func) { |
| 41 | for (auto it = enum_def->values()->cbegin(); it != enum_def->values()->cend(); |
| 42 | ++it) { |
| 43 | func(*it); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | void ForAllDocumentation( |
| 48 | const flatbuffers::Vector<flatbuffers::Offset<flatbuffers::String>> |
| 49 | *documentation, |
| 50 | std::function<void(const flatbuffers::String *)> func) { |
| 51 | if (!documentation) { return; } |
| 52 | for (auto it = documentation->cbegin(); it != documentation->cend(); ++it) { |
| 53 | func(*it); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | // Maps the field index into object->fields() to the field's ID (the ith element |
| 58 | // in the return vector). |
| 59 | static std::vector<uint32_t> FieldIdToIndex(const reflection::Object *object) { |
| 60 | std::vector<uint32_t> field_index_by_id; |
| 61 | field_index_by_id.resize(object->fields()->size()); |
| 62 | |
| 63 | // Create the mapping of field ID to the index into the vector. |
| 64 | for (uint32_t i = 0; i < object->fields()->size(); ++i) { |
| 65 | auto field = object->fields()->Get(i); |
| 66 | field_index_by_id[field->id()] = i; |
| 67 | } |
| 68 | |
| 69 | return field_index_by_id; |
| 70 | } |
| 71 | |
| 72 | static bool IsStructOrTable(const reflection::BaseType base_type) { |
| 73 | return base_type == reflection::BaseType::Obj; |
| 74 | } |
| 75 | |
| 76 | static bool IsScalar(const reflection::BaseType base_type) { |
| 77 | return base_type >= reflection::BaseType::UType && base_type <= reflection::BaseType::Double; |
| 78 | } |
| 79 | |
| 80 | static bool IsFloatingPoint(const reflection::BaseType base_type) { |
| 81 | return base_type == reflection::BaseType::Float || base_type == reflection::BaseType::Double; |
| 82 | } |
| 83 | |
| 84 | static bool IsBool(const reflection::BaseType base_type) { |
| 85 | return base_type == reflection::BaseType::Bool; |
| 86 | } |
| 87 | |
| 88 | static bool IsSingleByte(const reflection::BaseType base_type) { |
| 89 | return base_type >= reflection::BaseType::UType && base_type <= reflection::BaseType::UByte; |
| 90 | } |
| 91 | |
| 92 | static bool IsVector(const reflection::BaseType base_type) { |
| 93 | return base_type == reflection::BaseType::Vector; |
| 94 | } |
| 95 | |
| 96 | static std::string Denamespace(const flatbuffers::String *name, |
| 97 | std::string &ns) { |
| 98 | const size_t pos = name->str().find_last_of('.'); |
| 99 | if (pos == std::string::npos) { |
| 100 | ns = ""; |
| 101 | return name->str(); |
| 102 | } |
| 103 | ns = name->str().substr(0, pos); |
| 104 | return name->str().substr(pos + 1); |
| 105 | } |
| 106 | |
| 107 | static std::string Denamespace(const flatbuffers::String *name) { |
| 108 | std::string ns; |
| 109 | return Denamespace(name, ns); |
| 110 | } |
| 111 | |
| 112 | // A concrete base Flatbuffer Generator that specific language generators can |
| 113 | // derive from. |
| 114 | class BaseBfbsGenerator : public BfbsGenerator { |
| 115 | public: |
| 116 | virtual ~BaseBfbsGenerator() {} |
| 117 | BaseBfbsGenerator() : schema_(nullptr) {} |
| 118 | |
| 119 | virtual GeneratorStatus GenerateFromSchema( |
| 120 | const reflection::Schema *schema) = 0; |
| 121 | |
| 122 | // |
| 123 | virtual uint64_t SupportedAdvancedFeatures() const = 0; |
| 124 | |
| 125 | // Override of the Generator::generate method that does the initial |
| 126 | // deserialization and verification steps. |
| 127 | GeneratorStatus Generate(const uint8_t *buffer, |
| 128 | int64_t length) FLATBUFFERS_OVERRIDE { |
| 129 | flatbuffers::Verifier verifier(buffer, static_cast<size_t>(length)); |
| 130 | if (!reflection::VerifySchemaBuffer(verifier)) { |
| 131 | return FAILED_VERIFICATION; |
| 132 | } |
| 133 | |
| 134 | // Store the root schema since there are cases where leaf nodes refer to |
| 135 | // things in the root schema (e.g., indexing the objects). |
| 136 | schema_ = reflection::GetSchema(buffer); |
| 137 | |
| 138 | const uint64_t advance_features = static_cast<uint64_t>(schema_->advanced_features()); |
| 139 | if (advance_features > SupportedAdvancedFeatures()) { |
| 140 | return FAILED_VERIFICATION; |
| 141 | } |
| 142 | |
| 143 | GeneratorStatus status = GenerateFromSchema(schema_); |
| 144 | schema_ = nullptr; |
| 145 | return status; |
| 146 | } |
| 147 | |
| 148 | protected: |
| 149 | const reflection::Object *GetObject(const reflection::Type *type) const { |
| 150 | if (type->index() >= 0 && IsStructOrTable(type->base_type())) { |
| 151 | return GetObjectByIndex(type->index()); |
| 152 | } |
| 153 | return nullptr; |
| 154 | } |
| 155 | |
| 156 | const reflection::Enum *GetEnum(const reflection::Type *type) const { |
| 157 | // TODO(derekbailey): it would be better to have a explicit list of allowed |
| 158 | // base types, instead of negating Obj types. |
| 159 | if (type->index() >= 0 && !IsStructOrTable(type->base_type())) { |
| 160 | return GetEnumByIndex(type->index()); |
| 161 | } |
| 162 | return nullptr; |
| 163 | } |
| 164 | |
| 165 | // Used to get a object that is reference by index. (e.g. |
| 166 | // reflection::Type::index). Returns nullptr if no object is available. |
| 167 | const reflection::Object *GetObjectByIndex(int32_t index) const { |
| 168 | if (!schema_ || index < 0 || |
| 169 | index >= static_cast<int32_t>(schema_->objects()->size())) { |
| 170 | return nullptr; |
| 171 | } |
| 172 | return schema_->objects()->Get(index); |
| 173 | } |
| 174 | |
| 175 | // Used to get a enum that is reference by index. (e.g. |
| 176 | // reflection::Type::index). Returns nullptr if no enum is available. |
| 177 | const reflection::Enum *GetEnumByIndex(int32_t index) const { |
| 178 | if (!schema_ || index < 0 || |
| 179 | index >= static_cast<int32_t>(schema_->enums()->size())) { |
| 180 | return nullptr; |
| 181 | } |
| 182 | return schema_->enums()->Get(index); |
| 183 | } |
| 184 | |
| 185 | void ForAllFields(const reflection::Object *object, bool reverse, |
| 186 | std::function<void(const reflection::Field *)> func) const { |
| 187 | const std::vector<uint32_t> field_to_id_map = FieldIdToIndex(object); |
| 188 | for (size_t i = 0; i < field_to_id_map.size(); ++i) { |
| 189 | func(object->fields()->Get( |
| 190 | field_to_id_map[reverse ? field_to_id_map.size() - (i + 1) : i])); |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | bool IsTable(const reflection::Type *type, bool use_element = false) const { |
| 195 | return !IsStruct(type, use_element); |
| 196 | } |
| 197 | |
| 198 | bool IsStruct(const reflection::Type *type, bool use_element = false) const { |
| 199 | const reflection::BaseType base_type = |
| 200 | use_element ? type->element() : type->base_type(); |
| 201 | return IsStructOrTable(base_type) && |
| 202 | GetObjectByIndex(type->index())->is_struct(); |
| 203 | } |
| 204 | |
| 205 | const reflection::Schema *schema_; |
| 206 | }; |
| 207 | |
| 208 | } // namespace flatbuffers |
| 209 | |
| 210 | #endif // FLATBUFFERS_BFBS_GEN_H_ |