[ox/std] Add ox::make to wrap new in exception handling

This commit is contained in:
Gary Talent 2022-12-17 13:49:53 -06:00
parent 0d040ad752
commit 839a791ddd

View File

@ -8,13 +8,15 @@
#pragma once #pragma once
#include "error.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "types.hpp" #include "types.hpp"
#include "utility.hpp"
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <malloc.h> #include <malloc.h>
#elif OX_USE_STDLIB #elif OX_USE_STDLIB
#include <stdlib.h> #include <cstdlib>
#endif #endif
@ -33,6 +35,21 @@ constexpr void *operator new[](std::size_t, void *addr) noexcept {
namespace ox { namespace ox {
template<typename T, typename ...Args>
[[nodiscard]]
constexpr T *make(Args &&...args) noexcept {
#ifdef __cpp_exceptions
try {
return new T(ox::forward<Args>(args)...);
} catch (...) {
oxPanic(OxError(1, "Allocation or constructor failed"), "Allocation or constructor failed");
return nullptr;
}
#else
return new T(ox::forward<Args>(args)...);
#endif
}
constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0; constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0;
/** /**