UCommon
platform.h
Go to the documentation of this file.
1 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
2 // Copyright (C) 2015 Cherokees of Idaho.
3 //
4 // This file is part of GNU uCommon C++.
5 //
6 // GNU uCommon C++ is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU Lesser General Public License as published
8 // by the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // GNU uCommon C++ is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public License
17 // along with GNU uCommon C++. If not, see <http://www.gnu.org/licenses/>.
18 
29 #include <cstdlib>
30 #include <cstddef>
31 #if defined(sun) && defined(unix)
32 #include <malloc.h>
33 #endif
34 
35 #ifndef _UCOMMON_PLATFORM_H_
36 #define _UCOMMON_PLATFORM_H_
37 #define UCOMMON_ABI 6
38 
39 #ifndef UCOMMON_SYSRUNTIME
40 #ifndef NEW_STDCPP
41 #define NEW_STDCPP
42 #endif
43 #define _UCOMMON_EXTENDED_
44 #endif
45 
56 #define UCOMMON_NAMESPACE ucommon
57 #define NAMESPACE_UCOMMON namespace ucommon {
58 #define END_NAMESPACE }
59 
60 #ifndef _REENTRANT
61 #define _REENTRANT 1
62 #endif
63 
64 #ifndef __PTH__
65 #ifndef _THREADSAFE
66 #define _THREADSAFE 1
67 #endif
68 
69 #ifndef _POSIX_PTHREAD_SEMANTICS
70 #define _POSIX_PTHREAD_SEMANTICS
71 #endif
72 #endif
73 
74 #if !defined(__GNUC__) && !defined(__has_feature) && !defined(_MSC_VER)
75 #define UCOMMON_RTTI 1
76 #endif
77 
78 #if __GNUC__ > 3 && defined(__GXX_RTTI)
79 #define UCOMMON_RTTI 1
80 #endif
81 
82 #if defined(_MSC_VER) && defined(_CPPRTTI)
83 #define UCOMMON_RTTI 1
84 #endif
85 
86 #if defined(__has_feature)
87 #if __has_feature(cxx_rtti)
88 #define UCOMMON_RTTI 1
89 #endif
90 #endif
91 
92 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
93 #define _GNU_SOURCE
94 #endif
95 
96 #if !defined(__GNUC_PREREQ__)
97 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
98 #define __GNUC_PREREQ__(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
99 #else
100 #define __GNUC_PREREQ__(maj, min) 0
101 #endif
102 #endif
103 
104 #if __GNUC_PREREQ__(3,3)
105 #define __PRINTF(x,y) __attribute__ ((format (printf, x, y)))
106 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
107 #define __MALLOC __attribute__ ((malloc))
108 #endif
109 
110 #if __cplusplus < 201103L
111 #define __FINAL
112 #define __OVERRIDE
113 #define __DELETED
114 #define __DELETE_COPY(x) inline x(const x&);\
115  inline x& operator=(const x&)
116 #define __DELETE_DEFAULTS(x) inline x();\
117  __DELETE_COPY(x)
118 #else
119 #define __FINAL final
120 #define __OVERRIDE override
121 #define __DELETED =delete
122 #define __DELETE_COPY(x) inline x(const x&) =delete;\
123  inline x& operator=(const x&) =delete
124 #define __DELETE_DEFAULTS(x) inline x() =delete;\
125  __DELETE_COPY(x)
126 #endif
127 
128 #if __cplusplus <= 199711L && !defined(_MSC_VER)
129 #if defined(__GNUC_MINOR__) && !defined(__clang__)
130 #define nullptr __null
131 #else
132 const class nullptr_t
133 {
134 public:
135  template<class T>
136  inline operator T*() const {
137  return 0;
138  }
139 
140  template<class C, class T>
141  inline operator T C::*() const {
142  return 0;
143  }
144 
145 private:
146  void operator&() const;
147 
148 } nullptr = {};
149 #endif
150 #endif
151 
152 #ifndef __MALLOC
153 #define __PRINTF(x, y)
154 #define __SCANF(x, y)
155 #define __MALLOC
156 #endif
157 
158 #ifndef DEBUG
159 #ifndef NDEBUG
160 #define NDEBUG
161 #endif
162 #endif
163 
164 #ifdef DEBUG
165 #ifdef NDEBUG
166 #undef NDEBUG
167 #endif
168 #endif
169 
170 // see if targeting legacy Microsoft windows platform
171 
172 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
173 #define _MSWINDOWS_
174 
175 #if defined(_MSC_VER)
176 #define NOMINMAX
177 #if _MSC_VER < 1500
178 #warning "Probably won't build, need VS >= 2010 or later"
179 #endif
180 #endif
181 
182 // minimum required version requires conditional
183 #ifdef _WIN32_WINNT
184 #if _WIN32_WINNT < 0x0600
185 #undef _WIN32_WINNT
186 #undef WINVER
187 #endif
188 #endif
189 
190 #ifndef _WIN32_WINNT
191 #define _WIN32_WINNT 0x0600
192 #endif
193 
194 #ifdef _MSC_VER
195 #pragma warning(disable: 4251)
196 #pragma warning(disable: 4996)
197 #pragma warning(disable: 4355)
198 #pragma warning(disable: 4290)
199 #pragma warning(disable: 4291)
200 #endif
201 
202 #if defined(__BORLANDC__) && !defined(__MT__)
203 #error Please enable multithreading
204 #endif
205 
206 #if defined(_MSC_VER) && !defined(_MT)
207 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
208 #endif
209 
210 // Make sure we're consistent with _WIN32_WINNT
211 #ifndef WINVER
212 #define WINVER _WIN32_WINNT
213 #endif
214 
215 #ifndef WIN32_LEAN_AND_MEAN
216 #define WIN32_LEAN_AND_MEAN
217 #endif
218 
219 #include <winsock2.h>
220 #include <ws2tcpip.h>
221 
222 #if defined(_MSC_VER)
223 typedef int socksize_t;
224 typedef int socklen_t;
225 typedef signed long ssize_t;
226 typedef int pid_t;
227 #else
228 typedef size_t sockword_t;
229 typedef size_t socksize_t;
230 #endif
231 
232 #include <process.h>
233 #ifndef __EXPORT
234 #ifdef UCOMMON_STATIC
235 #define __EXPORT
236 #else
237 #define __EXPORT __declspec(dllimport)
238 #endif
239 #endif
240 #define __LOCAL
241 
242 // if runtime mode then non-runtime libraries are static on windows...
243 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
244 #define __SHARED
245 #else
246 #define __SHARED __EXPORT
247 #endif
248 
249 #else
250 typedef size_t socksize_t;
251 #define __EXPORT __attribute__ ((visibility("default")))
252 #define __LOCAL __attribute__ ((visibility("hidden")))
253 #define __SHARED __attribute__ ((visibility("default")))
254 #endif
255 
256 #ifdef _MSWINDOWS_
257 
258 #define _UWIN
259 
260 #include <sys/stat.h>
261 #include <io.h>
262 
263 // gcc mingw can do native high performance win32 conditionals...
264 #if defined(UCOMMON_WINPTHREAD) && __GNUC_PREREQ__(4, 8) && !defined(UCOMMON_SYSRUNTIME)
265 #define __MINGW_WINPTHREAD__
266 #include <pthread.h> // gnu libstdc++ now requires a win pthread
267 typedef size_t stacksize_t;
268 #else
269 #define _MSTHREADS_
270 typedef DWORD pthread_t;
271 typedef unsigned stacksize_t;
272 typedef CRITICAL_SECTION pthread_mutex_t;
273 #endif
274 typedef char *caddr_t;
275 typedef HANDLE fd_t;
276 typedef SOCKET socket_t;
277 
278 #ifdef _MSC_VER
279 typedef struct timespec {
280  time_t tv_sec;
281  long tv_nsec;
282 } timespec_t;
283 #endif
284 
285 inline void sleep(int seconds)
286  {::Sleep((seconds * 1000l));}
287 
288 extern "C" {
289 
290  #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
291 
292  typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
293 
294 #ifdef _MSTHREADS_
295  inline void pthread_exit(void *p)
296  {_endthreadex((DWORD)0);}
297 
298  inline pthread_t pthread_self(void)
299  {return (pthread_t)GetCurrentThreadId();}
300 
301  inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
302  {InitializeCriticalSection(mutex); return 0;}
303 
304  inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
305  {DeleteCriticalSection(mutex);}
306 
307  inline void pthread_mutex_lock(pthread_mutex_t *mutex)
308  {EnterCriticalSection(mutex);}
309 
310  inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
311  {LeaveCriticalSection(mutex);}
312 #endif
313 
314  inline char *strdup(const char *s)
315  {return _strdup(s);}
316 
317  inline int stricmp(const char *s1, const char *s2)
318  {return _stricmp(s1, s2);}
319 
320  inline int strnicmp(const char *s1, const char *s2, size_t l)
321  {return _strnicmp(s1, s2, l);}
322 }
323 
324 #elif defined(__PTH__)
325 
326 #include <pth.h>
327 #include <sys/wait.h>
328 
329 typedef size_t stacksize_t;
330 typedef int socket_t;
331 typedef int fd_t;
332 #define INVALID_SOCKET -1
333 #define INVALID_HANDLE_VALUE -1
334 #include <signal.h>
335 
336 #define pthread_mutex_t pth_mutex_t
337 #define pthread_cond_t pth_cond_t
338 #define pthread_t pth_t
339 
340 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
341  {return pth_sigmask(how, set, oset);};
342 
343 inline void pthread_exit(void *p)
344  {pth_exit(p);};
345 
346 inline void pthread_kill(pthread_t tid, int sig)
347  {pth_raise(tid, sig);};
348 
349 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
350  {return pth_mutex_init(mutex) != 0;};
351 
352 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
353  {};
354 
355 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
356  {pth_mutex_acquire(mutex, 0, NULL);};
357 
358 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
359  {pth_mutex_release(mutex);};
360 
361 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
362  {pth_cond_await(cond, mutex, NULL);};
363 
364 inline void pthread_cond_signal(pthread_cond_t *cond)
365  {pth_cond_notify(cond, FALSE);};
366 
367 inline void pthread_cond_broadcast(pthread_cond_t *cond)
368  {pth_cond_notify(cond, TRUE);};
369 
370 #else
371 
372 #include <pthread.h>
373 
374 typedef size_t stacksize_t;
375 typedef int socket_t;
376 typedef int fd_t;
377 #define INVALID_SOCKET -1
378 #define INVALID_HANDLE_VALUE -1
379 #include <signal.h>
380 
381 #endif
382 
383 #ifdef _MSC_VER
384 typedef signed __int8 int8_t;
385 typedef unsigned __int8 uint8_t;
386 typedef signed __int16 int16_t;
387 typedef unsigned __int16 uint16_t;
388 typedef signed __int32 int32_t;
389 typedef unsigned __int32 uint32_t;
390 typedef signed __int64 int64_t;
391 typedef unsigned __int64 uint64_t;
392 typedef char *caddr_t;
393 
394 #include <stdio.h>
395 #define snprintf(p, s, f, ...) _snprintf_s(p, s, _TRUNCATE, f, __VA_ARGS__)
396 #define vsnprintf(p, s, f, a) _vsnprintf_s(p, s, _TRUNCATE, f, a)
397 
398 #else
399 
400 #include <sys/stat.h>
401 #include <sys/types.h>
402 #include <stdint.h>
403 #include <unistd.h>
404 #include <stdio.h>
405 
406 #endif
407 
408 #undef getchar
409 #undef putchar
410 
411 #ifndef _GNU_SOURCE
412 typedef void (*sighandler_t)(int);
413 #endif
414 typedef unsigned long timeout_t;
415 
416 #include <cctype>
417 #include <climits>
418 #include <cerrno>
419 #ifndef UCOMMON_RUNTIME
420 #include <new>
421 #endif
422 
423 #ifdef _MSWINDOWS_
424 #ifndef ENETDOWN
425 #define ENETDOWN ((int)(WSAENETDOWN))
426 #endif
427 #ifndef EINPROGRESS
428 #define EINPROGRESS ((int)(WSAEINPROGRESS))
429 #endif
430 #ifndef ENOPROTOOPT
431 #define ENOPROTOOPT ((int)(WSAENOPROTOOPT))
432 #endif
433 #ifndef EADDRINUSE
434 #define EADDRINUSE ((int)(WSAEADDRINUSE))
435 #endif
436 #ifndef EADDRNOTAVAIL
437 #define EADDRNOTAVAIL ((int)(WSAEADDRNOTAVAIL))
438 #endif
439 #ifndef ENETUNREACH
440 #define ENETUNREACH ((int)(WSAENETUNREACH))
441 #endif
442 #ifndef EHOSTUNREACH
443 #define EHOSTUNREACH ((int)(WSAEHOSTUNREACH))
444 #endif
445 #ifndef EHOSTDOWN
446 #define EHOSTDOWN ((int)(WSAEHOSTDOWN))
447 #endif
448 #ifndef ENETRESET
449 #define ENETRESET ((int)(WSAENETRESET))
450 #endif
451 #ifndef ECONNABORTED
452 #define ECONNABORTED ((int)(WSAECONNABORTED))
453 #endif
454 #ifndef ECONNRESET
455 #define ECONNRESET ((int)(WSAECONNRESET))
456 #endif
457 #ifndef EISCONN
458 #define EISCONN ((int)(WSAEISCONN))
459 #endif
460 #ifndef ENOTCONN
461 #define ENOTCONN ((int)(WSAENOTCONN))
462 #endif
463 #ifndef ESHUTDOWN
464 #define ESHUTDOWN ((int)(WSAESHUTDOWN))
465 #endif
466 #ifndef ETIMEDOUT
467 #define ETIMEDOUT ((int)(WSAETIMEDOUT))
468 #endif
469 #ifndef ECONNREFUSED
470 #define ECONNREFUSED ((int)(WSAECONNREFUSED))
471 #endif
472 #endif
473 
474 #ifndef DEBUG
475 #ifndef NDEBUG
476 #define NDEBUG
477 #endif
478 #endif
479 
480 #ifdef DEBUG
481 #ifdef NDEBUG
482 #undef NDEBUG
483 #endif
484 #endif
485 
486 #ifndef PROGRAM_MAIN
487 #define PROGRAM_MAIN(argc, argv) extern "C" int main(int argc, char **argv)
488 #define PROGRAM_EXIT(code) return code
489 #endif
490 
491 #ifndef SERVICE_MAIN
492 #define SERVICE_MAIN(id, argc, argv) void service_##id(int argc, char **argv)
493 typedef void (*cpr_service_t)(int argc, char **argv);
494 #endif
495 
496 #include <assert.h>
497 #ifdef DEBUG
498 #define crit(x, text) assert(x)
499 #else
500 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
501 #endif
502 
509 template<class T>
510 inline T *init(T *memory)
511  {return ((memory) ? new(((void *)memory)) T : NULL);}
512 
513 typedef long Integer;
514 typedef unsigned long Unsigned;
515 typedef double Real;
516 typedef uint8_t ubyte_t;
517 
522 inline void strfree(char *str)
523  {::free(str);}
524 
525 template<class T, class S>
526 inline T polypointer_cast(S *s)
527 {
528 #if defined(DEBUG) && defined(UCOMMON_RTTI)
529  assert(dynamic_cast<T>(s) != NULL); // rtti for debug only...
530 #endif
531  return static_cast<T>(s);
532 }
533 
534 template<class T, class S>
535 inline T polyconst_cast(S *s)
536 {
537  return const_cast<T>(polypointer_cast<T>(s));
538 }
539 
540 template<class T, class S>
541 inline T polystatic_cast(S *s)
542 {
543  return static_cast<T>(s);
544 }
545 
546 template<class T, class S>
547 inline T polydynamic_cast(S *s)
548 {
549 #if defined(UCOMMON_RTTI)
550  return dynamic_cast<T>(s);
551 #else
552  return static_cast<T>(s);
553 #endif
554 }
555 
556 template<class T, class S>
557 inline T& polyreference_cast(S *s)
558 {
559 #if defined(DEBUG) && defined(UCOMMON_RTTI)
560  assert(dynamic_cast<T*>(s) != NULL); // rtti for debug only...
561 #endif
562  return *(static_cast<T*>(s));
563 }
564 
565 template<typename T>
566 inline T& reference_cast(T *pointer) {
567 #ifdef DEBUG
568  assert(pointer != NULL);
569 #endif
570  return *pointer;
571 }
572 
573 template<typename T>
574 inline const T immutable_cast(T p)
575 {
576  return static_cast<const T>(p);
577 }
578 
579 #endif
void(* sighandler_t)(int)
Convenient typedef for signal handlers.
Definition: platform.h:412
Process services.
void strfree(char *str)
Matching function for strdup().
Definition: platform.h:522
T * init(T *memory)
Template function to initialize memory by invoking default constructor.
Definition: platform.h:510