2020-01-18 11:38:21 +03:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
*
2021-04-22 11:24:48 +03:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 11:38:21 +03:00
*/
2018-10-10 12:53:07 +03:00
# pragma once
2021-11-08 02:51:39 +03:00
# include <AK/Error.h>
2022-12-16 06:51:55 +03:00
# include <AK/Forward.h>
2019-07-24 09:25:27 +03:00
# include <AK/NonnullOwnPtr.h>
2020-06-12 16:30:30 +03:00
# include <AK/RefCounted.h>
2018-10-10 12:53:07 +03:00
2021-10-07 22:10:56 +03:00
# define OWNPTR_SCRUB_BYTE 0xf0
2018-10-10 12:53:07 +03:00
namespace AK {
2022-12-16 06:51:55 +03:00
template < typename T , typename TDeleter >
2021-12-02 00:05:13 +03:00
class [ [ nodiscard ] ] OwnPtr {
2018-10-10 12:53:07 +03:00
public :
2021-01-11 02:29:28 +03:00
OwnPtr ( ) = default ;
2021-05-30 19:39:23 +03:00
OwnPtr ( decltype ( nullptr ) )
: m_ptr ( nullptr )
2019-05-28 12:53:16 +03:00
{
}
2021-05-30 19:39:23 +03:00
2019-05-28 12:53:16 +03:00
OwnPtr ( OwnPtr & & other )
: m_ptr ( other . leak_ptr ( ) )
{
}
2019-07-24 09:25:27 +03:00
template < typename U >
OwnPtr ( NonnullOwnPtr < U > & & other )
2020-04-05 12:32:30 +03:00
: m_ptr ( other . leak_ptr ( ) )
2019-07-24 09:25:27 +03:00
{
}
2019-05-28 12:53:16 +03:00
template < typename U >
OwnPtr ( OwnPtr < U > & & other )
2020-04-05 12:32:30 +03:00
: m_ptr ( other . leak_ptr ( ) )
2019-05-28 12:53:16 +03:00
{
}
2018-10-16 13:20:51 +03:00
~ OwnPtr ( )
{
clear ( ) ;
# ifdef SANITIZE_PTRS
2021-10-07 22:10:56 +03:00
m_ptr = ( T * ) ( explode_byte ( OWNPTR_SCRUB_BYTE ) ) ;
2018-10-16 13:20:51 +03:00
# endif
}
2018-10-10 12:53:07 +03:00
2022-04-01 20:58:27 +03:00
OwnPtr ( OwnPtr const & ) = delete ;
2019-07-24 09:25:27 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr ( OwnPtr < U > const & ) = delete ;
OwnPtr & operator = ( OwnPtr const & ) = delete ;
2019-07-24 09:25:27 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr & operator = ( OwnPtr < U > const & ) = delete ;
2019-07-24 09:25:27 +03:00
2019-08-01 16:46:18 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr ( NonnullOwnPtr < U > const & ) = delete ;
2019-08-01 16:46:18 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr & operator = ( NonnullOwnPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr ( RefPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr ( NonnullRefPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr ( WeakPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr & operator = ( RefPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr & operator = ( NonnullRefPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
template < typename U >
2022-04-01 20:58:27 +03:00
OwnPtr & operator = ( WeakPtr < U > const & ) = delete ;
2019-07-11 17:43:20 +03:00
2018-10-10 12:53:07 +03:00
OwnPtr & operator = ( OwnPtr & & other )
{
2020-01-24 11:31:14 +03:00
OwnPtr ptr ( move ( other ) ) ;
swap ( ptr ) ;
2018-10-10 12:53:07 +03:00
return * this ;
}
template < typename U >
OwnPtr & operator = ( OwnPtr < U > & & other )
{
2020-01-24 11:31:14 +03:00
OwnPtr ptr ( move ( other ) ) ;
swap ( ptr ) ;
2018-10-10 12:53:07 +03:00
return * this ;
}
2019-07-24 09:25:27 +03:00
template < typename U >
OwnPtr & operator = ( NonnullOwnPtr < U > & & other )
{
2020-01-24 11:31:14 +03:00
OwnPtr ptr ( move ( other ) ) ;
swap ( ptr ) ;
2021-02-23 22:42:32 +03:00
VERIFY ( m_ptr ) ;
2019-07-24 09:25:27 +03:00
return * this ;
}
2021-05-30 19:39:23 +03:00
OwnPtr & operator = ( T * ptr ) = delete ;
2018-10-10 12:53:07 +03:00
2022-12-13 09:59:30 +03:00
OwnPtr & operator = ( nullptr_t )
2018-10-10 12:53:07 +03:00
{
clear ( ) ;
return * this ;
}
void clear ( )
{
2023-04-21 14:36:32 +03:00
auto * ptr = exchange ( m_ptr , nullptr ) ;
TDeleter { } ( ptr ) ;
2018-10-10 12:53:07 +03:00
}
bool operator ! ( ) const { return ! m_ptr ; }
2020-08-05 16:40:03 +03:00
[ [ nodiscard ] ] T * leak_ptr ( )
2018-10-10 12:53:07 +03:00
{
2019-07-24 09:25:27 +03:00
T * leaked_ptr = m_ptr ;
2018-10-10 12:53:07 +03:00
m_ptr = nullptr ;
2019-07-24 09:25:27 +03:00
return leaked_ptr ;
2018-10-10 12:53:07 +03:00
}
2019-08-14 12:02:49 +03:00
NonnullOwnPtr < T > release_nonnull ( )
{
2021-02-23 22:42:32 +03:00
VERIFY ( m_ptr ) ;
2019-08-14 12:02:49 +03:00
return NonnullOwnPtr < T > ( NonnullOwnPtr < T > : : Adopt , * leak_ptr ( ) ) ;
}
2020-04-05 12:32:30 +03:00
template < typename U >
NonnullOwnPtr < U > release_nonnull ( )
{
2021-02-23 22:42:32 +03:00
VERIFY ( m_ptr ) ;
2020-04-05 12:32:30 +03:00
return NonnullOwnPtr < U > ( NonnullOwnPtr < U > : : Adopt , static_cast < U & > ( * leak_ptr ( ) ) ) ;
}
2022-11-19 04:03:48 +03:00
T * ptr ( ) const { return m_ptr ; }
2018-10-10 12:53:07 +03:00
2022-11-19 04:03:48 +03:00
T * operator - > ( ) const
2019-08-02 11:34:40 +03:00
{
2021-02-23 22:42:32 +03:00
VERIFY ( m_ptr ) ;
2019-08-02 11:34:40 +03:00
return m_ptr ;
}
2022-11-19 04:03:48 +03:00
T & operator * ( ) const
2019-08-02 11:34:40 +03:00
{
2021-02-23 22:42:32 +03:00
VERIFY ( m_ptr ) ;
2019-08-02 11:34:40 +03:00
return * m_ptr ;
}
2018-10-10 12:53:07 +03:00
2022-11-19 04:03:48 +03:00
operator T * ( ) const { return m_ptr ; }
2019-04-14 03:36:06 +03:00
2018-10-10 12:53:07 +03:00
operator bool ( ) { return ! ! m_ptr ; }
2020-01-24 11:31:14 +03:00
void swap ( OwnPtr & other )
{
2023-04-28 04:01:15 +03:00
AK : : swap ( m_ptr , other . m_ptr ) ;
2020-01-24 11:31:14 +03:00
}
template < typename U >
void swap ( OwnPtr < U > & other )
{
2023-04-28 04:01:15 +03:00
AK : : swap ( m_ptr , other . m_ptr ) ;
2020-01-24 11:31:14 +03:00
}
2021-05-30 19:39:23 +03:00
static OwnPtr lift ( T * ptr )
{
return OwnPtr { ptr } ;
}
protected :
explicit OwnPtr ( T * ptr )
: m_ptr ( ptr )
{
static_assert (
2021-06-25 08:33:15 +03:00
requires { requires typename T : : AllowOwnPtr ( ) ( ) ; } | | ! requires { requires ! typename T : : AllowOwnPtr ( ) ( ) ; declval < T > ( ) . ref ( ) ; declval < T > ( ) . unref ( ) ; } , " Use RefPtr<> for RefCounted types " ) ;
2021-05-30 19:39:23 +03:00
}
2018-10-10 12:53:07 +03:00
private :
T * m_ptr = nullptr ;
} ;
2020-01-24 11:31:14 +03:00
template < typename T , typename U >
inline void swap ( OwnPtr < T > & a , OwnPtr < U > & b )
{
a . swap ( b ) ;
}
2021-05-13 06:35:08 +03:00
template < typename T >
inline OwnPtr < T > adopt_own_if_nonnull ( T * object )
{
if ( object )
2021-05-30 19:39:23 +03:00
return OwnPtr < T > : : lift ( object ) ;
2021-05-13 06:35:08 +03:00
return { } ;
}
2018-10-26 18:42:12 +03:00
template < typename T >
2019-06-29 20:14:03 +03:00
struct Traits < OwnPtr < T > > : public GenericTraits < OwnPtr < T > > {
2021-05-08 12:11:37 +03:00
using PeekType = T * ;
2022-10-17 01:06:11 +03:00
using ConstPeekType = T const * ;
2022-04-01 20:58:27 +03:00
static unsigned hash ( OwnPtr < T > const & p ) { return ptr_hash ( p . ptr ( ) ) ; }
static bool equals ( OwnPtr < T > const & a , OwnPtr < T > const & b ) { return a . ptr ( ) = = b . ptr ( ) ; }
2018-10-26 18:42:12 +03:00
} ;
2018-10-10 12:53:07 +03:00
}
2022-11-26 14:18:30 +03:00
# if USING_AK_GLOBALLY
2021-05-13 06:35:08 +03:00
using AK : : adopt_own_if_nonnull ;
2019-05-28 12:53:16 +03:00
using AK : : OwnPtr ;
2022-11-26 14:18:30 +03:00
# endif