This is a simple game I'm writing to test the idea of using games to teach C++.
https://learncodethehardway.com/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
6 months ago
|
#include <string>
|
||
|
#include <source_location>
|
||
|
#include <fmt/core.h>
|
||
|
|
||
|
using namespace std;
|
||
|
|
||
|
namespace dbc {
|
||
|
class Error {
|
||
|
public:
|
||
|
const string message;
|
||
|
Error(string m) : message{m} {}
|
||
|
Error(const char *m) : message{m} {}
|
||
|
};
|
||
|
|
||
|
class CheckError : public Error {};
|
||
|
|
||
|
class SentinelError : public Error {};
|
||
|
class PreCondError : public Error {};
|
||
|
class PostCondError : public Error {};
|
||
|
|
||
|
void log(const string &message, std::source_location loc = std::source_location::current()) {
|
||
|
fmt::print("[{}:{}:{}] {}\n", loc.file_name(), loc.function_name(), loc.line(), message);
|
||
|
}
|
||
|
|
||
|
void sentinel(const string &message, std::source_location loc = std::source_location::current()) {
|
||
|
string err = fmt::format("[SENTINEL! {}:{}:{}:{}] {}\n",
|
||
|
loc.file_name(), loc.function_name(),
|
||
|
loc.line(), loc.column(), message);
|
||
|
throw SentinelError{err};
|
||
|
}
|
||
|
|
||
|
void pre(const string &message, std::function<bool()> tester, std::source_location loc = std::source_location::current()) {
|
||
|
if(!tester()) {
|
||
|
string err = fmt::format("[PRE! {}:{}:{}:{}] {}\n",
|
||
|
loc.file_name(), loc.function_name(),
|
||
|
loc.line(), loc.column(), message);
|
||
|
throw PreCondError{err};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void post(const string &message, std::function<bool()> tester, std::source_location loc = std::source_location::current()) {
|
||
|
if(!tester()) {
|
||
|
string err = fmt::format("[POST! {}:{}:{}:{}] {}\n",
|
||
|
loc.file_name(), loc.function_name(),
|
||
|
loc.line(), loc.column(), message);
|
||
|
throw PostCondError{err};
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void check(bool test, const string &message, std::source_location loc = std::source_location::current()) {
|
||
|
|
||
|
if(!test) {
|
||
|
string err = fmt::format("[CHECK! {}:{}:{}:{}] {}\n",
|
||
|
loc.file_name(), loc.function_name(),
|
||
|
loc.line(), loc.column(), message);
|
||
|
throw CheckError{err};
|
||
|
}
|
||
|
}
|
||
|
}
|