Improved union copy constructor.

It now at least works in simple cases.

Change-Id: I3af0738e676e62166b69accaa6bd19f531fbe5ee
Tested: on Linux.
This commit is contained in:
Wouter van Oortmerssen
2017-04-17 17:27:20 -07:00
parent 728bb64fed
commit e093f72d00
4 changed files with 116 additions and 10 deletions

View File

@@ -108,8 +108,9 @@ struct AnyUnion {
AnyUnion(AnyUnion&& u) FLATBUFFERS_NOEXCEPT :
type(Any_NONE), value(nullptr)
{ std::swap(type, u.type); std::swap(value, u.value); }
AnyUnion(const AnyUnion &) { assert(false); }
AnyUnion &operator=(const AnyUnion &) { assert(false); return *this; }
AnyUnion(const AnyUnion &) FLATBUFFERS_NOEXCEPT;
AnyUnion &operator=(AnyUnion u) FLATBUFFERS_NOEXCEPT
{ std::swap(type, u.type); std::swap(value, u.value); return *this; }
AnyUnion &operator=(AnyUnion &&u) FLATBUFFERS_NOEXCEPT
{ std::swap(type, u.type); std::swap(value, u.value); return *this; }
~AnyUnion() { Reset(); }
@@ -1333,6 +1334,25 @@ inline flatbuffers::Offset<void> AnyUnion::Pack(flatbuffers::FlatBufferBuilder &
}
}
inline AnyUnion::AnyUnion(const AnyUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) {
switch (type) {
case Any_Monster: {
assert(false); // MonsterT not copyable.
break;
}
case Any_TestSimpleTableWithEnum: {
value = new TestSimpleTableWithEnumT(*reinterpret_cast<TestSimpleTableWithEnumT *>(u.value));
break;
}
case Any_MyGame_Example2_Monster: {
value = new MyGame::Example2::MonsterT(*reinterpret_cast<MyGame::Example2::MonsterT *>(u.value));
break;
}
default:
break;
}
}
inline void AnyUnion::Reset() {
switch (type) {
case Any_Monster: {

View File

@@ -55,8 +55,9 @@ struct CharacterUnion {
CharacterUnion(CharacterUnion&& u) FLATBUFFERS_NOEXCEPT :
type(Character_NONE), value(nullptr)
{ std::swap(type, u.type); std::swap(value, u.value); }
CharacterUnion(const CharacterUnion &) { assert(false); }
CharacterUnion &operator=(const CharacterUnion &) { assert(false); return *this; }
CharacterUnion(const CharacterUnion &) FLATBUFFERS_NOEXCEPT;
CharacterUnion &operator=(CharacterUnion u) FLATBUFFERS_NOEXCEPT
{ std::swap(type, u.type); std::swap(value, u.value); return *this; }
CharacterUnion &operator=(CharacterUnion &&u) FLATBUFFERS_NOEXCEPT
{ std::swap(type, u.type); std::swap(value, u.value); return *this; }
~CharacterUnion() { Reset(); }
@@ -491,6 +492,37 @@ inline flatbuffers::Offset<void> CharacterUnion::Pack(flatbuffers::FlatBufferBui
}
}
inline CharacterUnion::CharacterUnion(const CharacterUnion &u) FLATBUFFERS_NOEXCEPT : type(u.type), value(nullptr) {
switch (type) {
case Character_MuLan: {
value = new AttackerT(*reinterpret_cast<AttackerT *>(u.value));
break;
}
case Character_Rapunzel: {
value = new Rapunzel(*reinterpret_cast<Rapunzel *>(u.value));
break;
}
case Character_Belle: {
value = new BookReader(*reinterpret_cast<BookReader *>(u.value));
break;
}
case Character_BookFan: {
value = new BookReader(*reinterpret_cast<BookReader *>(u.value));
break;
}
case Character_Other: {
value = new std::string(*reinterpret_cast<std::string *>(u.value));
break;
}
case Character_Unused: {
value = new std::string(*reinterpret_cast<std::string *>(u.value));
break;
}
default:
break;
}
}
inline void CharacterUnion::Reset() {
switch (type) {
case Character_MuLan: {