From 3ff97c5580dec29d6f9946bc3eabe41ab6f8f93c Mon Sep 17 00:00:00 2001 From: Vincent Rabaud Date: Fri, 12 Jul 2024 13:41:17 +0200 Subject: [PATCH] Merge pull request #25899 from vrabaud:move_no_except Mark cv::Mat(Mat&&) as noexcept #25899 This fixes https://github.com/opencv/opencv/issues/25065 ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [ ] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [ ] The feature is well documented and sample code can be built with the project CMake --- modules/core/include/opencv2/core/base.hpp | 18 ++++++++++++++++++ modules/core/include/opencv2/core/mat.hpp | 2 +- modules/core/src/matrix.cpp | 2 +- modules/core/src/system.cpp | 6 ++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/modules/core/include/opencv2/core/base.hpp b/modules/core/include/opencv2/core/base.hpp index cc4cc0ddd2..d237d57837 100644 --- a/modules/core/include/opencv2/core/base.hpp +++ b/modules/core/include/opencv2/core/base.hpp @@ -297,6 +297,21 @@ It is possible to alternate error processing by using redirectError(). */ CV_EXPORTS CV_NORETURN void error(int _code, const String& _err, const char* _func, const char* _file, int _line); +/*! @brief Signals an error and terminate application. + +By default the function prints information about the error to stderr, then it terminates application +with std::terminate. The function is designed for invariants check in functions and methods with +noexcept attribute. +@param _code - error code (Error::Code) +@param _err - error description +@param _func - function name. Available only when the compiler supports getting it +@param _file - source file name where the error has occurred +@param _line - line number in the source file where the error has occurred +@see CV_AssertTerminate + */ +CV_EXPORTS CV_NORETURN void terminate(int _code, const String& _err, const char* _func, const char* _file, int _line) CV_NOEXCEPT; + + #ifdef CV_STATIC_ANALYSIS // In practice, some macro are not processed correctly (noreturn is not detected). @@ -338,8 +353,11 @@ for example: The macros CV_Assert (and CV_DbgAssert(expr)) evaluate the specified expression. If it is 0, the macros raise an error (see cv::error). The macro CV_Assert checks the condition in both Debug and Release configurations while CV_DbgAssert is only retained in the Debug configuration. +CV_AssertTerminate is analog of CV_Assert for invariants check in functions with noexcept attribute. +It does not throw exception, but terminates the application. */ #define CV_Assert( expr ) do { if(!!(expr)) ; else cv::error( cv::Error::StsAssert, #expr, CV_Func, __FILE__, __LINE__ ); } while(0) +#define CV_AssertTerminate( expr ) do { if(!!(expr)) ; else cv::terminate( #expr, CV_Func, __FILE__, __LINE__ ); } while(0) #endif // CV_STATIC_ANALYSIS diff --git a/modules/core/include/opencv2/core/mat.hpp b/modules/core/include/opencv2/core/mat.hpp index 2bfb0966c2..7af3c9cfc4 100644 --- a/modules/core/include/opencv2/core/mat.hpp +++ b/modules/core/include/opencv2/core/mat.hpp @@ -2119,7 +2119,7 @@ public: /** @overload */ template void forEach(const Functor& operation) const; - Mat(Mat&& m); + Mat(Mat&& m) CV_NOEXCEPT; Mat& operator = (Mat&& m); enum { MAGIC_VAL = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG }; diff --git a/modules/core/src/matrix.cpp b/modules/core/src/matrix.cpp index 0701542dfd..1b11e12145 100644 --- a/modules/core/src/matrix.cpp +++ b/modules/core/src/matrix.cpp @@ -595,7 +595,7 @@ size_t Mat::total(int startDim, int endDim) const } -Mat::Mat(Mat&& m) +Mat::Mat(Mat&& m) CV_NOEXCEPT : flags(m.flags), dims(m.dims), rows(m.rows), cols(m.cols), data(m.data), datastart(m.datastart), dataend(m.dataend), datalimit(m.datalimit), allocator(m.allocator), u(m.u), size(&rows) diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index c02944079e..8227175b6a 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -1316,6 +1316,12 @@ redirectError( ErrorCallback errCallback, void* userdata, void** prevUserdata) return prevCallback; } +void terminate(int _code, const String& _err, const char* _func, const char* _file, int _line) CV_NOEXCEPT +{ + dumpException(cv::Exception(_code, _err, _func, _file, _line)); + std::terminate(); +} + } CV_IMPL int cvCheckHardwareSupport(int feature)