| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2025-08-10 15:06:39 +02:00
										 |  |  |  * Copyright (c) 2018-2025, Andreas Kling <andreas@ladybird.org> | 
					
						
							| 
									
										
										
										
											2022-02-26 09:09:45 -07:00
										 |  |  |  * Copyright (c) 2022, the SerenityOS developers. | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2021-04-22 01:24:48 -07:00
										 |  |  |  * SPDX-License-Identifier: BSD-2-Clause | 
					
						
							| 
									
										
										
										
											2020-01-18 09:38:21 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | #pragma once
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2024-12-26 11:50:25 +01:00
										 |  |  | #include <AK/AtomicRefCounted.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:29:06 +01:00
										 |  |  | #include <AK/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2019-09-22 00:17:53 +02:00
										 |  |  | #include <AK/Noncopyable.h>
 | 
					
						
							| 
									
										
										
										
											2021-05-19 14:35:34 +02:00
										 |  |  | #include <AK/OwnPtr.h>
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:15 -06:00
										 |  |  | #include <AK/StringView.h>
 | 
					
						
							| 
									
										
										
										
											2020-07-26 17:16:35 +02:00
										 |  |  | #include <AK/TypeCasts.h>
 | 
					
						
							| 
									
										
										
										
											2024-12-08 21:26:06 +04:00
										 |  |  | #include <AK/Vector.h>
 | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | #include <AK/Weakable.h>
 | 
					
						
							| 
									
										
										
										
											2020-02-14 22:29:06 +01:00
										 |  |  | #include <LibCore/Forward.h>
 | 
					
						
							| 
									
										
										
										
											2019-08-18 20:36:37 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  | namespace Core { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-11-14 10:38:36 -05:00
										 |  |  | #define C_OBJECT(klass)                                                                    \
 | 
					
						
							|  |  |  | public:                                                                                    \ | 
					
						
							| 
									
										
										
										
											2022-10-17 00:06:11 +02:00
										 |  |  |     virtual StringView class_name() const override                                         \ | 
					
						
							|  |  |  |     {                                                                                      \ | 
					
						
							|  |  |  |         return #klass##sv;                                                                 \ | 
					
						
							|  |  |  |     }                                                                                      \ | 
					
						
							| 
									
										
										
										
											2022-11-14 10:38:36 -05:00
										 |  |  |     template<typename Klass = klass, class... Args>                                        \ | 
					
						
							|  |  |  |     static NonnullRefPtr<klass> construct(Args&&... args)                                  \ | 
					
						
							|  |  |  |     {                                                                                      \ | 
					
						
							|  |  |  |         return adopt_ref(*new Klass(::forward<Args>(args)...));                            \ | 
					
						
							|  |  |  |     }                                                                                      \ | 
					
						
							|  |  |  |     template<typename Klass = klass, class... Args>                                        \ | 
					
						
							|  |  |  |     static ErrorOr<NonnullRefPtr<klass>> try_create(Args&&... args)                        \ | 
					
						
							|  |  |  |     {                                                                                      \ | 
					
						
							|  |  |  |         return adopt_nonnull_ref_or_enomem(new (nothrow) Klass(::forward<Args>(args)...)); \ | 
					
						
							| 
									
										
										
										
											2019-09-21 10:13:34 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-12-29 15:58:07 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-17 00:06:11 +02:00
										 |  |  | #define C_OBJECT_ABSTRACT(klass)                   \
 | 
					
						
							|  |  |  | public:                                            \ | 
					
						
							|  |  |  |     virtual StringView class_name() const override \ | 
					
						
							|  |  |  |     {                                              \ | 
					
						
							|  |  |  |         return #klass##sv;                         \ | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2019-07-25 19:49:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  | class EventReceiver | 
					
						
							| 
									
										
										
										
											2024-12-26 11:50:25 +01:00
										 |  |  |     : public AtomicRefCounted<EventReceiver> | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  |     , public Weakable<EventReceiver> { | 
					
						
							|  |  |  |     // NOTE: No C_OBJECT macro for Core::EventReceiver itself.
 | 
					
						
							| 
									
										
										
										
											2019-09-22 00:17:53 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  |     AK_MAKE_NONCOPYABLE(EventReceiver); | 
					
						
							|  |  |  |     AK_MAKE_NONMOVABLE(EventReceiver); | 
					
						
							| 
									
										
										
										
											2020-08-26 21:52:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-16 16:04:19 -04:00
										 |  |  | public: | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  |     virtual ~EventReceiver(); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-03-16 18:26:15 -06:00
										 |  |  |     virtual StringView class_name() const = 0; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-09-30 17:59:41 +02:00
										 |  |  |     template<typename T> | 
					
						
							|  |  |  |     bool fast_is() const = delete; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-11 11:45:39 +02:00
										 |  |  |     void start_timer(int ms); | 
					
						
							| 
									
										
										
										
											2019-01-31 17:31:23 +01:00
										 |  |  |     void stop_timer(); | 
					
						
							|  |  |  |     bool has_timer() const { return m_timer_id; } | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-08-30 10:43:28 +00:00
										 |  |  |     void deferred_invoke(Function<void()>); | 
					
						
							| 
									
										
										
										
											2019-04-07 14:36:10 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-08-11 11:30:20 +02:00
										 |  |  |     void dispatch_event(Core::Event&); | 
					
						
							| 
									
										
										
										
											2021-11-24 13:09:51 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-15 16:12:06 +01:00
										 |  |  | protected: | 
					
						
							| 
									
										
										
										
											2025-08-11 11:30:20 +02:00
										 |  |  |     EventReceiver(); | 
					
						
							| 
									
										
										
										
											2019-07-25 19:49:28 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-03-28 11:24:22 +02:00
										 |  |  |     virtual void event(Core::Event&); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-02 12:34:39 +01:00
										 |  |  |     virtual void timer_event(TimerEvent&); | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-03-15 16:12:06 +01:00
										 |  |  | private: | 
					
						
							| 
									
										
										
										
											2024-02-18 00:11:38 -05:00
										 |  |  |     intptr_t m_timer_id { 0 }; | 
					
						
							| 
									
										
										
										
											2019-01-20 04:49:48 +01:00
										 |  |  | }; | 
					
						
							| 
									
										
										
										
											2019-05-27 04:06:01 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2019-05-27 04:18:24 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-10-15 21:07:36 +02:00
										 |  |  | template<> | 
					
						
							| 
									
										
										
										
											2023-08-06 18:09:39 +02:00
										 |  |  | struct AK::Formatter<Core::EventReceiver> : AK::Formatter<FormatString> { | 
					
						
							|  |  |  |     ErrorOr<void> format(FormatBuilder& builder, Core::EventReceiver const& value) | 
					
						
							| 
									
										
										
										
											2021-01-09 01:00:22 +01:00
										 |  |  |     { | 
					
						
							| 
									
										
										
										
											2022-07-11 17:32:29 +00:00
										 |  |  |         return AK::Formatter<FormatString>::format(builder, "{}({})"sv, value.class_name(), &value); | 
					
						
							| 
									
										
										
										
											2021-01-09 01:00:22 +01:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-10-15 21:07:36 +02:00
										 |  |  | }; |