[ox/std] Move most macros to def.hpp in anticipation of C++20 modules

This commit is contained in:
Gary Talent 2022-01-28 19:57:19 -06:00
parent f10373864d
commit 99550b60ee
7 changed files with 104 additions and 70 deletions

View File

@ -24,7 +24,6 @@ add_library(
buffer.cpp buffer.cpp
buildinfo.cpp buildinfo.cpp
byteswap.cpp byteswap.cpp
defer.hpp
fmt.cpp fmt.cpp
heapmgr.cpp heapmgr.cpp
memops.cpp memops.cpp
@ -71,7 +70,9 @@ install(
buffer.hpp buffer.hpp
buildinfo.hpp buildinfo.hpp
byteswap.hpp byteswap.hpp
def.hpp
defines.hpp defines.hpp
defer.hpp
error.hpp error.hpp
fmt.hpp fmt.hpp
hardware.hpp hardware.hpp

View File

@ -11,9 +11,10 @@
#if __has_include(<cassert>) #if __has_include(<cassert>)
#include <cassert> #include <cassert>
#else #else
#define assert(e) {} #define assert(e) while (1);
#endif #endif
#include "def.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "error.hpp" #include "error.hpp"
@ -26,11 +27,3 @@ void assertFunc(const char *file, int line, const Error &err, const char *assert
void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *msg, [[maybe_unused]]const Error &err = OxError(0)) noexcept; void panic([[maybe_unused]]const char *file, [[maybe_unused]]int line, [[maybe_unused]]const char *msg, [[maybe_unused]]const Error &err = OxError(0)) noexcept;
} }
#define oxPanic(errCode, msg) ox::panic(__FILE__, __LINE__, msg, errCode)
#ifndef NDEBUG
#define oxAssert(pass, msg) ox::assertFunc(__FILE__, __LINE__, pass, #pass, msg)
#else
inline void oxAssert(bool, const char*) {}
inline void oxAssert(ox::Error, const char*) {}
#endif

97
deps/ox/src/ox/std/def.hpp vendored Normal file
View File

@ -0,0 +1,97 @@
/*
* 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
// Logging
#define oxLogError(...) ox::trace::logError(__FILE__, __LINE__, __VA_ARGS__)
#define oxTrace(...) ox::trace::TraceStream(__FILE__, __LINE__, __VA_ARGS__)
#define oxOut(...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", __VA_ARGS__)
#define oxErr(...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", __VA_ARGS__)
#ifdef OX_USE_STDLIB
// Non-GCC compilers don't like ##__VA_ARGS__, so use initializer list, which relies on std lib
#define oxTracef(ch, fmt, ...) ox::trace::TraceStream(__FILE__, __LINE__, ch, ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#define oxOutf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#define oxErrf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#else
#define oxTracef(ch, fmt, ...) ox::trace::TraceStream(__FILE__, __LINE__, ch, ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#define oxOutf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#define oxErrf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#endif
#define oxInfo(...) oxTrace("info", __VA_ARGS__)
#define oxInfof(...) oxTracef("info", __VA_ARGS__)
#define oxError(...) oxTrace("error", __VA_ARGS__)
#define oxErrorf(...) oxTracef("error", __VA_ARGS__)
#ifndef OX_NODEBUG
#define oxDebug(...) oxTrace("debug", __VA_ARGS__)
#define oxDebugf(...) oxTracef("debug", __VA_ARGS__)
#else
#define oxDebug(...) static_assert(false, "Debug prints were checked in."); oxTrace("debug", __VA_ARGS__)
#define oxDebugf(...) static_assert(false, "Debug prints were checked in."); oxTracef("debug", __VA_ARGS__)
#endif
// Error handling
#if __cplusplus >= 202002L
#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error
#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] throw ox::Exception(_ox_error)
#else
#define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error
#define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw ox::Exception(_ox_error)
#endif
#define oxConcatImpl(a, b) a##b
#define oxConcat(a, b) oxConcatImpl(a, b)
// oxRequire Mutable
#define oxRequireM(out, x) auto [out, oxConcat(oxRequire_err_, __LINE__)] = x; oxReturnError(oxConcat(oxRequire_err_, __LINE__))
#define oxRequire(out, x) const oxRequireM(out, x)
// oxRequire Mutable Throw
#define oxRequireMT(out, x) auto [out, oxConcat(oxRequire_err_, __LINE__)] = x; oxThrowError(oxConcat(oxRequire_err_, __LINE__))
// oxRequire Throw
#define oxRequireT(out, x) const oxRequireMT(out, x)
// Asserts
#define oxPanic(errCode, msg) ox::panic(__FILE__, __LINE__, msg, errCode)
#ifndef NDEBUG
#define oxAssert(pass, msg) ox::assertFunc(__FILE__, __LINE__, pass, #pass, msg)
#else
namespace ox {
struct [[nodiscard]] Error;
}
inline void oxAssert(bool, const char*) {}
inline void oxAssert(const ox::Error&, const char*) {}
#endif
// Alloca
#if defined(_MSC_VER)
#define ox_alloca(size) _alloca(size)
#elif OX_USE_STDLIB
#define ox_alloca(size) alloca(size)
#else
#define ox_alloca(size) __builtin_alloca(size)
#endif
/**
* @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation
*/
#if defined(OX_USE_STDLIB)
#define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(size > ox::MallocaStackLimit, new (size > ox::MallocaStackLimit ? new uint8_t[size] : ox_alloca(size)) Type(__VA_ARGS__))
#else
#define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(false, new (ox_alloca(size)) Type(__VA_ARGS__))
#endif

View File

@ -24,6 +24,7 @@ class exception {
} }
#endif #endif
#include "def.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "strongint.hpp" #include "strongint.hpp"
#include "typetraits.hpp" #include "typetraits.hpp"
@ -176,20 +177,3 @@ constexpr Error toError(const Result<T> &ve) noexcept {
constexpr void oxIgnoreError(const ox::Error&) noexcept {} constexpr void oxIgnoreError(const ox::Error&) noexcept {}
template<typename T> template<typename T>
constexpr void oxIgnoreError(const ox::Result<T>&) noexcept {} constexpr void oxIgnoreError(const ox::Result<T>&) noexcept {}
#if __cplusplus >= 202002L
#define oxReturnError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] return _ox_error
#define oxThrowError(x) if (const auto _ox_error = ox::detail::toError(x)) [[unlikely]] throw ox::Exception(_ox_error)
#else
#define oxReturnError(err) if (const auto _ox_error = ox::detail::toError(err)) return _ox_error
#define oxThrowError(err) if (const auto _ox_error = ox::detail::toError(err)) throw ox::Exception(_ox_error)
#endif
#define oxConcatImpl(a, b) a##b
#define oxConcat(a, b) oxConcatImpl(a, b)
// oxRequire Mutable
#define oxRequireM(out, x) auto [out, oxConcat(oxRequire_err_, __LINE__)] = x; oxReturnError(oxConcat(oxRequire_err_, __LINE__))
#define oxRequire(out, x) const oxRequireM(out, x)
// oxRequire Mutable Throw
#define oxRequireMT(out, x) auto [out, oxConcat(oxRequire_err_, __LINE__)] = x; oxThrowError(oxConcat(oxRequire_err_, __LINE__))
// oxRequire Throw
#define oxRequireT(out, x) const oxRequireMT(out, x)

View File

@ -13,12 +13,8 @@
#if defined(_MSC_VER) #if defined(_MSC_VER)
#include <malloc.h> #include <malloc.h>
#define ox_alloca(size) _alloca(size)
#elif OX_USE_STDLIB #elif OX_USE_STDLIB
#include <stdlib.h> #include <stdlib.h>
#define ox_alloca(size) alloca(size)
#else
#define ox_alloca(size) __builtin_alloca(size)
#endif #endif
@ -35,15 +31,6 @@ constexpr void *operator new[](std::size_t, void *addr) noexcept {
#endif #endif
/**
* @return an ox::MallocaPtr of the given type pointing to the requested size memory allocation
*/
#if defined(OX_USE_STDLIB)
#define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(size > ox::MallocaStackLimit, new (size > ox::MallocaStackLimit ? new uint8_t[size] : ox_alloca(size)) Type(__VA_ARGS__))
#else
#define ox_malloca(size, Type, ...) ox::MallocaPtr<Type>(false, new (ox_alloca(size)) Type(__VA_ARGS__))
#endif
namespace ox { namespace ox {
constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0; constexpr auto MallocaStackLimit = defines::UseStdLib ? 1024 : 0;

View File

@ -13,6 +13,7 @@
#include "bstring.hpp" #include "bstring.hpp"
#include "byteswap.hpp" #include "byteswap.hpp"
#include "defer.hpp" #include "defer.hpp"
#include "def.hpp"
#include "defines.hpp" #include "defines.hpp"
#include "error.hpp" #include "error.hpp"
#include "fmt.hpp" #include "fmt.hpp"

View File

@ -13,6 +13,7 @@
#endif #endif
#include "bstring.hpp" #include "bstring.hpp"
#include "def.hpp"
#include "fmt.hpp" #include "fmt.hpp"
#include "hashmap.hpp" #include "hashmap.hpp"
#include "string.hpp" #include "string.hpp"
@ -282,33 +283,3 @@ inline void logError(const char *file, int line, const Error &err) noexcept {
void init(); void init();
} }
#define oxLogError(...) ox::trace::logError(__FILE__, __LINE__, __VA_ARGS__)
#define oxTrace(...) ox::trace::TraceStream(__FILE__, __LINE__, __VA_ARGS__)
#define oxOut(...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", __VA_ARGS__)
#define oxErr(...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", __VA_ARGS__)
#ifdef OX_USE_STDLIB
// Non-GCC compilers don't like ##__VA_ARGS__, so use initializer list, which relies on std lib
#define oxTracef(ch, fmt, ...) ox::trace::TraceStream(__FILE__, __LINE__, ch, ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#define oxOutf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#define oxErrf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), {__VA_ARGS__})
#else
#define oxTracef(ch, fmt, ...) ox::trace::TraceStream(__FILE__, __LINE__, ch, ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#define oxOutf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stdout", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#define oxErrf(fmt, ...) ox::trace::OutStream(__FILE__, __LINE__, "stderr", ox::detail::fmtSegments<ox::detail::argCount(fmt)+1>(fmt), ##__VA_ARGS__)
#endif
#define oxInfo(...) oxTrace("info", __VA_ARGS__)
#define oxInfof(...) oxTracef("info", __VA_ARGS__)
#define oxError(...) oxTrace("error", __VA_ARGS__)
#define oxErrorf(...) oxTracef("error", __VA_ARGS__)
#ifndef OX_NODEBUG
#define oxDebug(...) oxTrace("debug", __VA_ARGS__)
#define oxDebugf(...) oxTracef("debug", __VA_ARGS__)
#else
#define oxDebug(...) static_assert(false, "Debug prints were checked in."); oxTrace("debug", __VA_ARGS__)
#define oxDebugf(...) static_assert(false, "Debug prints were checked in."); oxTracef("debug", __VA_ARGS__)
#endif