OKVIS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
assert_macros.hpp
Go to the documentation of this file.
1 /*********************************************************************************
2  * OKVIS - Open Keyframe-based Visual-Inertial SLAM
3  * Copyright (c) 2015, Autonomous Systems Lab / ETH Zurich
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright notice,
9  * this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright notice,
11  * this list of conditions and the following disclaimer in the documentation
12  * and/or other materials provided with the distribution.
13  * * Neither the name of Autonomous Systems Lab / ETH Zurich nor the names of
14  * its contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * Created on: Dec 12, 2011
30  * Author: Paul Furgale
31  * Modified: Stefan Leutenegger (s.leutenegger@imperial.ac.uk)
32  *********************************************************************************/
33 
41 #ifndef OKVIS_ASSERT_MACROS_HPP
42 #define OKVIS_ASSERT_MACROS_HPP
43 
44 #include <stdexcept>
45 #include <sstream>
46 #include <typeinfo>
48 
50 // (std::runtime_error should be top parent)
51 // adapted from ros/drivers/laser/hokuyo_driver/hokuyo.h
52 #define OKVIS_DEFINE_EXCEPTION(exceptionName, exceptionParent) \
53  class exceptionName : public exceptionParent { \
54  public: \
55  exceptionName(const char * message) : exceptionParent(message) {} \
56  exceptionName(std::string const & message) : exceptionParent(message) {} \
57  virtual ~exceptionName() throw() {} \
58  };
59 
61 namespace okvis {
62 
63  namespace detail {
64 
65  template<typename OKVIS_EXCEPTION_T>
66  inline void OKVIS_throw_exception(std::string const & exceptionType, okvis::source_file_pos sfp, std::string const & message)
67  {
68  std::stringstream okvis_assert_stringstream;
69  // I have no idea what broke doesn't work with the << operator. sleutenegger: not just Windows, but in general...???
70  okvis_assert_stringstream << exceptionType << sfp.toString() << " " << message;
71  throw(OKVIS_EXCEPTION_T(okvis_assert_stringstream.str()));
72  }
73 
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)
77  {
78  OKVIS_throw_exception<OKVIS_EXCEPTION_T>(exceptionType, okvis::source_file_pos(function,file,line),message);
79  }
80 
81 
82  } // namespace okvis::detail
83 
84  template<typename OKVIS_EXCEPTION_T>
85  inline void okvis_assert_throw(bool assert_condition, std::string message, okvis::source_file_pos sfp) {
86  if(!assert_condition)
87  {
88  detail::OKVIS_throw_exception<OKVIS_EXCEPTION_T>("", sfp,message);
89  }
90  }
91 
92 
93 
94 } // namespace okvis
95 
96 
97 
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()); \
102  }
103 
104 
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()); \
109  }
110 
111 #define OKVIS_ASSERT_TRUE(exceptionType, condition, message) \
112  if(!(condition)) \
113  { \
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()); \
117  }
118 
119 #define OKVIS_ASSERT_FALSE(exceptionType, condition, message) \
120  if((condition)) \
121  { \
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()); \
125  }
126 
127 
128 
129 #define OKVIS_ASSERT_GE_LT(exceptionType, value, lowerBound, upperBound, message) \
130  if((value) < (lowerBound) || (value) >= (upperBound)) \
131  { \
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()); \
135  }
136 
137 
138 
139 #define OKVIS_ASSERT_LT(exceptionType, value, upperBound, message) \
140  if((value) >= (upperBound)) \
141  { \
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()); \
145  }
146 
147 #define OKVIS_ASSERT_GE(exceptionType, value, lowerBound, message) \
148  if((value) < (lowerBound)) \
149  { \
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()); \
153  }
154 
155 
156 
157 #define OKVIS_ASSERT_LE(exceptionType, value, upperBound, message) \
158  if((value) > (upperBound)) \
159  { \
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()); \
163  }
164 
165 #define OKVIS_ASSERT_GT(exceptionType, value, lowerBound, message) \
166  if((value) <= (lowerBound)) \
167  { \
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()); \
171  }
172 
173 
174 
175 #define OKVIS_ASSERT_EQ(exceptionType, value, testValue, message) \
176  if((value) != (testValue)) \
177  { \
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()); \
181  }
182 
183 #define OKVIS_ASSERT_NE(exceptionType, value, testValue, message) \
184  if((value) == (testValue)) \
185  { \
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()); \
189  }
190 
191 #define OKVIS_ASSERT_NEAR(exceptionType, value, testValue, abs_error, message) \
192  if(!(fabs((testValue) - (value)) <= fabs(abs_error))) \
193  { \
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()); \
197  }
198 
199 
200 
201 #ifndef NDEBUG
202 
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()); \
207  }
208 
209 
210 
211 #define OKVIS_ASSERT_TRUE_DBG(exceptionType, condition, message) \
212  if(!(condition)) \
213  { \
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()); \
217  }
218 
219 #define OKVIS_ASSERT_FALSE_DBG(exceptionType, condition, message) \
220  if((condition)) \
221  { \
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()); \
225  }
226 
227 
228 #define OKVIS_ASSERT_DBG_RE( condition, message) OKVIS_ASSERT_DBG(std::runtime_error, condition, message)
229 
230 #define OKVIS_ASSERT_GE_LT_DBG(exceptionType, value, lowerBound, upperBound, message) \
231  if((value) < (lowerBound) || (value) >= (upperBound)) \
232  { \
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()); \
236  }
237 
238 
239 
240 #define OKVIS_ASSERT_LT_DBG(exceptionType, value, upperBound, message) \
241  if((value) >= (upperBound)) \
242  { \
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()); \
246  }
247 
248 
249 
250 #define OKVIS_ASSERT_GE_DBG(exceptionType, value, lowerBound, message) \
251  if((value) < (lowerBound)) \
252  { \
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()); \
256  }
257 
258 
259 
260 #define OKVIS_ASSERT_LE_DBG(exceptionType, value, upperBound, message) \
261  if((value) > (upperBound)) \
262  { \
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()); \
266  }
267 
268 #define OKVIS_ASSERT_GT_DBG(exceptionType, value, lowerBound, message) \
269  if((value) <= (lowerBound)) \
270  { \
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()); \
274  }
275 
276 
277 
278 #define OKVIS_ASSERT_EQ_DBG(exceptionType, value, testValue, message) \
279  if((value) != (testValue)) \
280  { \
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()); \
284  }
285 
286 
287 #define OKVIS_ASSERT_NE_DBG(exceptionType, value, testValue, message) \
288  if((value) == (testValue)) \
289  { \
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()); \
293  }
294 
295 
296 
297 #define OKVIS_ASSERT_NEAR_DBG(exceptionType, value, testValue, abs_error, message) \
298  if(!(fabs((testValue) - (value)) <= fabs(abs_error))) \
299  { \
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()); \
303  }
304 
305 
306 #define OKVIS_OUT(X) std::cout << #X << ": " << (X) << std::endl
307 
308 #else
309 
310 #define OKVIS_OUT(X)
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)
322 #endif
323 
324 
325 
326 #endif // OKVIS_ASSERT_MACROS_HPP
327 
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