Exceptions are now part of the PolyVox public API (client code may need to catch them) but the utility macros we use to throw them are still private implementation details.
This commit is contained in:
parent
4887fc2701
commit
3c31643e4f
61
include/PolyVox/Exceptions.h
Normal file
61
include/PolyVox/Exceptions.h
Normal file
@ -0,0 +1,61 @@
|
||||
/*******************************************************************************
|
||||
Copyright (c) 2005-2015 David Williams and Matthew 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.
|
||||
*******************************************************************************/
|
||||
|
||||
#ifndef __PolyVox_Exceptions_H__
|
||||
#define __PolyVox_Exceptions_H__
|
||||
|
||||
#include <stdexcept> // For base exception classes.
|
||||
#include <string.h> // Exception constuctors take strings.
|
||||
|
||||
// These exceptions form part of the public API because lcient code may need to catch them.
|
||||
// Note that our utility macros such as 'POLYVOX_THROW_IF' do not form part of the public API
|
||||
// as they are only for our own internal use.
|
||||
namespace PolyVox
|
||||
{
|
||||
/// A general purpose exception to indicate that an operation cannot be peformed.
|
||||
class invalid_operation : public std::logic_error
|
||||
{
|
||||
public:
|
||||
explicit invalid_operation(const std::string& message)
|
||||
: logic_error(message.c_str()) {}
|
||||
|
||||
explicit invalid_operation(const char *message)
|
||||
: logic_error(message) {}
|
||||
};
|
||||
|
||||
/// Thrown to indicate that a function is deliberatly not implmented. For example, perhaps you called a function
|
||||
/// in a base class whereas you are supposed to use a derived class which implements the function, or perhaps the
|
||||
/// function is not defined for a particular template parameter. It may be that the function is required to
|
||||
/// compile sucessfully but it should not be called.
|
||||
class not_implemented : public std::logic_error
|
||||
{
|
||||
public:
|
||||
explicit not_implemented(const std::string& message)
|
||||
: logic_error(message.c_str()) {}
|
||||
|
||||
explicit not_implemented(const char *message)
|
||||
: logic_error(message) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //__PolyVox_Exceptions_H__
|
@ -21,6 +21,7 @@ freely, subject to the following restrictions:
|
||||
distribution.
|
||||
*******************************************************************************/
|
||||
|
||||
// This file provides a simple way to include all error-handling functionality.
|
||||
#ifndef __PolyVox_ErrorHandling_H__
|
||||
#define __PolyVox_ErrorHandling_H__
|
||||
|
||||
|
@ -24,42 +24,47 @@ distribution.
|
||||
#ifndef __PolyVox_ExceptionsImpl_H__
|
||||
#define __PolyVox_ExceptionsImpl_H__
|
||||
|
||||
#include "PolyVox/Exceptions.h"
|
||||
|
||||
#include "PolyVox/Impl/Config.h"
|
||||
#include "PolyVox/Impl/Logging.h" // Exceptions can log when they are thrown.
|
||||
|
||||
#include <cstdlib> // For std::exit
|
||||
#include <iostream> // For std::cerr
|
||||
#include <stdexcept>
|
||||
#include <sstream>
|
||||
#include <string.h> // Exception constuctors take strings.
|
||||
#include <csignal>
|
||||
|
||||
#ifdef POLYVOX_THROW_ENABLED
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
template< typename ExceptionType, typename ... Args >
|
||||
void polyvox_throw(Args const& ... messageArgs)
|
||||
namespace Impl
|
||||
{
|
||||
std::string message = Impl::argListToString(messageArgs...);
|
||||
template< typename ExceptionType, typename ... Args >
|
||||
void polyvox_throw(Args const& ... messageArgs)
|
||||
{
|
||||
std::string message = argListToString(messageArgs...);
|
||||
#ifdef POLYVOX_LOG_ERROR_ENABLED
|
||||
PolyVox::Impl::getLoggerInstance()->logErrorMessage(message);
|
||||
getLoggerInstance()->logErrorMessage(message);
|
||||
#endif
|
||||
throw ExceptionType(message);
|
||||
}
|
||||
throw ExceptionType(message);
|
||||
}
|
||||
|
||||
template< typename ExceptionType, typename ... Args >
|
||||
void polyvox_throw_if(bool condition, Args const& ... messageArgs)
|
||||
{
|
||||
if (condition) { polyvox_throw<ExceptionType>(messageArgs...); }
|
||||
template< typename ExceptionType, typename ... Args >
|
||||
void polyvox_throw_if(bool condition, Args const& ... messageArgs)
|
||||
{
|
||||
if (condition) { polyvox_throw<ExceptionType>(messageArgs...); }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define POLYVOX_THROW(type, ...) PolyVox::polyvox_throw<type>(__VA_ARGS__)
|
||||
#define POLYVOX_THROW_IF(condition, type, ...) PolyVox::polyvox_throw_if<type>(condition, __VA_ARGS__)
|
||||
#define POLYVOX_THROW(type, ...) PolyVox::Impl::polyvox_throw<type>(__VA_ARGS__)
|
||||
#define POLYVOX_THROW_IF(condition, type, ...) PolyVox::Impl::polyvox_throw_if<type>(condition, __VA_ARGS__)
|
||||
|
||||
#else
|
||||
|
||||
// This stuff should possibly be in the 'Impl' namespace, but it may be complex and it's not
|
||||
// clear if this ability to disable throwing of exceptions is useful in the long term anyway.
|
||||
namespace PolyVox
|
||||
{
|
||||
typedef void(*ThrowHandler)(std::exception& e, const char* file, int line);
|
||||
@ -146,32 +151,4 @@ distribution.
|
||||
|
||||
#endif
|
||||
|
||||
namespace PolyVox
|
||||
{
|
||||
/// A general purpose exception to indicate that an operation cannot be peformed.
|
||||
class invalid_operation : public std::logic_error
|
||||
{
|
||||
public:
|
||||
explicit invalid_operation(const std::string& message)
|
||||
: logic_error(message.c_str()) {}
|
||||
|
||||
explicit invalid_operation(const char *message)
|
||||
: logic_error(message) {}
|
||||
};
|
||||
|
||||
/// Thrown to indicate that a function is deliberatly not implmented. For example, perhaps you called a function
|
||||
/// in a base class whereas you are supposed to use a derived class which implements the function, or perhaps the
|
||||
/// function is not defined for a particular template parameter. It may be that the function is required to
|
||||
/// compile sucessfully but it should not be called.
|
||||
class not_implemented : public std::logic_error
|
||||
{
|
||||
public:
|
||||
explicit not_implemented(const std::string& message)
|
||||
: logic_error(message.c_str()) {}
|
||||
|
||||
explicit not_implemented(const char *message)
|
||||
: logic_error(message) {}
|
||||
};
|
||||
}
|
||||
|
||||
#endif //__PolyVox_ExceptionsImpl_H__
|
||||
|
Loading…
x
Reference in New Issue
Block a user