From 132e6a822045434cd5a6fb636c32791480b27f37 Mon Sep 17 00:00:00 2001 From: desqaz Date: Thu, 22 Feb 2018 18:19:08 +0100 Subject: [PATCH] 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 --- include/flatbuffers/flatbuffers.h | 3 +++ samples/monster_generated.h | 15 +++++++++++ src/idl_gen_cpp.cpp | 24 ++++++++++++++++++ tests/monster_test.bfbs | Bin 5440 -> 5400 bytes tests/monster_test_generated.h | 15 +++++++++++ .../namespace_test2_generated.ts | 20 +++++++-------- tests/test.cpp | 8 +++--- tests/union_vector/union_vector_generated.h | 15 +++++++++++ 8 files changed, 86 insertions(+), 14 deletions(-) diff --git a/include/flatbuffers/flatbuffers.h b/include/flatbuffers/flatbuffers.h index 94e581656..214ce267b 100644 --- a/include/flatbuffers/flatbuffers.h +++ b/include/flatbuffers/flatbuffers.h @@ -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(buf); } + // Base class for native objects (FlatBuffer data de-serialized into native // C++ data structures). // Contains no functionality, purely documentative. diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 32d44d002..00c0595e1 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -727,6 +727,10 @@ inline const MyGame::Sample::Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot(buf); } +inline const MyGame::Sample::Monster *GetSizePrefixedMonster(const void *buf) { + return flatbuffers::GetSizePrefixedRoot(buf); +} + inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot(buf); } @@ -736,12 +740,23 @@ inline bool VerifyMonsterBuffer( return verifier.VerifyBuffer(nullptr); } +inline bool VerifySizePrefixedMonsterBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(nullptr); +} + inline void FinishMonsterBuffer( flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root); } +inline void FinishSizePrefixedMonsterBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root); +} + inline flatbuffers::unique_ptr UnPackMonster( const void *buf, const flatbuffers::resolver_function_t *res = nullptr) { diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index ea01bf1c2..52d76345f 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -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 = diff --git a/tests/monster_test.bfbs b/tests/monster_test.bfbs index 4cb46a7ca5a2f046e7e4daa2b4c5d2a7e1bc5bf5..843e308c5204d9c36bad66ae49ac2d37041b4cce 100644 GIT binary patch literal 5400 zcmai&e`uE16~}L4j4@438m-Mz*F9!ML_*N$q(~_-Q)@+|ROgBe;+y1o%`5NwJmLM3 z#n3;743RQMkumy58AFDQ(LYKVLrVWB#lK{K3@M{X{~Du=;kvBboORuveZJ?u_sx?R zWjCDMd!PF~=bm%!x#v0WGvu6`JTiIOxlL}!b-5nb=LXysW{&^P9fD3luR(L`9jne= z?Pt#T7NChi=1)2Ipwqe00q4GAzI(HCA45wBI8VP0p50Kn!@1+&x)`50ImMoSt&%3} zIb?fpV5FV(pVp!Nd1xQ>B7}H%;?m27N<4OWsZd!c$72VROJ3#*Yd4`eD1!Fi%Q8B4 z^{vk=9I$<4hwM9!z@7BcQSBMX%b#w_AD=#vCbfE8byvK+{taM0$g*sR{zoV{N{?Bs zCK+eqTK#lM`kg7vl;iJ~>hp(_M#bH3%8(EG+%|l%s*mK!Ios%3N;B4eL5JSIm`(j! zf9Jnh)@fzAHv4HT#e&|{t)YZoI;mL^m3)M^bGszTwaam z+=9sxRy#_A0kd0p+4ll80d0qxHlD1{$JHq!<@7>fHXb`tO)IY?v*pH|6qNi3I`_Hx zb7Ro2581LK*s0y5e=pSJK3gcu-}-ZI$6vjlM;S}zYtS&%=I1k)7UF~DQlS>ty!P*c z`62WcOoyNq#-Gxkhx(wK%zr^IyMm8DTgkE~kRJRmy`;lpalc&P=I_gvOWJ zd-T7?vL)y!xKHSBp@8CVl=1KAUq{9-(e`7g&RPVm@cetsm(cEy(h%Chd^i2u&`mI( zFu%&u2M};Gcn^J4NTS#$>1ZyUZB*i2pH`>S z;`ChF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zn1f8}Nyi_k#P_H8`_=SR`~%7T20qG*&g^CPl=^|h&m8La5#_!I-lv^8?Vye7>(mjE zH6wrAB0E#xVwCoU?K9Fgfh8K*eA(buf); } +inline const MyGame::Example::Monster *GetSizePrefixedMonster(const void *buf) { + return flatbuffers::GetSizePrefixedRoot(buf); +} + inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot(buf); } @@ -2242,6 +2246,11 @@ inline bool VerifyMonsterBuffer( return verifier.VerifyBuffer(MonsterIdentifier()); } +inline bool VerifySizePrefixedMonsterBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(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 root) { + fbb.FinishSizePrefixed(root, MonsterIdentifier()); +} + inline flatbuffers::unique_ptr UnPackMonster( const void *buf, const flatbuffers::resolver_function_t *res = nullptr) { diff --git a/tests/namespace_test/namespace_test2_generated.ts b/tests/namespace_test/namespace_test2_generated.ts index aa623a879..18f1d6e87 100644 --- a/tests/namespace_test/namespace_test2_generated.ts +++ b/tests/namespace_test/namespace_test2_generated.ts @@ -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); }; /** diff --git a/tests/test.cpp b/tests/test.cpp index a89e56ddb..2da79f195 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -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(nullptr), true); + TEST_EQ(VerifySizePrefixedMonsterBuffer(verifier), true); // Access it. - auto m = flatbuffers::GetSizePrefixedRoot( - 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"); diff --git a/tests/union_vector/union_vector_generated.h b/tests/union_vector/union_vector_generated.h index 77de579ed..ba9234dab 100644 --- a/tests/union_vector/union_vector_generated.h +++ b/tests/union_vector/union_vector_generated.h @@ -706,6 +706,10 @@ inline const Movie *GetMovie(const void *buf) { return flatbuffers::GetRoot(buf); } +inline const Movie *GetSizePrefixedMovie(const void *buf) { + return flatbuffers::GetSizePrefixedRoot(buf); +} + inline Movie *GetMutableMovie(void *buf) { return flatbuffers::GetMutableRoot(buf); } @@ -724,12 +728,23 @@ inline bool VerifyMovieBuffer( return verifier.VerifyBuffer(MovieIdentifier()); } +inline bool VerifySizePrefixedMovieBuffer( + flatbuffers::Verifier &verifier) { + return verifier.VerifySizePrefixedBuffer(MovieIdentifier()); +} + inline void FinishMovieBuffer( flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root, MovieIdentifier()); } +inline void FinishSizePrefixedMovieBuffer( + flatbuffers::FlatBufferBuilder &fbb, + flatbuffers::Offset root) { + fbb.FinishSizePrefixed(root, MovieIdentifier()); +} + inline flatbuffers::unique_ptr UnPackMovie( const void *buf, const flatbuffers::resolver_function_t *res = nullptr) {