Skip to content

Commit d6f8c4a

Browse files
committed
fix: use std::map for field ID mapping in ForAllFields
Replace std::vector indexed by field->id() with std::map in both ForAllFields (reflection.cpp) and FieldIdToIndex (bfbs_gen.h).
1 parent e223d69 commit d6f8c4a

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

src/bfbs_gen.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#define FLATBUFFERS_BFBS_GEN_H_
1919

2020
#include <cstdint>
21+
#include <map>
2122

2223
#include "flatbuffers/code_generator.h"
2324
#include "flatbuffers/reflection_generated.h"
@@ -63,16 +64,12 @@ static void ForAllDocumentation(
6364
}
6465
}
6566

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;
7371
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;
7673
}
7774

7875
return field_index_by_id;
@@ -186,10 +183,15 @@ class BaseBfbsGenerator : public CodeGenerator {
186183

187184
void ForAllFields(const reflection::Object* object, bool reverse,
188185
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));
193195
}
194196
}
195197

src/reflection.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
#include "flatbuffers/reflection.h"
1818

19+
#include <map>
20+
1921
#include "flatbuffers/util.h"
2022

2123
// Helper functionality for reflection.
@@ -378,18 +380,16 @@ std::string GetAnyValueS(reflection::BaseType type, const uint8_t* data,
378380

379381
void ForAllFields(const reflection::Object* object, bool reverse,
380382
std::function<void(const reflection::Field*)> func) {
381-
std::vector<uint32_t> field_to_id_map;
382-
field_to_id_map.resize(object->fields()->size());
383-
384-
// Create the mapping of field ID to the index into the vector.
385-
for (uint32_t i = 0; i < object->fields()->size(); ++i) {
386-
auto field = object->fields()->Get(i);
387-
field_to_id_map[field->id()] = i;
388-
}
383+
std::map<uint32_t, uint32_t> field_to_id_map;
384+
for (uint32_t i = 0; i < object->fields()->size(); ++i)
385+
field_to_id_map[object->fields()->Get(i)->id()] = i;
389386

390-
for (size_t i = 0; i < field_to_id_map.size(); ++i) {
391-
func(object->fields()->Get(
392-
field_to_id_map[reverse ? field_to_id_map.size() - (i + 1) : i]));
387+
if (!reverse) {
388+
for (auto it = field_to_id_map.begin(); it != field_to_id_map.end(); ++it)
389+
func(object->fields()->Get(it->second));
390+
} else {
391+
for (auto it = field_to_id_map.rbegin(); it != field_to_id_map.rend(); ++it)
392+
func(object->fields()->Get(it->second));
393393
}
394394
}
395395

0 commit comments

Comments
 (0)