mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
f2336d0144
`OwnPtrWithCustomDeleter` was a decorator which provided the ability to add a custom deleter to `OwnPtr` by wrapping and taking the deleter as a run-time argument to the constructor. This solution means that no additional space is needed for the `OwnPtr` because it doesn't need to store a pointer to the deleter, but comes at the cost of having an extra type that stores a pointer for every instance. This logic is moved directly into `OwnPtr` by adding a template argument that is defaulted to the default deleter for the type. This means that the type itself stores the pointer to the deleter instead of every instance and adds some type safety by encoding the deleter in the type itself instead of taking a run-time argument.
23 lines
554 B
C++
23 lines
554 B
C++
/*
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibTest/TestCase.h>
|
|
|
|
#include <AK/OwnPtr.h>
|
|
|
|
static u64 deleter_call_count = 0;
|
|
|
|
TEST_CASE(should_call_custom_deleter)
|
|
{
|
|
auto deleter = [](auto* p) { if (p) ++deleter_call_count; };
|
|
auto ptr = OwnPtr<u64, decltype(deleter)> {};
|
|
ptr.clear();
|
|
EXPECT_EQ(0u, deleter_call_count);
|
|
ptr = adopt_own_if_nonnull(&deleter_call_count);
|
|
EXPECT_EQ(0u, deleter_call_count);
|
|
ptr.clear();
|
|
EXPECT_EQ(1u, deleter_call_count);
|
|
}
|