.clang-format

This commit is contained in:
NukeBird 2025-02-13 19:47:52 +03:00
parent 432673fa0e
commit d89827046a
6 changed files with 295 additions and 237 deletions

4
.clang-format Normal file
View file

@ -0,0 +1,4 @@
BasedOnStyle: LLVM
BreakBeforeBraces: Allman
AccessModifierOffset: -4
IndentWidth: 4

30
dbc.cpp
View file

@ -1,42 +1,52 @@
#include "dbc.hpp"
#include <iostream>
void dbc::log(const string &message) {
void dbc::log(const string &message)
{
std::cerr << "!!!!!!!!!!" << message << std::endl;
}
void dbc::sentinel(const string &message) {
void dbc::sentinel(const string &message)
{
string err = fmt::format("[SENTINEL!] {}", message);
dbc::log(err);
throw dbc::SentinelError{err};
}
void dbc::pre(const string &message, bool test) {
if(!test) {
void dbc::pre(const string &message, bool test)
{
if (!test)
{
string err = fmt::format("[PRE!] {}", message);
dbc::log(err);
throw dbc::PreCondError{err};
}
}
void dbc::pre(const string &message, std::function<bool()> tester) {
void dbc::pre(const string &message, std::function<bool()> tester)
{
dbc::pre(message, tester());
}
void dbc::post(const string &message, bool test) {
if(!test) {
void dbc::post(const string &message, bool test)
{
if (!test)
{
string err = fmt::format("[POST!] {}", message);
dbc::log(err);
throw dbc::PostCondError{err};
}
}
void dbc::post(const string &message, std::function<bool()> tester) {
void dbc::post(const string &message, std::function<bool()> tester)
{
dbc::post(message, tester());
}
void dbc::check(bool test, const string &message) {
if(!test) {
void dbc::check(bool test, const string &message)
{
if (!test)
{
string err = fmt::format("[CHECK!] {}\n", message);
dbc::log(err);
throw dbc::CheckError{err};

26
dbc.hpp
View file

@ -1,23 +1,33 @@
#pragma once
#include <string>
#include <fmt/core.h>
#include <functional>
#include <string>
using std::string;
namespace dbc {
class Error {
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 {};
class CheckError : public Error
{
};
class SentinelError : public Error
{
};
class PreCondError : public Error
{
};
class PostCondError : public Error
{
};
void log(const string &message);
void sentinel(const string &message);
@ -26,4 +36,4 @@ namespace dbc {
void post(const string &message, bool test);
void post(const string &message, std::function<bool()> tester);
void check(bool test, const string &message);
}
} // namespace dbc

View file

@ -1,20 +1,22 @@
#pragma once
#include "dbc.hpp"
#include <any>
#include <functional>
#include <queue>
#include <tuple>
#include <typeindex>
#include <typeinfo>
#include <unordered_map>
#include <any>
#include <tuple>
#include <queue>
#include "dbc.hpp"
namespace DinkyECS {
namespace DinkyECS
{
typedef unsigned long Entity;
using EntityMap = std::unordered_map<Entity, std::any>;
struct Event {
struct Event
{
int event = 0;
Entity entity = 0;
std::any data;
@ -22,110 +24,121 @@ namespace DinkyECS {
typedef std::queue<Event> EventQueue;
struct World {
struct World
{
unsigned long entity_count = 0;
std::unordered_map<std::type_index, EntityMap> $components;
std::unordered_map<std::type_index, std::any> $facts;
std::unordered_map<std::type_index, EventQueue> $events;
std::vector<Entity> $constants;
Entity entity() {
return ++entity_count;
}
Entity entity() { return ++entity_count; }
void clone_into(DinkyECS::World &to_world) {
void clone_into(DinkyECS::World &to_world)
{
to_world.$constants = $constants;
to_world.$facts = $facts;
to_world.entity_count = entity_count;
for(auto eid : $constants) {
for(const auto &[tid, eid_map] : $components) {
for (auto eid : $constants)
{
for (const auto &[tid, eid_map] : $components)
{
auto &their_map = to_world.$components[tid];
if(eid_map.contains(eid)) {
if (eid_map.contains(eid))
{
their_map.insert_or_assign(eid, eid_map.at(eid));
}
}
}
}
void make_constant(DinkyECS::Entity entity) {
void make_constant(DinkyECS::Entity entity)
{
$constants.push_back(entity);
}
template <typename Comp>
EntityMap& entity_map_for() {
template <typename Comp> EntityMap &entity_map_for()
{
return $components[std::type_index(typeid(Comp))];
}
template <typename Comp>
EventQueue& queue_map_for() {
template <typename Comp> EventQueue &queue_map_for()
{
return $events[std::type_index(typeid(Comp))];
}
template <typename Comp>
void remove(Entity ent) {
template <typename Comp> void remove(Entity ent)
{
EntityMap &map = entity_map_for<Comp>();
map.erase(ent);
}
template <typename Comp>
void set_the(Comp val) {
template <typename Comp> void set_the(Comp val)
{
$facts.insert_or_assign(std::type_index(typeid(Comp)), val);
}
template <typename Comp>
Comp &get_the() {
template <typename Comp> Comp &get_the()
{
auto comp_id = std::type_index(typeid(Comp));
dbc::check($facts.contains(comp_id),
fmt::format("!!!! ATTEMPT to access world fact that hasn't been set yet: {}", typeid(Comp).name()));
fmt::format("!!!! ATTEMPT to access world fact that hasn't "
"been set yet: {}",
typeid(Comp).name()));
// use .at to get std::out_of_range if fact not set
std::any &res = $facts.at(comp_id);
return std::any_cast<Comp &>(res);
}
template <typename Comp>
bool has_the() {
template <typename Comp> bool has_the()
{
auto comp_id = std::type_index(typeid(Comp));
return $facts.contains(comp_id);
}
template <typename Comp>
void set(Entity ent, Comp val) {
template <typename Comp> void set(Entity ent, Comp val)
{
EntityMap &map = entity_map_for<Comp>();
map.insert_or_assign(ent, val);
}
template <typename Comp>
Comp &get(Entity ent) {
template <typename Comp> Comp &get(Entity ent)
{
EntityMap &map = entity_map_for<Comp>();
// use .at for bounds checking
std::any &res = map.at(ent);
return std::any_cast<Comp &>(res);
}
template <typename Comp>
bool has(Entity ent) {
template <typename Comp> bool has(Entity ent)
{
EntityMap &map = entity_map_for<Comp>();
return map.contains(ent);
}
template <typename Comp>
void query(std::function<void(const Entity&, Comp&)> cb) {
void query(std::function<void(const Entity &, Comp &)> cb)
{
EntityMap &map = entity_map_for<Comp>();
for(auto& [entity, any_comp] : map) {
for (auto &[entity, any_comp] : map)
{
Comp &res = std::any_cast<Comp &>(any_comp);
cb(entity, res);
}
}
template <typename CompA, typename CompB>
void query(std::function<void(const Entity&, CompA&, CompB&)> cb) {
void query(std::function<void(const Entity &, CompA &, CompB &)> cb)
{
EntityMap &map_a = entity_map_for<CompA>();
EntityMap &map_b = entity_map_for<CompB>();
for(auto& [entity, any_a] : map_a) {
if(map_b.contains(entity)) {
for (auto &[entity, any_a] : map_a)
{
if (map_b.contains(entity))
{
CompA &res_a = std::any_cast<CompA &>(any_a);
CompB &res_b = get<CompB>(entity);
cb(entity, res_a, res_b);
@ -133,24 +146,24 @@ namespace DinkyECS {
}
}
template<typename Comp>
void send(Comp event, Entity entity, std::any data) {
template <typename Comp> void send(Comp event, Entity entity, std::any data)
{
EventQueue &queue = queue_map_for<Comp>();
queue.push({event, entity, data});
}
template<typename Comp>
Event recv() {
template <typename Comp> Event recv()
{
EventQueue &queue = queue_map_for<Comp>();
Event evt = queue.front();
queue.pop();
return evt;
}
template<typename Comp>
bool has_event() {
template <typename Comp> bool has_event()
{
EventQueue &queue = queue_map_for<Comp>();
return !queue.empty();
}
};
}
} // namespace DinkyECS

View file

@ -10,19 +10,22 @@ using namespace fmt;
using DinkyECS::Entity;
using std::string;
struct Player {
struct Player
{
string name;
Entity eid;
};
struct Position {
struct Position
{
Point location;
};
// DINKY_HAS_COMPONENT(Point, x, y);
// DINKY_HAS_COMPONENT(Position, location);
struct Motion {
struct Motion
{
int dx;
int dy;
bool random = false;
@ -30,23 +33,27 @@ struct Motion {
// DINKY_HAS_COMPONENT(Motion, dx, dy, random);
struct Velocity {
struct Velocity
{
double x, y;
};
// DINKY_HAS_COMPONENT(Velocity, x, y);
struct Gravity {
struct Gravity
{
double level;
};
// DINKY_HAS_COMPONENT(Gravity, level);
struct DaGUI {
struct DaGUI
{
int event;
};
void configure(DinkyECS::World &world, Entity &test) {
void configure(DinkyECS::World &world, Entity &test)
{
println("---Configuring the base system.");
Entity test2 = world.entity();
@ -74,7 +81,8 @@ void configure(DinkyECS::World &world, Entity &test) {
world.set_the<Gravity>({0.9});
}
int main() {
int main()
{
DinkyECS::World world;
Entity test = world.entity();
@ -88,20 +96,26 @@ int main() {
REQUIRE(vel.x == 1);
REQUIRE(vel.y == 2);
world.query<Position>([](const auto &ent, auto &pos) {
world.query<Position>(
[](const auto &ent, auto &pos)
{
REQUIRE(ent > 0);
REQUIRE(pos.location.x >= 0);
REQUIRE(pos.location.y >= 0);
});
world.query<Velocity>([](const auto &ent, auto &vel) {
world.query<Velocity>(
[](const auto &ent, auto &vel)
{
REQUIRE(ent > 0);
REQUIRE(vel.x >= 0);
REQUIRE(vel.y >= 0);
});
println("--- Manually get the velocity in position system:");
world.query<Position>([&](const auto &ent, auto &pos) {
world.query<Position>(
[&](const auto &ent, auto &pos)
{
Velocity &vel = world.get<Velocity>(ent);
REQUIRE(ent > 0);
@ -113,7 +127,9 @@ int main() {
});
println("--- Query only entities with Position and Velocity:");
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
world.query<Position, Velocity>(
[&](const auto &ent, auto &pos, auto &vel)
{
Gravity &grav = world.get_the<Gravity>();
REQUIRE(grav.level <= 1.0f);
REQUIRE(grav.level > 0.5f);
@ -132,7 +148,9 @@ int main() {
REQUIRE(!world.has<Velocity>(test));
println("--- After remove test, should only result in test2:");
world.query<Position, Velocity>([&](const auto &ent, auto &pos, auto &vel) {
world.query<Position, Velocity>(
[&](const auto &ent, auto &pos, auto &vel)
{
auto &in_position = world.get<Position>(ent);
auto &in_velocity = world.get<Velocity>(ent);
REQUIRE(pos.location.x >= 0);

View file

@ -1,20 +1,23 @@
#pragma once
#include <vector>
struct Point {
struct Point
{
size_t x = 0;
size_t y = 0;
bool operator==(const Point& other) const {
bool operator==(const Point &other) const
{
return other.x == x && other.y == y;
}
};
typedef std::vector<Point> PointList;
template<> struct std::hash<Point> {
size_t operator()(const Point& p) const {
template <> struct std::hash<Point>
{
size_t operator()(const Point &p) const
{
auto hasher = std::hash<int>();
return hasher(p.x) ^ hasher(p.y);
}