Add ::Set function to Unions to make memory ownership clear.

Unions own the NativeTable* value member because they need to destroy them
when the Union goes out of scope.  Currently, the data is destroyed by calling
delete, which means that the member needs to be allocated with new.  However,
making the allocation the responsibility of the client and the destruction
the responsibility of the Union can lead to potential errors.  Adding a
Set function will ensure that the memory is allocated correctly so that it
can be deleted later.

From cl/142161569.

Change-Id: I4605f26d2749164819bfae0140e5fae08442b50a
This commit is contained in:
Wouter van Oortmerssen
2016-12-19 15:21:08 -08:00
parent d1e8899310
commit c66683f27f
3 changed files with 112 additions and 69 deletions

View File

@@ -38,21 +38,6 @@ enum Equipment {
Equipment_MAX = Equipment_Weapon
};
struct EquipmentUnion {
Equipment type;
flatbuffers::NativeTable *table;
EquipmentUnion() : type(Equipment_NONE), table(nullptr) {}
EquipmentUnion(const EquipmentUnion &);
EquipmentUnion &operator=(const EquipmentUnion &);
~EquipmentUnion();
static flatbuffers::NativeTable *UnPack(const void *union_obj, Equipment type, const flatbuffers::resolver_function_t *resolver);
flatbuffers::Offset<void> Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *rehasher = nullptr) const;
WeaponT *AsWeapon() { return type == Equipment_Weapon ? reinterpret_cast<WeaponT *>(table) : nullptr; }
};
inline const char **EnumNamesEquipment() {
static const char *names[] = { "NONE", "Weapon", nullptr };
return names;
@@ -68,6 +53,31 @@ template<> struct EquipmentTraits<Weapon> {
static const Equipment enum_value = Equipment_Weapon;
};
struct EquipmentUnion {
Equipment type = Equipment_NONE;
flatbuffers::NativeTable *table = nullptr;
EquipmentUnion() : type(Equipment_NONE), table(nullptr) {}
EquipmentUnion(const EquipmentUnion &);
EquipmentUnion &operator=(const EquipmentUnion &);
~EquipmentUnion() { Reset(); }
void Reset();
template <typename T>
void Set(T&& value) {
Reset();
type = EquipmentTraits<typename T::TableType>::enum_value;
if (type != Equipment_NONE) {
table = new T(std::move(value));
}
}
static flatbuffers::NativeTable *UnPack(const void *union_obj, Equipment type, const flatbuffers::resolver_function_t *resolver);
flatbuffers::Offset<void> Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *rehasher = nullptr) const;
WeaponT *AsWeapon() { return type == Equipment_Weapon ? reinterpret_cast<WeaponT *>(table) : nullptr; }
};
inline bool VerifyEquipment(flatbuffers::Verifier &verifier, const void *union_obj, Equipment type);
MANUALLY_ALIGNED_STRUCT(4) Vec3 FLATBUFFERS_FINAL_CLASS {
@@ -347,11 +357,13 @@ inline flatbuffers::Offset<void> EquipmentUnion::Pack(flatbuffers::FlatBufferBui
}
}
inline EquipmentUnion::~EquipmentUnion() {
inline void EquipmentUnion::Reset() {
switch (type) {
case Equipment_Weapon: delete reinterpret_cast<WeaponT *>(table); break;
default:;
default: break;
}
table = nullptr;
type = Equipment_NONE;
}
inline const MyGame::Sample::Monster *GetMonster(const void *buf) {