Skip to content
Snippets Groups Projects
Commit eac146e2 authored by Wuttke, Joachim's avatar Wuttke, Joachim
Browse files

ASSERT uses function pointer to call qFatal, so that core must not depend on Qt

parent 6ee2b04c
No related branches found
No related tags found
1 merge request!1393ASSERT uses function pointer to call qFatal, so that core must not depend on Qt (#494)
Pipeline #90537 failed
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
#include "App/AppOptions.h" #include "App/AppOptions.h"
#include "App/MessageHandler.h" #include "App/MessageHandler.h"
#include "Base/Util/Assert.h"
#include "GUI/Application/ApplicationSettings.h" #include "GUI/Application/ApplicationSettings.h"
#include "GUI/Support/Util/Path.h" #include "GUI/Support/Util/Path.h"
#include "GUI/View/Loaders/DataLoaderUtil.h" #include "GUI/View/Loaders/DataLoaderUtil.h"
...@@ -25,6 +26,7 @@ ...@@ -25,6 +26,7 @@
#include <QLocale> #include <QLocale>
#include <QMessageBox> #include <QMessageBox>
#include <QMetaType> #include <QMetaType>
#include <QtGlobal>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
...@@ -44,6 +46,7 @@ int main(int argc, char* argv[]) ...@@ -44,6 +46,7 @@ int main(int argc, char* argv[])
ApplicationSettings applicationSettings; ApplicationSettings applicationSettings;
qInstallMessageHandler(messageHandler); qInstallMessageHandler(messageHandler);
failedAssertion = [](std::string msg) { qFatal(msg.c_str()); };
register1DDataLoaders(); register1DDataLoaders();
......
...@@ -35,27 +35,14 @@ target_include_directories(${lib} ...@@ -35,27 +35,14 @@ target_include_directories(${lib}
${FFTW3_INCLUDE_DIR} ${FFTW3_INCLUDE_DIR}
) )
if(BA_GUI)
target_link_libraries(${lib}
PUBLIC
Qt6::Core
)
target_include_directories(${lib}
PUBLIC
${Qt_INCLUDE_DIR}
)
endif()
# g++ versions less than 9.1 need to link against libstdc++fs # g++ versions less than 9.1 need to link against libstdc++fs
# if std::filesystem is used # if std::filesystem is used
if(GCC if(GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1)
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1)
target_link_libraries(${lib} PRIVATE stdc++fs) target_link_libraries(${lib} PRIVATE stdc++fs)
endif() endif()
# the same applies to clang versions less than 9.0: they need to link # the same applies to clang versions less than 9.0: they need to link
# against libstdc++fs or libc++fs if std::filesystem is used # against libstdc++fs or libc++fs if std::filesystem is used
if(CLANG if(CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0)
if(LINUX) if(LINUX)
target_link_libraries(${lib} PRIVATE stdc++fs) target_link_libraries(${lib} PRIVATE stdc++fs)
else() else()
......
// ************************************************************************************************
//
// BornAgain: simulate and fit reflection and scattering
//
//! @file Base/Util/Assert.cpp
//! @brief Initializes function failedAssertion
//!
//! @homepage http://www.bornagainproject.org
//! @license GNU General Public License v3 or higher (see COPYING)
//! @copyright Forschungszentrum Jülich GmbH 2018
//! @authors Scientific Computing Group at MLZ (see CITATION, AUTHORS)
//
// ************************************************************************************************
#include "Base/Util/Assert.h"
#include <csignal>
#include <iostream>
std::function<void(std::string)> failedAssertion = [](std::string msg) {
#ifdef BA_DEBUG
std::cerr << msg << std::endl;
std::raise(11); // abort so that we can inspect the backtrace
#else
throw std::runtime_error(msg);
#endif
};
...@@ -18,53 +18,23 @@ ...@@ -18,53 +18,23 @@
#ifndef BORNAGAIN_BASE_UTIL_ASSERT_H #ifndef BORNAGAIN_BASE_UTIL_ASSERT_H
#define BORNAGAIN_BASE_UTIL_ASSERT_H #define BORNAGAIN_BASE_UTIL_ASSERT_H
// ASSERT must be declared as a macro, not a function, in order for the error #include <functional>
// message to correctly report the source line where the assertion failed.
// For an alternative implementation that calls qFatal, see Base/Utils/Assert.h before 29oct20.
#ifdef BA_DEBUG
#include <csignal>
#include <iostream>
#define ASSERT(condition) \
if (!(condition)) { \
std::cerr << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " \
<< __LINE__ << std::endl; \
std::raise(11); /* abort so that we can inspect the backtrace */ \
throw std::runtime_error("assertion failed ... and we should never get here"); \
}
#elifdef HAVE_QT
#include <QApplication>
#include <QtGlobal>
#include <sstream> #include <sstream>
#include <stdexcept> #include <stdexcept>
#define ASSERT(condition) \ #include <string>
if (!(condition)) { \
if (QCoreApplication::instance()) { \
qFatal("Assertion %s failed in %s, line %d", (#condition), __FILE__, __LINE__); \
} else { \
std::stringstream msg; \
msg << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " \
<< __LINE__; \
throw std::runtime_error(msg.str()); \
} \
}
#else extern std::function<void(std::string)> failedAssertion; // set in Assert.cpp, overriden by GUI
// ASSERT must be declared as a macro, not a function, in order for the error
// message to correctly report the source line where the assertion failed.
#include <sstream>
#include <stdexcept>
#define ASSERT(condition) \ #define ASSERT(condition) \
if (!(condition)) { \ if (!(condition)) { \
std::stringstream msg; \ std::stringstream msg; \
msg << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " << __LINE__; \ msg << "Assertion " << (#condition) << " failed in " << __FILE__ << ", line " << __LINE__; \
throw std::runtime_error(msg.str()); \ failedAssertion(msg.str()); \
/* The following throw is needed to prevent compiler warning -Wreturn-type */ \
throw std::runtime_error("Assertion failed ... and we should never get here"); \
} }
#endif // BA_DEBUG | HAVE_QT | -
#endif // BORNAGAIN_BASE_UTIL_ASSERT_H #endif // BORNAGAIN_BASE_UTIL_ASSERT_H
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment