diff --git a/library/PolyVoxCore/include/PolyVoxCore/Array.inl b/library/PolyVoxCore/include/PolyVoxCore/Array.inl index 1821817d..53f428ac 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Array.inl +++ b/library/PolyVoxCore/include/PolyVoxCore/Array.inl @@ -73,10 +73,7 @@ namespace PolyVox template SubArray Array::operator[](uint32_t uIndex) { - if(uIndex >= m_pDimensions[0]) - { - POLYVOX_THROW(std::out_of_range, "Array index out of range"); - } + POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range"); return SubArray(&m_pElements[uIndex*m_pOffsets[0]], @@ -95,10 +92,7 @@ namespace PolyVox template const SubArray Array::operator[](uint32_t uIndex) const { - if(uIndex >= m_pDimensions[0]) - { - POLYVOX_THROW(std::out_of_range, "Array index out of range"); - } + POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range"); return SubArray(&m_pElements[uIndex*m_pOffsets[0]], @@ -147,10 +141,7 @@ namespace PolyVox m_uNoOfElements = 1; for (uint32_t i = 0; i uint32_t Array::getDimension(uint32_t uDimension) { - if(uDimension >= noOfDims) - { - POLYVOX_THROW(std::out_of_range, "Array dimension out of range"); - } + POLYVOX_THROW_IF(uDimension >= noOfDims, std::out_of_range, "Array dimension out of range"); return m_pDimensions[uDimension]; } @@ -266,10 +254,7 @@ namespace PolyVox template ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex) { - if(uIndex >= m_pDimensions[0]) - { - POLYVOX_THROW(std::out_of_range, "Array index out of range"); - } + POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range"); return m_pElements[uIndex]; } @@ -277,10 +262,7 @@ namespace PolyVox template const ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex) const { - if(uIndex >= m_pDimensions[0]) - { - POLYVOX_THROW(std::out_of_range, "Array index out of range"); - } + POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range"); return m_pElements[uIndex]; } diff --git a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h index 5080f7db..afc452c0 100644 --- a/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h +++ b/library/PolyVoxCore/include/PolyVoxCore/Impl/ErrorHandling.h @@ -255,15 +255,36 @@ namespace PolyVox * ... */ #ifdef POLYVOX_THROW_ENABLED - #define POLYVOX_THROW(type, message) \ - PolyVox::logError() << (message); \ - throw type((message)) - // Some fast functions (getVoxel(), etc) use exceptions for error handling but don't want the overhead of logging. - // This overhead is present even if no exception is thrown, probably because the presence of the logging code prevents - // some inlining. Therefore we provide this macro which doesn't log for such specialised circumstances. - #define POLYVOX_THROW_DONT_LOG(type, message) \ - throw type((message)) + #define POLYVOX_THROW_IF(condition, type, message) \ + /* We use the do...while(0) construct in our macros (for reasons see here: http://stackoverflow.com/a/154138) \ + but Visual Studio gives unhelpful 'conditional expression is constant' warnings. The recommended solution \ + (http://stackoverflow.com/a/1946485) is to disable these warnings. */ \ + POLYVOX_MSC_WARNING_PUSH \ + POLYVOX_DISABLE_MSC_WARNING(4127) \ + do \ + { \ + if ((condition)) \ + { \ + PolyVox::logError() << (message); \ + throw type((message)); \ + } \ + } while(0) \ + POLYVOX_MSC_WARNING_POP + + #define POLYVOX_THROW(type, message) \ + /* We use the do...while(0) construct in our macros (for reasons see here: http://stackoverflow.com/a/154138) \ + but Visual Studio gives unhelpful 'conditional expression is constant' warnings. The recommended solution \ + (http://stackoverflow.com/a/1946485) is to disable these warnings. */ \ + POLYVOX_MSC_WARNING_PUSH \ + POLYVOX_DISABLE_MSC_WARNING(4127) \ + do \ + { \ + PolyVox::logError() << (message); \ + throw type((message)); \ + } while(0) \ + POLYVOX_MSC_WARNING_POP + #else namespace PolyVox { @@ -273,17 +294,37 @@ namespace PolyVox void setThrowHandler(ThrowHandler newHandler); } - #define POLYVOX_THROW(type, message) \ - PolyVox::logError() << (message); \ - type except = (type)((message)); \ - getThrowHandler()((except), __FILE__, __LINE__) + #define POLYVOX_THROW_IF(condition, type, message) \ + /* We use the do...while(0) construct in our macros (for reasons see here: http://stackoverflow.com/a/154138) \ + but Visual Studio gives unhelpful 'conditional expression is constant' warnings. The recommended solution \ + (http://stackoverflow.com/a/1946485) is to disable these warnings. */ \ + POLYVOX_MSC_WARNING_PUSH \ + POLYVOX_DISABLE_MSC_WARNING(4127) \ + do \ + { \ + if ((condition)) \ + { \ + PolyVox::logError() << (message); \ + type except = (type)((message)); \ + getThrowHandler()((except), __FILE__, __LINE__); \ + } \ + } while(0) \ + POLYVOX_MSC_WARNING_POP + + #define POLYVOX_THROW(type, message) \ + /* We use the do...while(0) construct in our macros (for reasons see here: http://stackoverflow.com/a/154138) \ + but Visual Studio gives unhelpful 'conditional expression is constant' warnings. The recommended solution \ + (http://stackoverflow.com/a/1946485) is to disable these warnings. */ \ + POLYVOX_MSC_WARNING_PUSH \ + POLYVOX_DISABLE_MSC_WARNING(4127) \ + do \ + { \ + PolyVox::logError() << (message); \ + type except = (type)((message)); \ + getThrowHandler()((except), __FILE__, __LINE__); \ + } while(0) \ + POLYVOX_MSC_WARNING_POP - // Some fast functions (getVoxel(), etc) use exceptions for error handling but don't want the overhead of logging. - // This overhead is present even if no exception is thrown, probably because the presence of the logging code prevents - // some inlining. Therefore we provide this macro which doesn't log for such specialised circumstances. - #define POLYVOX_THROW_DONT_LOG(type, message) \ - type except = (type)((message)); \ - getThrowHandler()((except), __FILE__, __LINE__) #endif namespace PolyVox