From d4d7a84e11f4b6299ed13b7efa3872b81d675719 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 4 May 2015 10:35:53 -0700 Subject: [PATCH 1/2] Added missing --gen-mutable to CMakeLists.txt Change-Id: I56392340de4439d05fa8f06a7336ff72c6f9346d Tested: on Linux --- CMakeLists.txt | 2 +- samples/monster_generated.h | 11 +++++++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 617f67b05..32287ea34 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -104,7 +104,7 @@ function(compile_flatbuffers_schema_to_cpp SRC_FBS) string(REGEX REPLACE "\\.fbs$" "_generated.h" GEN_HEADER ${SRC_FBS}) add_custom_command( OUTPUT ${GEN_HEADER} - COMMAND flatc -c -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" + COMMAND flatc -c --gen-mutable -o "${SRC_FBS_DIR}" "${CMAKE_CURRENT_SOURCE_DIR}/${SRC_FBS}" DEPENDS flatc) endfunction() diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 945d992fc..2d0b00266 100755 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -50,18 +50,27 @@ MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS { : x_(flatbuffers::EndianScalar(x)), y_(flatbuffers::EndianScalar(y)), z_(flatbuffers::EndianScalar(z)) { } float x() const { return flatbuffers::EndianScalar(x_); } + void mutate_x(float x) { flatbuffers::WriteScalar(&x_, x); } float y() const { return flatbuffers::EndianScalar(y_); } + void mutate_y(float y) { flatbuffers::WriteScalar(&y_, y); } float z() const { return flatbuffers::EndianScalar(z_); } + void mutate_z(float z) { flatbuffers::WriteScalar(&z_, z); } }; STRUCT_END(Vec3, 12); struct Monster FLATBUFFERS_FINAL_CLASS : private flatbuffers::Table { const Vec3 *pos() const { return GetStruct(4); } + Vec3 *mutable_pos() { return GetStruct(4); } int16_t mana() const { return GetField(6, 150); } + bool mutate_mana(int16_t mana) { return SetField(6, mana); } int16_t hp() const { return GetField(8, 100); } + bool mutate_hp(int16_t hp) { return SetField(8, hp); } const flatbuffers::String *name() const { return GetPointer(10); } + flatbuffers::String *mutable_name() { return GetPointer(10); } const flatbuffers::Vector *inventory() const { return GetPointer *>(14); } + flatbuffers::Vector *mutable_inventory() { return GetPointer *>(14); } Color color() const { return static_cast(GetField(16, 2)); } + bool mutate_color(Color color) { return SetField(16, static_cast(color)); } bool Verify(flatbuffers::Verifier &verifier) const { return VerifyTableStart(verifier) && VerifyField(verifier, 4 /* pos */) && @@ -120,6 +129,8 @@ inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, An inline const Monster *GetMonster(const void *buf) { return flatbuffers::GetRoot(buf); } +inline Monster *GetMutableMonster(void *buf) { return flatbuffers::GetMutableRoot(buf); } + inline bool VerifyMonsterBuffer(flatbuffers::Verifier &verifier) { return verifier.VerifyBuffer(); } inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root); } From 37e6efe1f97b0788bd457d61e52ed40e12d2e69a Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 4 May 2015 10:52:40 -0700 Subject: [PATCH 2/2] Added accessor for file_extension in generated code. Change-Id: I2de7d14dbb1f7b8f81022dd2c9da65060ae49300 Tested: on Linux. --- src/idl_gen_cpp.cpp | 7 +++++++ tests/monster_test_generated.h | 2 ++ tests/test.cpp | 1 + 3 files changed, 10 insertions(+) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index aab572905..37d968e8a 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -717,6 +717,13 @@ std::string GenerateCPP(const Parser &parser, code += name + "Identifier()); }\n\n"; } + if (parser.file_extension_.length()) { + // Return the extension + code += "inline const char *" + name; + code += "Extension() { return \"" + parser.file_extension_; + code += "\"; }\n\n"; + } + // Finish a buffer with a given root object: code += "inline void Finish" + name; code += "Buffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset<"; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index f729fa02a..5de3da024 100755 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -332,6 +332,8 @@ inline const char *MonsterIdentifier() { return "MONS"; } inline bool MonsterBufferHasIdentifier(const void *buf) { return flatbuffers::BufferHasIdentifier(buf, MonsterIdentifier()); } +inline const char *MonsterExtension() { return "mon"; } + inline void FinishMonsterBuffer(flatbuffers::FlatBufferBuilder &fbb, flatbuffers::Offset root) { fbb.Finish(root, MonsterIdentifier()); } } // namespace Example diff --git a/tests/test.cpp b/tests/test.cpp index 766b3258d..b0cace944 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -135,6 +135,7 @@ void AccessFlatBufferTest(const uint8_t *flatbuf, size_t length) { TEST_EQ(strcmp(MonsterIdentifier(), "MONS"), 0); TEST_EQ(MonsterBufferHasIdentifier(flatbuf), true); + TEST_EQ(strcmp(MonsterExtension(), "mon"), 0); // Access the buffer from the root. auto monster = GetMonster(flatbuf);