Add new c++ helpers to ease usage of size prefixed FlatBuffers (#4626)
It was missing some helpers when we choose to use size prefixed FlatBuffers. * Add general helper : GetPrefixedSize * Add generated helpers : * GetSizePrefixedXXX * VerfifySizePrefixedXXXBuffer * FinishSizePrefixedXXXBuffer
This commit is contained in:
parent
19c81b11b3
commit
132e6a8220
|
@ -2069,6 +2069,9 @@ inline const uint8_t *GetBufferStartFromRootPointer(const void *root) {
|
|||
return nullptr;
|
||||
}
|
||||
|
||||
/// @brief This return the prefixed size of a FlatBuffer.
|
||||
inline uoffset_t GetPrefixedSize(const uint8_t* buf){ return ReadScalar<uoffset_t>(buf); }
|
||||
|
||||
// Base class for native objects (FlatBuffer data de-serialized into native
|
||||
// C++ data structures).
|
||||
// Contains no functionality, purely documentative.
|
||||
|
|
|
@ -727,6 +727,10 @@ inline const MyGame::Sample::Monster *GetMonster(const void *buf) {
|
|||
return flatbuffers::GetRoot<MyGame::Sample::Monster>(buf);
|
||||
}
|
||||
|
||||
inline const MyGame::Sample::Monster *GetSizePrefixedMonster(const void *buf) {
|
||||
return flatbuffers::GetSizePrefixedRoot<MyGame::Sample::Monster>(buf);
|
||||
}
|
||||
|
||||
inline Monster *GetMutableMonster(void *buf) {
|
||||
return flatbuffers::GetMutableRoot<Monster>(buf);
|
||||
}
|
||||
|
@ -736,12 +740,23 @@ inline bool VerifyMonsterBuffer(
|
|||
return verifier.VerifyBuffer<MyGame::Sample::Monster>(nullptr);
|
||||
}
|
||||
|
||||
inline bool VerifySizePrefixedMonsterBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<MyGame::Sample::Monster>(nullptr);
|
||||
}
|
||||
|
||||
inline void FinishMonsterBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<MyGame::Sample::Monster> root) {
|
||||
fbb.Finish(root);
|
||||
}
|
||||
|
||||
inline void FinishSizePrefixedMonsterBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<MyGame::Sample::Monster> root) {
|
||||
fbb.FinishSizePrefixed(root);
|
||||
}
|
||||
|
||||
inline flatbuffers::unique_ptr<MonsterT> UnPackMonster(
|
||||
const void *buf,
|
||||
const flatbuffers::resolver_function_t *res = nullptr) {
|
||||
|
|
|
@ -333,6 +333,14 @@ class CppGenerator : public BaseGenerator {
|
|||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
code_ += "inline \\";
|
||||
code_ +=
|
||||
"const {{CPP_NAME}} *{{NULLABLE_EXT}}GetSizePrefixed{{STRUCT_NAME}}(const void "
|
||||
"*buf) {";
|
||||
code_ += " return flatbuffers::GetSizePrefixedRoot<{{CPP_NAME}}>(buf);";
|
||||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
if (parser_.opts.mutable_buffer) {
|
||||
code_ += "inline \\";
|
||||
code_ += "{{STRUCT_NAME}} *GetMutable{{STRUCT_NAME}}(void *buf) {";
|
||||
|
@ -370,6 +378,12 @@ class CppGenerator : public BaseGenerator {
|
|||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
code_ += "inline bool VerifySizePrefixed{{STRUCT_NAME}}Buffer(";
|
||||
code_ += " flatbuffers::Verifier &verifier) {";
|
||||
code_ += " return verifier.VerifySizePrefixedBuffer<{{CPP_NAME}}>({{ID}});";
|
||||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
if (parser_.file_extension_.length()) {
|
||||
// Return the extension
|
||||
code_ += "inline const char *{{STRUCT_NAME}}Extension() {";
|
||||
|
@ -389,6 +403,16 @@ class CppGenerator : public BaseGenerator {
|
|||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
code_ += "inline void FinishSizePrefixed{{STRUCT_NAME}}Buffer(";
|
||||
code_ += " flatbuffers::FlatBufferBuilder &fbb,";
|
||||
code_ += " flatbuffers::Offset<{{CPP_NAME}}> root) {";
|
||||
if (parser_.file_identifier_.length())
|
||||
code_ += " fbb.FinishSizePrefixed(root, {{STRUCT_NAME}}Identifier());";
|
||||
else
|
||||
code_ += " fbb.FinishSizePrefixed(root);";
|
||||
code_ += "}";
|
||||
code_ += "";
|
||||
|
||||
if (parser_.opts.generate_object_based_api) {
|
||||
// A convenient root unpack function.
|
||||
auto native_name =
|
||||
|
|
Binary file not shown.
|
@ -2224,6 +2224,10 @@ inline const MyGame::Example::Monster *GetMonster(const void *buf) {
|
|||
return flatbuffers::GetRoot<MyGame::Example::Monster>(buf);
|
||||
}
|
||||
|
||||
inline const MyGame::Example::Monster *GetSizePrefixedMonster(const void *buf) {
|
||||
return flatbuffers::GetSizePrefixedRoot<MyGame::Example::Monster>(buf);
|
||||
}
|
||||
|
||||
inline Monster *GetMutableMonster(void *buf) {
|
||||
return flatbuffers::GetMutableRoot<Monster>(buf);
|
||||
}
|
||||
|
@ -2242,6 +2246,11 @@ inline bool VerifyMonsterBuffer(
|
|||
return verifier.VerifyBuffer<MyGame::Example::Monster>(MonsterIdentifier());
|
||||
}
|
||||
|
||||
inline bool VerifySizePrefixedMonsterBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<MyGame::Example::Monster>(MonsterIdentifier());
|
||||
}
|
||||
|
||||
inline const char *MonsterExtension() {
|
||||
return "mon";
|
||||
}
|
||||
|
@ -2252,6 +2261,12 @@ inline void FinishMonsterBuffer(
|
|||
fbb.Finish(root, MonsterIdentifier());
|
||||
}
|
||||
|
||||
inline void FinishSizePrefixedMonsterBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<MyGame::Example::Monster> root) {
|
||||
fbb.FinishSizePrefixed(root, MonsterIdentifier());
|
||||
}
|
||||
|
||||
inline flatbuffers::unique_ptr<MonsterT> UnPackMonster(
|
||||
const void *buf,
|
||||
const flatbuffers::resolver_function_t *res = nullptr) {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
// automatically generated by the FlatBuffers compiler, do not modify
|
||||
|
||||
import * as NS4989953370203581498 from "./namespace_test1_generated";
|
||||
import * as NS9459827973991502386 from "./namespace_test1_generated";
|
||||
/**
|
||||
* @constructor
|
||||
*/
|
||||
|
@ -39,24 +39,24 @@ static getRootAsTableInFirstNS(bb:flatbuffers.ByteBuffer, obj?:TableInFirstNS):T
|
|||
* @param {NamespaceA.NamespaceB.TableInNestedNS=} obj
|
||||
* @returns {NamespaceA.NamespaceB.TableInNestedNS|null}
|
||||
*/
|
||||
fooTable(obj?:NS4989953370203581498.NamespaceA.NamespaceB.TableInNestedNS):NS4989953370203581498.NamespaceA.NamespaceB.TableInNestedNS|null {
|
||||
fooTable(obj?:NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS):NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 4);
|
||||
return offset ? (obj || new NS4989953370203581498.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
|
||||
return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.TableInNestedNS).__init(this.bb!.__indirect(this.bb_pos + offset), this.bb!) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
* @returns {NamespaceA.NamespaceB.EnumInNestedNS}
|
||||
*/
|
||||
fooEnum():NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS {
|
||||
fooEnum():NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 6);
|
||||
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS.A;
|
||||
return offset ? /** @type {NamespaceA.NamespaceB.EnumInNestedNS} */ (this.bb!.readInt8(this.bb_pos + offset)) : NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS.A;
|
||||
};
|
||||
|
||||
/**
|
||||
* @param {NamespaceA.NamespaceB.EnumInNestedNS} value
|
||||
* @returns {boolean}
|
||||
*/
|
||||
mutate_foo_enum(value:NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS):boolean {
|
||||
mutate_foo_enum(value:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS):boolean {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 6);
|
||||
|
||||
if (offset === 0) {
|
||||
|
@ -71,9 +71,9 @@ mutate_foo_enum(value:NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS
|
|||
* @param {NamespaceA.NamespaceB.StructInNestedNS=} obj
|
||||
* @returns {NamespaceA.NamespaceB.StructInNestedNS|null}
|
||||
*/
|
||||
fooStruct(obj?:NS4989953370203581498.NamespaceA.NamespaceB.StructInNestedNS):NS4989953370203581498.NamespaceA.NamespaceB.StructInNestedNS|null {
|
||||
fooStruct(obj?:NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS):NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS|null {
|
||||
var offset = this.bb!.__offset(this.bb_pos, 8);
|
||||
return offset ? (obj || new NS4989953370203581498.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null;
|
||||
return offset ? (obj || new NS9459827973991502386.NamespaceA.NamespaceB.StructInNestedNS).__init(this.bb_pos + offset, this.bb!) : null;
|
||||
};
|
||||
|
||||
/**
|
||||
|
@ -95,8 +95,8 @@ static addFooTable(builder:flatbuffers.Builder, fooTableOffset:flatbuffers.Offse
|
|||
* @param {flatbuffers.Builder} builder
|
||||
* @param {NamespaceA.NamespaceB.EnumInNestedNS} fooEnum
|
||||
*/
|
||||
static addFooEnum(builder:flatbuffers.Builder, fooEnum:NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS) {
|
||||
builder.addFieldInt8(1, fooEnum, NS4989953370203581498.NamespaceA.NamespaceB.EnumInNestedNS.A);
|
||||
static addFooEnum(builder:flatbuffers.Builder, fooEnum:NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS) {
|
||||
builder.addFieldInt8(1, fooEnum, NS9459827973991502386.NamespaceA.NamespaceB.EnumInNestedNS.A);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -514,16 +514,16 @@ void ObjectFlatBuffersTest(uint8_t *flatbuf) {
|
|||
void SizePrefixedTest() {
|
||||
// Create size prefixed buffer.
|
||||
flatbuffers::FlatBufferBuilder fbb;
|
||||
fbb.FinishSizePrefixed(
|
||||
FinishSizePrefixedMonsterBuffer(
|
||||
fbb,
|
||||
CreateMonster(fbb, 0, 200, 300, fbb.CreateString("bob")));
|
||||
|
||||
// Verify it.
|
||||
flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize());
|
||||
TEST_EQ(verifier.VerifySizePrefixedBuffer<Monster>(nullptr), true);
|
||||
TEST_EQ(VerifySizePrefixedMonsterBuffer(verifier), true);
|
||||
|
||||
// Access it.
|
||||
auto m = flatbuffers::GetSizePrefixedRoot<MyGame::Example::Monster>(
|
||||
fbb.GetBufferPointer());
|
||||
auto m = GetSizePrefixedMonster(fbb.GetBufferPointer());
|
||||
TEST_EQ(m->mana(), 200);
|
||||
TEST_EQ(m->hp(), 300);
|
||||
TEST_EQ_STR(m->name()->c_str(), "bob");
|
||||
|
|
|
@ -706,6 +706,10 @@ inline const Movie *GetMovie(const void *buf) {
|
|||
return flatbuffers::GetRoot<Movie>(buf);
|
||||
}
|
||||
|
||||
inline const Movie *GetSizePrefixedMovie(const void *buf) {
|
||||
return flatbuffers::GetSizePrefixedRoot<Movie>(buf);
|
||||
}
|
||||
|
||||
inline Movie *GetMutableMovie(void *buf) {
|
||||
return flatbuffers::GetMutableRoot<Movie>(buf);
|
||||
}
|
||||
|
@ -724,12 +728,23 @@ inline bool VerifyMovieBuffer(
|
|||
return verifier.VerifyBuffer<Movie>(MovieIdentifier());
|
||||
}
|
||||
|
||||
inline bool VerifySizePrefixedMovieBuffer(
|
||||
flatbuffers::Verifier &verifier) {
|
||||
return verifier.VerifySizePrefixedBuffer<Movie>(MovieIdentifier());
|
||||
}
|
||||
|
||||
inline void FinishMovieBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<Movie> root) {
|
||||
fbb.Finish(root, MovieIdentifier());
|
||||
}
|
||||
|
||||
inline void FinishSizePrefixedMovieBuffer(
|
||||
flatbuffers::FlatBufferBuilder &fbb,
|
||||
flatbuffers::Offset<Movie> root) {
|
||||
fbb.FinishSizePrefixed(root, MovieIdentifier());
|
||||
}
|
||||
|
||||
inline flatbuffers::unique_ptr<MovieT> UnPackMovie(
|
||||
const void *buf,
|
||||
const flatbuffers::resolver_function_t *res = nullptr) {
|
||||
|
|
Loading…
Reference in New Issue