2020-01-18 03:38:21 -05:00
/*
* Copyright ( c ) 2018 - 2020 , Andreas Kling < kling @ serenityos . org >
*
2021-04-22 04:24:48 -04:00
* SPDX - License - Identifier : BSD - 2 - Clause
2020-01-18 03:38:21 -05:00
*/
2018-10-10 05:53:07 -04:00
# pragma once
2021-11-07 18:51:39 -05:00
# include <AK/Error.h>
2019-07-24 02:25:27 -04:00
# include <AK/NonnullOwnPtr.h>
2020-06-12 09:30:30 -04:00
# include <AK/RefCounted.h>
2018-10-10 05:53:07 -04:00
2021-10-07 15:10:56 -04:00
# define OWNPTR_SCRUB_BYTE 0xf0
2018-10-10 05:53:07 -04:00
namespace AK {
template < typename T >
2021-12-01 16:05:13 -05:00
class [ [ nodiscard ] ] OwnPtr {
2018-10-10 05:53:07 -04:00
public :
2021-01-10 18:29:28 -05:00
OwnPtr ( ) = default ;
2021-05-30 12:39:23 -04:00
OwnPtr ( decltype ( nullptr ) )
: m_ptr ( nullptr )
2019-05-28 05:53:16 -04:00
{
}
2021-05-30 12:39:23 -04:00
2019-05-28 05:53:16 -04:00
OwnPtr ( OwnPtr & & other )
: m_ptr ( other . leak_ptr ( ) )
{
}
2019-07-24 02:25:27 -04:00
template < typename U >
OwnPtr ( NonnullOwnPtr < U > & & other )
2020-04-05 05:32:30 -04:00
: m_ptr ( other . leak_ptr ( ) )
2019-07-24 02:25:27 -04:00
{
}
2019-05-28 05:53:16 -04:00
template < typename U >
OwnPtr ( OwnPtr < U > & & other )
2020-04-05 05:32:30 -04:00
: m_ptr ( other . leak_ptr ( ) )
2019-05-28 05:53:16 -04:00
{
}
2018-10-16 06:20:51 -04:00
~ OwnPtr ( )
{
clear ( ) ;
# ifdef SANITIZE_PTRS
2021-10-07 15:10:56 -04:00
m_ptr = ( T * ) ( explode_byte ( OWNPTR_SCRUB_BYTE ) ) ;
2018-10-16 06:20:51 -04:00
# endif
}
2018-10-10 05:53:07 -04:00
2022-04-01 13:58:27 -04:00
OwnPtr ( OwnPtr const & ) = delete ;
2019-07-24 02:25:27 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr ( OwnPtr < U > const & ) = delete ;
OwnPtr & operator = ( OwnPtr const & ) = delete ;
2019-07-24 02:25:27 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr & operator = ( OwnPtr < U > const & ) = delete ;
2019-07-24 02:25:27 -04:00
2019-08-01 09:46:18 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr ( NonnullOwnPtr < U > const & ) = delete ;
2019-08-01 09:46:18 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr & operator = ( NonnullOwnPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr ( RefPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr ( NonnullRefPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr ( WeakPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr & operator = ( RefPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr & operator = ( NonnullRefPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
template < typename U >
2022-04-01 13:58:27 -04:00
OwnPtr & operator = ( WeakPtr < U > const & ) = delete ;
2019-07-11 10:43:20 -04:00
2018-10-10 05:53:07 -04:00
OwnPtr & operator = ( OwnPtr & & other )
{
2020-01-24 03:31:14 -05:00
OwnPtr ptr ( move ( other ) ) ;
swap ( ptr ) ;
2018-10-10 05:53:07 -04:00
return * this ;
}
template < typename U >
OwnPtr & operator = ( OwnPtr < U > & & other )
{
2020-01-24 03:31:14 -05:00
OwnPtr ptr ( move ( other ) ) ;
swap ( ptr ) ;
2018-10-10 05:53:07 -04:00
return * this ;
}
2019-07-24 02:25:27 -04:00
template < typename U >
OwnPtr & operator = ( NonnullOwnPtr < U > & & other )
{
2020-01-24 03:31:14 -05:00
OwnPtr ptr ( move ( other ) ) ;
swap ( ptr ) ;
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2019-07-24 02:25:27 -04:00
return * this ;
}
2021-05-30 12:39:23 -04:00
OwnPtr & operator = ( T * ptr ) = delete ;
2018-10-10 05:53:07 -04:00
OwnPtr & operator = ( std : : nullptr_t )
{
clear ( ) ;
return * this ;
}
void clear ( )
{
delete m_ptr ;
m_ptr = nullptr ;
}
bool operator ! ( ) const { return ! m_ptr ; }
2020-08-05 09:40:03 -04:00
[ [ nodiscard ] ] T * leak_ptr ( )
2018-10-10 05:53:07 -04:00
{
2019-07-24 02:25:27 -04:00
T * leaked_ptr = m_ptr ;
2018-10-10 05:53:07 -04:00
m_ptr = nullptr ;
2019-07-24 02:25:27 -04:00
return leaked_ptr ;
2018-10-10 05:53:07 -04:00
}
2019-08-14 05:02:49 -04:00
NonnullOwnPtr < T > release_nonnull ( )
{
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2019-08-14 05:02:49 -04:00
return NonnullOwnPtr < T > ( NonnullOwnPtr < T > : : Adopt , * leak_ptr ( ) ) ;
}
2020-04-05 05:32:30 -04:00
template < typename U >
NonnullOwnPtr < U > release_nonnull ( )
{
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2020-04-05 05:32:30 -04:00
return NonnullOwnPtr < U > ( NonnullOwnPtr < U > : : Adopt , static_cast < U & > ( * leak_ptr ( ) ) ) ;
}
2018-10-10 05:53:07 -04:00
T * ptr ( ) { return m_ptr ; }
const T * ptr ( ) const { return m_ptr ; }
2019-08-02 04:34:40 -04:00
T * operator - > ( )
{
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2019-08-02 04:34:40 -04:00
return m_ptr ;
}
const T * operator - > ( ) const
{
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2019-08-02 04:34:40 -04:00
return m_ptr ;
}
2018-10-10 05:53:07 -04:00
2019-08-02 04:34:40 -04:00
T & operator * ( )
{
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2019-08-02 04:34:40 -04:00
return * m_ptr ;
}
const T & operator * ( ) const
{
2021-02-23 14:42:32 -05:00
VERIFY ( m_ptr ) ;
2019-08-02 04:34:40 -04:00
return * m_ptr ;
}
2018-10-10 05:53:07 -04:00
2019-04-13 20:36:06 -04:00
operator const T * ( ) const { return m_ptr ; }
operator T * ( ) { return m_ptr ; }
2018-10-10 05:53:07 -04:00
operator bool ( ) { return ! ! m_ptr ; }
2020-01-24 03:31:14 -05:00
void swap ( OwnPtr & other )
{
: : swap ( m_ptr , other . m_ptr ) ;
}
template < typename U >
void swap ( OwnPtr < U > & other )
{
: : swap ( m_ptr , other . m_ptr ) ;
}
2021-05-30 12:39:23 -04:00
static OwnPtr lift ( T * ptr )
{
return OwnPtr { ptr } ;
}
protected :
explicit OwnPtr ( T * ptr )
: m_ptr ( ptr )
{
static_assert (
2021-06-25 01:33:15 -04: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 12:39:23 -04:00
}
2018-10-10 05:53:07 -04:00
private :
T * m_ptr = nullptr ;
} ;
2020-01-24 03:31:14 -05:00
template < typename T , typename U >
inline void swap ( OwnPtr < T > & a , OwnPtr < U > & b )
{
a . swap ( b ) ;
}
2021-05-12 23:35:08 -04:00
template < typename T >
inline OwnPtr < T > adopt_own_if_nonnull ( T * object )
{
if ( object )
2021-05-30 12:39:23 -04:00
return OwnPtr < T > : : lift ( object ) ;
2021-05-12 23:35:08 -04:00
return { } ;
}
2021-11-07 06:10:16 -05:00
template < typename T >
inline ErrorOr < NonnullOwnPtr < T > > adopt_nonnull_own_or_enomem ( T * object )
{
auto result = adopt_own_if_nonnull ( object ) ;
if ( ! result )
2021-12-29 17:29:12 -05:00
return Error : : from_errno ( ENOMEM ) ;
2021-11-07 06:10:16 -05:00
return result . release_nonnull ( ) ;
}
2021-08-15 04:37:33 -04:00
2021-06-20 04:04:41 -04:00
template < typename T , class . . . Args >
2022-02-03 10:19:53 -05:00
requires ( IsConstructible < T , Args . . . > ) inline ErrorOr < NonnullOwnPtr < T > > try_make ( Args & & . . . args )
2021-06-20 04:04:41 -04:00
{
2022-02-03 10:19:53 -05:00
return adopt_nonnull_own_or_enomem ( new ( nothrow ) T ( forward < Args > ( args ) . . . ) ) ;
2021-06-20 04:04:41 -04:00
}
2021-07-01 04:21:14 -04:00
// FIXME: Remove once P0960R3 is available in Clang.
template < typename T , class . . . Args >
2022-02-03 10:19:53 -05:00
inline ErrorOr < NonnullOwnPtr < T > > try_make ( Args & & . . . args )
2021-07-01 04:21:14 -04:00
{
2022-02-03 10:19:53 -05:00
return adopt_nonnull_own_or_enomem ( new ( nothrow ) T { forward < Args > ( args ) . . . } ) ;
2021-07-01 04:21:14 -04:00
}
2018-10-26 11:42:12 -04:00
template < typename T >
2019-06-29 13:14:03 -04:00
struct Traits < OwnPtr < T > > : public GenericTraits < OwnPtr < T > > {
2021-05-08 05:11:37 -04:00
using PeekType = T * ;
using ConstPeekType = const T * ;
2022-04-01 13:58:27 -04: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 11:42:12 -04:00
} ;
2018-10-10 05:53:07 -04:00
}
2021-11-07 06:10:16 -05:00
using AK : : adopt_nonnull_own_or_enomem ;
2021-05-12 23:35:08 -04:00
using AK : : adopt_own_if_nonnull ;
2019-05-28 05:53:16 -04:00
using AK : : OwnPtr ;
2021-06-20 04:04:41 -04:00
using AK : : try_make ;