[ox/std] Make oxAssert sort of usable in constexpr

This commit is contained in:
Gary Talent 2022-02-01 21:20:13 -06:00
parent 180cac5cc6
commit ac9cd26367
5 changed files with 84 additions and 47 deletions

View File

@ -17,53 +17,24 @@
namespace ox { 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 { void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *panicMsg, [[maybe_unused]]const Error &err) noexcept {
if (!pass) { #ifdef OX_USE_STDLIB
#if defined(OX_USE_STDLIB) if (!std::is_constant_evaluated()) {
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m " << assertTxt << " [" << file << ':' << line << "]: " << msg << std::endl; std::cerr << "\033[31;1;1mPANIC:\033[0m (" << file << ':' << line << "): " << panicMsg << '\n';
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';
if (err.msg) { 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) { if (err.file != nullptr) {
std::cerr << "\tError Location:\t" << err.file << ':' << err.line << '\n'; std::cerr << "\tError Location:\t" << err.file << ':' << err.line << '\n';
} }
printStackTrace(2); printStackTrace(2);
oxTrace("panic").del("") << "Panic: " << assertMsg << " [" << file << ":" << line << "]"; oxTrace("panic").del("") << "Panic: " << panicMsg << " (" << file << ":" << line << ")";
std::abort(); std::abort();
#else } else {
panic(file, line, assertMsg); assert(false);
#endif
} }
#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
} }

View File

@ -8,22 +8,72 @@
#pragma once #pragma once
#if __has_include(<cassert>) #if defined(OX_USE_STDLIB)
#include <cassert> #include <iostream>
#else
#define assert(e) while (1);
#endif #endif
#include "def.hpp" #include "def.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "error.hpp" #include "error.hpp"
#include "realstd.hpp"
#include "stacktrace.hpp"
#include "trace.hpp"
#include "typetraits.hpp"
namespace ox { 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);
}
}
}
} }

View File

@ -16,7 +16,7 @@
#include <QString> #include <QString>
#endif #endif
#include "assert.hpp" #include "realstd.hpp"
#include "error.hpp" #include "error.hpp"
#include "bstring.hpp" #include "bstring.hpp"
#include "string.hpp" #include "string.hpp"

15
deps/ox/src/ox/std/realstd.hpp vendored Normal file
View File

@ -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(<cassert>)
#include <cassert>
#else
#define assert(e) while (1);
#endif

View File

@ -27,6 +27,7 @@
#include "new.hpp" #include "new.hpp"
#include "optional.hpp" #include "optional.hpp"
#include "random.hpp" #include "random.hpp"
#include "realstd.hpp"
#include "stacktrace.hpp" #include "stacktrace.hpp"
#include "stddef.hpp" #include "stddef.hpp"
#include "string.hpp" #include "string.hpp"