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