Commit 853f7033 authored by Louis-Paul CORDIER's avatar Louis-Paul CORDIER Committed by Wouter van Oortmerssen

Remove copy constructor to make flatbuffers struct trivially copyable… (#4476)

* 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.
parent e2c7196e
...@@ -2284,11 +2284,6 @@ class CppGenerator : public BaseGenerator { ...@@ -2284,11 +2284,6 @@ class CppGenerator : public BaseGenerator {
code_ += " memset(this, 0, sizeof({{STRUCT_NAME}}));"; code_ += " memset(this, 0, sizeof({{STRUCT_NAME}}));";
code_ += " }"; 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. // Generate a constructor that takes all fields as arguments.
std::string arg_list; std::string arg_list;
std::string init_list; std::string init_list;
......
...@@ -197,9 +197,6 @@ MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS { ...@@ -197,9 +197,6 @@ MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
Test() { Test() {
memset(this, 0, sizeof(Test)); memset(this, 0, sizeof(Test));
} }
Test(const Test &_o) {
memcpy(this, &_o, sizeof(Test));
}
Test(int16_t _a, int8_t _b) Test(int16_t _a, int8_t _b)
: a_(flatbuffers::EndianScalar(_a)), : a_(flatbuffers::EndianScalar(_a)),
b_(flatbuffers::EndianScalar(_b)), b_(flatbuffers::EndianScalar(_b)),
...@@ -237,9 +234,6 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS { ...@@ -237,9 +234,6 @@ MANUALLY_ALIGNED_STRUCT(16) Vec3 FLATBUFFERS_FINAL_CLASS {
Vec3() { Vec3() {
memset(this, 0, sizeof(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) Vec3(float _x, float _y, float _z, double _test1, Color _test2, const Test &_test3)
: x_(flatbuffers::EndianScalar(_x)), : x_(flatbuffers::EndianScalar(_x)),
y_(flatbuffers::EndianScalar(_y)), y_(flatbuffers::EndianScalar(_y)),
...@@ -302,9 +296,6 @@ MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS { ...@@ -302,9 +296,6 @@ MANUALLY_ALIGNED_STRUCT(4) Ability FLATBUFFERS_FINAL_CLASS {
Ability() { Ability() {
memset(this, 0, sizeof(Ability)); memset(this, 0, sizeof(Ability));
} }
Ability(const Ability &_o) {
memcpy(this, &_o, sizeof(Ability));
}
Ability(uint32_t _id, uint32_t _distance) Ability(uint32_t _id, uint32_t _distance)
: id_(flatbuffers::EndianScalar(_id)), : id_(flatbuffers::EndianScalar(_id)),
distance_(flatbuffers::EndianScalar(_distance)) { distance_(flatbuffers::EndianScalar(_distance)) {
......
...@@ -526,6 +526,18 @@ void SizePrefixedTest() { ...@@ -526,6 +526,18 @@ void SizePrefixedTest() {
TEST_EQ_STR(m->name()->c_str(), "bob"); 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<Vec3>::value, true);
#endif
#endif
}
// example of parsing text straight into a buffer, and generating // example of parsing text straight into a buffer, and generating
// text back from it: // text back from it:
void ParseAndGenerateTextTest() { void ParseAndGenerateTextTest() {
...@@ -1809,6 +1821,9 @@ int main(int /*argc*/, const char * /*argv*/[]) { ...@@ -1809,6 +1821,9 @@ int main(int /*argc*/, const char * /*argv*/[]) {
#else #else
auto &flatbuf = flatbuf1; auto &flatbuf = flatbuf1;
#endif // !defined(FLATBUFFERS_CPP98_STL) #endif // !defined(FLATBUFFERS_CPP98_STL)
TriviallyCopyableTest();
AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()), AccessFlatBufferTest(reinterpret_cast<const uint8_t *>(rawbuf.c_str()),
rawbuf.length()); rawbuf.length());
AccessFlatBufferTest(flatbuf.data(), flatbuf.size()); AccessFlatBufferTest(flatbuf.data(), flatbuf.size());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment