A weird game.
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.
turings-tarpit/dbc.hpp

51 lines
1.1 KiB

#include <string>
#include <fmt/core.h>
#include <functional>
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) {
fmt::print("{}\n", message);
}
void sentinel(const string &message) {
string err = fmt::format("[SENTINEL!] {}\n", message);
throw SentinelError{err};
}
void pre(const string &message, std::function<bool()> tester) {
if(!tester()) {
string err = fmt::format("[PRE!] {}\n", message);
throw PreCondError{err};
}
}
void post(const string &message, std::function<bool()> tester) {
if(!tester()) {
string err = fmt::format("[POST!] {}\n", message);
throw PostCondError{err};
}
}
void check(bool test, const string &message) {
if(!test) {
string err = fmt::format("[CHECK!] {}\n", message);
throw CheckError{err};
}
}
}