瀏覽代碼

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.
Ali Mohammad Pur 4 年之前
父節點
當前提交
20c066b8e0
共有 1 個文件被更改,包括 5 次插入2 次删除
  1. 5 2
      Userland/Libraries/LibCore/Object.h

+ 5 - 2
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) \