mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-05 07:41:01 +00:00
AK: Destroy original value when assigning to Variant
This commit is contained in:
parent
515e2d9734
commit
39dd13fd17
Notes:
sideshowbarker
2024-07-18 10:31:06 +09:00
Author: https://github.com/BertalanD
Commit: 39dd13fd17
Pull-request: https://github.com/SerenityOS/serenity/pull/8391
Reviewed-by: https://github.com/alimpfard
2 changed files with 12 additions and 0 deletions
|
@ -266,6 +266,9 @@ public:
|
||||||
requires(!(IsTriviallyCopyConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
|
requires(!(IsTriviallyCopyConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
if constexpr (!(IsTriviallyDestructible<Ts> && ...)) {
|
||||||
|
Helper::delete_(m_index, m_data);
|
||||||
|
}
|
||||||
m_index = other.m_index;
|
m_index = other.m_index;
|
||||||
Helper::copy_(other.m_index, other.m_data, m_data);
|
Helper::copy_(other.m_index, other.m_data, m_data);
|
||||||
return *this;
|
return *this;
|
||||||
|
@ -276,6 +279,9 @@ public:
|
||||||
requires(!(IsTriviallyMoveConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
|
requires(!(IsTriviallyMoveConstructible<Ts> && ...) || !(IsTriviallyDestructible<Ts> && ...))
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
|
if constexpr (!(IsTriviallyDestructible<Ts> && ...)) {
|
||||||
|
Helper::delete_(m_index, m_data);
|
||||||
|
}
|
||||||
m_index = other.m_index;
|
m_index = other.m_index;
|
||||||
Helper::move_(other.m_index, other.m_data, m_data);
|
Helper::move_(other.m_index, other.m_data, m_data);
|
||||||
return *this;
|
return *this;
|
||||||
|
|
|
@ -57,6 +57,12 @@ TEST_CASE(destructor)
|
||||||
Variant<DestructionChecker> test_variant { DestructionChecker { was_destroyed } };
|
Variant<DestructionChecker> test_variant { DestructionChecker { was_destroyed } };
|
||||||
}
|
}
|
||||||
EXPECT(was_destroyed);
|
EXPECT(was_destroyed);
|
||||||
|
|
||||||
|
bool was_destroyed_when_assigned_to = false;
|
||||||
|
Variant<DestructionChecker, int> original { DestructionChecker { was_destroyed_when_assigned_to } };
|
||||||
|
Variant<DestructionChecker, int> other { 42 };
|
||||||
|
original = other;
|
||||||
|
EXPECT(was_destroyed_when_assigned_to);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE(move_moves)
|
TEST_CASE(move_moves)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue