31 #if __cplusplus >= 201103L
35 #if defined(sun) && defined(unix)
39 #ifndef _UCOMMON_PLATFORM_H_
40 #define _UCOMMON_PLATFORM_H_
43 #ifndef UCOMMON_SYSRUNTIME
47 #define _UCOMMON_EXTENDED_
49 #define __THROW_SIZE(x) throw std::length_error(x)
50 #define __THROW_RANGE(x) throw std::out_of_range(x)
51 #define __THROW_RUNTIME(x) throw std::runtime_error(x)
52 #define __THROW_ALLOC() throw std::bad_alloc()
53 #define __THROW_DEREF(v) if(v == nullptr) \
54 throw std::runtime_error("Dereference NULL")
55 #define __THROW_UNDEF(v,x) if(v == nullptr) throw std::runtime_error(x)
57 #define __THROW_RANGE(x) abort()
58 #define __THROW_SIZE(x) abort()
59 #define __THROW_RUNTIME(x) abort()
60 #define __THROW_ALLOC() abort()
61 #define __THROW_DEREF(v) if(v == nullptr) abort()
62 #define __THROW_UNDEF(v,x) if(v == nullptr) abort()
75 #define UCOMMON_NAMESPACE ucommon
76 #define NAMESPACE_UCOMMON namespace ucommon {
77 #define END_NAMESPACE }
88 #ifndef _POSIX_PTHREAD_SEMANTICS
89 #define _POSIX_PTHREAD_SEMANTICS
93 #if !defined(__GNUC__) && !defined(__has_feature) && !defined(_MSC_VER)
94 #define UCOMMON_RTTI 1
97 #if __GNUC__ > 3 && defined(__GXX_RTTI)
98 #define UCOMMON_RTTI 1
101 #if defined(_MSC_VER) && defined(_CPPRTTI)
102 #define UCOMMON_RTTI 1
105 #if defined(__has_feature)
106 #if __has_feature(cxx_rtti)
107 #define UCOMMON_RTTI 1
112 #define __PROTOCOL virtual
113 template<
typename T,
typename S>
114 T protocol_cast(S *s) {
115 return dynamic_cast<T
>(s);
119 template<
typename T,
typename S>
120 T protocol_cast(S *s) {
121 return static_cast<T
>(s);
125 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
129 #if !defined(__GNUC_PREREQ__)
130 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
131 #define __GNUC_PREREQ__(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
133 #define __GNUC_PREREQ__(maj, min) 0
137 #if __GNUC_PREREQ__(3,3)
138 #define __PRINTF(x,y) __attribute__ ((format (printf, x, y)))
139 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
140 #define __MALLOC __attribute__ ((malloc))
141 #define __NORETURN __attribute__ ((__noreturn__))
144 #define __UNUSED(x) (void)x
146 #if __cplusplus >= 201103L
147 #define __ALIGNED(x) alignas(x)
150 #define __ALIGNED(x) __declspec(align(x))
152 #define __ALIGNED(x) __attribute__(align(x))
156 #if __cplusplus < 201103L
160 #define __DELETE_COPY(x) inline x(const x&);\
161 inline x& operator=(const x&)
162 #define __DELETE_DEFAULTS(x) inline x();\
165 #define __FINAL final
166 #define __OVERRIDE override
167 #define __DELETED =delete
168 #define __DELETE_COPY(x) inline x(const x&) =delete;\
169 inline x& operator=(const x&) =delete
170 #define __DELETE_DEFAULTS(x) inline x() =delete;\
174 #if __cplusplus <= 199711L && !defined(_MSC_VER)
175 #if defined(__GNUC_MINOR__) && !defined(__clang__)
176 #define nullptr __null
177 #elif !defined(__clang__) || (defined(__clang__) && defined(__linux__))
178 const class nullptr_t
182 inline operator T*()
const {
186 template<
class C,
class T>
187 inline operator T C::*()
const {
192 void operator&()
const;
199 #define __PRINTF(x, y)
200 #define __SCANF(x, y)
218 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
221 #if defined(_MSC_VER)
224 #warning "Probably won't build, need VS >= 2010 or later"
230 #if _WIN32_WINNT < 0x0600
237 #define _WIN32_WINNT 0x0600
241 #pragma warning(disable: 4251)
242 #pragma warning(disable: 4996)
243 #pragma warning(disable: 4355)
244 #pragma warning(disable: 4290)
245 #pragma warning(disable: 4291)
248 #if defined(__BORLANDC__) && !defined(__MT__)
249 #error Please enable multithreading
252 #if defined(_MSC_VER) && !defined(_MT)
253 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
258 #define WINVER _WIN32_WINNT
261 #ifndef WIN32_LEAN_AND_MEAN
262 #define WIN32_LEAN_AND_MEAN
265 #include <winsock2.h>
266 #include <ws2tcpip.h>
268 #if defined(_MSC_VER)
269 typedef int socksize_t;
270 typedef int socklen_t;
271 typedef signed long ssize_t;
274 typedef size_t sockword_t;
275 typedef size_t socksize_t;
280 #ifdef UCOMMON_STATIC
283 #define __EXPORT __declspec(dllimport)
289 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
292 #define __SHARED __EXPORT
296 typedef size_t socksize_t;
297 #define __EXPORT __attribute__ ((visibility("default")))
298 #define __LOCAL __attribute__ ((visibility("hidden")))
299 #define __SHARED __attribute__ ((visibility("default")))
306 #include <sys/stat.h>
310 #if defined(UCOMMON_WINPTHREAD) && __GNUC_PREREQ__(4, 8) && !defined(UCOMMON_SYSRUNTIME)
311 #define __MINGW_WINPTHREAD__
313 typedef size_t stacksize_t;
316 typedef DWORD pthread_t;
317 typedef DWORD pthread_key_t;
318 typedef unsigned stacksize_t;
319 typedef CRITICAL_SECTION pthread_mutex_t;
321 typedef char *caddr_t;
323 typedef SOCKET socket_t;
326 typedef struct timespec {
332 inline void sleep(
int seconds)
333 {::Sleep((seconds * 1000l));}
337 #define __SERVICE(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
338 #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
340 typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
343 inline void pthread_exit(
void *p)
344 {_endthreadex((DWORD)0);}
346 inline pthread_t pthread_self(
void)
347 {
return (pthread_t)GetCurrentThreadId();}
349 inline int pthread_mutex_init(pthread_mutex_t *mutex,
void *x)
350 {InitializeCriticalSection(mutex);
return 0;}
352 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
353 {DeleteCriticalSection(mutex);}
355 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
356 {EnterCriticalSection(mutex);}
358 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
359 {LeaveCriticalSection(mutex);}
363 #elif defined(__PTH__)
366 #include <sys/wait.h>
368 typedef size_t stacksize_t;
369 typedef int socket_t;
371 #define INVALID_SOCKET -1
372 #define INVALID_HANDLE_VALUE -1
375 #define pthread_mutex_t pth_mutex_t
376 #define pthread_cond_t pth_cond_t
377 #define pthread_t pth_t
379 inline int pthread_sigmask(
int how,
const sigset_t *
set, sigset_t *oset)
380 {
return pth_sigmask(how,
set, oset);};
382 inline void pthread_exit(
void *p)
385 inline void pthread_kill(pthread_t tid,
int sig)
386 {pth_raise(tid, sig);};
388 inline int pthread_mutex_init(pthread_mutex_t *mutex,
void *x)
389 {
return pth_mutex_init(mutex) != 0;};
391 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
394 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
395 {pth_mutex_acquire(mutex, 0,
nullptr);};
397 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
398 {pth_mutex_release(mutex);};
400 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
401 {pth_cond_await(cond, mutex,
nullptr);};
403 inline void pthread_cond_signal(pthread_cond_t *cond)
404 {pth_cond_notify(cond, FALSE);};
406 inline void pthread_cond_broadcast(pthread_cond_t *cond)
407 {pth_cond_notify(cond, TRUE);};
413 typedef size_t stacksize_t;
414 typedef int socket_t;
416 #define INVALID_SOCKET -1
417 #define INVALID_HANDLE_VALUE -1
423 typedef signed __int8 int8_t;
424 typedef unsigned __int8 uint8_t;
425 typedef signed __int16 int16_t;
426 typedef unsigned __int16 uint16_t;
427 typedef signed __int32 int32_t;
428 typedef unsigned __int32 uint32_t;
429 typedef signed __int64 int64_t;
430 typedef unsigned __int64 uint64_t;
431 typedef char *caddr_t;
434 #define snprintf(p, s, f, ...) _snprintf_s(p, s, _TRUNCATE, f, __VA_ARGS__)
435 #define vsnprintf(p, s, f, a) _vsnprintf_s(p, s, _TRUNCATE, f, a)
439 #include <sys/stat.h>
440 #include <sys/types.h>
453 typedef unsigned long timeout_t;
458 #ifndef UCOMMON_RUNTIME
464 #define ENETDOWN ((int)(WSAENETDOWN))
467 #define EINPROGRESS ((int)(WSAEINPROGRESS))
470 #define ENOPROTOOPT ((int)(WSAENOPROTOOPT))
473 #define EADDRINUSE ((int)(WSAEADDRINUSE))
475 #ifndef EADDRNOTAVAIL
476 #define EADDRNOTAVAIL ((int)(WSAEADDRNOTAVAIL))
479 #define ENETUNREACH ((int)(WSAENETUNREACH))
482 #define EHOSTUNREACH ((int)(WSAEHOSTUNREACH))
485 #define EHOSTDOWN ((int)(WSAEHOSTDOWN))
488 #define ENETRESET ((int)(WSAENETRESET))
491 #define ECONNABORTED ((int)(WSAECONNABORTED))
494 #define ECONNRESET ((int)(WSAECONNRESET))
497 #define EISCONN ((int)(WSAEISCONN))
500 #define ENOTCONN ((int)(WSAENOTCONN))
503 #define ESHUTDOWN ((int)(WSAESHUTDOWN))
506 #define ETIMEDOUT ((int)(WSAETIMEDOUT))
509 #define ECONNREFUSED ((int)(WSAECONNREFUSED))
526 #define __PROGRAM(c,v) extern "C" int main(int c, char **v)
527 #define PROGRAM_MAIN(argc, argv) extern "C" int main(int argc, char **argv)
528 #define PROGRAM_EXIT(code) return code
532 #define __SERVICE(id, c, v) void service_##id(int c, char **v)
533 #define SERVICE_MAIN(id, argc, argv) void service_##id(int argc, char **argv)
534 typedef void (*cpr_service_t)(
int argc,
char **argv);
539 #define crit(x, text) assert(x)
541 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
552 {
return ((memory) ?
new(((
void *)memory)) T :
nullptr);}
554 typedef long Integer;
555 typedef unsigned long Unsigned;
557 typedef uint8_t ubyte_t;
566 template<
class T,
class S>
567 inline T polypointer_cast(S *s)
569 #if defined(DEBUG) && defined(UCOMMON_RTTI)
572 T ptr =
dynamic_cast<T
>(s);
576 return static_cast<T
>(s);
580 template<
class T,
class S>
581 inline T polyconst_cast(S *s)
583 return const_cast<T
>(polypointer_cast<T>(s));
586 template<
class T,
class S>
587 inline T polystatic_cast(S *s)
589 return static_cast<T
>(s);
592 template<
class T,
class S>
593 inline T polydynamic_cast(S *s)
595 #if defined(UCOMMON_RTTI)
596 return dynamic_cast<T
>(s);
598 return static_cast<T
>(s);
602 template<
class T,
class S>
603 inline T& polyreference_cast(S *s)
606 return *(
static_cast<T*
>(s));
610 inline T& reference_cast(T *pointer) {
611 __THROW_DEREF(pointer);
616 inline const T immutable_cast(T p)
618 return static_cast<const T
>(p);