From e6ea8b581e2c7df65c5adb1261e1d59692169ffe Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 30 May 2023 20:49:57 -0500 Subject: [PATCH] [ox] Add makeCatch function (synced from 90ef5866dd309d89fe5406968f97f248852931b5) --- src/ox/std/new.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/ox/std/new.hpp b/src/ox/std/new.hpp index c8084ca82..369311f8c 100644 --- a/src/ox/std/new.hpp +++ b/src/ox/std/new.hpp @@ -50,6 +50,22 @@ constexpr T *make(Args &&...args) noexcept { #endif } +template +[[nodiscard]] +constexpr Result makeCatch(Args &&...args) noexcept { +#ifdef __cpp_exceptions + try { + return new T(ox::forward(args)...); + } catch (const ox::Exception &ex) { + return ex.toError(); + } catch (...) { + return OxError(1, "Allocation or constructor failed"); + } +#else + return new T(ox::forward(args)...); +#endif +} + constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0; /**