Skip to content

Commit

Permalink
__has_include, std::bit_cast, std::source_location
Browse files Browse the repository at this point in the history
  • Loading branch information
aremmell committed Jan 7, 2024
1 parent fbea170 commit 731b138
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 17 deletions.
21 changes: 14 additions & 7 deletions include/bal.hh
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,6 @@
#ifndef _BAL_HH_INCLUDED
# define _BAL_HH_INCLUDED

# if defined(__has_include)
# define __HAS_INCLUDE(hdr) __has_include(hdr)
# else
# define __HAS_INCLUDE(hdr) false
# endif

# include "bal.h"
# include <type_traits>
# include <functional>
Expand All @@ -41,7 +35,20 @@
# include <vector>
# include <atomic>
# include <string>
# if __HAS_INCLUDE(<bit>)
# include <version>

# if defined(__has_include)
# define __HAS_INCLUDE(hdr) __has_include(hdr)
# else
# define __HAS_INCLUDE(hdr) false
# endif

# if defined(__cpp_lib_source_location) && __HAS_INCLUDE(<source_location>)
# include <source_location>
# define source_location std::source_location
# endif

# if defined(__cpp_lib_bit_cast) && __HAS_INCLUDE(<bit>)
# include <bit>
# define bit_cast std::bit_cast
# else
Expand Down
5 changes: 2 additions & 3 deletions include/bal/errors.h
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,10 @@ bool __bal_validate(bool expr, int err, const char* func, const char* file,
# if defined(BAL_DBGLOG)
void __bal_dbglog(const char* func, const char* file, uint32_t line,
const char* format, ...);
# if defined(__cplusplus) && __HAS_INCLUDE(<source_location>)
# include <source_location>
# if defined(__cplusplus) && defined(source_location)
# define _bal_dbglog(...) \
do { \
std::source_location loc = std::source_location::current(); \
source_location loc = source_location::current(); \
__bal_dbglog(loc.function_name(), loc.file_name(), loc.line(), __VA_ARGS__); \
} while (false)
# else
Expand Down
31 changes: 24 additions & 7 deletions tests/tests++.hh
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
# define _BAL_TESTSXX_HH_INCLUDED

# include <bal.hh>
# include <source_location>
# include "tests_shared.h"

/**
Expand Down Expand Up @@ -71,16 +70,34 @@ namespace bal::tests
try { \
initializer balinit;

/** Emits an error message when an unexpected exception is caught. */
# define _BAL_TEST_LOG_UNEXPECTED_EXCEPTION(func, what) \
ERROR_MSG("unexpected exception in %s: '%s'", (func), (what)); \
pass = false

/** Emits a message when an expected exception is caught. */
# define _BAL_TEST_LOG_EXPECTED_EXCEPTION(func, what) \
TEST_MSG(GREEN("expected exception in %s: '%s'"), (func), (what))

/** Implements recovery in the event that an unexpected exception is caught. */
# define _BAL_TEST_ON_EXCEPTION(ex) \
std::source_location loc = std::source_location::current(); \
ERROR_MSG("unexpected exception in %s: '%s'", loc.function_name(), ex.what()); \
pass = false
# if defined(source_location)
# define _BAL_TEST_ON_EXCEPTION(ex) \
source_location loc = source_location::current(); \
_BAL_TEST_LOG_UNEXPECTED_EXCEPTION(loc.function_name(), ex.what())
# else
# define _BAL_TEST_ON_EXCEPTION(ex) \
_BAL_TEST_LOG_UNEXPECTED_EXCEPTION(__func__, ex.what())
# endif

/** Handles an expected exception. */
# if defined(source_location)
# define _BAL_TEST_ON_EXPECTED_EXCEPTION(ex) \
source_location loc = source_location::current(); \
_BAL_TEST_LOG_EXPECTED_EXCEPTION(loc.function_name(), ex.what())
# else
# define _BAL_TEST_ON_EXPECTED_EXCEPTION(ex) \
std::source_location loc = std::source_location::current(); \
TEST_MSG(GREEN("expected exception in %s: '%s'"), loc.function_name(), ex.what())
_BAL_TEST_LOG_EXPECTED_EXCEPTION(__func__, ex.what())
# endif

# define _BAL_TEST_CONCLUDE \
} catch (bal::exception& ex) { \
Expand Down

0 comments on commit 731b138

Please sign in to comment.