[ox/std] Fix packed errors

This commit is contained in:
Gary Talent 2018-05-28 20:07:23 -05:00
parent 0da80081f3
commit 8e7fb4394b
4 changed files with 53 additions and 14 deletions

View File

@ -6,17 +6,19 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/ */
#if defined(OX_USE_STDLIB)
#include <bitset>
#include <iostream>
#endif
#include <ox/__buildinfo/defines.hpp> #include <ox/__buildinfo/defines.hpp>
#if defined(OX_USE_STDLIB) #include "assert.hpp"
#include <iostream>
#endif
namespace ox { namespace ox {
void _assert([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]bool pass, [[maybe_unused]]const char *msg) { template<>
void _assert<bool>([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]bool pass, [[maybe_unused]]const char *msg) {
#if defined(OX_USE_STDLIB) #if defined(OX_USE_STDLIB)
if (!pass) { if (!pass) {
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << std::endl; std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << std::endl;
@ -25,4 +27,20 @@ void _assert([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe
#endif #endif
} }
template<>
void _assert<Error>([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]Error err, [[maybe_unused]]const char *msg) {
#if defined(OX_USE_STDLIB)
if (err) {
auto ei = ErrorInfo(err);
std::cerr << "\033[31;1;1mASSERT FAILURE:\033[0m (" << file << ':' << line << "): " << msg << '\n';
std::cerr << "\tError Info: " << ei.errCode;
if (ei.file != nullptr) {
std::cerr << " (" << reinterpret_cast<const char*>(ei.file) << ':' << ei.line << ')';
}
std::cerr << std::endl;
std::abort();
}
#endif
}
} }

View File

@ -8,14 +8,30 @@
#pragma once #pragma once
#if defined(OX_USE_STDLIB)
#include <iostream>
#endif
#include <ox/__buildinfo/defines.hpp>
#include "types.hpp"
namespace ox { namespace ox {
void _assert(const char *file, int line, bool pass, const char *msg); template<typename T>
void _assert(const char*, int, T, const char*) {
}
template<>
void _assert<bool>(const char *file, int line, bool pass, const char *msg);
template<>
void _assert<Error>(const char *file, int line, Error err, const char*);
} }
#ifndef NDEBUG #ifndef NDEBUG
#define oxAssert(pass, msg) ox::_assert(__FILE__, __LINE__, pass, msg) #define oxAssert(pass, msg) ox::_assert<decltype(pass)>(__FILE__, __LINE__, pass, msg)
#else #else
#define oxAssert(pass, msg) #define oxAssert(pass, msg)
#endif #endif

View File

@ -20,9 +20,14 @@ template<typename T>
constexpr T onMask(int bits) { constexpr T onMask(int bits) {
T out = 0; T out = 0;
for (auto i = 0; i < bits; i++) { for (auto i = 0; i < bits; i++) {
out |= 1 << i; out |= static_cast<T>(1) << i;
} }
return out; return out;
} }
static_assert(onMask<int>(1) == 1);
static_assert(onMask<int>(2) == 3);
static_assert(onMask<int>(3) == 7);
static_assert(onMask<int>(4) == 15);
} }

View File

@ -65,15 +65,15 @@ struct ErrorInfo {
ErrorInfo(Error err) { ErrorInfo(Error err) {
this->file = reinterpret_cast<const char*>(err & onMask<Error>(48)); this->file = reinterpret_cast<const char*>(err & onMask<Error>(48));
this->line = static_cast<int>((err >> 48) & onMask<Error>(5)); this->line = static_cast<int>((err >> 48) & onMask<Error>(11));
this->errCode = (err >> 59) & onMask<Error>(11); this->errCode = (err >> 58) & onMask<Error>(5);
} }
}; };
constexpr Error ErrorTags(Error line, Error errCode) { constexpr Error ErrorTags(Error line, Error errCode) {
line &= onMask<Error>(5); line &= onMask<Error>(11);
line <<= 48; line <<= 48;
errCode &= onMask<Error>(11); errCode &= onMask<Error>(5);
errCode <<= 59; errCode <<= 59;
return errCode | line; return errCode | line;
} }
@ -81,7 +81,7 @@ constexpr Error ErrorTags(Error line, Error errCode) {
} }
#ifdef DEBUG #ifdef DEBUG
#define OxError(x) reinterpret_cast<uint64_t>(__FILE__) | ErrorTags(__LINE__, x) #define OxError(x) x ? reinterpret_cast<uint64_t>(__FILE__) | ErrorTags(__LINE__, x) : 0
#else #else
#define OxError(x) x #define OxError(x) x
#endif #endif