UCommon
ucommon/platform.h
Go to the documentation of this file.
00001 // Copyright (C) 2006-2010 David Sugar, Tycho Softworks.
00002 //
00003 // This file is part of GNU uCommon C++.
00004 //
00005 // GNU uCommon C++ is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU Lesser General Public License as published
00007 // by the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // GNU uCommon C++ is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU Lesser General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU Lesser General Public License
00016 // along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.
00017 
00027 #ifndef _UCOMMON_PLATFORM_H_
00028 #define _UCOMMON_PLATFORM_H_
00029 #define UCOMMON_ABI 5
00030 
00041 #define UCOMMON_NAMESPACE   ucommon
00042 #define NAMESPACE_UCOMMON   namespace ucommon {
00043 #define NAMESPACE_EXTERN_C  extern "C" {
00044 #define END_NAMESPACE       }
00045 #define EXTERN_C            extern "C"
00046 
00047 #ifndef _REENTRANT
00048 #define _REENTRANT 1
00049 #endif
00050 
00051 #ifndef __PTH__
00052 #ifndef _THREADSAFE
00053 #define _THREADSAFE 1
00054 #endif
00055 
00056 #ifndef _POSIX_PTHREAD_SEMANTICS
00057 #define _POSIX_PTHREAD_SEMANTICS
00058 #endif
00059 #endif
00060 
00061 #if defined(__GNUC__) && (__GNUC < 3) && !defined(_GNU_SOURCE)
00062 #define _GNU_SOURCE
00063 #endif
00064 
00065 #if __GNUC__ > 3 || (__GNUC__ == 3 && (__GNU_MINOR__ > 3))
00066 #define __PRINTF(x,y)   __attribute__ ((format (printf, x, y)))
00067 #define __SCANF(x, y) __attribute__ ((format (scanf, x, y)))
00068 #define __MALLOC      __attribute__ ((malloc))
00069 #endif
00070 
00071 #ifndef __MALLOC
00072 #define __PRINTF(x, y)
00073 #define __SCANF(x, y)
00074 #define __MALLOC
00075 #endif
00076 
00077 #ifndef DEBUG
00078 #ifndef NDEBUG
00079 #define NDEBUG
00080 #endif
00081 #endif
00082 
00083 #ifdef  DEBUG
00084 #ifdef  NDEBUG
00085 #undef  NDEBUG
00086 #endif
00087 #endif
00088 
00089 // see if we are building for or using extended stdc++ runtime library support
00090 
00091 #if defined(NEW_STDCPP) || defined(OLD_STDCPP)
00092 #define _UCOMMON_EXTENDED_
00093 #endif
00094 
00095 // see if targeting legacy Microsoft windows platform
00096 
00097 #if defined(_MSC_VER) || defined(WIN32) || defined(_WIN32)
00098 #define _MSWINDOWS_
00099 
00100 #if defined(_M_X64) || defined(_M_ARM)
00101 #define _MSCONDITIONALS_
00102 #ifndef _WIN32_WINNT    0x0600
00103 #define _WIN32_WINNT
00104 #endif
00105 #endif
00106 
00107 //#if defined(_WIN32_WINNT) && _WIN32_WINNT < 0x0501
00108 //#undef    _WIN32_WINNT
00109 //#define   _WIN32_WINNT 0x0501
00110 //#endif
00111 
00112 //#ifndef _WIN32_WINNT
00113 //#define   _WIN32_WINNT 0x0501
00114 //#endif
00115 
00116 #pragma warning(disable: 4251)
00117 #pragma warning(disable: 4996)
00118 #pragma warning(disable: 4355)
00119 #pragma warning(disable: 4290)
00120 #pragma warning(disable: 4291)
00121 
00122 #if defined(__BORLANDC__) && !defined(__MT__)
00123 #error Please enable multithreading
00124 #endif
00125 
00126 #if defined(_MSC_VER) && !defined(_MT)
00127 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
00128 #endif
00129 
00130 // Require for compiling with critical sections.
00131 #ifndef _WIN32_WINNT
00132 #define _WIN32_WINNT 0x0501
00133 #endif
00134 
00135 // Make sure we're consistent with _WIN32_WINNT
00136 #ifndef WINVER
00137 #define WINVER _WIN32_WINNT
00138 #endif
00139 
00140 #ifndef WIN32_LEAN_AND_MEAN
00141 #define WIN32_LEAN_AND_MEAN
00142 #endif
00143 
00144 #include <winsock2.h>
00145 #include <ws2tcpip.h>
00146 
00147 #if defined(_MSC_VER)
00148 typedef signed long ssize_t;
00149 typedef int pid_t;
00150 #endif
00151 
00152 #include <process.h>
00153 #ifndef __EXPORT
00154 #ifdef  UCOMMON_STATIC
00155 #define __EXPORT
00156 #else
00157 #define __EXPORT    __declspec(dllimport)
00158 #endif
00159 #endif
00160 #define __LOCAL
00161 
00162 // if runtime mode then non-runtime libraries are static on windows...
00163 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
00164 #define __SHARED
00165 #else
00166 #define __SHARED __EXPORT
00167 #endif
00168 
00169 #elif UCOMMON_VISIBILITY > 0
00170 #define __EXPORT    __attribute__ ((visibility("default")))
00171 #define __LOCAL     __attribute__ ((visibility("hidden")))
00172 #define __SHARED    __attribute__ ((visibility("default")))
00173 #else
00174 #define __EXPORT
00175 #define __LOCAL
00176 #define __SHARED
00177 #endif
00178 
00179 #ifdef  _MSWINDOWS_
00180 
00181 #define _UWIN
00182 
00183 #include <sys/stat.h>
00184 #include <io.h>
00185 
00186 typedef DWORD pthread_t;
00187 typedef CRITICAL_SECTION pthread_mutex_t;
00188 typedef char *caddr_t;
00189 typedef HANDLE fd_t;
00190 typedef SOCKET socket_t;
00191 
00192 typedef struct timespec {
00193     time_t tv_sec;
00194     long  tv_nsec;
00195 } timespec_t;
00196 
00197 extern "C" {
00198 
00199     #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
00200 
00201     typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
00202 
00203     inline void sleep(int seconds)
00204         {::Sleep((seconds * 1000l));};
00205 
00206     inline void pthread_exit(void *p)
00207         {_endthreadex((DWORD)p);};
00208 
00209     inline pthread_t pthread_self(void)
00210         {return (pthread_t)GetCurrentThreadId();};
00211 
00212     inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00213         {InitializeCriticalSection(mutex); return 0;};
00214 
00215     inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00216         {DeleteCriticalSection(mutex);};
00217 
00218     inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00219         {EnterCriticalSection(mutex);};
00220 
00221     inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00222         {LeaveCriticalSection(mutex);};
00223 
00224     inline char *strdup(const char *s)
00225         {return _strdup(s);};
00226 
00227     inline int stricmp(const char *s1, const char *s2)
00228         {return _stricmp(s1, s2);};
00229 
00230     inline int strnicmp(const char *s1, const char *s2, size_t l)
00231         {return _strnicmp(s1, s2, l);};
00232 };
00233 
00234 #elif defined(__PTH__)
00235 
00236 #include <pth.h>
00237 #include <sys/wait.h>
00238 
00239 typedef int socket_t;
00240 typedef int fd_t;
00241 #define INVALID_SOCKET -1
00242 #define INVALID_HANDLE_VALUE -1
00243 #include <signal.h>
00244 
00245 #define pthread_mutex_t pth_mutex_t
00246 #define pthread_cond_t pth_cond_t
00247 #define pthread_t pth_t
00248 
00249 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
00250     {return pth_sigmask(how, set, oset);};
00251 
00252 inline void pthread_exit(void *p)
00253     {pth_exit(p);};
00254 
00255 inline void pthread_kill(pthread_t tid, int sig)
00256     {pth_raise(tid, sig);};
00257 
00258 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00259     {return pth_mutex_init(mutex) != 0;};
00260 
00261 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00262     {};
00263 
00264 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00265     {pth_mutex_acquire(mutex, 0, NULL);};
00266 
00267 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00268     {pth_mutex_release(mutex);};
00269 
00270 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00271     {pth_cond_await(cond, mutex, NULL);};
00272 
00273 inline void pthread_cond_signal(pthread_cond_t *cond)
00274     {pth_cond_notify(cond, FALSE);};
00275 
00276 inline void pthread_cond_broadcast(pthread_cond_t *cond)
00277     {pth_cond_notify(cond, TRUE);};
00278 
00279 #else
00280 
00281 #include <pthread.h>
00282 
00283 typedef int socket_t;
00284 typedef int fd_t;
00285 #define INVALID_SOCKET -1
00286 #define INVALID_HANDLE_VALUE -1
00287 #include <signal.h>
00288 
00289 #endif
00290 
00291 #ifdef _MSC_VER
00292 typedef signed __int8 int8_t;
00293 typedef unsigned __int8 uint8_t;
00294 typedef signed __int16 int16_t;
00295 typedef unsigned __int16 uint16_t;
00296 typedef signed __int32 int32_t;
00297 typedef unsigned __int32 uint32_t;
00298 typedef signed __int64 int64_t;
00299 typedef unsigned __int64 uint64_t;
00300 typedef char *caddr_t;
00301 
00302 #include <stdio.h>
00303 #define snprintf _snprintf
00304 #define vsnprintf _vsnprintf
00305 
00306 #else
00307 
00308 #include <sys/stat.h>
00309 #include <sys/types.h>
00310 #include <stdint.h>
00311 #include <unistd.h>
00312 
00313 #endif
00314 
00315 #ifndef _GNU_SOURCE
00316 typedef void (*sighandler_t)(int);  
00317 #endif
00318 typedef unsigned long timeout_t;    
00320 #include <stdlib.h>
00321 #include <errno.h>
00322 
00323 #ifdef  _MSWINDOWS_
00324 #ifndef ENETDOWN
00325 #define ENETDOWN        ((int)(WSAENETDOWN))
00326 #endif
00327 #ifndef EINPROGRESS
00328 #define EINPROGRESS     ((int)(WSAEINPROGRESS))
00329 #endif
00330 #ifndef ENOPROTOOPT
00331 #define ENOPROTOOPT     ((int)(WSAENOPROTOOPT))
00332 #endif
00333 #ifndef EADDRINUSE
00334 #define EADDRINUSE      ((int)(WSAEADDRINUSE))
00335 #endif
00336 #ifndef EADDRNOTAVAIL
00337 #define EADDRNOTAVAIL   ((int)(WSAEADDRNOTAVAIL))
00338 #endif
00339 #ifndef ENETUNREACH
00340 #define ENETUNREACH     ((int)(WSAENETUNREACH))
00341 #endif
00342 #ifndef EHOSTUNREACH
00343 #define EHOSTUNREACH    ((int)(WSAEHOSTUNREACH))
00344 #endif
00345 #ifndef EHOSTDOWN
00346 #define EHOSTDOWN       ((int)(WSAEHOSTDOWN))
00347 #endif
00348 #ifndef ENETRESET
00349 #define ENETRESET       ((int)(WSAENETRESET))
00350 #endif
00351 #ifndef ECONNABORTED
00352 #define ECONNABORTED    ((int)(WSAECONNABORTED))
00353 #endif
00354 #ifndef ECONNRESET
00355 #define ECONNRESET      ((int)(WSAECONNRESET))
00356 #endif
00357 #ifndef EISCONN
00358 #define EISCONN         ((int)(WSAEISCONN))
00359 #endif
00360 #ifndef ENOTCONN
00361 #define ENOTCONN        ((int)(WSAENOTCONN))
00362 #endif
00363 #ifndef ESHUTDOWN
00364 #define ESHUTDOWN       ((int)(WSAESHUTDOWN))
00365 #endif
00366 #ifndef ETIMEDOUT
00367 #define ETIMEDOUT       ((int)(WSAETIMEDOUT))
00368 #endif
00369 #ifndef ECONNREFUSED
00370 #define ECONNREFUSED    ((int)(WSAECONNREFUSED))
00371 #endif
00372 #endif
00373 
00374 #ifndef DEBUG
00375 #ifndef NDEBUG
00376 #define NDEBUG
00377 #endif
00378 #endif
00379 
00380 #ifdef  DEBUG
00381 #ifdef  NDEBUG
00382 #undef  NDEBUG
00383 #endif
00384 #endif
00385 
00386 #ifndef PROGRAM_MAIN
00387 #define PROGRAM_MAIN(argc, argv)    extern "C" int main(int argc, char **argv)
00388 #define PROGRAM_EXIT(code)          return code
00389 #endif
00390 
00391 #ifndef SERVICE_MAIN
00392 #define SERVICE_MAIN(id, argc, argv)    void service_##id(int argc, char **argv)
00393 typedef void (*cpr_service_t)(int argc, char **argv);
00394 #endif
00395 
00396 #include <assert.h>
00397 #ifdef  DEBUG
00398 #define crit(x, text)   assert(x)
00399 #else
00400 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
00401 #endif
00402 
00409 template<class T>
00410 inline T *init(T *memory)
00411     {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
00412 
00413 typedef long Integer;
00414 typedef unsigned long Unsigned;
00415 typedef double Real;
00416 
00417 #endif