41 #ifndef OKVIS_ASSERT_MACROS_HPP
42 #define OKVIS_ASSERT_MACROS_HPP
52 #define OKVIS_DEFINE_EXCEPTION(exceptionName, exceptionParent) \
53 class exceptionName : public exceptionParent { \
55 exceptionName(const char * message) : exceptionParent(message) {} \
56 exceptionName(std::string const & message) : exceptionParent(message) {} \
57 virtual ~exceptionName() throw() {} \
65 template<
typename OKVIS_EXCEPTION_T>
68 std::stringstream okvis_assert_stringstream;
70 okvis_assert_stringstream << exceptionType << sfp.
toString() <<
" " << message;
71 throw(OKVIS_EXCEPTION_T(okvis_assert_stringstream.str()));
74 template<
typename OKVIS_EXCEPTION_T>
75 inline void OKVIS_throw_exception(std::string
const & exceptionType, std::string
const &
function, std::string
const & file,
76 int line, std::string
const & message)
78 OKVIS_throw_exception<OKVIS_EXCEPTION_T>(exceptionType,
okvis::source_file_pos(
function,file,line),message);
84 template<
typename OKVIS_EXCEPTION_T>
88 detail::OKVIS_throw_exception<OKVIS_EXCEPTION_T>(
"", sfp,message);
98 #define OKVIS_THROW(exceptionType, message) { \
99 std::stringstream okvis_assert_stringstream; \
100 okvis_assert_stringstream << message; \
101 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__, okvis_assert_stringstream.str()); \
105 #define OKVIS_THROW_SFP(exceptionType, SourceFilePos, message){ \
106 std::stringstream okvis_assert_stringstream; \
107 okvis_assert_stringstream << message; \
108 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", SourceFilePos, okvis_assert_stringstream.str()); \
111 #define OKVIS_ASSERT_TRUE(exceptionType, condition, message) \
114 std::stringstream okvis_assert_stringstream; \
115 okvis_assert_stringstream << "assert(" << #condition << ") failed: " << message; \
116 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__, okvis_assert_stringstream.str()); \
119 #define OKVIS_ASSERT_FALSE(exceptionType, condition, message) \
122 std::stringstream okvis_assert_stringstream; \
123 okvis_assert_stringstream << "assert( not " << #condition << ") failed: " << message; \
124 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__, okvis_assert_stringstream.str()); \
129 #define OKVIS_ASSERT_GE_LT(exceptionType, value, lowerBound, upperBound, message) \
130 if((value) < (lowerBound) || (value) >= (upperBound)) \
132 std::stringstream okvis_assert_stringstream; \
133 okvis_assert_stringstream << "assert(" << #lowerBound << " <= " << #value << " < " << #upperBound << ") failed [" << (lowerBound) << " <= " << (value) << " < " << (upperBound) << "]: " << message; \
134 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
139 #define OKVIS_ASSERT_LT(exceptionType, value, upperBound, message) \
140 if((value) >= (upperBound)) \
142 std::stringstream okvis_assert_stringstream; \
143 okvis_assert_stringstream << "assert(" << #value << " < " << #upperBound << ") failed [" << (value) << " < " << (upperBound) << "]: " << message; \
144 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
147 #define OKVIS_ASSERT_GE(exceptionType, value, lowerBound, message) \
148 if((value) < (lowerBound)) \
150 std::stringstream okvis_assert_stringstream; \
151 okvis_assert_stringstream << "assert(" << #value << " >= " << #lowerBound << ") failed [" << (value) << " >= " << (lowerBound) << "]: " << message; \
152 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
157 #define OKVIS_ASSERT_LE(exceptionType, value, upperBound, message) \
158 if((value) > (upperBound)) \
160 std::stringstream okvis_assert_stringstream; \
161 okvis_assert_stringstream << "assert(" << #value << " <= " << #upperBound << ") failed [" << (value) << " <= " << (upperBound) << "]: " << message; \
162 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
165 #define OKVIS_ASSERT_GT(exceptionType, value, lowerBound, message) \
166 if((value) <= (lowerBound)) \
168 std::stringstream okvis_assert_stringstream; \
169 okvis_assert_stringstream << "assert(" << #value << " > " << #lowerBound << ") failed [" << (value) << " > " << (lowerBound) << "]: " << message; \
170 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
175 #define OKVIS_ASSERT_EQ(exceptionType, value, testValue, message) \
176 if((value) != (testValue)) \
178 std::stringstream okvis_assert_stringstream; \
179 okvis_assert_stringstream << "assert(" << #value << " == " << #testValue << ") failed [" << (value) << " == " << (testValue) << "]: " << message; \
180 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
183 #define OKVIS_ASSERT_NE(exceptionType, value, testValue, message) \
184 if((value) == (testValue)) \
186 std::stringstream okvis_assert_stringstream; \
187 okvis_assert_stringstream << "assert(" << #value << " != " << #testValue << ") failed [" << (value) << " != " << (testValue) << "]: " << message; \
188 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
191 #define OKVIS_ASSERT_NEAR(exceptionType, value, testValue, abs_error, message) \
192 if(!(fabs((testValue) - (value)) <= fabs(abs_error))) \
194 std::stringstream okvis_assert_stringstream; \
195 okvis_assert_stringstream << "assert(" << #value << " == " << #testValue << ") failed [" << (value) << " == " << (testValue) << " (" << fabs((testValue) - (value)) << " > " << fabs(abs_error) << ")]: " << message; \
196 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
203 #define OKVIS_THROW_DBG(exceptionType, message){ \
204 std::stringstream okvis_assert_stringstream; \
205 okvis_assert_stringstream << message; \
206 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__, okvis_assert_stringstream.str()); \
211 #define OKVIS_ASSERT_TRUE_DBG(exceptionType, condition, message) \
214 std::stringstream okvis_assert_stringstream; \
215 okvis_assert_stringstream << "debug assert(" << #condition << ") failed: " << message; \
216 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__, okvis_assert_stringstream.str()); \
219 #define OKVIS_ASSERT_FALSE_DBG(exceptionType, condition, message) \
222 std::stringstream okvis_assert_stringstream; \
223 okvis_assert_stringstream << "debug assert( not " << #condition << ") failed: " << message; \
224 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__, okvis_assert_stringstream.str()); \
228 #define OKVIS_ASSERT_DBG_RE( condition, message) OKVIS_ASSERT_DBG(std::runtime_error, condition, message)
230 #define OKVIS_ASSERT_GE_LT_DBG(exceptionType, value, lowerBound, upperBound, message) \
231 if((value) < (lowerBound) || (value) >= (upperBound)) \
233 std::stringstream okvis_assert_stringstream; \
234 okvis_assert_stringstream << "debug assert(" << #lowerBound << " <= " << #value << " < " << #upperBound << ") failed [" << (lowerBound) << " <= " << (value) << " < " << (upperBound) << "]: " << message; \
235 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
240 #define OKVIS_ASSERT_LT_DBG(exceptionType, value, upperBound, message) \
241 if((value) >= (upperBound)) \
243 std::stringstream okvis_assert_stringstream; \
244 okvis_assert_stringstream << "debug assert(" << #value << " < " << #upperBound << ") failed [" << (value) << " < " << (upperBound) << "]: " << message; \
245 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
250 #define OKVIS_ASSERT_GE_DBG(exceptionType, value, lowerBound, message) \
251 if((value) < (lowerBound)) \
253 std::stringstream okvis_assert_stringstream; \
254 okvis_assert_stringstream << "debug assert(" << #value << " >= " << #lowerBound << ") failed [" << (value) << " >= " << (lowerBound) << "]: " << message; \
255 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
260 #define OKVIS_ASSERT_LE_DBG(exceptionType, value, upperBound, message) \
261 if((value) > (upperBound)) \
263 std::stringstream okvis_assert_stringstream; \
264 okvis_assert_stringstream << "debug assert(" << #value << " <= " << #upperBound << ") failed [" << (value) << " <= " << (upperBound) << "]: " << message; \
265 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
268 #define OKVIS_ASSERT_GT_DBG(exceptionType, value, lowerBound, message) \
269 if((value) <= (lowerBound)) \
271 std::stringstream okvis_assert_stringstream; \
272 okvis_assert_stringstream << "debug assert(" << #value << " > " << #lowerBound << ") failed [" << (value) << " > " << (lowerBound) << "]: " << message; \
273 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
278 #define OKVIS_ASSERT_EQ_DBG(exceptionType, value, testValue, message) \
279 if((value) != (testValue)) \
281 std::stringstream okvis_assert_stringstream; \
282 okvis_assert_stringstream << "debug assert(" << #value << " == " << #testValue << ") failed [" << (value) << " == " << (testValue) << "]: " << message; \
283 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
287 #define OKVIS_ASSERT_NE_DBG(exceptionType, value, testValue, message) \
288 if((value) == (testValue)) \
290 std::stringstream okvis_assert_stringstream; \
291 okvis_assert_stringstream << "debug assert(" << #value << " != " << #testValue << ") failed [" << (value) << " != " << (testValue) << "]: " << message; \
292 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
297 #define OKVIS_ASSERT_NEAR_DBG(exceptionType, value, testValue, abs_error, message) \
298 if(!(fabs((testValue) - (value)) <= fabs(abs_error))) \
300 std::stringstream okvis_assert_stringstream; \
301 okvis_assert_stringstream << "debug assert(" << #value << " == " << #testValue << ") failed [" << (value) << " == " << (testValue) << " (" << fabs((testValue) - (value)) << " > " << fabs(abs_error) << ")]: " << message; \
302 okvis::detail::OKVIS_throw_exception<exceptionType>("[" #exceptionType "] ", __FUNCTION__,__FILE__,__LINE__,okvis_assert_stringstream.str()); \
306 #define OKVIS_OUT(X) std::cout << #X << ": " << (X) << std::endl
311 #define OKVIS_THROW_DBG(exceptionType, message)
312 #define OKVIS_ASSERT_TRUE_DBG(exceptionType, condition, message)
313 #define OKVIS_ASSERT_FALSE_DBG(exceptionType, condition, message)
314 #define OKVIS_ASSERT_GE_LT_DBG(exceptionType, value, lowerBound, upperBound, message)
315 #define OKVIS_ASSERT_LT_DBG(exceptionType, value, upperBound, message)
316 #define OKVIS_ASSERT_GT_DBG(exceptionType, value, lowerBound, message)
317 #define OKVIS_ASSERT_LE_DBG(exceptionType, value, upperBound, message)
318 #define OKVIS_ASSERT_GE_DBG(exceptionType, value, lowerBound, message)
319 #define OKVIS_ASSERT_NE_DBG(exceptionType, value, testValue, message)
320 #define OKVIS_ASSERT_EQ_DBG(exceptionType, value, testValue, message)
321 #define OKVIS_ASSERT_NEAR_DBG(exceptionType, value, testValue, abs_error, message)
326 #endif // OKVIS_ASSERT_MACROS_HPP
Definition: source_file_pos.hpp:52
This file contains some helper functions for the assert macros.
void OKVIS_throw_exception(std::string const &exceptionType, okvis::source_file_pos sfp, std::string const &message)
Definition: assert_macros.hpp:66
void okvis_assert_throw(bool assert_condition, std::string message, okvis::source_file_pos sfp)
Definition: assert_macros.hpp:85
std::string toString() const
Definition: source_file_pos.hpp:67