PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: System.h * 00003 * 00004 * Copyright (C) 2002-2011 The PixelLight Team (http://www.pixellight.org/) 00005 * 00006 * This file is part of PixelLight. 00007 * 00008 * PixelLight is free software: you can redistribute it and/or modify 00009 * it under the terms of the GNU Lesser General Public License as published by 00010 * the Free Software Foundation, either version 3 of the License, or 00011 * (at your option) any later version. 00012 * 00013 * PixelLight is distributed in the hope that it will be useful, 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00016 * GNU Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public License 00019 * along with PixelLight. If not, see <http://www.gnu.org/licenses/>. 00020 \*********************************************************/ 00021 00022 00023 #ifndef __PLCORE_SYSTEM_H__ 00024 #define __PLCORE_SYSTEM_H__ 00025 #pragma once 00026 00027 00028 //[-------------------------------------------------------] 00029 //[ Includes ] 00030 //[-------------------------------------------------------] 00031 #include "PLCore/Tools/Time.h" 00032 #include "PLCore/Core/Singleton.h" 00033 00034 00035 //[-------------------------------------------------------] 00036 //[ Namespace ] 00037 //[-------------------------------------------------------] 00038 namespace PLCore { 00039 00040 00041 //[-------------------------------------------------------] 00042 //[ Forward declarations ] 00043 //[-------------------------------------------------------] 00044 class Thread; 00045 class Console; 00046 class SystemImpl; 00047 00048 00049 //[-------------------------------------------------------] 00050 //[ Classes ] 00051 //[-------------------------------------------------------] 00052 /** 00053 * @brief 00054 * Provides access to system and platform functions 00055 * 00056 * @note 00057 * - Implementation of the bridge design pattern, this class is the abstraction 00058 */ 00059 class System : public Singleton<System> { 00060 00061 00062 //[-------------------------------------------------------] 00063 //[ Friends ] 00064 //[-------------------------------------------------------] 00065 friend class Singleton<System>; 00066 00067 00068 //[-------------------------------------------------------] 00069 //[ Public static PLCore::Singleton functions ] 00070 //[-------------------------------------------------------] 00071 // This solution enhances the compatibility with legacy compilers like GCC 4.2.1 used on Mac OS X 10.6 00072 // -> The C++11 feature "extern template" (C++11, see e.g. http://www2.research.att.com/~bs/C++0xFAQ.html#extern-templates) can only be used on modern compilers like GCC 4.6 00073 // -> We can't break legacy compiler support, especially when only the singletons are responsible for the break 00074 // -> See PLCore::Singleton for more details about singletons 00075 public: 00076 static PLCORE_API System *GetInstance(); 00077 static PLCORE_API bool HasInstance(); 00078 00079 00080 //[-------------------------------------------------------] 00081 //[ Public functions ] 00082 //[-------------------------------------------------------] 00083 public: 00084 /** 00085 * @brief 00086 * Returns relevant system information in one string 00087 * 00088 * @return 00089 * Information string 00090 * 00091 * @remarks 00092 * The returned information are 00093 * - PLCore version information 00094 * - PLCore build information 00095 * - Endian 00096 * - Platform 00097 * - Operating system version 00098 */ 00099 PLCORE_API String GetInfo() const; 00100 00101 /** 00102 * @brief 00103 * Detects the current machine's endian ("byte order") 00104 * 00105 * @return 00106 * 'true' if the current machine is using 'Little Endian First', (LSB (least significant byte), also known as 'Intel-Format') 00107 * 'false' if the current machine is using 'Big Endian First', (MSB (most significant byte), also known as 'Motorola-Format') 00108 * 00109 * @note 00110 * - PixelLight is using 'Little Endian First' for it's binary file formats and so on 00111 */ 00112 PLCORE_API bool IsLittleEndian() const; 00113 00114 /** 00115 * @brief 00116 * Returns the name of the platform 00117 * 00118 * @return 00119 * Platform string (for instance 'Windows' for Windows, 'Linux' for Linux and so on) 00120 */ 00121 inline String GetPlatform() const; 00122 00123 /** 00124 * @brief 00125 * Returns the platform architecture 00126 * 00127 * @return 00128 * Platform architecture (for instance 'x86', 'x64', 'armeabi', 'armeabi-v7a' and so on) 00129 */ 00130 PLCORE_API String GetPlatformArchitecture() const; 00131 00132 /** 00133 * @brief 00134 * Returns the platform bit architecture 00135 * 00136 * @return 00137 * Platform bit architecture (for instance '32' for x86, '64' for x64) 00138 */ 00139 PLCORE_API uint32 GetPlatformBitArchitecture() const; 00140 00141 /** 00142 * @brief 00143 * Returns the name and version of the operating system 00144 * 00145 * @return 00146 * OS information string (for instance 'Windows 7 Service Pack 1 (Build 7601)') 00147 */ 00148 inline String GetOS() const; 00149 00150 /** 00151 * @brief 00152 * Returns the directory separator used by the operation system 00153 * 00154 * @return 00155 * The directory separator used by the operation system (e.g. '/' on Linux, '\' on Windows) 00156 */ 00157 inline char GetSeparator() const; 00158 00159 /** 00160 * @brief 00161 * Returns the shared library filename prefix used by the operation system 00162 * 00163 * @return 00164 * The shared library filename prefix used by the operation system (e.g. 'lib' as in 'libPLCore.so' on Linux, no prefix as in 'PLCore.dll' on Windows) 00165 */ 00166 inline String GetSharedLibraryPrefix() const; 00167 00168 /** 00169 * @brief 00170 * Returns the shared library file extension used by the operation system 00171 * 00172 * @return 00173 * The shared library file extension used by the operation system (e.g. 'so' on Linux, 'dll' on Windows) 00174 */ 00175 inline String GetSharedLibraryExtension() const; 00176 00177 /** 00178 * @brief 00179 * Get the CPU MHz 00180 * 00181 * @return 00182 * CPU MHz 00183 */ 00184 inline uint32 GetCPUMhz() const; 00185 00186 /** 00187 * @brief 00188 * Returns the name of the computer 00189 * 00190 * @return 00191 * Computer name 00192 */ 00193 inline String GetComputerName() const; 00194 00195 /** 00196 * @brief 00197 * Returns the current user name 00198 * 00199 * @return 00200 * User name 00201 */ 00202 inline String GetUserName() const; 00203 00204 /** 00205 * @brief 00206 * Get home directory of the current user 00207 * 00208 * @return 00209 * The home directory of the current user (native path style) 00210 * 00211 * @remarks 00212 * Examples on different systems: 00213 * - Linux: /home/<username> 00214 * - Windows XP: C:\Documents and Settings<Username> 00215 * - Windows 7: C:\Users<UserName> 00216 */ 00217 inline String GetUserHomeDir() const; 00218 00219 /** 00220 * @brief 00221 * Get data directory of the current user 00222 * 00223 * @return 00224 * The data directory of the current user (native path style) 00225 * 00226 * @remarks 00227 * Examples on different systems: 00228 * - Linux: /home/<username> 00229 * - Windows XP: C:\Documents and Settings<Username>\Application Data 00230 * - Windows 7: C:\Users<UserName>\AppData\Roaming 00231 */ 00232 inline String GetUserDataDir() const; 00233 00234 /** 00235 * @brief 00236 * Get name of data directory for given application name 00237 * 00238 * @param[in] sName 00239 * Application name 00240 * 00241 * @return 00242 * Name of data directory in native style of used OS 00243 * 00244 * @remarks 00245 * Examples on different systems: 00246 * - Linux: GetDataDirName("PixelLight") -> ".pixellight" 00247 * - Windows: GetDataDirName("PixelLight") -> "PixelLight" 00248 */ 00249 inline String GetDataDirName(const String &sName) const; 00250 00251 /** 00252 * @brief 00253 * Get absolute path of application executable 00254 * 00255 * @return 00256 * Path to executable (native path style, e.g. on Windows: 'C:\MyApplication\Test.exe') 00257 * 00258 * @note 00259 * - Application executable = currently running process 00260 */ 00261 inline String GetExecutableFilename() const; 00262 00263 /** 00264 * @brief 00265 * Reads an environment variable 00266 * 00267 * @param[in] sName 00268 * Name of the environment variable to read 00269 * 00270 * @return 00271 * Content of the variable 00272 */ 00273 inline String GetEnvironmentVariable(const String &sName) const; 00274 00275 /** 00276 * @brief 00277 * Writes an environment variable 00278 * 00279 * @param[in] sName 00280 * Name of the environment variable to write 00281 * @param[in] sValue 00282 * Value to write into the variable 00283 * 00284 * @return 00285 * 'true' if the variable has been set successfully, else 'false' 00286 */ 00287 inline bool SetEnvironmentVariable(const String &sName, const String &sValue) const; 00288 00289 /** 00290 * @brief 00291 * Deletes an environment variable 00292 * 00293 * @param[in] sName 00294 * Name of the environment variable to delete 00295 */ 00296 inline void DeleteEnvironmentVariable(const String &sName) const; 00297 00298 /** 00299 * @brief 00300 * Executes a system command (create a new process) 00301 * 00302 * @param[in] sCommand 00303 * Command to execute 00304 * @param[in] sParameters 00305 * Parameters to pass to the command 00306 * @param[in] sWorkingDir 00307 * Working directory in which to execute the command 00308 * 00309 * @return 00310 * 'true' if the command has been executed successfully, else 'false' 00311 */ 00312 inline bool Execute(const String &sCommand, const String &sParameters, const String &sWorkingDir = "") const; 00313 00314 /** 00315 * @brief 00316 * Returns the current program locale language information 00317 * 00318 * @remarks 00319 * Internally 'setlocale' is used to collect the current program locale information - but 00320 * only the 'language' information is returned as string. For instance, if the current locale 00321 * is 'English_USA.1252', 'English' is returned, if the locale is 'French_France.1252', just 00322 * 'French' is returned and so on. 00323 * This information can for instance be used to set a correct default language within the 00324 * localization system of PixelLight. 00325 * 00326 * @return 00327 * The current program locale language information 00328 */ 00329 inline String GetLocaleLanguage() const; 00330 00331 /** 00332 * @brief 00333 * Returns the current directory 00334 * 00335 * @return 00336 * Path to the current directory as the OS provided it (native path style) 00337 * 00338 * @note 00339 * - In case of an internally empty string you will receive "." in order to make it possible to add e.g. "/Data" and still end up in a valid path 00340 * - On Windows for example "C:\Programs\App" 00341 */ 00342 inline String GetCurrentDir() const; 00343 00344 /** 00345 * @brief 00346 * Sets the current directory 00347 * 00348 * @param[in] sPath 00349 * Path to the current directory 00350 * 00351 * @return 00352 * 'true', if all went fine, else 'false' 00353 * 00354 * @note 00355 * - Whenever possible, do not manipulate the current directory, this may backfire when you don't expect it 00356 */ 00357 inline bool SetCurrentDir(const String &sPath); 00358 00359 /** 00360 * @brief 00361 * Returns a pointer to the main thread 00362 * 00363 * @return 00364 * Main thread (assumed to be never a null pointer!) 00365 */ 00366 inline Thread *GetMainThread() const; 00367 00368 /** 00369 * @brief 00370 * Returns a pointer to the current thread 00371 * 00372 * @return 00373 * Current thread (assumed to be never a null pointer!) 00374 */ 00375 inline Thread *GetCurrentThread() const; 00376 00377 /** 00378 * @brief 00379 * Exit the application immediately 00380 * 00381 * @param[in] nReturn 00382 * Return value 00383 */ 00384 inline void Exit(int nReturn); 00385 00386 /** 00387 * @brief 00388 * Returns the console instance 00389 * 00390 * @return 00391 * The console instance 00392 */ 00393 inline const Console &GetConsole() const; 00394 00395 /** 00396 * @brief 00397 * Primitive way (e.g. by using a message box) to be able to tell the user that something went terrible wrong 00398 * 00399 * @param[in] sMessage 00400 * Message to show 00401 * 00402 * @remarks 00403 * Do not misuse this method in order to communicate with the user on a regular basis. This method does only 00404 * exist to be able to tell the user that something went terrible wrong. There are situations were one can't 00405 * use a log file, command line or something like this. Even when using e.g. a log file to write out error 00406 * information - an application may e.g. just close directly after it's start without any further information 00407 * and the user may even think that the application didn't start in the first place for an unknown reason. 00408 * In such a situation, it's polite to inform the user that something went terrible wrong and providing 00409 * a short hint how the issue may be solved. This method wasn't named "MessageBox()" by intend - because 00410 * such a feature may not be available on the used platform or is handled in another way as a normal MS Windows 00411 * message box. 00412 */ 00413 inline void UrgentMessage(const String &sMessage) const; 00414 00415 //[-------------------------------------------------------] 00416 //[ Time functions ] 00417 //[-------------------------------------------------------] 00418 /** 00419 * @brief 00420 * Returns the current date and time 00421 * 00422 * @return 00423 * Date and time string 00424 */ 00425 inline Time GetTime() const; 00426 00427 /** 00428 * @brief 00429 * Returns the number of milliseconds since the system was started 00430 * 00431 * @return 00432 * Number of milliseconds elapsed since the system was started 00433 */ 00434 inline uint64 GetMilliseconds() const; 00435 00436 /** 00437 * @brief 00438 * Retrieves the number of microseconds since the system was started 00439 * 00440 * @return 00441 * Number of microseconds elapsed since the system was started 00442 */ 00443 inline uint64 GetMicroseconds() const; 00444 00445 /** 00446 * @brief 00447 * Suspend the current thread for a specified time period 00448 * 00449 * @param[in] nMilliseconds 00450 * Number of milliseconds to sleep, should not be 0 because the behavior is implementation dependent (use 'Yield()' instead) 00451 */ 00452 inline void Sleep(uint64 nMilliseconds) const; 00453 00454 /** 00455 * @brief 00456 * Yields the rest of the current threads time slice 00457 * 00458 * @remarks 00459 * Yields the rest of the threads time slice so another active thread of equal or higher priority 00460 * waiting for processor time can run. Note that this function may return immediately and the behavior 00461 * is in general not exactly predictable. So, use this function to give the processor just a hint 00462 * that you are willed to give processor time away. 00463 */ 00464 inline void Yield() const; 00465 00466 //[-------------------------------------------------------] 00467 //[ Memory functions ] 00468 //[-------------------------------------------------------] 00469 /** 00470 * @brief 00471 * Returns an approximation of the percentage of used physical memory (0.0-100.0) 00472 * 00473 * @return 00474 * An approximation of the percentage of used physical memory 00475 */ 00476 inline float GetPercentageOfUsedPhysicalMemory() const; 00477 00478 /** 00479 * @brief 00480 * Returns the total physical memory in bytes 00481 * 00482 * @return 00483 * The total physical memory in bytes 00484 */ 00485 inline uint64 GetTotalPhysicalMemory() const; 00486 00487 /** 00488 * @brief 00489 * Returns the current free physical memory in bytes 00490 * 00491 * @return 00492 * The current free physical memory in bytes 00493 */ 00494 inline uint64 GetFreePhysicalMemory() const; 00495 00496 /** 00497 * @brief 00498 * Returns the total virtual memory in bytes 00499 * 00500 * @return 00501 * The total virtual memory in bytes 00502 */ 00503 inline uint64 GetTotalVirtualMemory() const; 00504 00505 /** 00506 * @brief 00507 * Returns the current free virtual memory in bytes 00508 * 00509 * @return 00510 * The current free virtual memory in bytes 00511 */ 00512 inline uint64 GetFreeVirtualMemory() const; 00513 00514 00515 //[-------------------------------------------------------] 00516 //[ Private functions ] 00517 //[-------------------------------------------------------] 00518 private: 00519 /** 00520 * @brief 00521 * Constructor 00522 */ 00523 PLCORE_API System(); 00524 00525 /** 00526 * @brief 00527 * Copy constructor 00528 * 00529 * @param[in] cSource 00530 * Source to copy from 00531 */ 00532 System(const System &cSource); 00533 00534 /** 00535 * @brief 00536 * Destructor 00537 */ 00538 PLCORE_API virtual ~System(); 00539 00540 /** 00541 * @brief 00542 * Copy operator 00543 * 00544 * @param[in] cSource 00545 * Source to copy from 00546 * 00547 * @return 00548 * Reference to this instance 00549 */ 00550 System &operator =(const System &cSource); 00551 00552 00553 //[-------------------------------------------------------] 00554 //[ Private data ] 00555 //[-------------------------------------------------------] 00556 private: 00557 SystemImpl *m_pSystemImpl; /**< Pointer to the system specific implementation (assumed to be never a null pointer!) */ 00558 Thread *m_pMainThread; /**< Pointer to the main thread (assumed to be never a null pointer!) */ 00559 00560 00561 }; 00562 00563 00564 //[-------------------------------------------------------] 00565 //[ Namespace ] 00566 //[-------------------------------------------------------] 00567 } // PLCore 00568 00569 00570 //[-------------------------------------------------------] 00571 //[ Implementation ] 00572 //[-------------------------------------------------------] 00573 #include "PLCore/System/System.inl" 00574 00575 00576 #endif // __PLCORE_SYSTEM_H__
|