Add function names to NullPointer and InvalidArgument exceptions

develop
lethosor 2018-05-11 09:49:27 -04:00
parent 58636f5a27
commit 272cdcb2b5
3 changed files with 21 additions and 11 deletions

@ -8,13 +8,13 @@ inline std::string safe_str(const char *s)
return s ? s : "(NULL)";
}
NullPointer::NullPointer(const char *varname)
:All("NULL pointer: " + safe_str(varname)),
NullPointer::NullPointer(const char *varname, const char *func)
:All("In " + safe_str(func) + ": NULL pointer: " + safe_str(varname)),
varname(varname)
{}
InvalidArgument::InvalidArgument(const char *expr)
:All("Invalid argument; expected: " + safe_str(expr)),
InvalidArgument::InvalidArgument(const char *expr, const char *func)
:All("In " + safe_str(func) + ": Invalid argument; expected: " + safe_str(expr)),
expr(expr)
{}

@ -24,11 +24,13 @@ distribution.
#pragma once
#include <exception>
#include <sstream>
#include <string>
#include "Export.h"
#include "MiscUtils.h"
#include "Pragma.h"
#include <string>
#include <sstream>
#include <exception>
namespace DFHack
{
@ -68,20 +70,20 @@ namespace DFHack
class DFHACK_EXPORT NullPointer : public All {
public:
const char *const varname;
NullPointer(const char *varname = NULL);
NullPointer(const char *varname = NULL, const char *func = NULL);
};
#define CHECK_NULL_POINTER(var) \
{ if (var == NULL) throw DFHack::Error::NullPointer(#var); }
{ if (var == NULL) throw DFHack::Error::NullPointer(#var, DFHACK_FUNCTION_SIG); }
class DFHACK_EXPORT InvalidArgument : public All {
public:
const char *const expr;
InvalidArgument(const char *expr = NULL);
InvalidArgument(const char *expr = NULL, const char *func = NULL);
};
#define CHECK_INVALID_ARGUMENT(expr) \
{ if (!(expr)) throw DFHack::Error::InvalidArgument(#expr); }
{ if (!(expr)) throw DFHack::Error::InvalidArgument(#expr, DFHACK_FUNCTION_SIG); }
class DFHACK_EXPORT VTableMissing : public All {
public:

@ -36,6 +36,14 @@ using std::ostream;
using std::stringstream;
using std::endl;
#if defined(_MSC_VER)
#define DFHACK_FUNCTION_SIG __FUNCSIG__
#elif defined(__GNUC__)
#define DFHACK_FUNCTION_SIG __PRETTY_FUNCTION__
#else
#define DFHACK_FUNCTION_SIG __func__
#endif
template <typename T>
void print_bits ( T val, ostream& out )
{