diff options
author | Lenny Maiorani <lenny@serenityos.org> | 2022-12-15 20:51:55 -0700 |
---|---|---|
committer | Tim Flynn <trflynn89@pm.me> | 2022-12-17 16:00:08 -0500 |
commit | f2336d0144fbbf6d72bd1eeaa34667dc4eb76760 (patch) | |
tree | a1f1c53b9257a11baf2f2e382ddf2f6bd9b2c27e /AK | |
parent | 53133b4359caf5a8cc5f30d1313759ea4a017870 (diff) | |
download | serenity-f2336d0144fbbf6d72bd1eeaa34667dc4eb76760.zip |
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.
Diffstat (limited to 'AK')
-rw-r--r-- | AK/DefaultDelete.h | 35 | ||||
-rw-r--r-- | AK/Forward.h | 3 | ||||
-rw-r--r-- | AK/NonnullRefPtr.h | 2 | ||||
-rw-r--r-- | AK/OwnPtr.h | 5 | ||||
-rw-r--r-- | AK/OwnPtrWithCustomDeleter.h | 41 | ||||
-rw-r--r-- | AK/RefPtr.h | 4 |
6 files changed, 41 insertions, 49 deletions
diff --git a/AK/DefaultDelete.h b/AK/DefaultDelete.h new file mode 100644 index 0000000000..be13a1018c --- /dev/null +++ b/AK/DefaultDelete.h @@ -0,0 +1,35 @@ +/* + * 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 diff --git a/AK/Forward.h b/AK/Forward.h index 86b288a318..c92787928b 100644 --- a/AK/Forward.h +++ b/AK/Forward.h @@ -6,6 +6,7 @@ #pragma once +#include <AK/DefaultDelete.h> #include <AK/Types.h> namespace AK { @@ -133,7 +134,7 @@ class LockRefPtr; template<typename T> class RefPtr; -template<typename T> +template<typename T, typename TDeleter = DefaultDelete<T>> class OwnPtr; template<typename T> diff --git a/AK/NonnullRefPtr.h b/AK/NonnullRefPtr.h index 2a1a0d5e06..7f3a054dd1 100644 --- a/AK/NonnullRefPtr.h +++ b/AK/NonnullRefPtr.h @@ -17,8 +17,6 @@ namespace AK { template<typename T> -class OwnPtr; -template<typename T> class RefPtr; template<typename T> diff --git a/AK/OwnPtr.h b/AK/OwnPtr.h index ece4c34391..c5452b8661 100644 --- a/AK/OwnPtr.h +++ b/AK/OwnPtr.h @@ -7,6 +7,7 @@ #pragma once #include <AK/Error.h> +#include <AK/Forward.h> #include <AK/NonnullOwnPtr.h> #include <AK/RefCounted.h> @@ -14,7 +15,7 @@ namespace AK { -template<typename T> +template<typename T, typename TDeleter> class [[nodiscard]] OwnPtr { public: OwnPtr() = default; @@ -105,7 +106,7 @@ public: void clear() { - delete m_ptr; + TDeleter {}(m_ptr); m_ptr = nullptr; } diff --git a/AK/OwnPtrWithCustomDeleter.h b/AK/OwnPtrWithCustomDeleter.h deleted file mode 100644 index f9968810ed..0000000000 --- a/AK/OwnPtrWithCustomDeleter.h +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright (c) 2022, Lucas Chollet <lucas.chollet@free.fr> - * - * SPDX-License-Identifier: BSD-2-Clause - */ - -#pragma once - -#include <AK/Function.h> -#include <AK/Noncopyable.h> -#include <AK/StdLibExtras.h> - -template<typename T> -struct OwnPtrWithCustomDeleter { - AK_MAKE_NONCOPYABLE(OwnPtrWithCustomDeleter); - -public: - OwnPtrWithCustomDeleter(T* ptr, Function<void(T*)> deleter) - : m_ptr(ptr) - , m_deleter(move(deleter)) - { - } - - OwnPtrWithCustomDeleter(OwnPtrWithCustomDeleter&& other) - { - swap(m_ptr, other.m_ptr); - swap(m_deleter, other.m_deleter); - }; - - ~OwnPtrWithCustomDeleter() - { - if (m_ptr) { - VERIFY(m_deleter); - m_deleter(m_ptr); - } - } - -private: - T* m_ptr { nullptr }; - Function<void(T*)> m_deleter {}; -}; diff --git a/AK/RefPtr.h b/AK/RefPtr.h index 7619017347..9586e9c11e 100644 --- a/AK/RefPtr.h +++ b/AK/RefPtr.h @@ -12,6 +12,7 @@ #include <AK/Atomic.h> #include <AK/Error.h> #include <AK/Format.h> +#include <AK/Forward.h> #include <AK/NonnullRefPtr.h> #include <AK/StdLibExtras.h> #include <AK/Traits.h> @@ -20,9 +21,6 @@ namespace AK { template<typename T> -class OwnPtr; - -template<typename T> class [[nodiscard]] RefPtr { template<typename U> friend class RefPtr; |