mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-21 15:10:19 +00:00
AK: Add NonnullRawPtr<T>
abstraction
It is a non-null `T*` with reference semantics. Or a `T&` whose address can be copied and re-assigned. Or a `NonnullRefPtr` whose memory is not managed. It can be useful when you want to store a reference in a data structure that needs to be copyable or assignable.
This commit is contained in:
parent
90b2142658
commit
e50b9f5478
Notes:
github-actions[bot]
2024-11-06 09:44:26 +00:00
Author: https://github.com/yyny Commit: https://github.com/LadybirdBrowser/ladybird/commit/e50b9f5478e Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/2093 Reviewed-by: https://github.com/AtkinsSJ ✅
1 changed files with 87 additions and 0 deletions
87
AK/NonnullRawPtr.h
Normal file
87
AK/NonnullRawPtr.h
Normal file
|
@ -0,0 +1,87 @@
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2024, the Ladybird developers.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
|
*/
|
||||||
|
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <AK/Forward.h>
|
||||||
|
#include <AK/Traits.h>
|
||||||
|
|
||||||
|
namespace AK {
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
requires(!IsLvalueReference<T> && !IsRvalueReference<T>) class [[nodiscard]] NonnullRawPtr {
|
||||||
|
public:
|
||||||
|
using ValueType = T;
|
||||||
|
|
||||||
|
NonnullRawPtr() = delete;
|
||||||
|
|
||||||
|
NonnullRawPtr(T& other)
|
||||||
|
: m_ptr(&other)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRawPtr(NonnullRawPtr const& other)
|
||||||
|
: m_ptr(other.m_ptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRawPtr(NonnullRawPtr&& other)
|
||||||
|
: m_ptr(other.m_ptr)
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRawPtr& operator=(NonnullRawPtr const& other)
|
||||||
|
{
|
||||||
|
m_ptr = other.m_ptr;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
NonnullRawPtr& operator=(NonnullRawPtr&& other)
|
||||||
|
{
|
||||||
|
m_ptr = other.m_ptr;
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
operator bool() const = delete;
|
||||||
|
bool operator!() const = delete;
|
||||||
|
|
||||||
|
operator T&() { return *m_ptr; }
|
||||||
|
operator T const&() const { return *m_ptr; }
|
||||||
|
|
||||||
|
[[nodiscard]] ALWAYS_INLINE T& value() { return *m_ptr; }
|
||||||
|
[[nodiscard]] ALWAYS_INLINE T const& value() const { return *m_ptr; }
|
||||||
|
|
||||||
|
[[nodiscard]] ALWAYS_INLINE T& operator*() { return value(); }
|
||||||
|
[[nodiscard]] ALWAYS_INLINE T const& operator*() const { return value(); }
|
||||||
|
|
||||||
|
ALWAYS_INLINE RETURNS_NONNULL T* operator->() { return &value(); }
|
||||||
|
ALWAYS_INLINE RETURNS_NONNULL T const* operator->() const { return &value(); }
|
||||||
|
|
||||||
|
private:
|
||||||
|
T* m_ptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
struct Traits<NonnullRawPtr<T>> : public DefaultTraits<NonnullRawPtr<T>> {
|
||||||
|
static unsigned hash(NonnullRawPtr<T> const& handle) { return Traits<T>::hash(handle); }
|
||||||
|
};
|
||||||
|
|
||||||
|
namespace Detail {
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool IsHashCompatible<NonnullRawPtr<T>, T> = true;
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
inline constexpr bool IsHashCompatible<T, NonnullRawPtr<T>> = true;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#if USING_AK_GLOBALLY
|
||||||
|
using AK::NonnullRawPtr;
|
||||||
|
#endif
|
Loading…
Reference in a new issue