diff options
author | Ali Mohammad Pur <mpfard@serenityos.org> | 2021-06-18 16:56:52 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2021-06-20 14:57:26 +0200 |
commit | 20c066b8e0d9c2097cd9a46fc7e56b92fc300255 (patch) | |
tree | fb19ef5d66d89be3feed0d063392ce2c35e9a7a3 /Userland/Libraries | |
parent | ade85d980b54575be56620a885b1dd6844128a63 (diff) | |
download | serenity-20c066b8e0d9c2097cd9a46fc7e56b92fc300255.zip |
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.
Diffstat (limited to 'Userland/Libraries')
-rw-r--r-- | Userland/Libraries/LibCore/Object.h | 7 |
1 files changed, 5 insertions, 2 deletions
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<class... Args> \ + template<typename Klass = klass, class... Args> \ static inline NonnullRefPtr<klass> construct(Args&&... args) \ { \ - return adopt_ref(*new klass(forward<Args>(args)...)); \ + auto obj = adopt_ref(*new Klass(forward<Args>(args)...)); \ + if constexpr (requires { declval<Klass>().did_construct(); }) \ + obj->did_construct(); \ + return obj; \ } #define C_OBJECT_ABSTRACT(klass) \ |