Przeglądaj źródła

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 lat temu
rodzic
commit
20c066b8e0
1 zmienionych plików z 5 dodań i 2 usunięć
  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) \