00001 //---------------------------------------------------------------------------- 00002 /** @file SgSystem.cpp 00003 See SgSystem.h 00004 */ 00005 //---------------------------------------------------------------------------- 00006 00007 #include "SgSystem.h" 00008 00009 #include <algorithm> 00010 #include <cassert> 00011 #include <cstdlib> 00012 #include <functional> 00013 #include <iostream> 00014 #include <limits> 00015 #include <list> 00016 #include "SgTime.h" 00017 00018 using namespace std; 00019 00020 //---------------------------------------------------------------------------- 00021 00022 namespace { 00023 00024 volatile bool s_userAbort = false; 00025 00026 /** Assertion handlers. 00027 Stored in a static function variable to ensure, that they exist at 00028 first usage, if this function is called from global variables in 00029 different compilation units. 00030 */ 00031 list<SgAssertionHandler*>& AssertionHandlers() 00032 { 00033 static list<SgAssertionHandler*> s_assertionHandlers; 00034 return s_assertionHandlers; 00035 } 00036 00037 } // namespace 00038 00039 //---------------------------------------------------------------------------- 00040 00041 SgAssertionHandler::SgAssertionHandler() 00042 { 00043 AssertionHandlers().push_back(this); 00044 } 00045 00046 SgAssertionHandler::~SgAssertionHandler() 00047 { 00048 AssertionHandlers().remove(this); 00049 } 00050 00051 //---------------------------------------------------------------------------- 00052 00053 #ifndef NDEBUG 00054 00055 /** Set the shell variable SMARTGAME_ASSERT_CONTINUE to drop into the debugger 00056 instead of aborting the program whenever an SG_ASSERT fails 00057 */ 00058 static bool s_assertContinue = std::getenv("SMARTGAME_ASSERT_CONTINUE"); 00059 00060 void SgHandleAssertion(const char* expr, const char* file, int line) 00061 { 00062 /** Set a breakpoint on the next line to drop into the debugger */ 00063 cerr << "Assertion failed " 00064 << file << ':' << line << ": " << expr << '\n'; 00065 for_each(AssertionHandlers().begin(), AssertionHandlers().end(), 00066 mem_fun(&SgAssertionHandler::Run)); 00067 if (! s_assertContinue) 00068 abort(); 00069 } 00070 #endif 00071 00072 //---------------------------------------------------------------------------- 00073 00074 void SgSetUserAbort(bool aborted) 00075 { 00076 s_userAbort = aborted; 00077 } 00078 00079 bool SgUserAbort() 00080 { 00081 return s_userAbort; 00082 } 00083 00084 //----------------------------------------------------------------------------