mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-27 23:58:05 +00:00
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.
This commit is contained in:
committed by
Wouter van Oortmerssen
parent
e2c7196ea8
commit
853f7033e0
@@ -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 {
|
|||||||
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 {
|
|||||||
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 {
|
|||||||
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() {
|
|||||||
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*/[]) {
|
|||||||
#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());
|
||||||
|
|||||||
Reference in New Issue
Block a user