PixelLightAPI
.
|
00001 /*********************************************************\ 00002 * File: System.h * 00003 * 00004 * Copyright (C) 2002-2012 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 * Returns the absolute filename of the shared library or executable a given memory address is located in 00266 * 00267 * @param[in] pMemoryAddress 00268 * Memory address ("memory anchor") inside the shared library or executable from which the absolute filename 00269 * should be returned from, can be a null pointer in which case the result will be an empty string 00270 * 00271 * @return 00272 * Absolute filename of the shared library or executable the given memory address is located in (native path style), 00273 * or an empty string in case it was impossible to determine the filename 00274 * 00275 * @note 00276 * - Result example on Windows: 'C:\MyApplication\PLCore.dll' or 'C:\MyApplication\Test.exe' 00277 */ 00278 inline String GetModuleFilenameByMemoryAddress(const void *pMemoryAddress) const; 00279 00280 /** 00281 * @brief 00282 * Reads an environment variable 00283 * 00284 * @param[in] sName 00285 * Name of the environment variable to read 00286 * 00287 * @return 00288 * Content of the variable 00289 */ 00290 inline String GetEnvironmentVariable(const String &sName) const; 00291 00292 /** 00293 * @brief 00294 * Writes an environment variable 00295 * 00296 * @param[in] sName 00297 * Name of the environment variable to write 00298 * @param[in] sValue 00299 * Value to write into the variable 00300 * 00301 * @return 00302 * 'true' if the variable has been set successfully, else 'false' 00303 */ 00304 inline bool SetEnvironmentVariable(const String &sName, const String &sValue) const; 00305 00306 /** 00307 * @brief 00308 * Deletes an environment variable 00309 * 00310 * @param[in] sName 00311 * Name of the environment variable to delete 00312 */ 00313 inline void DeleteEnvironmentVariable(const String &sName) const; 00314 00315 /** 00316 * @brief 00317 * Executes a system command (create a new process) 00318 * 00319 * @param[in] sCommand 00320 * Command to execute 00321 * @param[in] sParameters 00322 * Parameters to pass to the command 00323 * @param[in] sWorkingDir 00324 * Working directory in which to execute the command 00325 * 00326 * @return 00327 * 'true' if the command has been executed successfully, else 'false' 00328 */ 00329 inline bool Execute(const String &sCommand, const String &sParameters, const String &sWorkingDir = "") const; 00330 00331 /** 00332 * @brief 00333 * Returns the current program locale language information 00334 * 00335 * @remarks 00336 * Internally 'setlocale' is used to collect the current program locale information - but 00337 * only the 'language' information is returned as string. For instance, if the current locale 00338 * is 'English_USA.1252', 'English' is returned, if the locale is 'French_France.1252', just 00339 * 'French' is returned and so on. 00340 * This information can for instance be used to set a correct default language within the 00341 * localization system of PixelLight. 00342 * 00343 * @return 00344 * The current program locale language information 00345 */ 00346 inline String GetLocaleLanguage() const; 00347 00348 /** 00349 * @brief 00350 * Returns the current directory 00351 * 00352 * @return 00353 * Path to the current directory as the OS provided it (native path style) 00354 * 00355 * @note 00356 * - 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 00357 * - On Windows for example "C:\Programs\App" 00358 */ 00359 inline String GetCurrentDir() const; 00360 00361 /** 00362 * @brief 00363 * Sets the current directory 00364 * 00365 * @param[in] sPath 00366 * Path to the current directory 00367 * 00368 * @return 00369 * 'true', if all went fine, else 'false' 00370 * 00371 * @note 00372 * - Whenever possible, do not manipulate the current directory, this may backfire when you don't expect it 00373 */ 00374 inline bool SetCurrentDir(const String &sPath); 00375 00376 /** 00377 * @brief 00378 * Returns a pointer to the main thread 00379 * 00380 * @return 00381 * Main thread (assumed to be never a null pointer!) 00382 */ 00383 inline Thread *GetMainThread() const; 00384 00385 /** 00386 * @brief 00387 * Returns a pointer to the current thread 00388 * 00389 * @return 00390 * Current thread (assumed to be never a null pointer!) 00391 */ 00392 inline Thread *GetCurrentThread() const; 00393 00394 /** 00395 * @brief 00396 * Exit the application immediately 00397 * 00398 * @param[in] nReturn 00399 * Return value 00400 */ 00401 inline void Exit(int nReturn); 00402 00403 /** 00404 * @brief 00405 * Returns the console instance 00406 * 00407 * @return 00408 * The console instance 00409 */ 00410 inline const Console &GetConsole() const; 00411 00412 /** 00413 * @brief 00414 * Primitive way (e.g. by using a message box) to be able to tell the user that something went terrible wrong 00415 * 00416 * @param[in] sMessage 00417 * Message to show 00418 * 00419 * @remarks 00420 * Do not misuse this method in order to communicate with the user on a regular basis. This method does only 00421 * exist to be able to tell the user that something went terrible wrong. There are situations were one can't 00422 * use a log file, command line or something like this. Even when using e.g. a log file to write out error 00423 * information - an application may e.g. just close directly after it's start without any further information 00424 * and the user may even think that the application didn't start in the first place for an unknown reason. 00425 * In such a situation, it's polite to inform the user that something went terrible wrong and providing 00426 * a short hint how the issue may be solved. This method wasn't named "MessageBox()" by intend - because 00427 * such a feature may not be available on the used platform or is handled in another way as a normal MS Windows 00428 * message box. 00429 */ 00430 inline void UrgentMessage(const String &sMessage) const; 00431 00432 //[-------------------------------------------------------] 00433 //[ Time functions ] 00434 //[-------------------------------------------------------] 00435 /** 00436 * @brief 00437 * Returns the current date and time 00438 * 00439 * @return 00440 * Date and time string 00441 */ 00442 inline Time GetTime() const; 00443 00444 /** 00445 * @brief 00446 * Returns the number of milliseconds since the system was started 00447 * 00448 * @return 00449 * Number of milliseconds elapsed since the system was started 00450 */ 00451 inline uint64 GetMilliseconds() const; 00452 00453 /** 00454 * @brief 00455 * Retrieves the number of microseconds since the system was started 00456 * 00457 * @return 00458 * Number of microseconds elapsed since the system was started 00459 */ 00460 inline uint64 GetMicroseconds() const; 00461 00462 /** 00463 * @brief 00464 * Suspend the current thread for a specified time period 00465 * 00466 * @param[in] nMilliseconds 00467 * Number of milliseconds to sleep, should not be 0 because the behavior is implementation dependent (use 'Yield()' instead) 00468 */ 00469 inline void Sleep(uint64 nMilliseconds) const; 00470 00471 /** 00472 * @brief 00473 * Yields the rest of the current threads time slice 00474 * 00475 * @remarks 00476 * Yields the rest of the threads time slice so another active thread of equal or higher priority 00477 * waiting for processor time can run. Note that this function may return immediately and the behavior 00478 * is in general not exactly predictable. So, use this function to give the processor just a hint 00479 * that you are willed to give processor time away. 00480 */ 00481 inline void Yield() const; 00482 00483 //[-------------------------------------------------------] 00484 //[ Memory functions ] 00485 //[-------------------------------------------------------] 00486 /** 00487 * @brief 00488 * Returns an approximation of the percentage of used physical memory (0.0-100.0) 00489 * 00490 * @return 00491 * An approximation of the percentage of used physical memory 00492 */ 00493 inline float GetPercentageOfUsedPhysicalMemory() const; 00494 00495 /** 00496 * @brief 00497 * Returns the total physical memory in bytes 00498 * 00499 * @return 00500 * The total physical memory in bytes 00501 */ 00502 inline uint64 GetTotalPhysicalMemory() const; 00503 00504 /** 00505 * @brief 00506 * Returns the current free physical memory in bytes 00507 * 00508 * @return 00509 * The current free physical memory in bytes 00510 */ 00511 inline uint64 GetFreePhysicalMemory() const; 00512 00513 /** 00514 * @brief 00515 * Returns the total virtual memory in bytes 00516 * 00517 * @return 00518 * The total virtual memory in bytes 00519 */ 00520 inline uint64 GetTotalVirtualMemory() const; 00521 00522 /** 00523 * @brief 00524 * Returns the current free virtual memory in bytes 00525 * 00526 * @return 00527 * The current free virtual memory in bytes 00528 */ 00529 inline uint64 GetFreeVirtualMemory() const; 00530 00531 00532 //[-------------------------------------------------------] 00533 //[ Private functions ] 00534 //[-------------------------------------------------------] 00535 private: 00536 /** 00537 * @brief 00538 * Constructor 00539 */ 00540 PLCORE_API System(); 00541 00542 /** 00543 * @brief 00544 * Copy constructor 00545 * 00546 * @param[in] cSource 00547 * Source to copy from 00548 */ 00549 System(const System &cSource); 00550 00551 /** 00552 * @brief 00553 * Destructor 00554 */ 00555 PLCORE_API virtual ~System(); 00556 00557 /** 00558 * @brief 00559 * Copy operator 00560 * 00561 * @param[in] cSource 00562 * Source to copy from 00563 * 00564 * @return 00565 * Reference to this instance 00566 */ 00567 System &operator =(const System &cSource); 00568 00569 00570 //[-------------------------------------------------------] 00571 //[ Private data ] 00572 //[-------------------------------------------------------] 00573 private: 00574 SystemImpl *m_pSystemImpl; /**< Pointer to the system specific implementation (assumed to be never a null pointer!) */ 00575 Thread *m_pMainThread; /**< Pointer to the main thread (assumed to be never a null pointer!) */ 00576 00577 00578 }; 00579 00580 00581 //[-------------------------------------------------------] 00582 //[ Namespace ] 00583 //[-------------------------------------------------------] 00584 } // PLCore 00585 00586 00587 //[-------------------------------------------------------] 00588 //[ Implementation ] 00589 //[-------------------------------------------------------] 00590 #include "PLCore/System/System.inl" 00591 00592 00593 #endif // __PLCORE_SYSTEM_H__
|