dfhack/library/include/Error.h

160 lines
5.1 KiB
C

2010-02-27 17:13:34 -07:00
/*
https://github.com/peterix/dfhack
Copyright (c) 2009-2012 Petr Mrázek (peterix@gmail.com)
2010-02-27 17:13:34 -07:00
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.
*/
#pragma once
2011-12-31 04:48:42 -07:00
#include "Export.h"
#include "Pragma.h"
2010-02-27 17:13:34 -07:00
#include <string>
#include <sstream>
2010-02-27 17:13:34 -07:00
#include <exception>
namespace DFHack
{
namespace Error
{
/*
* our wrapper for the C++ exception. used to differentiate
* the whole array of DFHack exceptions from the rest
*/
#ifdef _MSC_VER
#pragma push
/**
* C4275 is - The warning officially is non dll-interface class 'std::exception' used as base for
* dll-interface class
*
* Basically, its saying that you might have an ABI problem if you mismatch compilers. We don't
* care since we build all of DFhack at once against whatever Toady is using
*/
#pragma warning(disable: 4275)
#endif
class DFHACK_EXPORT All : public std::exception{};
#ifdef _MSC_VER
#pragma pop
#endif
class DFHACK_EXPORT NullPointer : public All {
const char *varname_;
public:
NullPointer(const char *varname_ = NULL) : varname_(varname_) {}
const char *varname() const { return varname_; }
virtual const char *what() const throw();
};
#define CHECK_NULL_POINTER(var) \
{ if (var == NULL) throw DFHack::Error::NullPointer(#var); }
class DFHACK_EXPORT InvalidArgument : public All {
const char *expr_;
public:
InvalidArgument(const char *expr_ = NULL) : expr_(expr_) {}
const char *expr() const { return expr_; }
virtual const char *what() const throw();
};
#define CHECK_INVALID_ARGUMENT(expr) \
{ if (!(expr)) throw DFHack::Error::InvalidArgument(#expr); }
class DFHACK_EXPORT VTableMissing : public All {
const char *name_;
public:
VTableMissing(const char *name_ = NULL) : name_(name_) {}
const char *name() const { return name_; }
virtual const char *what() const throw();
};
class DFHACK_EXPORT AllSymbols : public All{};
2010-11-07 16:10:59 -07:00
// Syntax errors and whatnot, the xml can't be read
class DFHACK_EXPORT SymbolsXmlParse : public AllSymbols
2010-02-28 13:44:14 -07:00
{
public:
SymbolsXmlParse(const char* _desc, int _id, int _row, int _col)
2010-04-23 10:51:54 -06:00
:desc(_desc), id(_id), row(_row), col(_col)
{
std::stringstream s;
s << "error " << id << ": " << desc << ", at row " << row << " col " << col;
full = s.str();
}
std::string full;
2010-02-28 13:44:14 -07:00
const std::string desc;
const int id;
const int row;
const int col;
virtual ~SymbolsXmlParse() throw(){};
2010-02-28 13:44:14 -07:00
virtual const char* what() const throw()
{
2010-04-23 10:51:54 -06:00
return full.c_str();
2010-02-28 13:44:14 -07:00
}
};
class DFHACK_EXPORT SymbolsXmlBadAttribute : public All
2010-02-28 13:44:14 -07:00
{
public:
SymbolsXmlBadAttribute(const char* _attr) : attr(_attr)
2010-04-23 10:51:54 -06:00
{
std::stringstream s;
s << "attribute is either missing or invalid: " << attr;
full = s.str();
}
std::string full;
2010-02-28 13:44:14 -07:00
std::string attr;
virtual ~SymbolsXmlBadAttribute() throw(){};
2010-02-28 13:44:14 -07:00
virtual const char* what() const throw()
{
2010-04-23 10:51:54 -06:00
return full.c_str();
2010-02-28 13:44:14 -07:00
}
};
class DFHACK_EXPORT SymbolsXmlNoRoot : public All
2010-02-28 13:44:14 -07:00
{
public:
SymbolsXmlNoRoot() {}
virtual ~SymbolsXmlNoRoot() throw(){};
2010-02-28 13:44:14 -07:00
virtual const char* what() const throw()
{
return "Symbol file is missing root element.";
2010-02-28 13:44:14 -07:00
}
};
class DFHACK_EXPORT SymbolsXmlUnderspecifiedEntry : public All
2010-02-28 13:44:14 -07:00
{
public:
SymbolsXmlUnderspecifiedEntry(const char * _where) : where(_where)
2010-04-23 10:51:54 -06:00
{
std::stringstream s;
s << "Underspecified symbol file entry, each entry needs to set both the name attribute and have a value. parent: " << where;
2010-04-23 10:51:54 -06:00
full = s.str();
}
virtual ~SymbolsXmlUnderspecifiedEntry() throw(){};
std::string where;
2010-04-23 10:51:54 -06:00
std::string full;
2010-02-28 13:44:14 -07:00
virtual const char* what() const throw()
{
2010-04-23 10:51:54 -06:00
return full.c_str();
2010-02-28 13:44:14 -07:00
}
};
2010-02-27 17:13:34 -07:00
}
}