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