| 
									
										
										
										
											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>
 | 
					
						
							| 
									
										
										
										
											2021-07-21 19:45:21 +02:00
										 |  |  | #include <AK/IntrusiveList.h>
 | 
					
						
							| 
									
										
										
										
											2020-03-18 20:03:17 +01:00
										 |  |  | #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 }; | 
					
						
							| 
									
										
										
										
											2021-07-21 19:45:21 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |     IntrusiveListNode<HandleImpl> m_list_node; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-09-09 16:30:59 +04:30
										 |  |  |     using List = IntrusiveList<&HandleImpl::m_list_node>; | 
					
						
							| 
									
										
										
										
											2020-03-18 20:03:17 +01:00
										 |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | template<class T> | 
					
						
							|  |  |  | class Handle { | 
					
						
							|  |  |  | public: | 
					
						
							| 
									
										
										
										
											2021-09-16 00:00:32 -07:00
										 |  |  |     Handle() = default; | 
					
						
							| 
									
										
										
										
											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); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |