summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorWouter van Oortmerssen <aardappel@gmail.com>2016-12-19 15:21:08 -0800
committerWouter van Oortmerssen <aardappel@gmail.com>2016-12-19 15:21:08 -0800
commitc66683f27fbb4a6af70d3ff708e2c7f37cee0c60 (patch)
treecf284c97fb783c1eb7e6b1ca37cd367a9c8fdb1b /tests
parentd1e8899310d8e8758d9900628de448a3a43bd050 (diff)
downloadflatbuffers-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 'tests')
-rw-r--r--tests/monster_test_generated.h50
1 files changed, 31 insertions, 19 deletions
diff --git a/tests/monster_test_generated.h b/tests/monster_test_generated.h
index f1be4ace..8a2a111e 100644
--- a/tests/monster_test_generated.h
+++ b/tests/monster_test_generated.h
@@ -52,23 +52,6 @@ enum Any {
Any_MAX = Any_MyGame_Example2_Monster
};
-struct AnyUnion {
- Any type;
-
- flatbuffers::NativeTable *table;
- AnyUnion() : type(Any_NONE), table(nullptr) {}
- AnyUnion(const AnyUnion &);
- AnyUnion &operator=(const AnyUnion &);
- ~AnyUnion();
-
- static flatbuffers::NativeTable *UnPack(const void *union_obj, Any type, const flatbuffers::resolver_function_t *resolver);
- flatbuffers::Offset<void> Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *rehasher = nullptr) const;
-
- MonsterT *AsMonster() { return type == Any_Monster ? reinterpret_cast<MonsterT *>(table) : nullptr; }
- TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() { return type == Any_TestSimpleTableWithEnum ? reinterpret_cast<TestSimpleTableWithEnumT *>(table) : nullptr; }
- MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() { return type == Any_MyGame_Example2_Monster ? reinterpret_cast<MyGame::Example2::MonsterT *>(table) : nullptr; }
-};
-
inline const char **EnumNamesAny() {
static const char *names[] = { "NONE", "Monster", "TestSimpleTableWithEnum", "MyGame_Example2_Monster", nullptr };
return names;
@@ -92,6 +75,33 @@ template<> struct AnyTraits<MyGame::Example2::Monster> {
static const Any enum_value = Any_MyGame_Example2_Monster;
};
+struct AnyUnion {
+ Any type = Any_NONE;
+
+ flatbuffers::NativeTable *table = nullptr;
+ AnyUnion() : type(Any_NONE), table(nullptr) {}
+ AnyUnion(const AnyUnion &);
+ AnyUnion &operator=(const AnyUnion &);
+ ~AnyUnion() { Reset(); }
+ void Reset();
+
+ template <typename T>
+ void Set(T&& value) {
+ Reset();
+ type = AnyTraits<typename T::TableType>::enum_value;
+ if (type != Any_NONE) {
+ table = new T(std::move(value));
+ }
+ }
+
+ static flatbuffers::NativeTable *UnPack(const void *union_obj, Any type, const flatbuffers::resolver_function_t *resolver);
+ flatbuffers::Offset<void> Pack(flatbuffers::FlatBufferBuilder &_fbb, const flatbuffers::rehasher_function_t *rehasher = nullptr) const;
+
+ MonsterT *AsMonster() { return type == Any_Monster ? reinterpret_cast<MonsterT *>(table) : nullptr; }
+ TestSimpleTableWithEnumT *AsTestSimpleTableWithEnum() { return type == Any_TestSimpleTableWithEnum ? reinterpret_cast<TestSimpleTableWithEnumT *>(table) : nullptr; }
+ MyGame::Example2::MonsterT *AsMyGame_Example2_Monster() { return type == Any_MyGame_Example2_Monster ? reinterpret_cast<MyGame::Example2::MonsterT *>(table) : nullptr; }
+};
+
inline bool VerifyAny(flatbuffers::Verifier &verifier, const void *union_obj, Any type);
MANUALLY_ALIGNED_STRUCT(2) Test FLATBUFFERS_FINAL_CLASS {
@@ -767,13 +777,15 @@ inline flatbuffers::Offset<void> AnyUnion::Pack(flatbuffers::FlatBufferBuilder &
}
}
-inline AnyUnion::~AnyUnion() {
+inline void AnyUnion::Reset() {
switch (type) {
case Any_Monster: delete reinterpret_cast<MonsterT *>(table); break;
case Any_TestSimpleTableWithEnum: delete reinterpret_cast<TestSimpleTableWithEnumT *>(table); break;
case Any_MyGame_Example2_Monster: delete reinterpret_cast<MyGame::Example2::MonsterT *>(table); break;
- default:;
+ default: break;
}
+ table = nullptr;
+ type = Any_NONE;
}
inline const MyGame::Example::Monster *GetMonster(const void *buf) {