Skip to content

Commit f8fe811

Browse files
committed
FlexBuffers: allow getting size & undo of map in progress
1 parent 0cc525b commit f8fe811

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

include/flatbuffers/flexbuffers.h

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,7 @@ class Builder FLATBUFFERS_FINAL_CLASS {
11281128

11291129
size_t EndMap(size_t start) {
11301130
// We should have interleaved keys and values on the stack.
1131-
// Make sure it is an even number:
1132-
auto len = stack_.size() - start;
1133-
FLATBUFFERS_ASSERT(!(len & 1));
1134-
len /= 2;
1131+
auto len = MapElementCount(start);
11351132
// Make sure keys are all strings:
11361133
for (auto key = start; key < stack_.size(); key += 2) {
11371134
FLATBUFFERS_ASSERT(stack_[key].type_ == FBT_KEY);
@@ -1289,6 +1286,14 @@ class Builder FLATBUFFERS_FINAL_CLASS {
12891286
EndMap(start);
12901287
}
12911288

1289+
size_t MapElementCount(size_t start) {
1290+
// Make sure it is an even number:
1291+
auto len = stack_.size() - start;
1292+
FLATBUFFERS_ASSERT(!(len & 1));
1293+
len /= 2;
1294+
return len;
1295+
}
1296+
12921297
// If you wish to share a value explicitly (a value not shared automatically
12931298
// through one of the BUILDER_FLAG_SHARE_* flags) you can do so with these
12941299
// functions. Or if you wish to turn those flags off for performance reasons
@@ -1307,6 +1312,12 @@ class Builder FLATBUFFERS_FINAL_CLASS {
13071312
ReuseValue(v);
13081313
}
13091314

1315+
// Undo the last element serialized. Call once for a value and once for a
1316+
// key.
1317+
void Undo() {
1318+
stack_.pop_back();
1319+
}
1320+
13101321
// Overloaded Add that tries to call the correct function above.
13111322
void Add(int8_t i) { Int(i); }
13121323
void Add(int16_t i) { Int(i); }

0 commit comments

Comments
 (0)