VideoTools
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
gdef.h
1 /*
2  Copyright (C) 2017 Graeme Walker
3 
4  This program is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17 
18 /*
19  * gdef.h
20  */
21 
22 /* This header is always the first header included in source
23  * files. It takes care of some portability issues, and
24  * is a good candidate for precompilation. It requires
25  * either G_UNIX or G_WIN32 to be defined on the compiler
26  * command line.
27  */
28 
29 #ifndef G_DEF__H
30 #define G_DEF__H
31 
32  /* Pull in GCONFIG definitions. Use an odd file name to avoid
33  * picking up a header from some third-party library. If this
34  * file does not exist then there might be a suitable template
35  * file to copy, or just create an empty file and tweak the
36  * default values below.
37  */
38  #include <gconfig_defs.h>
39 
40  /* Check target operating-system switches
41  */
42  #if defined(_WIN32) && !defined(G_WIN32)
43  #define G_WIN32 1
44  #endif
45  #if !defined(_WIN32) && !defined(G_UNIX)
46  #define G_UNIX 1
47  #endif
48  #if defined(G_WIN32) && defined(G_UNIX)
49  #error invalid compilation switches - define G_WIN32 or G_UNIX but not both
50  #endif
51 
52  /* Define supplementary o/s switches
53  */
54  #if defined(__MINGW32__) && !defined(G_MINGW)
55  #define G_MINGW 1
56  #endif
57  #if defined(G_MINGW) && !defined(G_WIN32)
58  #error invalid compilation switches - G_MINGW requires G_WIN32
59  #endif
60  #if defined(G_WIN32) && ! defined(G_WINDOWS)
61  #define G_WINDOWS 1
62  #endif
63  #if defined(__NetBSD__)
64  #define G_UNIX_BSD 1
65  #define G_UNIX_NETBSD 1
66  #endif
67  #if defined(__OpenBSD__)
68  #define G_UNIX_BSD 1
69  #define G_UNIX_OPENBSD 1
70  #endif
71  #if defined(__FreeBSD__)
72  #define G_UNIX_BSD 1
73  #define G_UNIX_FREEBSD 1
74  #endif
75  #if defined(__APPLE__)
76  #define G_UNIX_BSD 1
77  #define G_UNIX_MAC 1
78  #endif
79  #if defined(linux) || defined(__linux__)
80  #define G_UNIX_LINUX 1
81  #endif
82 
83  /* Define the compiler family and capabilities
84  */
85  #if !defined(G_COMPILER_CXX_11)
86  #if defined(_MSC_VER)
87  /* MSV_VER 1500=>VS9(2008) 1700=>VS11(2012), 1800=>VS12(2013) 1900=>VS14(2015) */
88  #define G_COMPILER_CXX_11 (_MSC_VER >= 1900)
89  #else
90  #if __cplusplus > 199711L
91  #define G_COMPILER_CXX_11 1
92  #endif
93  #endif
94  #endif
95 
96  /* Fill in any GCONFIG gaps
97  */
98  #if !defined(GCONFIG_HAVE_CXX_NULLPTR)
99  #define GCONFIG_HAVE_CXX_NULLPTR defined(G_COMPILER_CXX_11)
100  #endif
101  #if !defined(GCONFIG_HAVE_CXX_CONSTEXPR)
102  #define GCONFIG_HAVE_CXX_CONSTEXPR defined(G_COMPILER_CXX_11)
103  #endif
104  #if !defined(GCONFIG_HAVE_CXX_NOEXCEPT)
105  #define GCONFIG_HAVE_CXX_NOEXCEPT defined(G_COMPILER_CXX_11)
106  #endif
107  #if !defined(GCONFIG_HAVE_CXX_OVERRIDE)
108  #define GCONFIG_HAVE_CXX_OVERRIDE (defined(G_COMPILER_CXX_11) || _MSC_VER >= 1800)
109  #endif
110  #if !defined(GCONFIG_HAVE_CXX_FINAL)
111  #define GCONFIG_HAVE_CXX_FINAL (defined(G_COMPILER_CXX_11) || _MSC_VER >= 1800)
112  #endif
113  #if !defined(GCONFIG_HAVE_CXX_SHARED_PTR)
114  #define GCONFIG_HAVE_CXX_SHARED_PTR (defined(G_COMPILER_CXX_11) || _MSC_VER >= 1800)
115  #endif
116  #if !defined(GCONFIG_HAVE_CXX_STD_THREAD)
117  #define GCONFIG_HAVE_CXX_STD_THREAD (defined(G_COMPILER_CXX_11) || _MSC_VER >= 1800)
118  #endif
119  #if !defined(GCONFIG_HAVE_CXX_TYPE_TRAITS_MAKE_UNSIGNED)
120  #define GCONFIG_HAVE_CXX_TYPE_TRAITS_MAKE_UNSIGNED (defined(G_COMPILER_CXX_11) || _MSC_VER >= 1800)
121  #endif
122  #if !defined(GCONFIG_HAVE_CXX_EMPLACE)
123  #define GCONFIG_HAVE_CXX_EMPLACE (defined(G_COMPILER_CXX_11) || _MSC_VER >= 1800)
124  #endif
125  #if !defined(GCONFIG_HAVE_DIRENT_H)
126  #define GCONFIG_HAVE_DIRENT_H 1
127  #endif
128  #if !defined(GCONFIG_HAVE_READLINK)
129  #define GCONFIG_HAVE_READLINK defined(G_UNIX)
130  #endif
131  #if !defined(GCONFIG_HAVE_ERRNO_T)
132  #define GCONFIG_HAVE_ERRNO_T defined(G_WIN32)
133  #endif
134  #if !defined(GCONFIG_HAVE_GETENV_S)
135  #define GCONFIG_HAVE_GETENV_S ( defined(G_WIN32) || defined(getenv_s) )
136  #endif
137  #if !defined(GCONFIG_HAVE_GETPWNAM)
138  #define GCONFIG_HAVE_GETPWNAM 1
139  #endif
140  #if !defined(GCONFIG_HAVE_GETPWNAM_R)
141  #define GCONFIG_HAVE_GETPWNAM_R 1
142  #endif
143  #if !defined(GCONFIG_HAVE_GMTIME_R)
144  #define GCONFIG_HAVE_GMTIME_R 1
145  #endif
146  #if !defined(GCONFIG_HAVE_GMTIME_S)
147  #define GCONFIG_HAVE_GMTIME_S ( defined(G_WIN32) || defined(gmtime_s) )
148  #endif
149  #if !defined(GCONFIG_HAVE_INTTYPES_H)
150  #define GCONFIG_HAVE_INTTYPES_H 1
151  #endif
152  #if !defined(GCONFIG_HAVE_IPV6)
153  #define GCONFIG_HAVE_IPV6 1
154  #endif
155  #if !defined(GCONFIG_HAVE_LOCALTIME_R)
156  #define GCONFIG_HAVE_LOCALTIME_R 1
157  #endif
158  #if !defined(GCONFIG_HAVE_LOCALTIME_S)
159  #define GCONFIG_HAVE_LOCALTIME_S ( defined(G_WIN32) || defined(localtime_s) )
160  #endif
161  #if !defined(GCONFIG_HAVE_MEMORY_H)
162  #define GCONFIG_HAVE_MEMORY_H 1
163  #endif
164  #if !defined(GCONFIG_HAVE_NDIR_H)
165  #define GCONFIG_HAVE_NDIR_H 0
166  #endif
167  #if !defined(GCONFIG_HAVE_SETGROUPS)
168  #define GCONFIG_HAVE_SETGROUPS defined(G_UNIX)
169  #endif
170  #if !defined(GCONFIG_HAVE_STATBUF_NSEC)
171  #define GCONFIG_HAVE_STATBUF_NSEC 0
172  #endif
173  #if !defined(GCONFIG_HAVE_SIN6_LEN)
174  #define GCONFIG_HAVE_SIN6_LEN 0
175  #endif
176  #if !defined(GCONFIG_HAVE_SOCKLEN_T)
177  #define GCONFIG_HAVE_SOCKLEN_T 1
178  #endif
179  #if !defined(GCONFIG_HAVE_STDINT_H)
180  #define GCONFIG_HAVE_STDINT_H 1
181  #endif
182  #if !defined(GCONFIG_HAVE_STDLIB_H)
183  #define GCONFIG_HAVE_STDLIB_H 1
184  #endif
185  #if !defined(GCONFIG_HAVE_STRNCPY_S)
186  #define GCONFIG_HAVE_STRNCPY_S ( defined(G_WIN32) || defined(strncpy_s) )
187  #endif
188  #if !defined(GCONFIG_HAVE_STRINGS_H)
189  #define GCONFIG_HAVE_STRINGS_H 1
190  #endif
191  #if !defined(GCONFIG_HAVE_STRING_H)
192  #define GCONFIG_HAVE_STRING_H 1
193  #endif
194  #if !defined(GCONFIG_HAVE_SYS_DIR_H)
195  #define GCONFIG_HAVE_SYS_DIR_H 0
196  #endif
197  #if !defined(GCONFIG_HAVE_SYS_NDIR_H)
198  #define GCONFIG_HAVE_SYS_NDIR_H 0
199  #endif
200  #if !defined(GCONFIG_HAVE_SYS_STAT_H)
201  #define GCONFIG_HAVE_SYS_STAT_H 1
202  #endif
203  #if !defined(GCONFIG_HAVE_SYS_TIME_H)
204  #define GCONFIG_HAVE_SYS_TIME_H 1
205  #endif
206  #if !defined(GCONFIG_HAVE_SYS_TYPES_H)
207  #define GCONFIG_HAVE_SYS_TYPES_H 1
208  #endif
209  #if !defined(GCONFIG_HAVE_INET_NTOP)
210  #define GCONFIG_HAVE_INET_NTOP 1
211  #endif
212  #if !defined(GCONFIG_HAVE_IF_NAMETOINDEX)
213  #define GCONFIG_HAVE_IF_NAMETOINDEX 1
214  #endif
215  #if !defined(GCONFIG_HAVE_INET_PTON)
216  #define GCONFIG_HAVE_INET_PTON 1
217  #endif
218  #if !defined(GCONFIG_HAVE_MISSING_STD_ABORT)
219  #define GCONFIG_HAVE_MISSING_STD_ABORT defined(G_WINDOWS)
220  #endif
221  #if !defined(GCONFIG_HAVE_MISSING_STD_EXIT)
222  #define GCONFIG_HAVE_MISSING_STD_EXIT defined(G_WINDOWS)
223  #endif
224  #if !defined(GCONFIG_HAVE_MISSING_STD_SYSTEM)
225  #define GCONFIG_HAVE_MISSING_STD_SYSTEM defined(G_WINDOWS)
226  #endif
227  #if !defined(GCONFIG_HAVE_MISSING_STD_MEMSET)
228  #define GCONFIG_HAVE_MISSING_STD_MEMSET defined(G_WINDOWS)
229  #endif
230  #if !defined(GCONFIG_HAVE_MISSING_STD_MEMCPY)
231  #define GCONFIG_HAVE_MISSING_STD_MEMCPY defined(G_WINDOWS)
232  #endif
233  #if !defined(GCONFIG_HAVE_MISSING_STD_STRERROR)
234  #define GCONFIG_HAVE_MISSING_STD_STRERROR defined(G_WINDOWS)
235  #endif
236  #if !defined(GCONFIG_HAVE_MISSING_STD_FOPEN)
237  #define GCONFIG_HAVE_MISSING_STD_FOPEN defined(G_WINDOWS)
238  #endif
239  #if !defined(GCONFIG_HAVE_MISSING_STD_FCLOSE)
240  #define GCONFIG_HAVE_MISSING_STD_FCLOSE defined(G_WINDOWS)
241  #endif
242  #if !defined(GCONFIG_HAVE_MISSING_STD_FPUTS)
243  #define GCONFIG_HAVE_MISSING_STD_FPUTS defined(G_WINDOWS)
244  #endif
245  #if !defined(GCONFIG_HAVE_UID_T)
246  #define GCONFIG_HAVE_UID_T defined(G_UNIX)
247  #endif
248  #if !defined(GCONFIG_HAVE_GID_T)
249  #define GCONFIG_HAVE_GID_T GCONFIG_HAVE_UID_T
250  #endif
251  #if !defined(GCONFIG_HAVE_SSIZE_T)
252  #define GCONFIG_HAVE_SSIZE_T ( defined(G_UNIX) || defined(G_MINGW) )
253  #endif
254  #if !defined(GCONFIG_HAVE_PID_T)
255  #define GCONFIG_HAVE_PID_T ( defined(G_UNIX) || defined(G_MINGW) )
256  #endif
257  #if !defined(GCONFIG_HAVE_GET_WINDOW_LONG_PTR)
258  #define GCONFIG_HAVE_GET_WINDOW_LONG_PTR defined(G_WIN32)
259  #endif
260  #if !defined(GCONFIG_HAVE_MREMAP)
261  #define GCONFIG_HAVE_MREMAP defined(G_UNIX_LINUX)
262  #endif
263  #if !defined(GCONFIG_HAVE_TIMERFD)
264  #define GCONFIG_HAVE_TIMERFD defined(G_UNIX_LINUX)
265  #endif
266  #if !defined(GCONFIG_HAVE_PAM)
267  #define GCONFIG_HAVE_PAM defined(G_UNIX)
268  #endif
269  #if !defined(GCONFIG_HAVE_PAM_IN_SECURITY)
270  #define GCONFIG_HAVE_PAM_IN_SECURITY defined(G_UNIX)
271  #endif
272  #if !defined(GCONFIG_HAVE_PAM_IN_INCLUDE)
273  #define GCONFIG_HAVE_PAM_IN_INCLUDE 0
274  #endif
275  #if !defined(GCONFIG_HAVE_PAM_IN_PAM)
276  #define GCONFIG_HAVE_PAM_IN_PAM 0
277  #endif
278  #if !defined(GCONFIG_HAVE_GET_NATIVE_SYSTEM_INFO)
279  #define GCONFIG_HAVE_GET_NATIVE_SYSTEM_INFO defined(G_WIN32)
280  #endif
281  #if !defined(GCONFIG_HAVE_LIBJPEG)
282  #define GCONFIG_HAVE_LIBJPEG 0
283  #endif
284  #if !defined(GCONFIG_HAVE_LIBPNG)
285  #define GCONFIG_HAVE_LIBPNG 0
286  #endif
287  #if !defined(GCONFIG_HAVE_LIBAV)
288  #define GCONFIG_HAVE_LIBAV 0
289  #endif
290  #if !defined(GCONFIG_HAVE_CURSES)
291  #define GCONFIG_HAVE_CURSES 0
292  #endif
293  #if !defined(GCONFIG_HAVE_LIBEXIV)
294  #define GCONFIG_HAVE_LIBEXIV 0
295  #endif
296  #if !defined(GCONFIG_HAVE_V4L)
297  #define GCONFIG_HAVE_V4L defined(G_UNIX_LINUX)
298  #endif
299  #if !defined(GCONFIG_HAVE_LIBV4L)
300  #define GCONFIG_HAVE_LIBV4L 0
301  #endif
302  #if !defined(GCONFIG_ENABLE_IPV6)
303  #define GCONFIG_ENABLE_IPV6 GCONFIG_HAVE_IPV6
304  #endif
305  #if !defined(GCONFIG_ENABLE_STD_THREAD)
306  #define GCONFIG_ENABLE_STD_THREAD GCONFIG_HAVE_CXX_STD_THREAD
307  #endif
308  #if !defined(GCONFIG_HAVE_SEM_INIT)
309  #define GCONFIG_HAVE_SEM_INIT ( defined(G_UNIX_LINUX) || defined(G_UNIX_FREEBSD) )
310  #endif
311  #if !defined(GCONFIG_HAVE_X11)
312  #define GCONFIG_HAVE_X11 defined(G_UNIX)
313  #endif
314  #if !defined(GCONFIG_HAVE_OPENSSL)
315  #define GCONFIG_HAVE_OPENSSL defined(G_UNIX)
316  #endif
317  #if !defined(GCONFIG_HAVE_OPENSSL_SSLv3)
318  #define GCONFIG_HAVE_OPENSSL_SSLv3 1
319  #endif
320  #if !defined(GCONFIG_HAVE_OPENSSL_TLSv1_1)
321  #define GCONFIG_HAVE_OPENSSL_TLSv1_1 1
322  #endif
323  #if !defined(GCONFIG_HAVE_OPENSSL_TLSv1_2)
324  #define GCONFIG_HAVE_OPENSSL_TLSv1_2 1
325  #endif
326  #if !defined(GCONFIG_HAVE_BOOST)
327  #define GCONFIG_HAVE_BOOST defined(G_UNIX_LINUX)
328  #endif
329  #if !defined(GCONFIG_HAVE_ALSA)
330  #define GCONFIG_HAVE_ALSA defined(G_UNIX_LINUX)
331  #endif
332  #if !defined(GCONFIG_HAVE_FMEMOPEN)
333  #define GCONFIG_HAVE_FMEMOPEN !defined(G_UNIX_OPENBSD)
334  #endif
335  #if !defined(GCONFIG_HAVE_X11)
336  #define GCONFIG_HAVE_X11 defined(G_UNIX)
337  #endif
338  #if !defined(GCONFIG_HAVE_IP_MREQN)
339  #define GCONFIG_HAVE_IP_MREQN ( defined(G_UNIX_LINUX) || defined(G_UNIX_FREEBSD) )
340  #endif
341 
342  /* Include main o/s headers
343  */
344  #if defined(G_WINDOWS)
345  #ifdef G_MINGW
346  #define __USE_W32_SOCKETS
347  #ifndef _WIN32_WINNT
348  #define _WIN32_WINNT _WIN32_WINNT_WINXP
349  #endif
350  #if (_WIN32_WINNT < _WIN32_WINNT_WINXP)
351  #undef _WIN32_WINNT
352  #define _WIN32_WINNT _WIN32_WINNT_WINXP
353  #endif
354  #endif
355  #ifndef WIN32_LEAN_AND_MEAN
356  #define WIN32_LEAN_AND_MEAN
357  #endif
358  #ifdef G_WIN32_IE
359  #ifndef _WIN32_IE
360  #define _WIN32_IE 0x600
361  #endif
362  #endif
363  #include <windows.h>
364  #include <shellapi.h>
365  #include <direct.h>
366  #ifdef G_MINGW
367  #include <sys/stat.h>
368  #include <unistd.h>
369  #endif
370  #include <winsock2.h>
371  #include <ws2tcpip.h>
372  #else
373  #include <unistd.h>
374  #include <sys/stat.h>
375  #include <sys/types.h>
376  #include <sys/wait.h>
377  #include <sys/utsname.h>
378  #include <sys/select.h>
379  #include <sys/socket.h>
380  #ifndef MSG_NOSIGNAL
381  #define MSG_NOSIGNAL 0
382  #endif
383  #include <sys/mman.h>
384  #if !defined(MAP_ANONYMOUS) && defined(MAP_ANON)
385  #define MAP_ANONYMOUS MAP_ANON
386  #endif
387  #ifndef MREMAP_MAYMOVE
388  #define MREMAP_MAYMOVE 0
389  #endif
390  #include <netinet/in.h>
391  #include <netdb.h>
392  #include <arpa/inet.h>
393  #if GCONFIG_HAVE_IF_NAMETOINDEX
394  #include <net/if.h>
395  #endif
396  #include <errno.h>
397  #endif
398 
399  /* Undefine some out-dated macros
400  */
401  #ifdef __cplusplus
402  #ifdef max
403  #undef max
404  #endif
405  #ifdef min
406  #undef min
407  #endif
408  #endif
409 
410  /* Include commonly-used system headers (good for pre-compilation)
411  */
412  #ifdef __cplusplus
413  #include <cstddef>
414  #include <cstdlib>
415  #include <exception>
416  #include <fstream>
417  #include <iostream>
418  #include <memory>
419  #include <sstream>
420  #include <string>
421  #include <ctime>
422  #else
423  #include <stddef.h>
424  #endif
425 
426  /* Define a few Windows-style types under unix
427  */
428  #if ! defined(G_WINDOWS)
429  typedef unsigned char BOOL ;
430  typedef unsigned int HDC ;
431  typedef unsigned int HWND ;
432  typedef unsigned int HINSTANCE ;
433  typedef unsigned int HANDLE ;
434  typedef wchar_t TCHAR ;
435  typedef int SOCKET ;
436  #endif
437 
438  /* Define fixed-size types
439  */
440  #if defined(G_WINDOWS)
441  typedef UINT32 g_uint32_t ;
442  typedef UINT16 g_uint16_t ;
443  typedef INT32 g_int32_t ;
444  typedef INT16 g_int16_t ;
445  #else
446  #include <stdint.h>
447  typedef uint32_t g_uint32_t ;
448  typedef uint16_t g_uint16_t ;
449  typedef int32_t g_int32_t ;
450  typedef int16_t g_int16_t ;
451  #endif
452  #if __cplusplus
453  typedef char assert_sizeof_uint16_is_2[sizeof(g_uint16_t)==2U?1:-1] ;
454  typedef char assert_sizeof_uint32_is_4[sizeof(g_uint32_t)==4U?1:-1] ;
455  #endif
456 
457  /* Define missing standard types
458  */
459  #if ! GCONFIG_HAVE_UID_T
460  typedef int uid_t ;
461  #endif
462  #if ! GCONFIG_HAVE_GID_T
463  typedef int gid_t ;
464  #endif
465  #if ! GCONFIG_HAVE_SSIZE_T
466  #if defined(SSIZE_T)
467  typedef SSIZE_T ssize_t ;
468  #else
469  typedef int ssize_t ;
470  #endif
471  #endif
472  #if ! GCONFIG_HAVE_PID_T
473  typedef unsigned int pid_t ;
474  #endif
475  #if ! GCONFIG_HAVE_SOCKLEN_T
476  typedef int socklen_t ;
477  #endif
478  #if ! GCONFIG_HAVE_ERRNO_T
479  typedef int errno_t ; /* for _s() return type */
480  #endif
481 
482  /* Pull some std types into the global namespace
483  */
484  #ifdef __cplusplus
485  using std::size_t ;
486  #endif
487 
488  /* Pull other stuff into the std namespace
489  */
490  #ifdef __cplusplus
491  #include <cstdio>
492  #include <cstring>
493  #include <ctime>
494  #include <cstdlib>
495  namespace std
496  {
497  #if GCONFIG_HAVE_MISSING_STD_ABORT
498  using ::abort ; // stdlib
499  #endif
500  #if GCONFIG_HAVE_MISSING_STD_EXIT
501  using ::exit ; // stdlib
502  #endif
503  #if GCONFIG_HAVE_MISSING_STD_SYSTEM
504  using ::system ; // stdlib
505  #endif
506  #if GCONFIG_HAVE_MISSING_STD_MEMSET
507  using ::memset ; // string
508  #endif
509  #if GCONFIG_HAVE_MISSING_STD_MEMCPY
510  using ::memcpy ; // string
511  #endif
512  #if GCONFIG_HAVE_MISSING_STD_STRERROR
513  using ::strerror ; // string
514  #endif
515  #if GCONFIG_HAVE_MISSING_STD_FOPEN
516  using ::fopen ; // stdio
517  #endif
518  #if GCONFIG_HAVE_MISSING_STD_FCLOSE
519  using ::fclose ; // stdio
520  #endif
521  #if GCONFIG_HAVE_MISSING_STD_FPUTS
522  using ::fputs ; // stdio
523  #endif
524  #if GCONFIG_HAVE_MISSING_STD_STRUCT_TM
525  using ::tm ; // time
526  #endif
527  #if GCONFIG_HAVE_MISSING_STD_TIME_T
528  using ::time_t ; // time
529  #endif
530  }
531  #endif
532 
533  /* Use smaller buffers and limits if building with the uClibc run-time library.
534  * See glimits.h. This assumes that the uClibc header "features.h" has been
535  * included as a side-effect of including system headers above.
536  */
537  #ifdef __UCLIBC__
538  #ifndef G_NOT_SMALL
539  #define G_SMALL
540  #endif
541  #endif
542 
543  /* Macros to explicitly ignore unused values
544  */
545  #define G_IGNORE_PASTE_IMP(a,b) a##b
546  #define G_IGNORE_PASTE(a,b) G_IGNORE_PASTE_IMP(a,b)
547  #define G_IGNORE_RETURN(type,expr) do { type G_IGNORE_PASTE(ignore_,__LINE__) = expr ; (void) G_IGNORE_PASTE(ignore_,__LINE__) ; } while(0)
548  #define G_IGNORE_PARAMETER(type,name) (void)name
549  #define G_IGNORE_VARIABLE(name) name=name
550 
551  /* C++ language backwards compatibility
552  */
553  #ifdef __cplusplus
554 
555  #if ! GCONFIG_HAVE_CXX_NULLPTR
556  #ifndef nullptr
557  #define nullptr NULL
558  #endif
559  #endif
560 
561  #if GCONFIG_HAVE_CXX_CONSTEXPR
562  #define g__constexpr constexpr
563  #else
564  /* for in-class initialisation of static integer-type constants only */
565  #define g__constexpr const
566  #endif
567 
568  #if GCONFIG_HAVE_CXX_NOEXCEPT
569  #define g__noexcept noexcept
570  #else
571  #define g__noexcept throw()
572  #endif
573 
574  #if ! GCONFIG_HAVE_CXX_OVERRIDE
575  #define override
576  #endif
577 
578  #if GCONFIG_HAVE_CXX_FINAL
579  #define g__final final
580  #else
581  #define g__final
582  #endif
583 
584  #if GCONFIG_HAVE_CXX_SHARED_PTR
585  #include <memory>
586  using std::shared_ptr ;
587  using std::unique_ptr ;
588  using std::const_pointer_cast ;
589  #else
590  namespace G {
591  template <typename T>
592  struct shared_ptr_deleter
593  {
594  static void fn( void * p ) { delete static_cast<T*>(p) ; }
595  } ;
596  struct shared_ptr_control
597  {
598  explicit shared_ptr_control( void * object , void (*deleter)(void*) ) :
599  m_object(object) , m_deleter(deleter) , m_n(1U) {}
600  explicit shared_ptr_control( const void * object , void (*deleter)(void*) ) :
601  m_object(const_cast<void*>(object)) , m_deleter(deleter) , m_n(1U) {}
602  void inc() { m_n++ ; }
603  bool dec() { m_n-- ; return m_n == 0 ; }
604  void * m_object ;
605  void (*m_deleter)(void *) ;
606  unsigned int m_n ;
607  } ;
608  template <typename T> class shared_ptr
609  {
610  public:
611  typedef shared_ptr<T> ptr ;
612  explicit shared_ptr( T * p = 0 ) : m_control(new shared_ptr_control(p,&shared_ptr_deleter<T>::fn)) , m_p(p) {}
613  shared_ptr( T * p , void (*d)(void*) ) : m_control(new shared_ptr_control(p,d)) , m_p(p) {}
614  template <typename Y> shared_ptr( const shared_ptr<Y> & obj , T * p ) : m_control(obj.m_control) , m_p(p) { inc(); }
615  ~shared_ptr() { dec(); }
616  void reset( T * p = 0 ) { ptr tmp(p); swap(tmp); }
617  void reset( T * p , void (*d)(void*) ) { ptr tmp(p,d); swap(tmp); }
618  T * get() { return m_p; }
619  const T * get() const { return m_p; }
620  T * operator->() { return m_p; }
621  const T * operator->() const { return m_p; }
622  T & operator*() { return *m_p; }
623  const T & operator*() const { return *m_p; }
624  operator bool () const { return m_p != nullptr; }
625  template <typename D> shared_ptr( const shared_ptr<D> & o ) : m_control(o.m_control) , m_p(o.m_p) { inc(); }
626  shared_ptr( const shared_ptr<T> & o ) : m_control(o.m_control) , m_p(o.m_p) { inc(); }
627  void operator=( const shared_ptr<T> & o ) { ptr tmp(o); swap(tmp); }
628  unsigned int use_count() const { return m_control->m_n ; }
629  bool unique() const { return use_count() == 1; }
630  private:
631  void swap( shared_ptr<T> & o ) { std::swap(m_p,o.m_p); std::swap(m_control,o.m_control); }
632  void dec()
633  {
634  if( m_control->dec() )
635  {
636  m_control->m_deleter( m_control->m_object ) ;
637  delete m_control ;
638  m_control = nullptr ;
639  m_p = nullptr ;
640  }
641  }
642  void inc() { m_control->inc(); }
643  public:
644  shared_ptr_control * m_control ;
645  T * m_p ;
646  } ;
647  template <typename T, typename U>
648  shared_ptr<T> const_pointer_cast( const shared_ptr<U> & ptr_in ) g__noexcept
649  {
650  T * p = const_cast<T*>(ptr_in.get()) ;
651  return shared_ptr<T>( ptr_in , p ) ;
652  }
653  template <typename T> class unique_ptr
654  {
655  public:
656  unique_ptr() : m_p(0) {}
657  explicit unique_ptr( T * p ) : m_p(p) {}
658  explicit unique_ptr( std::auto_ptr<T> p ) : m_p(p.release()) {}
659  ~unique_ptr() { delete m_p; }
660  void reset( T * p ) { delete m_p; m_p = p; }
661  void reset() { delete m_p; m_p = 0; }
662  T * get() { return m_p; }
663  T & operator*() { return *m_p; }
664  const T * get() const { return m_p; }
665  const T & operator*() const { return *m_p; }
666  T * operator->() { return m_p; }
667  const T * operator->() const { return m_p; }
668  T * release() { T * p = m_p; m_p = 0; return p; }
669  operator bool () const { return m_p != nullptr; }
670  unique_ptr( const unique_ptr<T> & rhs ) { m_p = rhs.m_p ; const_cast<unique_ptr<T>&>(rhs).m_p = 0 ; } // auto_ptr semantics
671  private:
672  void operator=( const unique_ptr<T> & ) ;
673  private:
674  T * m_p ;
675  } ;
676  }
677  using G::shared_ptr ;
678  using G::unique_ptr ;
679  using G::const_pointer_cast ;
680  #endif
681 
682  #if GCONFIG_ENABLE_STD_THREAD
683  #include <thread>
684  #include <mutex>
685  #include <cstring>
686  namespace G
687  {
688  struct threading /// Helper class for std::thread capabilities.
689  {
690  enum { using_std_thread = 1 } ;
691  typedef std::thread thread_type ;
692  typedef std::mutex mutex_type ;
693  typedef std::lock_guard<std::mutex> lock_type ;
694  static bool works() ; // run-time test -- see gthread.cpp
695  } ;
696  }
697  #else
698  namespace G
699  {
700  class dummy_thread
701  {
702  public:
703  template <typename T_fn> dummy_thread( T_fn fn ) { fn() ; }
704  template <typename T_fn,typename T_arg1> dummy_thread( T_fn fn , T_arg1 arg1 ) { fn(arg1) ; }
705  template <typename T_fn,typename T_arg1,typename T_arg2> dummy_thread( T_fn fn , T_arg1 arg1 , T_arg2 arg2 ) { fn(arg1,arg2) ; }
706  dummy_thread() {}
707  bool joinable() const { return false ; }
708  void detach() {}
709  void join() {}
710  } ;
711  class dummy_mutex {} ;
712  class dummy_lock { public: explicit dummy_lock( dummy_mutex & ) {} } ;
713  struct threading
714  {
715  enum { using_std_thread = 0 } ;
716  typedef G::dummy_thread thread_type ;
717  typedef G::dummy_mutex mutex_type ;
718  typedef G::dummy_lock lock_type ;
719  static bool works() ; // run-time test -- see gthread.cpp
720  } ;
721  }
722  #endif
723 
724  #if ! GCONFIG_HAVE_CXX_TYPE_TRAITS_MAKE_UNSIGNED
725  namespace std
726  {
727  template <typename T> struct make_unsigned {} ;
728  template <> struct make_unsigned<unsigned char> { typedef unsigned char type ; } ;
729  template <> struct make_unsigned<char> { typedef unsigned char type ; } ;
730  template <> struct make_unsigned<signed char> { typedef unsigned char type ; } ;
731  template <> struct make_unsigned<unsigned short> { typedef unsigned short type ; } ;
732  template <> struct make_unsigned<short> { typedef unsigned short type ; } ;
733  template <> struct make_unsigned<unsigned int> { typedef unsigned int type ; } ;
734  template <> struct make_unsigned<int> { typedef unsigned int type ; } ;
735  template <> struct make_unsigned<unsigned long> { typedef unsigned long type ; } ;
736  template <> struct make_unsigned<long> { typedef unsigned long type ; } ;
737  }
738  #endif
739 
740  #endif
741 
742  /* Run-time o/s identification */
743  #ifdef __cplusplus
744  namespace G
745  {
746  #ifdef G_WINDOWS
747  inline bool is_windows() { return true ; }
748  #else
749  inline bool is_windows() { return false ; }
750  #endif
751  #ifdef G_UNIX_LINUX
752  inline bool is_linux() { return true ; }
753  #else
754  inline bool is_linux() { return false ; }
755  #endif
756  #ifdef G_UNIX_FREEBSD
757  inline bool is_free_bsd() { return true ; }
758  #else
759  inline bool is_free_bsd() { return false ; }
760  #endif
761  #ifdef G_UNIX_OPENBSD
762  inline bool is_open_bsd() { return true ; }
763  #else
764  inline bool is_open_bsd() { return false ; }
765  #endif
766  #ifdef G_UNIX_BSD
767  inline bool is_bsd() { return true ; }
768  #else
769  inline bool is_bsd() { return false ; }
770  #endif
771  }
772  #endif
773 
774  /* Network code fix-ups
775  */
776 
777  typedef g_uint16_t g_port_t ; /* since 'in_port_t' not always available */
778 
779  #ifdef G_WINDOWS
780  #ifdef G_MINGW
781  #ifndef AI_NUMERICSERV
782  #define AI_NUMERICSERV 0
783  #endif
784  #ifndef AI_ADDRCONFIG
785  #define AI_ADDRCONFIG 0
786  #endif
787  #endif
788  #ifndef MSG_NOSIGNAL
789  #define MSG_NOSIGNAL 0
790  #endif
791  #endif
792 
793  #ifndef INADDR_NONE
794  /* (should be in netinet/in.h) */
795  #define INADDR_NONE 0xffffffff
796  #endif
797 
798  /* Inline portability shims
799  */
800 
801  #ifdef __cplusplus
802  #if GCONFIG_HAVE_IPV6
803  inline void gnet_address6_init( sockaddr_in6 & s )
804  {
805  #if GCONFIG_HAVE_SIN6_LEN
806  s.sin6_len = sizeof(s) ;
807  #else
808  (void) s ;
809  #endif
810  }
811  #endif
812  #endif
813 
814  /* Inline definitions of missing functions
815  */
816 
817  #if defined(G_UNIX)
818  #endif
819 
820  #if ! GCONFIG_HAVE_INET_PTON
821  #ifdef __cplusplus
822  namespace GNet { int inet_pton_imp( int f , const char * p , void * result ) ; }
823  inline int inet_pton( int f , const char * p , void * result )
824  {
825  return GNet::inet_pton_imp( f , p , result ) ;
826  }
827  #endif
828  #endif
829 
830  #if ! GCONFIG_HAVE_INET_NTOP
831  #ifdef __cplusplus
832  namespace GNet { const char * inet_ntop_imp( int f , void * ap , char * buffer , size_t n ) ; }
833  inline const char * inet_ntop( int f , void * ap , char * buffer , size_t n )
834  {
835  return GNet::inet_ntop_imp( f , ap , buffer , n ) ;
836  }
837  #endif
838  #endif
839 
840  #if ! GCONFIG_HAVE_IF_NAMETOINDEX
841  #ifdef __cplusplus
842  inline unsigned int if_nametoindex( const char * )
843  {
844  return 0U ;
845  }
846  #endif
847  #endif
848 
849  #if ! GCONFIG_HAVE_READLINK && !defined(readlink)
850  #ifdef __cplusplus
851  inline ssize_t readlink( const char * , char * , size_t )
852  {
853  return -1 ;
854  }
855  #endif
856  #endif
857 
858  #if GCONFIG_HAVE_GETPWNAM && ! GCONFIG_HAVE_GETPWNAM_R
859  #ifdef __cplusplus
860  #include <sys/types.h>
861  #include <pwd.h>
862  inline int getpwnam_r( const char * name , struct passwd * pwd ,
863  char * buf , size_t buflen , struct passwd ** result )
864  {
865  struct passwd * p = ::getpwnam( name ) ;
866  if( p )
867  {
868  *pwd = *p ; /* string pointers still point into library storage */
869  *result = pwd ;
870  return 0 ;
871  }
872  else
873  {
874  *result = nullptr ;
875  return 0 ; /* or errno */
876  }
877  }
878  #endif
879  #endif
880 
881  #if ! GCONFIG_HAVE_GMTIME_R && !defined(gmtime_r) && defined(G_UNIX)
882  #ifdef __cplusplus
883  #include <ctime>
884  inline std::tm * gmtime_r( const std::time_t * tp , std::tm * tm_p )
885  {
886  const struct std::tm * p = std::gmtime( tp ) ;
887  if( p == 0 ) return 0 ;
888  *tm_p = *p ;
889  return tm_p ;
890  }
891  #endif
892  #endif
893 
894  #if ! GCONFIG_HAVE_LOCALTIME_R && !defined(localtime_r) && defined(G_UNIX)
895  #ifdef __cplusplus
896  #include <ctime>
897  inline struct std::tm * localtime_r( const std::time_t * tp , struct std::tm * tm_p )
898  {
899  const struct std::tm * p = std::localtime( tp ) ;
900  if( p == 0 ) return 0 ;
901  *tm_p = *p ;
902  return tm_p ;
903  }
904  #endif
905  #endif
906 
907  #if ! GCONFIG_HAVE_LOCALTIME_S && !defined(localtime_s)
908  #ifdef __cplusplus
909  inline errno_t localtime_s( struct std::tm * tm_p , const std::time_t * tp )
910  {
911  const errno_t e_inval = 22 ;
912  if( tm_p == nullptr ) return e_inval ;
913  tm_p->tm_sec = tm_p->tm_min = tm_p->tm_hour = tm_p->tm_mday = tm_p->tm_mon =
914  tm_p->tm_year = tm_p->tm_wday = tm_p->tm_yday = tm_p->tm_isdst = -1 ;
915  if( tp == nullptr || *tp < 0 ) return e_inval ;
916  const struct std::tm * p = std::localtime( tp ) ;
917  if( p == 0 ) return e_inval ;
918  *tm_p = *p ;
919  return 0 ;
920  }
921  #endif
922  #endif
923 
924  #if ! GCONFIG_HAVE_GMTIME_S && !defined(gmtime_s)
925  #ifdef __cplusplus
926  inline errno_t gmtime_s( struct std::tm * tm_p , const std::time_t * tp )
927  {
928  const errno_t e_inval = 22 ;
929  if( tm_p == nullptr ) return e_inval ;
930  tm_p->tm_sec = tm_p->tm_min = tm_p->tm_hour = tm_p->tm_mday = tm_p->tm_mon =
931  tm_p->tm_year = tm_p->tm_wday = tm_p->tm_yday = tm_p->tm_isdst = -1 ;
932  if( tp == nullptr || *tp < 0 ) return e_inval ;
933  const struct std::tm * p = std::gmtime( tp ) ;
934  if( p == 0 ) return e_inval ;
935  *tm_p = *p ;
936  return 0 ;
937  }
938  #endif
939  #endif
940 
941  #if GCONFIG_HAVE_SETGROUPS
942  #include <grp.h>
943  #else
944  #ifdef __cplusplus
945  inline int setgroups( size_t , const gid_t * )
946  {
947  return 0 ;
948  }
949  #endif
950  #endif
951 
952  #if ! GCONFIG_HAVE_GETENV_S
953  #ifdef __cplusplus
954  #include <cstdlib>
955  inline errno_t getenv_s( size_t * n_out , char * buffer , size_t n_in , const char * name )
956  {
957  const errno_t e_inval = 22 ;
958  const errno_t e_range = 34 ;
959  if( n_out == nullptr || name == nullptr ) return e_inval ;
960  const char * p = ::getenv( name ) ;
961  *n_out = p ? (strlen(p) + 1U) : 0 ;
962  if( p && *n_out > n_in ) return e_range ;
963  if( p && buffer ) strcpy( buffer , p ) ;
964  return 0 ;
965  }
966 
967  #endif
968  #endif
969 
970  #if ! GCONFIG_HAVE_STRNCPY_S
971  #ifndef _TRUNCATE
972  #define _TRUNCATE (~((size_t)(0U)))
973  #endif
974  #ifdef __cplusplus
975  inline errno_t strncpy_s( char * dst , size_t n , const char * src , size_t c )
976  {
977  if( dst == nullptr ) return EINVAL ;
978  if( src == nullptr ) { *dst = '\0' ; return EINVAL ; }
979  if( n == 0 ) { return EINVAL ; }
980  size_t d = strlen(src) ; if( c != _TRUNCATE && c < d ) d = c ;
981  if( d >= n )
982  {
983  if( c != _TRUNCATE ) { *dst = '\0' ; return ERANGE ; }
984  strncpy( dst , src , n ) ;
985  dst[n-1U] = '\0' ;
986  }
987  else
988  {
989  strncpy( dst , src , d ) ;
990  dst[d] = '\0' ;
991  }
992  return 0 ;
993  }
994 
995  #endif
996  #endif
997 
998  #if ! GCONFIG_HAVE_GET_WINDOW_LONG_PTR && defined(G_WINDOWS)
999  #ifdef __cplusplus
1000  typedef char assert_thirty_two_bit_windows[sizeof(void*)==4U?1:-1] ; // if this fails then we are on win64 so no need for this block at all
1001  const int GWLP_HINSTANCE = GWL_HINSTANCE ;
1002  const int GWLP_WNDPROC = GWL_WNDPROC ;
1003  const int DWLP_USER = DWL_USER ;
1004  inline LONG_PTR GetWindowLongPtr( HWND h , int id )
1005  {
1006  return static_cast<LONG_PTR>(::GetWindowLong(h,id)) ;
1007  }
1008  inline LONG_PTR SetWindowLongPtr( HWND h , int id , LONG_PTR value )
1009  {
1010  return static_cast<LONG_PTR>(::SetWindowLong(h,id,static_cast<LONG>(value))) ;
1011  }
1012  #endif
1013  #endif
1014 
1015  #if ! GCONFIG_HAVE_MREMAP && defined(G_UNIX)
1016  #ifdef __cplusplus
1017  inline void * mremap( void * , size_t , size_t , int )
1018  {
1019  errno = ENOSYS ;
1020  return (void*)(-1) ;
1021  }
1022  #endif
1023  #endif
1024 
1025 #endif
Helper class for std::thread capabilities.
Definition: gdef.h:688