From f00814e38db556ad7a31e6dee203e41eea48e7a6 Mon Sep 17 00:00:00 2001 From: Skreg <85214856+shyama7004@users.noreply.github.com> Date: Fri, 10 Jan 2025 17:32:18 +0530 Subject: [PATCH] Merge pull request #26602 from shyama7004:minor-fix Improved dumpVector, cv::Rect operator<< and exceptions #26602 - Applied format for vector element formatting to ensure consistent and clear output representation. - Moved `operator<<` to the `cv` namespace to align with OpenCV's coding standards and improve maintainability. - Enhanced error handling by including detailed exception messages using `e.what()` for better debugging. ### 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 - [ ] 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/src/bindings_utils.cpp | 41 ++++++++++++++++++++--------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/modules/core/src/bindings_utils.cpp b/modules/core/src/bindings_utils.cpp index 78093002f7..6191f26f7c 100644 --- a/modules/core/src/bindings_utils.cpp +++ b/modules/core/src/bindings_utils.cpp @@ -9,7 +9,12 @@ #include #include -namespace cv { namespace utils { +namespace cv { +static inline std::ostream& operator<<(std::ostream& os, const Rect& rect) +{ + return os << "[x=" << rect.x << ", y=" << rect.y << ", w=" << rect.width << ", h=" << rect.height << ']'; +} +namespace utils { String dumpInputArray(InputArray argument) { @@ -51,9 +56,13 @@ String dumpInputArray(InputArray argument) ss << " type(-1)=" << cv::typeToString(argument.type(-1)); } while (0); } + catch (const std::exception& e) + { + ss << " ERROR: exception occurred: " << e.what(); + } catch (...) { - ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: unknown exception occurred, dump is non-complete"; } return ss.str(); } @@ -104,9 +113,13 @@ CV_EXPORTS_W String dumpInputArrayOfArrays(InputArrayOfArrays argument) } } while (0); } + catch (const std::exception& e) + { + ss << " ERROR: exception occurred: " << e.what(); + } catch (...) { - ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: unknown exception occurred, dump is non-complete"; } return ss.str(); } @@ -151,9 +164,13 @@ CV_EXPORTS_W String dumpInputOutputArray(InputOutputArray argument) ss << " type(-1)=" << cv::typeToString(argument.type(-1)); } while (0); } + catch (const std::exception& e) + { + ss << " ERROR: exception occurred: " << e.what(); + } catch (...) { - ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: unknown exception occurred, dump is non-complete"; } return ss.str(); } @@ -204,28 +221,28 @@ CV_EXPORTS_W String dumpInputOutputArrayOfArrays(InputOutputArrayOfArrays argume } } while (0); } + catch (const std::exception& e) + { + ss << " ERROR: exception occurred: " << e.what(); + } catch (...) { - ss << " ERROR: exception occurred, dump is non-complete"; // need to properly support different kinds + ss << " ERROR: unknown exception occurred, dump is non-complete"; } return ss.str(); } -static inline std::ostream& operator<<(std::ostream& os, const cv::Rect& rect) -{ - return os << "[x=" << rect.x << ", y=" << rect.y << ", w=" << rect.width << ", h=" << rect.height << ']'; -} - template static inline String dumpVector(const std::vector& vec, Formatter format) { std::ostringstream oss("[", std::ios::ate); if (!vec.empty()) { - oss << format << vec[0]; + format(oss) << vec[0]; for (std::size_t i = 1; i < vec.size(); ++i) { - oss << ", " << format << vec[i]; + oss << ", "; + format(oss) << vec[i]; } } oss << "]";