1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 04:57:45 +00:00

LibCore: Introduce a C_OBJECT macro.

This macro goes at the top of every CObject-derived class like so:

class SomeClass : public CObject {
    C_OBJECT(SomeClass)
public:
    ...

At the moment, all it does is create an override for the class_name() getter
but in the future this will be used to automatically insert member functions
into these classes.
This commit is contained in:
Andreas Kling 2019-07-25 19:49:28 +02:00
parent d21a4f7518
commit a599317624
56 changed files with 70 additions and 105 deletions

View file

@ -4,6 +4,7 @@
#include <LibCore/CIODevice.h>
class CFile final : public CIODevice {
C_OBJECT(CFile)
public:
CFile() {}
explicit CFile(const StringView&);
@ -20,8 +21,6 @@ public:
};
bool open(int fd, CIODevice::OpenMode, ShouldCloseFileDescription);
virtual const char* class_name() const override { return "CFile"; }
private:
String m_filename;
ShouldCloseFileDescription m_should_close_file_descriptor { ShouldCloseFileDescription::Yes };

View file

@ -7,14 +7,13 @@
class CTCPSocket;
class CHttpJob final : public CNetworkJob {
C_OBJECT(CHttpJob)
public:
explicit CHttpJob(const CHttpRequest&);
virtual ~CHttpJob() override;
virtual void start() override;
virtual const char* class_name() const override { return "CHttpJob"; }
private:
void on_socket_connected();

View file

@ -5,6 +5,7 @@
#include <LibCore/CObject.h>
class CIODevice : public CObject {
C_OBJECT(CIODevice)
public:
enum OpenMode {
NotOpen = 0,
@ -52,8 +53,6 @@ public:
int printf(const char*, ...);
virtual const char* class_name() const override { return "CIODevice"; }
protected:
explicit CIODevice(CObject* parent = nullptr);

View file

@ -2,6 +2,7 @@
#include <LibCore/CSocket.h>
class CLocalSocket final : public CSocket {
C_OBJECT(CLocalSocket)
public:
explicit CLocalSocket(CObject* parent = nullptr);
virtual ~CLocalSocket() override;

View file

@ -6,6 +6,7 @@
class CNetworkResponse;
class CNetworkJob : public CObject {
C_OBJECT(CNetworkJob)
public:
enum class Error {
None,
@ -24,8 +25,6 @@ public:
virtual void start() = 0;
virtual const char* class_name() const override { return "CNetworkJob"; }
protected:
CNetworkJob();
void did_finish(NonnullRefPtr<CNetworkResponse>&&);

View file

@ -4,6 +4,7 @@
#include "CObject.h"
class CNotifier : public CObject {
C_OBJECT(CNotifier)
public:
enum Event {
None = 0,
@ -12,7 +13,7 @@ public:
Exceptional = 4,
};
CNotifier(int fd, unsigned event_mask);
~CNotifier();
virtual ~CNotifier() override;
void set_enabled(bool);
@ -23,8 +24,7 @@ public:
unsigned event_mask() const { return m_event_mask; }
void set_event_mask(unsigned event_mask) { m_event_mask = event_mask; }
const char* class_name() const override { return "CNotifier"; }
void event(CEvent& event) override;
void event(CEvent&) override;
private:
int m_fd { -1 };

View file

@ -11,13 +11,16 @@ class CChildEvent;
class CCustomEvent;
class CTimerEvent;
#define C_OBJECT(klass) \
public: \
virtual const char* class_name() const override { return #klass; }
class CObject : public Weakable<CObject> {
// NOTE: No C_OBJECT macro for CObject itself.
public:
CObject(CObject* parent = nullptr, bool is_widget = false);
virtual ~CObject();
virtual const char* class_name() const { return "CObject"; }
virtual const char* class_name() const = 0;
virtual void event(CEvent&);
const String& name() const { return m_name; }
@ -58,6 +61,8 @@ public:
virtual bool is_window() const { return false; }
protected:
CObject(CObject* parent = nullptr, bool is_widget = false);
virtual void timer_event(CTimerEvent&);
virtual void child_event(CChildEvent&);
virtual void custom_event(CCustomEvent&);

View file

@ -6,6 +6,7 @@
class CNotifier;
class CSocket : public CIODevice {
C_OBJECT(CSocket)
public:
enum class Type {
Invalid,
@ -35,8 +36,6 @@ public:
Function<void()> on_connected;
virtual const char* class_name() const override { return "CSocket"; }
protected:
CSocket(Type, CObject* parent);

View file

@ -1,6 +1,7 @@
#include <LibCore/CSocket.h>
class CTCPSocket final : public CSocket {
C_OBJECT(CTCPSocket)
public:
explicit CTCPSocket(CObject* parent = nullptr);
virtual ~CTCPSocket() override;

View file

@ -4,6 +4,7 @@
#include <LibCore/CObject.h>
class CTimer final : public CObject {
C_OBJECT(CTimer)
public:
explicit CTimer(CObject* parent = nullptr);
CTimer(int interval, Function<void()>&& timeout_handler, CObject* parent = nullptr);
@ -29,8 +30,6 @@ public:
Function<void()> on_timeout;
virtual const char* class_name() const override { return "CTimer"; }
private:
virtual void timer_event(CTimerEvent&) override;

View file

@ -49,6 +49,7 @@ private:
template <typename ServerMessage, typename ClientMessage>
class Connection : public CObject {
C_OBJECT(Connection)
public:
Connection(const StringView& address)
: m_notifier(CNotifier(m_connection.fd(), CNotifier::Read))

View file

@ -55,8 +55,8 @@ T* new_connection_for_client(Args&& ... args)
};
template <typename ServerMessage, typename ClientMessage>
class Connection : public CObject
{
class Connection : public CObject {
C_OBJECT(Connection)
public:
Connection(int fd, int client_id)
: m_socket(fd)
@ -168,8 +168,6 @@ public:
m_notifier.set_enabled(false);
}
const char* class_name() const override { return "Connection"; }
int client_id() const { return m_client_id; }
pid_t client_pid() const { return m_pid; }
void set_client_pid(pid_t pid) { m_pid = pid; }
@ -194,10 +192,9 @@ protected:
private:
// TODO: A way to create some kind of CIODevice with an open FD would be nice.
class COpenedSocket : public CIODevice
{
class COpenedSocket : public CIODevice {
C_OBJECT(COpenedSocket)
public:
const char* class_name() const override { return "COpenedSocket"; }
COpenedSocket(int fd)
{
set_fd(fd);