Explorar el Código

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 hace 4 años
padre
commit
20c066b8e0
Se han modificado 1 ficheros con 5 adiciones y 2 borrados
  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)                                                \
 #define C_OBJECT(klass)                                                \
 public:                                                                \
 public:                                                                \
     virtual const char* class_name() const override { return #klass; } \
     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)       \
     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) \
 #define C_OBJECT_ABSTRACT(klass) \