Merge remote-tracking branch 'upstream/3.4' into merge-3.4
[platform/upstream/opencv.git] / modules / core / src / check.cpp
1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
4
5 #include "precomp.hpp"
6
7 #include "opencv2/core/check.hpp"
8
9 namespace cv {
10
11 const char* depthToString(int depth)
12 {
13     const char* s = detail::depthToString_(depth);
14     return s ? s : "<invalid depth>";
15 }
16
17 const cv::String typeToString(int type)
18 {
19     cv::String s = detail::typeToString_(type);
20     if (s.empty())
21     {
22         static cv::String invalidType("<invalid type>");
23         return invalidType;
24     }
25     return s;
26 }
27
28
29 namespace detail {
30
31 static const char* getTestOpPhraseStr(unsigned testOp)
32 {
33     static const char* _names[] = { "{custom check}", "equal to", "not equal to", "less than or equal to", "less than", "greater than or equal to", "greater than" };
34     CV_DbgAssert(testOp < CV__LAST_TEST_OP);
35     return testOp < CV__LAST_TEST_OP ? _names[testOp] : "???";
36 }
37 static const char* getTestOpMath(unsigned testOp)
38 {
39     static const char* _names[] = { "???", "==", "!=", "<=", "<", ">=", ">" };
40     CV_DbgAssert(testOp < CV__LAST_TEST_OP);
41     return testOp < CV__LAST_TEST_OP ? _names[testOp] : "???";
42 }
43
44 const char* depthToString_(int depth)
45 {
46     static const char* depthNames[] = { "CV_8U", "CV_8S", "CV_16U", "CV_16S", "CV_32S", "CV_32F", "CV_64F", "CV_USRTYPE1" };
47     return (depth <= CV_USRTYPE1 && depth >= 0) ? depthNames[depth] : NULL;
48 }
49
50 const cv::String typeToString_(int type)
51 {
52     int depth = CV_MAT_DEPTH(type);
53     int cn = CV_MAT_CN(type);
54     if (depth >= 0 && depth <= CV_USRTYPE1)
55         return cv::format("%sC%d", depthToString_(depth), cn);
56     return cv::String();
57 }
58
59 template<typename T> static CV_NORETURN
60 void check_failed_auto_(const T& v1, const T& v2, const CheckContext& ctx)
61 {
62     std::stringstream ss;
63     ss  << ctx.message << " (expected: '" << ctx.p1_str << " " << getTestOpMath(ctx.testOp) << " " << ctx.p2_str << "'), where" << std::endl
64         << "    '" << ctx.p1_str << "' is " << v1 << std::endl;
65     if (ctx.testOp != TEST_CUSTOM && ctx.testOp < CV__LAST_TEST_OP)
66     {
67         ss << "must be " << getTestOpPhraseStr(ctx.testOp) << std::endl;
68     }
69     ss  << "    '" << ctx.p2_str << "' is " << v2;
70     cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
71 }
72 void check_failed_MatDepth(const int v1, const int v2, const CheckContext& ctx)
73 {
74     std::stringstream ss;
75     ss  << ctx.message << " (expected: '" << ctx.p1_str << " " << getTestOpMath(ctx.testOp) << " " << ctx.p2_str << "'), where" << std::endl
76         << "    '" << ctx.p1_str << "' is " << v1 << " (" << depthToString(v1) << ")" << std::endl;
77     if (ctx.testOp != TEST_CUSTOM && ctx.testOp < CV__LAST_TEST_OP)
78     {
79         ss << "must be " << getTestOpPhraseStr(ctx.testOp) << std::endl;
80     }
81     ss  << "    '" << ctx.p2_str << "' is " << v2 << " (" << depthToString(v2) << ")";
82     cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
83 }
84 void check_failed_MatType(const int v1, const int v2, const CheckContext& ctx)
85 {
86     std::stringstream ss;
87     ss  << ctx.message << " (expected: '" << ctx.p1_str << " " << getTestOpMath(ctx.testOp) << " " << ctx.p2_str << "'), where" << std::endl
88         << "    '" << ctx.p1_str << "' is " << v1 << " (" << typeToString(v1) << ")" << std::endl;
89     if (ctx.testOp != TEST_CUSTOM && ctx.testOp < CV__LAST_TEST_OP)
90     {
91         ss << "must be " << getTestOpPhraseStr(ctx.testOp) << std::endl;
92     }
93     ss  << "    '" << ctx.p2_str << "' is " << v2 << " (" << typeToString(v2) << ")";
94     cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
95 }
96 void check_failed_MatChannels(const int v1, const int v2, const CheckContext& ctx)
97 {
98     check_failed_auto_<int>(v1, v2, ctx);
99 }
100 void check_failed_auto(const int v1, const int v2, const CheckContext& ctx)
101 {
102     check_failed_auto_<int>(v1, v2, ctx);
103 }
104 void check_failed_auto(const size_t v1, const size_t v2, const CheckContext& ctx)
105 {
106     check_failed_auto_<size_t>(v1, v2, ctx);
107 }
108 void check_failed_auto(const float v1, const float v2, const CheckContext& ctx)
109 {
110     check_failed_auto_<float>(v1, v2, ctx);
111 }
112 void check_failed_auto(const double v1, const double v2, const CheckContext& ctx)
113 {
114     check_failed_auto_<double>(v1, v2, ctx);
115 }
116
117
118 template<typename T> static CV_NORETURN
119 void check_failed_auto_(const T& v, const CheckContext& ctx)
120 {
121     std::stringstream ss;
122     ss  << ctx.message << ":" << std::endl
123         << "    '" << ctx.p2_str << "'" << std::endl
124         << "where" << std::endl
125         << "    '" << ctx.p1_str << "' is " << v;
126     cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
127 }
128 void check_failed_MatDepth(const int v, const CheckContext& ctx)
129 {
130     std::stringstream ss;
131     ss  << ctx.message << ":" << std::endl
132         << "    '" << ctx.p2_str << "'" << std::endl
133         << "where" << std::endl
134         << "    '" << ctx.p1_str << "' is " << v << " (" << depthToString(v) << ")";
135     cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
136 }
137 void check_failed_MatType(const int v, const CheckContext& ctx)
138 {
139     std::stringstream ss;
140     ss  << ctx.message << ":" << std::endl
141         << "    '" << ctx.p2_str << "'" << std::endl
142         << "where" << std::endl
143         << "    '" << ctx.p1_str << "' is " << v << " (" << typeToString(v) << ")";
144     cv::error(cv::Error::StsError, ss.str(), ctx.func, ctx.file, ctx.line);
145 }
146 void check_failed_MatChannels(const int v, const CheckContext& ctx)
147 {
148     check_failed_auto_<int>(v, ctx);
149 }
150 void check_failed_auto(const int v, const CheckContext& ctx)
151 {
152     check_failed_auto_<int>(v, ctx);
153 }
154 void check_failed_auto(const size_t v, const CheckContext& ctx)
155 {
156     check_failed_auto_<size_t>(v, ctx);
157 }
158 void check_failed_auto(const float v, const CheckContext& ctx)
159 {
160     check_failed_auto_<float>(v, ctx);
161 }
162 void check_failed_auto(const double v, const CheckContext& ctx)
163 {
164     check_failed_auto_<double>(v, ctx);
165 }
166
167
168 }} // namespace