65 #ifndef IRPLIB_UTILS_H
66 #define IRPLIB_UTILS_H
80 #define IRPLIB_XSTRINGIFY(TOSTRING) #TOSTRING
81 #define IRPLIB_STRINGIFY(TOSTRING) IRPLIB_XSTRINGIFY(TOSTRING)
83 #if !defined __GNUC__ && !defined __inline__
84 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
85 #define __inline__ inline
91 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
92 #define IRPLIB_DIAG_PRAGMA_PUSH_IGN(x) \
93 _Pragma("GCC diagnostic push") \
94 _Pragma(IRPLIB_STRINGIFY(GCC diagnostic ignored #x))
95 #define IRPLIB_DIAG_PRAGMA_PUSH_ERR(x) \
96 _Pragma("GCC diagnostic push") \
97 _Pragma(IRPLIB_STRINGIFY(GCC diagnostic error #x))
98 #define IRPLIB_DIAG_PRAGMA_POP \
99 _Pragma("GCC diagnostic pop")
101 #define IRPLIB_DIAG_PRAGMA_PUSH_IGN(x)
102 #define IRPLIB_DIAG_PRAGMA_PUSH_ERR(x)
103 #define IRPLIB_DIAG_PRAGMA_POP
109 #define irplib_trace() do if (cpl_error_get_code()) { \
110 cpl_msg_debug(cpl_func, __FILE__ " at line %d: ERROR '%s' at %s", \
111 __LINE__, cpl_error_get_message(), cpl_error_get_where()); \
113 cpl_msg_debug(cpl_func, __FILE__ " at line %d: OK", __LINE__); \
116 #define irplib_error_recover(ESTATE, ...) \
117 do if (!cpl_errorstate_is_equal(ESTATE)) { \
118 cpl_msg_warning(cpl_func, __VA_ARGS__); \
119 cpl_msg_indent_more(); \
120 cpl_errorstate_dump(ESTATE, CPL_FALSE, \
121 cpl_errorstate_dump_one_warning); \
122 cpl_msg_indent_less(); \
123 cpl_errorstate_set(ESTATE); \
136 #define IRPLIB_UTIL_SET_ROW(table_set_row) \
137 cpl_boolean table_set_row(cpl_table *, \
141 const cpl_parameterlist *)
152 #define IRPLIB_UTIL_CHECK(table_check) \
153 cpl_error_code table_check(cpl_table *, \
154 const cpl_frameset *, \
155 const cpl_parameterlist *)
206 #define skip_if(CONDITION) \
208 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
209 goto cleanup, "Propagating a pre-existing error"); \
210 cpl_error_ensure(!(CONDITION), cpl_error_get_code(), \
211 goto cleanup, "Propagating error");\
226 #define skip_if_lt(A, B, ...) \
229 const double irplib_utils_a = (double)(A); \
230 const double irplib_utils_b = (double)(B); \
232 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
233 goto cleanup, "Propagating a pre-existing error"); \
234 if (irplib_utils_a < irplib_utils_b) { \
235 char * irplib_utils_msg = cpl_sprintf(__VA_ARGS__); \
236 (void)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND, \
237 "Need at least %g (not %g) %s", \
238 irplib_utils_b, irplib_utils_a, \
240 cpl_free(irplib_utils_msg); \
253 #define bug_if(CONDITION) \
255 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
256 goto cleanup, "Propagating an unexpected error, " \
257 "please report to " PACKAGE_BUGREPORT); \
258 cpl_error_ensure(!(CONDITION), CPL_ERROR_UNSPECIFIED, \
259 goto cleanup, "Internal error, please report to " \
260 PACKAGE_BUGREPORT); \
275 #define error_if(CONDITION, ERROR, ...) \
276 cpl_error_ensure(cpl_error_get_code() == CPL_ERROR_NONE && \
277 !(CONDITION), ERROR, goto cleanup, __VA_ARGS__)
286 #define any_if(...) \
287 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
288 goto cleanup, __VA_ARGS__)
300 if (cpl_error_get_code()) \
301 cpl_msg_debug(cpl_func, "Cleanup in " __FILE__ " line %u with " \
302 "error '%s' at %s", __LINE__, \
303 cpl_error_get_message(), cpl_error_get_where()); \
305 cpl_msg_debug(cpl_func, "Cleanup in " __FILE__ " line %u", \
323 #define irplib_ensure(CONDITION, ec, ...) \
324 cpl_error_ensure(CONDITION, ec, goto cleanup, __VA_ARGS__)
358 #define irplib_check(COMMAND, ...) \
360 cpl_errorstate irplib_check_prestate = cpl_errorstate_get(); \
364 irplib_ensure(cpl_errorstate_is_equal(irplib_check_prestate), \
365 cpl_error_get_code(), __VA_ARGS__); \
374 const cpl_parameterlist *,
375 const cpl_frameset *,
380 const cpl_propertylist *,
387 const cpl_parameterlist *,
388 const cpl_frameset *,
391 const cpl_propertylist *,
397 const cpl_parameterlist *,
398 const cpl_frameset *,
399 const cpl_imagelist *,
403 const cpl_propertylist *,
409 const cpl_parameterlist *,
410 const cpl_frameset *,
412 const cpl_propertylist *,
415 const cpl_propertylist *,
422 const cpl_parameterlist *,
423 const cpl_frameset *,
428 const cpl_propertylist *,
442 #if defined HAVE_ISNAN && HAVE_ISNAN != 0
443 #if !defined isnan && defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 0
452 int,
char,
const char *,
const char *,
453 const cpl_parameterlist *,
const char *,
454 const cpl_propertylist *,
const cpl_propertylist *,
455 const char *,
const char *,
const char *,
456 cpl_boolean (*)(cpl_table *,
const char *,
int,
458 const cpl_parameterlist *),
459 cpl_error_code (*)(cpl_table *,
460 const cpl_frameset *,
461 const cpl_parameterlist *));
464 const cpl_frameset *,
467 const cpl_parameterlist *,
469 (cpl_table *,
const char *,
470 int,
const cpl_frame *,
471 const cpl_parameterlist *));
474 cpl_image *, cpl_image *, cpl_image *,
478 cpl_boolean, cpl_boolean, cpl_boolean);
484 cpl_polynomial * irplib_polynomial_fit_1d_create(
485 const cpl_vector * x_pos,
486 const cpl_vector * values,
490 cpl_polynomial * irplib_polynomial_fit_1d_create_chiq(
491 const cpl_vector * x_pos,
492 const cpl_vector * values,
504 cpl_error_code irplib_frameset_sort(
505 const cpl_frameset *
self,
511 void * irplib_aligned_malloc(
size_t alignment,
size_t size) CPL_ATTR_ALLOC;
512 void * irplib_aligned_calloc(
size_t alignment,
513 size_t nelem,
size_t nbytes) CPL_ATTR_ALLOC;
514 void irplib_aligned_free (
void * aligned_ptr);
527 static __inline__
int irplib_isinf(
double value)
530 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
531 return __builtin_isinf(value);
532 #elif defined HAVE_ISINF && HAVE_ISINF
535 return value != 0 && value == 2 * value;
545 static __inline__
int irplib_isnan(
double value)
548 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
549 return __builtin_isnan(value);
550 #elif defined HAVE_ISNAN && HAVE_ISNAN
553 return value != value;