From ac9cd26367d42c8f54f08bab948ac9303f8a8521 Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Tue, 1 Feb 2022 21:20:13 -0600 Subject: [PATCH] [ox/std] Make oxAssert sort of usable in constexpr --- deps/ox/src/ox/std/assert.cpp | 49 ++++++-------------------- deps/ox/src/ox/std/assert.hpp | 64 ++++++++++++++++++++++++++++++---- deps/ox/src/ox/std/fmt.hpp | 2 +- deps/ox/src/ox/std/realstd.hpp | 15 ++++++++ deps/ox/src/ox/std/std.hpp | 1 + 5 files changed, 84 insertions(+), 47 deletions(-) create mode 100644 deps/ox/src/ox/std/realstd.hpp diff --git a/deps/ox/src/ox/std/assert.cpp b/deps/ox/src/ox/std/assert.cpp index d3127b31..27f0c53e 100644 --- a/deps/ox/src/ox/std/assert.cpp +++ b/deps/ox/src/ox/std/assert.cpp @@ -17,53 +17,24 @@ namespace ox { -void assertFunc([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]bool pass, [[maybe_unused]]const char *assertTxt, [[maybe_unused]]const char *msg) noexcept { - if (!pass) { -#if defined(OX_USE_STDLIB) - std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m " << assertTxt << " [" << file << ':' << line << "]: " << msg << std::endl; - printStackTrace(2); - oxTrace("assert").del("") << "Failed assert: " << msg << " (" << assertTxt << ") " << " [" << file << ":" << line << "]"; - std::abort(); -#else - panic(file, line, msg); -#endif - } -} - -void assertFunc(const char *file, int line, const Error &err, const char*, const char *assertMsg) noexcept { - if (err) { -#if defined(OX_USE_STDLIB) - std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << assertMsg << '\n'; +void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *panicMsg, [[maybe_unused]]const Error &err) noexcept { +#ifdef OX_USE_STDLIB + if (!std::is_constant_evaluated()) { + std::cerr << "\033[31;1;1mPANIC:\033[0m (" << file << ':' << line << "): " << panicMsg << '\n'; if (err.msg) { - std::cerr << "\tError Message:\t" << err.msg << '\n'; + std::cerr << "\tError Message:\t" << err.msg << '\n'; } - std::cerr << "\tError Code:\t" << err << '\n'; + std::cerr << "\tError Code:\t" << err << '\n'; if (err.file != nullptr) { std::cerr << "\tError Location:\t" << err.file << ':' << err.line << '\n'; } printStackTrace(2); - oxTrace("panic").del("") << "Panic: " << assertMsg << " [" << file << ":" << line << "]"; + oxTrace("panic").del("") << "Panic: " << panicMsg << " (" << file << ":" << line << ")"; std::abort(); -#else - panic(file, line, assertMsg); -#endif + } else { + assert(false); } +#endif } -#if defined(OX_USE_STDLIB) -void panic(const char *file, int line, const char *panicMsg, const Error &err) noexcept { - std::cerr << "\033[31;1;1mPANIC:\033[0m (" << file << ':' << line << "): " << panicMsg << '\n'; - if (err.msg) { - std::cerr << "\tError Message:\t" << err.msg << '\n'; - } - std::cerr << "\tError Code:\t" << err << '\n'; - if (err.file != nullptr) { - std::cerr << "\tError Location:\t" << err.file << ':' << err.line << '\n'; - } - printStackTrace(2); - oxTrace("panic").del("") << "Panic: " << panicMsg << " (" << file << ":" << line << ")"; - std::abort(); -} -#endif - } diff --git a/deps/ox/src/ox/std/assert.hpp b/deps/ox/src/ox/std/assert.hpp index 666f1ab9..7eb15b4f 100644 --- a/deps/ox/src/ox/std/assert.hpp +++ b/deps/ox/src/ox/std/assert.hpp @@ -8,22 +8,72 @@ #pragma once -#if __has_include() -#include -#else -#define assert(e) while (1); +#if defined(OX_USE_STDLIB) +#include #endif #include "def.hpp" #include "defines.hpp" #include "error.hpp" +#include "realstd.hpp" +#include "stacktrace.hpp" +#include "trace.hpp" +#include "typetraits.hpp" namespace ox { -void assertFunc(const char *file, int line, bool pass, const char *assertTxt, const char *msg) noexcept; +void panic(const char *file, int line, const char *panicMsg, const Error &err = OxError(0)) noexcept; -void assertFunc(const char *file, int line, const Error &err, const char *assertTxt, const char *msg) noexcept; +constexpr void constexprPanic(const char *file, int line, const char *panicMsg, const Error &err = OxError(0)) noexcept { + if (!std::is_constant_evaluated()) { + panic(file, line, panicMsg, err); + } else { + while (true); + } +} -void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *msg, [[maybe_unused]]const Error &err = OxError(0)) noexcept; +constexpr void assertFunc(const char *file, int line, bool pass, [[maybe_unused]]const char *assertTxt, [[maybe_unused]]const char *msg) noexcept { + if (!pass) { + if (!std::is_constant_evaluated()) { +#ifdef OX_USE_STDLIB + std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m " << assertTxt << " [" << file << ':' << line << "]: " + << msg << std::endl; + printStackTrace(2); + oxTrace("assert").del("") << "Failed assert: " << msg << " (" << assertTxt << ") " << " [" << file + << ":" + << line << "]"; + std::abort(); +#else + constexprPanic(file, line, msg); +#endif + } else { + while (true); + } + } +} + +constexpr void assertFunc(const char *file, int line, const Error &err, const char*, const char *assertMsg) noexcept { + if (err) { + if (!std::is_constant_evaluated()) { +#if defined(OX_USE_STDLIB) + std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << assertMsg << '\n'; + if (err.msg) { + std::cerr << "\tError Message:\t" << err.msg << '\n'; + } + std::cerr << "\tError Code:\t" << err << '\n'; + if (err.file != nullptr) { + std::cerr << "\tError Location:\t" << err.file << ':' << err.line << '\n'; + } + printStackTrace(2); + oxTrace("constexprPanic").del("") << "Panic: " << assertMsg << " [" << file << ":" << line << "]"; + std::abort(); +#else + constexprPanic(file, line, assertMsg); +#endif + } else { + while (true); + } + } +} } diff --git a/deps/ox/src/ox/std/fmt.hpp b/deps/ox/src/ox/std/fmt.hpp index 4cc62a9f..e808dc7e 100644 --- a/deps/ox/src/ox/std/fmt.hpp +++ b/deps/ox/src/ox/std/fmt.hpp @@ -16,7 +16,7 @@ #include #endif -#include "assert.hpp" +#include "realstd.hpp" #include "error.hpp" #include "bstring.hpp" #include "string.hpp" diff --git a/deps/ox/src/ox/std/realstd.hpp b/deps/ox/src/ox/std/realstd.hpp new file mode 100644 index 00000000..9d916323 --- /dev/null +++ b/deps/ox/src/ox/std/realstd.hpp @@ -0,0 +1,15 @@ +/* + * Copyright 2015 - 2022 gary@drinkingtea.net + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ + +#pragma once + +#if __has_include() +#include +#else +#define assert(e) while (1); +#endif \ No newline at end of file diff --git a/deps/ox/src/ox/std/std.hpp b/deps/ox/src/ox/std/std.hpp index 6ed72cbc..02fa07b1 100644 --- a/deps/ox/src/ox/std/std.hpp +++ b/deps/ox/src/ox/std/std.hpp @@ -27,6 +27,7 @@ #include "new.hpp" #include "optional.hpp" #include "random.hpp" +#include "realstd.hpp" #include "stacktrace.hpp" #include "stddef.hpp" #include "string.hpp"