example: custom identifier (and some fixes for the purpose)

This commit is contained in:
Michele Caini
2020-07-02 15:08:22 +02:00
parent 2c7455ea2b
commit cec1b932bd
8 changed files with 86 additions and 26 deletions

View File

@@ -20,7 +20,7 @@ jobs:
env:
CXX: ${{ matrix.compiler }}
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ..
cmake -DBUILD_LIB=ON -DBUILD_EXAMPLE=ON ..
make -j4
- name: Run tests
working-directory: build
@@ -49,7 +49,7 @@ jobs:
- name: Compile tests
working-directory: build
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ${{ matrix.toolset_option }} ..
cmake -DBUILD_LIB=ON -DBUILD_EXAMPLE=ON ${{ matrix.toolset_option }} ..
cmake --build . -j 4
- name: Run tests
working-directory: build
@@ -66,7 +66,7 @@ jobs:
- name: Compile tests
working-directory: build
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ..
cmake -DBUILD_LIB=ON -DBUILD_EXAMPLE=ON ..
make -j4
- name: Run tests
working-directory: build

View File

@@ -16,7 +16,7 @@ jobs:
CXXFLAGS: "-O0 --coverage -fno-inline -fno-inline-small-functions -fno-default-inline"
CXX: g++
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ..
cmake -DBUILD_LIB=ON -DBUILD_EXAMPLE=ON ..
make -j4
- name: Run tests
working-directory: build

View File

@@ -148,13 +148,14 @@ export(PACKAGE EnTT)
# Tests
#
option(BUILD_TESTING "Enable testing with ctest." OFF)
include(CTest)
if(BUILD_TESTING)
option(FIND_GTEST_PACKAGE "Enable finding gtest package." OFF)
option(BUILD_BENCHMARK "Build benchmark." OFF)
option(BUILD_LIB "Build lib example." OFF)
option(BUILD_SNAPSHOT "Build snapshot example." OFF)
option(BUILD_EXAMPLE "Build examples." OFF)
option(BUILD_LIB "Build lib tests." OFF)
option(BUILD_SNAPSHOT "Build snapshot test with Cereal." OFF)
enable_testing()
add_subdirectory(test)

View File

@@ -2,6 +2,7 @@
#define ENTT_ENTITY_ENTITY_HPP
#include <cstddef>
#include <cstdint>
#include <type_traits>
#include "../config/config.h"
@@ -48,11 +49,11 @@ struct entt_traits<std::uint16_t> {
using difference_type = std::int32_t;
/*! @brief Mask to use to get the entity number out of an identifier. */
static constexpr std::uint16_t entity_mask = 0xFFF;
static constexpr entity_type entity_mask = 0xFFF;
/*! @brief Mask to use to get the version out of an identifier. */
static constexpr std::uint16_t version_mask = 0xF;
static constexpr entity_type version_mask = 0xF;
/*! @brief Extent of the entity number within an identifier. */
static constexpr auto entity_shift = 12;
static constexpr std::size_t entity_shift = 12u;
};
@@ -74,11 +75,11 @@ struct entt_traits<std::uint32_t> {
using difference_type = std::int64_t;
/*! @brief Mask to use to get the entity number out of an identifier. */
static constexpr std::uint32_t entity_mask = 0xFFFFF;
static constexpr entity_type entity_mask = 0xFFFFF;
/*! @brief Mask to use to get the version out of an identifier. */
static constexpr std::uint32_t version_mask = 0xFFF;
static constexpr entity_type version_mask = 0xFFF;
/*! @brief Extent of the entity number within an identifier. */
static constexpr auto entity_shift = 20;
static constexpr std::size_t entity_shift = 20u;
};
@@ -100,11 +101,11 @@ struct entt_traits<std::uint64_t> {
using difference_type = std::int64_t;
/*! @brief Mask to use to get the entity number out of an identifier. */
static constexpr std::uint64_t entity_mask = 0xFFFFFFFF;
static constexpr entity_type entity_mask = 0xFFFFFFFF;
/*! @brief Mask to use to get the version out of an identifier. */
static constexpr std::uint64_t version_mask = 0xFFFFFFFF;
static constexpr entity_type version_mask = 0xFFFFFFFF;
/*! @brief Extent of the entity number within an identifier. */
static constexpr auto entity_shift = 32;
static constexpr std::size_t entity_shift = 32u;
};
@@ -129,14 +130,10 @@ template<typename Entity>
namespace internal {
class null {
template<typename Entity>
using traits_type = entt_traits<Entity>;
public:
struct null {
template<typename Entity>
[[nodiscard]] constexpr operator Entity() const ENTT_NOEXCEPT {
return Entity{traits_type<Entity>::entity_mask};
return Entity{entt_traits<Entity>::entity_mask};
}
[[nodiscard]] constexpr bool operator==(null) const ENTT_NOEXCEPT {
@@ -149,7 +146,7 @@ public:
template<typename Entity>
[[nodiscard]] constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
return (to_integral(entity) & traits_type<Entity>::entity_mask) == to_integral(static_cast<Entity>(*this));
return (to_integral(entity) & entt_traits<Entity>::entity_mask) == to_integral(static_cast<Entity>(*this));
}
template<typename Entity>

View File

@@ -150,11 +150,11 @@ class sparse_set {
};
[[nodiscard]] auto page(const Entity entt) const ENTT_NOEXCEPT {
return std::size_t{(to_integral(entt) & traits_type::entity_mask) / entt_per_page};
return size_type{(to_integral(entt) & traits_type::entity_mask) / entt_per_page};
}
[[nodiscard]] auto offset(const Entity entt) const ENTT_NOEXCEPT {
return std::size_t{to_integral(entt) & (entt_per_page - 1)};
return size_type{to_integral(entt) & (entt_per_page - 1)};
}
[[nodiscard]] page_type & assure(const std::size_t pos) {

View File

@@ -478,7 +478,6 @@ private:
/*! @copydoc storage */
template<typename Entity, typename Type>
class storage<Entity, Type, std::enable_if_t<ENTT_IS_EMPTY(Type)>>: public sparse_set<Entity> {
using traits_type = entt_traits<Entity>;
using underlying_type = sparse_set<Entity>;
public:

View File

@@ -95,6 +95,12 @@ if(BUILD_BENCHMARK)
SETUP_BASIC_TEST(benchmark benchmark/benchmark.cpp)
endif()
# Test example
if(BUILD_EXAMPLE)
SETUP_BASIC_TEST(custom_identifier example/custom_identifier.cpp)
endif()
# Test lib
if(BUILD_LIB)

View File

@@ -0,0 +1,57 @@
#include <type_traits>
#include <gtest/gtest.h>
#include <entt/entity/entity.hpp>
#include <entt/entity/registry.hpp>
struct entity_id {
using entity_type = typename entt::entt_traits<entt::entity>::entity_type;
entity_id(entity_type value = entt::null)
: entt{value}
{}
entity_id(const entity_id &other)
: entt{other.entt}
{}
operator entity_type() const {
return entt;
}
private:
entity_type entt;
};
template<>
struct entt::entt_traits<entity_id>: entt::entt_traits<entt::entity> {};
TEST(Example, CustomIdentifier) {
entt::basic_registry<entity_id> registry{};
entity_id entity{};
ASSERT_FALSE(registry.valid(entity));
ASSERT_TRUE(entity == entt::null);
entity = registry.create();
ASSERT_TRUE(registry.valid(entity));
ASSERT_TRUE(entity != entt::null);
ASSERT_FALSE((registry.has<int, char>(entity)));
ASSERT_EQ(registry.try_get<int>(entity), nullptr);
registry.emplace<int>(entity, 42);
ASSERT_TRUE((registry.any<int, char>(entity)));
ASSERT_EQ(registry.get<int>(entity), 42);
registry.destroy(entity);
ASSERT_FALSE(registry.valid(entity));
ASSERT_TRUE(entity != entt::null);
entity = registry.create();
ASSERT_TRUE(registry.valid(entity));
ASSERT_TRUE(entity != entt::null);
}