sta-core/include/sta/assert.hpp
2023-01-13 14:03:09 +01:00

168 lines
3.4 KiB
C++

/**
* @file
* @brief Assertion handling.
*/
#ifndef STA_ASSERT_HPP
#define STA_ASSERT_HPP
/**
* @defgroup staCore Core
* @brief STA Core library
*/
/**
* @defgroup staCoreBuildConfig Build Config
* @ingroup staCore
* @brief Build configuration options
*/
/**
* @defgroup staCoreAssert Assert
* @ingroup staCore
* @brief Assertion handling.
*/
#ifdef DOXYGEN
/**
* @def STA_ASSERT_ENABLE
* @brief Enable module.
*
* Automatically defined if DEBUG is defined.
* Automatically disabled if NDEBUG is defined.
*
* @ingroup staCoreBuildConfig
*/
# define STA_ASSERT_ENABLE
/**
* @def STA_ASSERT_DISABLE
* @brief Force disable module.
*
* Overrides STA_ASSERT_ENABLE option.
*
* @ingroup staCoreBuildConfig
*/
# define STA_ASSERT_DISABLE
#endif // DOXYGEN
#include <sta/config.hpp>
#ifdef DEBUG
# ifndef STA_ASSERT_ENABLE
# define STA_ASSERT_ENABLE
# endif // !STA_ASSERT_ENABLE
#endif // DEBUG
#if defined(NDEBUG) || defined(STA_ASSERT_DISABLE)
# ifdef STA_ASSERT_ENABLE
# undef STA_ASSERT_ENABLE
# endif // STA_ASSERT_ENABLE
#endif // NDEBUG || STA_ASSERT_DISABLE
// Show enabled module in doxygen output
#ifdef DOXYGEN
# define STA_ASSERT_ENABLE
#endif // DOXYGEN
#ifdef STA_ASSERT_ENABLE
#include <cstdint>
namespace sta
{
/**
* @brief Handle failed assertions.
*
* The default implementation uses **STA_DEBUG_PRINT** and will not
* generate any output if the **DEBUG_SERIAL** module is disabled.
*
* Weak implementation can be overridden.
*
* @param expr Asserted expression or message
* @param file File name
* @param line Line number
*
* @ingroup staCoreAssert
*/
void assert_failed(const char * expr, const char * file, uint32_t line);
/**
* @brief Stop execution.
*
* Weak implementation can be overridden.
*
* @ingroup staCoreAssert
*/
void assert_halt();
} // namespace sta
/**
* @def STA_HALT
* @brief Set function called after failed asserts.
*
* @ingroup staCoreBuildConfig
*/
# ifndef STA_HALT
# define STA_HALT() sta::assert_halt()
# endif // !STA_HALT
/**
* @brief Assert expression.
*
* @param expr Expression
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT(expr) ( (void)( !(expr) && ( sta::assert_failed(#expr, __FILE__, __LINE__), 1 ) && ( STA_HALT(), 1 ) ) )
/**
* @brief Assert expression.
*
* @param expr Expression
* @param msg Message shown on failure
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT_MSG(expr, msg) ( (void)( !(expr) && ( sta::assert_failed(msg, __FILE__, __LINE__), 1 ) && ( STA_HALT(), 1 ) ) )
/**
* @brief Assert expression if condition is true.
*
* @param cond Condition
* @param expr Expression
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT_COND(cond, expr) ( (void)( (cond) ? STA_ASSERT(expr) : 1 ) )
/**
* @brief Assert expression if condition is true.
*
* @param cond Condition
* @param expr Expression
* @param msg Message shown on failure
*
* @ingroup staCoreAssert
*/
# define STA_ASSERT_COND_MSG(cond, expr, msg) ( (void)( (cond) ? STA_ASSERT_MSG(expr, msg) ) )
# define STA_ASSERT_EXTRA(expr) expr;
#else // !STA_ASSERT_ENABLE
# define STA_ASSERT(expr) ((void)0)
# define STA_ASSERT_MSG(expr, msg) ((void)0)
# define STA_ASSERT_COND(cond, expr) ((void)0)
# define STA_ASSERT_COND_MSG(cond, expr, msg) ((void)0)
# define STA_ASSERT_EXTRA(expr) ((void)0)
#endif // !STA_ASSERT_ENABLE
#endif // STA_ASSERT_HPP