32 #ifndef CPL_BASE_H_INCLUDED
33 #define CPL_BASE_H_INCLUDED
45 #if defined(_WIN32) && !defined(WIN32)
49 #if defined(_WINDOWS) && !defined(WIN32)
58 # ifndef _CRT_SECURE_NO_DEPRECATE
59 # define _CRT_SECURE_NO_DEPRECATE
61 # ifndef _CRT_NONSTDC_NO_DEPRECATE
62 # define _CRT_NONSTDC_NO_DEPRECATE
66 #include "cpl_config.h"
73 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
74 #error "Unexpected value for SIZEOF_INT"
77 #if !defined(SIZEOF_UNSIGNED_LONG) || (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
78 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
81 #if !defined(SIZEOF_VOIDP) || (SIZEOF_VOIDP != 4 && SIZEOF_VOIDP != 8)
82 #error "Unexpected value for SIZEOF_VOIDP"
95 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
96 # define _LARGEFILE64_SOURCE 1
105 #if defined(HAVE_ICONV)
106 # define CPL_RECODE_ICONV
109 #define CPL_RECODE_STUB
119 #if defined(__MINGW32__)
120 #ifndef __MSVCRT_VERSION__
121 #define __MSVCRT_VERSION__ 0x0700
126 #if defined(GDAL_COMPILATION) && defined(__sun__) && __STDC_VERSION__ >= 201112L && _XOPEN_SOURCE < 600
130 #define _XOPEN_SOURCE 600
147 #if defined(HAVE_ERRNO_H)
160 # include <strings.h>
163 #if defined(HAVE_LIBDBMALLOC) && defined(HAVE_DBMALLOC_H) && defined(DEBUG)
165 # include <dbmalloc.h>
168 #if !defined(DBMALLOC) && defined(HAVE_DMALLOC_H)
170 # include <dmalloc.h>
185 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
186 # if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
187 # error Must have C++11 or newer.
189 # if __cplusplus >= 201402L
190 # define HAVE_CXX14 1
198 #if UINT_MAX == 65535
216 #ifndef CPL_GBOOL_DEFINED
218 #define CPL_GBOOL_DEFINED
226 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
227 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
229 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
230 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
238 #if defined(WIN32) && defined(_MSC_VER)
239 #define VSI_LARGE_API_SUPPORTED
252 #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
254 #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
256 #define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
259 #define CPL_HAS_GINT64 1
270 #define GINT64_MIN GINTBIG_MIN
272 #define GINT64_MAX GINTBIG_MAX
274 #define GUINT64_MAX GUINTBIG_MAX
283 #define GINTBIG_MIN INT_MIN
284 #define GINTBIG_MAX INT_MAX
285 #define GUINTBIG_MAX UINT_MAX
288 #if SIZEOF_VOIDP == 8
296 #ifdef GDAL_COMPILATION
299 typedef uintptr_t GUIntptr_t;
300 #elif SIZEOF_VOIDP == 8
303 typedef unsigned int GUIntptr_t;
306 #define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
310 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
311 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
314 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
316 #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
320 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
322 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
325 #define GUINTBIG_TO_DOUBLE(x) CPL_STATIC_CAST(double, x)
329 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
330 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
332 #define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
341 # define CPL_C_START extern "C" {
350 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
351 # define CPL_DLL __declspec(dllexport)
353 # if defined(USE_GCC_VISIBILITY_FLAG)
354 # define CPL_DLL __attribute__ ((visibility("default")))
363 #ifdef CPL_OPTIONAL_APIS
364 # define CPL_ODLL CPL_DLL
371 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
372 # define CPL_STDCALL __stdcall
380 # define FORCE_CDECL __cdecl
388 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
389 #define HAS_CPL_INLINE 1
390 #define CPL_INLINE __inline
391 #elif defined(__SUNPRO_CC)
392 #define HAS_CPL_INLINE 1
393 #define CPL_INLINE inline
401 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
403 # define MAX(a,b) (((a)>(b)) ? (a) : (b))
408 # define ABS(x) (((x)<0) ? (-1*(x)) : (x))
413 # define M_PI 3.14159265358979323846
424 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
433 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
435 static inline int CPL_afl_friendly_memcmp(
const void* ptr1,
const void* ptr2,
size_t len)
436 __attribute__((always_inline));
438 static inline int CPL_afl_friendly_memcmp(
const void* ptr1,
const void* ptr2,
size_t len)
440 const unsigned char* bptr1 = (
const unsigned char*)ptr1;
441 const unsigned char* bptr2 = (
const unsigned char*)ptr2;
444 unsigned char b1 = *(bptr1++);
445 unsigned char b2 = *(bptr2++);
446 if( b1 != b2 )
return b1 - b2;
451 static inline int CPL_afl_friendly_strcmp(
const char* ptr1,
const char* ptr2)
452 __attribute__((always_inline));
454 static inline int CPL_afl_friendly_strcmp(
const char* ptr1,
const char* ptr2)
456 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
457 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
460 unsigned char ch1 = *(usptr1++);
461 unsigned char ch2 = *(usptr2++);
462 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
466 static inline int CPL_afl_friendly_strncmp(
const char* ptr1,
const char* ptr2,
size_t len)
467 __attribute__((always_inline));
469 static inline int CPL_afl_friendly_strncmp(
const char* ptr1,
const char* ptr2,
size_t len)
471 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
472 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
475 unsigned char ch1 = *(usptr1++);
476 unsigned char ch2 = *(usptr2++);
477 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
482 static inline int CPL_afl_friendly_strcasecmp(
const char* ptr1,
const char* ptr2)
483 __attribute__((always_inline));
485 static inline int CPL_afl_friendly_strcasecmp(
const char* ptr1,
const char* ptr2)
487 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
488 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
491 unsigned char ch1 = *(usptr1++);
492 unsigned char ch2 = *(usptr2++);
493 ch1 = (
unsigned char)toupper(ch1);
494 ch2 = (
unsigned char)toupper(ch2);
495 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
499 static inline int CPL_afl_friendly_strncasecmp(
const char* ptr1,
const char* ptr2,
size_t len)
500 __attribute__((always_inline));
502 static inline int CPL_afl_friendly_strncasecmp(
const char* ptr1,
const char* ptr2,
size_t len)
504 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
505 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
508 unsigned char ch1 = *(usptr1++);
509 unsigned char ch2 = *(usptr2++);
510 ch1 = (
unsigned char)toupper(ch1);
511 ch2 = (
unsigned char)toupper(ch2);
512 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
517 static inline char* CPL_afl_friendly_strstr(
const char* haystack,
const char* needle)
518 __attribute__((always_inline));
520 static inline char* CPL_afl_friendly_strstr(
const char* haystack,
const char* needle)
522 const char* ptr_haystack = haystack;
525 const char* ptr_haystack2 = ptr_haystack;
526 const char* ptr_needle = needle;
529 char ch1 = *(ptr_haystack2++);
530 char ch2 = *(ptr_needle++);
532 return (
char*)ptr_haystack;
536 if( *ptr_haystack == 0 )
544 #define memcmp CPL_afl_friendly_memcmp
545 #define strcmp CPL_afl_friendly_strcmp
546 #define strncmp CPL_afl_friendly_strncmp
547 #define strcasecmp CPL_afl_friendly_strcasecmp
548 #define strncasecmp CPL_afl_friendly_strncasecmp
549 #define strstr CPL_afl_friendly_strstr
554 # define STRCASECMP(a,b) (stricmp(a,b))
555 # define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
558 # define STRCASECMP(a,b) (strcasecmp(a,b))
560 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
563 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
565 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
572 #ifndef STARTS_WITH_CI
574 #define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
576 #define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
580 #ifndef CPL_THREADLOCAL
581 # define CPL_THREADLOCAL
596 # define CPLIsNan(x) _isnan(x)
597 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
598 # define CPLIsFinite(x) _finite(x)
599 #elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
602 # define CPLIsNan(x) __builtin_isnan(x)
603 # define CPLIsInf(x) __builtin_isinf(x)
604 # define CPLIsFinite(x) __builtin_isfinite(x)
605 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
610 static inline int CPLIsNan(
float f) {
return std::isnan(f); }
611 static inline int CPLIsNan(
double f) {
return std::isnan(f); }
612 static inline int CPLIsInf(
float f) {
return std::isinf(f); }
613 static inline int CPLIsInf(
double f) {
return std::isinf(f); }
614 static inline int CPLIsFinite(
float f) {
return std::isfinite(f); }
615 static inline int CPLIsFinite(
double f) {
return std::isfinite(f); }
619 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
623 static inline int CPLIsNan(
float f) {
return __isnanf(f); }
624 static inline int CPLIsNan(
double f) {
return __isnan(f); }
625 static inline int CPLIsInf(
float f) {
return __isinff(f); }
626 static inline int CPLIsInf(
double f) {
return __isinf(f); }
627 static inline int CPLIsFinite(
float f) {
return !__isnanf(f) && !__isinff(f); }
628 static inline int CPLIsFinite(
double f) {
return !__isnan(f) && !__isinf(f); }
631 # define CPLIsNan(x) isnan(x)
632 # if defined(isinf) || defined(__FreeBSD__)
634 # define CPLIsInf(x) isinf(x)
636 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
637 # elif defined(__sun__)
639 # define CPLIsInf(x) (!finite(x) && !isnan(x))
640 # define CPLIsFinite(x) finite(x)
642 # define CPLIsInf(x) (0)
643 # define CPLIsFinite(x) (!isnan(x))
656 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
660 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
665 # define CPL_IS_LSB 1
667 # define CPL_IS_LSB 0
671 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
676 template <
bool b>
struct CPLStaticAssert {};
677 template<>
struct CPLStaticAssert<true>
679 static void my_function() {}
684 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
685 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
689 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
699 #define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
701 #if defined(HAVE_GCC_BSWAP) && (defined(__i386__) || defined(__x86_64__))
705 #include <x86intrin.h>
707 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
709 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
710 #elif defined(_MSC_VER)
711 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
712 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
715 #define CPL_SWAP32(x) \
716 CPL_STATIC_CAST(GUInt32, \
717 ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
718 ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
719 ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
720 ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
723 #define CPL_SWAP64(x) \
724 ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
725 (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
730 #define CPL_SWAP16PTR(x) \
732 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
733 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
735 byTemp = _pabyDataT[0]; \
736 _pabyDataT[0] = _pabyDataT[1]; \
737 _pabyDataT[1] = byTemp; \
740 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
743 #define CPL_SWAP32PTR(x) \
745 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
746 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
748 byTemp = _pabyDataT[0]; \
749 _pabyDataT[0] = _pabyDataT[3]; \
750 _pabyDataT[3] = byTemp; \
751 byTemp = _pabyDataT[1]; \
752 _pabyDataT[1] = _pabyDataT[2]; \
753 _pabyDataT[2] = byTemp; \
757 #define CPL_SWAP64PTR(x) \
759 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
760 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
762 byTemp = _pabyDataT[0]; \
763 _pabyDataT[0] = _pabyDataT[7]; \
764 _pabyDataT[7] = byTemp; \
765 byTemp = _pabyDataT[1]; \
766 _pabyDataT[1] = _pabyDataT[6]; \
767 _pabyDataT[6] = byTemp; \
768 byTemp = _pabyDataT[2]; \
769 _pabyDataT[2] = _pabyDataT[5]; \
770 _pabyDataT[5] = byTemp; \
771 byTemp = _pabyDataT[3]; \
772 _pabyDataT[3] = _pabyDataT[4]; \
773 _pabyDataT[4] = byTemp; \
779 #define CPL_SWAP32PTR(x) \
783 memcpy(&_n32, _lx, 4); \
784 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
785 _n32 = CPL_SWAP32(_n32); \
786 memcpy(_lx, &_n32, 4); \
790 #define CPL_SWAP64PTR(x) \
794 memcpy(&_n64, _lx, 8); \
795 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
796 _n64 = CPL_SWAP64(_n64); \
797 memcpy(_lx, &_n64, 8); \
803 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
806 # define CPL_MSBWORD16(x) (x)
807 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
808 # define CPL_MSBWORD32(x) (x)
809 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
810 # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
811 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
812 # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
813 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
814 # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
815 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
818 # define CPL_LSBWORD16(x) (x)
820 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
822 # define CPL_LSBWORD32(x) (x)
824 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
826 # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
828 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
830 # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
832 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
834 # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
836 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
842 #define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
847 #define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
848 (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
851 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
854 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
857 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
860 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
864 #ifndef UNREFERENCED_PARAM
865 # ifdef UNREFERENCED_PARAMETER
866 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
868 # define UNREFERENCED_PARAM(param) ((void)param)
882 #ifndef DISABLE_CVSID
883 #if defined(__GNUC__) && __GNUC__ >= 4
884 # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
886 # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
887 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
890 # define CPL_CVSID(string)
895 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
897 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
900 # define CPL_NULL_TERMINATED
903 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
905 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
907 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
910 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
912 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
915 #if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
919 # define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
922 # define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
925 # define CPL_FORMAT_STRING(arg) arg
927 # define CPL_SCANF_FORMAT_STRING(arg) arg
930 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
932 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
935 #define CPL_WARN_UNUSED_RESULT
938 #if defined(__GNUC__) && __GNUC__ >= 4
940 # define CPL_UNUSED __attribute((__unused__))
947 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
949 #define CPL_NO_RETURN __attribute__((noreturn))
952 #define CPL_NO_RETURN
957 #ifndef __has_attribute
958 #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
963 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
965 # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
968 # define CPL_RETURNS_NONNULL
971 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
973 #define CPL_RESTRICT __restrict__
979 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
983 # define CPL_OVERRIDE override
986 # define CPL_FINAL final
993 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
994 ClassName( const ClassName & ) = delete; \
995 ClassName &operator=( const ClassName & ) = delete;
999 #if !defined(DOXYGEN_SKIP)
1000 #if defined(__has_extension)
1001 #if __has_extension(attribute_deprecated_with_message)
1003 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
1005 #define CPL_WARN_DEPRECATED(x)
1007 #elif defined(__GNUC__)
1008 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
1010 #define CPL_WARN_DEPRECATED(x)
1014 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1016 # if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1017 int vsnprintf(
char *str,
size_t size,
const char* fmt, va_list args)
1018 CPL_WARN_DEPRECATED(
"Use CPLvsnprintf() instead");
1019 int snprintf(
char *str,
size_t size,
const char* fmt, ...)
1022 int sprintf(
char *str, const
char* fmt, ...)
1025 # elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1026 int sprintf(
char *str,
const char* fmt, ...)
1027 CPL_PRINT_FUNC_FORMAT(2, 3)
1028 CPL_WARN_DEPRECATED("Use snprintf() or
CPLsnprintf() instead");
1033 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
1035 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
1039 #if defined(__cplusplus)
1041 #define CPL_ARRAYSIZE(array) \
1042 ((sizeof(array) / sizeof(*(array))) / \
1043 static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1046 template<
class T>
static void CPL_IGNORE_RET_VAL(T) {}
1047 inline static bool CPL_TO_BOOL(
int x) {
return x != 0; }
1052 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
1053 #define HAVE_GCC_DIAGNOSTIC_PUSH
1056 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
1057 #define HAVE_GCC_SYSTEM_HEADER
1060 #if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1062 # define CPL_FALLTHROUGH [[clang::fallthrough]];
1065 # define CPL_FALLTHROUGH
1073 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
1075 class MSVCPedanticBool
1078 friend bool operator== (
const bool& one,
const MSVCPedanticBool& other);
1079 friend bool operator!= (
const bool& one,
const MSVCPedanticBool& other);
1082 MSVCPedanticBool(
int bIn);
1087 MSVCPedanticBool() {}
1088 MSVCPedanticBool(
bool bIn) : b(bIn) {}
1089 MSVCPedanticBool(
const MSVCPedanticBool& other) : b(other.b) {}
1091 MSVCPedanticBool& operator= (
const MSVCPedanticBool& other) { b = other.b;
return *
this; }
1092 MSVCPedanticBool& operator&= (
const MSVCPedanticBool& other) { b &= other.b;
return *
this; }
1093 MSVCPedanticBool& operator|= (
const MSVCPedanticBool& other) { b |= other.b;
return *
this; }
1095 bool operator== (
const bool& other)
const {
return b == other; }
1096 bool operator!= (
const bool& other)
const {
return b != other; }
1097 bool operator< (
const bool& other)
const {
return b < other; }
1098 bool operator== (
const MSVCPedanticBool& other)
const {
return b == other.b; }
1099 bool operator!= (
const MSVCPedanticBool& other)
const {
return b != other.b; }
1100 bool operator< (
const MSVCPedanticBool& other)
const {
return b < other.b; }
1102 bool operator! ()
const {
return !b; }
1103 operator bool()
const {
return b; }
1104 operator int()
const {
return b; }
1105 operator GIntBig()
const {
return b; }
1108 inline bool operator== (
const bool& one,
const MSVCPedanticBool& other) {
return one == other.b; }
1109 inline bool operator!= (
const bool& one,
const MSVCPedanticBool& other) {
return one != other.b; }
1120 #include <algorithm>
1121 #include <functional>
1125 #include <unordered_map>
1127 #include <unordered_set>
1139 #define EMULATED_BOOL int
1142 #define bool MSVCPedanticBool
1146 #define VOLATILE_BOOL bool
1158 #define EMULATED_BOOL bool
1159 #define VOLATILE_BOOL volatile bool
1163 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1164 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1166 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1171 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1172 #define CPL_NULLPTR nullptr
1174 #define CPL_NULLPTR NULL
1183 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:341
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:221
int GInt32
Int32 type.
Definition: cpl_port.h:203
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:205
short GInt16
Int16 type.
Definition: cpl_port.h:209
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:213
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:910
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:267
GIntBig GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:290
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1190
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:249
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:211
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1337
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:343
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:246
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:265