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__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 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 || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
190 # define HAVE_CXX14 1
192 # if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
193 # define HAVE_CXX17 1
200 #if UINT_MAX == 65535
218 #ifndef CPL_GBOOL_DEFINED
220 #define CPL_GBOOL_DEFINED
228 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
229 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
231 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
232 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
240 #if defined(WIN32) && defined(_MSC_VER)
241 #define VSI_LARGE_API_SUPPORTED
254 #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
256 #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
258 #define GUINTBIG_MAX ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
261 #define CPL_HAS_GINT64 1
272 #define GINT64_MIN GINTBIG_MIN
274 #define GINT64_MAX GINTBIG_MAX
276 #define GUINT64_MAX GUINTBIG_MAX
280 #error "64bit integer support required"
284 #if SIZEOF_VOIDP == 8
292 #ifdef GDAL_COMPILATION
295 typedef uintptr_t GUIntptr_t;
296 #elif SIZEOF_VOIDP == 8
299 typedef unsigned int GUIntptr_t;
302 #define CPL_IS_ALIGNED(ptr, quant) ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void*, ptr)) % (quant)) == 0)
306 #if defined(__MSVCRT__) || (defined(WIN32) && defined(_MSC_VER))
307 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
310 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
312 #define CPL_FRMT_GB_WITHOUT_PREFIX "l"
316 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
318 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
321 #define GUINTBIG_TO_DOUBLE(x) CPL_STATIC_CAST(double, x)
325 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
326 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
328 #define CPL_INT64_FITS_ON_INT32(x) (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
337 # define CPL_C_START extern "C" {
346 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
347 # ifdef GDAL_COMPILATION
348 # define CPL_DLL __declspec(dllexport)
352 # define CPL_INTERNAL
354 # if defined(USE_GCC_VISIBILITY_FLAG)
355 # define CPL_DLL __attribute__ ((visibility("default")))
356 # if !defined(__MINGW32__)
357 # define CPL_INTERNAL __attribute__((visibility("hidden")))
359 # define CPL_INTERNAL
363 # define CPL_INTERNAL
368 #define CPL_UNSTABLE_API CPL_DLL
374 #ifdef CPL_OPTIONAL_APIS
375 # define CPL_ODLL CPL_DLL
382 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
383 # define CPL_STDCALL __stdcall
391 # define FORCE_CDECL __cdecl
399 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
400 #define HAS_CPL_INLINE 1
401 #define CPL_INLINE __inline
402 #elif defined(__SUNPRO_CC)
403 #define HAS_CPL_INLINE 1
404 #define CPL_INLINE inline
412 # define MIN(a,b) (((a)<(b)) ? (a) : (b))
414 # define MAX(a,b) (((a)>(b)) ? (a) : (b))
419 # define ABS(x) (((x)<0) ? (-1*(x)) : (x))
424 # define M_PI 3.14159265358979323846
435 # define CPLIsEqual(x,y) (fabs((x) - (y)) < 0.0000000000001)
444 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
446 static inline int CPL_afl_friendly_memcmp(
const void* ptr1,
const void* ptr2,
size_t len)
447 __attribute__((always_inline));
449 static inline int CPL_afl_friendly_memcmp(
const void* ptr1,
const void* ptr2,
size_t len)
451 const unsigned char* bptr1 = (
const unsigned char*)ptr1;
452 const unsigned char* bptr2 = (
const unsigned char*)ptr2;
455 unsigned char b1 = *(bptr1++);
456 unsigned char b2 = *(bptr2++);
457 if( b1 != b2 )
return b1 - b2;
462 static inline int CPL_afl_friendly_strcmp(
const char* ptr1,
const char* ptr2)
463 __attribute__((always_inline));
465 static inline int CPL_afl_friendly_strcmp(
const char* ptr1,
const char* ptr2)
467 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
468 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
471 unsigned char ch1 = *(usptr1++);
472 unsigned char ch2 = *(usptr2++);
473 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
477 static inline int CPL_afl_friendly_strncmp(
const char* ptr1,
const char* ptr2,
size_t len)
478 __attribute__((always_inline));
480 static inline int CPL_afl_friendly_strncmp(
const char* ptr1,
const char* ptr2,
size_t len)
482 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
483 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
486 unsigned char ch1 = *(usptr1++);
487 unsigned char ch2 = *(usptr2++);
488 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
493 static inline int CPL_afl_friendly_strcasecmp(
const char* ptr1,
const char* ptr2)
494 __attribute__((always_inline));
496 static inline int CPL_afl_friendly_strcasecmp(
const char* ptr1,
const char* ptr2)
498 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
499 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
502 unsigned char ch1 = *(usptr1++);
503 unsigned char ch2 = *(usptr2++);
504 ch1 = (
unsigned char)toupper(ch1);
505 ch2 = (
unsigned char)toupper(ch2);
506 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
510 static inline int CPL_afl_friendly_strncasecmp(
const char* ptr1,
const char* ptr2,
size_t len)
511 __attribute__((always_inline));
513 static inline int CPL_afl_friendly_strncasecmp(
const char* ptr1,
const char* ptr2,
size_t len)
515 const unsigned char* usptr1 = (
const unsigned char*)ptr1;
516 const unsigned char* usptr2 = (
const unsigned char*)ptr2;
519 unsigned char ch1 = *(usptr1++);
520 unsigned char ch2 = *(usptr2++);
521 ch1 = (
unsigned char)toupper(ch1);
522 ch2 = (
unsigned char)toupper(ch2);
523 if( ch1 == 0 || ch1 != ch2 )
return ch1 - ch2;
528 static inline char* CPL_afl_friendly_strstr(
const char* haystack,
const char* needle)
529 __attribute__((always_inline));
531 static inline char* CPL_afl_friendly_strstr(
const char* haystack,
const char* needle)
533 const char* ptr_haystack = haystack;
536 const char* ptr_haystack2 = ptr_haystack;
537 const char* ptr_needle = needle;
540 char ch1 = *(ptr_haystack2++);
541 char ch2 = *(ptr_needle++);
543 return (
char*)ptr_haystack;
547 if( *ptr_haystack == 0 )
555 #define memcmp CPL_afl_friendly_memcmp
556 #define strcmp CPL_afl_friendly_strcmp
557 #define strncmp CPL_afl_friendly_strncmp
558 #define strcasecmp CPL_afl_friendly_strcasecmp
559 #define strncasecmp CPL_afl_friendly_strncasecmp
560 #define strstr CPL_afl_friendly_strstr
565 # define STRCASECMP(a,b) (stricmp(a,b))
566 # define STRNCASECMP(a,b,n) (strnicmp(a,b,n))
569 # define STRCASECMP(a,b) (strcasecmp(a,b))
571 # define STRNCASECMP(a,b,n) (strncasecmp(a,b,n))
574 # define EQUALN(a,b,n) (STRNCASECMP(a,b,n)==0)
576 # define EQUAL(a,b) (STRCASECMP(a,b)==0)
583 #ifndef STARTS_WITH_CI
585 #define STARTS_WITH(a,b) (strncmp(a,b,strlen(b)) == 0)
587 #define STARTS_WITH_CI(a,b) EQUALN(a,b,strlen(b))
591 #ifndef CPL_THREADLOCAL
592 # define CPL_THREADLOCAL
607 # define CPLIsNan(x) _isnan(x)
608 # define CPLIsInf(x) (!_isnan(x) && !_finite(x))
609 # define CPLIsFinite(x) _finite(x)
610 #elif defined(__GNUC__) && ( __GNUC__ > 4 || ( __GNUC__ == 4 && __GNUC_MINOR__ >= 4 ) )
613 # define CPLIsNan(x) __builtin_isnan(x)
614 # define CPLIsInf(x) __builtin_isinf(x)
615 # define CPLIsFinite(x) __builtin_isfinite(x)
616 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
621 static inline int CPLIsNan(
float f) {
return std::isnan(f); }
622 static inline int CPLIsNan(
double f) {
return std::isnan(f); }
623 static inline int CPLIsInf(
float f) {
return std::isinf(f); }
624 static inline int CPLIsInf(
double f) {
return std::isinf(f); }
625 static inline int CPLIsFinite(
float f) {
return std::isfinite(f); }
626 static inline int CPLIsFinite(
double f) {
return std::isfinite(f); }
630 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
634 static inline int CPLIsNan(
float f) {
return __isnanf(f); }
635 static inline int CPLIsNan(
double f) {
return __isnan(f); }
636 static inline int CPLIsInf(
float f) {
return __isinff(f); }
637 static inline int CPLIsInf(
double f) {
return __isinf(f); }
638 static inline int CPLIsFinite(
float f) {
return !__isnanf(f) && !__isinff(f); }
639 static inline int CPLIsFinite(
double f) {
return !__isnan(f) && !__isinf(f); }
642 # define CPLIsNan(x) isnan(x)
643 # if defined(isinf) || defined(__FreeBSD__)
645 # define CPLIsInf(x) isinf(x)
647 # define CPLIsFinite(x) (!isnan(x) && !isinf(x))
648 # elif defined(__sun__)
650 # define CPLIsInf(x) (!finite(x) && !isnan(x))
651 # define CPLIsFinite(x) finite(x)
653 # define CPLIsInf(x) (0)
654 # define CPLIsFinite(x) (!isnan(x))
667 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
671 #if ! ( defined(CPL_LSB) || defined(CPL_MSB) )
676 # define CPL_IS_LSB 1
678 # define CPL_IS_LSB 0
682 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
687 template <
bool b>
struct CPLStaticAssert {};
688 template<>
struct CPLStaticAssert<true>
690 static void my_function() {}
695 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
696 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
700 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
710 #define CPL_SWAP16(x) CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | (CPL_STATIC_CAST(GUInt16, x) >> 8) )
712 #if defined(HAVE_GCC_BSWAP) && (defined(__i386__) || defined(__x86_64__))
716 #include <x86intrin.h>
718 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
720 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
721 #elif defined(_MSC_VER)
722 #define CPL_SWAP32(x) CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
723 #define CPL_SWAP64(x) CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
726 #define CPL_SWAP32(x) \
727 CPL_STATIC_CAST(GUInt32, \
728 ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
729 ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
730 ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
731 ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24) )
734 #define CPL_SWAP64(x) \
735 ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) << 32) | \
736 (CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
741 #define CPL_SWAP16PTR(x) \
743 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
744 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2); \
746 byTemp = _pabyDataT[0]; \
747 _pabyDataT[0] = _pabyDataT[1]; \
748 _pabyDataT[1] = byTemp; \
751 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
754 #define CPL_SWAP32PTR(x) \
756 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
757 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
759 byTemp = _pabyDataT[0]; \
760 _pabyDataT[0] = _pabyDataT[3]; \
761 _pabyDataT[3] = byTemp; \
762 byTemp = _pabyDataT[1]; \
763 _pabyDataT[1] = _pabyDataT[2]; \
764 _pabyDataT[2] = byTemp; \
768 #define CPL_SWAP64PTR(x) \
770 GByte byTemp, *_pabyDataT = CPL_REINTERPRET_CAST(GByte*, x); \
771 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
773 byTemp = _pabyDataT[0]; \
774 _pabyDataT[0] = _pabyDataT[7]; \
775 _pabyDataT[7] = byTemp; \
776 byTemp = _pabyDataT[1]; \
777 _pabyDataT[1] = _pabyDataT[6]; \
778 _pabyDataT[6] = byTemp; \
779 byTemp = _pabyDataT[2]; \
780 _pabyDataT[2] = _pabyDataT[5]; \
781 _pabyDataT[5] = byTemp; \
782 byTemp = _pabyDataT[3]; \
783 _pabyDataT[3] = _pabyDataT[4]; \
784 _pabyDataT[4] = byTemp; \
790 #define CPL_SWAP32PTR(x) \
794 memcpy(&_n32, _lx, 4); \
795 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4); \
796 _n32 = CPL_SWAP32(_n32); \
797 memcpy(_lx, &_n32, 4); \
801 #define CPL_SWAP64PTR(x) \
805 memcpy(&_n64, _lx, 8); \
806 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8); \
807 _n64 = CPL_SWAP64(_n64); \
808 memcpy(_lx, &_n64, 8); \
814 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
817 # define CPL_MSBWORD16(x) (x)
818 # define CPL_LSBWORD16(x) CPL_SWAP16(x)
819 # define CPL_MSBWORD32(x) (x)
820 # define CPL_LSBWORD32(x) CPL_SWAP32(x)
821 # define CPL_MSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
822 # define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
823 # define CPL_MSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
824 # define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
825 # define CPL_MSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
826 # define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
829 # define CPL_LSBWORD16(x) (x)
831 # define CPL_MSBWORD16(x) CPL_SWAP16(x)
833 # define CPL_LSBWORD32(x) (x)
835 # define CPL_MSBWORD32(x) CPL_SWAP32(x)
837 # define CPL_LSBPTR16(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
839 # define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
841 # define CPL_LSBPTR32(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
843 # define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
845 # define CPL_LSBPTR64(x) CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
847 # define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
853 #define CPL_LSBINT16PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8))
858 #define CPL_LSBINT32PTR(x) ((*CPL_REINTERPRET_CAST(const GByte*, x)) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+1) << 8) | \
859 (*((CPL_REINTERPRET_CAST(const GByte*, x))+2) << 16) | (*((CPL_REINTERPRET_CAST(const GByte*, x))+3) << 24))
862 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16,CPL_LSBINT16PTR(x))
865 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
868 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
871 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
875 #ifndef UNREFERENCED_PARAM
876 # ifdef UNREFERENCED_PARAMETER
877 # define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
879 # define UNREFERENCED_PARAM(param) ((void)param)
893 #ifndef DISABLE_CVSID
894 #if defined(__GNUC__) && __GNUC__ >= 4
895 # define CPL_CVSID(string) static const char cpl_cvsid[] __attribute__((used)) = string;
897 # define CPL_CVSID(string) static const char cpl_cvsid[] = string; \
898 static const char *cvsid_aw() { return( cvsid_aw() ? NULL : cpl_cvsid ); }
901 # define CPL_CVSID(string)
906 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
908 # define CPL_NULL_TERMINATED __attribute__((__sentinel__))
911 # define CPL_NULL_TERMINATED
914 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
916 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__printf__, format_idx, arg_idx)))
918 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx ) __attribute__((__format__ (__scanf__, format_idx, arg_idx)))
921 #define CPL_PRINT_FUNC_FORMAT( format_idx, arg_idx )
923 #define CPL_SCAN_FUNC_FORMAT( format_idx, arg_idx )
926 #if defined(_MSC_VER) && (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
930 # define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
933 # define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
936 # define CPL_FORMAT_STRING(arg) arg
938 # define CPL_SCANF_FORMAT_STRING(arg) arg
941 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
943 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
946 #define CPL_WARN_UNUSED_RESULT
949 #if defined(__GNUC__) && __GNUC__ >= 4
951 # define CPL_UNUSED __attribute((__unused__))
958 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
960 #define CPL_NO_RETURN __attribute__((noreturn))
963 #define CPL_NO_RETURN
968 #ifndef __has_attribute
969 #define __has_attribute(x) 0
974 #if ((defined(__GNUC__) && (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || __has_attribute(returns_nonnull)) && !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
976 # define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
979 # define CPL_RETURNS_NONNULL
982 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
984 #define CPL_RESTRICT __restrict__
990 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
994 # define CPL_OVERRIDE override
997 # define CPL_FINAL final
1000 # define CPL_NON_FINAL
1007 # define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
1008 ClassName( const ClassName & ) = delete; \
1009 ClassName &operator=( const ClassName & ) = delete;
1013 #if !defined(DOXYGEN_SKIP)
1014 #if defined(__has_extension)
1015 #if __has_extension(attribute_deprecated_with_message)
1017 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated(x)))
1019 #define CPL_WARN_DEPRECATED(x)
1021 #elif defined(__GNUC__)
1022 #define CPL_WARN_DEPRECATED(x) __attribute__ ((deprecated))
1024 #define CPL_WARN_DEPRECATED(x)
1028 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1030 # if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1031 int vsnprintf(
char *str,
size_t size,
const char* fmt, va_list args)
1032 CPL_WARN_DEPRECATED(
"Use CPLvsnprintf() instead");
1033 int snprintf(
char *str,
size_t size,
const char* fmt, ...)
1036 int sprintf(
char *str, const
char* fmt, ...)
1039 # elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1040 int sprintf(
char *str,
const char* fmt, ...)
1042 CPL_WARN_DEPRECATED("Use snprintf() or
CPLsnprintf() instead");
1047 #if defined(MAKE_SANITIZE_HAPPY) || !(defined(__i386__) || defined(__x86_64__) || defined(_M_IX86) || defined(_M_X64))
1049 #define CPL_CPU_REQUIRES_ALIGNED_ACCESS
1053 #if defined(__cplusplus)
1055 #define CPL_ARRAYSIZE(array) \
1056 ((sizeof(array) / sizeof(*(array))) / \
1057 static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1060 template<
class T>
static void CPL_IGNORE_RET_VAL(
const T&) {}
1061 inline static bool CPL_TO_BOOL(
int x) {
return x != 0; }
1066 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || (defined(__clang__) && __clang_major__ >= 3)) && !defined(_MSC_VER))
1067 #define HAVE_GCC_DIAGNOSTIC_PUSH
1070 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && !defined(_MSC_VER))
1071 #define HAVE_GCC_SYSTEM_HEADER
1074 #if ((defined(__clang__) && (__clang_major__ > 3 || (__clang_major__ == 3 && __clang_minor__ >=7))) || __GNUC__ >= 7)
1076 # define CPL_FALLTHROUGH [[clang::fallthrough]];
1079 # define CPL_FALLTHROUGH
1087 #if defined(__cplusplus) && defined(DEBUG_BOOL) && !defined(DO_NOT_USE_DEBUG_BOOL) && !defined(CPL_SUPRESS_CPLUSPLUS)
1089 class MSVCPedanticBool
1092 friend bool operator== (
const bool& one,
const MSVCPedanticBool& other);
1093 friend bool operator!= (
const bool& one,
const MSVCPedanticBool& other);
1096 MSVCPedanticBool(
int bIn);
1101 MSVCPedanticBool() {}
1102 MSVCPedanticBool(
bool bIn) : b(bIn) {}
1103 MSVCPedanticBool(
const MSVCPedanticBool& other) : b(other.b) {}
1105 MSVCPedanticBool& operator= (
const MSVCPedanticBool& other) { b = other.b;
return *
this; }
1106 MSVCPedanticBool& operator&= (
const MSVCPedanticBool& other) { b &= other.b;
return *
this; }
1107 MSVCPedanticBool& operator|= (
const MSVCPedanticBool& other) { b |= other.b;
return *
this; }
1109 bool operator== (
const bool& other)
const {
return b == other; }
1110 bool operator!= (
const bool& other)
const {
return b != other; }
1111 bool operator< (
const bool& other)
const {
return b < other; }
1112 bool operator== (
const MSVCPedanticBool& other)
const {
return b == other.b; }
1113 bool operator!= (
const MSVCPedanticBool& other)
const {
return b != other.b; }
1114 bool operator< (
const MSVCPedanticBool& other)
const {
return b < other.b; }
1116 bool operator! ()
const {
return !b; }
1117 operator bool()
const {
return b; }
1118 operator int()
const {
return b; }
1119 operator GIntBig()
const {
return b; }
1122 inline bool operator== (
const bool& one,
const MSVCPedanticBool& other) {
return one == other.b; }
1123 inline bool operator!= (
const bool& one,
const MSVCPedanticBool& other) {
return one != other.b; }
1134 #include <algorithm>
1135 #include <functional>
1139 #include <unordered_map>
1141 #include <unordered_set>
1153 #define EMULATED_BOOL int
1156 #define bool MSVCPedanticBool
1160 #define VOLATILE_BOOL bool
1172 #define EMULATED_BOOL bool
1173 #define VOLATILE_BOOL volatile bool
1177 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1178 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW __attribute__((no_sanitize("unsigned-integer-overflow")))
1180 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1183 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && defined(GDAL_COMPILATION)
1185 template<
class C,
class A,
class B>
1186 CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1187 inline C CPLUnsanitizedAdd(A a, B b)
1197 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1198 #define CPL_NULLPTR nullptr
1200 #define CPL_NULLPTR NULL
1209 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && !defined(DOXYGEN_SKIP)
int GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:289
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:251
short GInt16
Int16 type.
Definition: cpl_port.h:211
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:339
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:337
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:267
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:223
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:207
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:921
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1216
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:269
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:213
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:215
int GInt32
Int32 type.
Definition: cpl_port.h:205
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:248
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