|
18 | 18 | #define FLATBUFFERS_BFBS_GEN_H_ |
19 | 19 |
|
20 | 20 | #include <cstdint> |
| 21 | +#include <map> |
21 | 22 |
|
22 | 23 | #include "flatbuffers/code_generator.h" |
23 | 24 | #include "flatbuffers/reflection_generated.h" |
@@ -63,16 +64,12 @@ static void ForAllDocumentation( |
63 | 64 | } |
64 | 65 | } |
65 | 66 |
|
66 | | -// Maps the field index into object->fields() to the field's ID (the ith element |
67 | | -// in the return vector). |
68 | | -static std::vector<uint32_t> FieldIdToIndex(const reflection::Object* object) { |
69 | | - std::vector<uint32_t> field_index_by_id; |
70 | | - field_index_by_id.resize(object->fields()->size()); |
71 | | - |
72 | | - // Create the mapping of field ID to the index into the vector. |
| 67 | +// Maps field ID to the field's index in object->fields(). |
| 68 | +static std::map<uint32_t, uint32_t> FieldIdToIndex( |
| 69 | + const reflection::Object* object) { |
| 70 | + std::map<uint32_t, uint32_t> field_index_by_id; |
73 | 71 | for (uint32_t i = 0; i < object->fields()->size(); ++i) { |
74 | | - auto field = object->fields()->Get(i); |
75 | | - field_index_by_id[field->id()] = i; |
| 72 | + field_index_by_id[object->fields()->Get(i)->id()] = i; |
76 | 73 | } |
77 | 74 |
|
78 | 75 | return field_index_by_id; |
@@ -186,10 +183,15 @@ class BaseBfbsGenerator : public CodeGenerator { |
186 | 183 |
|
187 | 184 | void ForAllFields(const reflection::Object* object, bool reverse, |
188 | 185 | std::function<void(const reflection::Field*)> func) const { |
189 | | - const std::vector<uint32_t> field_to_id_map = FieldIdToIndex(object); |
190 | | - for (size_t i = 0; i < field_to_id_map.size(); ++i) { |
191 | | - func(object->fields()->Get( |
192 | | - field_to_id_map[reverse ? field_to_id_map.size() - (i + 1) : i])); |
| 186 | + const auto field_to_id_map = FieldIdToIndex(object); |
| 187 | + if (!reverse) { |
| 188 | + for (auto it = field_to_id_map.begin(); it != field_to_id_map.end(); |
| 189 | + ++it) |
| 190 | + func(object->fields()->Get(it->second)); |
| 191 | + } else { |
| 192 | + for (auto it = field_to_id_map.rbegin(); it != field_to_id_map.rend(); |
| 193 | + ++it) |
| 194 | + func(object->fields()->Get(it->second)); |
193 | 195 | } |
194 | 196 | } |
195 | 197 |
|
|
0 commit comments