Removed dbc and replaced with plain asserts everywhere.

main
Zed A. Shaw 4 hours ago
parent 767147c301
commit adc192c6dc
  1. 2
      demos/calc.cpp
  2. 50
      include/guecs/dbc.hpp
  3. 2
      include/guecs/sfml/backend.hpp
  4. 1
      include/guecs/sfml/components.hpp
  5. 4
      include/guecs/ui.hpp
  6. 1
      meson.build
  7. 47
      src/guecs/dbc.cpp
  8. 13
      src/guecs/lel.cpp
  9. 48
      src/guecs/lel_parser.cpp
  10. 2
      src/guecs/lel_parser.rl
  11. 3
      src/guecs/sfml/components.cpp
  12. 8
      src/guecs/sfml/config.cpp
  13. 17
      src/guecs/sfml/shaders.cpp
  14. 9
      src/guecs/sfml/sound.cpp
  15. 17
      src/guecs/sfml/textures.cpp
  16. 12
      src/guecs/ui.cpp
  17. 18
      tests/dbc.cpp

@ -76,7 +76,7 @@ struct Calculator {
temp = left / right;
break;
default:
dbc::sentinel("invalid op passed to op()");
assert(true && "invalid op passed to op()");
}
stack.push_back(temp);

@ -1,50 +0,0 @@
#pragma once
#include <string>
#include <fmt/core.h>
#include <functional>
#include <source_location>
using std::string;
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,
const std::source_location location =
std::source_location::current());
[[noreturn]] void sentinel(const string &message,
const std::source_location location =
std::source_location::current());
void pre(const string &message, bool test,
const std::source_location location =
std::source_location::current());
void pre(const string &message, std::function<bool()> tester,
const std::source_location location =
std::source_location::current());
void post(const string &message, bool test,
const std::source_location location =
std::source_location::current());
void post(const string &message, std::function<bool()> tester,
const std::source_location location =
std::source_location::current());
void check(bool test, const string &message,
const std::source_location location =
std::source_location::current());
}

@ -1,6 +1,8 @@
#include "guecs/ui.hpp"
namespace sfml {
using std::string;
class Backend : public guecs::Backend {
int $shaders_version = 0;

@ -1,6 +1,5 @@
#pragma once
#include <SFML/Graphics.hpp>
#include "guecs/dbc.hpp"
#include "guecs/lel.hpp"
#include <string>
#include <memory>

@ -1,5 +1,4 @@
#pragma once
#include "guecs/dbc.hpp"
#include "guecs/lel.hpp"
#include <string>
#include <memory>
@ -10,6 +9,7 @@
#include <unordered_map>
#include "guecs/theme.hpp"
#include "guecs/sfml/components.hpp"
#include <cassert>
namespace guecs {
using std::shared_ptr, std::wstring, std::string;
@ -177,7 +177,7 @@ namespace guecs {
template <typename Comp>
void set_init(Entity ent, Comp val) {
dbc::check(has<lel::Cell>(ent),"WRONG! slot is missing its cell?!");
assert(has<lel::Cell>(ent) && "WRONG! slot is missing its cell?!");
auto& cell = get<lel::Cell>(ent);
val.init(cell);
set<Comp>(ent, val);

@ -70,7 +70,6 @@ dependencies += [
sources = [
'src/guecs/dbc.cpp',
'src/guecs/ui.cpp',
'src/guecs/lel.cpp',
'src/guecs/theme.cpp',

@ -1,47 +0,0 @@
#include "guecs/dbc.hpp"
#include <iostream>
void dbc::log(const string &message, const std::source_location location) {
std::cout << '[' << location.file_name() << ':'
<< location.line() << "|"
<< location.function_name() << "] "
<< message << std::endl;
}
void dbc::sentinel(const string &message, const std::source_location location) {
string err = fmt::format("[SENTINEL!] {}", message);
dbc::log(err, location);
throw dbc::SentinelError{err};
}
void dbc::pre(const string &message, bool test, const std::source_location location) {
if(!test) {
string err = fmt::format("[PRE!] {}", message);
dbc::log(err, location);
throw dbc::PreCondError{err};
}
}
void dbc::pre(const string &message, std::function<bool()> tester, const std::source_location location) {
dbc::pre(message, tester(), location);
}
void dbc::post(const string &message, bool test, const std::source_location location) {
if(!test) {
string err = fmt::format("[POST!] {}", message);
dbc::log(err, location);
throw dbc::PostCondError{err};
}
}
void dbc::post(const string &message, std::function<bool()> tester, const std::source_location location) {
dbc::post(message, tester(), location);
}
void dbc::check(bool test, const string &message, const std::source_location location) {
if(!test) {
string err = fmt::format("[CHECK!] {}\n", message);
dbc::log(err, location);
throw dbc::CheckError{err};
}
}

@ -1,7 +1,7 @@
#include "guecs/lel.hpp"
#include <fmt/core.h>
#include "guecs/dbc.hpp"
#include <numeric>
#include <cassert>
#include "./lel_parser.cpp"
@ -27,8 +27,7 @@ namespace lel {
void Parser::id(std::string name) {
if(name != "_") {
dbc::check(!cells.contains(name),
fmt::format("duplicate cell name {}", name));
assert(!cells.contains(name) && "duplicate cell name");
cells.insert_or_assign(name, cur);
}
@ -44,8 +43,8 @@ namespace lel {
for(auto& row : grid) {
size_t columns = row.size();
int cell_width = grid_w / columns;
dbc::check(cell_width > 0, "invalid cell width calc");
dbc::check(cell_height > 0, "invalid cell height calc");
assert(cell_width > 0 && "invalid cell width calc");
assert(cell_height > 0 && "invalid cell height calc");
for(auto& name : row) {
if(name == "_") continue;
@ -66,8 +65,8 @@ namespace lel {
cell.w = cell.expand ? std::min(cell.max_w, grid_w) : std::min(cell_width, cell.max_w);
cell.h = cell.expand ? std::min(cell.max_h, grid_h) : std::min(cell_height, cell.max_h);
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
assert(cell.h > 0 && "invalid height cell is <= 0");
assert(cell.w > 0 && "invalid width cell is <= 0");
cell.x = grid_x + (cell.col * cell_width);
cell.y = grid_y + (cell.row * cell_height);

@ -1,5 +1,5 @@
#line 1 ".\\src\\guecs\\lel_parser.rl"
#line 1 "src/guecs/lel_parser.rl"
#include "guecs/lel.hpp"
#include <fmt/core.h>
#include <iostream>
@ -7,11 +7,11 @@
namespace lel {
#line 41 ".\\src\\guecs\\lel_parser.rl"
#line 41 "src/guecs/lel_parser.rl"
#line 10 ".\\src\\guecs\\lel_parser.cpp"
#line 10 "src/guecs/lel_parser.cpp"
static const char _Parser_actions[] = {
0, 1, 1, 1, 2, 1, 3, 1,
4, 1, 5, 1, 6, 1, 9, 1,
@ -84,7 +84,7 @@ static const int Parser_error = 0;
static const int Parser_en_main = 1;
#line 44 ".\\src\\guecs\\lel_parser.rl"
#line 44 "src/guecs/lel_parser.rl"
bool Parser::parse(std::string input) {
reset();
@ -96,14 +96,14 @@ bool Parser::parse(std::string input) {
std::string tk;
#line 91 ".\\src\\guecs\\lel_parser.cpp"
#line 91 "src/guecs/lel_parser.cpp"
{
cs = Parser_start;
}
#line 55 ".\\src\\guecs\\lel_parser.rl"
#line 55 "src/guecs/lel_parser.rl"
#line 94 ".\\src\\guecs\\lel_parser.cpp"
#line 94 "src/guecs/lel_parser.cpp"
{
int _klen;
unsigned int _trans;
@ -178,62 +178,62 @@ _match:
switch ( *_acts++ )
{
case 0:
#line 11 ".\\src\\guecs\\lel_parser.rl"
#line 11 "src/guecs/lel_parser.rl"
{tk = input.substr(start - begin, p - start); }
break;
case 1:
#line 13 ".\\src\\guecs\\lel_parser.rl"
#line 13 "src/guecs/lel_parser.rl"
{}
break;
case 2:
#line 14 ".\\src\\guecs\\lel_parser.rl"
#line 14 "src/guecs/lel_parser.rl"
{ grid.push_back(Row()); }
break;
case 3:
#line 15 ".\\src\\guecs\\lel_parser.rl"
#line 15 "src/guecs/lel_parser.rl"
{ cur.bottom = (*p) == '.'; }
break;
case 4:
#line 16 ".\\src\\guecs\\lel_parser.rl"
#line 16 "src/guecs/lel_parser.rl"
{ id(input.substr(start - begin, p - start)); }
break;
case 5:
#line 17 ".\\src\\guecs\\lel_parser.rl"
#line 17 "src/guecs/lel_parser.rl"
{ cur.col = 0; cur.row++; }
break;
case 6:
#line 18 ".\\src\\guecs\\lel_parser.rl"
#line 18 "src/guecs/lel_parser.rl"
{ cur.right = (*p) == '>'; }
break;
case 7:
#line 19 ".\\src\\guecs\\lel_parser.rl"
#line 19 "src/guecs/lel_parser.rl"
{cur.max_w = std::stoi(tk); }
break;
case 8:
#line 20 ".\\src\\guecs\\lel_parser.rl"
#line 20 "src/guecs/lel_parser.rl"
{ cur.max_h = std::stoi(tk); }
break;
case 9:
#line 21 ".\\src\\guecs\\lel_parser.rl"
#line 21 "src/guecs/lel_parser.rl"
{ cur.expand = true; }
break;
case 10:
#line 22 ".\\src\\guecs\\lel_parser.rl"
#line 22 "src/guecs/lel_parser.rl"
{ cur.center = true; }
break;
case 11:
#line 23 ".\\src\\guecs\\lel_parser.rl"
#line 23 "src/guecs/lel_parser.rl"
{ cur.percent = true; }
break;
case 12:
#line 33 ".\\src\\guecs\\lel_parser.rl"
#line 33 "src/guecs/lel_parser.rl"
{ start = p; }
break;
case 13:
#line 36 ".\\src\\guecs\\lel_parser.rl"
#line 36 "src/guecs/lel_parser.rl"
{start = p;}
break;
#line 209 ".\\src\\guecs\\lel_parser.cpp"
#line 209 "src/guecs/lel_parser.cpp"
}
}
@ -246,13 +246,13 @@ _again:
_out: {}
}
#line 56 ".\\src\\guecs\\lel_parser.rl"
#line 56 "src/guecs/lel_parser.rl"
bool good = pe - p == 0;
if(good) {
finalize();
} else {
dbc::log("error at:");
std::cout << "error at:";
std::cout << p;
}
return good;

@ -58,7 +58,7 @@ bool Parser::parse(std::string input) {
if(good) {
finalize();
} else {
dbc::log("error at:");
std::cout << "error at:";
std::cout << p;
}
return good;

@ -1,11 +1,12 @@
#include "guecs/ui.hpp"
#include "guecs/sfml/backend.hpp"
#include <cassert>
namespace guecs {
using std::make_shared;
void Textual::init(lel::Cell &cell, shared_ptr<sf::Font> font_ptr) {
dbc::check(font_ptr != nullptr, "you failed to initialize this WideText");
assert(font_ptr != nullptr && "you failed to initialize this WideText");
if(font == nullptr) font = font_ptr;
if(text == nullptr) text = make_shared<sf::Text>(*font, content, size);
text->setFillColor(color);

@ -1,5 +1,4 @@
#include "guecs/sfml/config.hpp"
#include "guecs/dbc.hpp"
#include <fmt/core.h>
using nlohmann::json;
@ -11,13 +10,14 @@ Config::Config(const std::string src_path) : $src_path(src_path) {
}
json &Config::operator[](const std::string &key) {
dbc::check($config.contains(key), fmt::format("ERROR in config, key {} doesn't exist.", key));
assert($config.contains(key) &&
"ERROR in config, key doesn't exist.");
return $config[key];
}
std::wstring Config::wstring(const std::string main_key, const std::string sub_key) {
dbc::check($config.contains(main_key), fmt::format("ERROR wstring main/key in config, main_key {} doesn't exist.", main_key));
dbc::check($config[main_key].contains(sub_key), fmt::format("ERROR wstring in config, main_key/key {}/{} doesn't exist.", main_key, sub_key));
assert($config.contains(main_key) && "ERROR wstring main/key in config, main_key doesn't exist.");
assert($config[main_key].contains(sub_key) && "ERROR wstring in config, main_key/key doesn't exist.");
const std::string& str_val = $config[main_key][sub_key];
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> $converter;

@ -1,6 +1,5 @@
#include "guecs/sfml/shaders.hpp"
#include "guecs/sfml/config.hpp"
#include "guecs/dbc.hpp"
#include <SFML/Graphics/Image.hpp>
#include <fmt/core.h>
#include <memory>
@ -30,21 +29,21 @@ namespace shaders {
void init() {
if(!INITIALIZED) {
dbc::check(sf::Shader::isAvailable(), "no shaders?!");
assert(sf::Shader::isAvailable() && "no shaders?!");
INITIALIZED = true;
Config config("assets/shaders.json");
bool good = load_shader("ERROR", config["ERROR"]);
dbc::check(good, "Failed to load ERROR shader. Look in assets/shaders.json");
assert(good && "Failed to load ERROR shader. Look in assets/shaders.json");
for(auto& [name, settings] : config.json().items()) {
if(name == "ERROR") continue;
dbc::check(!SMGR.shaders.contains(name),
fmt::format("shader name '{}' duplicated in assets/shaders.json", name));
assert(!SMGR.shaders.contains(name) &&
"shader name duplicated in assets/shaders.json");
good = load_shader(name, settings);
if(!good) {
dbc::log(fmt::format("failed to load shader {}", name));
fmt::println("[SOUND] failed to load shader {}", name);
SMGR.shaders.insert_or_assign(name, SMGR.shaders.at("ERROR"));
}
}
@ -52,9 +51,9 @@ namespace shaders {
}
std::shared_ptr<sf::Shader> get(const std::string& name) {
dbc::check(INITIALIZED, "you forgot to shaders::init()");
dbc::check(SMGR.shaders.contains(name),
fmt::format("shader name '{}' not in assets/shaders.json", name));
assert(INITIALIZED && "you forgot to shaders::init()");
assert(SMGR.shaders.contains(name) &&
"shader name not in assets/shaders.json");
auto& rec = SMGR.shaders.at(name);
return rec.ptr;
}

@ -1,7 +1,7 @@
#include "guecs/sfml/sound.hpp"
#include "guecs/sfml/config.hpp"
#include "guecs/dbc.hpp"
#include <fmt/core.h>
#include <cassert>
namespace sound {
static SoundManager SMGR;
@ -13,14 +13,13 @@ namespace sound {
namespace fs = std::filesystem;
SoundPair& get_sound_pair(const std::string& name) {
dbc::check(initialized, "You need to call sound::init() first");
assert(initialized && "You need to call sound::init() first");
if(SMGR.sounds.contains(name)) {
// get the sound from the sound map
return SMGR.sounds.at(name);
} else {
dbc::log(fmt::format("Attempted to stop {} sound but not available.",
name));
fmt::println("[SOUND] Attempted to stop {} sound but not available.", name);
return SMGR.sounds.at("blank");
}
}
@ -38,7 +37,7 @@ namespace sound {
}
void load(const std::string& name, const std::string& sound_path) {
dbc::check(fs::exists(sound_path), fmt::format("sound file {} does not exist", sound_path));
assert(fs::exists(sound_path) && "sound file does not exist");
// create the buffer and keep in the buffer map
auto buffer = make_shared<sf::SoundBuffer>(sound_path);

@ -1,4 +1,3 @@
#include "guecs/dbc.hpp"
#include "guecs/sfml/textures.hpp"
#include "guecs/sfml/config.hpp"
#include <SFML/Graphics/Image.hpp>
@ -36,16 +35,16 @@ namespace textures {
}
SpriteTexture get(const std::string& name) {
dbc::check(initialized, "you forgot to call textures::init()");
dbc::check(TMGR.sprite_textures.contains(name),
fmt::format("!!!!! texture pack does not contain {} sprite", name));
assert(initialized && "you forgot to call textures::init()");
assert(TMGR.sprite_textures.contains(name) &&
"!!!!! texture pack does not contain sprite");
auto result = TMGR.sprite_textures.at(name);
dbc::check(result.sprite != nullptr,
fmt::format("bad sprite from textures::get named {}", name));
dbc::check(result.texture != nullptr,
fmt::format("bad texture from textures::get named {}", name));
assert(result.sprite != nullptr &&
"bad sprite from textures::get named can't be null");
assert(result.texture != nullptr &&
"bad texture from textures::get named can't be null");
return result;
}
@ -53,7 +52,7 @@ namespace textures {
sf::Image load_image(const std::string& filename) {
sf::Image texture;
bool good = texture.loadFromFile(filename);
dbc::check(good, fmt::format("failed to load {}", filename));
assert(good && "failed to load image file");
return texture;
}
};

@ -1,5 +1,7 @@
#include "guecs/ui.hpp"
#include <typeinfo>
#include <cassert>
#include <fmt/core.h>
namespace guecs {
using std::make_shared;
@ -23,7 +25,7 @@ namespace guecs {
void UI::layout(const string& grid) {
$grid = grid;
bool good = $parser.parse($grid);
dbc::check(good, "LEL parsing failed.");
assert(good && "LEL parsing failed.");
for(auto& [name, cell] : $parser.cells) {
auto ent = init_entity(name);
@ -41,8 +43,8 @@ namespace guecs {
}
Entity UI::entity(const string& name) {
dbc::check($name_ents.contains(name),
fmt::format("GUECS entity {} does not exist. Mispelled cell name?", name));
assert($name_ents.contains(name) &&
"GUECS entity does not exist. Mispelled cell name?");
return $name_ents.at(name);
}
@ -190,8 +192,8 @@ namespace guecs {
auto ent = entity(name);
if(required) {
dbc::check(has<Clickable>(ent),
fmt::format("click_on required '{}' to exist but it doesn't", name));
assert(has<Clickable>(ent) &&
"click_on required '{}' to exist but it doesn't");
}
click_on(ent);

@ -1,18 +0,0 @@
#include <catch2/catch_test_macros.hpp>
#include "guecs/dbc.hpp"
using namespace dbc;
TEST_CASE("basic feature tests", "[dbc]") {
log("Logging a message.");
pre("confirm positive cases work", 1 == 1);
pre("confirm positive lambda", [&]{ return 1 == 1;});
post("confirm positive post", 1 == 1);
post("confirm postitive post with lamdba", [&]{ return 1 == 1;});
check(1 == 1, "one equals 1");
}
Loading…
Cancel
Save