From 20c066b8e0d9c2097cd9a46fc7e56b92fc300255 Mon Sep 17 00:00:00 2001 From: Ali Mohammad Pur Date: Fri, 18 Jun 2021 16:56:52 -0600 Subject: LibCore: Call optional did_construct() method when constucting objects Some objects need to perform tasks during construction that require the object to be fully constructed, which can't be done in the constructor. This allows postponing such tasks into a did_construct method that will be called right after the object was fully constructed. --- Userland/Libraries/LibCore/Object.h | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'Userland/Libraries') diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h index d9d0a2df2a..1cfa64683b 100644 --- a/Userland/Libraries/LibCore/Object.h +++ b/Userland/Libraries/LibCore/Object.h @@ -59,10 +59,13 @@ enum class TimerShouldFireWhenNotVisible { #define C_OBJECT(klass) \ public: \ virtual const char* class_name() const override { return #klass; } \ - template \ + template \ static inline NonnullRefPtr construct(Args&&... args) \ { \ - return adopt_ref(*new klass(forward(args)...)); \ + auto obj = adopt_ref(*new Klass(forward(args)...)); \ + if constexpr (requires { declval().did_construct(); }) \ + obj->did_construct(); \ + return obj; \ } #define C_OBJECT_ABSTRACT(klass) \ -- cgit v1.2.3