From 839a791ddd8033312943e38ee2f40463906e2ac2 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sat, 17 Dec 2022 13:49:53 -0600 Subject: [PATCH] [ox/std] Add ox::make to wrap new in exception handling --- deps/ox/src/ox/std/new.hpp | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/deps/ox/src/ox/std/new.hpp b/deps/ox/src/ox/std/new.hpp index 85a7b466..c8084ca8 100644 --- a/deps/ox/src/ox/std/new.hpp +++ b/deps/ox/src/ox/std/new.hpp @@ -8,13 +8,15 @@ #pragma once +#include "error.hpp" #include "defines.hpp" #include "types.hpp" +#include "utility.hpp" #if defined(_MSC_VER) #include #elif OX_USE_STDLIB -#include +#include #endif @@ -33,6 +35,21 @@ constexpr void *operator new[](std::size_t, void *addr) noexcept { namespace ox { +template +[[nodiscard]] +constexpr T *make(Args &&...args) noexcept { +#ifdef __cpp_exceptions + try { + return new T(ox::forward(args)...); + } catch (...) { + oxPanic(OxError(1, "Allocation or constructor failed"), "Allocation or constructor failed"); + return nullptr; + } +#else + return new T(ox::forward(args)...); +#endif +} + constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0; /**