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