ladybird/AK/DefaultDelete.h
Lenny Maiorani f2336d0144 AK+Everywhere: Move custom deleter capability to OwnPtr
`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.
2022-12-17 16:00:08 -05:00

36 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