|
19 | 19 |
|
20 | 20 | #include "rcljava_common/visibility_control.hpp" |
21 | 21 |
|
| 22 | +/// Execute \a error_statement if an exception has happened. |
| 23 | +/** |
| 24 | + * \param env a JNIEnv pointer, used to check for exceptions. |
| 25 | + * \param error_statement statement executed if an exception has happened. |
| 26 | + */ |
| 27 | +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, error_statement) \ |
| 28 | + do { \ |
| 29 | + if (env->ExceptionCheck()) { \ |
| 30 | + error_statement; \ |
| 31 | + } \ |
| 32 | + } while (0) |
| 33 | + |
| 34 | +/// Return from the current function if a java exception has happened. |
| 35 | +/** |
| 36 | + * \param env a JNIEnv pointer, used to check for exceptions. |
| 37 | + */ |
| 38 | +#define RCLJAVA_COMMON_CHECK_FOR_EXCEPTION(env) \ |
| 39 | + RCLJAVA_COMMON_CHECK_FOR_EXCEPTION_WITH_ERROR_STATEMENT(env, return ) |
| 40 | + |
| 41 | +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK, |
| 42 | +/// and execute \a error_statement in that case. |
| 43 | +/** |
| 44 | + * The rcl error message will be added to \a base_message, and the rcl error state will be reset. |
| 45 | + * |
| 46 | + * \param env a JNIEnv pointer, used to throw a java exception from the rcl error. |
| 47 | + * \param ret rcl_ret_t error that will be checked. |
| 48 | + * \param base_message error message that will be passed to the thrown exception. |
| 49 | + * The complete error will be "${base_message}: ${rcl_error_string}". |
| 50 | + * \a base_message can be either a `const char *` or as `std::string`. |
| 51 | + * \param error_statement statement executed if ret was not RCL_RET_OK. |
| 52 | + */ |
| 53 | +#define RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT( \ |
| 54 | + env, ret, base_message, error_statement) \ |
| 55 | + do { \ |
| 56 | + if (RCL_RET_OK != ret) { \ |
| 57 | + rcljava_common::exceptions::rcljava_throw_rclexception( \ |
| 58 | + env, ret, static_cast<std::string>(base_message) + ": " + rcl_get_error_string().str); \ |
| 59 | + rcl_reset_error(); \ |
| 60 | + error_statement; \ |
| 61 | + } \ |
| 62 | + } while (0) |
| 63 | + |
| 64 | +/// Call \ref rcljava_throw_rclexception if \a ret is not RCL_RET_OK and return. |
| 65 | +/** |
| 66 | + * \param env a JNIEnv pointer, used to check for exceptions. |
| 67 | + * \param ret rcl_ret_t error that will be checked. |
| 68 | + * \param base_message error message that will be passed to the thrown exception. |
| 69 | + */ |
| 70 | +#define RCLJAVA_COMMON_THROW_FROM_RCL(env, ret, base_message) \ |
| 71 | + RCLJAVA_COMMON_THROW_FROM_RCL_WITH_ERROR_STATEMENT(env, ret, base_message, return ) |
| 72 | + |
22 | 73 | namespace rcljava_common |
23 | 74 | { |
24 | 75 | namespace exceptions |
|
0 commit comments