mirror of
https://github.com/RGBCube/serenity
synced 2025-07-27 11:27:35 +00:00
Add clang-format file
Also run it across the whole tree to get everything using the One True Style. We don't yet run this in an automated fashion as it's a little slow, but there is a snippet to do so in makeall.sh.
This commit is contained in:
parent
c11351ac50
commit
0dc9af5f7e
286 changed files with 3244 additions and 2424 deletions
157
AK/Retained.h
157
AK/Retained.h
|
@ -4,15 +4,15 @@
|
|||
#include <AK/Types.h>
|
||||
|
||||
#ifdef __clang__
|
||||
#define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
|
||||
#define CALLABLE_WHEN(...) __attribute__((callable_when(__VA_ARGS__)))
|
||||
#define SET_TYPESTATE(state) __attribute__((set_typestate(state)))
|
||||
#define RETURN_TYPESTATE(state) __attribute__((return_typestate(state)))
|
||||
# define CONSUMABLE(initial_state) __attribute__((consumable(initial_state)))
|
||||
# define CALLABLE_WHEN(...) __attribute__((callable_when(__VA_ARGS__)))
|
||||
# define SET_TYPESTATE(state) __attribute__((set_typestate(state)))
|
||||
# define RETURN_TYPESTATE(state) __attribute__((return_typestate(state)))
|
||||
#else
|
||||
#define CONSUMABLE(initial_state)
|
||||
#define CALLABLE_WHEN(state)
|
||||
#define SET_TYPESTATE(state)
|
||||
#define RETURN_TYPESTATE(state)
|
||||
# define CONSUMABLE(initial_state)
|
||||
# define CALLABLE_WHEN(state)
|
||||
# define SET_TYPESTATE(state)
|
||||
# define RETURN_TYPESTATE(state)
|
||||
#endif
|
||||
|
||||
namespace AK {
|
||||
|
@ -34,30 +34,75 @@ inline void release_if_not_null(T* ptr)
|
|||
template<typename T>
|
||||
class CONSUMABLE(unconsumed) Retained {
|
||||
public:
|
||||
enum AdoptTag { Adopt };
|
||||
enum AdoptTag {
|
||||
Adopt
|
||||
};
|
||||
|
||||
RETURN_TYPESTATE(unconsumed) Retained(const T& object) : m_ptr(const_cast<T*>(&object)) { m_ptr->retain(); }
|
||||
RETURN_TYPESTATE(unconsumed) Retained(T& object) : m_ptr(&object) { m_ptr->retain(); }
|
||||
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(U& object) : m_ptr(&static_cast<T&>(object)) { m_ptr->retain(); }
|
||||
RETURN_TYPESTATE(unconsumed) Retained(AdoptTag, T& object) : m_ptr(&object) { }
|
||||
RETURN_TYPESTATE(unconsumed) Retained(Retained& other) : m_ptr(&other.copy_ref().leak_ref()) { }
|
||||
RETURN_TYPESTATE(unconsumed) Retained(Retained&& other) : m_ptr(&other.leak_ref()) { }
|
||||
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(Retained<U>&& other) : m_ptr(static_cast<T*>(&other.leak_ref())) { }
|
||||
RETURN_TYPESTATE(unconsumed) Retained(const Retained& other) : m_ptr(&const_cast<Retained&>(other).copy_ref().leak_ref()) { }
|
||||
template<typename U> RETURN_TYPESTATE(unconsumed) Retained(const Retained<U>& other) : m_ptr(&const_cast<Retained<U>&>(other).copy_ref().leak_ref()) { }
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(const T& object)
|
||||
: m_ptr(const_cast<T*>(&object))
|
||||
{
|
||||
m_ptr->retain();
|
||||
}
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(T& object)
|
||||
: m_ptr(&object)
|
||||
{
|
||||
m_ptr->retain();
|
||||
}
|
||||
template<typename U>
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(U& object)
|
||||
: m_ptr(&static_cast<T&>(object))
|
||||
{
|
||||
m_ptr->retain();
|
||||
}
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(AdoptTag, T& object)
|
||||
: m_ptr(&object)
|
||||
{
|
||||
}
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(Retained& other)
|
||||
: m_ptr(&other.copy_ref().leak_ref())
|
||||
{
|
||||
}
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(Retained&& other)
|
||||
: m_ptr(&other.leak_ref())
|
||||
{
|
||||
}
|
||||
template<typename U>
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(Retained<U>&& other)
|
||||
: m_ptr(static_cast<T*>(&other.leak_ref()))
|
||||
{
|
||||
}
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(const Retained& other)
|
||||
: m_ptr(&const_cast<Retained&>(other).copy_ref().leak_ref())
|
||||
{
|
||||
}
|
||||
template<typename U>
|
||||
RETURN_TYPESTATE(unconsumed)
|
||||
Retained(const Retained<U>& other)
|
||||
: m_ptr(&const_cast<Retained<U>&>(other).copy_ref().leak_ref())
|
||||
{
|
||||
}
|
||||
~Retained()
|
||||
{
|
||||
release_if_not_null(m_ptr);
|
||||
m_ptr = nullptr;
|
||||
#ifdef SANITIZE_PTRS
|
||||
if constexpr(sizeof(T*) == 8)
|
||||
if constexpr (sizeof(T*) == 8)
|
||||
m_ptr = (T*)(0xb0b0b0b0b0b0b0b0);
|
||||
else
|
||||
m_ptr = (T*)(0xb0b0b0b0);
|
||||
#endif
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) Retained& operator=(Retained&& other)
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
Retained& operator=(Retained&& other)
|
||||
{
|
||||
if (this != &other) {
|
||||
release_if_not_null(m_ptr);
|
||||
|
@ -67,7 +112,8 @@ public:
|
|||
}
|
||||
|
||||
template<typename U>
|
||||
CALLABLE_WHEN(unconsumed) Retained& operator=(Retained<U>&& other)
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
Retained& operator=(Retained<U>&& other)
|
||||
{
|
||||
if (this != static_cast<void*>(&other)) {
|
||||
release_if_not_null(m_ptr);
|
||||
|
@ -76,7 +122,8 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) Retained& operator=(T& object)
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
Retained& operator=(T& object)
|
||||
{
|
||||
if (m_ptr != &object)
|
||||
release_if_not_null(m_ptr);
|
||||
|
@ -85,12 +132,14 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) Retained copy_ref() const
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
Retained copy_ref() const
|
||||
{
|
||||
return Retained(*m_ptr);
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) SET_TYPESTATE(consumed)
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
SET_TYPESTATE(consumed)
|
||||
T& leak_ref()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
|
@ -99,20 +148,60 @@ public:
|
|||
return *leakedPtr;
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) T* ptr() { ASSERT(m_ptr); return m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed) const T* ptr() const { ASSERT(m_ptr); return m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
T* ptr()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
const T* ptr() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) T* operator->() { ASSERT(m_ptr); return m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed) const T* operator->() const { ASSERT(m_ptr); return m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
T* operator->()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
const T* operator->() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) T& operator*() { ASSERT(m_ptr); return *m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed) const T& operator*() const { ASSERT(m_ptr); return *m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
T& operator*()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
}
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
const T& operator*() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return *m_ptr;
|
||||
}
|
||||
|
||||
CALLABLE_WHEN(unconsumed) operator T*() { ASSERT(m_ptr); return m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed) operator const T*() const { ASSERT(m_ptr); return m_ptr; }
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
operator T*()
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
CALLABLE_WHEN(unconsumed)
|
||||
operator const T*() const
|
||||
{
|
||||
ASSERT(m_ptr);
|
||||
return m_ptr;
|
||||
}
|
||||
|
||||
private:
|
||||
Retained() { }
|
||||
Retained() {}
|
||||
|
||||
T* m_ptr { nullptr };
|
||||
};
|
||||
|
@ -125,5 +214,5 @@ inline Retained<T> adopt(T& object)
|
|||
|
||||
}
|
||||
|
||||
using AK::Retained;
|
||||
using AK::adopt;
|
||||
using AK::Retained;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue