29 #pragma clang diagnostic ignored "-Wpadded"
30 #pragma clang diagnostic ignored "-Wswitch-enum"
31 #pragma clang diagnostic ignored "-Wmissing-noreturn"
32 #pragma clang diagnostic ignored "-Wold-style-cast"
33 #pragma clang diagnostic ignored "-Wcast-qual"
34 #pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
38 #pragma GCC diagnostic ignored "-Wunused-result"
39 #pragma GCC diagnostic ignored "-Wold-style-cast"
40 #pragma GCC diagnostic ignored "-Wcast-qual"
41 #pragma GCC diagnostic ignored "-Wzero-as-null-pointer-constant"
46 #if __cplusplus >= 201103L
50 #if defined(sun) && defined(unix)
54 #ifndef _UCOMMON_PLATFORM_H_
55 #define _UCOMMON_PLATFORM_H_
58 #ifndef UCOMMON_SYSRUNTIME
62 #define _UCOMMON_EXTENDED_
64 #define __THROW_SIZE(x) throw std::length_error(x)
65 #define __THROW_RANGE(x) throw std::out_of_range(x)
66 #define __THROW_RUNTIME(x) throw std::runtime_error(x)
67 #define __THROW_ALLOC() throw std::bad_alloc()
68 #define __THROW_DEREF(v) if(v == nullptr) \
69 throw std::runtime_error("Dereference NULL")
70 #define __THROW_UNDEF(v,x) if(v == nullptr) throw std::runtime_error(x)
72 #define __THROW_RANGE(x) abort()
73 #define __THROW_SIZE(x) abort()
74 #define __THROW_RUNTIME(x) abort()
75 #define __THROW_ALLOC() abort()
76 #define __THROW_DEREF(v) if(v == nullptr) abort()
77 #define __THROW_UNDEF(v,x) if(v == nullptr) abort()
90 #define UCOMMON_NAMESPACE ucommon
91 #define NAMESPACE_UCOMMON namespace ucommon {
92 #define END_NAMESPACE }
100 #define _THREADSAFE 1
103 #ifndef _POSIX_PTHREAD_SEMANTICS
104 #define _POSIX_PTHREAD_SEMANTICS
108 #if !defined(__GNUC__) && !defined(__has_feature) && !defined(_MSC_VER)
109 #define UCOMMON_RTTI 1
112 #if __GNUC__ > 3 && defined(__GXX_RTTI)
113 #define UCOMMON_RTTI 1
116 #if defined(_MSC_VER) && defined(_CPPRTTI)
117 #define UCOMMON_RTTI 1
120 #if defined(__has_feature)
121 #if __has_feature(cxx_rtti)
122 #define UCOMMON_RTTI 1
127 #define __PROTOCOL virtual
128 template<
typename T,
typename S>
129 T protocol_cast(S *s) {
130 return dynamic_cast<T
>(s);
134 template<
typename T,
typename S>
135 T protocol_cast(S *s) {
136 return static_cast<T
>(s);
140 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
144 #if !defined(__GNUC_PREREQ__)
145 #if defined(__GNUC__) && defined(__GNUC_MINOR__)
146 #define __GNUC_PREREQ__(maj, min) ((__GNUC__ << 16) + __GNUC_MINOR__ >= ((maj) << 16) + (min))
148 #define __GNUC_PREREQ__(maj, min) 0
152 #if __GNUC_PREREQ__(3,3)
153 #define __PRINTF(x,y) __attribute__ ((format (printf, x, y)))
154 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
155 #define __MALLOC __attribute__ ((malloc))
156 #define __NORETURN __attribute__ ((__noreturn__))
159 #define __UNUSED(x) (void)x
161 #if __cplusplus >= 201103L
162 #define __ALIGNED(x) alignas(x)
165 #define __ALIGNED(x) __declspec(align(x))
167 #define __ALIGNED(x) __attribute__(align(x))
171 #if __cplusplus < 201103L
175 #define __DELETE_COPY(x) inline x(const x&);\
176 inline x& operator=(const x&)
177 #define __DELETE_DEFAULTS(x) inline x();\
180 #define __FINAL final
181 #define __OVERRIDE override
182 #define __DELETED =delete
183 #define __DELETE_COPY(x) inline x(const x&) =delete;\
184 inline x& operator=(const x&) =delete
185 #define __DELETE_DEFAULTS(x) inline x() =delete;\
189 #if __cplusplus <= 199711L && !defined(_MSC_VER)
190 #if defined(__GNUC_MINOR__) && !defined(__clang__)
191 #define nullptr __null
192 #elif !defined(__clang__) || (defined(__clang__) && defined(__linux__))
193 const class nullptr_t
197 inline operator T*()
const {
201 template<
class C,
class T>
202 inline operator T C::*()
const {
207 void operator&()
const;
214 #define __PRINTF(x, y)
215 #define __SCANF(x, y)
233 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
236 #if defined(_MSC_VER)
239 #warning "Probably won't build, need VS >= 2010 or later"
245 #if _WIN32_WINNT < 0x0600
252 #define _WIN32_WINNT 0x0600
256 #pragma warning(disable: 4251)
257 #pragma warning(disable: 4996)
258 #pragma warning(disable: 4355)
259 #pragma warning(disable: 4290)
260 #pragma warning(disable: 4291)
263 #if defined(__BORLANDC__) && !defined(__MT__)
264 #error Please enable multithreading
267 #if defined(_MSC_VER) && !defined(_MT)
268 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
273 #define WINVER _WIN32_WINNT
276 #ifndef WIN32_LEAN_AND_MEAN
277 #define WIN32_LEAN_AND_MEAN
280 #include <winsock2.h>
281 #include <ws2tcpip.h>
283 #if defined(_MSC_VER)
284 typedef int socksize_t;
285 typedef int socklen_t;
286 typedef signed long ssize_t;
289 typedef size_t sockword_t;
290 typedef size_t socksize_t;
295 #ifdef UCOMMON_STATIC
298 #define __EXPORT __declspec(dllimport)
304 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
307 #define __SHARED __declspec(dllimport)
311 typedef size_t socksize_t;
312 #define __EXPORT __attribute__ ((visibility("default")))
313 #define __LOCAL __attribute__ ((visibility("hidden")))
314 #define __SHARED __attribute__ ((visibility("default")))
321 #include <sys/stat.h>
325 #if defined(UCOMMON_WINPTHREAD) && __GNUC_PREREQ__(4, 8) && !defined(UCOMMON_SYSRUNTIME)
326 #define __MINGW_WINPTHREAD__
328 typedef size_t stacksize_t;
331 typedef DWORD pthread_t;
332 typedef DWORD pthread_key_t;
333 typedef unsigned stacksize_t;
334 typedef CRITICAL_SECTION pthread_mutex_t;
336 typedef char *caddr_t;
338 typedef SOCKET socket_t;
340 #if defined(_MSC_VER) && defined(_CRT_NO_TIME_T)
341 typedef struct timespec {
347 inline void sleep(
int seconds)
348 {::Sleep((seconds * 1000l));}
352 #define __SERVICE(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
353 #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
355 typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
358 inline void pthread_exit(
void *p)
359 {_endthreadex((DWORD)0);}
361 inline pthread_t pthread_self(
void)
362 {
return (pthread_t)GetCurrentThreadId();}
364 inline int pthread_mutex_init(pthread_mutex_t *mutex,
void *x)
365 {InitializeCriticalSection(mutex);
return 0;}
367 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
368 {DeleteCriticalSection(mutex);}
370 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
371 {EnterCriticalSection(mutex);}
373 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
374 {LeaveCriticalSection(mutex);}
378 #elif defined(__PTH__)
381 #include <sys/wait.h>
383 typedef size_t stacksize_t;
384 typedef int socket_t;
386 #define INVALID_SOCKET -1
387 #define INVALID_HANDLE_VALUE -1
390 #define pthread_mutex_t pth_mutex_t
391 #define pthread_cond_t pth_cond_t
392 #define pthread_t pth_t
394 inline int pthread_sigmask(
int how,
const sigset_t *set, sigset_t *oset)
395 {
return pth_sigmask(how, set, oset);};
397 inline void pthread_exit(
void *p)
400 inline void pthread_kill(pthread_t tid,
int sig)
401 {pth_raise(tid, sig);};
403 inline int pthread_mutex_init(pthread_mutex_t *mutex,
void *x)
404 {
return pth_mutex_init(mutex) != 0;};
406 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
409 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
410 {pth_mutex_acquire(mutex, 0,
nullptr);};
412 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
413 {pth_mutex_release(mutex);};
415 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
416 {pth_cond_await(cond, mutex,
nullptr);};
418 inline void pthread_cond_signal(pthread_cond_t *cond)
419 {pth_cond_notify(cond, FALSE);};
421 inline void pthread_cond_broadcast(pthread_cond_t *cond)
422 {pth_cond_notify(cond, TRUE);};
428 typedef size_t stacksize_t;
429 typedef int socket_t;
431 #define INVALID_SOCKET -1
432 #define INVALID_HANDLE_VALUE -1
438 typedef signed __int8 int8_t;
439 typedef unsigned __int8 uint8_t;
440 typedef signed __int16 int16_t;
441 typedef unsigned __int16 uint16_t;
442 typedef signed __int32 int32_t;
443 typedef unsigned __int32 uint32_t;
444 typedef signed __int64 int64_t;
445 typedef unsigned __int64 uint64_t;
446 typedef char *caddr_t;
449 #define snprintf(p, s, f, ...) _snprintf_s(p, s, _TRUNCATE, f, __VA_ARGS__)
450 #define vsnprintf(p, s, f, a) _vsnprintf_s(p, s, _TRUNCATE, f, a)
454 #include <sys/stat.h>
455 #include <sys/types.h>
468 typedef unsigned long timeout_t;
473 #ifndef UCOMMON_RUNTIME
479 #define ENETDOWN ((int)(WSAENETDOWN))
482 #define EINPROGRESS ((int)(WSAEINPROGRESS))
485 #define ENOPROTOOPT ((int)(WSAENOPROTOOPT))
488 #define EADDRINUSE ((int)(WSAEADDRINUSE))
490 #ifndef EADDRNOTAVAIL
491 #define EADDRNOTAVAIL ((int)(WSAEADDRNOTAVAIL))
494 #define ENETUNREACH ((int)(WSAENETUNREACH))
497 #define EHOSTUNREACH ((int)(WSAEHOSTUNREACH))
500 #define EHOSTDOWN ((int)(WSAEHOSTDOWN))
503 #define ENETRESET ((int)(WSAENETRESET))
506 #define ECONNABORTED ((int)(WSAECONNABORTED))
509 #define ECONNRESET ((int)(WSAECONNRESET))
512 #define EISCONN ((int)(WSAEISCONN))
515 #define ENOTCONN ((int)(WSAENOTCONN))
518 #define ESHUTDOWN ((int)(WSAESHUTDOWN))
521 #define ETIMEDOUT ((int)(WSAETIMEDOUT))
524 #define ECONNREFUSED ((int)(WSAECONNREFUSED))
541 #define __PROGRAM(c,v) extern "C" int main(int c, char **v)
542 #define PROGRAM_MAIN(argc, argv) extern "C" int main(int argc, char **argv)
543 #define PROGRAM_EXIT(code) return code
547 #define __SERVICE(id, c, v) void service_##id(int c, char **v)
548 #define SERVICE_MAIN(id, argc, argv) void service_##id(int argc, char **argv)
549 typedef void (*cpr_service_t)(
int argc,
char **argv);
554 #define crit(x, text) assert(x)
556 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
567 {
return ((memory) ?
new(((
void *)memory)) T :
nullptr);}
569 typedef long Integer;
570 typedef unsigned long Unsigned;
572 typedef uint8_t ubyte_t;
581 template<
class T,
class S>
582 inline T polypointer_cast(S *s)
584 #if defined(DEBUG) && defined(UCOMMON_RTTI)
587 T ptr =
dynamic_cast<T
>(s);
591 return static_cast<T
>(s);
595 template<
class T,
class S>
596 inline T polyconst_cast(S *s)
598 return const_cast<T
>(polypointer_cast<T>(s));
601 template<
class T,
class S>
602 inline T polystatic_cast(S *s)
604 return static_cast<T
>(s);
607 template<
class T,
class S>
608 inline T polydynamic_cast(S *s)
610 #if defined(UCOMMON_RTTI)
611 return dynamic_cast<T
>(s);
613 return static_cast<T
>(s);
617 template<
class T,
class S>
618 inline T& polyreference_cast(S *s)
621 return *(
static_cast<T*
>(s));
625 inline T& reference_cast(T *pointer) {
626 __THROW_DEREF(pointer);
631 inline const T immutable_cast(T p)
633 return static_cast<const T
>(p);
void(* sighandler_t)(int)
Convenient typedef for signal handlers.
void strfree(char *str)
Matching function for strdup().
T * init(T *memory)
Template function to initialize memory by invoking default constructor.