diff --git a/App/main.cpp b/App/main.cpp index b40f3f895b8e59419997297c3d655655e59c2031..0b853e97d747d9983411c4cf357bd24553f36e1f 100644 --- a/App/main.cpp +++ b/App/main.cpp @@ -14,6 +14,7 @@ #include "App/AppOptions.h" #include "App/MessageHandler.h" +#include "Base/Util/Assert.h" #include "GUI/Application/ApplicationSettings.h" #include "GUI/Support/Util/Path.h" #include "GUI/View/Loaders/DataLoaderUtil.h" @@ -25,6 +26,7 @@ #include <QLocale> #include <QMessageBox> #include <QMetaType> +#include <QtGlobal> int main(int argc, char* argv[]) { @@ -44,6 +46,7 @@ int main(int argc, char* argv[]) ApplicationSettings applicationSettings; qInstallMessageHandler(messageHandler); + failedAssertion = [](std::string msg) { qFatal(msg.c_str()); }; register1DDataLoaders(); diff --git a/Base/CMakeLists.txt b/Base/CMakeLists.txt index 4b7c4fb4e31f538101fceabb625a27d7f61b7f0e..994c10a8b3e34dc1341a6aa1c804e02c9a773723 100644 --- a/Base/CMakeLists.txt +++ b/Base/CMakeLists.txt @@ -35,27 +35,14 @@ target_include_directories(${lib} ${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 # if std::filesystem is used -if(GCC - AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1) +if(GCC AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.1) target_link_libraries(${lib} PRIVATE stdc++fs) endif() # 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 -if(CLANG - AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) +if(CLANG AND CMAKE_CXX_COMPILER_VERSION VERSION_LESS 9.0) if(LINUX) target_link_libraries(${lib} PRIVATE stdc++fs) else() diff --git a/Base/Util/Assert.cpp b/Base/Util/Assert.cpp new file mode 100644 index 0000000000000000000000000000000000000000..57712bfdc847b0f8b59a63c781f0f4f41081b355 --- /dev/null +++ b/Base/Util/Assert.cpp @@ -0,0 +1,26 @@ +// ************************************************************************************************ +// +// 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 +}; diff --git a/Base/Util/Assert.h b/Base/Util/Assert.h index c635e9412168c23f4ce98bab3046997a98e842bf..1b9f394cc94274eb304ded8b30ba5d8c8b68c37d 100644 --- a/Base/Util/Assert.h +++ b/Base/Util/Assert.h @@ -18,53 +18,23 @@ #ifndef 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 -// 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 <functional> #include <sstream> #include <stdexcept> -#define ASSERT(condition) \ - 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()); \ - } \ - } +#include <string> -#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) \ if (!(condition)) { \ std::stringstream msg; \ 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