From 4325ffabc4cea8bc32bebd76d523c4ec42de9fc7 Mon Sep 17 00:00:00 2001 From: David Williams Date: Fri, 6 Feb 2015 21:08:19 +0100 Subject: [PATCH] Moved error handling functions/macros to be header-only. --- library/PolyVoxCore/CMakeLists.txt | 2 +- .../include/PolyVoxCore/Impl/ErrorHandling.h | 45 ++++++++++- .../PolyVoxCore/source/Impl/ErrorHandling.cpp | 74 ------------------- library/PolyVoxCore/source/Impl/Timer.cpp | 0 4 files changed, 44 insertions(+), 77 deletions(-) delete mode 100644 library/PolyVoxCore/source/Impl/ErrorHandling.cpp delete mode 100644 library/PolyVoxCore/source/Impl/Timer.cpp diff --git a/library/PolyVoxCore/CMakeLists.txt b/library/PolyVoxCore/CMakeLists.txt index 51ed0841..76df58b2 100644 --- a/library/PolyVoxCore/CMakeLists.txt +++ b/library/PolyVoxCore/CMakeLists.txt @@ -82,7 +82,7 @@ SET(CORE_INC_FILES ) SET(IMPL_SRC_FILES - source/Impl/ErrorHandling.cpp + #source/Impl/ErrorHandling.cpp source/Impl/Logging.cpp #source/Impl/MarchingCubesTables.cpp #source/Impl/RandomUnitVectors.cpp diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h index be7515a6..a1049bf7 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h @@ -155,8 +155,49 @@ freely, subject to the following restrictions: { typedef void (*ThrowHandler)(std::exception& e, const char* file, int line); - ThrowHandler getThrowHandler(); - void setThrowHandler(ThrowHandler newHandler); + inline void defaultThrowHandler(std::exception& e, const char* file, int line) + { + std::stringstream ss; \ + ss << "\n"; \ + ss << " PolyVox exception thrown!"; \ + ss << " ========================="; \ + ss << " PolyVox has tried to throw an exception but it was built without support"; \ + ss << " for exceptions. In this scenario PolyVox will call a 'throw handler'"; \ + ss << " and this message is being printed by the default throw handler."; \ + ss << "\n"; \ + ss << " If you don't want to enable exceptions then you should try to determine why"; \ + ss << " this exception was thrown and make sure it doesn't happen again. If it was"; \ + ss << " due to something like an invalid argument to a function then you should be"; \ + ss << " able to fix it quite easily by validating parameters as appropriate. More"; \ + ss << " complex exception scenarios (out of memory, etc) might be harder to fix and"; \ + ss << " you should replace this default handler with something which is more"; \ + ss << " meaningful to your users."; \ + ss << "\n"; \ + ss << " Exception details"; \ + ss << " -----------------"; \ + ss << " Line: " << line; \ + ss << " File: " << file; \ + ss << " Message: " << e.what(); \ + ss << "\n"; \ + PolyVox::Impl::getLoggerInstance()->logFatalMessage(ss.str()); \ + POLYVOX_HALT(); \ + } + + inline ThrowHandler& getThrowHandlerInstance() + { + static ThrowHandler s_fThrowHandler = &defaultThrowHandler; + return s_fThrowHandler; + } + + inline ThrowHandler getThrowHandler() + { + return getThrowHandlerInstance(); + } + + inline void setThrowHandler(ThrowHandler fNewHandler) + { + getThrowHandlerInstance() = fNewHandler; + } } #define POLYVOX_THROW_IF(condition, type, message) \ diff --git a/library/PolyVoxCore/source/Impl/ErrorHandling.cpp b/library/PolyVoxCore/source/Impl/ErrorHandling.cpp deleted file mode 100644 index 98e03fd2..00000000 --- a/library/PolyVoxCore/source/Impl/ErrorHandling.cpp +++ /dev/null @@ -1,74 +0,0 @@ -/******************************************************************************* -Copyright (c) 2005-2009 David Williams - -This software is provided 'as-is', without any express or implied -warranty. In no event will the authors be held liable for any damages -arising from the use of this software. - -Permission is granted to anyone to use this software for any purpose, -including commercial applications, and to alter it and redistribute it -freely, subject to the following restrictions: - - 1. The origin of this software must not be misrepresented; you must not - claim that you wrote the original software. If you use this software - in a product, an acknowledgment in the product documentation would be - appreciated but is not required. - - 2. Altered source versions must be plainly marked as such, and must not be - misrepresented as being the original software. - - 3. This notice may not be removed or altered from any source - distribution. -*******************************************************************************/ - -#include "PolyVoxCore/Impl/ErrorHandling.h" - -namespace PolyVox -{ - -#ifndef POLYVOX_THROW_ENABLED - void defaultThrowHandler(std::exception& e, const char* file, int line) - { - std::stringstream ss; \ - ss << "\n"; \ - ss << " PolyVox exception thrown!"; \ - ss << " ========================="; \ - ss << " PolyVox has tried to throw an exception but it was built without support"; \ - ss << " for exceptions. In this scenario PolyVox will call a 'throw handler'"; \ - ss << " and this message is being printed by the default throw handler."; \ - ss << "\n"; \ - ss << " If you don't want to enable exceptions then you should try to determine why"; \ - ss << " this exception was thrown and make sure it doesn't happen again. If it was"; \ - ss << " due to something like an invalid argument to a function then you should be"; \ - ss << " able to fix it quite easily by validating parameters as appropriate. More"; \ - ss << " complex exception scenarios (out of memory, etc) might be harder to fix and"; \ - ss << " you should replace this default handler with something which is more"; \ - ss << " meaningful to your users."; \ - ss << "\n"; \ - ss << " Exception details"; \ - ss << " -----------------"; \ - ss << " Line: " << line; \ - ss << " File: " << file; \ - ss << " Message: " << e.what(); \ - ss << "\n"; \ - PolyVox::Impl::getLoggerInstance()->logFatalMessage(ss.str()); \ - POLYVOX_HALT(); \ - } - - ThrowHandler& getThrowHandlerInstance() - { - static ThrowHandler s_fThrowHandler = &defaultThrowHandler; - return s_fThrowHandler; - } - - ThrowHandler getThrowHandler() - { - return getThrowHandlerInstance(); - } - - void setThrowHandler(ThrowHandler fNewHandler) - { - getThrowHandlerInstance() = fNewHandler; - } -#endif -} diff --git a/library/PolyVoxCore/source/Impl/Timer.cpp b/library/PolyVoxCore/source/Impl/Timer.cpp deleted file mode 100644 index e69de29b..00000000