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 6
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
00103 #define _WIN32_WINNT    0x0600
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 #ifdef  _MSC_VER
00117 #pragma warning(disable: 4251)
00118 #pragma warning(disable: 4996)
00119 #pragma warning(disable: 4355)
00120 #pragma warning(disable: 4290)
00121 #pragma warning(disable: 4291)
00122 #endif
00123 
00124 #if defined(__BORLANDC__) && !defined(__MT__)
00125 #error Please enable multithreading
00126 #endif
00127 
00128 #if defined(_MSC_VER) && !defined(_MT)
00129 #error Please enable multithreading (Project -> Settings -> C/C++ -> Code Generation -> Use Runtime Library)
00130 #endif
00131 
00132 // Require for compiling with critical sections.
00133 #ifndef _WIN32_WINNT
00134 #define _WIN32_WINNT 0x0501
00135 #endif
00136 
00137 // Make sure we're consistent with _WIN32_WINNT
00138 #ifndef WINVER
00139 #define WINVER _WIN32_WINNT
00140 #endif
00141 
00142 #ifndef WIN32_LEAN_AND_MEAN
00143 #define WIN32_LEAN_AND_MEAN
00144 #endif
00145 
00146 #include <winsock2.h>
00147 #include <ws2tcpip.h>
00148 
00149 #if defined(_MSC_VER)
00150 typedef signed long ssize_t;
00151 typedef int pid_t;
00152 #endif
00153 
00154 #include <process.h>
00155 #ifndef __EXPORT
00156 #ifdef  UCOMMON_STATIC
00157 #define __EXPORT
00158 #else
00159 #define __EXPORT    __declspec(dllimport)
00160 #endif
00161 #endif
00162 #define __LOCAL
00163 
00164 // if runtime mode then non-runtime libraries are static on windows...
00165 #if defined(UCOMMON_RUNTIME) || defined(UCOMMON_STATIC)
00166 #define __SHARED
00167 #else
00168 #define __SHARED __EXPORT
00169 #endif
00170 
00171 #elif UCOMMON_VISIBILITY > 0
00172 #define __EXPORT    __attribute__ ((visibility("default")))
00173 #define __LOCAL     __attribute__ ((visibility("hidden")))
00174 #define __SHARED    __attribute__ ((visibility("default")))
00175 #else
00176 #define __EXPORT
00177 #define __LOCAL
00178 #define __SHARED
00179 #endif
00180 
00181 #ifdef  _MSWINDOWS_
00182 
00183 #define _UWIN
00184 
00185 #include <sys/stat.h>
00186 #include <io.h>
00187 
00188 typedef DWORD pthread_t;
00189 typedef CRITICAL_SECTION pthread_mutex_t;
00190 typedef char *caddr_t;
00191 typedef HANDLE fd_t;
00192 typedef SOCKET socket_t;
00193 
00194 #ifdef  _MSC_VER
00195 typedef struct timespec {
00196     time_t tv_sec;
00197     long  tv_nsec;
00198 } timespec_t;
00199 #endif
00200 
00201 extern "C" {
00202 
00203     #define SERVICE_MAIN(id, argc, argv) void WINAPI service_##id(DWORD argc, LPSTR *argv)
00204 
00205     typedef LPSERVICE_MAIN_FUNCTION cpr_service_t;
00206 
00207     inline void sleep(int seconds)
00208         {::Sleep((seconds * 1000l));}
00209 
00210     inline void pthread_exit(void *p)
00211         {_endthreadex((DWORD)0);}
00212 
00213     inline pthread_t pthread_self(void)
00214         {return (pthread_t)GetCurrentThreadId();}
00215 
00216     inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00217         {InitializeCriticalSection(mutex); return 0;}
00218 
00219     inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00220         {DeleteCriticalSection(mutex);}
00221 
00222     inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00223         {EnterCriticalSection(mutex);}
00224 
00225     inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00226         {LeaveCriticalSection(mutex);}
00227 
00228     inline char *strdup(const char *s)
00229         {return _strdup(s);}
00230 
00231     inline int stricmp(const char *s1, const char *s2)
00232         {return _stricmp(s1, s2);}
00233 
00234     inline int strnicmp(const char *s1, const char *s2, size_t l)
00235         {return _strnicmp(s1, s2, l);}
00236 }
00237 
00238 #elif defined(__PTH__)
00239 
00240 #include <pth.h>
00241 #include <sys/wait.h>
00242 
00243 typedef int socket_t;
00244 typedef int fd_t;
00245 #define INVALID_SOCKET -1
00246 #define INVALID_HANDLE_VALUE -1
00247 #include <signal.h>
00248 
00249 #define pthread_mutex_t pth_mutex_t
00250 #define pthread_cond_t pth_cond_t
00251 #define pthread_t pth_t
00252 
00253 inline int pthread_sigmask(int how, const sigset_t *set, sigset_t *oset)
00254     {return pth_sigmask(how, set, oset);};
00255 
00256 inline void pthread_exit(void *p)
00257     {pth_exit(p);};
00258 
00259 inline void pthread_kill(pthread_t tid, int sig)
00260     {pth_raise(tid, sig);};
00261 
00262 inline int pthread_mutex_init(pthread_mutex_t *mutex, void *x)
00263     {return pth_mutex_init(mutex) != 0;};
00264 
00265 inline void pthread_mutex_destroy(pthread_mutex_t *mutex)
00266     {};
00267 
00268 inline void pthread_mutex_lock(pthread_mutex_t *mutex)
00269     {pth_mutex_acquire(mutex, 0, NULL);};
00270 
00271 inline void pthread_mutex_unlock(pthread_mutex_t *mutex)
00272     {pth_mutex_release(mutex);};
00273 
00274 inline void pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
00275     {pth_cond_await(cond, mutex, NULL);};
00276 
00277 inline void pthread_cond_signal(pthread_cond_t *cond)
00278     {pth_cond_notify(cond, FALSE);};
00279 
00280 inline void pthread_cond_broadcast(pthread_cond_t *cond)
00281     {pth_cond_notify(cond, TRUE);};
00282 
00283 #else
00284 
00285 #include <pthread.h>
00286 
00287 typedef int socket_t;
00288 typedef int fd_t;
00289 #define INVALID_SOCKET -1
00290 #define INVALID_HANDLE_VALUE -1
00291 #include <signal.h>
00292 
00293 #endif
00294 
00295 #ifdef _MSC_VER
00296 typedef signed __int8 int8_t;
00297 typedef unsigned __int8 uint8_t;
00298 typedef signed __int16 int16_t;
00299 typedef unsigned __int16 uint16_t;
00300 typedef signed __int32 int32_t;
00301 typedef unsigned __int32 uint32_t;
00302 typedef signed __int64 int64_t;
00303 typedef unsigned __int64 uint64_t;
00304 typedef char *caddr_t;
00305 
00306 #include <stdio.h>
00307 #define snprintf _snprintf
00308 #define vsnprintf _vsnprintf
00309 
00310 #else
00311 
00312 #include <sys/stat.h>
00313 #include <sys/types.h>
00314 #include <stdint.h>
00315 #include <unistd.h>
00316 #include <stdio.h>
00317 
00318 #endif
00319 
00320 #undef  getchar
00321 #undef  putchar
00322 
00323 #ifndef _GNU_SOURCE
00324 typedef void (*sighandler_t)(int);  
00325 #endif
00326 typedef unsigned long timeout_t;    
00328 #include <stdlib.h>
00329 #include <ctype.h>
00330 #include <limits.h>
00331 #include <errno.h>
00332 
00333 #ifdef  _MSWINDOWS_
00334 #ifndef ENETDOWN
00335 #define ENETDOWN        ((int)(WSAENETDOWN))
00336 #endif
00337 #ifndef EINPROGRESS
00338 #define EINPROGRESS     ((int)(WSAEINPROGRESS))
00339 #endif
00340 #ifndef ENOPROTOOPT
00341 #define ENOPROTOOPT     ((int)(WSAENOPROTOOPT))
00342 #endif
00343 #ifndef EADDRINUSE
00344 #define EADDRINUSE      ((int)(WSAEADDRINUSE))
00345 #endif
00346 #ifndef EADDRNOTAVAIL
00347 #define EADDRNOTAVAIL   ((int)(WSAEADDRNOTAVAIL))
00348 #endif
00349 #ifndef ENETUNREACH
00350 #define ENETUNREACH     ((int)(WSAENETUNREACH))
00351 #endif
00352 #ifndef EHOSTUNREACH
00353 #define EHOSTUNREACH    ((int)(WSAEHOSTUNREACH))
00354 #endif
00355 #ifndef EHOSTDOWN
00356 #define EHOSTDOWN       ((int)(WSAEHOSTDOWN))
00357 #endif
00358 #ifndef ENETRESET
00359 #define ENETRESET       ((int)(WSAENETRESET))
00360 #endif
00361 #ifndef ECONNABORTED
00362 #define ECONNABORTED    ((int)(WSAECONNABORTED))
00363 #endif
00364 #ifndef ECONNRESET
00365 #define ECONNRESET      ((int)(WSAECONNRESET))
00366 #endif
00367 #ifndef EISCONN
00368 #define EISCONN         ((int)(WSAEISCONN))
00369 #endif
00370 #ifndef ENOTCONN
00371 #define ENOTCONN        ((int)(WSAENOTCONN))
00372 #endif
00373 #ifndef ESHUTDOWN
00374 #define ESHUTDOWN       ((int)(WSAESHUTDOWN))
00375 #endif
00376 #ifndef ETIMEDOUT
00377 #define ETIMEDOUT       ((int)(WSAETIMEDOUT))
00378 #endif
00379 #ifndef ECONNREFUSED
00380 #define ECONNREFUSED    ((int)(WSAECONNREFUSED))
00381 #endif
00382 #endif
00383 
00384 #ifndef DEBUG
00385 #ifndef NDEBUG
00386 #define NDEBUG
00387 #endif
00388 #endif
00389 
00390 #ifdef  DEBUG
00391 #ifdef  NDEBUG
00392 #undef  NDEBUG
00393 #endif
00394 #endif
00395 
00396 #ifndef PROGRAM_MAIN
00397 #define PROGRAM_MAIN(argc, argv)    extern "C" int main(int argc, char **argv)
00398 #define PROGRAM_EXIT(code)          return code
00399 #endif
00400 
00401 #ifndef SERVICE_MAIN
00402 #define SERVICE_MAIN(id, argc, argv)    void service_##id(int argc, char **argv)
00403 typedef void (*cpr_service_t)(int argc, char **argv);
00404 #endif
00405 
00406 #include <assert.h>
00407 #ifdef  DEBUG
00408 #define crit(x, text)   assert(x)
00409 #else
00410 #define crit(x, text) if(!(x)) cpr_runtime_error(text)
00411 #endif
00412 
00419 template<class T>
00420 inline T *init(T *memory)
00421     {return ((memory) ? new(((caddr_t)memory)) T : NULL);}
00422 
00423 typedef long Integer;
00424 typedef unsigned long Unsigned;
00425 typedef double Real;
00426 
00431 inline void strfree(char *str)
00432     {::free(str);}
00433 
00434 #endif

Generated on 14 Aug 2013 for UCommon by  doxygen 1.4.7