detail::type_family

This commit is contained in:
2018-12-24 21:19:45 +07:00
parent bec0291cf2
commit 5644ac5bae
4 changed files with 66 additions and 10 deletions

View File

@@ -1,3 +1,4 @@
ignore: ignore:
- catch.hpp - "catch.hpp"
- catch_main.hpp - "catch_main.hpp"
- "*_tests.cpp"

44
ecs.hpp
View File

@@ -7,11 +7,14 @@
#pragma once #pragma once
#include <cassert> #include <cassert>
#include <cstddef>
#include <cstdint> #include <cstdint>
#include <mutex> #include <mutex>
#include <limits> #include <limits>
#include <utility>
#include <functional> #include <functional>
#include <type_traits>
#include <unordered_set> #include <unordered_set>
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
@@ -25,7 +28,46 @@ namespace ecs_hpp
class world; class world;
class entity; class entity;
using entity_id = std::uint64_t; using family_id = std::uint16_t;
using entity_id = std::uint32_t;
static_assert(
std::is_unsigned<family_id>::value,
"ecs_hpp::family_id must be an unsigned integer");
static_assert(
std::is_unsigned<entity_id>::value,
"ecs_hpp::entity_id must be an unsigned integer");
}
// -----------------------------------------------------------------------------
//
// detail
//
// -----------------------------------------------------------------------------
namespace ecs_hpp
{
namespace detail
{
template < typename Tag = void >
struct type_family_base {
static family_id last_id_;
};
template < typename T >
class type_family : private type_family_base<> {
public:
static family_id id() noexcept {
static family_id self_id = ++last_id_;
assert(self_id > 0u && "ecs_hpp::family_id overflow");
return self_id;
}
};
template < typename Tag >
family_id type_family_base<Tag>::last_id_{0u};
}
} }
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------

View File

@@ -12,23 +12,36 @@ namespace ecs = ecs_hpp;
namespace namespace
{ {
struct position { struct position_c {
int x{0}; int x{0};
int y{0}; int y{0};
position() = default; position_c() = default;
position(int nx, int ny) : x(nx), y(ny) {} position_c(int nx, int ny) : x(nx), y(ny) {}
}; };
struct velocity { struct velocity_c {
int dx{0}; int dx{0};
int dy{0}; int dy{0};
velocity() = default; velocity_c() = default;
velocity(int ndx, int ndy) : dx(ndx), dy(ndy) {} velocity_c(int ndx, int ndy) : dx(ndx), dy(ndy) {}
}; };
} }
TEST_CASE("detail") {
SECTION("get_type_id") {
REQUIRE(ecs::detail::type_family<position_c>::id() == 1u);
REQUIRE(ecs::detail::type_family<position_c>::id() == 1u);
REQUIRE(ecs::detail::type_family<velocity_c>::id() == 2u);
REQUIRE(ecs::detail::type_family<velocity_c>::id() == 2u);
REQUIRE(ecs::detail::type_family<position_c>::id() == 1u);
REQUIRE(ecs::detail::type_family<velocity_c>::id() == 2u);
}
}
TEST_CASE("world") { TEST_CASE("world") {
SECTION("entities") { SECTION("entities") {
{ {

View File

@@ -11,7 +11,7 @@ lcov -d . -z
ctest --verbose ctest --verbose
lcov -d . -c -o "coverage.info" lcov -d . -c -o "coverage.info"
lcov -r "coverage.info" "*/usr/*" "*/catch.hpp" "*/catch_main.cpp" -o "coverage.info" lcov -r "coverage.info" "*/usr/*" "*/catch.hpp" "*/catch_main.cpp" "*_tests.cpp" -o "coverage.info"
lcov -l "coverage.info" lcov -l "coverage.info"
bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports" bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"