Sprout/sprout/assert.hpp

285 lines
8.1 KiB
C++
Raw Normal View History

2013-08-08 09:54:33 +00:00
/*=============================================================================
Copyright (c) 2011-2018 Bolero MURAKAMI
2013-08-08 09:54:33 +00:00
https://github.com/bolero-MURAKAMI/Sprout
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
2013-03-18 10:12:21 +00:00
#ifndef SPROUT_ASEERT_HPP
#define SPROUT_ASEERT_HPP
#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)
# include <sprout/config.hpp>
2014-07-03 14:18:11 +00:00
# include <sprout/preprocessor/config.hpp>
# include <sprout/preprocessor/cat.hpp>
# include <sprout/preprocessor/variadic/size.hpp>
2013-03-18 10:12:21 +00:00
#elif defined(SPROUT_ENABLE_ASSERT_HANDLER)
# include <sprout/config.hpp>
2014-07-03 14:18:11 +00:00
# include <sprout/preprocessor/config.hpp>
# include <sprout/preprocessor/cat.hpp>
# include <sprout/preprocessor/variadic/size.hpp>
2013-03-18 10:12:21 +00:00
#else
# include <cstdlib>
# include <iostream>
# include <sprout/config.hpp>
2014-07-03 14:18:11 +00:00
# include <sprout/preprocessor/config.hpp>
# include <sprout/preprocessor/cat.hpp>
# include <sprout/preprocessor/variadic/size.hpp>
2013-03-18 10:12:21 +00:00
#endif
#include <sprout/preprocessor/stringize.hpp>
2013-03-18 10:12:21 +00:00
//
// SPROUT_ASSERTION_FAILED_FORMAT
//
#ifndef SPROUT_ASSERTION_FAILED_FORMAT
# define SPROUT_ASSERTION_FAILED_FORMAT(expr, file, line) \
"***** Internal Program Error - assertion (" #expr ") failed: " file "(" SPROUT_PP_STRINGIZE(line) ")"
2013-03-18 10:12:21 +00:00
#endif
//
2014-07-03 14:18:11 +00:00
// SPROUT_ASSERT_MSG
2013-03-18 10:12:21 +00:00
//
#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)
2014-07-03 14:18:11 +00:00
# define SPROUT_ASSERT_MSG(expr, msg) \
2013-03-19 07:32:48 +00:00
((void)0)
2013-03-18 10:12:21 +00:00
#elif defined(SPROUT_ENABLE_ASSERT_HANDLER)
2013-03-19 17:21:25 +00:00
namespace sprout {
//
2014-07-03 14:18:11 +00:00
// assertion_info_msg
2013-03-19 17:21:25 +00:00
//
2014-07-03 14:18:11 +00:00
class assertion_info_msg {
2013-03-19 17:21:25 +00:00
private:
char const* expr_;
2014-07-03 14:18:11 +00:00
char const* msg_;
2013-03-19 17:21:25 +00:00
char const* function_;
char const* file_;
long line_;
public:
2014-07-03 14:18:11 +00:00
SPROUT_CONSTEXPR assertion_info_msg(char const* expr, char const* msg, char const* function, char const* file, long line)
: expr_(expr), msg_(msg), function_(function), file_(file), line_(line)
2013-03-19 17:21:25 +00:00
{}
SPROUT_CONSTEXPR char const* expr() const SPROUT_NOEXCEPT {
return expr_;
}
2014-07-03 14:18:11 +00:00
SPROUT_CONSTEXPR char const* msg() const SPROUT_NOEXCEPT {
return msg_;
}
2013-03-19 17:21:25 +00:00
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_;
}
};
2013-03-18 10:12:21 +00:00
2017-09-29 14:17:17 +00:00
//
// assertion_failed_msg_default
//
inline SPROUT_NON_CONSTEXPR bool
assertion_failed_default(sprout::assertion_info_msg const& info) {
return (std::cerr << "***** Internal Program Error - assertion (" << info.expr() << ") failed: " << info.file() << "(" << info.line() << ")" << ": " << msg << std::endl),
std::abort(), false
;
}
2013-03-19 17:21:25 +00:00
//
2014-07-03 14:18:11 +00:00
// assertion_failed_msg
2013-03-19 17:21:25 +00:00
// * user defined
//
void
2014-07-03 14:18:11 +00:00
assertion_failed_msg(sprout::assertion_info_msg const&);
} // namespace sprout
2013-03-18 10:12:21 +00:00
namespace sprout {
namespace detail {
2013-03-19 17:21:25 +00:00
inline bool
2014-07-03 14:18:11 +00:00
assertion_failed_msg(bool cond, char const* formatted, char const* expr, char const* msg, char const* function, char const* file, long line) {
2013-03-19 17:21:25 +00:00
return cond ? true
2014-07-03 14:18:11 +00:00
: ((void)sprout::assertion_failed_msg(sprout::assertion_info_msg(expr, msg, function, file, line)), false)
2013-03-19 17:21:25 +00:00
;
}
2013-03-18 10:12:21 +00:00
inline SPROUT_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_check_msg(bool cond, char const* formatted, char const* expr, char const* msg, char const* function, char const* file, long line) {
2013-03-18 10:12:21 +00:00
return cond ? true
2014-07-03 14:18:11 +00:00
: sprout::detail::assertion_failed_msg(cond, formatted, expr, msg, function, file, line)
2013-03-18 10:12:21 +00:00
;
}
} // namespace detail
} // namespace sprout
2014-07-03 14:18:11 +00:00
# define SPROUT_ASSERT_MSG(expr, msg) ( \
(void)sprout::detail::assertion_check_msg( \
2013-03-18 10:12:21 +00:00
(expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), \
2014-07-03 14:18:11 +00:00
#expr, msg, "(unknown)"/*SPROUT_CURRENT_FUNCTION*/, __FILE__, __LINE__ \
2013-03-19 07:32:48 +00:00
) \
2013-03-18 10:12:21 +00:00
)
#else
namespace sprout {
namespace detail {
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_failed_msg(char const* formatted, char const* msg) {
return (std::cerr << formatted << ": " << msg << std::endl), std::abort(), false;
2013-03-18 10:12:21 +00:00
}
inline SPROUT_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_check_msg(bool cond, char const* formatted, char const* msg) {
2013-03-18 10:12:21 +00:00
return cond ? true
2014-07-03 14:18:11 +00:00
: sprout::detail::assertion_failed_msg(formatted, msg)
2013-03-18 10:12:21 +00:00
;
}
} // namespace detail
} // namespace sprout
2014-07-03 14:18:11 +00:00
# define SPROUT_ASSERT_MSG(expr, msg) \
((void)sprout::detail::assertion_check_msg((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), msg))
2013-03-18 10:12:21 +00:00
#endif
//
2014-07-03 14:18:11 +00:00
// SPROUT_ASSERT
2013-03-18 10:12:21 +00:00
//
#if defined(SPROUT_DISABLE_ASSERTS) || defined(NDEBUG)
2014-07-03 14:18:11 +00:00
# define SPROUT_ASSERT_1(expr) \
2013-03-19 07:32:48 +00:00
((void)0)
2013-03-18 10:12:21 +00:00
#elif defined(SPROUT_ENABLE_ASSERT_HANDLER)
namespace sprout {
2013-03-19 17:21:25 +00:00
//
2014-07-03 14:18:11 +00:00
// assertion_info
2013-03-19 17:21:25 +00:00
//
2014-07-03 14:18:11 +00:00
class assertion_info {
2013-03-19 17:21:25 +00:00
private:
char const* expr_;
char const* function_;
char const* file_;
long line_;
public:
2014-07-03 14:18:11 +00:00
SPROUT_CONSTEXPR assertion_info(char const* expr, char const* function, char const* file, long line)
: expr_(expr), function_(function), file_(file), line_(line)
2013-03-19 17:21:25 +00:00
{}
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_;
}
};
2017-09-29 14:17:17 +00:00
//
// assertion_failed_default
//
inline SPROUT_NON_CONSTEXPR bool
assertion_failed_default(sprout::assertion_info const& info) {
return (std::cerr << "***** Internal Program Error - assertion (" << info.expr() << ") failed: " << info.file() << "(" << info.line() << ")" << std::endl),
std::abort(), false
;
}
2013-03-18 10:12:21 +00:00
//
2014-07-03 14:18:11 +00:00
// assertion_failed
2013-03-18 10:12:21 +00:00
// * user defined
//
void
2014-07-03 14:18:11 +00:00
assertion_failed(sprout::assertion_info const&);
} // namespace sprout
2013-03-18 10:12:21 +00:00
2013-03-19 17:21:25 +00:00
namespace sprout {
2013-03-18 10:12:21 +00:00
namespace detail {
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_failed(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) {
2013-03-19 17:21:25 +00:00
return cond ? true
2014-07-03 14:18:11 +00:00
: ((void)sprout::assertion_failed(sprout::assertion_info(expr, function, file, line)), false)
2013-03-19 17:21:25 +00:00
;
2013-03-18 10:12:21 +00:00
}
inline SPROUT_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_check(bool cond, char const* formatted, char const* expr, char const* function, char const* file, long line) {
2013-03-18 10:12:21 +00:00
return cond ? true
2014-07-03 14:18:11 +00:00
: sprout::detail::assertion_failed(cond, formatted, expr, function, file, line)
2013-03-18 10:12:21 +00:00
;
}
} // namespace detail
} // namespace sprout
2014-07-03 14:18:11 +00:00
# define SPROUT_ASSERT_1(expr) \
(void)sprout::detail::assertion_check( \
2013-03-18 10:12:21 +00:00
(expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__), \
2014-07-03 14:18:11 +00:00
#expr, "(unknown)"/*SPROUT_CURRENT_FUNCTION*/, __FILE__, __LINE__ \
2013-03-19 07:32:48 +00:00
) \
2013-03-18 10:12:21 +00:00
)
#else
namespace sprout {
namespace detail {
2014-07-22 00:33:13 +00:00
inline SPROUT_NON_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_failed(char const* formatted) {
return (std::cerr << formatted << std::endl), std::abort(), false;
2013-03-18 10:12:21 +00:00
}
inline SPROUT_CONSTEXPR bool
2014-07-03 14:18:11 +00:00
assertion_check(bool cond, char const* formatted) {
2013-03-18 10:12:21 +00:00
return cond ? true
2014-07-03 14:18:11 +00:00
: sprout::detail::assertion_failed(formatted)
2013-03-18 10:12:21 +00:00
;
}
} // namespace detail
} // namespace sprout
2014-07-03 14:18:11 +00:00
# define SPROUT_ASSERT_1(expr) \
((void)sprout::detail::assertion_check((expr), SPROUT_ASSERTION_FAILED_FORMAT(expr, __FILE__, __LINE__)))
#endif
2013-03-18 10:12:21 +00:00
2014-07-03 14:18:11 +00:00
#if SPROUT_PP_VARIADICS
# define SPROUT_ASSERT_2(expr, msg) \
SPROUT_ASSERT_MSG(expr, msg)
2014-07-03 14:23:48 +00:00
# define SPROUT_ASSERT(...) \
2014-07-03 14:18:11 +00:00
SPROUT_PP_CAT(SPROUT_ASSERT_, SPROUT_PP_VARIADIC_SIZE(__VA_ARGS__))(__VA_ARGS__)
#else
# define SPROUT_ASSERT(expr) \
SPROUT_ASSERT_1(expr)
2013-03-18 10:12:21 +00:00
#endif
//
// SPROUT_VERIFY
//
#if defined(SPROUT_DISABLE_ASSERTS) || (!defined(SPROUT_ENABLE_ASSERT_HANDLER) && defined(NDEBUG))
namespace sprout {
namespace detail {
inline SPROUT_CONSTEXPR bool
verification_disabled(bool) SPROUT_NOEXCEPT {
return true;
}
} // namespace detail
} // namespace sprout
# define SPROUT_VERIFY(expr) \
2013-03-19 07:32:48 +00:00
((void)(sprout::detail::verification_disabled((expr))))
2013-03-18 10:12:21 +00:00
#else
2013-03-19 07:32:48 +00:00
# define SPROUT_VERIFY(expr) \
SPROUT_ASSERT(expr)
2013-03-18 10:12:21 +00:00
#endif
#endif // #ifndef SPROUT_ASEERT_HPP