From 5a3f5582baa9666902b39eafdf63e65e20718547 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Tue, 23 Nov 2021 14:17:05 +0100 Subject: [PATCH] LibCore: Add try_create(...) helper to all Core::Object derived classes This is like construct(...) but returns ErrorOr>. --- Userland/Libraries/LibCore/Object.h | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibCore/Object.h b/Userland/Libraries/LibCore/Object.h index 51c35c8e77..cb627ba9a3 100644 --- a/Userland/Libraries/LibCore/Object.h +++ b/Userland/Libraries/LibCore/Object.h @@ -63,14 +63,18 @@ enum class TimerShouldFireWhenNotVisible { Yes }; -#define C_OBJECT(klass) \ -public: \ - virtual const char* class_name() const override { return #klass; } \ - template \ - static inline NonnullRefPtr construct(Args&&... args) \ - { \ - auto obj = adopt_ref(*new Klass(forward(args)...)); \ - return obj; \ +#define C_OBJECT(klass) \ +public: \ + virtual const char* class_name() const override { return #klass; } \ + template \ + static NonnullRefPtr construct(Args&&... args) \ + { \ + return adopt_ref(*new Klass(forward(args)...)); \ + } \ + template \ + static ErrorOr> try_create(Args&&... args) \ + { \ + return adopt_nonnull_ref_or_enomem(new (nothrow) Klass(forward(args)...)); \ } #define C_OBJECT_ABSTRACT(klass) \