From c66683f27fbb4a6af70d3ff708e2c7f37cee0c60 Mon Sep 17 00:00:00 2001 From: Wouter van Oortmerssen Date: Mon, 19 Dec 2016 15:21:08 -0800 Subject: 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 --- samples/monster_generated.h | 46 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) (limited to 'samples') diff --git a/samples/monster_generated.h b/samples/monster_generated.h index 1833c62d..2504e830 100644 --- a/samples/monster_generated.h +++ b/samples/monster_generated.h @@ -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 Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *rehasher = nullptr) const; - - WeaponT *AsWeapon() { return type == Equipment_Weapon ? reinterpret_cast(table) : nullptr; } -}; - inline const char **EnumNamesEquipment() { static const char *names[] = { "NONE", "Weapon", nullptr }; return names; @@ -68,6 +53,31 @@ template<> struct EquipmentTraits { 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 + void Set(T&& value) { + Reset(); + type = EquipmentTraits::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 Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *rehasher = nullptr) const; + + WeaponT *AsWeapon() { return type == Equipment_Weapon ? reinterpret_cast(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 EquipmentUnion::Pack(flatbuffers::FlatBufferBui } } -inline EquipmentUnion::~EquipmentUnion() { +inline void EquipmentUnion::Reset() { switch (type) { case Equipment_Weapon: delete reinterpret_cast(table); break; - default:; + default: break; } + table = nullptr; + type = Equipment_NONE; } inline const MyGame::Sample::Monster *GetMonster(const void *buf) { -- cgit v1.2.3