$treeview $search $mathjax
Stratagus
2.2.6
$projectbrief
|
$projectbrief
|
$searchbox |
_________ __ __ / _____// |_____________ _/ |______ ____ __ __ ______ \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ \ /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > \/ \/ \//_____/ \/ ______________________ ______________________ T H E W A R B E G I N S Stratagus - A free fantasy real time strategy game engine
00001 // _________ __ __ 00002 // / _____// |_____________ _/ |______ ____ __ __ ______ 00003 // \_____ \\ __\_ __ \__ \\ __\__ \ / ___\| | \/ ___/ 00004 // / \| | | | \// __ \| | / __ \_/ /_/ > | /\___ | 00005 // /_______ /|__| |__| (____ /__| (____ /\___ /|____//____ > 00006 // \/ \/ \//_____/ \/ 00007 // ______________________ ______________________ 00008 // T H E W A R B E G I N S 00009 // Stratagus - A free fantasy real time strategy game engine 00010 // 00012 // 00013 // (c) Copyright 1998-2006 by Lutz Sammer and Jimmy Salmon 00014 // 00015 // This program is free software; you can redistribute it and/or modify 00016 // it under the terms of the GNU General Public License as published by 00017 // the Free Software Foundation; only version 2 of the License. 00018 // 00019 // This program is distributed in the hope that it will be useful, 00020 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00021 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00022 // GNU General Public License for more details. 00023 // 00024 // You should have received a copy of the GNU General Public License 00025 // along with this program; if not, write to the Free Software 00026 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00027 // 02111-1307, USA. 00028 // 00029 00030 #ifndef __UTIL_H__ 00031 #define __UTIL_H__ 00032 00034 00035 /*---------------------------------------------------------------------------- 00036 -- Threads 00037 ----------------------------------------------------------------------------*/ 00038 00039 #ifndef __unix 00040 #undef NOUSER 00041 #ifndef _WIN32_WINNT 00042 #define _WIN32_WINNT 0x0400 00043 #endif 00044 #include <winsock2.h> 00045 #include <windows.h> 00046 #elif defined(__hpux) 00047 #include <sys/mpctl.h> 00048 #ifndef MPC_GETNUMSPUS_SYS 00049 #define MPC_GETNUMSPUS_SYS MPC_GETNUMSPUS 00050 #endif 00051 #else 00052 #include <unistd.h> 00053 #include <pthread.h> 00054 #endif 00055 00056 inline int get_cpu_count() { 00057 #ifndef __unix 00058 SYSTEM_INFO info; 00059 GetSystemInfo(&info); 00060 return info.dwNumberOfProcessors; 00061 #elif defined(__linux) || defined (__sun) 00062 return sysconf (_SC_NPROCESSORS_ONLN); 00063 #elif defined(__hpux) 00064 return mpctl(MPC_GETNUMSPUS_SYS, 0, 0); 00065 #else 00066 return 1; 00067 #endif 00068 } 00069 00070 00071 class CMutex 00072 { 00073 CMutex (const CMutex&); // prohibited 00074 CMutex& operator= (const CMutex&); // prohibited 00075 00076 #if !defined (__unix) 00077 CRITICAL_SECTION _mut; 00078 #else 00079 pthread_mutexattr_t _attr; 00080 pthread_mutex_t _mut; 00081 #endif 00082 public: 00083 CMutex (); 00084 ~CMutex (); 00085 00086 void Lock () 00087 { 00088 #if !defined (__unix) 00089 EnterCriticalSection (&_mut); 00090 #else 00091 pthread_mutex_lock (&_mut); 00092 #endif 00093 } 00094 00095 void UnLock () 00096 { 00097 #if !defined (__unix) 00098 LeaveCriticalSection (&_mut); 00099 #else 00100 pthread_mutex_unlock (&_mut); 00101 #endif 00102 } 00103 00104 bool TryLock () 00105 { 00106 #if !defined (__unix) 00107 return 0 != TryEnterCriticalSection (&_mut); 00108 #else 00109 return 0 == pthread_mutex_trylock (&_mut); 00110 #endif 00111 } 00112 }; 00113 00114 00115 class CThread 00116 { 00117 CThread (const CThread&); // prohibited 00118 CThread& operator= (const CThread&); // prohibited 00119 00120 bool m_bRunning; 00121 int m_dExitFlag; 00122 00123 #if !defined (__unix) 00124 unsigned long m_dThreadID; 00125 static unsigned long WINAPI threadFun (void *pThread); 00126 HANDLE m_hndlThread; 00127 #else 00128 pthread_t m_dThreadID; 00129 static void* threadFun (void *pThread); 00130 #endif 00131 00132 public: 00133 CThread () : m_bRunning (false), m_dExitFlag (0) {}; 00134 virtual ~CThread(); 00135 00136 virtual void Run () = 0; 00137 int Start (); 00138 int Wait (); 00139 void Exit (); 00140 void Terminate (); 00141 00142 bool IsRunning () const 00143 { 00144 return m_bRunning; 00145 } 00146 00147 void SetExitFlag (int dExitCode = 1) 00148 { 00149 m_dExitFlag = dExitCode; 00150 } 00151 00152 int GetExitFlag () const 00153 { 00154 return m_dExitFlag; 00155 } 00156 00157 #if !defined (__unix) 00158 static unsigned long GetThreadID () 00159 { 00160 return GetCurrentThreadId (); 00161 } 00162 #else 00163 static pthread_t GetThreadID () 00164 { 00165 return pthread_self (); 00166 } 00167 #endif 00168 00169 }; 00170 00171 00172 00173 /*---------------------------------------------------------------------------- 00174 -- Random 00175 ----------------------------------------------------------------------------*/ 00176 00177 extern unsigned SyncRandSeed; 00178 00179 extern void InitSyncRand(); 00180 extern int SyncRand(); 00181 extern int SyncRand(int max); 00182 00183 /*---------------------------------------------------------------------------- 00184 -- Math 00185 ----------------------------------------------------------------------------*/ 00186 00188 #define MyRand() rand() 00189 00190 //for 32 bit signed int 00191 extern inline int MyAbs(int x) 00192 { 00193 return (x ^ (x >> 31)) - (x >> 31); 00194 } 00195 00197 extern long isqrt(long num); 00198 00199 /*---------------------------------------------------------------------------- 00200 -- Strings 00201 ----------------------------------------------------------------------------*/ 00202 00203 #if !defined(_MSC_VER) || _MSC_VER < 1400 00204 #define _TRUNCATE ((size_t)-1) 00205 extern unsigned int strcpy_s(char *dst, size_t dstsize, const char *src); 00206 extern unsigned int strncpy_s(char *dst, size_t dstsize, const char *src, size_t count); 00207 extern unsigned int strcat_s(char *dst, size_t dstsize, const char *src); 00208 #endif 00209 00210 #ifndef HAVE_STRCASESTR 00212 extern char *strcasestr(const char *str, const char *substr); 00213 #endif // !HAVE_STRCASESTR 00214 00215 #ifndef HAVE_STRNLEN 00217 extern size_t strnlen(const char *str, size_t strsize); 00218 #endif // !HAVE_STRNLEN 00219 00220 /*---------------------------------------------------------------------------- 00221 -- Clipboard 00222 ----------------------------------------------------------------------------*/ 00223 00224 #include <string> 00225 00226 int GetClipboard(std::string &str); 00227 00228 /*---------------------------------------------------------------------------- 00229 -- UTF8 00230 ----------------------------------------------------------------------------*/ 00231 00232 int UTF8GetNext(const std::string &text, int curpos); 00233 int UTF8GetPrev(const std::string &text, int curpos); 00234 00236 00237 #endif /* __UTIL_H__ */