diff options
author | Wouter van Oortmerssen <aardappel@gmail.com> | 2016-12-19 15:21:08 -0800 |
---|---|---|
committer | Wouter van Oortmerssen <aardappel@gmail.com> | 2016-12-19 15:21:08 -0800 |
commit | c66683f27fbb4a6af70d3ff708e2c7f37cee0c60 (patch) | |
tree | cf284c97fb783c1eb7e6b1ca37cd367a9c8fdb1b /samples | |
parent | d1e8899310d8e8758d9900628de448a3a43bd050 (diff) | |
download | flatbuffers-c66683f27fbb4a6af70d3ff708e2c7f37cee0c60.tar.gz flatbuffers-c66683f27fbb4a6af70d3ff708e2c7f37cee0c60.tar.bz2 flatbuffers-c66683f27fbb4a6af70d3ff708e2c7f37cee0c60.zip |
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
Diffstat (limited to 'samples')
-rw-r--r-- | samples/monster_generated.h | 46 |
1 files changed, 29 insertions, 17 deletions
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<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) { |