mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 15:40: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.
35 lines
505 B
C++
35 lines
505 B
C++
/*
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
namespace AK {
|
|
|
|
template<class T>
|
|
struct DefaultDelete {
|
|
constexpr DefaultDelete() = default;
|
|
|
|
constexpr void operator()(T* t)
|
|
{
|
|
delete t;
|
|
}
|
|
};
|
|
|
|
template<class T>
|
|
struct DefaultDelete<T[]> {
|
|
constexpr DefaultDelete() = default;
|
|
|
|
constexpr void operator()(T* t)
|
|
{
|
|
delete[] t;
|
|
}
|
|
};
|
|
|
|
}
|
|
|
|
#ifdef USING_AK_GLOBALLY
|
|
using AK::DefaultDelete;
|
|
#endif
|