ExpressionException.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2013
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
28 
29 #include <string>
30 
31 
33 {
40  class ExpressionException : public armarx::LocalException
41  {
42  public:
43  ExpressionException(std::string expression) noexcept :
44  armarx::LocalException("The following expression is not true, but needs to be: '" + expression + "'"),
45  expression(expression)
46  { }
47 
48  std::string name() const override
49  {
50  return "armarx::exceptions::local::ExpressionException";
51  }
52 
53  std::string getExpression() const
54  {
55  return expression;
56  }
57 
58  private:
59  std::string expression;
60  };
61 
62 }
63 
70 #define ARMARX_CHECK_EXPRESSION(expression) \
71  if( !(expression) ) \
72  throw ::armarx::exceptions::local::ExpressionException(#expression) \
73  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " \
74  << __FUNCTION__ << " in )\n"
75 
80 #define ARMARX_CHECK(expression) ARMARX_CHECK_EXPRESSION(expression)
81 
88 #define ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, cmp) \
89  if( !(lhs cmp rhs) ) \
90  throw ::armarx::exceptions::local::ExpressionException(#lhs " " #cmp " " #rhs) \
91  << "\n(lhs = " << lhs << ", rhs = " << rhs << ")" \
92  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
93 
100 #define ARMARX_CHECK_LESS(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, <)
101 
107 #define ARMARX_CHECK_LESS_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, <=)
108 
114 #define ARMARX_CHECK_GREATER(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, >)
115 
121 #define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, >=)
122 
128 #define ARMARX_CHECK_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, ==)
129 
135 #define ARMARX_CHECK_NOT_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, !=)
136 
143 #define ARMARX_CHECK_POSITIVE(number) ARMARX_CHECK_GREATER(number, 0)
144 
150 #define ARMARX_CHECK_NONNEGATIVE(number) ARMARX_CHECK_GREATER_EQUAL(number, 0)
151 
157 #define ARMARX_CHECK_FITS_SIZE(number, size) \
158  ARMARX_CHECK_NONNEGATIVE(number); \
159  ARMARX_CHECK_LESS(size_t(number), size)
160 
167 #define ARMARX_CHECK_CLOSE(lhs, rhs, prec) \
168  if( !(std::abs(lhs - rhs) <= prec) ) \
169  throw ::armarx::exceptions::local::ExpressionException("| " #lhs " - " #rhs "| <= " #prec) \
170  << "\n(lhs = " << lhs << ", rhs = " << rhs << ", prec = " << prec << ")" \
171  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
172 
173 
180 #define ARMARX_CHECK_FINITE(number) \
181  if( !(std::isfinite(number)) ) \
182  throw ::armarx::exceptions::local::ExpressionException("std::isfinite(" #number ")") \
183  << "\n(number = " << number << ")" \
184  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
185 
192 #define ARMARX_CHECK_IS_NULL(ptr) \
193  if( ptr ) \
194  throw ::armarx::exceptions::local::ExpressionException("ptr == nullptr") \
195  << "\n(ptr = " << ptr << ")" \
196  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
197 
204 #define ARMARX_CHECK_NOT_NULL(ptr) \
205  if( !ptr ) \
206  throw ::armarx::exceptions::local::ExpressionException("ptr != nullptr") \
207  << "\n(ptr = nullptr)" \
208  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
209 
210 #define ARMARX_CHECK_NULL(ptr) \
211  if( ptr ) \
212  throw ::armarx::exceptions::local::ExpressionException("ptr == nullptr") \
213  << "\n(ptr = nullptr)" \
214  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
215 
216 #define ARMARX_CHECK_EMPTY(c) \
217  if( ! c.empty() ) \
218  throw ::armarx::exceptions::local::ExpressionException("c was expected to be empty") \
219  << "\n(c = " << c << ")" \
220  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
221 
222 #define ARMARX_CHECK_NOT_EMPTY(c) \
223  if( c.empty() ) \
224  throw ::armarx::exceptions::local::ExpressionException("c was not expected to be empty") \
225  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
226 
227 #define ARMARX_CHECK_MULTIPLE_OF(val, mod) \
228  if( (val % mod) != 0 ) \
229  throw ::armarx::exceptions::local::ExpressionException("val was expected to be a multiple of mod") \
230  << "\n(val = " << val << ", mod = " << mod << ")" \
231  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"\
232 
233 #define ARMARX_CHECK_NOT_MULTIPLE_OF(val, mod) \
234  if( (val % mod) == 0 ) \
235  throw ::armarx::exceptions::local::ExpressionException("val was not expected to be a multiple of mod") \
236  << "\n(val = " << val << ", mod = " << mod << ")" \
237  <<"\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << " in )\n"
238 
243 #define ARMARX_CHECK_AND_THROW(expression, ExceptionType) do { \
244  if( !(expression) ) \
245  throw ExceptionType(#expression); \
246  } while(0);
247 
248 #undef eigen_assert
249 #define eigen_assert(expression) ARMARX_CHECK_EXPRESSION(expression)
armarx::exceptions::local::ExpressionException::name
std::string name() const override
Definition: ExpressionException.h:48
LocalException.h
armarx::exceptions::local
Definition: DynamicLibraryException.h:31
armarx::exceptions::local::ExpressionException::ExpressionException
ExpressionException(std::string expression) noexcept
Definition: ExpressionException.h:43
armarx::exceptions::local::ExpressionException::getExpression
std::string getExpression() const
Definition: ExpressionException.h:53
armarx::exceptions::local::ExpressionException
This exception is thrown if the macro ARMARX_CHECK_EXPRESSION is used.
Definition: ExpressionException.h:40