diff options
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; |