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

@@ -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: {