GDAL

cpl_port.h
Go to the documentation of this file.
1 /******************************************************************************
2 * $Id$
3 *
4 * Project: CPL - Common Portability Library
5 * Author: Frank Warmerdam, warmerdam@pobox.com
6 * Purpose: Include file providing low level portability services for CPL.
7 * This should be the first include file for any CPL based code.
8 *
9 ******************************************************************************
10 * Copyright (c) 1998, 2005, Frank Warmerdam <warmerdam@pobox.com>
11 * Copyright (c) 2008-2013, Even Rouault <even dot rouault at spatialys.com>
12 *
13 * Permission is hereby granted, free of charge, to any person obtaining a
14 * copy of this software and associated documentation files (the "Software"),
15 * to deal in the Software without restriction, including without limitation
16 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
17 * and/or sell copies of the Software, and to permit persons to whom the
18 * Software is furnished to do so, subject to the following conditions:
19 *
20 * The above copyright notice and this permission notice shall be included
21 * in all copies or substantial portions of the Software.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29 * DEALINGS IN THE SOFTWARE.
30 ****************************************************************************/
31
32 #ifndef CPL_BASE_H_INCLUDED
33 #define CPL_BASE_H_INCLUDED
34
42 /* ==================================================================== */
43 /* We will use WIN32 as a standard windows define. */
44 /* ==================================================================== */
45 #if defined(_WIN32) && !defined(WIN32)
46 #define WIN32
47 #endif
48
49 #if defined(_WINDOWS) && !defined(WIN32)
50 #define WIN32
51 #endif
52
53 /* -------------------------------------------------------------------- */
54 /* The following apparently allow you to use strcpy() and other */
55 /* functions judged "unsafe" by microsoft in VS 8 (2005). */
56 /* -------------------------------------------------------------------- */
57 #ifdef _MSC_VER
58 #ifndef _CRT_SECURE_NO_DEPRECATE
59 #define _CRT_SECURE_NO_DEPRECATE
60 #endif
61 #ifndef _CRT_NONSTDC_NO_DEPRECATE
62 #define _CRT_NONSTDC_NO_DEPRECATE
63 #endif
64 #endif
65
66 #include "cpl_config.h"
67
68 /* ==================================================================== */
69 /* A few sanity checks, mainly to detect problems that sometimes */
70 /* arise with bad configured cross-compilation. */
71 /* ==================================================================== */
72
73 #if !defined(SIZEOF_INT) || SIZEOF_INT != 4
74 #error "Unexpected value for SIZEOF_INT"
75 #endif
76
77 #if !defined(SIZEOF_UNSIGNED_LONG) || \
78 (SIZEOF_UNSIGNED_LONG != 4 && SIZEOF_UNSIGNED_LONG != 8)
79 #error "Unexpected value for SIZEOF_UNSIGNED_LONG"
80 #endif
81
82 #if !defined(SIZEOF_VOIDP)
83 #error "Unexpected value for SIZEOF_VOIDP"
84 #endif
85
86 /* ==================================================================== */
87 /* This will disable most WIN32 stuff in a Cygnus build which */
88 /* defines unix to 1. */
89 /* ==================================================================== */
90
91 #ifdef unix
92 #undef WIN32
93 #endif
94
96 #if defined(VSI_NEED_LARGEFILE64_SOURCE) && !defined(_LARGEFILE64_SOURCE)
97 #define _LARGEFILE64_SOURCE 1
98 #endif
99
100 /* ==================================================================== */
101 /* If iconv() is available use extended recoding module. */
102 /* Stub implementation is always compiled in, because it works */
103 /* faster than iconv() for encodings it supports. */
104 /* ==================================================================== */
105
106 #if defined(HAVE_ICONV)
107 #define CPL_RECODE_ICONV
108 #endif
109
110 #define CPL_RECODE_STUB
111
113 /* ==================================================================== */
114 /* MinGW stuff */
115 /* ==================================================================== */
116
117 /* Needed for std=c11 on Solaris to have strcasecmp() */
118 #if defined(GDAL_COMPILATION) && defined(__sun__) && \
119 (__STDC_VERSION__ + 0) >= 201112L && (_XOPEN_SOURCE + 0) < 600
120 #ifdef _XOPEN_SOURCE
121 #undef _XOPEN_SOURCE
122 #endif
123 #define _XOPEN_SOURCE 600
124 #endif
125
126 /* ==================================================================== */
127 /* Standard include files. */
128 /* ==================================================================== */
129
130 #include <stdio.h>
131 #include <stdlib.h>
132 #include <math.h>
133 #include <stdarg.h>
134 #include <string.h>
135 #include <ctype.h>
136 #include <limits.h>
137
138 #include <time.h>
139
140 #include <errno.h>
141
142 #ifdef HAVE_LOCALE_H
143 #include <locale.h>
144 #endif
145
146 #ifdef HAVE_DIRECT_H
147 #include <direct.h>
148 #endif
149
150 #if !defined(WIN32)
151 #include <strings.h>
152 #endif
153
154 /* ==================================================================== */
155 /* Base portability stuff ... this stuff may need to be */
156 /* modified for new platforms. */
157 /* ==================================================================== */
158
159 /* -------------------------------------------------------------------- */
160 /* Which versions of C++ are available. */
161 /* -------------------------------------------------------------------- */
162
163 /* MSVC fails to define a decent value of __cplusplus. Try to target VS2015*/
164 /* as a minimum */
165
166 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
167 #if !(__cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1900))
168 #error Must have C++11 or newer.
169 #endif
170 #if __cplusplus >= 201402L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201402L)
171 #define HAVE_CXX14 1
172 #endif
173 #if __cplusplus >= 201703L || (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L)
174 #define HAVE_CXX17 1
175 #endif
176 #endif /* __cplusplus */
177
178 /*---------------------------------------------------------------------
179 * types for 16 and 32 bits integers, etc...
180 *--------------------------------------------------------------------*/
181 #if UINT_MAX == 65535
182 typedef long GInt32;
183 typedef unsigned long GUInt32;
184 #else
185
186 typedef int GInt32;
188 typedef unsigned int GUInt32;
189 #endif
190
192 typedef short GInt16;
194 typedef unsigned short GUInt16;
196 typedef unsigned char GByte;
198 typedef signed char GInt8;
199 /* hack for PDF driver and poppler >= 0.15.0 that defines incompatible "typedef
200 * bool GBool" */
201 /* in include/poppler/goo/gtypes.h */
202 #ifndef CPL_GBOOL_DEFINED
203
204 #define CPL_GBOOL_DEFINED
205
207 typedef int GBool;
208 #endif
209
211 #ifdef __cplusplus
212 #define CPL_STATIC_CAST(type, expr) static_cast<type>(expr)
213 #define CPL_REINTERPRET_CAST(type, expr) reinterpret_cast<type>(expr)
214 #else
215 #define CPL_STATIC_CAST(type, expr) ((type)(expr))
216 #define CPL_REINTERPRET_CAST(type, expr) ((type)(expr))
217 #endif
218
220 /* -------------------------------------------------------------------- */
221 /* 64bit support */
222 /* -------------------------------------------------------------------- */
223
226 typedef long long GIntBig;
229 typedef unsigned long long GUIntBig;
230
232 #define GINTBIG_MIN (CPL_STATIC_CAST(GIntBig, 0x80000000) << 32)
233
234 #define GINTBIG_MAX ((CPL_STATIC_CAST(GIntBig, 0x7FFFFFFF) << 32) | 0xFFFFFFFFU)
235
236 #define GUINTBIG_MAX \
237 ((CPL_STATIC_CAST(GUIntBig, 0xFFFFFFFFU) << 32) | 0xFFFFFFFFU)
238
240 #define CPL_HAS_GINT64 1
241
243 /* Note: we might want to use instead int64_t / uint64_t if they are available
244 */
245
247 typedef GIntBig GInt64;
249 typedef GUIntBig GUInt64;
250
252 #define GINT64_MIN GINTBIG_MIN
253
254 #define GINT64_MAX GINTBIG_MAX
255
256 #define GUINT64_MAX GUINTBIG_MAX
257
258 #if SIZEOF_VOIDP > 8
259 #include <stddef.h> // ptrdiff_t
261 typedef ptrdiff_t GPtrDiff_t;
262 #elif SIZEOF_VOIDP == 8
263
264 typedef GIntBig GPtrDiff_t;
265 #else
266
267 typedef int GPtrDiff_t;
268 #endif
269
270 #ifdef GDAL_COMPILATION
271 #include <stdint.h>
272 typedef uintptr_t GUIntptr_t;
273 #define CPL_IS_ALIGNED(ptr, quant) \
274 ((CPL_REINTERPRET_CAST(GUIntptr_t, CPL_STATIC_CAST(const void *, ptr)) % \
275 (quant)) == 0)
276
277 #endif
278
279 #if (defined(__MSVCRT__) && !(defined(__MINGW64__) && __GNUC__ >= 10)) || \
280 (defined(WIN32) && defined(_MSC_VER))
281 #define CPL_FRMT_GB_WITHOUT_PREFIX "I64"
282 #else
283
284 #define CPL_FRMT_GB_WITHOUT_PREFIX "ll"
285 #endif
286
288 #define CPL_FRMT_GIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "d"
289
290 #define CPL_FRMT_GUIB "%" CPL_FRMT_GB_WITHOUT_PREFIX "u"
291
293 #ifdef COMPAT_WITH_ICC_CONVERSION_CHECK
294 #define CPL_INT64_FITS_ON_INT32(x) ((x) >= INT_MIN && (x) <= INT_MAX)
295 #else
296 #define CPL_INT64_FITS_ON_INT32(x) \
297 (CPL_STATIC_CAST(GIntBig, CPL_STATIC_CAST(int, x)) == (x))
298 #endif
299
301 /* ==================================================================== */
302 /* Other standard services. */
303 /* ==================================================================== */
304 #ifdef __cplusplus
305
306 #define CPL_C_START \
307 extern "C" \
308 {
309
310 #define CPL_C_END }
311 #else
312 #define CPL_C_START
313 #define CPL_C_END
314 #endif
315
316 #ifndef CPL_DLL
317 #if defined(_MSC_VER) && !defined(CPL_DISABLE_DLL)
318 #ifdef GDAL_COMPILATION
319 #define CPL_DLL __declspec(dllexport)
320 #else
321 #define CPL_DLL
322 #endif
323 #define CPL_INTERNAL
324 #else
325 #if defined(USE_GCC_VISIBILITY_FLAG)
326 #define CPL_DLL __attribute__((visibility("default")))
327 #if !defined(__MINGW32__)
328 #define CPL_INTERNAL __attribute__((visibility("hidden")))
329 #else
330 #define CPL_INTERNAL
331 #endif
332 #else
333 #define CPL_DLL
334 #define CPL_INTERNAL
335 #endif
336 #endif
337
338 // Marker for unstable API
339 #define CPL_UNSTABLE_API CPL_DLL
340
341 #endif
342
344 /* Should optional (normally private) interfaces be exported? */
345 #ifdef CPL_OPTIONAL_APIS
346 #define CPL_ODLL CPL_DLL
347 #else
348 #define CPL_ODLL
349 #endif
350
352 #ifndef CPL_STDCALL
353 #if defined(_MSC_VER) && !defined(CPL_DISABLE_STDCALL)
354 #define CPL_STDCALL __stdcall
355 #else
356 #define CPL_STDCALL
357 #endif
358 #endif
359
361 #ifdef _MSC_VER
362 #define FORCE_CDECL __cdecl
363 #else
364 #define FORCE_CDECL
365 #endif
366
369 /* TODO : support for other compilers needed */
370 #if (defined(__GNUC__) && !defined(__NO_INLINE__)) || defined(_MSC_VER)
371 #define HAS_CPL_INLINE 1
372 #define CPL_INLINE __inline
373 #elif defined(__SUNPRO_CC)
374 #define HAS_CPL_INLINE 1
375 #define CPL_INLINE inline
376 #else
377 #define CPL_INLINE
378 #endif
379
381 #ifndef MAX
382
383 #define MIN(a, b) (((a) < (b)) ? (a) : (b))
384
385 #define MAX(a, b) (((a) > (b)) ? (a) : (b))
386 #endif
387
388 #ifndef ABS
389
390 #define ABS(x) (((x) < 0) ? (-1 * (x)) : (x))
391 #endif
392
393 #ifndef M_PI
394
395 #define M_PI 3.14159265358979323846
396 /* 3.1415926535897932384626433832795 */
397 #endif
398
399 /* -------------------------------------------------------------------- */
400 /* Macro to test equality of two floating point values. */
401 /* We use fabs() function instead of ABS() macro to avoid side */
402 /* effects. */
403 /* -------------------------------------------------------------------- */
405 #ifndef CPLIsEqual
406 #define CPLIsEqual(x, y) (fabs((x) - (y)) < 0.0000000000001)
407 #endif
408
410 /* -------------------------------------------------------------------- */
411 /* Provide macros for case insensitive string comparisons. */
412 /* -------------------------------------------------------------------- */
413 #ifndef EQUAL
414
415 #if defined(AFL_FRIENDLY) && defined(__GNUC__)
416
417 static inline int CPL_afl_friendly_memcmp(const void *ptr1, const void *ptr2,
418 size_t len)
419 __attribute__((always_inline));
420
421 static inline int CPL_afl_friendly_memcmp(const void *ptr1, const void *ptr2,
422 size_t len)
423 {
424 const unsigned char *bptr1 = (const unsigned char *)ptr1;
425 const unsigned char *bptr2 = (const unsigned char *)ptr2;
426 while (len--)
427 {
428 unsigned char b1 = *(bptr1++);
429 unsigned char b2 = *(bptr2++);
430 if (b1 != b2)
431 return b1 - b2;
432 }
433 return 0;
434 }
435
436 static inline int CPL_afl_friendly_strcmp(const char *ptr1, const char *ptr2)
437 __attribute__((always_inline));
438
439 static inline int CPL_afl_friendly_strcmp(const char *ptr1, const char *ptr2)
440 {
441 const unsigned char *usptr1 = (const unsigned char *)ptr1;
442 const unsigned char *usptr2 = (const unsigned char *)ptr2;
443 while (1)
444 {
445 unsigned char ch1 = *(usptr1++);
446 unsigned char ch2 = *(usptr2++);
447 if (ch1 == 0 || ch1 != ch2)
448 return ch1 - ch2;
449 }
450 }
451
452 static inline int CPL_afl_friendly_strncmp(const char *ptr1, const char *ptr2,
453 size_t len)
454 __attribute__((always_inline));
455
456 static inline int CPL_afl_friendly_strncmp(const char *ptr1, const char *ptr2,
457 size_t len)
458 {
459 const unsigned char *usptr1 = (const unsigned char *)ptr1;
460 const unsigned char *usptr2 = (const unsigned char *)ptr2;
461 while (len--)
462 {
463 unsigned char ch1 = *(usptr1++);
464 unsigned char ch2 = *(usptr2++);
465 if (ch1 == 0 || ch1 != ch2)
466 return ch1 - ch2;
467 }
468 return 0;
469 }
470
471 static inline int CPL_afl_friendly_strcasecmp(const char *ptr1,
472 const char *ptr2)
473 __attribute__((always_inline));
474
475 static inline int CPL_afl_friendly_strcasecmp(const char *ptr1,
476 const char *ptr2)
477 {
478 const unsigned char *usptr1 = (const unsigned char *)ptr1;
479 const unsigned char *usptr2 = (const unsigned char *)ptr2;
480 while (1)
481 {
482 unsigned char ch1 = *(usptr1++);
483 unsigned char ch2 = *(usptr2++);
484 ch1 = (unsigned char)toupper(ch1);
485 ch2 = (unsigned char)toupper(ch2);
486 if (ch1 == 0 || ch1 != ch2)
487 return ch1 - ch2;
488 }
489 }
490
491 static inline int CPL_afl_friendly_strncasecmp(const char *ptr1,
492 const char *ptr2, size_t len)
493 __attribute__((always_inline));
494
495 static inline int CPL_afl_friendly_strncasecmp(const char *ptr1,
496 const char *ptr2, size_t len)
497 {
498 const unsigned char *usptr1 = (const unsigned char *)ptr1;
499 const unsigned char *usptr2 = (const unsigned char *)ptr2;
500 while (len--)
501 {
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)
507 return ch1 - ch2;
508 }
509 return 0;
510 }
511
512 static inline char *CPL_afl_friendly_strstr(const char *haystack,
513 const char *needle)
514 __attribute__((always_inline));
515
516 static inline char *CPL_afl_friendly_strstr(const char *haystack,
517 const char *needle)
518 {
519 const char *ptr_haystack = haystack;
520 while (1)
521 {
522 const char *ptr_haystack2 = ptr_haystack;
523 const char *ptr_needle = needle;
524 while (1)
525 {
526 char ch1 = *(ptr_haystack2++);
527 char ch2 = *(ptr_needle++);
528 if (ch2 == 0)
529 return (char *)ptr_haystack;
530 if (ch1 != ch2)
531 break;
532 }
533 if (*ptr_haystack == 0)
534 return NULL;
535 ptr_haystack++;
536 }
537 }
538
539 #undef strcmp
540 #undef strncmp
541 #define memcmp CPL_afl_friendly_memcmp
542 #define strcmp CPL_afl_friendly_strcmp
543 #define strncmp CPL_afl_friendly_strncmp
544 #define strcasecmp CPL_afl_friendly_strcasecmp
545 #define strncasecmp CPL_afl_friendly_strncasecmp
546 #define strstr CPL_afl_friendly_strstr
547
548 #endif /* defined(AFL_FRIENDLY) && defined(__GNUC__) */
549
550 #if defined(WIN32)
551 #define STRCASECMP(a, b) (_stricmp(a, b))
552 #define STRNCASECMP(a, b, n) (_strnicmp(a, b, n))
553 #else
554
555 #define STRCASECMP(a, b) (strcasecmp(a, b))
556
557 #define STRNCASECMP(a, b, n) (strncasecmp(a, b, n))
558 #endif
559
560 #define EQUALN(a, b, n) (STRNCASECMP(a, b, n) == 0)
561
562 #define EQUAL(a, b) (STRCASECMP(a, b) == 0)
563 #endif
564
565 /*---------------------------------------------------------------------
566 * Does a string "a" start with string "b". Search is case-sensitive or,
567 * with CI, it is a case-insensitive comparison.
568 *--------------------------------------------------------------------- */
569 #ifndef STARTS_WITH_CI
570
571 #define STARTS_WITH(a, b) (strncmp(a, b, strlen(b)) == 0)
572
573 #define STARTS_WITH_CI(a, b) EQUALN(a, b, strlen(b))
574 #endif
575
577 #ifndef CPL_THREADLOCAL
578 #define CPL_THREADLOCAL
579 #endif
580
582 /* -------------------------------------------------------------------- */
583 /* Handle isnan() and isinf(). Note that isinf() and isnan() */
584 /* are supposed to be macros according to C99, defined in math.h */
585 /* Some systems (i.e. Tru64) don't have isinf() at all, so if */
586 /* the macro is not defined we just assume nothing is infinite. */
587 /* This may mean we have no real CPLIsInf() on systems with isinf()*/
588 /* function but no corresponding macro, but I can live with */
589 /* that since it isn't that important a test. */
590 /* -------------------------------------------------------------------- */
591 #ifdef _MSC_VER
592 #include <float.h>
593 #define CPLIsNan(x) _isnan(x)
594 #define CPLIsInf(x) (!_isnan(x) && !_finite(x))
595 #define CPLIsFinite(x) _finite(x)
596 #elif defined(__GNUC__) && \
597 (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 4))
598 /* When including <cmath> in C++11 the isnan() macro is undefined, so that */
599 /* std::isnan() can work (#6489). This is a GCC specific workaround for now. */
600 #define CPLIsNan(x) __builtin_isnan(x)
601 #define CPLIsInf(x) __builtin_isinf(x)
602 #define CPLIsFinite(x) __builtin_isfinite(x)
603 #elif defined(__cplusplus) && defined(HAVE_STD_IS_NAN) && HAVE_STD_IS_NAN
604 extern "C++"
605 {
606 #ifndef DOXYGEN_SKIP
607 #include <cmath>
608 #endif
609 static inline int CPLIsNan(float f)
610 {
611 return std::isnan(f);
612 }
613 static inline int CPLIsNan(double f)
614 {
615 return std::isnan(f);
616 }
617 static inline int CPLIsInf(float f)
618 {
619 return std::isinf(f);
620 }
621 static inline int CPLIsInf(double f)
622 {
623 return std::isinf(f);
624 }
625 static inline int CPLIsFinite(float f)
626 {
627 return std::isfinite(f);
628 }
629 static inline int CPLIsFinite(double f)
630 {
631 return std::isfinite(f);
632 }
633 }
634 #else
635
636 #if defined(__cplusplus) && defined(__GNUC__) && defined(__linux) && \
637 !defined(__ANDROID__) && !defined(CPL_SUPRESS_CPLUSPLUS)
638 /* so to not get warning about conversion from double to float with */
639 /* gcc -Wfloat-conversion when using isnan()/isinf() macros */
640 extern "C++"
641 {
642 static inline int CPLIsNan(float f)
643 {
644 return __isnanf(f);
645 }
646 static inline int CPLIsNan(double f)
647 {
648 return __isnan(f);
649 }
650 static inline int CPLIsInf(float f)
651 {
652 return __isinff(f);
653 }
654 static inline int CPLIsInf(double f)
655 {
656 return __isinf(f);
657 }
658 static inline int CPLIsFinite(float f)
659 {
660 return !__isnanf(f) && !__isinff(f);
661 }
662 static inline int CPLIsFinite(double f)
663 {
664 return !__isnan(f) && !__isinf(f);
665 }
666 }
667 #else
668 #define CPLIsNan(x) isnan(x)
669 #if defined(isinf) || defined(__FreeBSD__)
670
671 #define CPLIsInf(x) isinf(x)
672
673 #define CPLIsFinite(x) (!isnan(x) && !isinf(x))
674 #elif defined(__sun__)
675 #include <ieeefp.h>
676 #define CPLIsInf(x) (!finite(x) && !isnan(x))
677 #define CPLIsFinite(x) finite(x)
678 #else
679 #define CPLIsInf(x) (0)
680 #define CPLIsFinite(x) (!isnan(x))
681 #endif
682 #endif
683 #endif
684
686 /*---------------------------------------------------------------------
687 * CPL_LSB and CPL_MSB
688 * Only one of these 2 macros should be defined and specifies the byte
689 * ordering for the current platform.
690 * This should be defined in the Makefile, but if it is not then
691 * the default is CPL_LSB (Intel ordering, LSB first).
692 *--------------------------------------------------------------------*/
693 #if defined(WORDS_BIGENDIAN) && !defined(CPL_MSB) && !defined(CPL_LSB)
694 #define CPL_MSB
695 #endif
696
697 #if !(defined(CPL_LSB) || defined(CPL_MSB))
698 #define CPL_LSB
699 #endif
700
701 #if defined(CPL_LSB)
702 #define CPL_IS_LSB 1
703 #else
704 #define CPL_IS_LSB 0
705 #endif
706
708 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
709
711 extern "C++"
712 {
713
714 template <bool b> struct CPLStaticAssert
715 {
716 };
717 template <> struct CPLStaticAssert<true>
718 {
719 static void my_function()
720 {
721 }
722 };
723
724 } /* extern "C++" */
725
726 #define CPL_STATIC_ASSERT(x) CPLStaticAssert<x>::my_function()
727 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x) CPL_STATIC_ASSERT(x)
728
729 #else /* __cplusplus */
730
731 #define CPL_STATIC_ASSERT_IF_AVAILABLE(x)
732
733 #endif /* __cplusplus */
734
736 /*---------------------------------------------------------------------
737 * Little endian <==> big endian byte swap macros.
738 *--------------------------------------------------------------------*/
739
741 #define CPL_SWAP16(x) \
742 CPL_STATIC_CAST(GUInt16, (CPL_STATIC_CAST(GUInt16, x) << 8) | \
743 (CPL_STATIC_CAST(GUInt16, x) >> 8))
744
745 #if defined(HAVE_GCC_BSWAP)
746
747 #define CPL_SWAP32(x) \
748 CPL_STATIC_CAST(GUInt32, __builtin_bswap32(CPL_STATIC_CAST(GUInt32, x)))
749
750 #define CPL_SWAP64(x) \
751 CPL_STATIC_CAST(GUInt64, __builtin_bswap64(CPL_STATIC_CAST(GUInt64, x)))
752 #elif defined(_MSC_VER)
753 #define CPL_SWAP32(x) \
754 CPL_STATIC_CAST(GUInt32, _byteswap_ulong(CPL_STATIC_CAST(GUInt32, x)))
755 #define CPL_SWAP64(x) \
756 CPL_STATIC_CAST(GUInt64, _byteswap_uint64(CPL_STATIC_CAST(GUInt64, x)))
757 #else
758
759 #define CPL_SWAP32(x) \
760 CPL_STATIC_CAST(GUInt32, \
761 ((CPL_STATIC_CAST(GUInt32, x) & 0x000000ffU) << 24) | \
762 ((CPL_STATIC_CAST(GUInt32, x) & 0x0000ff00U) << 8) | \
763 ((CPL_STATIC_CAST(GUInt32, x) & 0x00ff0000U) >> 8) | \
764 ((CPL_STATIC_CAST(GUInt32, x) & 0xff000000U) >> 24))
765
767 #define CPL_SWAP64(x) \
768 ((CPL_STATIC_CAST(GUInt64, CPL_SWAP32(CPL_STATIC_CAST(GUInt32, x))) \
769 << 32) | \
770 (CPL_STATIC_CAST(GUInt64, \
771 CPL_SWAP32(CPL_STATIC_CAST( \
772 GUInt32, CPL_STATIC_CAST(GUInt64, x) >> 32)))))
773
774 #endif
775
777 #define CPL_SWAP16PTR(x) \
778 do \
779 { \
780 GUInt16 _n16; \
781 void *_lx = x; \
782 memcpy(&_n16, _lx, 2); \
783 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || \
784 sizeof(*(x)) == 2); \
785 _n16 = CPL_SWAP16(_n16); \
786 memcpy(_lx, &_n16, 2); \
787 } while (0)
788
790 #define CPL_SWAP32PTR(x) \
791 do \
792 { \
793 GUInt32 _n32; \
794 void *_lx = x; \
795 memcpy(&_n32, _lx, 4); \
796 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || \
797 sizeof(*(x)) == 4); \
798 _n32 = CPL_SWAP32(_n32); \
799 memcpy(_lx, &_n32, 4); \
800 } while (0)
801
803 #define CPL_SWAP64PTR(x) \
804 do \
805 { \
806 GUInt64 _n64; \
807 void *_lx = x; \
808 memcpy(&_n64, _lx, 8); \
809 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || \
810 sizeof(*(x)) == 8); \
811 _n64 = CPL_SWAP64(_n64); \
812 memcpy(_lx, &_n64, 8); \
813 } while (0)
814
816 #define CPL_SWAPDOUBLE(p) CPL_SWAP64PTR(p)
817
818 #ifdef CPL_MSB
819 #define CPL_MSBWORD16(x) (x)
820 #define CPL_LSBWORD16(x) CPL_SWAP16(x)
821 #define CPL_MSBWORD32(x) (x)
822 #define CPL_LSBWORD32(x) CPL_SWAP32(x)
823 #define CPL_MSBPTR16(x) \
824 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
825 #define CPL_LSBPTR16(x) CPL_SWAP16PTR(x)
826 #define CPL_MSBPTR32(x) \
827 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
828 #define CPL_LSBPTR32(x) CPL_SWAP32PTR(x)
829 #define CPL_MSBPTR64(x) \
830 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
831 #define CPL_LSBPTR64(x) CPL_SWAP64PTR(x)
832 #else
833
834 #define CPL_LSBWORD16(x) (x)
835
836 #define CPL_MSBWORD16(x) CPL_SWAP16(x)
837
838 #define CPL_LSBWORD32(x) (x)
839
840 #define CPL_MSBWORD32(x) CPL_SWAP32(x)
841
843 #define CPL_LSBPTR16(x) \
844 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 2)
845
847 #define CPL_MSBPTR16(x) CPL_SWAP16PTR(x)
848
850 #define CPL_LSBPTR32(x) \
851 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 4)
852
854 #define CPL_MSBPTR32(x) CPL_SWAP32PTR(x)
855
857 #define CPL_LSBPTR64(x) \
858 CPL_STATIC_ASSERT_IF_AVAILABLE(sizeof(*(x)) == 1 || sizeof(*(x)) == 8)
859
861 #define CPL_MSBPTR64(x) CPL_SWAP64PTR(x)
862 #endif
863
867 #define CPL_LSBINT16PTR(x) \
868 ((*CPL_REINTERPRET_CAST(const GByte *, x)) | \
869 (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 1) << 8))
870
874 #define CPL_LSBINT32PTR(x) \
875 ((*CPL_REINTERPRET_CAST(const GByte *, x)) | \
876 (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 1) << 8) | \
877 (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 2) << 16) | \
878 (*((CPL_REINTERPRET_CAST(const GByte *, x)) + 3) << 24))
879
881 #define CPL_LSBSINT16PTR(x) CPL_STATIC_CAST(GInt16, CPL_LSBINT16PTR(x))
882
885 #define CPL_LSBUINT16PTR(x) CPL_STATIC_CAST(GUInt16, CPL_LSBINT16PTR(x))
886
888 #define CPL_LSBSINT32PTR(x) CPL_STATIC_CAST(GInt32, CPL_LSBINT32PTR(x))
889
892 #define CPL_LSBUINT32PTR(x) CPL_STATIC_CAST(GUInt32, CPL_LSBINT32PTR(x))
893
895 /* Utility macro to explicitly mark intentionally unreferenced parameters. */
896 #ifndef UNREFERENCED_PARAM
897 #ifdef UNREFERENCED_PARAMETER /* May be defined by Windows API */
898 #define UNREFERENCED_PARAM(param) UNREFERENCED_PARAMETER(param)
899 #else
900 #define UNREFERENCED_PARAM(param) ((void)param)
901 #endif /* UNREFERENCED_PARAMETER */
902 #endif /* UNREFERENCED_PARAM */
903
905 /***********************************************************************
906 * Define CPL_CVSID() macro. It can be disabled during a build by
907 * defining DISABLE_CVSID in the compiler options.
908 *
909 * The cvsid_aw() function is just there to prevent reports of cpl_cvsid()
910 * being unused.
911 */
912
914 #ifndef DISABLE_CVSID
915 #if defined(__GNUC__) && __GNUC__ >= 4
916 #define CPL_CVSID(string) \
917 static const char cpl_cvsid[] __attribute__((used)) = string;
918 #else
919 #define CPL_CVSID(string) \
920 static const char cpl_cvsid[] = string; \
921 static const char *cvsid_aw() \
922 { \
923 return (cvsid_aw() ? NULL : cpl_cvsid); \
924 }
925 #endif
926 #else
927 #define CPL_CVSID(string)
928 #endif
929
931 /* We exclude mingw64 4.6 which seems to be broken regarding this */
932 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP) && \
933 !(defined(__MINGW64__) && __GNUC__ == 4 && __GNUC_MINOR__ == 6)
934
935 #define CPL_NULL_TERMINATED __attribute__((__sentinel__))
936 #else
937
938 #define CPL_NULL_TERMINATED
939 #endif
940
941 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
942
943 #define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx) \
944 __attribute__((__format__(__printf__, format_idx, arg_idx)))
945
946 #define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx) \
947 __attribute__((__format__(__scanf__, format_idx, arg_idx)))
948 #else
949
950 #define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
951
952 #define CPL_SCAN_FUNC_FORMAT(format_idx, arg_idx)
953 #endif
954
955 #if defined(_MSC_VER) && \
956 (defined(GDAL_COMPILATION) || defined(CPL_ENABLE_MSVC_ANNOTATIONS))
957 #include <sal.h>
960 #define CPL_FORMAT_STRING(arg) _Printf_format_string_ arg
961
963 #define CPL_SCANF_FORMAT_STRING(arg) _Scanf_format_string_ arg
964 #else
965
966 #define CPL_FORMAT_STRING(arg) arg
967
968 #define CPL_SCANF_FORMAT_STRING(arg) arg
969 #endif /* defined(_MSC_VER) && defined(GDAL_COMPILATION) */
970
971 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
972
973 #define CPL_WARN_UNUSED_RESULT __attribute__((warn_unused_result))
974 #else
975
976 #define CPL_WARN_UNUSED_RESULT
977 #endif
978
979 #if defined(__GNUC__) && __GNUC__ >= 4
980
981 #define CPL_UNUSED __attribute((__unused__))
982 #else
983 /* TODO: add cases for other compilers */
985 #define CPL_UNUSED
986 #endif
987
988 #if defined(__GNUC__) && __GNUC__ >= 3 && !defined(DOXYGEN_SKIP)
989
991 #define CPL_NO_RETURN __attribute__((noreturn))
992 #else
993
995 #define CPL_NO_RETURN
996 #endif
997
999 /* Clang __has_attribute */
1000 #ifndef __has_attribute
1001 #define __has_attribute(x) 0 // Compatibility with non-clang compilers.
1002 #endif
1003
1006 #if ((defined(__GNUC__) && \
1007 (__GNUC__ >= 5 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 9))) || \
1008 __has_attribute(returns_nonnull)) && \
1009 !defined(DOXYGEN_SKIP) && !defined(__INTEL_COMPILER)
1010
1011 #define CPL_RETURNS_NONNULL __attribute__((returns_nonnull))
1012 #else
1013
1014 #define CPL_RETURNS_NONNULL
1015 #endif
1016
1017 #if defined(__GNUC__) && __GNUC__ >= 4 && !defined(DOXYGEN_SKIP)
1018
1019 #define CPL_RESTRICT __restrict__
1020 #else
1021
1022 #define CPL_RESTRICT
1023 #endif
1024
1025 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1026
1029 #define CPL_OVERRIDE override
1030
1032 #define CPL_FINAL final
1033
1035 #define CPL_NON_FINAL
1036
1042 #define CPL_DISALLOW_COPY_ASSIGN(ClassName) \
1043 ClassName(const ClassName &) = delete; \
1044 ClassName &operator=(const ClassName &) = delete;
1045
1046 #endif /* __cplusplus */
1047
1048 #if !defined(DOXYGEN_SKIP) && !defined(CPL_WARN_DEPRECATED)
1049 #if defined(__has_extension)
1050 #if __has_extension(attribute_deprecated_with_message)
1051 /* Clang extension */
1052 #define CPL_WARN_DEPRECATED(x) __attribute__((deprecated(x)))
1053 #else
1054 #define CPL_WARN_DEPRECATED(x)
1055 #endif
1056 #elif defined(__GNUC__)
1057 #define CPL_WARN_DEPRECATED(x) __attribute__((deprecated))
1058 #else
1059 #define CPL_WARN_DEPRECATED(x)
1060 #endif
1061 #endif
1062
1063 #if !defined(_MSC_VER) && !defined(__APPLE__) && !defined(_FORTIFY_SOURCE)
1064 CPL_C_START
1065 #if defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF)
1066 int vsnprintf(char *str, size_t size, const char *fmt, va_list args)
1067 CPL_WARN_DEPRECATED("Use CPLvsnprintf() instead");
1068 int snprintf(char *str, size_t size, const char *fmt, ...)
1069 CPL_PRINT_FUNC_FORMAT(3, 4)
1070 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1071 int sprintf(char *str, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3)
1072 CPL_WARN_DEPRECATED("Use CPLsnprintf() instead");
1073 #elif defined(GDAL_COMPILATION) && !defined(DONT_DEPRECATE_SPRINTF)
1074 int sprintf(char *str, const char *fmt, ...) CPL_PRINT_FUNC_FORMAT(2, 3)
1075 CPL_WARN_DEPRECATED("Use snprintf() or CPLsnprintf() instead");
1076 #endif /* defined(GDAL_COMPILATION) && defined(WARN_STANDARD_PRINTF) */
1077 CPL_C_END
1078 #endif /* !defined(_MSC_VER) && !defined(__APPLE__) */
1079
1080 #if defined(__cplusplus)
1081 #ifndef CPPCHECK
1082
1083 #define CPL_ARRAYSIZE(array) \
1084 ((sizeof(array) / sizeof(*(array))) / \
1085 static_cast<size_t>(!(sizeof(array) % sizeof(*(array)))))
1086 #else
1087 /* simplified version for cppcheck */
1088 #define CPL_ARRAYSIZE(array) (sizeof(array) / sizeof(array[0]))
1089 #endif
1090
1091 extern "C++"
1092 {
1093 template <class T> static void CPL_IGNORE_RET_VAL(const T &)
1094 {
1095 }
1096 inline static bool CPL_TO_BOOL(int x)
1097 {
1098 return x != 0;
1099 }
1100 } /* extern "C++" */
1101
1102 #endif /* __cplusplus */
1103
1104 #if (((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 6)) || \
1105 (defined(__clang__) && __clang_major__ >= 3)) && \
1106 !defined(_MSC_VER))
1107 #define HAVE_GCC_DIAGNOSTIC_PUSH
1108 #endif
1109
1110 #if ((__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 2)) && \
1111 !defined(_MSC_VER))
1112 #define HAVE_GCC_SYSTEM_HEADER
1113 #endif
1114
1115 #if defined(__has_cpp_attribute)
1116 #if __has_cpp_attribute(fallthrough)
1117
1118 #define CPL_FALLTHROUGH [[fallthrough]];
1119 #endif
1120 #elif defined(__clang__) || __GNUC__ >= 7
1121
1122 #define CPL_FALLTHROUGH [[clang::fallthrough]];
1123 #endif
1124 #ifndef CPL_FALLTHROUGH
1125
1126 #define CPL_FALLTHROUGH
1127 #endif
1128
1131 #ifndef FALSE
1132 #define FALSE 0
1133 #endif
1134
1135 #ifndef TRUE
1136 #define TRUE 1
1137 #endif
1138
1139 #if __clang_major__ >= 4 || (__clang_major__ == 3 && __clang_minor__ >= 8)
1140 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW \
1141 __attribute__((no_sanitize("unsigned-integer-overflow")))
1142 #else
1143 #define CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW
1144 #endif
1145
1146 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && \
1147 defined(GDAL_COMPILATION)
1148 extern "C++"
1149 {
1150 template <class C, class A, class B>
1151 CPL_NOSANITIZE_UNSIGNED_INT_OVERFLOW inline C CPLUnsanitizedAdd(A a, B b)
1152 {
1153 return a + b;
1154 }
1155 }
1156 #endif
1157
1158 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS)
1159 #define CPL_NULLPTR nullptr
1160 #else
1161 #define CPL_NULLPTR NULL
1162 #endif
1163
1165 /* This typedef is for C functions that take char** as argument, but */
1166 /* with the semantics of a const list. In C, char** is not implicitly cast to */
1167 /* const char* const*, contrary to C++. So when seen for C++, it is OK */
1168 /* to expose the prototypes as const char* const*, but for C we keep the */
1169 /* historical definition to avoid warnings. */
1170 #if defined(__cplusplus) && !defined(CPL_SUPRESS_CPLUSPLUS) && \
1171 !defined(DOXYGEN_SKIP)
1172
1174 typedef const char *const *CSLConstList;
1175 #else
1176
1178 typedef char **CSLConstList;
1179 #endif
1180
1181 #endif /* ndef CPL_BASE_H_INCLUDED */
CPL_PRINT_FUNC_FORMAT
#define CPL_PRINT_FUNC_FORMAT(format_idx, arg_idx)
Tag a function to have printf() formatting.
Definition: cpl_port.h:950
GByte
unsigned char GByte
Unsigned byte type.
Definition: cpl_port.h:196
GUInt64
GUIntBig GUInt64
Unsigned 64 bit integer type.
Definition: cpl_port.h:249
GInt16
short GInt16
Int16 type.
Definition: cpl_port.h:192
CPLsnprintf
int CPLsnprintf(char *str, size_t size, const char *fmt,...)
snprintf() wrapper that is not sensitive to LC_NUMERIC settings.
Definition: cpl_string.cpp:1367
GInt64
GIntBig GInt64
Signed 64 bit integer type.
Definition: cpl_port.h:247
GPtrDiff_t
int GPtrDiff_t
Integer type large enough to hold the difference between 2 addresses.
Definition: cpl_port.h:267
GInt8
signed char GInt8
Signed int8 type.
Definition: cpl_port.h:198
CPL_C_START
#define CPL_C_START
Macro to start a block of C symbols.
Definition: cpl_port.h:306
CSLConstList
char ** CSLConstList
Type of a constant null-terminated list of nul terminated strings.
Definition: cpl_port.h:1178
GUIntBig
unsigned long long GUIntBig
Large unsigned integer type (generally 64-bit unsigned integer type).
Definition: cpl_port.h:229
CPLIsNan
#define CPLIsNan(x)
Return whether a floating-pointer number is NaN.
Definition: cpl_port.h:668
CPL_C_END
#define CPL_C_END
Macro to end a block of C symbols.
Definition: cpl_port.h:310
GIntBig
long long GIntBig
Large signed integer type (generally 64-bit integer type).
Definition: cpl_port.h:226
GBool
int GBool
Type for boolean values (alias to int)
Definition: cpl_port.h:207
CPLIsInf
#define CPLIsInf(x)
Return whether a floating-pointer number is +/- infinity.
Definition: cpl_port.h:671
GUInt16
unsigned short GUInt16
Unsigned int16 type.
Definition: cpl_port.h:194
GInt32
int GInt32
Int32 type.
Definition: cpl_port.h:186
CPLIsFinite
#define CPLIsFinite(x)
Return whether a floating-pointer number is finite.
Definition: cpl_port.h:673
GUInt32
unsigned int GUInt32
Unsigned int32 type.
Definition: cpl_port.h:188