/** * @brief Assertion handling. * * Define **STA_ASSERT_ENABLE** in `` to enable module. * * When **DEBUG** is defined the module will be enabled automatically. * Defining **NDEBUG** or **STA_ASSERT_DISABLE** always overrides enabling the module. * * Both `assert_failed` and `assert_halt` provide weak definitions and * can be overridden by the application. `assert_halt` is only called * via the STA_HALT macro which can also be provided by the application. * * The default implementation of `assert_failed` uses **STA_DEBUG_PRINT** internally * and will not generate any output if ` is disabled. */ #ifndef STA_ASSERT_HPP #define STA_ASSERT_HPP #include #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 #ifdef STA_ASSERT_ENABLE #include namespace sta { /** * @brief Handle failed assertions. * * @param expr Asserted expression or message * @param file File name * @param line Line number */ void assert_failed(const char * expr, const char * file, uint32_t line); /** * @brief Stop execution. */ void assert_halt(); } // namespace sta // Use assert_halt if STA_HALT is not defined # ifndef STA_HALT # define STA_HALT() sta::assert_halt() # endif // !STA_HALT /** * @brief Assert expression. * * @param expr Expression */ # 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 */ # 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 */ # 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 */ # define STA_ASSERT_COND_MSG(cond, expr, msg) ((void)((cond) ? STA_ASSERT_MSG(expr, msg))) #else // !STA_ASSERT_ENABLE /** * @brief Assert expression. * * @param expr Expression */ # define STA_ASSERT(expr, msg) ((void)0) /** * @brief Assert expression. * * @param expr Expression * @param msg Message shown on failure */ # define STA_ASSERT_MSG(expr, msg) ((void)0) /** * @brief Assert expression if condition is true. * * @param cond Condition * @param expr Expression */ # define STA_ASSERT_COND(cond, expr) ((void)0) /** * @brief Assert expression if condition is true. * * @param cond Condition * @param expr Expression * @param msg Message shown on failure */ # define STA_ASSERT_COND_MSG(cond, expr, msg) ((void)0) #endif // !STA_ASSERT_ENABLE #endif // STA_ASSERT_HPP