Skip to content

Commit

Permalink
feat(assert): add general assertion that always executes its condition
Browse files Browse the repository at this point in the history
Its purpose is used to check the value of conditions whose sideeffects are
critical to the program.
this makes it possible to avoid code such as
```cpp
[[maybe_unused]] bool successful = moveToThread(m_pThread.get());
DEBUG_ASSERT(successful);
```
and instead write
```cpp
ASSERT(moveToThread(m_pThread.get()));
```
  • Loading branch information
Swiftb0y committed May 16, 2024
1 parent da261b6 commit df691bd
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/util/assert.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li
qFatal("ASSERT: \"%s\" in function %s at %s:%d", assertion, function, file, line);
}

inline void mixxx_assert(const char* assertion, const char* file, int line, const char* function) {
qWarning("ASSERT: \"%s\" in function %s at %s:%d", assertion, function, file, line);
}

// These macros provide the demangled function name (including helpful template
// type information) and are supported on every version of GCC, Clang, and MSVC
// that Mixxx supports.
Expand All @@ -44,6 +48,26 @@ inline void mixxx_release_assert(const char* assertion, const char* file, int li
} \
while (0)

/// Checks that cond is true. If cond is false then prints a warning message
/// but does not quit.
/// Its purpose is used to check the value of conditions whose sideeffects are
/// critical to the program.
// this makes it possible to avoid code such as
// ```cpp
// [[maybe_unused]] bool successful = moveToThread(m_pThread.get());
// DEBUG_ASSERT(successful);
// ```
// and instead write
// ```cpp
// ASSERT(moveToThread(m_pThread.get()));
// ```
#define ASSERT(cond) \
do \
if (!static_cast<bool>(cond)) [[unlikely]] { \
mixxx_assert(#cond, __FILE__, __LINE__, ASSERT_FUNCTION); \
} \
while (0)

/// Checks that cond is true in debug builds. If cond is false then prints a
/// warning message to the console. If Mixxx is built with
/// MIXXX_DEBUG_ASSERTIONS_FATAL then the warning message is fatal. Compiles
Expand Down

0 comments on commit df691bd

Please sign in to comment.