From 853f7033e03ec2f6e1593946cd0bb6e2eb561c2c Mon Sep 17 00:00:00 2001 From: Louis-Paul CORDIER Date: Thu, 16 Nov 2017 19:08:41 +0100 Subject: [PATCH] =?UTF-8?q?Remove=20copy=20constructor=20to=20make=20flatb?= =?UTF-8?q?uffers=20struct=20trivially=20copyable=E2=80=A6=20(#4476)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Remove copy constructor to make flatbuffers struct trivially copyable + add tests. * Add support non c++11 compliant compilers. * Fix std::trivially_copyiable test for non-C++11 compliant compilers. * Fix trivially_copyable not part of glibc < 5 even with c++11 switch enabled. --- src/idl_gen_cpp.cpp | 5 ----- tests/monster_test_generated.h | 9 --------- tests/test.cpp | 15 +++++++++++++++ 3 files changed, 15 insertions(+), 14 deletions(-) diff --git a/src/idl_gen_cpp.cpp b/src/idl_gen_cpp.cpp index b94c2f7b0..623ada2f1 100644 --- a/src/idl_gen_cpp.cpp +++ b/src/idl_gen_cpp.cpp @@ -2284,11 +2284,6 @@ class CppGenerator : public BaseGenerator { code_ += " memset(this, 0, sizeof({{STRUCT_NAME}}));"; code_ += " }"; - // Generate a copy constructor. - code_ += " {{STRUCT_NAME}}(const {{STRUCT_NAME}} &_o) {"; - code_ += " memcpy(this, &_o, sizeof({{STRUCT_NAME}}));"; - code_ += " }"; - // Generate a constructor that takes all fields as arguments. std::string arg_list; std::string init_list; diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h index c9b1572dd..14a2ae82c 100644 --- a/tests/monster_test_generated.h +++ b/tests/monster_test_generated.h @@ -197,9 +197,6 @@ MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { Test() { memset(this, 0, sizeof(Test)); } - Test(const Test &_o) { - memcpy(this, &_o, sizeof(Test)); - } Test(int16_t _a, int8_t _b) : a_(flatbuffers::EndianScalar(_a)), b_(flatbuffers::EndianScalar(_b)), @@ -237,9 +234,6 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { Vec3() { memset(this, 0, sizeof(Vec3)); } - Vec3(const Vec3 &_o) { - memcpy(this, &_o, sizeof(Vec3)); - } Vec3(float _x, float _y, float _z, double _test1, Color _test2, const Test &_test3) : x_(flatbuffers::EndianScalar(_x)), y_(flatbuffers::EndianScalar(_y)), @@ -302,9 +296,6 @@ MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { Ability() { memset(this, 0, sizeof(Ability)); } - Ability(const Ability &_o) { - memcpy(this, &_o, sizeof(Ability)); - } Ability(uint32_t _id, uint32_t _distance) : id_(flatbuffers::EndianScalar(_id)), distance_(flatbuffers::EndianScalar(_distance)) { diff --git a/tests/test.cpp b/tests/test.cpp index e4145bd22..e2a3e1f66 100644 --- a/tests/test.cpp +++ b/tests/test.cpp @@ -526,6 +526,18 @@ void SizePrefixedTest() { TEST_EQ_STR(m->name()->c_str(), "bob"); } + +void TriviallyCopyableTest() { + #if __GNUG__ && __GNUC__ < 5 + TEST_EQ(__has_trivial_copy(Vec3), true); + #else + #if __cplusplus >= 201103L + TEST_EQ(std::is_trivially_copyable::value, true); + #endif + #endif +} + + // example of parsing text straight into a buffer, and generating // text back from it: void ParseAndGenerateTextTest() { @@ -1809,6 +1821,9 @@ int main(int /*argc*/, const char * /*argv*/[]) { #else auto &flatbuf = flatbuf1; #endif // !defined(FLATBUFFERS_CPP98_STL) + + TriviallyCopyableTest(); + AccessFlatBufferTest(reinterpret_cast(rawbuf.c_str()), rawbuf.length()); AccessFlatBufferTest(flatbuf.data(), flatbuf.size());