49 lines
1.4 KiB
C++
49 lines
1.4 KiB
C++
|
#ifndef POKEMONSCRIPTTESTER_ASSERTIONFAILED_HPP
|
||
|
#define POKEMONSCRIPTTESTER_ASSERTIONFAILED_HPP
|
||
|
#include <exception>
|
||
|
#include <sstream>
|
||
|
|
||
|
class AssertionFailed : public std::exception {
|
||
|
static inline std::string& ltrim(std::string& s, const char* t = " \t\n\r\f\v") {
|
||
|
s.erase(0, s.find_first_not_of(t));
|
||
|
return s;
|
||
|
}
|
||
|
static std::string GetLine(const std::string& path, size_t num) {
|
||
|
std::ifstream file;
|
||
|
file.open(path);
|
||
|
|
||
|
file.seekg(std::ios::beg);
|
||
|
for (size_t i = 0; i < num - 1; ++i) {
|
||
|
file.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
|
||
|
}
|
||
|
std::string line;
|
||
|
std::getline(file, line);
|
||
|
file.close();
|
||
|
line = ltrim(line);
|
||
|
return line;
|
||
|
}
|
||
|
|
||
|
char* _msg;
|
||
|
|
||
|
public:
|
||
|
AssertionFailed(const std::string& path, size_t line, const std::string& message = "") {
|
||
|
auto l = GetLine(path, line);
|
||
|
std::stringstream ss;
|
||
|
ss << "Assertion failed at: " << path << ":" << line << std::endl;
|
||
|
ss << l;
|
||
|
if (message.length() > 0) {
|
||
|
ss << std::endl << message;
|
||
|
}
|
||
|
auto str = ss.str();
|
||
|
_msg = new char[str.length() + 1];
|
||
|
str.copy(_msg, str.length(), 0);
|
||
|
_msg[str.length()] = 0;
|
||
|
}
|
||
|
|
||
|
~AssertionFailed() { delete[] _msg; }
|
||
|
|
||
|
const char* what() const _GLIBCXX_TXN_SAFE_DYN _GLIBCXX_NOTHROW override { return _msg; }
|
||
|
};
|
||
|
|
||
|
#endif // POKEMONSCRIPTTESTER_ASSERTIONFAILED_HPP
|