62 #ifndef IRPLIB_UTILS_H
63 #define IRPLIB_UTILS_H
77 #define IRPLIB_XSTRINGIFY(TOSTRING) #TOSTRING
78 #define IRPLIB_STRINGIFY(TOSTRING) IRPLIB_XSTRINGIFY(TOSTRING)
80 #if !defined __GNUC__ && !defined __inline__
81 #if defined __STDC_VERSION__ && __STDC_VERSION__ >= 199901L
82 #define __inline__ inline
88 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)
89 #define IRPLIB_DIAG_PRAGMA_PUSH_IGN(x) \
90 _Pragma("GCC diagnostic push") \
91 _Pragma(IRPLIB_STRINGIFY(GCC diagnostic ignored #x))
92 #define IRPLIB_DIAG_PRAGMA_PUSH_ERR(x) \
93 _Pragma("GCC diagnostic push") \
94 _Pragma(IRPLIB_STRINGIFY(GCC diagnostic error #x))
95 #define IRPLIB_DIAG_PRAGMA_POP \
96 _Pragma("GCC diagnostic pop")
98 #define IRPLIB_DIAG_PRAGMA_PUSH_IGN(x)
99 #define IRPLIB_DIAG_PRAGMA_PUSH_ERR(x)
100 #define IRPLIB_DIAG_PRAGMA_POP
106 #define irplib_trace() do if (cpl_error_get_code()) { \
107 cpl_msg_debug(cpl_func, __FILE__ " at line %d: ERROR '%s' at %s", \
108 __LINE__, cpl_error_get_message(), cpl_error_get_where()); \
110 cpl_msg_debug(cpl_func, __FILE__ " at line %d: OK", __LINE__); \
113 #define irplib_error_recover(ESTATE, ...) \
114 do if (!cpl_errorstate_is_equal(ESTATE)) { \
115 cpl_msg_warning(cpl_func, __VA_ARGS__); \
116 cpl_msg_indent_more(); \
117 cpl_errorstate_dump(ESTATE, CPL_FALSE, \
118 cpl_errorstate_dump_one_warning); \
119 cpl_msg_indent_less(); \
120 cpl_errorstate_set(ESTATE); \
133 #define IRPLIB_UTIL_SET_ROW(table_set_row) \
134 cpl_boolean table_set_row(cpl_table *, \
138 const cpl_parameterlist *)
149 #define IRPLIB_UTIL_CHECK(table_check) \
150 cpl_error_code table_check(cpl_table *, \
151 const cpl_frameset *, \
152 const cpl_parameterlist *)
203 #define skip_if(CONDITION) \
205 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
206 goto cleanup, "Propagating a pre-existing error"); \
207 cpl_error_ensure(!(CONDITION), cpl_error_get_code(), \
208 goto cleanup, "Propagating error");\
223 #define skip_if_lt(A, B, ...) \
226 const double irplib_utils_a = (double)(A); \
227 const double irplib_utils_b = (double)(B); \
229 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
230 goto cleanup, "Propagating a pre-existing error"); \
231 if (irplib_utils_a < irplib_utils_b) { \
232 char * irplib_utils_msg = cpl_sprintf(__VA_ARGS__); \
233 (void)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND, \
234 "Need at least %g (not %g) %s", \
235 irplib_utils_b, irplib_utils_a, \
237 cpl_free(irplib_utils_msg); \
250 #define bug_if(CONDITION) \
252 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
253 goto cleanup, "Propagating an unexpected error, " \
254 "please report to " PACKAGE_BUGREPORT); \
255 cpl_error_ensure(!(CONDITION), CPL_ERROR_UNSPECIFIED, \
256 goto cleanup, "Internal error, please report to " \
257 PACKAGE_BUGREPORT); \
272 #define error_if(CONDITION, ERROR, ...) \
273 cpl_error_ensure(cpl_error_get_code() == CPL_ERROR_NONE && \
274 !(CONDITION), ERROR, goto cleanup, __VA_ARGS__)
283 #define any_if(...) \
284 cpl_error_ensure(!cpl_error_get_code(), cpl_error_get_code(), \
285 goto cleanup, __VA_ARGS__)
297 if (cpl_error_get_code()) \
298 cpl_msg_debug(cpl_func, "Cleanup in " __FILE__ " line %u with " \
299 "error '%s' at %s", __LINE__, \
300 cpl_error_get_message(), cpl_error_get_where()); \
302 cpl_msg_debug(cpl_func, "Cleanup in " __FILE__ " line %u", \
320 #define irplib_ensure(CONDITION, ec, ...) \
321 cpl_error_ensure(CONDITION, ec, goto cleanup, __VA_ARGS__)
355 #define irplib_check(COMMAND, ...) \
357 cpl_errorstate irplib_check_prestate = cpl_errorstate_get(); \
361 irplib_ensure(cpl_errorstate_is_equal(irplib_check_prestate), \
362 cpl_error_get_code(), __VA_ARGS__); \
371 const cpl_parameterlist *,
372 const cpl_frameset *,
377 const cpl_propertylist *,
384 const cpl_parameterlist *,
385 const cpl_frameset *,
388 const cpl_propertylist *,
394 const cpl_parameterlist *,
395 const cpl_frameset *,
396 const cpl_imagelist *,
400 const cpl_propertylist *,
406 const cpl_parameterlist *,
407 const cpl_frameset *,
409 const cpl_propertylist *,
412 const cpl_propertylist *,
419 const cpl_parameterlist *,
420 const cpl_frameset *,
425 const cpl_propertylist *,
439 #if defined HAVE_ISNAN && HAVE_ISNAN != 0
440 #if !defined isnan && defined HAVE_DECL_ISNAN && HAVE_DECL_ISNAN == 0
449 int,
char,
const char *,
const char *,
450 const cpl_parameterlist *,
const char *,
451 const cpl_propertylist *,
const cpl_propertylist *,
452 const char *,
const char *,
const char *,
453 cpl_boolean (*)(cpl_table *,
const char *,
int,
455 const cpl_parameterlist *),
456 cpl_error_code (*)(cpl_table *,
457 const cpl_frameset *,
458 const cpl_parameterlist *));
461 const cpl_frameset *,
464 const cpl_parameterlist *,
466 (cpl_table *,
const char *,
467 int,
const cpl_frame *,
468 const cpl_parameterlist *));
471 cpl_image *, cpl_image *, cpl_image *,
475 cpl_boolean, cpl_boolean, cpl_boolean);
481 cpl_polynomial * irplib_polynomial_fit_1d_create(
482 const cpl_vector * x_pos,
483 const cpl_vector * values,
487 cpl_polynomial * irplib_polynomial_fit_1d_create_chiq(
488 const cpl_vector * x_pos,
489 const cpl_vector * values,
501 cpl_error_code irplib_frameset_sort(
502 const cpl_frameset *
self,
508 void * irplib_aligned_malloc(
size_t alignment,
size_t size) CPL_ATTR_ALLOC;
509 void * irplib_aligned_calloc(
size_t alignment,
510 size_t nelem,
size_t nbytes) CPL_ATTR_ALLOC;
511 void irplib_aligned_free (
void * aligned_ptr);
524 static __inline__
int irplib_isinf(
double value)
527 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
528 return __builtin_isinf(value);
529 #elif defined HAVE_ISINF && HAVE_ISINF
532 return value != 0 && value == 2 * value;
542 static __inline__
int irplib_isnan(
double value)
545 #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)
546 return __builtin_isnan(value);
547 #elif defined HAVE_ISNAN && HAVE_ISNAN
550 return value != value;