Added POLYVOX_THROW_IF macro to simplify error handling.

This commit is contained in:
David Williams 2013-06-26 22:39:15 +02:00
parent 77a340f2b3
commit 56cf423bfd
2 changed files with 65 additions and 42 deletions

View File

@ -73,10 +73,7 @@ namespace PolyVox
template <uint32_t noOfDims, typename ElementType> template <uint32_t noOfDims, typename ElementType>
SubArray<noOfDims-1, ElementType> Array<noOfDims, ElementType>::operator[](uint32_t uIndex) SubArray<noOfDims-1, ElementType> Array<noOfDims, ElementType>::operator[](uint32_t uIndex)
{ {
if(uIndex >= m_pDimensions[0]) POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range");
{
POLYVOX_THROW(std::out_of_range, "Array index out of range");
}
return return
SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]], SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
@ -95,10 +92,7 @@ namespace PolyVox
template <uint32_t noOfDims, typename ElementType> template <uint32_t noOfDims, typename ElementType>
const SubArray<noOfDims-1, ElementType> Array<noOfDims, ElementType>::operator[](uint32_t uIndex) const const SubArray<noOfDims-1, ElementType> Array<noOfDims, ElementType>::operator[](uint32_t uIndex) const
{ {
if(uIndex >= m_pDimensions[0]) POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range");
{
POLYVOX_THROW(std::out_of_range, "Array index out of range");
}
return return
SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]], SubArray<noOfDims-1, ElementType>(&m_pElements[uIndex*m_pOffsets[0]],
@ -147,10 +141,7 @@ namespace PolyVox
m_uNoOfElements = 1; m_uNoOfElements = 1;
for (uint32_t i = 0; i<noOfDims; i++) for (uint32_t i = 0; i<noOfDims; i++)
{ {
if(pDimensions[i] == 0) POLYVOX_THROW_IF(pDimensions[i] == 0, std::out_of_range, "Invalid array dimension");
{
POLYVOX_THROW(std::out_of_range, "Invalid array dimension");
}
m_uNoOfElements *= pDimensions[i]; m_uNoOfElements *= pDimensions[i];
m_pDimensions[i] = pDimensions[i]; m_pDimensions[i] = pDimensions[i];
@ -197,10 +188,7 @@ namespace PolyVox
template <uint32_t noOfDims, typename ElementType> template <uint32_t noOfDims, typename ElementType>
uint32_t Array<noOfDims, ElementType>::getDimension(uint32_t uDimension) uint32_t Array<noOfDims, ElementType>::getDimension(uint32_t uDimension)
{ {
if(uDimension >= noOfDims) POLYVOX_THROW_IF(uDimension >= noOfDims, std::out_of_range, "Array dimension out of range");
{
POLYVOX_THROW(std::out_of_range, "Array dimension out of range");
}
return m_pDimensions[uDimension]; return m_pDimensions[uDimension];
} }
@ -266,10 +254,7 @@ namespace PolyVox
template <typename ElementType> template <typename ElementType>
ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex) ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex)
{ {
if(uIndex >= m_pDimensions[0]) POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range");
{
POLYVOX_THROW(std::out_of_range, "Array index out of range");
}
return m_pElements[uIndex]; return m_pElements[uIndex];
} }
@ -277,10 +262,7 @@ namespace PolyVox
template <typename ElementType> template <typename ElementType>
const ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex) const const ElementType& Array<1, ElementType>::operator[] (uint32_t uIndex) const
{ {
if(uIndex >= m_pDimensions[0]) POLYVOX_THROW_IF(uIndex >= m_pDimensions[0], std::out_of_range, "Array index out of range");
{
POLYVOX_THROW(std::out_of_range, "Array index out of range");
}
return m_pElements[uIndex]; return m_pElements[uIndex];
} }

View File

@ -255,15 +255,36 @@ namespace PolyVox
* ... * ...
*/ */
#ifdef POLYVOX_THROW_ENABLED #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. #define POLYVOX_THROW_IF(condition, type, message) \
// This overhead is present even if no exception is thrown, probably because the presence of the logging code prevents /* We use the do...while(0) construct in our macros (for reasons see here: http://stackoverflow.com/a/154138) \
// some inlining. Therefore we provide this macro which doesn't log for such specialised circumstances. but Visual Studio gives unhelpful 'conditional expression is constant' warnings. The recommended solution \
#define POLYVOX_THROW_DONT_LOG(type, message) \ (http://stackoverflow.com/a/1946485) is to disable these warnings. */ \
throw type((message)) 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 #else
namespace PolyVox namespace PolyVox
{ {
@ -273,17 +294,37 @@ namespace PolyVox
void setThrowHandler(ThrowHandler newHandler); void setThrowHandler(ThrowHandler newHandler);
} }
#define POLYVOX_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); \ PolyVox::logError() << (message); \
type except = (type)((message)); \ type except = (type)((message)); \
getThrowHandler()((except), __FILE__, __LINE__) 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. #define POLYVOX_THROW(type, message) \
// This overhead is present even if no exception is thrown, probably because the presence of the logging code prevents /* We use the do...while(0) construct in our macros (for reasons see here: http://stackoverflow.com/a/154138) \
// some inlining. Therefore we provide this macro which doesn't log for such specialised circumstances. but Visual Studio gives unhelpful 'conditional expression is constant' warnings. The recommended solution \
#define POLYVOX_THROW_DONT_LOG(type, message) \ (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)); \ type except = (type)((message)); \
getThrowHandler()((except), __FILE__, __LINE__) getThrowHandler()((except), __FILE__, __LINE__); \
} while(0) \
POLYVOX_MSC_WARNING_POP
#endif #endif
namespace PolyVox namespace PolyVox