2020-03-18 20:21:06 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-03-18 20:21:06 +01:00
|
|
|
*/
|
|
|
|
|
|
2020-03-18 20:03:17 +01:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include <AK/Badge.h>
|
|
|
|
|
#include <AK/Noncopyable.h>
|
|
|
|
|
#include <AK/RefCounted.h>
|
|
|
|
|
#include <AK/RefPtr.h>
|
|
|
|
|
#include <LibJS/Forward.h>
|
|
|
|
|
|
|
|
|
|
namespace JS {
|
|
|
|
|
|
|
|
|
|
class HandleImpl : public RefCounted<HandleImpl> {
|
|
|
|
|
AK_MAKE_NONCOPYABLE(HandleImpl);
|
|
|
|
|
AK_MAKE_NONMOVABLE(HandleImpl);
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
~HandleImpl();
|
|
|
|
|
|
|
|
|
|
Cell* cell() { return m_cell; }
|
|
|
|
|
const Cell* cell() const { return m_cell; }
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
template<class T>
|
|
|
|
|
friend class Handle;
|
|
|
|
|
|
|
|
|
|
explicit HandleImpl(Cell*);
|
|
|
|
|
Cell* m_cell { nullptr };
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
class Handle {
|
|
|
|
|
public:
|
2020-09-18 09:49:51 +02:00
|
|
|
Handle() { }
|
2020-03-18 20:03:17 +01:00
|
|
|
|
|
|
|
|
static Handle create(T* cell)
|
|
|
|
|
{
|
2021-04-23 16:46:57 +02:00
|
|
|
return Handle(adopt_ref(*new HandleImpl(cell)));
|
2020-03-18 20:03:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
T* cell() { return static_cast<T*>(m_impl->cell()); }
|
|
|
|
|
const T* cell() const { return static_cast<const T*>(m_impl->cell()); }
|
|
|
|
|
|
2020-12-22 14:18:33 +03:30
|
|
|
bool is_null() const { return m_impl.is_null(); }
|
|
|
|
|
|
2020-03-18 20:03:17 +01:00
|
|
|
private:
|
|
|
|
|
explicit Handle(NonnullRefPtr<HandleImpl> impl)
|
|
|
|
|
: m_impl(move(impl))
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
RefPtr<HandleImpl> m_impl;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
|
inline Handle<T> make_handle(T* cell)
|
|
|
|
|
{
|
|
|
|
|
return Handle<T>::create(cell);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|