2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-05-24 17:32:46 +00:00
|
|
|
#include <AK/Assertions.h>
|
2019-07-04 05:05:58 +00:00
|
|
|
#include <AK/LogStream.h>
|
2020-01-18 12:33:44 +00:00
|
|
|
#include <AK/StdLibExtras.h>
|
2019-02-25 11:43:52 +00:00
|
|
|
#include <AK/Types.h>
|
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename T>
|
|
|
|
class OwnPtr;
|
2019-08-02 09:51:28 +00:00
|
|
|
template<typename T>
|
|
|
|
class RefPtr;
|
2019-07-11 14:43:20 +00:00
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
template<typename T>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE void ref_if_not_null(T* ptr)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (ptr)
|
2019-06-21 13:29:31 +00:00
|
|
|
ptr->ref();
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE void unref_if_not_null(T* ptr)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
if (ptr)
|
2020-01-23 14:14:21 +00:00
|
|
|
ptr->unref();
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2020-05-16 08:51:21 +00:00
|
|
|
class NonnullRefPtr {
|
2019-02-25 11:43:52 +00:00
|
|
|
public:
|
2019-07-25 09:10:28 +00:00
|
|
|
typedef T ElementType;
|
|
|
|
|
|
|
|
enum AdoptTag { Adopt };
|
2019-05-28 09:53:16 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(const T& object)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(const_cast<T*>(&object))
|
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
template<typename U>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(const U& object)
|
2020-04-05 09:11:07 +00:00
|
|
|
: m_ptr(&const_cast<U&>(object))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-21 13:29:31 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(AdoptTag, T& object)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(&object)
|
|
|
|
{
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr&& other)
|
2019-05-28 09:53:16 +00:00
|
|
|
: m_ptr(&other.leak_ref())
|
|
|
|
{
|
|
|
|
}
|
|
|
|
template<typename U>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(NonnullRefPtr<U>&& other)
|
2020-04-05 09:11:07 +00:00
|
|
|
: m_ptr(&other.leak_ref())
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(const NonnullRefPtr& other)
|
2019-06-24 08:23:59 +00:00
|
|
|
: m_ptr(const_cast<T*>(other.ptr()))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-24 08:23:59 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
|
|
|
template<typename U>
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr(const NonnullRefPtr<U>& other)
|
2020-04-05 09:11:07 +00:00
|
|
|
: m_ptr(const_cast<U*>(other.ptr()))
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
2019-06-24 08:23:59 +00:00
|
|
|
m_ptr->ref();
|
2019-05-28 09:53:16 +00:00
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE ~NonnullRefPtr()
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2020-01-23 14:14:21 +00:00
|
|
|
unref_if_not_null(m_ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
m_ptr = nullptr;
|
|
|
|
#ifdef SANITIZE_PTRS
|
2019-05-28 09:53:16 +00:00
|
|
|
if constexpr (sizeof(T*) == 8)
|
2019-02-25 11:43:52 +00:00
|
|
|
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
|
|
|
|
else
|
|
|
|
m_ptr = (T*)(0xb0b0b0b0);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2019-07-11 14:43:20 +00:00
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr(const OwnPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr& operator=(const OwnPtr<U>&) = delete;
|
|
|
|
|
2019-08-02 09:51:28 +00:00
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr(const RefPtr<U>&) = delete;
|
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr& operator=(const RefPtr<U>&) = delete;
|
|
|
|
NonnullRefPtr(const RefPtr<T>&) = delete;
|
|
|
|
NonnullRefPtr& operator=(const RefPtr<T>&) = delete;
|
|
|
|
|
2019-06-24 07:58:21 +00:00
|
|
|
NonnullRefPtr& operator=(const NonnullRefPtr& other)
|
|
|
|
{
|
2020-01-18 12:33:44 +00:00
|
|
|
NonnullRefPtr ptr(other);
|
|
|
|
swap(ptr);
|
2019-06-24 07:58:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
NonnullRefPtr& operator=(const NonnullRefPtr<U>& other)
|
|
|
|
{
|
2020-01-18 12:33:44 +00:00
|
|
|
NonnullRefPtr ptr(other);
|
|
|
|
swap(ptr);
|
2019-06-24 07:58:21 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE NonnullRefPtr& operator=(NonnullRefPtr&& other)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2020-01-18 12:33:44 +00:00
|
|
|
NonnullRefPtr ptr(move(other));
|
|
|
|
swap(ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
2019-06-21 16:37:47 +00:00
|
|
|
NonnullRefPtr& operator=(NonnullRefPtr<U>&& other)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2020-01-18 12:33:44 +00:00
|
|
|
NonnullRefPtr ptr(move(other));
|
|
|
|
swap(ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-08-02 09:51:28 +00:00
|
|
|
NonnullRefPtr& operator=(const T& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2020-01-18 12:33:44 +00:00
|
|
|
NonnullRefPtr ptr(object);
|
|
|
|
swap(ptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
[[nodiscard]] ALWAYS_INLINE T& leak_ref()
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
2019-08-02 10:05:09 +00:00
|
|
|
return *exchange(m_ptr, nullptr);
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE T* ptr()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE const T* ptr() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE T* operator->()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE const T* operator->() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE T& operator*()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE const T& operator*() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
2019-02-25 11:43:52 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE operator T*()
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE operator const T*() const
|
2019-05-28 09:53:16 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return m_ptr;
|
|
|
|
}
|
2019-04-14 00:36:06 +00:00
|
|
|
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE operator T&()
|
2019-06-15 16:45:44 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
2020-05-20 11:59:31 +00:00
|
|
|
ALWAYS_INLINE operator const T&() const
|
2019-06-15 16:45:44 +00:00
|
|
|
{
|
|
|
|
ASSERT(m_ptr);
|
|
|
|
return *m_ptr;
|
|
|
|
}
|
|
|
|
|
2019-11-07 17:00:05 +00:00
|
|
|
operator bool() const = delete;
|
|
|
|
bool operator!() const = delete;
|
|
|
|
|
2020-01-18 12:33:44 +00:00
|
|
|
void swap(NonnullRefPtr& other)
|
|
|
|
{
|
|
|
|
::swap(m_ptr, other.m_ptr);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
void swap(NonnullRefPtr<U>& other)
|
|
|
|
{
|
|
|
|
::swap(m_ptr, other.m_ptr);
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
private:
|
2019-06-24 07:58:21 +00:00
|
|
|
NonnullRefPtr() = delete;
|
2019-02-25 11:43:52 +00:00
|
|
|
|
|
|
|
T* m_ptr { nullptr };
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
2019-06-21 16:37:47 +00:00
|
|
|
inline NonnullRefPtr<T> adopt(T& object)
|
2019-02-25 11:43:52 +00:00
|
|
|
{
|
2019-06-21 16:37:47 +00:00
|
|
|
return NonnullRefPtr<T>(NonnullRefPtr<T>::Adopt, object);
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
2019-07-04 05:05:58 +00:00
|
|
|
template<typename T>
|
|
|
|
inline const LogStream& operator<<(const LogStream& stream, const NonnullRefPtr<T>& value)
|
|
|
|
{
|
|
|
|
return stream << value.ptr();
|
|
|
|
}
|
|
|
|
|
2020-01-19 09:29:21 +00:00
|
|
|
template<typename T, typename U>
|
|
|
|
inline void swap(NonnullRefPtr<T>& a, NonnullRefPtr<U>& b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
|
2019-02-25 11:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
using AK::adopt;
|
2019-06-21 16:37:47 +00:00
|
|
|
using AK::NonnullRefPtr;
|