diff --git a/Applications/IRCClient/IRCClient.cpp b/Applications/IRCClient/IRCClient.cpp index 2fd70babd34..baee644a152 100644 --- a/Applications/IRCClient/IRCClient.cpp +++ b/Applications/IRCClient/IRCClient.cpp @@ -35,7 +35,7 @@ IRCClient::IRCClient() , m_log(IRCLogBuffer::create()) , m_config(CConfigFile::get_for_app("IRCClient")) { - m_socket = new CTCPSocket(this); + m_socket = CTCPSocket::construct(this); m_nickname = m_config->read_entry("User", "Nickname", "seren1ty"); m_hostname = m_config->read_entry("Connection", "Server", ""); m_port = m_config->read_num_entry("Connection", "Port", 6667); diff --git a/Applications/IRCClient/IRCClient.h b/Applications/IRCClient/IRCClient.h index 56a005061f7..75e20e7ae16 100644 --- a/Applications/IRCClient/IRCClient.h +++ b/Applications/IRCClient/IRCClient.h @@ -137,7 +137,7 @@ private: String m_hostname; int m_port { 6667 }; - CTCPSocket* m_socket { nullptr }; + ObjectPtr m_socket; String m_nickname; ObjectPtr m_notifier; diff --git a/Libraries/LibCore/CHttpJob.cpp b/Libraries/LibCore/CHttpJob.cpp index 716d170acb7..472aae01d9b 100644 --- a/Libraries/LibCore/CHttpJob.cpp +++ b/Libraries/LibCore/CHttpJob.cpp @@ -116,7 +116,7 @@ void CHttpJob::finish_up() void CHttpJob::start() { ASSERT(!m_socket); - m_socket = new CTCPSocket(this); + m_socket = CTCPSocket::construct(this); m_socket->on_connected = [this] { dbg() << "CHttpJob: on_connected callback"; on_socket_connected(); diff --git a/Libraries/LibCore/CHttpJob.h b/Libraries/LibCore/CHttpJob.h index becc6a030fa..d0aa714920a 100644 --- a/Libraries/LibCore/CHttpJob.h +++ b/Libraries/LibCore/CHttpJob.h @@ -26,7 +26,7 @@ private: }; CHttpRequest m_request; - CTCPSocket* m_socket { nullptr }; + ObjectPtr m_socket; State m_state { State::InStatus }; int m_code { -1 }; HashMap m_headers; diff --git a/Libraries/LibCore/CObject.h b/Libraries/LibCore/CObject.h index bfe9a14f005..95fea755904 100644 --- a/Libraries/LibCore/CObject.h +++ b/Libraries/LibCore/CObject.h @@ -1,11 +1,12 @@ #pragma once -#include #include #include #include +#include #include #include +#include namespace AK { class JsonObject; @@ -16,9 +17,14 @@ class CChildEvent; class CCustomEvent; class CTimerEvent; -#define C_OBJECT(klass) \ -public: \ - virtual const char* class_name() const override { return #klass; } +#define C_OBJECT(klass) \ +public: \ + virtual const char* class_name() const override { return #klass; } \ + template \ + static inline ObjectPtr construct(Args&&... args) \ + { \ + return ObjectPtr(new klass(forward(args)...)); \ + } class CObject : public Weakable { // NOTE: No C_OBJECT macro for CObject itself. diff --git a/Libraries/LibCore/CTCPServer.cpp b/Libraries/LibCore/CTCPServer.cpp index c1e61994c5c..71a2b15906e 100644 --- a/Libraries/LibCore/CTCPServer.cpp +++ b/Libraries/LibCore/CTCPServer.cpp @@ -40,7 +40,7 @@ bool CTCPServer::listen(const IPv4Address& address, u16 port) return true; } -CTCPSocket* CTCPServer::accept() +ObjectPtr CTCPServer::accept() { ASSERT(m_listening); sockaddr_in in; @@ -51,5 +51,5 @@ CTCPSocket* CTCPServer::accept() return nullptr; } - return new CTCPSocket({}, accepted_fd); + return CTCPSocket::construct(accepted_fd); } diff --git a/Libraries/LibCore/CTCPServer.h b/Libraries/LibCore/CTCPServer.h index 09b38521187..67b551e22c2 100644 --- a/Libraries/LibCore/CTCPServer.h +++ b/Libraries/LibCore/CTCPServer.h @@ -15,7 +15,7 @@ public: bool is_listening() const { return m_listening; } bool listen(const IPv4Address& address, u16 port); - CTCPSocket* accept(); + ObjectPtr accept(); Function on_ready_to_accept; diff --git a/Libraries/LibCore/CTCPSocket.cpp b/Libraries/LibCore/CTCPSocket.cpp index 5f5ebb79da3..060b76cd6bb 100644 --- a/Libraries/LibCore/CTCPSocket.cpp +++ b/Libraries/LibCore/CTCPSocket.cpp @@ -2,7 +2,7 @@ #include #include -CTCPSocket::CTCPSocket(Badge, int fd, CObject* parent) +CTCPSocket::CTCPSocket(int fd, CObject* parent) : CSocket(CSocket::Type::TCP, parent) { set_fd(fd); diff --git a/Libraries/LibCore/CTCPSocket.h b/Libraries/LibCore/CTCPSocket.h index 7eccc7a6b78..c183d6e1c5a 100644 --- a/Libraries/LibCore/CTCPSocket.h +++ b/Libraries/LibCore/CTCPSocket.h @@ -8,7 +8,9 @@ class CTCPServer; class CTCPSocket final : public CSocket { C_OBJECT(CTCPSocket) public: - explicit CTCPSocket(CObject* parent = nullptr); - CTCPSocket(Badge, int fd, CObject* parent = nullptr); virtual ~CTCPSocket() override; + +private: + CTCPSocket(int fd, CObject* parent = nullptr); + explicit CTCPSocket(CObject* parent = nullptr); }; diff --git a/Libraries/LibCore/CoreIPCClient.h b/Libraries/LibCore/CoreIPCClient.h index c26e3928fdb..f785ecb0240 100644 --- a/Libraries/LibCore/CoreIPCClient.h +++ b/Libraries/LibCore/CoreIPCClient.h @@ -47,7 +47,6 @@ namespace Client { template class Connection : public CObject { - C_OBJECT(Connection) public: Connection(const StringView& address) : m_connection(this) @@ -238,7 +237,6 @@ namespace Client { template class ConnectionNG : public CObject { - C_OBJECT(Connection) public: ConnectionNG(const StringView& address) : m_connection(this) diff --git a/Libraries/LibCore/CoreIPCServer.h b/Libraries/LibCore/CoreIPCServer.h index 69a1681eb1f..dd65494e949 100644 --- a/Libraries/LibCore/CoreIPCServer.h +++ b/Libraries/LibCore/CoreIPCServer.h @@ -64,7 +64,6 @@ namespace Server { template class Connection : public CObject { - C_OBJECT(Connection) public: Connection(CLocalSocket& socket, int client_id) : m_socket(socket) @@ -206,7 +205,6 @@ namespace Server { template class ConnectionNG : public CObject { - C_OBJECT(Connection) public: ConnectionNG(Endpoint& endpoint, CLocalSocket& socket, int client_id) : m_endpoint(endpoint) diff --git a/Libraries/LibGUI/GEventLoop.h b/Libraries/LibGUI/GEventLoop.h index 3358f30207a..8171300216e 100644 --- a/Libraries/LibGUI/GEventLoop.h +++ b/Libraries/LibGUI/GEventLoop.h @@ -11,6 +11,7 @@ class CNotifier; class GWindow; class GWindowServerConnection : public IPC::Client::Connection { + C_OBJECT(GWindowServerConnection) public: GWindowServerConnection() : Connection("/tmp/wsportal") diff --git a/Servers/TelnetServer/Client.cpp b/Servers/TelnetServer/Client.cpp index 6c71ac3231b..a813849a47e 100644 --- a/Servers/TelnetServer/Client.cpp +++ b/Servers/TelnetServer/Client.cpp @@ -10,9 +10,9 @@ #include "Client.h" -Client::Client(int id, CTCPSocket* socket, int ptm_fd) +Client::Client(int id, ObjectPtr socket, int ptm_fd) : m_id(id) - , m_socket(socket) + , m_socket(move(socket)) , m_ptm_fd(ptm_fd) , m_ptm_notifier(CNotifier::create(ptm_fd, CNotifier::Read)) { diff --git a/Servers/TelnetServer/Client.h b/Servers/TelnetServer/Client.h index 60e222cc989..83e9ec32f9f 100644 --- a/Servers/TelnetServer/Client.h +++ b/Servers/TelnetServer/Client.h @@ -11,15 +11,15 @@ class Client : public RefCounted { public: - static NonnullRefPtr create(int id, CTCPSocket* socket, int ptm_fd) + static NonnullRefPtr create(int id, ObjectPtr socket, int ptm_fd) { - return adopt(*new Client(id, socket, ptm_fd)); + return adopt(*new Client(id, move(socket), ptm_fd)); } Function on_exit; protected: - Client(int id, CTCPSocket* socket, int ptm_fd); + Client(int id, ObjectPtr socket, int ptm_fd); void drain_socket(); void drain_pty(); @@ -35,7 +35,7 @@ private: // client id int m_id { 0 }; // client resources - CTCPSocket* m_socket { nullptr }; + ObjectPtr m_socket; Parser m_parser; // pty resources int m_ptm_fd { -1 }; diff --git a/Servers/TelnetServer/main.cpp b/Servers/TelnetServer/main.cpp index d8cfd364c8d..e932fccad71 100644 --- a/Servers/TelnetServer/main.cpp +++ b/Servers/TelnetServer/main.cpp @@ -107,7 +107,7 @@ int main(int argc, char** argv) server.on_ready_to_accept = [&next_id, &clients, &server] { int id = next_id++; - auto* client_socket = server.accept(); + auto client_socket = server.accept(); if (!client_socket) { perror("accept"); return;