mirror of
https://github.com/bolero-MURAKAMI/Sprout
synced 2025-08-03 12:49:50 +00:00
add variadic assert macro
This commit is contained in:
parent
15c3f55ebb
commit
23be867d87
7 changed files with 307 additions and 93 deletions
|
@ -10,12 +10,21 @@
|
|||
|
||||
#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)
|
||||
# include <sprout/config.hpp>
|
||||
# include <sprout/preprocessor/config.hpp>
|
||||
# include <sprout/preprocessor/cat.hpp>
|
||||
# include <sprout/preprocessor/variadic/size.hpp>
|
||||
#elif defined(SPROUT_ENABLE_ASSERT_HANDLER)
|
||||
# include <sprout/config.hpp>
|
||||
# include <sprout/preprocessor/config.hpp>
|
||||
# include <sprout/preprocessor/cat.hpp>
|
||||
# include <sprout/preprocessor/variadic/size.hpp>
|
||||
#else
|
||||
# include <cstdlib>
|
||||
# include <iostream>
|
||||
# include <sprout/config.hpp>
|
||||
# include <sprout/preprocessor/config.hpp>
|
||||
# include <sprout/preprocessor/cat.hpp>
|
||||
# include <sprout/preprocessor/variadic/size.hpp>
|
||||
#endif
|
||||
#if !(defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG))
|
||||
# include <sprout/preprocessor/stringize.hpp>
|
||||
|
@ -29,99 +38,6 @@
|
|||
"***** Internal Program Error - assertion (" #expr ") failed: " file "(" SPROUT_PP_STRINGIZE(line) ")"
|
||||
#endif
|
||||
|
||||
//
|
||||
// SPROUT_ASSERT
|
||||
//
|
||||
|
||||
#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)
|
||||
|
||||
# define SPROUT_ASSERT(expr) \
|
||||
((void)0)
|
||||
|
||||
#elif defined(SPROUT_ENABLE_ASSERT_HANDLER)
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// assertion_info
|
||||
//
|
||||
class assertion_info {
|
||||
private:
|
||||
char const* expr_;
|
||||
char const* function_;
|
||||
char const* file_;
|
||||
long line_;
|
||||
public:
|
||||
SPROUT_CONSTEXPR assertion_info(char const* expr, char const* function, char const* file, long line)
|
||||
: expr_(expr), function_(function), file_(file), line_(line)
|
||||
{}
|
||||
SPROUT_CONSTEXPR char const* expr() const SPROUT_NOEXCEPT {
|
||||
return expr_;
|
||||
}
|
||||
SPROUT_CONSTEXPR char const* function() const SPROUT_NOEXCEPT {
|
||||
return function_;
|
||||
}
|
||||
SPROUT_CONSTEXPR char const* file() const SPROUT_NOEXCEPT {
|
||||
return file_;
|
||||
}
|
||||
SPROUT_CONSTEXPR long line() const SPROUT_NOEXCEPT {
|
||||
return line_;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// assertion_failed
|
||||
// * user defined
|
||||
//
|
||||
void
|
||||
assertion_failed(sprout::assertion_info const&);
|
||||
} // namespace sprout
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
inline bool
|
||||
assertion_failed(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) {
|
||||
return cond ? true
|
||||
: ((void)sprout::assertion_failed(sprout::assertion_info(expr, function, file, line)), false)
|
||||
;
|
||||
}
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
assertion_check(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) {
|
||||
return cond ? true
|
||||
: sprout::detail::assertion_failed(cond, formatted, expr, function, file, line)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
# define SPROUT_ASSERT(expr) ( \
|
||||
(void)sprout::detail::assertion_check( \
|
||||
(expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), \
|
||||
#expr, "(unknown)"/*SPROUT_CURRENT_FUNCTION*/, __FILE__, __LINE__ \
|
||||
) \
|
||||
)
|
||||
|
||||
#else
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
inline bool
|
||||
assertion_failed(char const* formatted) {
|
||||
return (std::cerr << formatted << std::endl), std::abort(), false;
|
||||
}
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
assertion_check(bool cond, char const* formatted) {
|
||||
return cond ? true
|
||||
: sprout::detail::assertion_failed(formatted)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
# define SPROUT_ASSERT(expr) \
|
||||
((void)sprout::detail::assertion_check((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__)))
|
||||
|
||||
#endif
|
||||
|
||||
//
|
||||
// SPROUT_ASSERT_MSG
|
||||
//
|
||||
|
@ -219,6 +135,109 @@ namespace sprout {
|
|||
|
||||
#endif
|
||||
|
||||
//
|
||||
// SPROUT_ASSERT
|
||||
//
|
||||
|
||||
#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)
|
||||
|
||||
# define SPROUT_ASSERT_1(expr) \
|
||||
((void)0)
|
||||
|
||||
#elif defined(SPROUT_ENABLE_ASSERT_HANDLER)
|
||||
|
||||
namespace sprout {
|
||||
//
|
||||
// assertion_info
|
||||
//
|
||||
class assertion_info {
|
||||
private:
|
||||
char const* expr_;
|
||||
char const* function_;
|
||||
char const* file_;
|
||||
long line_;
|
||||
public:
|
||||
SPROUT_CONSTEXPR assertion_info(char const* expr, char const* function, char const* file, long line)
|
||||
: expr_(expr), function_(function), file_(file), line_(line)
|
||||
{}
|
||||
SPROUT_CONSTEXPR char const* expr() const SPROUT_NOEXCEPT {
|
||||
return expr_;
|
||||
}
|
||||
SPROUT_CONSTEXPR char const* function() const SPROUT_NOEXCEPT {
|
||||
return function_;
|
||||
}
|
||||
SPROUT_CONSTEXPR char const* file() const SPROUT_NOEXCEPT {
|
||||
return file_;
|
||||
}
|
||||
SPROUT_CONSTEXPR long line() const SPROUT_NOEXCEPT {
|
||||
return line_;
|
||||
}
|
||||
};
|
||||
|
||||
//
|
||||
// assertion_failed
|
||||
// * user defined
|
||||
//
|
||||
void
|
||||
assertion_failed(sprout::assertion_info const&);
|
||||
} // namespace sprout
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
inline bool
|
||||
assertion_failed(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) {
|
||||
return cond ? true
|
||||
: ((void)sprout::assertion_failed(sprout::assertion_info(expr, function, file, line)), false)
|
||||
;
|
||||
}
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
assertion_check(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) {
|
||||
return cond ? true
|
||||
: sprout::detail::assertion_failed(cond, formatted, expr, function, file, line)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
# define SPROUT_ASSERT_1(expr) \
|
||||
(void)sprout::detail::assertion_check( \
|
||||
(expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), \
|
||||
#expr, "(unknown)"/*SPROUT_CURRENT_FUNCTION*/, __FILE__, __LINE__ \
|
||||
) \
|
||||
)
|
||||
|
||||
#else
|
||||
|
||||
namespace sprout {
|
||||
namespace detail {
|
||||
inline bool
|
||||
assertion_failed(char const* formatted) {
|
||||
return (std::cerr << formatted << std::endl), std::abort(), false;
|
||||
}
|
||||
inline SPROUT_CONSTEXPR bool
|
||||
assertion_check(bool cond, char const* formatted) {
|
||||
return cond ? true
|
||||
: sprout::detail::assertion_failed(formatted)
|
||||
;
|
||||
}
|
||||
} // namespace detail
|
||||
} // namespace sprout
|
||||
|
||||
# define SPROUT_ASSERT_1(expr) \
|
||||
((void)sprout::detail::assertion_check((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__)))
|
||||
|
||||
#endif
|
||||
|
||||
#if SPROUT_PP_VARIADICS
|
||||
# define SPROUT_ASSERT_2(expr, msg) \
|
||||
SPROUT_ASSERT_MSG(expr, msg)
|
||||
# define SPROUT_ASSERT(__VA_ARGS__) \
|
||||
SPROUT_PP_CAT(SPROUT_ASSERT_, SPROUT_PP_VARIADIC_SIZE(__VA_ARGS__))(__VA_ARGS__)
|
||||
#else
|
||||
# define SPROUT_ASSERT(expr) \
|
||||
SPROUT_ASSERT_1(expr)
|
||||
#endif
|
||||
|
||||
//
|
||||
// SPROUT_VERIFY
|
||||
//
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue