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 
27 #include <string>
28 
30 
32 {
33  /**
34  \class ExpressionException
35  \brief This exception is thrown if the macro \ref ARMARX_CHECK_EXPRESSION
36  is used.
37  \ingroup Exceptions
38  */
39  class ExpressionException : public armarx::LocalException
40  {
41  public:
42  ExpressionException(std::string expression) noexcept :
43  armarx::LocalException("The following expression is not true, but needs to be: '" +
44  expression + "'"),
45  expression(expression)
46  {
47  }
48 
49  std::string
50  name() const override
51  {
52  return "armarx::exceptions::local::ExpressionException";
53  }
54 
55  std::string
56  getExpression() const
57  {
58  return expression;
59  }
60 
61  private:
62  std::string expression;
63  };
64 
65 } // namespace armarx::exceptions::local
66 
67 /**
68 \ingroup Exceptions
69 \brief This macro evaluates the expression and if it turns out to be
70 false it will throw an ExpressionException with the expression converted
71 into a string.
72  **/
73 #define ARMARX_CHECK_EXPRESSION(expression) \
74  if (!(expression)) \
75  throw ::armarx::exceptions::local::ExpressionException(#expression) \
76  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
77 
78 /**
79 \ingroup Exceptions
80 \brief Shortcut for ARMARX_CHECK_EXPRESSION
81  **/
82 #define ARMARX_CHECK(expression) ARMARX_CHECK_EXPRESSION(expression)
83 
84 /**
85 \ingroup Exceptions
86 \brief This macro evaluates the expression (pred(lhs, rhs)) and if it turns out to be
87 false it will throw an ExpressionException with the expression converted
88 into a string.
89  **/
90 #define ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, cmp) \
91  if (!(lhs cmp rhs)) \
92  throw ::armarx::exceptions::local::ExpressionException(#lhs " " #cmp " " #rhs) \
93  << "\n(lhs = " << lhs << ", rhs = " << rhs << ")" \
94  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
95 
96 /**
97 \ingroup Exceptions
98 \brief This macro evaluates whether lhs is less (<) than rhs and if it turns out to be
99 false it will throw an ExpressionException with the expression converted
100 into a string.
101  **/
102 #define ARMARX_CHECK_LESS(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, <)
103 /**
104 \ingroup Exceptions
105 \brief This macro evaluates whether lhs is less or equal (<=) rhs and if it turns out to be
106 false it will throw an ExpressionException with the expression converted
107 into a string.
108  **/
109 #define ARMARX_CHECK_LESS_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, <=)
110 /**
111 \ingroup Exceptions
112 \brief This macro evaluates whether lhs is greater (>) than rhs and if it turns out to be
113 false it will throw an ExpressionException with the expression converted
114 into a string.
115  **/
116 #define ARMARX_CHECK_GREATER(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, >)
117 /**
118 \ingroup Exceptions
119 \brief This macro evaluates whether lhs is greater or equal (>=) rhs and if it turns out to be
120 false it will throw an ExpressionException with the expression converted
121 into a string.
122  **/
123 #define ARMARX_CHECK_GREATER_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, >=)
124 /**
125 \ingroup Exceptions
126 \brief This macro evaluates whether lhs is equal (==) rhs and if it turns out to be
127 false it will throw an ExpressionException with the expression converted
128 into a string.
129  **/
130 #define ARMARX_CHECK_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, ==)
131 /**
132 \ingroup Exceptions
133 \brief This macro evaluates whether lhs is inequal (!=) rhs and if it turns out to be
134 false it will throw an ExpressionException with the expression converted
135 into a string.
136  **/
137 #define ARMARX_CHECK_NOT_EQUAL(lhs, rhs) ARMARX_CHECK_BINARY_PREDICATE(lhs, rhs, !=)
138 
139 /**
140 \ingroup Exceptions
141 \brief This macro evaluates whether number is positive (> 0) and if it turns out to be
142 false it will throw an ExpressionException with the expression converted
143 into a string.
144  **/
145 #define ARMARX_CHECK_POSITIVE(number) ARMARX_CHECK_GREATER(number, 0)
146 
147 /**
148 \ingroup Exceptions
149 \brief Check whether number is nonnegative (>= 0). If it is not, throw an
150 ExpressionException with the expression converted into a string.
151  **/
152 #define ARMARX_CHECK_NONNEGATIVE(number) ARMARX_CHECK_GREATER_EQUAL(number, 0)
153 
154 /**
155 \ingroup Exceptions
156 \brief Check whether number is nonnegative (>= 0) and less than size. If it is not,
157 throw an ExpressionException with the expression converted into a string.
158  **/
159 #define ARMARX_CHECK_FITS_SIZE(number, size) \
160  ARMARX_CHECK_NONNEGATIVE(number); \
161  ARMARX_CHECK_LESS(size_t(number), size)
162 
163 /**
164  * \ingroup Exceptions
165  * \brief Check whether `lhs` is close to `rhs`, i.e. whether the absolute
166  * difference s not more than `prec`.
167  * \throw an ExpressionException with the expression converted into a string.
168  */
169 #define ARMARX_CHECK_CLOSE(lhs, rhs, prec) \
170  if (!(std::abs(lhs - rhs) <= prec)) \
171  throw ::armarx::exceptions::local::ExpressionException("| " #lhs " - " #rhs "| <= " #prec) \
172  << "\n(lhs = " << lhs << ", rhs = " << rhs << ", prec = " << prec << ")" \
173  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
174 
175 
176 /**
177 \ingroup Exceptions
178 \brief This macro evaluates whether number is finite (not nan or inf) and if it turns out to be
179 false it will throw an ExpressionException with the expression converted
180 into a string.
181  **/
182 #define ARMARX_CHECK_FINITE(number) \
183  if (!(std::isfinite(number))) \
184  throw ::armarx::exceptions::local::ExpressionException("std::isfinite(" #number ")") \
185  << "\n(number = " << number << ")" \
186  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
187 
188 /**
189 \ingroup Exceptions
190 \brief This macro evaluates whether ptr is null and if it turns out to be
191 false it will throw an ExpressionException with the expression converted
192 into a string.
193  **/
194 #define ARMARX_CHECK_IS_NULL(ptr) \
195  if (ptr) \
196  throw ::armarx::exceptions::local::ExpressionException(#ptr " == nullptr") \
197  << "\n(ptr = " << ptr << ")" \
198  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
199 
200 /**
201 \ingroup Exceptions
202 \brief This macro evaluates whether ptr is not null and if it turns out to be
203 false it will throw an ExpressionException with the expression converted
204 into a string.
205  **/
206 #define ARMARX_CHECK_NOT_NULL(ptr) \
207  if (!ptr) \
208  throw ::armarx::exceptions::local::ExpressionException(#ptr " != nullptr") \
209  << "\n(ptr = nullptr)" \
210  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
211 
212 #define ARMARX_CHECK_NULL(ptr) \
213  if (ptr) \
214  throw ::armarx::exceptions::local::ExpressionException(#ptr " == nullptr") \
215  << "\n(ptr = nullptr)" \
216  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
217 
218 #define ARMARX_CHECK_EMPTY(c) \
219  if (!c.empty()) \
220  throw ::armarx::exceptions::local::ExpressionException(#c ".empty()") \
221  << "\n(c = " << c << ")" \
222  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
223 
224 #define ARMARX_CHECK_NOT_EMPTY(c) \
225  if (c.empty()) \
226  throw ::armarx::exceptions::local::ExpressionException("not " #c ".empty()") \
227  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
228 
229 #define ARMARX_CHECK_MULTIPLE_OF(val, mod) \
230  if ((val % mod) != 0) \
231  throw ::armarx::exceptions::local::ExpressionException("valis a multiple of mod") \
232  << "\n(val = " << val << ", mod = " << mod << ")" \
233  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
234 
235 #define ARMARX_CHECK_NOT_MULTIPLE_OF(val, mod) \
236  if ((val % mod) == 0) \
237  throw ::armarx::exceptions::local::ExpressionException("val is not a multiple of mod") \
238  << "\n(val = " << val << ", mod = " << mod << ")" \
239  << "\n(at " << __FILE__ << ':' << __LINE__ << " in " << __FUNCTION__ << "(...))\n"
240 /**
241 \ingroup Exceptions
242 \brief This macro evaluates the expression and if it turns out to be
243 false it will throw an exception of the given type.
244  **/
245 #define ARMARX_CHECK_AND_THROW(expression, ExceptionType) \
246  do \
247  { \
248  if (!(expression)) \
249  throw ExceptionType(#expression); \
250  } while (0);
251 
252 #undef eigen_assert
253 #define eigen_assert(expression) ARMARX_CHECK_EXPRESSION(expression)
armarx::exceptions::local::ExpressionException::name
std::string name() const override
Definition: ExpressionException.h:50
LocalException.h
armarx::exceptions::local
Definition: DynamicLibraryException.h:31
armarx::exceptions::local::ExpressionException::ExpressionException
ExpressionException(std::string expression) noexcept
Definition: ExpressionException.h:42
armarx::exceptions::local::ExpressionException::getExpression
std::string getExpression() const
Definition: ExpressionException.h:56
armarx::exceptions::local::ExpressionException
This exception is thrown if the macro ARMARX_CHECK_EXPRESSION is used.
Definition: ExpressionException.h:39