Compare commits
115 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
d417984ff3 | ||
|
|
d38b3e641b | ||
|
|
28ce491dd5 | ||
|
|
c260d72125 | ||
|
|
d1d1b3156d | ||
|
|
472064b751 | ||
|
|
95ab9a0b70 | ||
|
|
4b03f6a039 | ||
|
|
c3460727fa | ||
|
|
2cc1850212 | ||
|
|
2d7443acaf | ||
|
|
13d0b0940c | ||
|
|
c101797924 | ||
|
|
83b55f8e3f | ||
|
|
b3b6362cd9 | ||
|
|
fc9af32d5f | ||
|
|
4cd1025011 | ||
|
|
5233fe8abc | ||
|
|
041e31ea78 | ||
|
|
7a3e881099 | ||
|
|
631bf42f84 | ||
|
|
1f704a7019 | ||
|
|
d295c88474 | ||
|
|
1dd9da4dff | ||
|
|
f2eb0c8427 | ||
|
|
c8ba11faf8 | ||
|
|
a2e243d992 | ||
|
|
c588fff5ca | ||
|
|
87f9599fea | ||
|
|
0459599b1d | ||
|
|
9447b1a696 | ||
|
|
0ccb7443c2 | ||
|
|
02cf27091f | ||
|
|
fdfbd04503 | ||
|
|
866c18200a | ||
|
|
c1cada49d4 | ||
|
|
7bf550a75f | ||
|
|
9c540c03aa | ||
|
|
b3df46db19 | ||
|
|
7ca615a1c1 | ||
|
|
c83db557a6 | ||
|
|
d54594f11d | ||
|
|
434e38608f | ||
|
|
871f090ca0 | ||
|
|
d1d235e025 | ||
|
|
e822a5fd53 | ||
|
|
7b82a4ae50 | ||
|
|
c532e9f2eb | ||
|
|
3fd034816e | ||
|
|
bb4b868c79 | ||
|
|
3b3da11a36 | ||
|
|
f2cbb5306b | ||
|
|
94ede1b324 | ||
|
|
0367248338 | ||
|
|
936db30e58 | ||
|
|
4822f0dd11 | ||
|
|
456d220829 | ||
|
|
b459ba6ea7 | ||
|
|
a19ef9bd16 | ||
|
|
59cec88a28 | ||
|
|
3ebc75af80 | ||
|
|
4dce474e03 | ||
|
|
31a18da578 | ||
|
|
8c499850fc | ||
|
|
6b6998a247 | ||
|
|
a6cb0fc856 | ||
|
|
e36b93e87b | ||
|
|
1e3723b8bb | ||
|
|
412372289e | ||
|
|
96f7e66073 | ||
|
|
6040f8f263 | ||
|
|
9761b6e14a | ||
|
|
cb49910ed2 | ||
|
|
62bd742673 | ||
|
|
42d0a3d734 | ||
|
|
f0f8681455 | ||
|
|
c801afddcb | ||
|
|
20e0e1333e | ||
|
|
a6b373fec4 | ||
|
|
41c77720bb | ||
|
|
92e6340120 | ||
|
|
1221f63cbd | ||
|
|
0f24418891 | ||
|
|
f477c0ab87 | ||
|
|
9358691901 | ||
|
|
cd343ba598 | ||
|
|
50069d3743 | ||
|
|
1e03f27f23 | ||
|
|
36bb55a9ce | ||
|
|
451e4050db | ||
|
|
367fd3e87f | ||
|
|
a67a2e12fd | ||
|
|
292978daf0 | ||
|
|
85a4a76a14 | ||
|
|
9d0ab7ed70 | ||
|
|
3d5b6a5e0b | ||
|
|
ab20372093 | ||
|
|
ab887f30e4 | ||
|
|
6cb6a8c25f | ||
|
|
9d1d2aca0a | ||
|
|
75cb2cd1f7 | ||
|
|
ed6adbbfd7 | ||
|
|
b6c950ffc5 | ||
|
|
8b89c69d5f | ||
|
|
290dda50fe | ||
|
|
a7278573a8 | ||
|
|
68ce4dc689 | ||
|
|
a9f5118013 | ||
|
|
d1f2e8ecf9 | ||
|
|
fe6873b61a | ||
|
|
7c7bcf80cf | ||
|
|
cf6022866d | ||
|
|
c630cb1de2 | ||
|
|
2e6c8d542c | ||
|
|
2f781906b5 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1,2 +1 @@
|
||||
# QtCreator
|
||||
*.user
|
||||
|
||||
@@ -16,7 +16,7 @@ endif()
|
||||
# Project configuration
|
||||
#
|
||||
|
||||
project(entt VERSION 2.1.0)
|
||||
project(entt VERSION 2.5.0)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
@@ -29,7 +29,7 @@ set(PROJECT_AUTHOR_EMAIL "michele.caini@gmail.com")
|
||||
|
||||
message("*")
|
||||
message("* ${PROJECT_NAME} v${PROJECT_VERSION} (${CMAKE_BUILD_TYPE})")
|
||||
message("* Copyright (c) 2017 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
|
||||
message("* Copyright (c) 2018 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
|
||||
message("*")
|
||||
|
||||
#
|
||||
@@ -37,13 +37,28 @@ message("*")
|
||||
#
|
||||
|
||||
set(CMAKE_CXX_STANDARD 14)
|
||||
set(CMAKE_CXX_EXTENSIONS OFF)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
if(NOT MSVC)
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
|
||||
include(CheckCXXSourceCompiles)
|
||||
|
||||
set(OLD_CMAKE_CXX_FLAGS ${CMAKE_CXX_FLAGS})
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -stdlib=libc++")
|
||||
|
||||
check_cxx_source_compiles("
|
||||
#include<type_traits>
|
||||
int main() { return std::is_same<int, int>::value ? 0 : 1; }
|
||||
" HAS_LIBCPP)
|
||||
|
||||
if(NOT HAS_LIBCPP)
|
||||
set(CMAKE_CXX_FLAGS "${OLD_CMAKE_CXX_FLAGS}")
|
||||
endif()
|
||||
|
||||
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall")
|
||||
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DRELEASE")
|
||||
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -DDEBUG")
|
||||
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
|
||||
|
||||
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
|
||||
# it seems that -O3 ruins the performance when using clang ...
|
||||
@@ -55,14 +70,10 @@ if(NOT MSVC)
|
||||
endif()
|
||||
|
||||
#
|
||||
# CMake configuration
|
||||
# Include EnTT
|
||||
#
|
||||
|
||||
set(PROJECT_CMAKE_IN ${entt_SOURCE_DIR}/cmake/in)
|
||||
set(PROJECT_DEPS_DIR ${entt_SOURCE_DIR}/deps)
|
||||
set(PROJECT_SRC_DIR ${entt_SOURCE_DIR}/src)
|
||||
|
||||
set(PROJECT_RUNTIME_OUTPUT_DIRECTORY bin)
|
||||
include_directories(${entt_SOURCE_DIR}/src)
|
||||
|
||||
#
|
||||
# Tests
|
||||
@@ -74,9 +85,13 @@ if(BUILD_TESTING)
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
find_package(Threads REQUIRED)
|
||||
|
||||
option(BUILD_BENCHMARK "Build benchmark." OFF)
|
||||
option(BUILD_MOD "Build mod example." OFF)
|
||||
option(BUILD_SNAPSHOT "Build snapshot example." OFF)
|
||||
|
||||
# gtest, gtest_main, gmock and gmock_main targets are available from now on
|
||||
set(GOOGLETEST_DEPS_DIR ${PROJECT_DEPS_DIR}/googletest)
|
||||
configure_file(${PROJECT_CMAKE_IN}/googletest.in ${GOOGLETEST_DEPS_DIR}/CMakeLists.txt)
|
||||
set(GOOGLETEST_DEPS_DIR ${entt_SOURCE_DIR}/deps/googletest)
|
||||
configure_file(${entt_SOURCE_DIR}/cmake/in/googletest.in ${GOOGLETEST_DEPS_DIR}/CMakeLists.txt)
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
@@ -95,3 +110,18 @@ find_package(Doxygen 1.8)
|
||||
if(DOXYGEN_FOUND)
|
||||
add_subdirectory(docs)
|
||||
endif()
|
||||
|
||||
#
|
||||
# AOB
|
||||
#
|
||||
|
||||
add_custom_target(
|
||||
entt_aob
|
||||
SOURCES
|
||||
appveyor.yml
|
||||
AUTHORS
|
||||
LICENSE
|
||||
README.md
|
||||
TODO
|
||||
.travis.yml
|
||||
)
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017 Michele Caini
|
||||
Copyright (c) 2018 Michele Caini
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
|
||||
9
TODO
Normal file
9
TODO
Normal file
@@ -0,0 +1,9 @@
|
||||
* custom allocators and EnTT allocator-aware in general (long term feature, I don't actually need it at the moment) - see #22
|
||||
* to analyze, long term feature: systems organizer based on dependency graphs for implicit parallelism (I don't want to think anymore in future :-))
|
||||
* scene management (I prefer the concept of spaces, that is a kind of scene anyway)
|
||||
* blueprint registry - kind of factory to create entitites template for initialization (get rid of the extra versions of Registry::create)
|
||||
* review doc: separate it in multiple md/dox files, reduce the readme to a minimum and provide users with links to the online documentation on gh-pages
|
||||
* debugging tools (#60): the issue online already contains interesting tips on this, look at it
|
||||
* signals on component creation/destruction: crtp + internal detection, probably it works - test it!!
|
||||
* define a macro for the noexcept policy, so as to provide users with an easy way to disable exception handling
|
||||
* AOB
|
||||
@@ -14,7 +14,7 @@ configuration:
|
||||
|
||||
before_build:
|
||||
- cd %BUILD_DIR%
|
||||
- cmake .. -G"Visual Studio 15 2017"
|
||||
- cmake .. -DCMAKE_CXX_FLAGS=/D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING -G"Visual Studio 15 2017"
|
||||
|
||||
build:
|
||||
parallel: true
|
||||
|
||||
19
cmake/in/cereal.in
Normal file
19
cmake/in/cereal.in
Normal file
@@ -0,0 +1,19 @@
|
||||
project(cereal-download NONE)
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
include(ExternalProject)
|
||||
|
||||
ExternalProject_Add(
|
||||
cereal
|
||||
GIT_REPOSITORY https://github.com/USCiLab/cereal.git
|
||||
GIT_TAG v1.2.2
|
||||
DOWNLOAD_DIR ${CEREAL_DEPS_DIR}
|
||||
TMP_DIR ${CEREAL_DEPS_DIR}/tmp
|
||||
STAMP_DIR ${CEREAL_DEPS_DIR}/stamp
|
||||
SOURCE_DIR ${CEREAL_DEPS_DIR}/src
|
||||
BINARY_DIR ${CEREAL_DEPS_DIR}/build
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
||||
19
cmake/in/duktape.in
Normal file
19
cmake/in/duktape.in
Normal file
@@ -0,0 +1,19 @@
|
||||
project(duktape-download NONE)
|
||||
cmake_minimum_required(VERSION 3.2)
|
||||
|
||||
include(ExternalProject)
|
||||
|
||||
ExternalProject_Add(
|
||||
duktape
|
||||
GIT_REPOSITORY https://github.com/svaarala/duktape-releases.git
|
||||
GIT_TAG v2.2.0
|
||||
DOWNLOAD_DIR ${DUKTAPE_DEPS_DIR}
|
||||
TMP_DIR ${DUKTAPE_DEPS_DIR}/tmp
|
||||
STAMP_DIR ${DUKTAPE_DEPS_DIR}/stamp
|
||||
SOURCE_DIR ${DUKTAPE_DEPS_DIR}/src
|
||||
BINARY_DIR ${DUKTAPE_DEPS_DIR}/build
|
||||
CONFIGURE_COMMAND ""
|
||||
BUILD_COMMAND ""
|
||||
INSTALL_COMMAND ""
|
||||
TEST_COMMAND ""
|
||||
)
|
||||
@@ -4,21 +4,18 @@
|
||||
|
||||
set(TARGET_DOCS docs)
|
||||
|
||||
set(DOXY_IN_FILE doxy.in)
|
||||
|
||||
set(DOXY_SOURCE_DIRECTORY ${PROJECT_SRC_DIR})
|
||||
set(DOXY_SOURCE_DIRECTORY ${entt_SOURCE_DIR}/src)
|
||||
set(DOXY_DOCS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(DOXY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
|
||||
set(DOXY_CFG_FILE doxy.cfg)
|
||||
|
||||
configure_file(${DOXY_IN_FILE} ${DOXY_CFG_FILE} @ONLY)
|
||||
configure_file(doxy.in doxy.cfg @ONLY)
|
||||
|
||||
add_custom_target(
|
||||
${TARGET_DOCS}
|
||||
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/${DOXY_CFG_FILE}
|
||||
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doxy.cfg
|
||||
WORKING_DIRECTORY ${entt_SOURCE_DIR}
|
||||
VERBATIM
|
||||
SOURCES ${DOXY_IN_FILE}
|
||||
SOURCES doxy.in
|
||||
)
|
||||
|
||||
install(
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
|
||||
#include<type_traits>
|
||||
#include<cstddef>
|
||||
#include<atomic>
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -18,14 +19,11 @@ namespace entt {
|
||||
*/
|
||||
template<typename...>
|
||||
class Family {
|
||||
static std::size_t identifier() noexcept {
|
||||
static std::size_t value = 0;
|
||||
return value++;
|
||||
}
|
||||
static std::atomic<std::size_t> identifier;
|
||||
|
||||
template<typename...>
|
||||
static std::size_t family() noexcept {
|
||||
static const std::size_t value = identifier();
|
||||
static const std::size_t value = identifier.fetch_add(1);
|
||||
return value;
|
||||
}
|
||||
|
||||
@@ -38,12 +36,16 @@ public:
|
||||
* @return Statically generated unique identifier for the given type.
|
||||
*/
|
||||
template<typename... Type>
|
||||
static family_type type() noexcept {
|
||||
inline static family_type type() noexcept {
|
||||
return family<std::decay_t<Type>...>();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename... Types>
|
||||
std::atomic<std::size_t> Family<Types...>::identifier{};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
110
src/entt/core/hashed_string.hpp
Normal file
110
src/entt/core/hashed_string.hpp
Normal file
@@ -0,0 +1,110 @@
|
||||
#ifndef ENTT_CORE_HASHED_STRING_HPP
|
||||
#define ENTT_CORE_HASHED_STRING_HPP
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Zero overhead resource identifier.
|
||||
*
|
||||
* A hashed string is a compile-time tool that allows users to use
|
||||
* human-readable identifers in the codebase while using their numeric
|
||||
* counterparts at runtime.<br/>
|
||||
* Because of that, a hashed string can also be used in constant expressions if
|
||||
* required.
|
||||
*/
|
||||
class HashedString final {
|
||||
struct ConstCharWrapper final {
|
||||
// non-explicit constructor on purpose
|
||||
constexpr ConstCharWrapper(const char *str) noexcept: str{str} {}
|
||||
const char *str;
|
||||
};
|
||||
|
||||
static constexpr std::uint64_t offset = 14695981039346656037ull;
|
||||
static constexpr std::uint64_t prime = 1099511628211ull;
|
||||
|
||||
// Fowler–Noll–Vo hash function v. 1a - the good
|
||||
static constexpr std::uint64_t helper(std::uint64_t partial, const char *str) noexcept {
|
||||
return str[0] == 0 ? partial : helper((partial^str[0])*prime, str+1);
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using hash_type = std::uint64_t;
|
||||
|
||||
/**
|
||||
* @brief Constructs a hashed string from an array of const chars.
|
||||
*
|
||||
* Forcing template resolution avoids implicit conversions. An
|
||||
* human-readable identifier can be anything but a plain, old bunch of
|
||||
* characters.<br/>
|
||||
* Example of use:
|
||||
* @code{.cpp}
|
||||
* HashedString sh{"my.png"};
|
||||
* @endcode
|
||||
*
|
||||
* @tparam N Number of characters of the identifier.
|
||||
* @param str Human-readable identifer.
|
||||
*/
|
||||
template <std::size_t N>
|
||||
constexpr HashedString(const char (&str)[N]) noexcept
|
||||
: hash{helper(offset, str)}, str{str}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Explicit constructor on purpose to avoid constructing a hashed
|
||||
* string directly from a `const char *`.
|
||||
*
|
||||
* @param wrapper Helps achieving the purpose by relying on overloading.
|
||||
*/
|
||||
explicit constexpr HashedString(ConstCharWrapper wrapper) noexcept
|
||||
: hash{helper(offset, wrapper.str)}, str{wrapper.str}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Returns the human-readable representation of a hashed string.
|
||||
* @return The string used to initialize the instance.
|
||||
*/
|
||||
constexpr operator const char *() const noexcept { return str; }
|
||||
|
||||
/**
|
||||
* @brief Returns the numeric representation of a hashed string.
|
||||
* @return The numeric representation of the instance.
|
||||
*/
|
||||
constexpr operator hash_type() const noexcept { return hash; }
|
||||
|
||||
/**
|
||||
* @brief Compares two hashed strings.
|
||||
* @param other Hashed string with which to compare.
|
||||
* @return True if the two hashed strings are identical, false otherwise.
|
||||
*/
|
||||
constexpr bool operator==(const HashedString &other) const noexcept {
|
||||
return hash == other.hash;
|
||||
}
|
||||
|
||||
private:
|
||||
const hash_type hash;
|
||||
const char *str;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Compares two hashed strings.
|
||||
* @param lhs A valid hashed string.
|
||||
* @param rhs A valid hashed string.
|
||||
* @return True if the two hashed strings are identical, false otherwise.
|
||||
*/
|
||||
constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) noexcept {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_CORE_HASHED_STRING_HPP
|
||||
154
src/entt/entity/actor.hpp
Normal file
154
src/entt/entity/actor.hpp
Normal file
@@ -0,0 +1,154 @@
|
||||
#ifndef ENTT_ENTITY_ACTOR_HPP
|
||||
#define ENTT_ENTITY_ACTOR_HPP
|
||||
|
||||
|
||||
#include <utility>
|
||||
#include "registry.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Dedicated to those who aren't confident with entity-component systems.
|
||||
*
|
||||
* Tiny wrapper around a registry, for all those users that aren't confident
|
||||
* with entity-component systems and prefer to iterate objects directly.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Delta Type to use to provide elapsed time.
|
||||
*/
|
||||
template<typename Entity, typename Delta>
|
||||
struct Actor {
|
||||
/*! @brief Type of registry used internally. */
|
||||
using registry_type = Registry<Entity>;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = Entity;
|
||||
/*! @brief Type used to provide elapsed time. */
|
||||
using delta_type = Delta;
|
||||
|
||||
/**
|
||||
* @brief Constructs an actor by using the given registry.
|
||||
* @param reg An entity-component system properly initialized.
|
||||
*/
|
||||
Actor(Registry<Entity> ®)
|
||||
: reg{reg}, entity{reg.create()}
|
||||
{}
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Actor() {
|
||||
reg.destroy(entity);
|
||||
}
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Actor(const Actor &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Actor(Actor &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This actor. */
|
||||
Actor & operator=(const Actor &) = default;
|
||||
/*! @brief Default move assignment operator. @return This actor. */
|
||||
Actor & operator=(Actor &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Assigns the given component to an actor.
|
||||
*
|
||||
* A new instance of the given component is created and initialized with the
|
||||
* arguments provided (the component must have a proper constructor or be of
|
||||
* aggregate type). Then the component is assigned to the actor.<br/>
|
||||
* In case the actor already has a component of the given type, it's
|
||||
* replaced with the new one.
|
||||
*
|
||||
* @tparam Component Type of the component to create.
|
||||
* @tparam Args Types of arguments to use to construct the component.
|
||||
* @param args Parameters to use to initialize the component.
|
||||
* @return A reference to the newly created component.
|
||||
*/
|
||||
template<typename Component, typename... Args>
|
||||
Component & set(Args &&... args) {
|
||||
return reg.template accommodate<Component>(entity, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes the given component from an actor.
|
||||
* @tparam Component Type of the component to remove.
|
||||
*/
|
||||
template<typename Component>
|
||||
void unset() {
|
||||
reg.template remove<Component>(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if an actor has the given component.
|
||||
* @tparam Component Type of the component for which to perform the check.
|
||||
* @return True if the actor has the component, false otherwise.
|
||||
*/
|
||||
template<typename Component>
|
||||
bool has() const noexcept {
|
||||
return reg.template has<Component>(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given component for an actor.
|
||||
* @tparam Component Type of the component to get.
|
||||
* @return A reference to the instance of the component owned by the entity.
|
||||
*/
|
||||
template<typename Component>
|
||||
const Component & get() const noexcept {
|
||||
return reg.template get<Component>(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given component for an actor.
|
||||
* @tparam Component Type of the component to get.
|
||||
* @return A reference to the instance of the component owned by the entity.
|
||||
*/
|
||||
template<typename Component>
|
||||
Component & get() noexcept {
|
||||
return const_cast<Component &>(const_cast<const Actor *>(this)->get<Component>());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the underlying registry.
|
||||
* @return A reference to the underlying registry
|
||||
*/
|
||||
const registry_type & registry() const noexcept {
|
||||
return reg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the underlying registry.
|
||||
* @return A reference to the underlying registry
|
||||
*/
|
||||
registry_type & registry() noexcept {
|
||||
return const_cast<registry_type &>(const_cast<const Actor *>(this)->registry());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates an actor, whatever it means to update it.
|
||||
* @param delta Elapsed time.
|
||||
*/
|
||||
virtual void update(delta_type delta) = 0;
|
||||
|
||||
private:
|
||||
registry_type ®
|
||||
Entity entity;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Default actor class.
|
||||
*
|
||||
* The default actor is the best choice for almost all the applications.<br/>
|
||||
* Users should have a really good reason to choose something different.
|
||||
*
|
||||
* @tparam Delta Type to use to provide elapsed time.
|
||||
*/
|
||||
template<typename Delta>
|
||||
using DefaultActor = Actor<DefaultRegistry::entity_type, Delta>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_ACTOR_HPP
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef ENTT_ENTITY_ENTT_HPP
|
||||
#define ENTT_ENTITY_ENTT_HPP
|
||||
#ifndef ENTT_ENTITY_ENTT_TRAITS_HPP
|
||||
#define ENTT_ENTITY_ENTT_TRAITS_HPP
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
@@ -22,6 +22,7 @@ struct entt_traits;
|
||||
* @brief Entity traits for a 16 bits entity identifier.
|
||||
*
|
||||
* A 16 bits entity identifier guarantees:
|
||||
*
|
||||
* * 12 bits for the entity number (up to 4k entities).
|
||||
* * 4 bit for the version (resets in [0-15]).
|
||||
*/
|
||||
@@ -37,7 +38,7 @@ struct entt_traits<std::uint16_t> {
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr auto version_mask = 0xF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto version_shift = 12;
|
||||
static constexpr auto entity_shift = 12;
|
||||
};
|
||||
|
||||
|
||||
@@ -45,6 +46,7 @@ struct entt_traits<std::uint16_t> {
|
||||
* @brief Entity traits for a 32 bits entity identifier.
|
||||
*
|
||||
* A 32 bits entity identifier guarantees:
|
||||
*
|
||||
* * 24 bits for the entity number (suitable for almost all the games).
|
||||
* * 8 bit for the version (resets in [0-255]).
|
||||
*/
|
||||
@@ -56,11 +58,11 @@ struct entt_traits<std::uint32_t> {
|
||||
using version_type = std::uint16_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr auto entity_mask = 0xFFFFFF;
|
||||
static constexpr auto entity_mask = 0xFFFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr auto version_mask = 0xFF;
|
||||
static constexpr auto version_mask = 0xFFF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto version_shift = 24;
|
||||
static constexpr auto entity_shift = 20;
|
||||
};
|
||||
|
||||
|
||||
@@ -68,6 +70,7 @@ struct entt_traits<std::uint32_t> {
|
||||
* @brief Entity traits for a 64 bits entity identifier.
|
||||
*
|
||||
* A 64 bits entity identifier guarantees:
|
||||
*
|
||||
* * 40 bits for the entity number (an indecently large number).
|
||||
* * 24 bit for the version (an indecently large number).
|
||||
*/
|
||||
@@ -83,11 +86,11 @@ struct entt_traits<std::uint64_t> {
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr auto version_mask = 0xFFFFFF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto version_shift = 40;
|
||||
static constexpr auto entity_shift = 40;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_ENTT_HPP
|
||||
#endif // ENTT_ENTITY_ENTT_TRAITS_HPP
|
||||
File diff suppressed because it is too large
Load Diff
713
src/entt/entity/snapshot.hpp
Normal file
713
src/entt/entity/snapshot.hpp
Normal file
@@ -0,0 +1,713 @@
|
||||
#ifndef ENTT_ENTITY_SNAPSHOT_HPP
|
||||
#define ENTT_ENTITY_SNAPSHOT_HPP
|
||||
|
||||
|
||||
#include <unordered_map>
|
||||
#include <algorithm>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include "entt_traits.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Forward declaration of the registry class.
|
||||
*/
|
||||
template<typename>
|
||||
class Registry;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Utility class to create snapshots from a registry.
|
||||
*
|
||||
* A _snapshot_ can be either a dump of the entire registry or a narrower
|
||||
* selection of components and tags of interest.<br/>
|
||||
* This type can be used in both cases if provided with a correctly configured
|
||||
* output archive.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class Snapshot final {
|
||||
/*! @brief A registry is allowed to create snapshots. */
|
||||
friend class Registry<Entity>;
|
||||
|
||||
using follow_fn_type = Entity(*)(const Registry<Entity> &, Entity);
|
||||
using raw_fn_type = const Entity *(*)(const Registry<Entity> &, typename Registry<Entity>::component_type);
|
||||
|
||||
Snapshot(const Registry<Entity> ®istry, Entity seed, std::size_t size, follow_fn_type follow, raw_fn_type raw) noexcept
|
||||
: registry{registry},
|
||||
seed{seed},
|
||||
size{size},
|
||||
follow{follow},
|
||||
raw{raw}
|
||||
{}
|
||||
|
||||
Snapshot(const Snapshot &) = default;
|
||||
Snapshot(Snapshot &&) = default;
|
||||
|
||||
Snapshot & operator=(const Snapshot &) = default;
|
||||
Snapshot & operator=(Snapshot &&) = default;
|
||||
|
||||
template<typename Component, typename Archive>
|
||||
void get(Archive &archive, const Registry<Entity> ®istry) {
|
||||
const auto component = registry.template component<Component>();
|
||||
const auto sz = registry.template size<Component>();
|
||||
const auto *entities = raw(registry, component);
|
||||
|
||||
archive(static_cast<Entity>(sz));
|
||||
|
||||
for(std::remove_const_t<decltype(sz)> i{}; i < sz; ++i) {
|
||||
const auto entity = entities[i];
|
||||
archive(entity);
|
||||
archive(registry.template get<Component>(entity));
|
||||
};
|
||||
}
|
||||
|
||||
template<typename Tag, typename Archive>
|
||||
void get(Archive &archive) {
|
||||
const bool has = registry.template has<Tag>();
|
||||
|
||||
// numerical length is forced for tags to facilitate loading
|
||||
archive(has ? Entity(1): Entity{});
|
||||
|
||||
if(has) {
|
||||
archive(registry.template attachee<Tag>());
|
||||
archive(registry.template get<Tag>());
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Puts aside all the entities that are still in use.
|
||||
*
|
||||
* Entities are serialized along with their versions. Destroyed entities are
|
||||
* not taken in consideration by this function.
|
||||
*
|
||||
* @tparam Archive Type of output archive.
|
||||
* @param archive A valid reference to an output archive.
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename Archive>
|
||||
Snapshot entities(Archive &archive) && {
|
||||
archive(static_cast<Entity>(registry.size()));
|
||||
registry.each([&archive, this](auto entity) { archive(entity); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside destroyed entities.
|
||||
*
|
||||
* Entities are serialized along with their versions. Entities that are
|
||||
* still in use are not taken in consideration by this function.
|
||||
*
|
||||
* @tparam Archive Type of output archive.
|
||||
* @param archive A valid reference to an output archive.
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename Archive>
|
||||
Snapshot destroyed(Archive &archive) && {
|
||||
archive(static_cast<Entity>(size));
|
||||
|
||||
if(size) {
|
||||
auto curr = seed;
|
||||
archive(curr);
|
||||
|
||||
for(auto i = size - 1; i; --i) {
|
||||
curr = follow(registry, curr);
|
||||
archive(curr);
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside the given components.
|
||||
*
|
||||
* Each component is serialized together with the entity to which it
|
||||
* belongs. Entities are serialized along with their versions.
|
||||
*
|
||||
* @tparam Component Types of components to serialize.
|
||||
* @tparam Archive Type of output archive.
|
||||
* @param archive A valid reference to an output archive.
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename... Component, typename Archive>
|
||||
Snapshot component(Archive &archive) && {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (get<Component>(archive, registry), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside the given tags.
|
||||
*
|
||||
* Each tag is serialized together with the entity to which it belongs.
|
||||
* Entities are serialized along with their versions.
|
||||
*
|
||||
* @tparam Tag Types of tags to serialize.
|
||||
* @tparam Archive Type of output archive.
|
||||
* @param archive A valid reference to an output archive.
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename... Tag, typename Archive>
|
||||
Snapshot tag(Archive &archive) && {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (get<Tag>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
const Registry<Entity> ®istry;
|
||||
const Entity seed;
|
||||
const std::size_t size;
|
||||
follow_fn_type follow;
|
||||
raw_fn_type raw;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Utility class to restore a snapshot as a whole.
|
||||
*
|
||||
* A snapshot loader requires that the destination registry be empty and loads
|
||||
* all the data at once while keeping intact the identifiers that the entities
|
||||
* originally had.<br/>
|
||||
* An example of use is the implementation of a save/restore utility.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class SnapshotLoader final {
|
||||
/*! @brief A registry is allowed to create snapshot loaders. */
|
||||
friend class Registry<Entity>;
|
||||
|
||||
using assure_fn_type = void(*)(Registry<Entity> &, Entity, bool);
|
||||
|
||||
SnapshotLoader(Registry<Entity> ®istry, assure_fn_type assure_fn) noexcept
|
||||
: registry{registry},
|
||||
assure_fn{assure_fn}
|
||||
{
|
||||
// restore a snapshot as a whole requires a clean registry
|
||||
assert(!registry.capacity());
|
||||
}
|
||||
|
||||
SnapshotLoader(const SnapshotLoader &) = default;
|
||||
SnapshotLoader(SnapshotLoader &&) = default;
|
||||
|
||||
SnapshotLoader & operator=(const SnapshotLoader &) = default;
|
||||
SnapshotLoader & operator=(SnapshotLoader &&) = default;
|
||||
|
||||
template<typename Archive, typename Func>
|
||||
void each(Archive &archive, Func func) {
|
||||
Entity length{};
|
||||
archive(length);
|
||||
|
||||
while(length) {
|
||||
Entity entity{};
|
||||
archive(entity);
|
||||
func(entity);
|
||||
--length;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Component, typename Archive>
|
||||
void assign(Archive &archive) {
|
||||
each(archive, [&archive, this](auto entity) {
|
||||
static constexpr auto destroyed = false;
|
||||
assure_fn(registry, entity, destroyed);
|
||||
archive(registry.template assign<Component>(entity));
|
||||
});
|
||||
}
|
||||
|
||||
template<typename Tag, typename Archive>
|
||||
void attach(Archive &archive) {
|
||||
each(archive, [&archive, this](auto entity) {
|
||||
static constexpr auto destroyed = false;
|
||||
assure_fn(registry, entity, destroyed);
|
||||
archive(registry.template attach<Tag>(entity));
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Restores entities that were in use during serialization.
|
||||
*
|
||||
* This function restores the entities that were in use during serialization
|
||||
* and gives them the versions they originally had.
|
||||
*
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename Archive>
|
||||
SnapshotLoader entities(Archive &archive) && {
|
||||
each(archive, [this](auto entity) {
|
||||
static constexpr auto destroyed = false;
|
||||
assure_fn(registry, entity, destroyed);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores entities that were destroyed during serialization.
|
||||
*
|
||||
* This function restores the entities that were destroyed during
|
||||
* serialization and gives them the versions they originally had.
|
||||
*
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename Archive>
|
||||
SnapshotLoader destroyed(Archive &archive) && {
|
||||
each(archive, [this](auto entity) {
|
||||
static constexpr auto destroyed = true;
|
||||
assure_fn(registry, entity, destroyed);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores components and assigns them to the right entities.
|
||||
*
|
||||
* The template parameter list must be exactly the same used during
|
||||
* serialization. In the event that the entity to which the component is
|
||||
* assigned doesn't exist yet, the loader will take care to create it with
|
||||
* the version it originally had.
|
||||
*
|
||||
* @tparam Component Types of components to restore.
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename... Component, typename Archive>
|
||||
SnapshotLoader component(Archive &archive) && {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (assign<Component>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores tags and assigns them to the right entities.
|
||||
*
|
||||
* The template parameter list must be exactly the same used during
|
||||
* serialization. In the event that the entity to which the tag is assigned
|
||||
* doesn't exist yet, the loader will take care to create it with the
|
||||
* version it originally had.
|
||||
*
|
||||
* @tparam Tag Types of tags to restore.
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename... Tag, typename Archive>
|
||||
SnapshotLoader tag(Archive &archive) && {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (attach<Tag>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Destroys those entities that have neither components nor tags.
|
||||
*
|
||||
* In case all the entities were serialized but only part of the components
|
||||
* and tags was saved, it could happen that some of the entities have
|
||||
* neither components nor tags once restored.<br/>
|
||||
* This functions helps to identify and destroy those entities.
|
||||
*
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
SnapshotLoader orphans() && {
|
||||
registry.orphans([this](auto entity) {
|
||||
registry.destroy(entity);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
Registry<Entity> ®istry;
|
||||
assure_fn_type assure_fn;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Utility class for _continuous loading_.
|
||||
*
|
||||
* A _continuous loader_ is designed to load data from a source registry to a
|
||||
* (possibly) non-empty destination. The loader can accomodate in a registry
|
||||
* more than one snapshot in a sort of _continuous loading_ that updates the
|
||||
* destination one step at a time.<br/>
|
||||
* Identifiers that entities originally had are not transferred to the target.
|
||||
* Instead, the loader maps remote identifiers to local ones while restoring a
|
||||
* snapshot.<br/>
|
||||
* An example of use is the implementation of a client-server applications with
|
||||
* the requirement of transferring somehow parts of the representation side to
|
||||
* side.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class ContinuousLoader final {
|
||||
using traits_type = entt_traits<Entity>;
|
||||
|
||||
Entity destroy(Entity entity) {
|
||||
const auto it = remloc.find(entity);
|
||||
|
||||
if(it == remloc.cend()) {
|
||||
const auto local = registry.create();
|
||||
remloc.emplace(entity, std::make_pair(local, true));
|
||||
registry.destroy(local);
|
||||
}
|
||||
|
||||
return remloc[entity].first;
|
||||
}
|
||||
|
||||
Entity restore(Entity entity) {
|
||||
const auto it = remloc.find(entity);
|
||||
|
||||
if(it == remloc.cend()) {
|
||||
const auto local = registry.create();
|
||||
remloc.emplace(entity, std::make_pair(local, true));
|
||||
} else {
|
||||
remloc[entity].first =
|
||||
registry.valid(remloc[entity].first)
|
||||
? remloc[entity].first
|
||||
: registry.create();
|
||||
|
||||
// set the dirty flag
|
||||
remloc[entity].second = true;
|
||||
}
|
||||
|
||||
return remloc[entity].first;
|
||||
}
|
||||
|
||||
template<typename Instance, typename Type>
|
||||
std::enable_if_t<std::is_same<Type, Entity>::value>
|
||||
update(Instance &instance, Type Instance::*member) {
|
||||
instance.*member = map(instance.*member);
|
||||
}
|
||||
|
||||
template<typename Instance, typename Type>
|
||||
std::enable_if_t<std::is_same<typename std::iterator_traits<typename Type::iterator>::value_type, Entity>::value>
|
||||
update(Instance &instance, Type Instance::*member) {
|
||||
for(auto &entity: (instance.*member)) {
|
||||
entity = map(entity);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Archive, typename Func>
|
||||
void each(Archive &archive, Func func) {
|
||||
Entity length{};
|
||||
archive(length);
|
||||
|
||||
while(length) {
|
||||
Entity entity{};
|
||||
archive(entity);
|
||||
func(entity);
|
||||
--length;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
void reset() {
|
||||
for(auto &&ref: remloc) {
|
||||
const auto local = ref.second.first;
|
||||
|
||||
if(registry.valid(local)) {
|
||||
registry.template reset<Component>(local);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Component, typename Archive>
|
||||
void assign(Archive &archive) {
|
||||
reset<Component>();
|
||||
|
||||
each(archive, [&archive, this](auto entity) {
|
||||
entity = restore(entity);
|
||||
archive(registry.template accommodate<Component>(entity));
|
||||
});
|
||||
}
|
||||
|
||||
template<typename Component, typename Archive, typename... Type>
|
||||
void assign(Archive &archive, Type Component::*... member) {
|
||||
reset<Component>();
|
||||
|
||||
each(archive, [&archive, member..., this](auto entity) {
|
||||
entity = restore(entity);
|
||||
auto &component = registry.template accommodate<Component>(entity);
|
||||
archive(component);
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (update(component, member), 0)... };
|
||||
(void)accumulator;
|
||||
});
|
||||
}
|
||||
|
||||
template<typename Tag, typename Archive>
|
||||
void attach(Archive &archive) {
|
||||
registry.template remove<Tag>();
|
||||
|
||||
each(archive, [&archive, this](auto entity) {
|
||||
entity = restore(entity);
|
||||
archive(registry.template attach<Tag>(entity));
|
||||
});
|
||||
}
|
||||
|
||||
template<typename Tag, typename Archive, typename... Type>
|
||||
void attach(Archive &archive, Type Tag::*... member) {
|
||||
registry.template remove<Tag>();
|
||||
|
||||
each(archive, [&archive, member..., this](auto entity) {
|
||||
entity = restore(entity);
|
||||
auto &tag = registry.template attach<Tag>(entity);
|
||||
archive(tag);
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (update(tag, member), 0)... };
|
||||
(void)accumulator;
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = Entity;
|
||||
|
||||
/**
|
||||
* @brief Constructs a loader that is bound to a given registry.
|
||||
* @param registry A valid reference to a registry.
|
||||
*/
|
||||
ContinuousLoader(Registry<entity_type> ®istry) noexcept
|
||||
: registry{registry}
|
||||
{}
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
ContinuousLoader(const ContinuousLoader &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
ContinuousLoader(ContinuousLoader &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This loader. */
|
||||
ContinuousLoader & operator=(const ContinuousLoader &) = default;
|
||||
/*! @brief Default move assignment operator. @return This loader. */
|
||||
ContinuousLoader & operator=(ContinuousLoader &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Restores entities that were in use during serialization.
|
||||
*
|
||||
* This function restores the entities that were in use during serialization
|
||||
* and creates local counterparts for them if required.
|
||||
*
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename Archive>
|
||||
ContinuousLoader & entities(Archive &archive) {
|
||||
each(archive, [this](auto entity) { restore(entity); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores entities that were destroyed during serialization.
|
||||
*
|
||||
* This function restores the entities that were destroyed during
|
||||
* serialization and creates local counterparts for them if required.
|
||||
*
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename Archive>
|
||||
ContinuousLoader & destroyed(Archive &archive) {
|
||||
each(archive, [this](auto entity) { destroy(entity); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores components and assigns them to the right entities.
|
||||
*
|
||||
* The template parameter list must be exactly the same used during
|
||||
* serialization. In the event that the entity to which the component is
|
||||
* assigned doesn't exist yet, the loader will take care to create a local
|
||||
* counterpart for it.
|
||||
*
|
||||
* @tparam Component Types of components to restore.
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename... Component, typename Archive>
|
||||
ContinuousLoader & component(Archive &archive) {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (assign<Component>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores components and assigns them to the right entities.
|
||||
*
|
||||
* The template parameter list must be exactly the same used during
|
||||
* serialization. In the event that the entity to which the component is
|
||||
* assigned doesn't exist yet, the loader will take care to create a local
|
||||
* counterpart for it.<br/>
|
||||
* Members can be either data members of type entity_type or containers of
|
||||
* entities. In both cases, the loader will visit them and update the
|
||||
* entities by replacing each one with its local counterpart.
|
||||
*
|
||||
* @tparam Component Type of component to restore.
|
||||
* @tparam Archive Type of input archive.
|
||||
* @tparam Type Types of members to update with their local counterparts.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @param member Members to update with their local counterparts.
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename Component, typename Archive, typename... Type>
|
||||
ContinuousLoader & component(Archive &archive, Type Component::*... member) {
|
||||
assign(archive, member...);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores tags and assigns them to the right entities.
|
||||
*
|
||||
* The template parameter list must be exactly the same used during
|
||||
* serialization. In the event that the entity to which the tag is assigned
|
||||
* doesn't exist yet, the loader will take care to create a local
|
||||
* counterpart for it.
|
||||
*
|
||||
* @tparam Tag Types of tags to restore.
|
||||
* @tparam Archive Type of input archive.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename... Tag, typename Archive>
|
||||
ContinuousLoader & tag(Archive &archive) {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (attach<Tag>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restores tags and assigns them to the right entities.
|
||||
*
|
||||
* The template parameter list must be exactly the same used during
|
||||
* serialization. In the event that the entity to which the tag is assigned
|
||||
* doesn't exist yet, the loader will take care to create a local
|
||||
* counterpart for it.<br/>
|
||||
* Members can be either data members of type entity_type or containers of
|
||||
* entities. In both cases, the loader will visit them and update the
|
||||
* entities by replacing each one with its local counterpart.
|
||||
*
|
||||
* @tparam Tag Type of tag to restore.
|
||||
* @tparam Archive Type of input archive.
|
||||
* @tparam Type Types of members to update with their local counterparts.
|
||||
* @param archive A valid reference to an input archive.
|
||||
* @param member Members to update with their local counterparts.
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename Tag, typename Archive, typename... Type>
|
||||
ContinuousLoader & tag(Archive &archive, Type Tag::*... member) {
|
||||
attach<Tag>(archive, member...);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Helps to purge entities that no longer have a conterpart.
|
||||
*
|
||||
* Users should invoke this member function after restoring each snapshot,
|
||||
* unless they know exactly what they are doing.
|
||||
*
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
ContinuousLoader & shrink() {
|
||||
auto it = remloc.begin();
|
||||
|
||||
while(it != remloc.cend()) {
|
||||
const auto local = it->second.first;
|
||||
bool &dirty = it->second.second;
|
||||
|
||||
if(dirty) {
|
||||
dirty = false;
|
||||
++it;
|
||||
} else {
|
||||
if(registry.valid(local)) {
|
||||
registry.destroy(local);
|
||||
}
|
||||
|
||||
it = remloc.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destroys those entities that have neither components nor tags.
|
||||
*
|
||||
* In case all the entities were serialized but only part of the components
|
||||
* and tags was saved, it could happen that some of the entities have
|
||||
* neither components nor tags once restored.<br/>
|
||||
* This functions helps to identify and destroy those entities.
|
||||
*
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
ContinuousLoader & orphans() {
|
||||
registry.orphans([this](auto entity) {
|
||||
registry.destroy(entity);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Tests if a loader knows about a given entity.
|
||||
* @param entity An entity identifier.
|
||||
* @return True if `entity` is managed by the loader, false otherwise.
|
||||
*/
|
||||
bool has(entity_type entity) {
|
||||
return !(remloc.find(entity) == remloc.cend());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the identifier to which an entity refers.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that isn't managed by the loader results in
|
||||
* undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* loader doesn't knows about the entity.
|
||||
*
|
||||
* @param entity An entity identifier.
|
||||
* @return The identifier to which `entity` refers in the target registry.
|
||||
*/
|
||||
entity_type map(entity_type entity) {
|
||||
assert(has(entity));
|
||||
return remloc[entity].first;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<Entity, std::pair<Entity, bool>> remloc;
|
||||
Registry<Entity> ®istry;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_SNAPSHOT_HPP
|
||||
@@ -3,11 +3,14 @@
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <cassert>
|
||||
#include "traits.hpp"
|
||||
#include <type_traits>
|
||||
#include "entt_traits.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -54,10 +57,14 @@ template<typename Entity>
|
||||
class SparseSet<Entity> {
|
||||
using traits_type = entt_traits<Entity>;
|
||||
|
||||
struct Iterator {
|
||||
struct Iterator final {
|
||||
using difference_type = std::size_t;
|
||||
using value_type = Entity;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type;
|
||||
using iterator_category = std::input_iterator_tag;
|
||||
|
||||
Iterator(const std::vector<Entity> *direct, std::size_t pos)
|
||||
Iterator(const std::vector<value_type> &direct, std::size_t pos)
|
||||
: direct{direct}, pos{pos}
|
||||
{}
|
||||
|
||||
@@ -70,23 +77,34 @@ class SparseSet<Entity> {
|
||||
return ++(*this), orig;
|
||||
}
|
||||
|
||||
Iterator & operator+=(difference_type value) noexcept {
|
||||
pos -= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Iterator operator+(difference_type value) noexcept {
|
||||
return Iterator{direct, pos-value};
|
||||
}
|
||||
|
||||
bool operator==(const Iterator &other) const noexcept {
|
||||
return other.pos == pos && other.direct == direct;
|
||||
return other.pos == pos;
|
||||
}
|
||||
|
||||
bool operator!=(const Iterator &other) const noexcept {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
value_type operator*() const noexcept {
|
||||
return (*direct)[pos-1];
|
||||
reference operator*() const noexcept {
|
||||
return direct[pos-1];
|
||||
}
|
||||
|
||||
private:
|
||||
const std::vector<Entity> *direct;
|
||||
const std::vector<value_type> &direct;
|
||||
std::size_t pos;
|
||||
};
|
||||
|
||||
static constexpr Entity in_use = (Entity{1} << traits_type::entity_shift);
|
||||
|
||||
public:
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = Entity;
|
||||
@@ -97,24 +115,50 @@ public:
|
||||
/*! @brief Input iterator type. */
|
||||
using iterator_type = Iterator;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit SparseSet() noexcept = default;
|
||||
/*! @brief Default constructor. */
|
||||
SparseSet() noexcept = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~SparseSet() noexcept = default;
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. */
|
||||
SparseSet(const SparseSet &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
SparseSet(SparseSet &&) = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~SparseSet() noexcept = default;
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. @return This sparse set. */
|
||||
SparseSet & operator=(const SparseSet &) = delete;
|
||||
/*! @brief Default move assignment operator. @return This sparse set. */
|
||||
SparseSet & operator=(SparseSet &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Returns the number of elements in the sparse set.
|
||||
* @brief Increases the capacity of a sparse set.
|
||||
*
|
||||
* If the new capacity is greater than the current capacity, new storage is
|
||||
* allocated, otherwise the method does nothing.
|
||||
*
|
||||
* @param cap Desired capacity.
|
||||
*/
|
||||
void reserve(size_type cap) {
|
||||
direct.reserve(cap);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the extent of a sparse set.
|
||||
*
|
||||
* The extent of a sparse set is also the size of the internal sparse array.
|
||||
* There is no guarantee that the internal packed array has the same size.
|
||||
* Usually the size of the internal sparse array is equal or greater than
|
||||
* the one of the internal packed array.
|
||||
*
|
||||
* @return Extent of the sparse set.
|
||||
*/
|
||||
size_type extent() const noexcept {
|
||||
return reverse.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the number of elements in a sparse set.
|
||||
*
|
||||
* The number of elements is also the size of the internal packed array.
|
||||
* There is no guarantee that the internal sparse array has the same size.
|
||||
@@ -128,8 +172,8 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the sparse set is empty.
|
||||
* @return True is the sparse set is empty, false otherwise.
|
||||
* @brief Checks whether a sparse set is empty.
|
||||
* @return True if the sparse set is empty, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
return direct.empty();
|
||||
@@ -142,7 +186,7 @@ public:
|
||||
* always a valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order, even though `sort` has been
|
||||
* There are no guarantees on the order, even though `respect` has been
|
||||
* previously invoked. Internal data structures arrange elements to maximize
|
||||
* performance. Accessing them directly gives a performance boost but less
|
||||
* guarantees. Use `begin` and `end` if you want to iterate the sparse set
|
||||
@@ -157,48 +201,73 @@ public:
|
||||
/**
|
||||
* @brief Returns an iterator to the beginning.
|
||||
*
|
||||
* The returned iterator points to the first element of the internal packed
|
||||
* The returned iterator points to the first entity of the internal packed
|
||||
* array. If the sparse set is empty, the returned iterator will be equal to
|
||||
* `end()`.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed by a call to `sort`.
|
||||
* Input iterators stay true to the order imposed by a call to `respect`.
|
||||
*
|
||||
* @return An iterator to the first element of the internal packed array.
|
||||
* @return An iterator to the first entity of the internal packed array.
|
||||
*/
|
||||
iterator_type begin() const noexcept {
|
||||
return Iterator{&direct, direct.size()};
|
||||
return Iterator{direct, direct.size()};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the end.
|
||||
*
|
||||
* The returned iterator points to the element following the last element in
|
||||
* The returned iterator points to the element following the last entity in
|
||||
* the internal packed array. Attempting to dereference the returned
|
||||
* iterator results in undefined behavior.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed by a call to `sort`.
|
||||
* Input iterators stay true to the order imposed by a call to `respect`.
|
||||
*
|
||||
* @return An iterator to the element following the last element of the
|
||||
* @return An iterator to the element following the last entity of the
|
||||
* internal packed array.
|
||||
*/
|
||||
iterator_type end() const noexcept {
|
||||
return Iterator{&direct, 0};
|
||||
return Iterator{direct, 0};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the sparse set contains the given entity.
|
||||
* @brief Checks if a sparse set contains an entity.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return True if the sparse set contains the entity, false otherwise.
|
||||
*/
|
||||
bool has(entity_type entity) const noexcept {
|
||||
const auto entt = entity & traits_type::entity_mask;
|
||||
return entt < reverse.size() && reverse[entt] < direct.size() && direct[reverse[entt]] == entity;
|
||||
const auto pos = size_type(entity & traits_type::entity_mask);
|
||||
// the in-use control bit permits to avoid accessing the direct vector
|
||||
return (pos < reverse.size()) && (reverse[pos] & in_use);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the position of the entity in the sparse set.
|
||||
* @brief Checks if a sparse set contains an entity (unsafe).
|
||||
*
|
||||
* Alternative version of `has`. It accesses the underlying data structures
|
||||
* without bounds checking and thus it's both unsafe and risky to use.<br/>
|
||||
* You should not invoke directly this function unless you know exactly what
|
||||
* you are doing. Prefer the `has` member function instead.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the sparse set can
|
||||
* result in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode in case of
|
||||
* bounds violation.
|
||||
*
|
||||
* @param entity A valid entity identifier.
|
||||
* @return True if the sparse set contains the entity, false otherwise.
|
||||
*/
|
||||
bool fast(entity_type entity) const noexcept {
|
||||
const auto pos = size_type(entity & traits_type::entity_mask);
|
||||
assert(pos < reverse.size());
|
||||
// the in-use control bit permits to avoid accessing the direct vector
|
||||
return (reverse[pos] & in_use);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the position of an entity in a sparse set.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to get the position of an entity that doesn't belong to the
|
||||
@@ -211,11 +280,13 @@ public:
|
||||
*/
|
||||
pos_type get(entity_type entity) const noexcept {
|
||||
assert(has(entity));
|
||||
return reverse[entity & traits_type::entity_mask];
|
||||
const auto entt = entity & traits_type::entity_mask;
|
||||
// we must get rid of the in-use bit for it's not part of the position
|
||||
return reverse[entt] & traits_type::entity_mask;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns an entity to the sparse set.
|
||||
* @brief Assigns an entity to a sparse set.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to assign an entity that already belongs to the sparse set
|
||||
@@ -224,25 +295,23 @@ public:
|
||||
* sparse set already contains the given entity.
|
||||
*
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The position of the entity in the internal packed array.
|
||||
*/
|
||||
pos_type construct(entity_type entity) {
|
||||
void construct(entity_type entity) {
|
||||
assert(!has(entity));
|
||||
const auto entt = entity & traits_type::entity_mask;
|
||||
const auto pos = size_type(entity & traits_type::entity_mask);
|
||||
|
||||
if(!(entt < reverse.size())) {
|
||||
reverse.resize(entt+1);
|
||||
if(!(pos < reverse.size())) {
|
||||
reverse.resize(pos+1, pos_type{});
|
||||
}
|
||||
|
||||
const auto pos = pos_type(direct.size());
|
||||
reverse[entt] = pos;
|
||||
// we exploit the fact that pos_type is equal to entity_type and pos has
|
||||
// traits_type::version_mask bits unused we can use to mark it as in-use
|
||||
reverse[pos] = pos_type(direct.size()) | in_use;
|
||||
direct.emplace_back(entity);
|
||||
|
||||
return pos;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes the given entity from the sparse set.
|
||||
* @brief Removes an entity from a sparse set.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to remove an entity that doesn't belong to the sparse set
|
||||
@@ -256,14 +325,17 @@ public:
|
||||
assert(has(entity));
|
||||
const auto entt = entity & traits_type::entity_mask;
|
||||
const auto back = direct.back() & traits_type::entity_mask;
|
||||
const auto pos = reverse[entt];
|
||||
reverse[back] = pos;
|
||||
// we must get rid of the in-use bit for it's not part of the position
|
||||
const auto pos = reverse[entt] & traits_type::entity_mask;
|
||||
reverse[back] = reverse[entt];
|
||||
reverse[entt] = pos;
|
||||
// swapping isn't required here, we are getting rid of the last element
|
||||
direct[pos] = direct.back();
|
||||
direct.pop_back();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Swaps the position of the entities in the internal packed array.
|
||||
* @brief Swaps the position of two entities in the internal packed array.
|
||||
*
|
||||
* For what it's worth, this function affects both the internal sparse array
|
||||
* and the internal packed array. Users should not care of that anyway.
|
||||
@@ -274,49 +346,20 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* sparse set doesn't contain the given entities.
|
||||
*
|
||||
* @param lhs A valid entity identifier.
|
||||
* @param rhs A valid entity identifier.
|
||||
* @param lhs A valid position within the sparse set.
|
||||
* @param rhs A valid position within the sparse set.
|
||||
*/
|
||||
virtual void swap(entity_type lhs, entity_type rhs) {
|
||||
assert(has(lhs));
|
||||
assert(has(rhs));
|
||||
const auto le = lhs & traits_type::entity_mask;
|
||||
const auto re = rhs & traits_type::entity_mask;
|
||||
std::swap(direct[reverse[le]], direct[reverse[re]]);
|
||||
std::swap(reverse[le], reverse[re]);
|
||||
void swap(pos_type lhs, pos_type rhs) noexcept {
|
||||
assert(lhs < direct.size());
|
||||
assert(rhs < direct.size());
|
||||
const auto src = direct[lhs] & traits_type::entity_mask;
|
||||
const auto dst = direct[rhs] & traits_type::entity_mask;
|
||||
std::swap(reverse[src], reverse[dst]);
|
||||
std::swap(direct[lhs], direct[rhs]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sort entities according to the given comparison function.
|
||||
*
|
||||
* Sort the elements so that iterating the sparse set with a couple of
|
||||
* iterators returns them in the expected order. See `begin` and `end` for
|
||||
* more details.
|
||||
*
|
||||
* @note
|
||||
* Attempting to iterate elements using the raw pointer returned by `data`
|
||||
* gives no guarantees on the order, even though `sort` has been invoked.
|
||||
*
|
||||
* @tparam Compare Type of the comparison function.
|
||||
* @param compare A comparison function whose signature shall be equivalent
|
||||
* to: `bool(Entity, Entity)`.
|
||||
*/
|
||||
template<typename Compare>
|
||||
void sort(Compare compare) {
|
||||
std::vector<pos_type> copy{direct.cbegin(), direct.cend()};
|
||||
std::sort(copy.begin(), copy.end(), [compare = std::move(compare)](auto... args) {
|
||||
return !compare(args...);
|
||||
});
|
||||
|
||||
for(pos_type i = 0; i < copy.size(); ++i) {
|
||||
if(direct[i] != copy[i]) {
|
||||
swap(direct[i], copy[i]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sort entities according to their order in the given sparse set.
|
||||
* @brief Sort entities according to their order in another sparse set.
|
||||
*
|
||||
* Entities that are part of both the sparse sets are ordered internally
|
||||
* according to the order they have in `other`. All the other entities goes
|
||||
@@ -325,45 +368,36 @@ public:
|
||||
* sets by using one of them as a master and the other one as a slave.
|
||||
*
|
||||
* Iterating the sparse set with a couple of iterators returns elements in
|
||||
* the expected order after a call to `sort`. See `begin` and `end` for more
|
||||
* details.
|
||||
* the expected order after a call to `respect`. See `begin` and `end` for
|
||||
* more details.
|
||||
*
|
||||
* @note
|
||||
* Attempting to iterate elements using the raw pointer returned by `data`
|
||||
* gives no guarantees on the order, even though `sort` has been invoked.
|
||||
* gives no guarantees on the order, even though `respect` has been invoked.
|
||||
*
|
||||
* @param other The sparse sets that imposes the order of the entities.
|
||||
*/
|
||||
void respect(const SparseSet<Entity> &other) {
|
||||
struct Bool { bool value{false}; };
|
||||
std::vector<Bool> check(std::max(other.reverse.size(), reverse.size()));
|
||||
void respect(const SparseSet<Entity> &other) noexcept {
|
||||
auto from = other.begin();
|
||||
auto to = other.end();
|
||||
|
||||
for(auto entity: other.direct) {
|
||||
check[entity & traits_type::entity_mask].value = true;
|
||||
}
|
||||
pos_type pos = direct.size() - 1;
|
||||
|
||||
sort([this, &other, &check](auto lhs, auto rhs) {
|
||||
const auto le = lhs & traits_type::entity_mask;
|
||||
const auto re = rhs & traits_type::entity_mask;
|
||||
while(pos && from != to) {
|
||||
if(has(*from)) {
|
||||
if(*from != direct[pos]) {
|
||||
swap(pos, get(*from));
|
||||
}
|
||||
|
||||
const bool bLhs = check[le].value;
|
||||
const bool bRhs = check[re].value;
|
||||
bool compare = false;
|
||||
|
||||
if(bLhs && bRhs) {
|
||||
compare = other.get(rhs) < other.get(lhs);
|
||||
} else if(!bLhs && !bRhs) {
|
||||
compare = re < le;
|
||||
} else {
|
||||
compare = bLhs;
|
||||
--pos;
|
||||
}
|
||||
|
||||
return compare;
|
||||
});
|
||||
++from;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets the sparse set.
|
||||
* @brief Resets a sparse set.
|
||||
*/
|
||||
virtual void reset() {
|
||||
reverse.clear();
|
||||
@@ -371,7 +405,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<entity_type> reverse;
|
||||
std::vector<pos_type> reverse;
|
||||
std::vector<entity_type> direct;
|
||||
};
|
||||
|
||||
@@ -402,9 +436,59 @@ template<typename Entity, typename Type>
|
||||
class SparseSet<Entity, Type>: public SparseSet<Entity> {
|
||||
using underlying_type = SparseSet<Entity>;
|
||||
|
||||
struct Iterator final {
|
||||
using difference_type = std::size_t;
|
||||
using value_type = Type;
|
||||
using pointer = value_type *;
|
||||
using reference = value_type &;
|
||||
using iterator_category = std::input_iterator_tag;
|
||||
|
||||
Iterator(std::vector<value_type> &instances, std::size_t pos)
|
||||
: instances{instances}, pos{pos}
|
||||
{}
|
||||
|
||||
Iterator & operator++() noexcept {
|
||||
return --pos, *this;
|
||||
}
|
||||
|
||||
Iterator operator++(int) noexcept {
|
||||
Iterator orig = *this;
|
||||
return ++(*this), orig;
|
||||
}
|
||||
|
||||
Iterator & operator+=(difference_type value) noexcept {
|
||||
pos -= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
Iterator operator+(difference_type value) noexcept {
|
||||
return Iterator{instances, pos-value};
|
||||
}
|
||||
|
||||
bool operator==(const Iterator &other) const noexcept {
|
||||
return other.pos == pos;
|
||||
}
|
||||
|
||||
bool operator!=(const Iterator &other) const noexcept {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
reference operator*() noexcept {
|
||||
return instances[pos-1];
|
||||
}
|
||||
|
||||
pointer operator->() noexcept {
|
||||
return &instances.data()[pos-1];
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<value_type> &instances;
|
||||
std::size_t pos;
|
||||
};
|
||||
|
||||
public:
|
||||
/*! @brief Type of the objects associated to the entities. */
|
||||
using type = Type;
|
||||
using object_type = Type;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename underlying_type::entity_type;
|
||||
/*! @brief Entity dependent position type. */
|
||||
@@ -412,10 +496,10 @@ public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename underlying_type::size_type;
|
||||
/*! @brief Input iterator type. */
|
||||
using iterator_type = typename underlying_type::iterator_type;
|
||||
using iterator_type = Iterator;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit SparseSet() noexcept = default;
|
||||
/*! @brief Default constructor. */
|
||||
SparseSet() noexcept = default;
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. */
|
||||
SparseSet(const SparseSet &) = delete;
|
||||
@@ -427,6 +511,19 @@ public:
|
||||
/*! @brief Default move assignment operator. @return This sparse set. */
|
||||
SparseSet & operator=(SparseSet &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Increases the capacity of a sparse set.
|
||||
*
|
||||
* If the new capacity is greater than the current capacity, new storage is
|
||||
* allocated, otherwise the method does nothing.
|
||||
*
|
||||
* @param cap Desired capacity.
|
||||
*/
|
||||
void reserve(size_type cap) {
|
||||
underlying_type::reserve(cap);
|
||||
instances.reserve(cap);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the array of objects.
|
||||
*
|
||||
@@ -434,15 +531,15 @@ public:
|
||||
* always a valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order, even though `sort` has been
|
||||
* previously invoked. Internal data structures arrange elements to maximize
|
||||
* performance. Accessing them directly gives a performance boost but less
|
||||
* guarantees. Use `begin` and `end` if you want to iterate the sparse set
|
||||
* in the expected order.
|
||||
* There are no guarantees on the order, even though either `sort` or
|
||||
* `respect` has been previously invoked. Internal data structures arrange
|
||||
* elements to maximize performance. Accessing them directly gives a
|
||||
* performance boost but less guarantees. Use `begin` and `end` if you want
|
||||
* to iterate the sparse set in the expected order.
|
||||
*
|
||||
* @return A pointer to the array of objects.
|
||||
*/
|
||||
const type * raw() const noexcept {
|
||||
const object_type * raw() const noexcept {
|
||||
return instances.data();
|
||||
}
|
||||
|
||||
@@ -453,20 +550,54 @@ public:
|
||||
* always a valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order, even though `sort` has been
|
||||
* previously invoked. Internal data structures arrange elements to maximize
|
||||
* performance. Accessing them directly gives a performance boost but less
|
||||
* guarantees. Use `begin` and `end` if you want to iterate the sparse set
|
||||
* in the expected order.
|
||||
* There are no guarantees on the order, even though either `sort` or
|
||||
* `respect` has been previously invoked. Internal data structures arrange
|
||||
* elements to maximize performance. Accessing them directly gives a
|
||||
* performance boost but less guarantees. Use `begin` and `end` if you want
|
||||
* to iterate the sparse set in the expected order.
|
||||
*
|
||||
* @return A pointer to the array of objects.
|
||||
*/
|
||||
type * raw() noexcept {
|
||||
object_type * raw() noexcept {
|
||||
return instances.data();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated to the given entity.
|
||||
* @brief Returns an iterator to the beginning.
|
||||
*
|
||||
* The returned iterator points to the first instance of the given type. If
|
||||
* the sparse set is empty, the returned iterator will be equal to `end()`.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed by a call to either `sort`
|
||||
* or `respect`.
|
||||
*
|
||||
* @return An iterator to the first instance of the given type.
|
||||
*/
|
||||
iterator_type begin() noexcept {
|
||||
return Iterator{instances, instances.size()};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the end.
|
||||
*
|
||||
* The returned iterator points to the element following the last instance
|
||||
* of the given type. Attempting to dereference the returned iterator
|
||||
* results in undefined behavior.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed by a call to either `sort`
|
||||
* or `respect`.
|
||||
*
|
||||
* @return An iterator to the element following the last instance of the
|
||||
* given type.
|
||||
*/
|
||||
iterator_type end() noexcept {
|
||||
return Iterator{instances, 0};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated to an entity.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the sparse set results
|
||||
@@ -477,12 +608,12 @@ public:
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The object associated to the entity.
|
||||
*/
|
||||
const type & get(entity_type entity) const noexcept {
|
||||
const object_type & get(entity_type entity) const noexcept {
|
||||
return instances[underlying_type::get(entity)];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated to the given entity.
|
||||
* @brief Returns the object associated to an entity.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the sparse set results
|
||||
@@ -493,12 +624,18 @@ public:
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The object associated to the entity.
|
||||
*/
|
||||
type & get(entity_type entity) noexcept {
|
||||
return const_cast<type &>(const_cast<const SparseSet *>(this)->get(entity));
|
||||
object_type & get(entity_type entity) noexcept {
|
||||
return const_cast<object_type &>(const_cast<const SparseSet *>(this)->get(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns an entity to the sparse set and constructs its object.
|
||||
* @brief Assigns an entity to a sparse set and constructs its object.
|
||||
*
|
||||
* @note
|
||||
* _Sfinae'd_ function.<br/>
|
||||
* This version is used for types that can be constructed in place directly.
|
||||
* It doesn't work well with aggregates because of the placement new usually
|
||||
* performed under the hood during an _emplace back_.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that already belongs to the sparse set
|
||||
@@ -512,14 +649,43 @@ public:
|
||||
* @return The object associated to the entity.
|
||||
*/
|
||||
template<typename... Args>
|
||||
type & construct(entity_type entity, Args&&... args) {
|
||||
std::enable_if_t<std::is_constructible<Type, Args...>::value, object_type &>
|
||||
construct(entity_type entity, Args &&... args) {
|
||||
underlying_type::construct(entity);
|
||||
instances.push_back({ std::forward<Args>(args)... });
|
||||
instances.emplace_back(std::forward<Args>(args)...);
|
||||
return instances.back();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an entity from the sparse set and destroies its object.
|
||||
* @brief Assigns an entity to a sparse set and constructs its object.
|
||||
*
|
||||
* @note
|
||||
* _Sfinae'd_ function.<br/>
|
||||
* Fallback for aggregates and types in general that do not work well with a
|
||||
* placement new as performed usually under the hood during an
|
||||
* _emplace back_.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that already belongs to the sparse set
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* sparse set already contains the given entity.
|
||||
*
|
||||
* @tparam Args Types of arguments to use to construct the object.
|
||||
* @param entity A valid entity identifier.
|
||||
* @param args Parameters to use to construct an object for the entity.
|
||||
* @return The object associated to the entity.
|
||||
*/
|
||||
template<typename... Args>
|
||||
std::enable_if_t<!std::is_constructible<Type, Args...>::value, object_type &>
|
||||
construct(entity_type entity, Args &&... args) {
|
||||
underlying_type::construct(entity);
|
||||
instances.emplace_back(Type{std::forward<Args>(args)...});
|
||||
return instances.back();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an entity from a sparse set and destroies its object.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the sparse set results
|
||||
@@ -530,34 +696,111 @@ public:
|
||||
* @param entity A valid entity identifier.
|
||||
*/
|
||||
void destroy(entity_type entity) override {
|
||||
instances[underlying_type::get(entity)] = std::move(instances.back());
|
||||
// swapping isn't required here, we are getting rid of the last element
|
||||
// however, we must protect ourselves from self assignments (see #37)
|
||||
auto tmp = std::move(instances.back());
|
||||
instances[underlying_type::get(entity)] = std::move(tmp);
|
||||
instances.pop_back();
|
||||
underlying_type::destroy(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Swaps the two entities and their objects.
|
||||
* @brief Sort components according to the given comparison function.
|
||||
*
|
||||
* Sort the elements so that iterating the sparse set with a couple of
|
||||
* iterators returns them in the expected order. See `begin` and `end` for
|
||||
* more details.
|
||||
*
|
||||
* The comparison function object must return `true` if the first element
|
||||
* is _less_ than the second one, `false` otherwise. The signature of the
|
||||
* comparison function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* bool(const Type &, const Type &)
|
||||
* @endcode
|
||||
*
|
||||
* @note
|
||||
* This function doesn't swap objects between entities. It exchanges entity
|
||||
* and object positions in the sparse set. It's used mainly for sorting.
|
||||
* Attempting to iterate elements using a raw pointer returned by a call to
|
||||
* either `data` or `raw` gives no guarantees on the order, even though
|
||||
* `sort` has been invoked.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use entities that don't belong to the sparse set results
|
||||
* in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* sparse set doesn't contain the given entities.
|
||||
*
|
||||
* @param lhs A valid entity identifier.
|
||||
* @param rhs A valid entity identifier.
|
||||
* @tparam Compare Type of comparison function object.
|
||||
* @param compare A valid comparison function object.
|
||||
*/
|
||||
void swap(entity_type lhs, entity_type rhs) override {
|
||||
std::swap(instances[underlying_type::get(lhs)], instances[underlying_type::get(rhs)]);
|
||||
underlying_type::swap(lhs, rhs);
|
||||
template<typename Compare>
|
||||
void sort(Compare compare) {
|
||||
std::vector<pos_type> copy(instances.size());
|
||||
std::iota(copy.begin(), copy.end(), 0);
|
||||
|
||||
std::sort(copy.begin(), copy.end(), [this, compare = std::move(compare)](auto lhs, auto rhs) {
|
||||
return compare(const_cast<const object_type &>(instances[rhs]), const_cast<const object_type &>(instances[lhs]));
|
||||
});
|
||||
|
||||
for(pos_type pos = 0, last = copy.size(); pos < last; ++pos) {
|
||||
auto curr = pos;
|
||||
auto next = copy[curr];
|
||||
|
||||
while(curr != next) {
|
||||
const auto lhs = copy[curr];
|
||||
const auto rhs = copy[next];
|
||||
std::swap(instances[lhs], instances[rhs]);
|
||||
underlying_type::swap(lhs, rhs);
|
||||
copy[curr] = curr;
|
||||
curr = next;
|
||||
next = copy[curr];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets the sparse set.
|
||||
* @brief Sort components according to the order of the entities in another
|
||||
* sparse set.
|
||||
*
|
||||
* Entities that are part of both the sparse sets are ordered internally
|
||||
* according to the order they have in `other`. All the other entities goes
|
||||
* to the end of the list and there are no guarantess on their order.
|
||||
* Components are sorted according to the entities to which they
|
||||
* belong.<br/>
|
||||
* In other terms, this function can be used to impose the same order on two
|
||||
* sets by using one of them as a master and the other one as a slave.
|
||||
*
|
||||
* Iterating the sparse set with a couple of iterators returns elements in
|
||||
* the expected order after a call to `respect`. See `begin` and `end` for
|
||||
* more details.
|
||||
*
|
||||
* @note
|
||||
* Attempting to iterate elements using a raw pointer returned by a call to
|
||||
* either `data` or `raw` gives no guarantees on the order, even though
|
||||
* `respect` has been invoked.
|
||||
*
|
||||
* @param other The sparse sets that imposes the order of the entities.
|
||||
*/
|
||||
void respect(const SparseSet<Entity> &other) noexcept {
|
||||
auto from = other.begin();
|
||||
auto to = other.end();
|
||||
|
||||
pos_type pos = underlying_type::size() - 1;
|
||||
const auto *local = underlying_type::data();
|
||||
|
||||
while(pos && from != to) {
|
||||
const auto curr = *from;
|
||||
|
||||
if(underlying_type::has(curr)) {
|
||||
if(curr != *(local + pos)) {
|
||||
auto candidate = underlying_type::get(curr);
|
||||
std::swap(instances[pos], instances[candidate]);
|
||||
underlying_type::swap(pos, candidate);
|
||||
}
|
||||
|
||||
--pos;
|
||||
}
|
||||
|
||||
++from;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets a sparse set.
|
||||
*/
|
||||
void reset() override {
|
||||
underlying_type::reset();
|
||||
@@ -565,7 +808,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<type> instances;
|
||||
std::vector<object_type> instances;
|
||||
};
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,12 +1,21 @@
|
||||
#include "core/family.hpp"
|
||||
#include "core/hashed_string.hpp"
|
||||
#include "core/ident.hpp"
|
||||
#include "entity/actor.hpp"
|
||||
#include "entity/entt_traits.hpp"
|
||||
#include "entity/registry.hpp"
|
||||
#include "entity/snapshot.hpp"
|
||||
#include "entity/sparse_set.hpp"
|
||||
#include "entity/traits.hpp"
|
||||
#include "entity/view.hpp"
|
||||
#include "locator/locator.hpp"
|
||||
#include "process/process.hpp"
|
||||
#include "process/scheduler.hpp"
|
||||
#include "resource/cache.hpp"
|
||||
#include "resource/handle.hpp"
|
||||
#include "resource/loader.hpp"
|
||||
#include "signal/bus.hpp"
|
||||
#include "signal/delegate.hpp"
|
||||
#include "signal/dispatcher.hpp"
|
||||
#include "signal/emitter.hpp"
|
||||
#include "signal/sigh.hpp"
|
||||
#include "signal/signal.hpp"
|
||||
|
||||
@@ -14,7 +14,7 @@ namespace entt {
|
||||
* @brief Service locator, nothing more.
|
||||
*
|
||||
* A service locator can be used to do what it promises: locate services.<br/>
|
||||
* Usually service locators are tighly bound to the services they expose and
|
||||
* Usually service locators are tightly bound to the services they expose and
|
||||
* thus it's hard to define a general purpose class to do that. This template
|
||||
* based implementation tries to fill the gap and to get rid of the burden of
|
||||
* defining a different specific locator for each application.
|
||||
@@ -78,7 +78,7 @@ struct ServiceLocator final {
|
||||
* @param args Parameters to use to construct the service.
|
||||
*/
|
||||
template<typename Impl = Service, typename... Args>
|
||||
inline static void set(Args&&... args) {
|
||||
inline static void set(Args &&... args) {
|
||||
service = std::make_shared<Impl>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
|
||||
338
src/entt/process/process.hpp
Normal file
338
src/entt/process/process.hpp
Normal file
@@ -0,0 +1,338 @@
|
||||
#ifndef ENTT_PROCESS_PROCESS_HPP
|
||||
#define ENTT_PROCESS_PROCESS_HPP
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Base class for processes.
|
||||
*
|
||||
* This class stays true to the CRTP idiom. Derived classes must specify what's
|
||||
* the intended type for elapsed times.<br/>
|
||||
* A process should expose publicly the following member functions whether
|
||||
* required:
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void update(Delta, void *);
|
||||
* @endcode
|
||||
*
|
||||
* It's invoked once per tick until a process is explicitly aborted or it
|
||||
* terminates either with or without errors. Even though it's not mandatory to
|
||||
* declare this member function, as a rule of thumb each process should at
|
||||
* least define it to work properly. The `void *` parameter is an opaque
|
||||
* pointer to user data (if any) forwarded directly to the process during an
|
||||
* update.
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void init(void *);
|
||||
* @endcode
|
||||
*
|
||||
* It's invoked at the first tick, immediately before an update. The `void *`
|
||||
* parameter is an opaque pointer to user data (if any) forwarded directly to
|
||||
* the process during an update.
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void succeeded();
|
||||
* @endcode
|
||||
*
|
||||
* It's invoked in case of success, immediately after an update and during the
|
||||
* same tick.
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void failed();
|
||||
* @endcode
|
||||
*
|
||||
* It's invoked in case of errors, immediately after an update and during the
|
||||
* same tick.
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void aborted();
|
||||
* @endcode
|
||||
*
|
||||
* It's invoked only if a process is explicitly aborted. There is no guarantee
|
||||
* that it executes in the same tick, this depends solely on whether the
|
||||
* process is aborted immediately or not.
|
||||
*
|
||||
* Derived classes can change the internal state of a process by invoking the
|
||||
* `succeed` and `fail` protected member functions and even pause or unpause the
|
||||
* process itself.
|
||||
*
|
||||
* @sa Scheduler
|
||||
*
|
||||
* @tparam Derived Actual type of process that extends the class template.
|
||||
* @tparam Delta Type to use to provide elapsed time.
|
||||
*/
|
||||
template<typename Derived, typename Delta>
|
||||
class Process {
|
||||
enum class State: unsigned int {
|
||||
UNINITIALIZED = 0,
|
||||
RUNNING,
|
||||
PAUSED,
|
||||
SUCCEEDED,
|
||||
FAILED,
|
||||
ABORTED,
|
||||
FINISHED
|
||||
};
|
||||
|
||||
template<State state>
|
||||
using tag = std::integral_constant<State, state>;
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::UNINITIALIZED>, void *data)
|
||||
-> decltype(std::declval<Target>().init(data)) {
|
||||
static_cast<Target *>(this)->init(data);
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::RUNNING>, Delta delta, void *data)
|
||||
-> decltype(std::declval<Target>().update(delta, data)) {
|
||||
static_cast<Target *>(this)->update(delta, data);
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::SUCCEEDED>)
|
||||
-> decltype(std::declval<Target>().succeeded()) {
|
||||
static_cast<Target *>(this)->succeeded();
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::FAILED>)
|
||||
-> decltype(std::declval<Target>().failed()) {
|
||||
static_cast<Target *>(this)->failed();
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::ABORTED>)
|
||||
-> decltype(std::declval<Target>().aborted()) {
|
||||
static_cast<Target *>(this)->aborted();
|
||||
}
|
||||
|
||||
template<State S, typename... Args>
|
||||
void tick(char, tag<S>, Args &&...) {}
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Terminates a process with success if it's still alive.
|
||||
*
|
||||
* The function is idempotent and it does nothing if the process isn't
|
||||
* alive.
|
||||
*/
|
||||
void succeed() noexcept {
|
||||
if(alive()) {
|
||||
current = State::SUCCEEDED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Terminates a process with errors if it's still alive.
|
||||
*
|
||||
* The function is idempotent and it does nothing if the process isn't
|
||||
* alive.
|
||||
*/
|
||||
void fail() noexcept {
|
||||
if(alive()) {
|
||||
current = State::FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Stops a process if it's in a running state.
|
||||
*
|
||||
* The function is idempotent and it does nothing if the process isn't
|
||||
* running.
|
||||
*/
|
||||
void pause() noexcept {
|
||||
if(current == State::RUNNING) {
|
||||
current = State::PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Restarts a process if it's paused.
|
||||
*
|
||||
* The function is idempotent and it does nothing if the process isn't
|
||||
* paused.
|
||||
*/
|
||||
void unpause() noexcept {
|
||||
if(current == State::PAUSED) {
|
||||
current = State::RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Type used to provide elapsed time. */
|
||||
using delta_type = Delta;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Process() noexcept {
|
||||
static_assert(std::is_base_of<Process, Derived>::value, "!");
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Aborts a process if it's still alive.
|
||||
*
|
||||
* The function is idempotent and it does nothing if the process isn't
|
||||
* alive.
|
||||
*
|
||||
* @param immediately Requests an immediate operation.
|
||||
*/
|
||||
void abort(bool immediately = false) noexcept {
|
||||
if(alive()) {
|
||||
current = State::ABORTED;
|
||||
|
||||
if(immediately) {
|
||||
tick(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if a process is either running or paused.
|
||||
* @return True if the process is still alive, false otherwise.
|
||||
*/
|
||||
bool alive() const noexcept {
|
||||
return current == State::RUNNING || current == State::PAUSED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if a process is already terminated.
|
||||
* @return True if the process is terminated, false otherwise.
|
||||
*/
|
||||
bool dead() const noexcept {
|
||||
return current == State::FINISHED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if a process is currently paused.
|
||||
* @return True if the process is paused, false otherwise.
|
||||
*/
|
||||
bool paused() const noexcept {
|
||||
return current == State::PAUSED;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if a process terminated with errors.
|
||||
* @return True if the process terminated with errors, false otherwise.
|
||||
*/
|
||||
bool rejected() const noexcept {
|
||||
return stopped;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates a process and its internal state if required.
|
||||
* @param delta Elapsed time.
|
||||
* @param data Optional data.
|
||||
*/
|
||||
void tick(Delta delta, void *data = nullptr) {
|
||||
switch (current) {
|
||||
case State::UNINITIALIZED:
|
||||
tick(0, tag<State::UNINITIALIZED>{}, data);
|
||||
current = State::RUNNING;
|
||||
// no break on purpose, tasks are executed immediately
|
||||
case State::RUNNING:
|
||||
tick(0, tag<State::RUNNING>{}, delta, data);
|
||||
default:
|
||||
// suppress warnings
|
||||
break;
|
||||
}
|
||||
|
||||
// if it's dead, it must be notified and removed immediately
|
||||
switch(current) {
|
||||
case State::SUCCEEDED:
|
||||
tick(0, tag<State::SUCCEEDED>{});
|
||||
current = State::FINISHED;
|
||||
break;
|
||||
case State::FAILED:
|
||||
tick(0, tag<State::FAILED>{});
|
||||
current = State::FINISHED;
|
||||
stopped = true;
|
||||
break;
|
||||
case State::ABORTED:
|
||||
tick(0, tag<State::ABORTED>{});
|
||||
current = State::FINISHED;
|
||||
stopped = true;
|
||||
break;
|
||||
default:
|
||||
// suppress warnings
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
State current{State::UNINITIALIZED};
|
||||
bool stopped{false};
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Adaptor for lambdas and functors to turn them into processes.
|
||||
*
|
||||
* Lambdas and functors can't be used directly with a scheduler for they are not
|
||||
* properly defined processes with managed life cycles.<br/>
|
||||
* This class helps in filling the gap and turning lambdas and functors into
|
||||
* full featured processes usable by a scheduler.
|
||||
*
|
||||
* The signature of the function call operator should be equivalent to the
|
||||
* following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(Delta delta, void *data, auto succeed, auto fail);
|
||||
* @endcode
|
||||
*
|
||||
* Where:
|
||||
*
|
||||
* * `delta` is the elapsed time.
|
||||
* * `data` is an opaque pointer to user data if any, `nullptr` otherwise.
|
||||
* * `succeed` is a function to call when a process terminates with success.
|
||||
* * `fail` is a function to call when a process terminates with errors.
|
||||
*
|
||||
* The signature of the function call operator of both `succeed` and `fail`
|
||||
* is equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void();
|
||||
* @endcode
|
||||
*
|
||||
* Usually users shouldn't worry about creating adaptors. A scheduler will
|
||||
* create them internally each and avery time a lambda or a functor is used as
|
||||
* a process.
|
||||
*
|
||||
* @sa Process
|
||||
* @sa Scheduler
|
||||
*
|
||||
* @tparam Func Actual type of process.
|
||||
* @tparam Delta Type to use to provide elapsed time.
|
||||
*/
|
||||
template<typename Func, typename Delta>
|
||||
struct ProcessAdaptor: Process<ProcessAdaptor<Func, Delta>, Delta>, private Func {
|
||||
/**
|
||||
* @brief Constructs a process adaptor from a lambda or a functor.
|
||||
* @tparam Args Types of arguments to use to initialize the actual process.
|
||||
* @param args Parameters to use to initialize the actual process.
|
||||
*/
|
||||
template<typename... Args>
|
||||
ProcessAdaptor(Args &&... args)
|
||||
: Func{std::forward<Args>(args)...}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Updates a process and its internal state if required.
|
||||
* @param delta Elapsed time.
|
||||
* @param data Optional data.
|
||||
*/
|
||||
void update(Delta delta, void *data) {
|
||||
Func::operator()(delta, data, [this]() { this->succeed(); }, [this]() { this->fail(); });
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_PROCESS_PROCESS_HPP
|
||||
320
src/entt/process/scheduler.hpp
Normal file
320
src/entt/process/scheduler.hpp
Normal file
@@ -0,0 +1,320 @@
|
||||
#ifndef ENTT_PROCESS_SCHEDULER_HPP
|
||||
#define ENTT_PROCESS_SCHEDULER_HPP
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
#include "process.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Cooperative scheduler for processes.
|
||||
*
|
||||
* A cooperative scheduler runs processes and helps managing their life cycles.
|
||||
*
|
||||
* Each process is invoked once per tick. If a process terminates, it's
|
||||
* removed automatically from the scheduler and it's never invoked again.<br/>
|
||||
* A process can also have a child. In this case, the process is replaced with
|
||||
* its child when it terminates if it returns with success. In case of errors,
|
||||
* both the process and its child are discarded.
|
||||
*
|
||||
* Example of use (pseudocode):
|
||||
*
|
||||
* @code{.cpp}
|
||||
* scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
|
||||
* // code
|
||||
* }).then<MyProcess>(arguments...);
|
||||
* @endcode
|
||||
*
|
||||
* In order to invoke all scheduled processes, call the `update` member function
|
||||
* passing it the elapsed time to forward to the tasks.
|
||||
*
|
||||
* @sa Process
|
||||
*
|
||||
* @tparam Delta Type to use to provide elapsed time.
|
||||
*/
|
||||
template<typename Delta>
|
||||
class Scheduler final {
|
||||
template<typename T>
|
||||
struct tag { using type = T; };
|
||||
|
||||
struct ProcessHandler final {
|
||||
using instance_type = std::unique_ptr<void, void(*)(void *)>;
|
||||
using update_type = bool(*)(ProcessHandler &, Delta, void *);
|
||||
using abort_type = void(*)(ProcessHandler &, bool);
|
||||
using next_type = std::unique_ptr<ProcessHandler>;
|
||||
|
||||
instance_type instance;
|
||||
update_type update;
|
||||
abort_type abort;
|
||||
next_type next;
|
||||
};
|
||||
|
||||
template<typename Lambda>
|
||||
struct Then final: Lambda {
|
||||
Then(Lambda &&lambda, ProcessHandler *handler)
|
||||
: Lambda{std::forward<Lambda>(lambda)}, handler{handler}
|
||||
{}
|
||||
|
||||
template<typename Proc, typename... Args>
|
||||
decltype(auto) then(Args &&... args) && {
|
||||
static_assert(std::is_base_of<Process<Proc, Delta>, Proc>::value, "!");
|
||||
handler = Lambda::operator()(handler, tag<Proc>{}, std::forward<Args>(args)...);
|
||||
return std::move(*this);
|
||||
}
|
||||
|
||||
template<typename Func>
|
||||
decltype(auto) then(Func &&func) && {
|
||||
using Proc = ProcessAdaptor<std::decay_t<Func>, Delta>;
|
||||
return std::move(*this).template then<Proc>(std::forward<Func>(func));
|
||||
}
|
||||
|
||||
private:
|
||||
ProcessHandler *handler;
|
||||
};
|
||||
|
||||
template<typename Proc>
|
||||
static bool update(ProcessHandler &handler, Delta delta, void *data) {
|
||||
auto *process = static_cast<Proc *>(handler.instance.get());
|
||||
process->tick(delta, data);
|
||||
|
||||
auto dead = process->dead();
|
||||
|
||||
if(dead) {
|
||||
if(handler.next && !process->rejected()) {
|
||||
handler = std::move(*handler.next);
|
||||
dead = handler.update(handler, delta, data);
|
||||
} else {
|
||||
handler.instance.reset();
|
||||
}
|
||||
}
|
||||
|
||||
return dead;
|
||||
}
|
||||
|
||||
template<typename Proc>
|
||||
static void abort(ProcessHandler &handler, bool immediately) {
|
||||
static_cast<Proc *>(handler.instance.get())->abort(immediately);
|
||||
}
|
||||
|
||||
template<typename Proc>
|
||||
static void deleter(void *proc) {
|
||||
delete static_cast<Proc *>(proc);
|
||||
}
|
||||
|
||||
auto then(ProcessHandler *handler) {
|
||||
auto lambda = [](ProcessHandler *handler, auto next, auto... args) {
|
||||
using Proc = typename decltype(next)::type;
|
||||
|
||||
if(handler) {
|
||||
auto proc = typename ProcessHandler::instance_type{new Proc{std::forward<decltype(args)>(args)...}, &Scheduler::deleter<Proc>};
|
||||
handler->next.reset(new ProcessHandler{std::move(proc), &Scheduler::update<Proc>, &Scheduler::abort<Proc>, nullptr});
|
||||
handler = handler->next.get();
|
||||
}
|
||||
|
||||
return handler;
|
||||
};
|
||||
|
||||
return Then<decltype(lambda)>{std::move(lambda), handler};
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename std::vector<ProcessHandler>::size_type;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Scheduler() noexcept= default;
|
||||
|
||||
/*! @brief Copying a scheduler isn't allowed. */
|
||||
Scheduler(const Scheduler &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Scheduler(Scheduler &&) = default;
|
||||
|
||||
/*! @brief Copying a scheduler isn't allowed. @return This scheduler. */
|
||||
Scheduler & operator=(const Scheduler &) = delete;
|
||||
/*! @brief Default move assignament operator. @return This scheduler. */
|
||||
Scheduler & operator=(Scheduler &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Number of processes currently scheduled.
|
||||
* @return Number of processes currently scheduled.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return handlers.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if at least a process is currently scheduled.
|
||||
* @return True if there are scheduled processes, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
return handlers.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Discards all scheduled processes.
|
||||
*
|
||||
* Processes aren't aborted. They are discarded along with their children
|
||||
* and never executed again.
|
||||
*/
|
||||
void clear() {
|
||||
handlers.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Schedules a process for the next tick.
|
||||
*
|
||||
* Returned value is an opaque object that can be used to attach a child to
|
||||
* the given process. The child is automatically scheduled when the process
|
||||
* terminates and only if the process returns with success.
|
||||
*
|
||||
* Example of use (pseudocode):
|
||||
*
|
||||
* @code{.cpp}
|
||||
* // schedules a task in the form of a process class
|
||||
* scheduler.attach<MyProcess>(arguments...)
|
||||
* // appends a child in the form of a lambda function
|
||||
* .then([](auto delta, void *, auto succeed, auto fail) {
|
||||
* // code
|
||||
* })
|
||||
* // appends a child in the form of another process class
|
||||
* .then<MyOtherProcess>();
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Proc Type of process to schedule.
|
||||
* @tparam Args Types of arguments to use to initialize the process.
|
||||
* @param args Parameters to use to initialize the process.
|
||||
* @return An opaque object to use to concatenate processes.
|
||||
*/
|
||||
template<typename Proc, typename... Args>
|
||||
auto attach(Args &&... args) {
|
||||
static_assert(std::is_base_of<Process<Proc, Delta>, Proc>::value, "!");
|
||||
|
||||
auto proc = typename ProcessHandler::instance_type{new Proc{std::forward<Args>(args)...}, &Scheduler::deleter<Proc>};
|
||||
ProcessHandler handler{std::move(proc), &Scheduler::update<Proc>, &Scheduler::abort<Proc>, nullptr};
|
||||
handlers.push_back(std::move(handler));
|
||||
|
||||
return then(&handlers.back());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Schedules a process for the next tick.
|
||||
*
|
||||
* A process can be either a lambda or a functor. The scheduler wraps both
|
||||
* of them in a process adaptor internally.<br/>
|
||||
* The signature of the function call operator should be equivalent to the
|
||||
* following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(Delta delta, auto succeed, auto fail);
|
||||
* @endcode
|
||||
*
|
||||
* Where:
|
||||
*
|
||||
* * `delta` is the elapsed time.
|
||||
* * `succeed` is a function to call when a process terminates with success.
|
||||
* * `fail` is a function to call when a process terminates with errors.
|
||||
*
|
||||
* The signature of the function call operator of both `succeed` and `fail`
|
||||
* is equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void();
|
||||
* @endcode
|
||||
*
|
||||
* Returned value is an opaque object that can be used to attach a child to
|
||||
* the given process. The child is automatically scheduled when the process
|
||||
* terminates and only if the process returns with success.
|
||||
*
|
||||
* Example of use (pseudocode):
|
||||
*
|
||||
* @code{.cpp}
|
||||
* // schedules a task in the form of a lambda function
|
||||
* scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
|
||||
* // code
|
||||
* })
|
||||
* // appends a child in the form of another lambda function
|
||||
* .then([](auto delta, void *, auto succeed, auto fail) {
|
||||
* // code
|
||||
* })
|
||||
* // appends a child in the form of a process class
|
||||
* .then<MyProcess>(arguments...);
|
||||
* @endcode
|
||||
*
|
||||
* @sa ProcessAdaptor
|
||||
*
|
||||
* @tparam Func Type of process to schedule.
|
||||
* @param func Either a lambda or a functor to use as a process.
|
||||
* @return An opaque object to use to concatenate processes.
|
||||
*/
|
||||
template<typename Func>
|
||||
auto attach(Func &&func) {
|
||||
using Proc = ProcessAdaptor<std::decay_t<Func>, Delta>;
|
||||
return attach<Proc>(std::forward<Func>(func));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Updates all scheduled processes.
|
||||
*
|
||||
* All scheduled processes are executed in no specific order.<br/>
|
||||
* If a process terminates with success, it's replaced with its child, if
|
||||
* any. Otherwise, if a process terminates with an error, it's removed along
|
||||
* with its child.
|
||||
*
|
||||
* @param delta Elapsed time.
|
||||
* @param data Optional data.
|
||||
*/
|
||||
void update(Delta delta, void *data = nullptr) {
|
||||
bool clean = false;
|
||||
|
||||
for(auto pos = handlers.size(); pos; --pos) {
|
||||
auto &handler = handlers[pos-1];
|
||||
const bool dead = handler.update(handler, delta, data);
|
||||
clean = clean || dead;
|
||||
}
|
||||
|
||||
if(clean) {
|
||||
handlers.erase(std::remove_if(handlers.begin(), handlers.end(), [](auto &handler) {
|
||||
return !handler.instance;
|
||||
}), handlers.end());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Aborts all scheduled processes.
|
||||
*
|
||||
* Unless an immediate operation is requested, the abort is scheduled for
|
||||
* the next tick. Processes won't be executed anymore in any case.<br/>
|
||||
* Once a process is fully aborted and thus finished, it's discarded along
|
||||
* with its child, if any.
|
||||
*
|
||||
* @param immediately Requests an immediate operation.
|
||||
*/
|
||||
void abort(bool immediately = false) {
|
||||
decltype(handlers) exec;
|
||||
exec.swap(handlers);
|
||||
|
||||
std::for_each(exec.begin(), exec.end(), [immediately](auto &handler) {
|
||||
handler.abort(handler, immediately);
|
||||
});
|
||||
|
||||
std::move(handlers.begin(), handlers.end(), std::back_inserter(exec));
|
||||
handlers.swap(exec);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<ProcessHandler> handlers{};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_PROCESS_SCHEDULER_HPP
|
||||
200
src/entt/resource/cache.hpp
Normal file
200
src/entt/resource/cache.hpp
Normal file
@@ -0,0 +1,200 @@
|
||||
#ifndef ENTT_RESOURCE_CACHE_HPP
|
||||
#define ENTT_RESOURCE_CACHE_HPP
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include "../core/hashed_string.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "loader.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Simple cache for resources of a given type.
|
||||
*
|
||||
* Minimal implementation of a cache for resources of a given type. It doesn't
|
||||
* offer much functionalities but it's suitable for small or medium sized
|
||||
* applications and can be freely inherited to add targeted functionalities for
|
||||
* large sized applications.
|
||||
*
|
||||
* @tparam Resource Type of resources managed by a cache.
|
||||
*/
|
||||
template<typename Resource>
|
||||
class ResourceCache {
|
||||
using container_type = std::unordered_map<HashedString::hash_type, std::shared_ptr<Resource>>;
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename container_type::size_type;
|
||||
/*! @brief Type of resources managed by a cache. */
|
||||
using resource_type = HashedString;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
ResourceCache() = default;
|
||||
|
||||
/*! @brief Copying a cache isn't allowed. */
|
||||
ResourceCache(const ResourceCache &) noexcept = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
ResourceCache(ResourceCache &&) noexcept = default;
|
||||
|
||||
/*! @brief Copying a cache isn't allowed. @return This cache. */
|
||||
ResourceCache & operator=(const ResourceCache &) noexcept = delete;
|
||||
/*! @brief Default move assignment operator. @return This cache. */
|
||||
ResourceCache & operator=(ResourceCache &&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Number of resources managed by a cache.
|
||||
* @return Number of resources currently stored.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return resources.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if a cache contains no resources, false otherwise.
|
||||
* @return True if the cache contains no resources, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
return resources.empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Clears a cache and discards all its resources.
|
||||
*
|
||||
* Handles are not invalidated and the memory used by a resource isn't
|
||||
* freed as long as at least a handle keeps the resource itself alive.
|
||||
*/
|
||||
void clear() noexcept {
|
||||
resources.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Loads the resource that corresponds to a given identifier.
|
||||
*
|
||||
* In case an identifier isn't already present in the cache, it loads its
|
||||
* resource and stores it aside for future uses. Arguments are forwarded
|
||||
* directly to the loader in order to construct properly the requested
|
||||
* resource.
|
||||
*
|
||||
* @note
|
||||
* If the identifier is already present in the cache, this function does
|
||||
* nothing and the arguments are simply discarded.
|
||||
*
|
||||
* @tparam Loader Type of loader to use to load the resource if required.
|
||||
* @tparam Args Types of arguments to use to load the resource if required.
|
||||
* @param id Unique resource identifier.
|
||||
* @param args Arguments to use to load the resource if required.
|
||||
* @return True if the resource is ready to use, false otherwise.
|
||||
*/
|
||||
template<typename Loader, typename... Args>
|
||||
bool load(resource_type id, Args &&... args) {
|
||||
static_assert(std::is_base_of<ResourceLoader<Loader, Resource>, Loader>::value, "!");
|
||||
|
||||
bool loaded = true;
|
||||
|
||||
if(resources.find(id) == resources.cend()) {
|
||||
std::shared_ptr<Resource> resource = Loader{}.get(std::forward<Args>(args)...);
|
||||
loaded = (static_cast<bool>(resource) ? (resources[id] = std::move(resource), loaded) : false);
|
||||
}
|
||||
|
||||
return loaded;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Reloads a resource or loads it for the first time if not present.
|
||||
*
|
||||
* Equivalent to the following snippet (pseudocode):
|
||||
*
|
||||
* @code{.cpp}
|
||||
* cache.discard(id);
|
||||
* cache.load(id, args...);
|
||||
* @endcode
|
||||
*
|
||||
* Arguments are forwarded directly to the loader in order to construct
|
||||
* properly the requested resource.
|
||||
*
|
||||
* @tparam Loader Type of loader to use to load the resource.
|
||||
* @tparam Args Types of arguments to use to load the resource.
|
||||
* @param id Unique resource identifier.
|
||||
* @param args Arguments to use to load the resource.
|
||||
* @return True if the resource is ready to use, false otherwise.
|
||||
*/
|
||||
template<typename Loader, typename... Args>
|
||||
bool reload(resource_type id, Args &&... args) {
|
||||
return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a temporary handle for a resource.
|
||||
*
|
||||
* Arguments are forwarded directly to the loader in order to construct
|
||||
* properly the requested resource. The handle isn't stored aside and the
|
||||
* cache isn't in charge of the lifetime of the resource itself.
|
||||
*
|
||||
* @tparam Loader Type of loader to use to load the resource.
|
||||
* @tparam Args Types of arguments to use to load the resource.
|
||||
* @param args Arguments to use to load the resource.
|
||||
* @return A handle for the given resource.
|
||||
*/
|
||||
template<typename Loader, typename... Args>
|
||||
ResourceHandle<Resource> temp(Args &&... args) const {
|
||||
return { Loader{}.get(std::forward<Args>(args)...) };
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Creates a handle for a given resource identifier.
|
||||
*
|
||||
* A resource handle can be in a either valid or invalid state. In other
|
||||
* terms, a resource handle is properly initialized with a resource if the
|
||||
* cache contains the resource itself. Otherwise the returned handle is
|
||||
* uninitialized and accessing it results in undefined behavior.
|
||||
*
|
||||
* @sa ResourceHandle
|
||||
*
|
||||
* @param id Unique resource identifier.
|
||||
* @return A handle for the given resource.
|
||||
*/
|
||||
ResourceHandle<Resource> handle(resource_type id) const {
|
||||
auto it = resources.find(id);
|
||||
return { it == resources.end() ? nullptr : it->second };
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if a cache contains a given identifier.
|
||||
* @param id Unique resource identifier.
|
||||
* @return True if the cache contains the resource, false otherwise.
|
||||
*/
|
||||
bool contains(resource_type id) const noexcept {
|
||||
return !(resources.find(id) == resources.cend());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Discards the resource that corresponds to a given identifier.
|
||||
*
|
||||
* Handles are not invalidated and the memory used by the resource isn't
|
||||
* freed as long as at least a handle keeps the resource itself alive.
|
||||
*
|
||||
* @param id Unique resource identifier.
|
||||
*/
|
||||
void discard(resource_type id) noexcept {
|
||||
auto it = resources.find(id);
|
||||
|
||||
if(it != resources.end()) {
|
||||
resources.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
container_type resources;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_RESOURCE_CACHE_HPP
|
||||
115
src/entt/resource/handle.hpp
Normal file
115
src/entt/resource/handle.hpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#ifndef ENTT_RESOURCE_HANDLE_HPP
|
||||
#define ENTT_RESOURCE_HANDLE_HPP
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
template<typename Resource>
|
||||
class ResourceCache;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Shared resource handle.
|
||||
*
|
||||
* A shared resource handle is a small class that wraps a resource and keeps it
|
||||
* alive even if it's deleted from the cache. It can be either copied or
|
||||
* moved. A handle shares a reference to the same resource with all the other
|
||||
* handles constructed for the same identifier.<br/>
|
||||
* As a rule of thumb, resources should never be copied nor moved. Handles are
|
||||
* the way to go to keep references to them.
|
||||
*
|
||||
* @tparam Resource Type of resource managed by a handle.
|
||||
*/
|
||||
template<typename Resource>
|
||||
class ResourceHandle final {
|
||||
/*! @brief Resource handles are friends of their caches. */
|
||||
friend class ResourceCache<Resource>;
|
||||
|
||||
ResourceHandle(std::shared_ptr<Resource> res) noexcept
|
||||
: resource{std::move(res)}
|
||||
{}
|
||||
|
||||
public:
|
||||
/*! @brief Default copy constructor. */
|
||||
ResourceHandle(const ResourceHandle &) noexcept = default;
|
||||
/*! @brief Default move constructor. */
|
||||
ResourceHandle(ResourceHandle &&) noexcept = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This handle. */
|
||||
ResourceHandle & operator=(const ResourceHandle &) noexcept = default;
|
||||
/*! @brief Default move assignment operator. @return This handle. */
|
||||
ResourceHandle & operator=(ResourceHandle &&) noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Gets a reference to the managed resource.
|
||||
*
|
||||
* @warning
|
||||
* The behavior is undefined if the handle doesn't contain a resource.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* handle is empty.
|
||||
*
|
||||
* @return A reference to the managed resource.
|
||||
*/
|
||||
const Resource & get() const noexcept {
|
||||
assert(static_cast<bool>(resource));
|
||||
return *resource;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Casts a handle and gets a reference to the managed resource.
|
||||
*
|
||||
* @warning
|
||||
* The behavior is undefined if the handle doesn't contain a resource.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* handle is empty.
|
||||
*/
|
||||
inline operator const Resource & () const noexcept { return get(); }
|
||||
|
||||
/**
|
||||
* @brief Dereferences a handle to obtain the managed resource.
|
||||
*
|
||||
* @warning
|
||||
* The behavior is undefined if the handle doesn't contain a resource.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* handle is empty.
|
||||
*
|
||||
* @return A reference to the managed resource.
|
||||
*/
|
||||
inline const Resource & operator *() const noexcept { return get(); }
|
||||
|
||||
/**
|
||||
* @brief Gets a pointer to the managed resource from a handle .
|
||||
*
|
||||
* @warning
|
||||
* The behavior is undefined if the handle doesn't contain a resource.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* handle is empty.
|
||||
*
|
||||
* @return A pointer to the managed resource or `nullptr` if the handle
|
||||
* contains no resource at all.
|
||||
*/
|
||||
inline const Resource * operator ->() const noexcept {
|
||||
assert(static_cast<bool>(resource));
|
||||
return resource.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if the handle contains a resource, false otherwise.
|
||||
*/
|
||||
explicit operator bool() const { return static_cast<bool>(resource); }
|
||||
|
||||
private:
|
||||
std::shared_ptr<Resource> resource;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_RESOURCE_HANDLE_HPP
|
||||
62
src/entt/resource/loader.hpp
Normal file
62
src/entt/resource/loader.hpp
Normal file
@@ -0,0 +1,62 @@
|
||||
#ifndef ENTT_RESOURCE_LOADER_HPP
|
||||
#define ENTT_RESOURCE_LOADER_HPP
|
||||
|
||||
|
||||
#include <memory>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
template<typename Resource>
|
||||
class ResourceCache;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Base class for resource loaders.
|
||||
*
|
||||
* Resource loaders must inherit from this class and stay true to the CRTP
|
||||
* idiom. Moreover, a resource loader must expose a public, const member
|
||||
* function named `load` that accepts a variable number of arguments and returns
|
||||
* a shared pointer to the resource just created.<br/>
|
||||
* As an example:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* struct MyResource {};
|
||||
*
|
||||
* struct MyLoader: entt::ResourceLoader<MyLoader, MyResource> {
|
||||
* std::shared_ptr<MyResource> load(int) const {
|
||||
* // use the integer value somehow
|
||||
* return std::make_shared<MyResource>();
|
||||
* }
|
||||
* };
|
||||
* @endcode
|
||||
*
|
||||
* In general, resource loaders should not have a state or retain data of any
|
||||
* type. They should let the cache manage their resources instead.
|
||||
*
|
||||
* @note
|
||||
* Base class and CRTP idiom aren't strictly required with the current
|
||||
* implementation. One could argue that a cache can easily work with loaders of
|
||||
* any type. However, future changes won't be breaking ones by forcing the use
|
||||
* of a base class today and that's why the model is already in its place.
|
||||
*
|
||||
* @tparam Loader Type of the derived class.
|
||||
* @tparam Resource Type of resource for which to use the loader.
|
||||
*/
|
||||
template<typename Loader, typename Resource>
|
||||
class ResourceLoader {
|
||||
/*! @brief Resource loaders are friends of their caches. */
|
||||
friend class ResourceCache<Resource>;
|
||||
|
||||
template<typename... Args>
|
||||
std::shared_ptr<Resource> get(Args &&... args) const {
|
||||
return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_RESOURCE_LOADER_HPP
|
||||
@@ -46,21 +46,6 @@ public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = std::size_t;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Bus() noexcept = default;
|
||||
/*! @brief Default destructor. */
|
||||
~Bus() noexcept = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Bus(const Bus &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Bus(Bus &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This bus. */
|
||||
Bus & operator=(const Bus &) = default;
|
||||
/*! @brief Default move assignment operator. @return This bus. */
|
||||
Bus & operator=(Bus &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Unregisters all the member functions of an instance.
|
||||
*
|
||||
@@ -117,7 +102,7 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns false is at least a listener is connected to the bus.
|
||||
* @brief Returns false if at least a listener is connected to the bus.
|
||||
* @return True if the bus has no listeners connected, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
@@ -157,7 +142,7 @@ public:
|
||||
* @param args Arguments to use to construct the event.
|
||||
*/
|
||||
template<typename Type, typename... Args>
|
||||
void publish(Args&&... args) {
|
||||
void publish(Args &&... args) {
|
||||
Bus<Sig, Type>::publish(std::forward<Args>(args)...);
|
||||
}
|
||||
};
|
||||
@@ -203,21 +188,6 @@ public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename signal_type::size_type;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Bus() noexcept = default;
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Bus() noexcept = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Bus(const Bus &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Bus(Bus &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This bus. */
|
||||
Bus & operator=(const Bus &) = default;
|
||||
/*! @brief Default move assignment operator. @return This bus. */
|
||||
Bus & operator=(Bus &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Unregisters member functions of instances.
|
||||
*
|
||||
@@ -259,7 +229,7 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns false is at least a listener is connected to the bus.
|
||||
* @brief Returns false if at least a listener is connected to the bus.
|
||||
* @return True if the bus has no listeners connected, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
@@ -293,7 +263,7 @@ public:
|
||||
* @param args Arguments to use to construct the event.
|
||||
*/
|
||||
template<typename... Args>
|
||||
void publish(Args&&... args) {
|
||||
void publish(Args &&... args) {
|
||||
signal.publish({ std::forward<Args>(args)... });
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ class Delegate;
|
||||
|
||||
|
||||
/**
|
||||
* @brief A delegate class to send around functions and member functions.
|
||||
* @brief Utility class to send around functions and member functions.
|
||||
*
|
||||
* Unmanaged delegate for function pointers and member functions. Users of this
|
||||
* class are in charge of disconnecting instances before deleting them.
|
||||
@@ -49,8 +49,8 @@ class Delegate<Ret(Args...)> final {
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Delegate() noexcept
|
||||
/*! @brief Default constructor. */
|
||||
Delegate() noexcept
|
||||
: stub{std::make_pair(nullptr, &fallback)}
|
||||
{}
|
||||
|
||||
|
||||
@@ -40,8 +40,8 @@ class Dispatcher final {
|
||||
|
||||
template<typename Event>
|
||||
struct SignalWrapper final: BaseSignalWrapper {
|
||||
void publish(std::size_t current) final override {
|
||||
for(auto &&event: events[current]) {
|
||||
void publish(std::size_t current) override {
|
||||
for(const auto &event: events[current]) {
|
||||
signal.publish(event);
|
||||
}
|
||||
|
||||
@@ -59,12 +59,12 @@ class Dispatcher final {
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void trigger(Args&&... args) {
|
||||
inline void trigger(Args &&... args) {
|
||||
signal.publish({ std::forward<Args>(args)... });
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline void enqueue(std::size_t current, Args&&... args) {
|
||||
inline void enqueue(std::size_t current, Args &&... args) {
|
||||
events[current].push_back({ std::forward<Args>(args)... });
|
||||
}
|
||||
|
||||
@@ -79,7 +79,7 @@ class Dispatcher final {
|
||||
|
||||
template<typename Event>
|
||||
SignalWrapper<Event> & wrapper() {
|
||||
auto type = event_family::type<Event>();
|
||||
const auto type = event_family::type<Event>();
|
||||
|
||||
if(!(type < wrappers.size())) {
|
||||
wrappers.resize(type + 1);
|
||||
@@ -93,24 +93,11 @@ class Dispatcher final {
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Dispatcher() noexcept
|
||||
/*! @brief Default constructor. */
|
||||
Dispatcher() noexcept
|
||||
: wrappers{}, mode{false}
|
||||
{}
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Dispatcher() = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Dispatcher(const Dispatcher &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Dispatcher(Dispatcher &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This dispatcher. */
|
||||
Dispatcher & operator=(const Dispatcher &) = default;
|
||||
/*! @brief Default move assignment operator. @return This dispatcher. */
|
||||
Dispatcher & operator=(Dispatcher &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Registers a listener given in the form of a member function.
|
||||
*
|
||||
@@ -140,8 +127,9 @@ public:
|
||||
* automatically detected and unregistered if available.
|
||||
*
|
||||
* @warning
|
||||
* Disonnecting a listener during an update may lead to unexpected behavior.
|
||||
* Unregister listeners before or after invoking the update if possible.
|
||||
* Disconnecting a listener during an update may lead to unexpected
|
||||
* behavior. Unregister listeners before or after invoking the update if
|
||||
* possible.
|
||||
*
|
||||
* @tparam Event Type of event from which to disconnect the function.
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
@@ -164,7 +152,7 @@ public:
|
||||
* @param args Arguments to use to construct the event.
|
||||
*/
|
||||
template<typename Event, typename... Args>
|
||||
void trigger(Args&&... args) {
|
||||
void trigger(Args &&... args) {
|
||||
wrapper<Event>().trigger(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
@@ -179,7 +167,7 @@ public:
|
||||
* @param args Arguments to use to construct the event.
|
||||
*/
|
||||
template<typename Event, typename... Args>
|
||||
void enqueue(Args&&... args) {
|
||||
void enqueue(Args &&... args) {
|
||||
wrapper<Event>().enqueue(buffer(mode), std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
@@ -191,10 +179,12 @@ public:
|
||||
* to reduce at a minimum the time spent in the bodies of the listeners.
|
||||
*/
|
||||
void update() {
|
||||
auto buf = buffer(mode);
|
||||
const auto buf = buffer(mode);
|
||||
mode = !mode;
|
||||
|
||||
for(auto &&wrapper: wrappers) {
|
||||
for(auto pos = wrappers.size(); pos; --pos) {
|
||||
auto &wrapper = wrappers[pos-1];
|
||||
|
||||
if(wrapper) {
|
||||
wrapper->publish(buf);
|
||||
}
|
||||
|
||||
@@ -52,7 +52,7 @@ class Emitter {
|
||||
using connection_type = typename container_type::iterator;
|
||||
|
||||
bool empty() const noexcept override {
|
||||
auto pred = [](auto &&element){ return element.first; };
|
||||
auto pred = [](auto &&element) { return element.first; };
|
||||
|
||||
return std::all_of(onceL.cbegin(), onceL.cend(), pred) &&
|
||||
std::all_of(onL.cbegin(), onL.cend(), pred);
|
||||
@@ -60,7 +60,7 @@ class Emitter {
|
||||
|
||||
void clear() noexcept override {
|
||||
if(publishing) {
|
||||
auto func = [](auto &&element){ element.first = true; };
|
||||
auto func = [](auto &&element) { element.first = true; };
|
||||
std::for_each(onceL.begin(), onceL.end(), func);
|
||||
std::for_each(onL.begin(), onL.end(), func);
|
||||
} else {
|
||||
@@ -81,7 +81,7 @@ class Emitter {
|
||||
conn->first = true;
|
||||
|
||||
if(!publishing) {
|
||||
auto pred = [](auto &&element){ return element.first; };
|
||||
auto pred = [](auto &&element) { return element.first; };
|
||||
onceL.remove_if(pred);
|
||||
onL.remove_if(pred);
|
||||
}
|
||||
@@ -102,7 +102,7 @@ class Emitter {
|
||||
|
||||
publishing = false;
|
||||
|
||||
onL.remove_if([](auto &&element){ return element.first; });
|
||||
onL.remove_if([](auto &&element) { return element.first; });
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -124,7 +124,7 @@ class Emitter {
|
||||
|
||||
template<typename Event>
|
||||
Handler<Event> & handler() noexcept {
|
||||
std::size_t family = type<Event>();
|
||||
const std::size_t family = type<Event>();
|
||||
|
||||
if(!(family < handlers.size())) {
|
||||
handlers.resize(family+1);
|
||||
@@ -156,16 +156,8 @@ public:
|
||||
/** @brief Event emitters are friend classes of connections. */
|
||||
friend class Emitter;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Connection() = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Connection(const Connection &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Connection(Connection &&) = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Connection() = default;
|
||||
/*! @brief Default constructor. */
|
||||
Connection() noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Creates a connection that wraps its underlying instance.
|
||||
@@ -175,6 +167,11 @@ public:
|
||||
: Handler<Event>::connection_type{std::move(conn)}
|
||||
{}
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Connection(const Connection &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Connection(Connection &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Default copy assignament operator.
|
||||
* @return This connection.
|
||||
@@ -188,19 +185,19 @@ public:
|
||||
Connection & operator=(Connection &&) = default;
|
||||
};
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Emitter() noexcept = default;
|
||||
|
||||
/*! @brief Copying an emitter isn't allowed. */
|
||||
Emitter(const Emitter &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Emitter(Emitter &&) = default;
|
||||
/*! @brief Default constructor. */
|
||||
Emitter() noexcept = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Emitter() noexcept {
|
||||
static_assert(std::is_base_of<Emitter<Derived>, Derived>::value, "!");
|
||||
}
|
||||
|
||||
/*! @brief Copying an emitter isn't allowed. */
|
||||
Emitter(const Emitter &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Emitter(Emitter &&) = default;
|
||||
|
||||
/*! @brief Copying an emitter isn't allowed. @return This emitter. */
|
||||
Emitter & operator=(const Emitter &) = delete;
|
||||
/*! @brief Default move assignament operator. @return This emitter. */
|
||||
@@ -218,7 +215,7 @@ public:
|
||||
* @param args Parameters to use to initialize the event.
|
||||
*/
|
||||
template<typename Event, typename... Args>
|
||||
void publish(Args&&... args) {
|
||||
void publish(Args &&... args) {
|
||||
handler<Event>().publish({ std::forward<Args>(args)... }, *static_cast<Derived *>(this));
|
||||
}
|
||||
|
||||
@@ -290,7 +287,7 @@ public:
|
||||
* @brief Disconnects all the listeners for the given event type.
|
||||
*
|
||||
* All the connections previously returned for the given event are
|
||||
* invalidated. Using them results in undefined behaviour.
|
||||
* invalidated. Using them results in undefined behavior.
|
||||
*
|
||||
* @tparam Event Type of event to reset.
|
||||
*/
|
||||
@@ -303,11 +300,11 @@ public:
|
||||
* @brief Disconnects all the listeners.
|
||||
*
|
||||
* All the connections previously returned are invalidated. Using them
|
||||
* results in undefined behaviour.
|
||||
* results in undefined behavior.
|
||||
*/
|
||||
void clear() noexcept {
|
||||
std::for_each(handlers.begin(), handlers.end(),
|
||||
[](auto &&handler){ if(handler) { handler->clear(); } });
|
||||
[](auto &&handler) { if(handler) { handler->clear(); } });
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -317,7 +314,7 @@ public:
|
||||
*/
|
||||
template<typename Event>
|
||||
bool empty() const noexcept {
|
||||
std::size_t family = type<Event>();
|
||||
const std::size_t family = type<Event>();
|
||||
|
||||
return (!(family < handlers.size()) ||
|
||||
!handlers[family] ||
|
||||
@@ -330,7 +327,7 @@ public:
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
return std::all_of(handlers.cbegin(), handlers.cend(),
|
||||
[](auto &&handler){ return !handler || handler->empty(); });
|
||||
[](auto &&handler) { return !handler || handler->empty(); });
|
||||
}
|
||||
|
||||
private:
|
||||
|
||||
@@ -24,21 +24,25 @@ struct Invoker<Ret(Args...), Collector> {
|
||||
|
||||
virtual ~Invoker() = default;
|
||||
|
||||
template<typename SFINAE = Ret>
|
||||
typename std::enable_if<std::is_void<SFINAE>::value, bool>::type
|
||||
invoke(Collector &, proto_type proto, void *instance, Args... args) {
|
||||
proto(instance, args...);
|
||||
return true;
|
||||
}
|
||||
|
||||
template<typename SFINAE = Ret>
|
||||
typename std::enable_if<!std::is_void<SFINAE>::value, bool>::type
|
||||
invoke(Collector &collector, proto_type proto, void *instance, Args... args) {
|
||||
bool invoke(Collector &collector, proto_type proto, void *instance, Args... args) {
|
||||
return collector(proto(instance, args...));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename... Args, typename Collector>
|
||||
struct Invoker<void(Args...), Collector> {
|
||||
using proto_type = void(*)(void *, Args...);
|
||||
using call_type = std::pair<void *, proto_type>;
|
||||
|
||||
virtual ~Invoker() = default;
|
||||
|
||||
bool invoke(Collector &, proto_type proto, void *instance, Args... args) {
|
||||
return (proto(instance, args...), true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename Ret>
|
||||
struct NullCollector final {
|
||||
using result_type = Ret;
|
||||
@@ -91,14 +95,16 @@ class SigH;
|
||||
* of this class are in charge of disconnecting instances before deleting them.
|
||||
*
|
||||
* This class serves mainly two purposes:
|
||||
*
|
||||
* * Creating signals used later to notify a bunch of listeners.
|
||||
* * Collecting results from a set of functions like in a voting system.
|
||||
*
|
||||
* The default collector does nothing. To properly collect data, define and use
|
||||
* a class that has a call operator the signature of which is `bool(Param)` and:
|
||||
*
|
||||
* * `Param` is a type to which `Ret` can be converted.
|
||||
* * The return type is true if the handler must stop collecting data, false
|
||||
* otherwise.
|
||||
* otherwise.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of arguments of a function type.
|
||||
@@ -131,51 +137,6 @@ public:
|
||||
template<typename Class>
|
||||
using instance_type = Class *;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit SigH() noexcept = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~SigH() noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor, listeners are also connected to this signal.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
SigH(const SigH &other)
|
||||
: calls{other.calls}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Default move constructor.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
SigH(SigH &&other): SigH{} {
|
||||
swap(*this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Copy assignment operator.
|
||||
*
|
||||
* Listeners are also connected to this signal.
|
||||
*
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
SigH & operator=(const SigH &other) {
|
||||
calls = other.calls;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Move assignment operator.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
SigH & operator=(SigH &&other) {
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Number of listeners connected to the signal.
|
||||
* @return Number of listeners currently connected.
|
||||
@@ -185,7 +146,7 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns false is at least a listener is connected to the signal.
|
||||
* @brief Returns false if at least a listener is connected to the signal.
|
||||
* @return True if the signal has no listeners connected, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
@@ -272,7 +233,8 @@ public:
|
||||
* @param args Arguments to use to invoke listeners.
|
||||
*/
|
||||
void publish(Args... args) {
|
||||
for(auto &&call: calls) {
|
||||
for(auto pos = calls.size(); pos; --pos) {
|
||||
auto &call = calls[pos-1];
|
||||
call.second(call.first, args...);
|
||||
}
|
||||
}
|
||||
@@ -285,7 +247,9 @@ public:
|
||||
collector_type collect(Args... args) {
|
||||
collector_type collector;
|
||||
|
||||
for(auto &&call: calls) {
|
||||
for(auto pos = calls.size(); pos; --pos) {
|
||||
auto &call = calls[pos-1];
|
||||
|
||||
if(!this->invoke(collector, call.second, call.first, args...)) {
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -70,51 +70,6 @@ public:
|
||||
template<typename Class>
|
||||
using instance_type = std::shared_ptr<Class>;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Signal() noexcept = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Signal() noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor, listeners are also connected to this signal.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
Signal(const Signal &other)
|
||||
: calls{other.calls}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Default move constructor.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
Signal(Signal &&other): Signal{} {
|
||||
swap(*this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Copy assignment operator.
|
||||
*
|
||||
* Listeners are also connected to this signal.
|
||||
*
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
Signal & operator=(const Signal &other) {
|
||||
calls = other.calls;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Move assignment operator.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
Signal & operator=(Signal &&other) {
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Number of listeners connected to the signal.
|
||||
* @return Number of listeners currently connected.
|
||||
@@ -124,7 +79,7 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns false is at least a listener is connected to the signal.
|
||||
* @brief Returns false if at least a listener is connected to the signal.
|
||||
* @return True if the signal has no listeners connected, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
@@ -212,11 +167,17 @@ public:
|
||||
* @param args Arguments to use to invoke listeners.
|
||||
*/
|
||||
void publish(Args... args) {
|
||||
for(auto it = calls.rbegin(), end = calls.rend(); it != end; it++) {
|
||||
if(!(it->second)(it->first, args...)) {
|
||||
calls.erase(std::next(it).base());
|
||||
std::vector<call_type> next;
|
||||
|
||||
for(auto pos = calls.size(); pos; --pos) {
|
||||
auto &call = calls[pos-1];
|
||||
|
||||
if((call.second)(call.first, args...)) {
|
||||
next.push_back(call);
|
||||
}
|
||||
}
|
||||
|
||||
calls.swap(next);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -2,28 +2,66 @@
|
||||
# Tests configuration
|
||||
#
|
||||
|
||||
include_directories(${PROJECT_SRC_DIR})
|
||||
|
||||
add_library(odr OBJECT odr.cpp)
|
||||
|
||||
# Test benchmark
|
||||
|
||||
if(CMAKE_BUILD_TYPE MATCHES Release)
|
||||
if(BUILD_BENCHMARK)
|
||||
add_executable(
|
||||
benchmark
|
||||
$<TARGET_OBJECTS:odr>
|
||||
entt/entity/benchmark.cpp
|
||||
benchmark/benchmark.cpp
|
||||
)
|
||||
target_link_libraries(benchmark PRIVATE gtest_main Threads::Threads)
|
||||
add_test(NAME benchmark COMMAND benchmark)
|
||||
endif()
|
||||
|
||||
# Test mod
|
||||
|
||||
if(BUILD_MOD)
|
||||
set(DUKTAPE_DEPS_DIR ${entt_SOURCE_DIR}/deps/duktape)
|
||||
configure_file(${entt_SOURCE_DIR}/cmake/in/duktape.in ${DUKTAPE_DEPS_DIR}/CMakeLists.txt)
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${DUKTAPE_DEPS_DIR})
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${DUKTAPE_DEPS_DIR})
|
||||
set(DUKTAPE_SRC_DIR ${DUKTAPE_DEPS_DIR}/src/src)
|
||||
|
||||
add_executable(
|
||||
mod
|
||||
$<TARGET_OBJECTS:odr>
|
||||
${DUKTAPE_SRC_DIR}/duktape.c
|
||||
mod/mod.cpp
|
||||
)
|
||||
target_include_directories(mod PRIVATE ${DUKTAPE_SRC_DIR})
|
||||
target_link_libraries(mod PRIVATE gtest_main Threads::Threads m)
|
||||
add_test(NAME mod COMMAND mod)
|
||||
endif()
|
||||
|
||||
# Test snapshot
|
||||
|
||||
if(BUILD_SNAPSHOT)
|
||||
set(CEREAL_DEPS_DIR ${entt_SOURCE_DIR}/deps/cereal)
|
||||
configure_file(${entt_SOURCE_DIR}/cmake/in/cereal.in ${CEREAL_DEPS_DIR}/CMakeLists.txt)
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${CEREAL_DEPS_DIR})
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${CEREAL_DEPS_DIR})
|
||||
set(CEREAL_SRC_DIR ${CEREAL_DEPS_DIR}/src/include)
|
||||
|
||||
add_executable(
|
||||
snapshot
|
||||
$<TARGET_OBJECTS:odr>
|
||||
snapshot/snapshot.cpp
|
||||
)
|
||||
target_include_directories(snapshot PRIVATE ${CEREAL_SRC_DIR})
|
||||
target_link_libraries(snapshot PRIVATE gtest_main Threads::Threads)
|
||||
add_test(NAME snapshot COMMAND snapshot)
|
||||
endif()
|
||||
|
||||
# Test core
|
||||
|
||||
add_executable(
|
||||
core
|
||||
$<TARGET_OBJECTS:odr>
|
||||
entt/core/family.cpp
|
||||
entt/core/hashed_string.cpp
|
||||
entt/core/ident.cpp
|
||||
)
|
||||
target_link_libraries(core PRIVATE gtest_main Threads::Threads)
|
||||
@@ -34,7 +72,9 @@ add_test(NAME core COMMAND core)
|
||||
add_executable(
|
||||
entity
|
||||
$<TARGET_OBJECTS:odr>
|
||||
entt/entity/actor.cpp
|
||||
entt/entity/registry.cpp
|
||||
entt/entity/snapshot.cpp
|
||||
entt/entity/sparse_set.cpp
|
||||
entt/entity/view.cpp
|
||||
)
|
||||
@@ -51,7 +91,28 @@ add_executable(
|
||||
target_link_libraries(locator PRIVATE gtest_main Threads::Threads)
|
||||
add_test(NAME locator COMMAND locator)
|
||||
|
||||
# Test locator
|
||||
# Test process
|
||||
|
||||
add_executable(
|
||||
process
|
||||
$<TARGET_OBJECTS:odr>
|
||||
entt/process/process.cpp
|
||||
entt/process/scheduler.cpp
|
||||
)
|
||||
target_link_libraries(process PRIVATE gtest_main Threads::Threads)
|
||||
add_test(NAME process COMMAND process)
|
||||
|
||||
# Test resource
|
||||
|
||||
add_executable(
|
||||
resource
|
||||
$<TARGET_OBJECTS:odr>
|
||||
entt/resource/resource.cpp
|
||||
)
|
||||
target_link_libraries(resource PRIVATE gtest_main Threads::Threads)
|
||||
add_test(NAME resource COMMAND resource)
|
||||
|
||||
# Test signal
|
||||
|
||||
add_executable(
|
||||
signal
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <iostream>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <chrono>
|
||||
#include <vector>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
struct Position {
|
||||
uint64_t x;
|
||||
uint64_t y;
|
||||
std::uint64_t x;
|
||||
std::uint64_t y;
|
||||
};
|
||||
|
||||
struct Velocity {
|
||||
uint64_t x;
|
||||
uint64_t y;
|
||||
std::uint64_t x;
|
||||
std::uint64_t y;
|
||||
};
|
||||
|
||||
template<std::size_t>
|
||||
struct Comp {};
|
||||
struct Comp { int x; };
|
||||
|
||||
struct Timer final {
|
||||
Timer(): start{std::chrono::system_clock::now()} {}
|
||||
@@ -30,40 +30,39 @@ private:
|
||||
std::chrono::time_point<std::chrono::system_clock> start;
|
||||
};
|
||||
|
||||
TEST(DefaultRegistry, Construct) {
|
||||
TEST(Benchmark, Construct) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Constructing 10000000 entities" << std::endl;
|
||||
std::cout << "Constructing 1000000 entities" << std::endl;
|
||||
|
||||
Timer timer;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create();
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, Destroy) {
|
||||
TEST(Benchmark, Destroy) {
|
||||
entt::DefaultRegistry registry;
|
||||
std::vector<entt::DefaultRegistry::entity_type> entities{};
|
||||
|
||||
std::cout << "Destroying 10000000 entities" << std::endl;
|
||||
std::cout << "Destroying 1000000 entities" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
entities.push_back(registry.create());
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
for(auto entity: entities) {
|
||||
registry.each([®istry](auto entity) {
|
||||
registry.destroy(entity);
|
||||
}
|
||||
});
|
||||
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
|
||||
TEST(Benchmark, IterateCreateDeleteSingleComponent) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Looping 10000 times creating and deleting a random number of entities" << std::endl;
|
||||
@@ -78,21 +77,24 @@ TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
|
||||
}
|
||||
|
||||
for(auto entity: view) {
|
||||
const auto &position = view.get(entity);
|
||||
(void)position;
|
||||
|
||||
if(rand() % 2 == 0) {
|
||||
registry.destroy(entity);
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateSingleComponent10M) {
|
||||
TEST(Benchmark, IterateSingleComponent1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, one component" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, one component" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position>();
|
||||
}
|
||||
|
||||
@@ -101,12 +103,28 @@ TEST(DefaultRegistry, IterateSingleComponent10M) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents10M) {
|
||||
TEST(Benchmark, IterateSingleComponentRaw1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, one component, raw view" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
for(auto &&component: registry.raw<Position>()) {
|
||||
(void)component;
|
||||
}
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(Benchmark, IterateTwoComponents1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 1000000 entities, two components" << std::endl;
|
||||
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position, Velocity>();
|
||||
}
|
||||
|
||||
@@ -115,12 +133,12 @@ TEST(DefaultRegistry, IterateTwoComponents10M) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
|
||||
TEST(Benchmark, IterateTwoComponents1MHalf) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, half of the entities have all the components" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, two components, half of the entities have all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
@@ -130,12 +148,12 @@ TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents10MOne) {
|
||||
TEST(Benchmark, IterateTwoComponents1MOne) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, only one entity has all the components" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, two components, only one entity has all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
@@ -145,13 +163,13 @@ TEST(DefaultRegistry, IterateTwoComponents10MOne) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent10M) {
|
||||
TEST(Benchmark, IterateTwoComponentsPersistent1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, persistent view" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, two components, persistent view" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position, Velocity>();
|
||||
}
|
||||
|
||||
@@ -160,44 +178,12 @@ TEST(DefaultRegistry, IterateTwoComponentsPersistent10M) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent10MHalf) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, persistent view, half of the entities have all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent10MOne) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, persistent view, only one entity has all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateFiveComponents10M) {
|
||||
TEST(Benchmark, IterateFiveComponents1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, five components" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, five components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
}
|
||||
|
||||
@@ -206,57 +192,43 @@ TEST(DefaultRegistry, IterateFiveComponents10M) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponents10M) {
|
||||
TEST(Benchmark, IterateFiveComponents1MHalf) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, five components, half of the entities have all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponents10MHalf) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, half of the entities have all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponents10MOne) {
|
||||
TEST(Benchmark, IterateFiveComponents1MOne) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, only one entity has all the components" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, five components, only one entity has all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateFiveComponentsPersistent10M) {
|
||||
TEST(Benchmark, IterateFiveComponentsPersistent1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, five components, persistent view" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, five components, persistent view" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
}
|
||||
|
||||
@@ -265,13 +237,57 @@ TEST(DefaultRegistry, IterateFiveComponentsPersistent10M) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponentsPersistent10M) {
|
||||
TEST(Benchmark, IterateTenComponents1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 1000000 entities, ten components" << std::endl;
|
||||
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(Benchmark, IterateTenComponents1MHalf) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 1000000 entities, ten components, half of the entities have all the components" << std::endl;
|
||||
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(Benchmark, IterateTenComponents1MOne) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 1000000 entities, ten components, only one entity has all the components" << std::endl;
|
||||
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(Benchmark, IterateTenComponentsPersistent1M) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, persistent view" << std::endl;
|
||||
std::cout << "Iterating over 1000000 entities, ten components, persistent view" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(std::uint64_t i = 0; i < 1000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
}
|
||||
|
||||
@@ -280,47 +296,13 @@ TEST(DefaultRegistry, IterateTenComponentsPersistent10M) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponentsPersistent10MHalf) {
|
||||
TEST(Benchmark, SortSingle) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, persistent view, half of the entities have all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponentsPersistent10MOne) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, persistent view, only one entity has all the components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, SortSingle) {
|
||||
entt::DefaultRegistry registry;
|
||||
std::vector<entt::DefaultRegistry::entity_type> entities{};
|
||||
|
||||
std::cout << "Sort 150000 entities, one component" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 150000L; i++) {
|
||||
auto entity = registry.create<Position>({ i, i });
|
||||
entities.push_back(entity);
|
||||
for(std::uint64_t i = 0; i < 150000L; i++) {
|
||||
registry.create<Position>({ i, i });
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
@@ -332,15 +314,13 @@ TEST(DefaultRegistry, SortSingle) {
|
||||
timer.elapsed();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, SortMulti) {
|
||||
TEST(Benchmark, SortMulti) {
|
||||
entt::DefaultRegistry registry;
|
||||
std::vector<entt::DefaultRegistry::entity_type> entities{};
|
||||
|
||||
std::cout << "Sort 150000 entities, two components" << std::endl;
|
||||
|
||||
for(uint64_t i = 0; i < 150000L; i++) {
|
||||
auto entity = registry.create<Position, Velocity>({ i, i }, { i, i });
|
||||
entities.push_back(entity);
|
||||
for(std::uint64_t i = 0; i < 150000L; i++) {
|
||||
registry.create<Position, Velocity>({ i, i }, { i, i });
|
||||
}
|
||||
|
||||
registry.sort<Position>([](const auto &lhs, const auto &rhs) {
|
||||
49
test/entt/core/hashed_string.cpp
Normal file
49
test/entt/core/hashed_string.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <cstddef>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
|
||||
constexpr bool ptr(const char *str) {
|
||||
using hash_type = entt::HashedString::hash_type;
|
||||
|
||||
return (static_cast<hash_type>(entt::HashedString{str}) == entt::HashedString{str}
|
||||
&& static_cast<const char *>(entt::HashedString{str}) == str
|
||||
&& entt::HashedString{str} == entt::HashedString{str}
|
||||
&& !(entt::HashedString{str} != entt::HashedString{str}));
|
||||
}
|
||||
|
||||
template<std::size_t N>
|
||||
constexpr bool ref(const char (&str)[N]) {
|
||||
using hash_type = entt::HashedString::hash_type;
|
||||
|
||||
return (static_cast<hash_type>(entt::HashedString{str}) == entt::HashedString{str}
|
||||
&& static_cast<const char *>(entt::HashedString{str}) == str
|
||||
&& entt::HashedString{str} == entt::HashedString{str}
|
||||
&& !(entt::HashedString{str} != entt::HashedString{str}));
|
||||
}
|
||||
|
||||
TEST(HashedString, Constexprness) {
|
||||
// how would you test a constexpr otherwise?
|
||||
static_assert(ptr("foo"), "!");
|
||||
static_assert(ref("bar"), "!");
|
||||
ASSERT_TRUE(true);
|
||||
}
|
||||
|
||||
TEST(HashedString, Functionalities) {
|
||||
using hash_type = entt::HashedString::hash_type;
|
||||
|
||||
const char *bar = "bar";
|
||||
|
||||
auto fooHs = entt::HashedString{"foo"};
|
||||
auto barHs = entt::HashedString{bar};
|
||||
|
||||
ASSERT_NE(static_cast<hash_type>(fooHs), static_cast<hash_type>(barHs));
|
||||
ASSERT_EQ(static_cast<const char *>(fooHs), "foo");
|
||||
ASSERT_EQ(static_cast<const char *>(barHs), bar);
|
||||
|
||||
ASSERT_TRUE(fooHs == fooHs);
|
||||
ASSERT_TRUE(fooHs != barHs);
|
||||
|
||||
entt::HashedString hs{"foobar"};
|
||||
|
||||
ASSERT_EQ(static_cast<hash_type>(hs), 0x85944171f73967e8);
|
||||
}
|
||||
57
test/entt/entity/actor.cpp
Normal file
57
test/entt/entity/actor.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include <functional>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/actor.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
struct TestActor: entt::DefaultActor<unsigned int> {
|
||||
using entt::DefaultActor<unsigned int>::DefaultActor;
|
||||
void update(unsigned int) {}
|
||||
};
|
||||
|
||||
struct Position final {};
|
||||
struct Velocity final {};
|
||||
|
||||
TEST(Actor, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
TestActor *actor = new TestActor{registry};
|
||||
const auto &cactor = *actor;
|
||||
|
||||
ASSERT_EQ(®istry, &actor->registry());
|
||||
ASSERT_EQ(®istry, &cactor.registry());
|
||||
ASSERT_TRUE(registry.empty<Position>());
|
||||
ASSERT_TRUE(registry.empty<Velocity>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE(actor->has<Position>());
|
||||
ASSERT_FALSE(actor->has<Velocity>());
|
||||
|
||||
const auto &position = actor->set<Position>();
|
||||
|
||||
ASSERT_EQ(&position, &actor->get<Position>());
|
||||
ASSERT_EQ(&position, &cactor.get<Position>());
|
||||
ASSERT_FALSE(registry.empty<Position>());
|
||||
ASSERT_TRUE(registry.empty<Velocity>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_TRUE(actor->has<Position>());
|
||||
ASSERT_FALSE(actor->has<Velocity>());
|
||||
|
||||
actor->unset<Position>();
|
||||
|
||||
ASSERT_TRUE(registry.empty<Position>());
|
||||
ASSERT_TRUE(registry.empty<Velocity>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE(actor->has<Position>());
|
||||
ASSERT_FALSE(actor->has<Velocity>());
|
||||
|
||||
actor->set<Position>();
|
||||
actor->set<Velocity>();
|
||||
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE(registry.empty<Position>());
|
||||
ASSERT_FALSE(registry.empty<Velocity>());
|
||||
|
||||
delete actor;
|
||||
|
||||
ASSERT_TRUE(registry.empty());
|
||||
ASSERT_TRUE(registry.empty<Position>());
|
||||
ASSERT_TRUE(registry.empty<Velocity>());
|
||||
}
|
||||
@@ -1,11 +1,18 @@
|
||||
#include <unordered_map>
|
||||
#include <unordered_set>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/entt_traits.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
TEST(DefaultRegistry, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
ASSERT_EQ(registry.size(), entt::DefaultRegistry::size_type{0});
|
||||
ASSERT_NO_THROW(registry.reserve(42));
|
||||
ASSERT_NO_THROW(registry.reserve<int>(8));
|
||||
ASSERT_NO_THROW(registry.reserve<char>(8));
|
||||
ASSERT_TRUE(registry.empty());
|
||||
|
||||
ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{0});
|
||||
@@ -14,8 +21,11 @@ TEST(DefaultRegistry, Functionalities) {
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
ASSERT_TRUE(registry.empty<char>());
|
||||
|
||||
auto e1 = registry.create();
|
||||
auto e2 = registry.create<int, char>();
|
||||
auto e0 = registry.create();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
ASSERT_TRUE(registry.has<>(e0));
|
||||
ASSERT_TRUE(registry.has<>(e1));
|
||||
|
||||
ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{2});
|
||||
ASSERT_EQ(registry.size<int>(), entt::DefaultRegistry::size_type{1});
|
||||
@@ -23,63 +33,70 @@ TEST(DefaultRegistry, Functionalities) {
|
||||
ASSERT_FALSE(registry.empty<int>());
|
||||
ASSERT_FALSE(registry.empty<char>());
|
||||
|
||||
ASSERT_NE(e1, e2);
|
||||
|
||||
ASSERT_FALSE(registry.has<int>(e1));
|
||||
ASSERT_TRUE(registry.has<int>(e2));
|
||||
ASSERT_FALSE(registry.has<char>(e1));
|
||||
ASSERT_TRUE(registry.has<char>(e2));
|
||||
ASSERT_FALSE((registry.has<int, char>(e1)));
|
||||
ASSERT_TRUE((registry.has<int, char>(e2)));
|
||||
|
||||
ASSERT_EQ(registry.assign<int>(e1, 42), 42);
|
||||
ASSERT_EQ(registry.assign<char>(e1, 'c'), 'c');
|
||||
ASSERT_NO_THROW(registry.remove<int>(e2));
|
||||
ASSERT_NO_THROW(registry.remove<char>(e2));
|
||||
ASSERT_NE(e0, e1);
|
||||
|
||||
ASSERT_FALSE(registry.has<int>(e0));
|
||||
ASSERT_TRUE(registry.has<int>(e1));
|
||||
ASSERT_FALSE(registry.has<int>(e2));
|
||||
ASSERT_FALSE(registry.has<char>(e0));
|
||||
ASSERT_TRUE(registry.has<char>(e1));
|
||||
ASSERT_FALSE(registry.has<char>(e2));
|
||||
ASSERT_FALSE((registry.has<int, char>(e0)));
|
||||
ASSERT_TRUE((registry.has<int, char>(e1)));
|
||||
ASSERT_FALSE((registry.has<int, char>(e2)));
|
||||
|
||||
auto e3 = registry.create();
|
||||
ASSERT_EQ(registry.assign<int>(e0, 42), 42);
|
||||
ASSERT_EQ(registry.assign<char>(e0, 'c'), 'c');
|
||||
ASSERT_NO_THROW(registry.remove<int>(e1));
|
||||
ASSERT_NO_THROW(registry.remove<char>(e1));
|
||||
|
||||
registry.accomodate<int>(e3, registry.get<int>(e1));
|
||||
registry.accomodate<char>(e3, registry.get<char>(e1));
|
||||
ASSERT_TRUE(registry.has<int>(e0));
|
||||
ASSERT_FALSE(registry.has<int>(e1));
|
||||
ASSERT_TRUE(registry.has<char>(e0));
|
||||
ASSERT_FALSE(registry.has<char>(e1));
|
||||
ASSERT_TRUE((registry.has<int, char>(e0)));
|
||||
ASSERT_FALSE((registry.has<int, char>(e1)));
|
||||
|
||||
ASSERT_TRUE(registry.has<int>(e3));
|
||||
ASSERT_TRUE(registry.has<char>(e3));
|
||||
ASSERT_EQ(registry.get<int>(e1), 42);
|
||||
ASSERT_EQ(registry.get<char>(e1), 'c');
|
||||
ASSERT_EQ(registry.get<int>(e1), registry.get<int>(e3));
|
||||
ASSERT_EQ(registry.get<char>(e1), registry.get<char>(e3));
|
||||
ASSERT_NE(®istry.get<int>(e1), ®istry.get<int>(e3));
|
||||
ASSERT_NE(®istry.get<char>(e1), ®istry.get<char>(e3));
|
||||
auto e2 = registry.create();
|
||||
|
||||
ASSERT_NO_THROW(registry.replace<int>(e1, 0));
|
||||
ASSERT_EQ(registry.get<int>(e1), 0);
|
||||
registry.accommodate<int>(e2, registry.get<int>(e0));
|
||||
registry.accommodate<char>(e2, registry.get<char>(e0));
|
||||
|
||||
ASSERT_NO_THROW(registry.accomodate<int>(e1, 1));
|
||||
ASSERT_NO_THROW(registry.accomodate<int>(e2, 1));
|
||||
ASSERT_TRUE(registry.has<int>(e2));
|
||||
ASSERT_TRUE(registry.has<char>(e2));
|
||||
ASSERT_EQ(registry.get<int>(e0), 42);
|
||||
ASSERT_EQ(registry.get<char>(e0), 'c');
|
||||
|
||||
ASSERT_EQ(std::get<0>(registry.get<int, char>(e0)), 42);
|
||||
ASSERT_EQ(std::get<1>(static_cast<const entt::DefaultRegistry &>(registry).get<int, char>(e0)), 'c');
|
||||
|
||||
ASSERT_EQ(registry.get<int>(e0), registry.get<int>(e2));
|
||||
ASSERT_EQ(registry.get<char>(e0), registry.get<char>(e2));
|
||||
ASSERT_NE(®istry.get<int>(e0), ®istry.get<int>(e2));
|
||||
ASSERT_NE(®istry.get<char>(e0), ®istry.get<char>(e2));
|
||||
|
||||
ASSERT_NO_THROW(registry.replace<int>(e0, 0));
|
||||
ASSERT_EQ(registry.get<int>(e0), 0);
|
||||
|
||||
ASSERT_NO_THROW(registry.accommodate<int>(e0, 1));
|
||||
ASSERT_NO_THROW(registry.accommodate<int>(e1, 1));
|
||||
ASSERT_EQ(static_cast<const entt::DefaultRegistry &>(registry).get<int>(e0), 1);
|
||||
ASSERT_EQ(static_cast<const entt::DefaultRegistry &>(registry).get<int>(e1), 1);
|
||||
ASSERT_EQ(static_cast<const entt::DefaultRegistry &>(registry).get<int>(e2), 1);
|
||||
|
||||
ASSERT_EQ(registry.size(), entt::DefaultRegistry::size_type{3});
|
||||
ASSERT_FALSE(registry.empty());
|
||||
|
||||
ASSERT_EQ(registry.version(e3), entt::DefaultRegistry::version_type{0});
|
||||
ASSERT_EQ(registry.current(e3), entt::DefaultRegistry::version_type{0});
|
||||
ASSERT_EQ(registry.version(e2), entt::DefaultRegistry::version_type{0});
|
||||
ASSERT_EQ(registry.current(e2), entt::DefaultRegistry::version_type{0});
|
||||
ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{3});
|
||||
ASSERT_NO_THROW(registry.destroy(e3));
|
||||
ASSERT_NO_THROW(registry.destroy(e2));
|
||||
ASSERT_EQ(registry.capacity(), entt::DefaultRegistry::size_type{3});
|
||||
ASSERT_EQ(registry.version(e3), entt::DefaultRegistry::version_type{0});
|
||||
ASSERT_EQ(registry.current(e3), entt::DefaultRegistry::version_type{1});
|
||||
ASSERT_EQ(registry.version(e2), entt::DefaultRegistry::version_type{0});
|
||||
ASSERT_EQ(registry.current(e2), entt::DefaultRegistry::version_type{1});
|
||||
|
||||
ASSERT_TRUE(registry.valid(e0));
|
||||
ASSERT_TRUE(registry.fast(e0));
|
||||
ASSERT_TRUE(registry.valid(e1));
|
||||
ASSERT_TRUE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
ASSERT_TRUE(registry.fast(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.fast(e2));
|
||||
|
||||
ASSERT_EQ(registry.size(), entt::DefaultRegistry::size_type{2});
|
||||
ASSERT_FALSE(registry.empty());
|
||||
@@ -110,30 +127,283 @@ TEST(DefaultRegistry, Functionalities) {
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
ASSERT_TRUE(registry.empty<char>());
|
||||
|
||||
e1 = registry.create<int>();
|
||||
e2 = registry.create();
|
||||
e0 = registry.create<int>();
|
||||
e1 = registry.create();
|
||||
|
||||
ASSERT_NO_THROW(registry.reset<int>(e0));
|
||||
ASSERT_NO_THROW(registry.reset<int>(e1));
|
||||
ASSERT_NO_THROW(registry.reset<int>(e2));
|
||||
|
||||
ASSERT_EQ(registry.size<int>(), entt::DefaultRegistry::size_type{0});
|
||||
ASSERT_EQ(registry.size<char>(), entt::DefaultRegistry::size_type{0});
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, CreateDestroyEntities) {
|
||||
TEST(DefaultRegistry, CreateDestroyCornerCase) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto pre = registry.create<double>();
|
||||
registry.destroy(pre);
|
||||
auto post = registry.create<double>();
|
||||
auto e0 = registry.create();
|
||||
auto e1 = registry.create();
|
||||
|
||||
registry.destroy(e0);
|
||||
registry.destroy(e1);
|
||||
|
||||
registry.each([](auto) { FAIL(); });
|
||||
|
||||
ASSERT_EQ(registry.current(e0), entt::DefaultRegistry::version_type{1});
|
||||
ASSERT_EQ(registry.current(e1), entt::DefaultRegistry::version_type{1});
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, VersionOverflow) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto entity = registry.create();
|
||||
registry.destroy(entity);
|
||||
|
||||
ASSERT_EQ(registry.version(entity), entt::DefaultRegistry::version_type{});
|
||||
|
||||
for(auto i = entt::entt_traits<entt::DefaultRegistry::entity_type>::version_mask; i; --i) {
|
||||
ASSERT_NE(registry.current(entity), registry.version(entity));
|
||||
registry.destroy(registry.create());
|
||||
}
|
||||
|
||||
ASSERT_EQ(registry.current(entity), registry.version(entity));
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultRegistry::size_type tot;
|
||||
entt::DefaultRegistry::size_type match;
|
||||
|
||||
registry.create();
|
||||
registry.create<int>();
|
||||
registry.create();
|
||||
registry.create<int>();
|
||||
registry.create();
|
||||
|
||||
tot = 0u;
|
||||
match = 0u;
|
||||
|
||||
registry.each([&](auto entity) {
|
||||
if(registry.has<int>(entity)) { ++match; }
|
||||
registry.create();
|
||||
++tot;
|
||||
});
|
||||
|
||||
ASSERT_EQ(tot, 5u);
|
||||
ASSERT_EQ(match, 2u);
|
||||
|
||||
tot = 0u;
|
||||
match = 0u;
|
||||
|
||||
registry.each([&](auto entity) {
|
||||
if(registry.has<int>(entity)) {
|
||||
registry.destroy(entity);
|
||||
++match;
|
||||
}
|
||||
|
||||
++tot;
|
||||
});
|
||||
|
||||
ASSERT_EQ(tot, 10u);
|
||||
ASSERT_EQ(match, 2u);
|
||||
|
||||
tot = 0u;
|
||||
match = 0u;
|
||||
|
||||
registry.each([&](auto entity) {
|
||||
if(registry.has<int>(entity)) { ++match; }
|
||||
registry.destroy(entity);
|
||||
++tot;
|
||||
});
|
||||
|
||||
ASSERT_EQ(tot, 8u);
|
||||
ASSERT_EQ(match, 0u);
|
||||
|
||||
registry.each([&](auto) { FAIL(); });
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, Orphans) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultRegistry::size_type tot{};
|
||||
|
||||
registry.create<int>();
|
||||
registry.create();
|
||||
registry.create<int>();
|
||||
registry.create();
|
||||
registry.attach<double>(registry.create());
|
||||
|
||||
registry.orphans([&](auto) { ++tot; });
|
||||
ASSERT_EQ(tot, 2u);
|
||||
tot = 0u;
|
||||
|
||||
registry.each([&](auto entity) { registry.reset<int>(entity); });
|
||||
registry.orphans([&](auto) { ++tot; });
|
||||
ASSERT_EQ(tot, 4u);
|
||||
registry.reset();
|
||||
tot = 0u;
|
||||
|
||||
registry.orphans([&](auto) { ++tot; });
|
||||
ASSERT_EQ(tot, 0u);
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, Types) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
ASSERT_EQ(registry.tag<int>(), registry.tag<int>());
|
||||
ASSERT_EQ(registry.component<int>(), registry.component<int>());
|
||||
|
||||
ASSERT_NE(registry.tag<int>(), registry.tag<double>());
|
||||
ASSERT_NE(registry.component<int>(), registry.component<double>());
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, CreateDestroyEntities) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultRegistry::entity_type pre{}, post{};
|
||||
|
||||
for(int i = 0; i < 10; ++i) {
|
||||
registry.create<double>();
|
||||
}
|
||||
|
||||
registry.reset();
|
||||
|
||||
for(int i = 0; i < 7; ++i) {
|
||||
auto entity = registry.create<int>();
|
||||
if(i == 3) { pre = entity; }
|
||||
}
|
||||
|
||||
registry.reset();
|
||||
|
||||
for(int i = 0; i < 5; ++i) {
|
||||
auto entity = registry.create();
|
||||
if(i == 3) { post = entity; }
|
||||
}
|
||||
|
||||
ASSERT_FALSE(registry.valid(pre));
|
||||
ASSERT_TRUE(registry.valid(post));
|
||||
ASSERT_NE(registry.version(pre), registry.version(post));
|
||||
ASSERT_EQ(registry.version(pre) + 1, registry.version(post));
|
||||
ASSERT_EQ(registry.current(pre), registry.current(post));
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, AttachSetRemoveTags) {
|
||||
entt::DefaultRegistry registry;
|
||||
const auto &cregistry = registry;
|
||||
|
||||
ASSERT_FALSE(registry.has<int>());
|
||||
|
||||
auto entity = registry.create();
|
||||
registry.attach<int>(entity, 42);
|
||||
|
||||
ASSERT_TRUE(registry.has<int>());
|
||||
ASSERT_EQ(registry.get<int>(), 42);
|
||||
ASSERT_EQ(cregistry.get<int>(), 42);
|
||||
ASSERT_EQ(registry.attachee<int>(), entity);
|
||||
|
||||
registry.set<int>(3);
|
||||
|
||||
ASSERT_TRUE(registry.has<int>());
|
||||
ASSERT_EQ(registry.get<int>(), 3);
|
||||
ASSERT_EQ(cregistry.get<int>(), 3);
|
||||
ASSERT_EQ(registry.attachee<int>(), entity);
|
||||
|
||||
auto other = registry.create();
|
||||
registry.move<int>(other);
|
||||
|
||||
ASSERT_TRUE(registry.has<int>());
|
||||
ASSERT_EQ(registry.get<int>(), 3);
|
||||
ASSERT_EQ(cregistry.get<int>(), 3);
|
||||
ASSERT_EQ(registry.attachee<int>(), other);
|
||||
|
||||
registry.remove<int>();
|
||||
|
||||
ASSERT_FALSE(registry.has<int>());
|
||||
|
||||
registry.attach<int>(entity, 42);
|
||||
registry.destroy(entity);
|
||||
|
||||
ASSERT_FALSE(registry.has<int>());
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, StandardViews) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto mview = registry.view<int, char>();
|
||||
auto iview = registry.view<int>();
|
||||
auto cview = registry.view<char>();
|
||||
|
||||
registry.create(0, 'c');
|
||||
registry.create(0);
|
||||
registry.create(0, 'c');
|
||||
|
||||
ASSERT_EQ(iview.size(), decltype(iview)::size_type{3});
|
||||
ASSERT_EQ(cview.size(), decltype(cview)::size_type{2});
|
||||
|
||||
decltype(mview)::size_type cnt{0};
|
||||
mview.each([&cnt](auto...) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, decltype(mview)::size_type{2});
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, PersistentViews) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.persistent<int, char>();
|
||||
|
||||
ASSERT_TRUE((registry.contains<int, char>()));
|
||||
ASSERT_FALSE((registry.contains<int, double>()));
|
||||
|
||||
registry.prepare<int, double>();
|
||||
|
||||
ASSERT_TRUE((registry.contains<int, double>()));
|
||||
|
||||
registry.discard<int, double>();
|
||||
|
||||
ASSERT_FALSE((registry.contains<int, double>()));
|
||||
|
||||
registry.create(0, 'c');
|
||||
registry.create(0);
|
||||
registry.create(0, 'c');
|
||||
|
||||
decltype(view)::size_type cnt{0};
|
||||
view.each([&cnt](auto...) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, decltype(view)::size_type{2});
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, CleanStandardViewsAfterReset) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.view<int>();
|
||||
registry.create(0);
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{1});
|
||||
|
||||
registry.reset();
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{0});
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, CleanPersistentViewsAfterReset) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.persistent<int, char>();
|
||||
registry.create(0, 'c');
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{1});
|
||||
|
||||
registry.reset();
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{0});
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, CleanTagsAfterReset) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto entity = registry.create();
|
||||
registry.attach<int>(entity);
|
||||
|
||||
ASSERT_TRUE(registry.has<int>());
|
||||
|
||||
registry.reset();
|
||||
|
||||
ASSERT_FALSE(registry.has<int>());
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, SortSingle) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
@@ -183,3 +453,58 @@ TEST(DefaultRegistry, SortMulti) {
|
||||
ASSERT_EQ(registry.get<int>(entity), ival++);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, ComponentsWithTypesFromStandardTemplateLibrary) {
|
||||
// see #37 - the test shouldn't crash, that's all
|
||||
entt::DefaultRegistry registry;
|
||||
auto entity = registry.create();
|
||||
registry.assign<std::unordered_set<int>>(entity).insert(42);
|
||||
registry.destroy(entity);
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, ConstructWithComponents) {
|
||||
// it should compile, that's all
|
||||
entt::DefaultRegistry registry;
|
||||
const auto value = 0;
|
||||
registry.create(value);
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, MergeTwoRegistries) {
|
||||
using entity_type = entt::DefaultRegistry::entity_type;
|
||||
|
||||
entt::DefaultRegistry src;
|
||||
entt::DefaultRegistry dst;
|
||||
|
||||
std::unordered_map<entity_type, entity_type> ref;
|
||||
|
||||
auto merge = [&ref](const auto &view, auto &dst) {
|
||||
view.each([&](auto entity, const auto &component) {
|
||||
if(ref.find(entity) == ref.cend()) {
|
||||
ref.emplace(entity, dst.create(component));
|
||||
} else {
|
||||
using component_type = std::decay_t<decltype(component)>;
|
||||
dst.template assign<component_type>(ref[entity], component);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
src.create<int, float, double>();
|
||||
src.create<char, float, int>();
|
||||
|
||||
dst.create<int, char, double>();
|
||||
dst.create<float, int>();
|
||||
|
||||
auto eq = [](auto begin, auto end) { ASSERT_EQ(begin, end); };
|
||||
auto ne = [](auto begin, auto end) { ASSERT_NE(begin, end); };
|
||||
|
||||
eq(dst.view<int, float, double>().begin(), dst.view<int, float, double>().end());
|
||||
eq(dst.view<char, float, int>().begin(), dst.view<char, float, int>().end());
|
||||
|
||||
merge(src.view<int>(), dst);
|
||||
merge(src.view<char>(), dst);
|
||||
merge(src.view<double>(), dst);
|
||||
merge(src.view<float>(), dst);
|
||||
|
||||
ne(dst.view<int, float, double>().begin(), dst.view<int, float, double>().end());
|
||||
ne(dst.view<char, float, int>().begin(), dst.view<char, float, int>().end());
|
||||
}
|
||||
|
||||
489
test/entt/entity/snapshot.cpp
Normal file
489
test/entt/entity/snapshot.cpp
Normal file
@@ -0,0 +1,489 @@
|
||||
#include <tuple>
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
template<typename Storage>
|
||||
struct OutputArchive {
|
||||
OutputArchive(Storage &storage)
|
||||
: storage{storage}
|
||||
{}
|
||||
|
||||
template<typename Value>
|
||||
void operator()(const Value &value) {
|
||||
std::get<std::queue<Value>>(storage).push(value);
|
||||
}
|
||||
|
||||
private:
|
||||
Storage &storage;
|
||||
};
|
||||
|
||||
template<typename Storage>
|
||||
struct InputArchive {
|
||||
InputArchive(Storage &storage)
|
||||
: storage{storage}
|
||||
{}
|
||||
|
||||
template<typename Value>
|
||||
void operator()(Value &value) {
|
||||
auto &queue = std::get<std::queue<Value>>(storage);
|
||||
value = queue.front();
|
||||
queue.pop();
|
||||
}
|
||||
|
||||
private:
|
||||
Storage &storage;
|
||||
};
|
||||
|
||||
struct AComponent {};
|
||||
|
||||
struct AnotherComponent {
|
||||
int key;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct Foo {
|
||||
entt::DefaultRegistry::entity_type bar;
|
||||
std::vector<entt::DefaultRegistry::entity_type> quux;
|
||||
};
|
||||
|
||||
TEST(Snapshot, Dump) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 42);
|
||||
registry.assign<char>(e0, 'c');
|
||||
registry.assign<double>(e0, .1);
|
||||
|
||||
auto e1 = registry.create();
|
||||
|
||||
auto e2 = registry.create();
|
||||
registry.assign<int>(e2, 3);
|
||||
|
||||
auto e3 = registry.create();
|
||||
registry.assign<char>(e3, '0');
|
||||
registry.attach<float>(e3, .3f);
|
||||
|
||||
auto e4 = registry.create();
|
||||
registry.attach<AComponent>(e4);
|
||||
|
||||
registry.destroy(e1);
|
||||
auto v1 = registry.current(e1);
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entt::DefaultRegistry::entity_type>,
|
||||
std::queue<int>,
|
||||
std::queue<char>,
|
||||
std::queue<double>,
|
||||
std::queue<float>,
|
||||
std::queue<bool>,
|
||||
std::queue<AComponent>,
|
||||
std::queue<AnotherComponent>,
|
||||
std::queue<Foo>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
|
||||
registry.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<int, char, AnotherComponent, double>(output)
|
||||
.tag<float, bool, AComponent>(output);
|
||||
|
||||
registry.reset();
|
||||
|
||||
ASSERT_FALSE(registry.valid(e0));
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
ASSERT_FALSE(registry.valid(e4));
|
||||
|
||||
registry.restore()
|
||||
.entities(input)
|
||||
.destroyed(input)
|
||||
.component<int, char, AnotherComponent, double>(input)
|
||||
.tag<float, bool, AComponent>(input)
|
||||
.orphans();
|
||||
|
||||
ASSERT_TRUE(registry.valid(e0));
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_TRUE(registry.valid(e2));
|
||||
ASSERT_TRUE(registry.valid(e3));
|
||||
ASSERT_TRUE(registry.valid(e4));
|
||||
|
||||
ASSERT_FALSE(registry.orphan(e0));
|
||||
ASSERT_FALSE(registry.orphan(e2));
|
||||
ASSERT_FALSE(registry.orphan(e3));
|
||||
ASSERT_FALSE(registry.orphan(e4));
|
||||
|
||||
ASSERT_EQ(registry.get<int>(e0), 42);
|
||||
ASSERT_EQ(registry.get<char>(e0), 'c');
|
||||
ASSERT_EQ(registry.get<double>(e0), .1);
|
||||
ASSERT_EQ(registry.current(e1), v1);
|
||||
ASSERT_EQ(registry.get<int>(e2), 3);
|
||||
ASSERT_EQ(registry.get<char>(e3), '0');
|
||||
|
||||
ASSERT_TRUE(registry.has<float>());
|
||||
ASSERT_EQ(registry.attachee<float>(), e3);
|
||||
ASSERT_EQ(registry.get<float>(), .3f);
|
||||
|
||||
ASSERT_TRUE(registry.has<AComponent>());
|
||||
ASSERT_EQ(registry.attachee<AComponent>(), e4);
|
||||
|
||||
ASSERT_TRUE(registry.empty<AnotherComponent>());
|
||||
ASSERT_FALSE(registry.has<long int>());
|
||||
}
|
||||
|
||||
TEST(Snapshot, Partial) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 42);
|
||||
registry.assign<char>(e0, 'c');
|
||||
registry.assign<double>(e0, .1);
|
||||
|
||||
auto e1 = registry.create();
|
||||
|
||||
auto e2 = registry.create();
|
||||
registry.assign<int>(e2, 3);
|
||||
|
||||
auto e3 = registry.create();
|
||||
registry.assign<char>(e3, '0');
|
||||
registry.attach<float>(e3, .3f);
|
||||
|
||||
auto e4 = registry.create();
|
||||
registry.attach<AComponent>(e4);
|
||||
|
||||
registry.destroy(e1);
|
||||
auto v1 = registry.current(e1);
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entt::DefaultRegistry::entity_type>,
|
||||
std::queue<int>,
|
||||
std::queue<char>,
|
||||
std::queue<double>,
|
||||
std::queue<float>,
|
||||
std::queue<bool>,
|
||||
std::queue<AComponent>,
|
||||
std::queue<Foo>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
|
||||
registry.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<char, int>(output)
|
||||
.tag<bool, float>(output);
|
||||
|
||||
registry.reset();
|
||||
|
||||
ASSERT_FALSE(registry.valid(e0));
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
ASSERT_FALSE(registry.valid(e4));
|
||||
|
||||
registry.restore()
|
||||
.entities(input)
|
||||
.destroyed(input)
|
||||
.component<char, int>(input)
|
||||
.tag<bool, float>(input);
|
||||
|
||||
ASSERT_TRUE(registry.valid(e0));
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_TRUE(registry.valid(e2));
|
||||
ASSERT_TRUE(registry.valid(e3));
|
||||
ASSERT_TRUE(registry.valid(e4));
|
||||
|
||||
ASSERT_EQ(registry.get<int>(e0), 42);
|
||||
ASSERT_EQ(registry.get<char>(e0), 'c');
|
||||
ASSERT_FALSE(registry.has<double>(e0));
|
||||
ASSERT_EQ(registry.current(e1), v1);
|
||||
ASSERT_EQ(registry.get<int>(e2), 3);
|
||||
ASSERT_EQ(registry.get<char>(e3), '0');
|
||||
ASSERT_TRUE(registry.orphan(e4));
|
||||
|
||||
ASSERT_TRUE(registry.has<float>());
|
||||
ASSERT_EQ(registry.attachee<float>(), e3);
|
||||
ASSERT_EQ(registry.get<float>(), .3f);
|
||||
ASSERT_FALSE(registry.has<long int>());
|
||||
|
||||
registry.snapshot()
|
||||
.tag<float>(output)
|
||||
.destroyed(output)
|
||||
.entities(output);
|
||||
|
||||
registry.reset();
|
||||
|
||||
ASSERT_FALSE(registry.valid(e0));
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
ASSERT_FALSE(registry.valid(e4));
|
||||
|
||||
registry.restore()
|
||||
.tag<float>(input)
|
||||
.destroyed(input)
|
||||
.entities(input)
|
||||
.orphans();
|
||||
|
||||
ASSERT_FALSE(registry.valid(e0));
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_TRUE(registry.valid(e3));
|
||||
ASSERT_FALSE(registry.valid(e4));
|
||||
}
|
||||
|
||||
TEST(Snapshot, Continuous) {
|
||||
using entity_type = entt::DefaultRegistry::entity_type;
|
||||
|
||||
entt::DefaultRegistry src;
|
||||
entt::DefaultRegistry dst;
|
||||
|
||||
entt::ContinuousLoader<entity_type> loader{dst};
|
||||
|
||||
std::vector<entity_type> entities;
|
||||
entity_type entity;
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entity_type>,
|
||||
std::queue<AComponent>,
|
||||
std::queue<AnotherComponent>,
|
||||
std::queue<Foo>,
|
||||
std::queue<double>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
|
||||
for(int i = 0; i < 10; ++i) {
|
||||
src.create();
|
||||
}
|
||||
|
||||
src.each([&src](auto entity) {
|
||||
src.destroy(entity);
|
||||
});
|
||||
|
||||
for(int i = 0; i < 5; ++i) {
|
||||
entity = src.create();
|
||||
entities.push_back(entity);
|
||||
|
||||
src.assign<AComponent>(entity);
|
||||
src.assign<AnotherComponent>(entity, i, i);
|
||||
|
||||
if(i % 2) {
|
||||
src.assign<Foo>(entity, entity);
|
||||
} else if(i == 2) {
|
||||
src.attach<double>(entity, .3);
|
||||
}
|
||||
}
|
||||
|
||||
src.view<Foo>().each([&entities](auto, auto &foo) {
|
||||
foo.quux.insert(foo.quux.begin(), entities.begin(), entities.end());
|
||||
});
|
||||
|
||||
entity = dst.create();
|
||||
dst.assign<AComponent>(entity);
|
||||
dst.assign<AnotherComponent>(entity, -1, -1);
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, Foo>(output)
|
||||
.tag<double>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent>(input)
|
||||
.component<Foo>(input, &Foo::bar, &Foo::quux)
|
||||
.tag<double>(input)
|
||||
.orphans();
|
||||
|
||||
decltype(dst.size()) aComponentCnt{};
|
||||
decltype(dst.size()) anotherComponentCnt{};
|
||||
decltype(dst.size()) fooCnt{};
|
||||
|
||||
dst.each([&dst, &aComponentCnt](auto entity) {
|
||||
ASSERT_TRUE(dst.has<AComponent>(entity));
|
||||
++aComponentCnt;
|
||||
});
|
||||
|
||||
dst.view<AnotherComponent>().each([&anotherComponentCnt](auto, const auto &component) {
|
||||
ASSERT_EQ(component.value, component.key < 0 ? -1 : component.key);
|
||||
++anotherComponentCnt;
|
||||
});
|
||||
|
||||
dst.view<Foo>().each([&dst, &fooCnt](auto entity, const auto &component) {
|
||||
ASSERT_EQ(entity, component.bar);
|
||||
|
||||
for(auto entity: component.quux) {
|
||||
ASSERT_TRUE(dst.valid(entity));
|
||||
}
|
||||
|
||||
++fooCnt;
|
||||
});
|
||||
|
||||
ASSERT_TRUE(dst.has<double>());
|
||||
ASSERT_EQ(dst.get<double>(), .3);
|
||||
|
||||
src.view<AnotherComponent>().each([](auto, auto &component) {
|
||||
component.value = 2 * component.key;
|
||||
});
|
||||
|
||||
auto size = dst.size();
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, Foo>(output)
|
||||
.tag<double>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent>(input)
|
||||
.component<Foo>(input, &Foo::bar, &Foo::quux)
|
||||
.tag<double>(input)
|
||||
.orphans();
|
||||
|
||||
ASSERT_EQ(size, dst.size());
|
||||
|
||||
ASSERT_EQ(dst.size<AComponent>(), aComponentCnt);
|
||||
ASSERT_EQ(dst.size<AnotherComponent>(), anotherComponentCnt);
|
||||
ASSERT_EQ(dst.size<Foo>(), fooCnt);
|
||||
ASSERT_TRUE(dst.has<double>());
|
||||
|
||||
dst.view<AnotherComponent>().each([](auto, auto &component) {
|
||||
ASSERT_EQ(component.value, component.key < 0 ? -1 : (2 * component.key));
|
||||
});
|
||||
|
||||
entity = src.create();
|
||||
|
||||
src.view<Foo>().each([entity](auto, auto &component) {
|
||||
component.bar = entity;
|
||||
});
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, Foo>(output)
|
||||
.tag<double>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent>(input)
|
||||
.component<Foo>(input, &Foo::bar, &Foo::quux)
|
||||
.tag<double>(input)
|
||||
.orphans();
|
||||
|
||||
dst.view<Foo>().each([&loader, entity](auto, auto &component) {
|
||||
ASSERT_EQ(component.bar, loader.map(entity));
|
||||
});
|
||||
|
||||
entities.clear();
|
||||
for(auto entity: src.view<AComponent>()) {
|
||||
entities.push_back(entity);
|
||||
}
|
||||
|
||||
src.destroy(entity);
|
||||
loader.shrink();
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, Foo>(output)
|
||||
.tag<double>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent>(input)
|
||||
.component<Foo>(input, &Foo::bar, &Foo::quux)
|
||||
.tag<double>(input)
|
||||
.orphans()
|
||||
.shrink();
|
||||
|
||||
dst.view<Foo>().each([&dst, &loader, entity](auto, auto &component) {
|
||||
ASSERT_FALSE(dst.valid(component.bar));
|
||||
});
|
||||
|
||||
ASSERT_FALSE(loader.has(entity));
|
||||
|
||||
entity = src.create();
|
||||
|
||||
src.view<Foo>().each([entity](auto, auto &component) {
|
||||
component.bar = entity;
|
||||
});
|
||||
|
||||
dst.reset<AComponent>();
|
||||
aComponentCnt = src.size<AComponent>();
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, Foo>(output)
|
||||
.tag<double>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent>(input)
|
||||
.component<Foo>(input, &Foo::bar, &Foo::quux)
|
||||
.tag<double>(input)
|
||||
.orphans();
|
||||
|
||||
ASSERT_EQ(dst.size<AComponent>(), aComponentCnt);
|
||||
ASSERT_TRUE(dst.has<double>());
|
||||
|
||||
src.reset<AComponent>();
|
||||
src.remove<double>();
|
||||
aComponentCnt = {};
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, Foo>(output)
|
||||
.tag<double>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent>(input)
|
||||
.component<Foo>(input, &Foo::bar, &Foo::quux)
|
||||
.tag<double>(input)
|
||||
.orphans();
|
||||
|
||||
ASSERT_EQ(dst.size<AComponent>(), aComponentCnt);
|
||||
ASSERT_FALSE(dst.has<double>());
|
||||
}
|
||||
|
||||
TEST(Snapshot, ContinuousMoreOnShrink) {
|
||||
using entity_type = entt::DefaultRegistry::entity_type;
|
||||
|
||||
entt::DefaultRegistry src;
|
||||
entt::DefaultRegistry dst;
|
||||
|
||||
entt::ContinuousLoader<entity_type> loader{dst};
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entity_type>,
|
||||
std::queue<AComponent>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
|
||||
auto entity = src.create();
|
||||
src.snapshot().entities(output);
|
||||
loader.entities(input).shrink();
|
||||
|
||||
ASSERT_TRUE(dst.valid(entity));
|
||||
|
||||
loader.shrink();
|
||||
|
||||
ASSERT_FALSE(dst.valid(entity));
|
||||
}
|
||||
@@ -1,22 +1,27 @@
|
||||
#include <unordered_set>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/sparse_set.hpp>
|
||||
|
||||
TEST(SparseSetNoType, Functionalities) {
|
||||
entt::SparseSet<unsigned int> set;
|
||||
|
||||
ASSERT_NO_THROW(set.reserve(42));
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
ASSERT_EQ(set.construct(42), 0u);
|
||||
set.construct(42);
|
||||
|
||||
ASSERT_EQ(set.get(42), 0u);
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 1u);
|
||||
ASSERT_NE(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_TRUE(set.has(42));
|
||||
ASSERT_TRUE(set.fast(42));
|
||||
ASSERT_EQ(set.get(42), 0u);
|
||||
|
||||
set.destroy(42);
|
||||
@@ -27,7 +32,9 @@ TEST(SparseSetNoType, Functionalities) {
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
ASSERT_EQ(set.construct(42), 0u);
|
||||
set.construct(42);
|
||||
|
||||
ASSERT_EQ(set.get(42), 0u);
|
||||
|
||||
set.reset();
|
||||
|
||||
@@ -45,39 +52,216 @@ TEST(SparseSetNoType, Functionalities) {
|
||||
TEST(SparseSetNoType, DataBeginEnd) {
|
||||
entt::SparseSet<unsigned int> set;
|
||||
|
||||
ASSERT_EQ(set.construct(3), 0u);
|
||||
ASSERT_EQ(set.construct(12), 1u);
|
||||
ASSERT_EQ(set.construct(42), 2u);
|
||||
set.construct(3);
|
||||
set.construct(12);
|
||||
set.construct(42);
|
||||
|
||||
ASSERT_EQ(set.get(3), 0u);
|
||||
ASSERT_EQ(set.get(12), 1u);
|
||||
ASSERT_EQ(set.get(42), 2u);
|
||||
|
||||
ASSERT_EQ(*(set.data() + 0u), 3u);
|
||||
ASSERT_EQ(*(set.data() + 1u), 12u);
|
||||
ASSERT_EQ(*(set.data() + 2u), 42u);
|
||||
|
||||
auto it = set.begin();
|
||||
|
||||
ASSERT_EQ(*it, 42u);
|
||||
ASSERT_EQ(*(it+1), 12u);
|
||||
ASSERT_EQ(*(it+2), 3u);
|
||||
ASSERT_EQ(it += 3, set.end());
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ(*(begin++), 42u);
|
||||
ASSERT_EQ(*(begin++), 12u);
|
||||
ASSERT_EQ(*(begin++), 3u);
|
||||
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectDisjoint) {
|
||||
entt::SparseSet<unsigned int> lhs;
|
||||
entt::SparseSet<unsigned int> rhs;
|
||||
const auto &clhs = lhs;
|
||||
|
||||
lhs.construct(3);
|
||||
lhs.construct(12);
|
||||
lhs.construct(42);
|
||||
|
||||
ASSERT_EQ(lhs.get(3), 0u);
|
||||
ASSERT_EQ(lhs.get(12), 1u);
|
||||
ASSERT_EQ(lhs.get(42), 2u);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(clhs.get(3), 0u);
|
||||
ASSERT_EQ(clhs.get(12), 1u);
|
||||
ASSERT_EQ(clhs.get(42), 2u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectOverlap) {
|
||||
entt::SparseSet<unsigned int> lhs;
|
||||
entt::SparseSet<unsigned int> rhs;
|
||||
const auto &clhs = lhs;
|
||||
|
||||
lhs.construct(3);
|
||||
lhs.construct(12);
|
||||
lhs.construct(42);
|
||||
|
||||
rhs.construct(12);
|
||||
|
||||
ASSERT_EQ(lhs.get(3), 0u);
|
||||
ASSERT_EQ(lhs.get(12), 1u);
|
||||
ASSERT_EQ(lhs.get(42), 2u);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(clhs.get(3), 0u);
|
||||
ASSERT_EQ(clhs.get(12), 2u);
|
||||
ASSERT_EQ(clhs.get(42), 1u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectOrdered) {
|
||||
entt::SparseSet<unsigned int> lhs;
|
||||
entt::SparseSet<unsigned int> rhs;
|
||||
|
||||
lhs.construct(1);
|
||||
lhs.construct(2);
|
||||
lhs.construct(3);
|
||||
lhs.construct(4);
|
||||
lhs.construct(5);
|
||||
|
||||
ASSERT_EQ(lhs.get(1), 0u);
|
||||
ASSERT_EQ(lhs.get(2), 1u);
|
||||
ASSERT_EQ(lhs.get(3), 2u);
|
||||
ASSERT_EQ(lhs.get(4), 3u);
|
||||
ASSERT_EQ(lhs.get(5), 4u);
|
||||
|
||||
rhs.construct(6);
|
||||
rhs.construct(1);
|
||||
rhs.construct(2);
|
||||
rhs.construct(3);
|
||||
rhs.construct(4);
|
||||
rhs.construct(5);
|
||||
|
||||
ASSERT_EQ(rhs.get(6), 0u);
|
||||
ASSERT_EQ(rhs.get(1), 1u);
|
||||
ASSERT_EQ(rhs.get(2), 2u);
|
||||
ASSERT_EQ(rhs.get(3), 3u);
|
||||
ASSERT_EQ(rhs.get(4), 4u);
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
|
||||
rhs.respect(lhs);
|
||||
|
||||
ASSERT_EQ(rhs.get(6), 0u);
|
||||
ASSERT_EQ(rhs.get(1), 1u);
|
||||
ASSERT_EQ(rhs.get(2), 2u);
|
||||
ASSERT_EQ(rhs.get(3), 3u);
|
||||
ASSERT_EQ(rhs.get(4), 4u);
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectReverse) {
|
||||
entt::SparseSet<unsigned int> lhs;
|
||||
entt::SparseSet<unsigned int> rhs;
|
||||
|
||||
lhs.construct(1);
|
||||
lhs.construct(2);
|
||||
lhs.construct(3);
|
||||
lhs.construct(4);
|
||||
lhs.construct(5);
|
||||
|
||||
ASSERT_EQ(lhs.get(1), 0u);
|
||||
ASSERT_EQ(lhs.get(2), 1u);
|
||||
ASSERT_EQ(lhs.get(3), 2u);
|
||||
ASSERT_EQ(lhs.get(4), 3u);
|
||||
ASSERT_EQ(lhs.get(5), 4u);
|
||||
|
||||
rhs.construct(5);
|
||||
rhs.construct(4);
|
||||
rhs.construct(3);
|
||||
rhs.construct(2);
|
||||
rhs.construct(1);
|
||||
rhs.construct(6);
|
||||
|
||||
ASSERT_EQ(rhs.get(5), 0u);
|
||||
ASSERT_EQ(rhs.get(4), 1u);
|
||||
ASSERT_EQ(rhs.get(3), 2u);
|
||||
ASSERT_EQ(rhs.get(2), 3u);
|
||||
ASSERT_EQ(rhs.get(1), 4u);
|
||||
ASSERT_EQ(rhs.get(6), 5u);
|
||||
|
||||
rhs.respect(lhs);
|
||||
|
||||
ASSERT_EQ(rhs.get(6), 0u);
|
||||
ASSERT_EQ(rhs.get(1), 1u);
|
||||
ASSERT_EQ(rhs.get(2), 2u);
|
||||
ASSERT_EQ(rhs.get(3), 3u);
|
||||
ASSERT_EQ(rhs.get(4), 4u);
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectUnordered) {
|
||||
entt::SparseSet<unsigned int> lhs;
|
||||
entt::SparseSet<unsigned int> rhs;
|
||||
|
||||
lhs.construct(1);
|
||||
lhs.construct(2);
|
||||
lhs.construct(3);
|
||||
lhs.construct(4);
|
||||
lhs.construct(5);
|
||||
|
||||
ASSERT_EQ(lhs.get(1), 0u);
|
||||
ASSERT_EQ(lhs.get(2), 1u);
|
||||
ASSERT_EQ(lhs.get(3), 2u);
|
||||
ASSERT_EQ(lhs.get(4), 3u);
|
||||
ASSERT_EQ(lhs.get(5), 4u);
|
||||
|
||||
rhs.construct(3);
|
||||
rhs.construct(2);
|
||||
rhs.construct(6);
|
||||
rhs.construct(1);
|
||||
rhs.construct(4);
|
||||
rhs.construct(5);
|
||||
|
||||
ASSERT_EQ(rhs.get(3), 0u);
|
||||
ASSERT_EQ(rhs.get(2), 1u);
|
||||
ASSERT_EQ(rhs.get(6), 2u);
|
||||
ASSERT_EQ(rhs.get(1), 3u);
|
||||
ASSERT_EQ(rhs.get(4), 4u);
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
|
||||
rhs.respect(lhs);
|
||||
|
||||
ASSERT_EQ(rhs.get(6), 0u);
|
||||
ASSERT_EQ(rhs.get(1), 1u);
|
||||
ASSERT_EQ(rhs.get(2), 2u);
|
||||
ASSERT_EQ(rhs.get(3), 3u);
|
||||
ASSERT_EQ(rhs.get(4), 4u);
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, Functionalities) {
|
||||
entt::SparseSet<unsigned int, int> set;
|
||||
|
||||
ASSERT_NO_THROW(set.reserve(42));
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
ASSERT_EQ(set.construct(42, 3), 3);
|
||||
set.construct(42, 3);
|
||||
|
||||
ASSERT_EQ(set.get(42), 3);
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 1u);
|
||||
ASSERT_NE(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_TRUE(set.has(42));
|
||||
ASSERT_TRUE(set.fast(42));
|
||||
ASSERT_EQ(set.get(42), 3);
|
||||
|
||||
set.destroy(42);
|
||||
@@ -88,7 +272,9 @@ TEST(SparseSetWithType, Functionalities) {
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
ASSERT_EQ(set.construct(42, 12), 12);
|
||||
set.construct(42, 12);
|
||||
|
||||
ASSERT_EQ(set.get(42), 12);
|
||||
|
||||
set.reset();
|
||||
|
||||
@@ -103,12 +289,29 @@ TEST(SparseSetWithType, Functionalities) {
|
||||
other = std::move(set);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, AggregatesMustWork) {
|
||||
struct AggregateType { int value; };
|
||||
// the goal of this test is to enforce the requirements for aggregate types
|
||||
entt::SparseSet<unsigned int, AggregateType>{}.construct(0, 42);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, TypesFromStandardTemplateLibraryMustWork) {
|
||||
// see #37 - this test shouldn't crash, that's all
|
||||
entt::SparseSet<unsigned int, std::unordered_set<int>> set;
|
||||
set.construct(0).insert(42);
|
||||
set.destroy(0);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, RawBeginEnd) {
|
||||
entt::SparseSet<unsigned int, int> set;
|
||||
|
||||
ASSERT_EQ(set.construct(3, 3), 3);
|
||||
ASSERT_EQ(set.construct(12, 6), 6);
|
||||
ASSERT_EQ(set.construct(42, 9), 9);
|
||||
set.construct(3, 3);
|
||||
set.construct(12, 6);
|
||||
set.construct(42, 9);
|
||||
|
||||
ASSERT_EQ(set.get(3), 3);
|
||||
ASSERT_EQ(set.get(12), 6);
|
||||
ASSERT_EQ(set.get(42), 9);
|
||||
|
||||
ASSERT_EQ(*(set.raw() + 0u), 3);
|
||||
ASSERT_EQ(*(set.raw() + 1u), 6);
|
||||
@@ -117,23 +320,29 @@ TEST(SparseSetWithType, RawBeginEnd) {
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ(set.get(*(begin++)), 9);
|
||||
ASSERT_EQ(set.get(*(begin++)), 6);
|
||||
ASSERT_EQ(set.get(*(begin++)), 3);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, SortOrdered) {
|
||||
entt::SparseSet<unsigned int, int> set;
|
||||
|
||||
ASSERT_EQ(set.construct(12, 12), 12);
|
||||
ASSERT_EQ(set.construct(42, 9), 9);
|
||||
ASSERT_EQ(set.construct(7, 6), 6);
|
||||
ASSERT_EQ(set.construct(3, 3), 3);
|
||||
ASSERT_EQ(set.construct(9, 1), 1);
|
||||
set.construct(12, 12);
|
||||
set.construct(42, 9);
|
||||
set.construct(7, 6);
|
||||
set.construct(3, 3);
|
||||
set.construct(9, 1);
|
||||
|
||||
set.sort([&set](auto lhs, auto rhs) {
|
||||
return set.get(lhs) < set.get(rhs);
|
||||
ASSERT_EQ(set.get(12), 12);
|
||||
ASSERT_EQ(set.get(42), 9);
|
||||
ASSERT_EQ(set.get(7), 6);
|
||||
ASSERT_EQ(set.get(3), 3);
|
||||
ASSERT_EQ(set.get(9), 1);
|
||||
|
||||
set.sort([](auto lhs, auto rhs) {
|
||||
return lhs < rhs;
|
||||
});
|
||||
|
||||
ASSERT_EQ(*(set.raw() + 0u), 12);
|
||||
@@ -145,25 +354,31 @@ TEST(SparseSetWithType, SortOrdered) {
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ(set.get(*(begin++)), 1);
|
||||
ASSERT_EQ(set.get(*(begin++)), 3);
|
||||
ASSERT_EQ(set.get(*(begin++)), 6);
|
||||
ASSERT_EQ(set.get(*(begin++)), 9);
|
||||
ASSERT_EQ(set.get(*(begin++)), 12);
|
||||
ASSERT_EQ(*(begin++), 1);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 12);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, SortReverse) {
|
||||
entt::SparseSet<unsigned int, int> set;
|
||||
|
||||
ASSERT_EQ(set.construct(12, 1), 1);
|
||||
ASSERT_EQ(set.construct(42, 3), 3);
|
||||
ASSERT_EQ(set.construct(7, 6), 6);
|
||||
ASSERT_EQ(set.construct(3, 9), 9);
|
||||
ASSERT_EQ(set.construct(9, 12), 12);
|
||||
set.construct(12, 1);
|
||||
set.construct(42, 3);
|
||||
set.construct(7, 6);
|
||||
set.construct(3, 9);
|
||||
set.construct(9, 12);
|
||||
|
||||
set.sort([&set](auto lhs, auto rhs) {
|
||||
return set.get(lhs) < set.get(rhs);
|
||||
ASSERT_EQ(set.get(12), 1);
|
||||
ASSERT_EQ(set.get(42), 3);
|
||||
ASSERT_EQ(set.get(7), 6);
|
||||
ASSERT_EQ(set.get(3), 9);
|
||||
ASSERT_EQ(set.get(9), 12);
|
||||
|
||||
set.sort([](auto lhs, auto rhs) {
|
||||
return lhs < rhs;
|
||||
});
|
||||
|
||||
ASSERT_EQ(*(set.raw() + 0u), 12);
|
||||
@@ -175,25 +390,31 @@ TEST(SparseSetWithType, SortReverse) {
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ(set.get(*(begin++)), 1);
|
||||
ASSERT_EQ(set.get(*(begin++)), 3);
|
||||
ASSERT_EQ(set.get(*(begin++)), 6);
|
||||
ASSERT_EQ(set.get(*(begin++)), 9);
|
||||
ASSERT_EQ(set.get(*(begin++)), 12);
|
||||
ASSERT_EQ(*(begin++), 1);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 12);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, SortUnordered) {
|
||||
entt::SparseSet<unsigned int, int> set;
|
||||
|
||||
ASSERT_EQ(set.construct(12, 6), 6);
|
||||
ASSERT_EQ(set.construct(42, 3), 3);
|
||||
ASSERT_EQ(set.construct(7, 1), 1);
|
||||
ASSERT_EQ(set.construct(3, 9), 9);
|
||||
ASSERT_EQ(set.construct(9, 12), 12);
|
||||
set.construct(12, 6);
|
||||
set.construct(42, 3);
|
||||
set.construct(7, 1);
|
||||
set.construct(3, 9);
|
||||
set.construct(9, 12);
|
||||
|
||||
set.sort([&set](auto lhs, auto rhs) {
|
||||
return set.get(lhs) < set.get(rhs);
|
||||
ASSERT_EQ(set.get(12), 6);
|
||||
ASSERT_EQ(set.get(42), 3);
|
||||
ASSERT_EQ(set.get(7), 1);
|
||||
ASSERT_EQ(set.get(3), 9);
|
||||
ASSERT_EQ(set.get(9), 12);
|
||||
|
||||
set.sort([](auto lhs, auto rhs) {
|
||||
return lhs < rhs;
|
||||
});
|
||||
|
||||
ASSERT_EQ(*(set.raw() + 0u), 12);
|
||||
@@ -205,11 +426,11 @@ TEST(SparseSetWithType, SortUnordered) {
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ(set.get(*(begin++)), 1);
|
||||
ASSERT_EQ(set.get(*(begin++)), 3);
|
||||
ASSERT_EQ(set.get(*(begin++)), 6);
|
||||
ASSERT_EQ(set.get(*(begin++)), 9);
|
||||
ASSERT_EQ(set.get(*(begin++)), 12);
|
||||
ASSERT_EQ(*(begin++), 1);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 12);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
@@ -218,9 +439,13 @@ TEST(SparseSetWithType, RespectDisjoint) {
|
||||
entt::SparseSet<unsigned int, int> rhs;
|
||||
const auto &clhs = lhs;
|
||||
|
||||
ASSERT_EQ(lhs.construct(3, 3), 3);
|
||||
ASSERT_EQ(lhs.construct(12, 6), 6);
|
||||
ASSERT_EQ(lhs.construct(42, 9), 9);
|
||||
lhs.construct(3, 3);
|
||||
lhs.construct(12, 6);
|
||||
lhs.construct(42, 9);
|
||||
|
||||
ASSERT_EQ(clhs.get(3), 3);
|
||||
ASSERT_EQ(clhs.get(12), 6);
|
||||
ASSERT_EQ(clhs.get(42), 9);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
@@ -228,12 +453,12 @@ TEST(SparseSetWithType, RespectDisjoint) {
|
||||
ASSERT_EQ(*(clhs.raw() + 1u), 6);
|
||||
ASSERT_EQ(*(clhs.raw() + 2u), 9);
|
||||
|
||||
auto begin = clhs.begin();
|
||||
auto end = clhs.end();
|
||||
auto begin = lhs.begin();
|
||||
auto end = lhs.end();
|
||||
|
||||
ASSERT_EQ(clhs.get(*(begin++)), 9);
|
||||
ASSERT_EQ(clhs.get(*(begin++)), 6);
|
||||
ASSERT_EQ(clhs.get(*(begin++)), 3);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
@@ -242,10 +467,15 @@ TEST(SparseSetWithType, RespectOverlap) {
|
||||
entt::SparseSet<unsigned int, int> rhs;
|
||||
const auto &clhs = lhs;
|
||||
|
||||
ASSERT_EQ(lhs.construct(3, 3), 3);
|
||||
ASSERT_EQ(lhs.construct(12, 6), 6);
|
||||
ASSERT_EQ(lhs.construct(42, 9), 9);
|
||||
ASSERT_EQ(rhs.construct(12, 6), 6);
|
||||
lhs.construct(3, 3);
|
||||
lhs.construct(12, 6);
|
||||
lhs.construct(42, 9);
|
||||
rhs.construct(12, 6);
|
||||
|
||||
ASSERT_EQ(clhs.get(3), 3);
|
||||
ASSERT_EQ(clhs.get(12), 6);
|
||||
ASSERT_EQ(clhs.get(42), 9);
|
||||
ASSERT_EQ(rhs.get(12), 6);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
@@ -253,12 +483,12 @@ TEST(SparseSetWithType, RespectOverlap) {
|
||||
ASSERT_EQ(*(clhs.raw() + 1u), 9);
|
||||
ASSERT_EQ(*(clhs.raw() + 2u), 6);
|
||||
|
||||
auto begin = clhs.begin();
|
||||
auto end = clhs.end();
|
||||
auto begin = lhs.begin();
|
||||
auto end = lhs.end();
|
||||
|
||||
ASSERT_EQ(clhs.get(*(begin++)), 6);
|
||||
ASSERT_EQ(clhs.get(*(begin++)), 9);
|
||||
ASSERT_EQ(clhs.get(*(begin++)), 3);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
@@ -266,18 +496,31 @@ TEST(SparseSetWithType, RespectOrdered) {
|
||||
entt::SparseSet<unsigned int, int> lhs;
|
||||
entt::SparseSet<unsigned int, int> rhs;
|
||||
|
||||
ASSERT_EQ(lhs.construct(1, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(2, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(3, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(4, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(5, 0), 0);
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 0);
|
||||
|
||||
ASSERT_EQ(rhs.construct(6, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(1, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(2, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(3, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(4, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(5, 0), 0);
|
||||
ASSERT_EQ(lhs.get(1), 0);
|
||||
ASSERT_EQ(lhs.get(2), 0);
|
||||
ASSERT_EQ(lhs.get(3), 0);
|
||||
ASSERT_EQ(lhs.get(4), 0);
|
||||
ASSERT_EQ(lhs.get(5), 0);
|
||||
|
||||
rhs.construct(6, 0);
|
||||
rhs.construct(1, 0);
|
||||
rhs.construct(2, 0);
|
||||
rhs.construct(3, 0);
|
||||
rhs.construct(4, 0);
|
||||
rhs.construct(5, 0);
|
||||
|
||||
ASSERT_EQ(rhs.get(6), 0);
|
||||
ASSERT_EQ(rhs.get(1), 0);
|
||||
ASSERT_EQ(rhs.get(2), 0);
|
||||
ASSERT_EQ(rhs.get(3), 0);
|
||||
ASSERT_EQ(rhs.get(4), 0);
|
||||
ASSERT_EQ(rhs.get(5), 0);
|
||||
|
||||
rhs.respect(lhs);
|
||||
|
||||
@@ -299,18 +542,31 @@ TEST(SparseSetWithType, RespectReverse) {
|
||||
entt::SparseSet<unsigned int, int> lhs;
|
||||
entt::SparseSet<unsigned int, int> rhs;
|
||||
|
||||
ASSERT_EQ(lhs.construct(1, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(2, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(3, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(4, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(5, 0), 0);
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 0);
|
||||
|
||||
ASSERT_EQ(rhs.construct(5, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(4, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(3, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(2, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(1, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(6, 0), 0);
|
||||
ASSERT_EQ(lhs.get(1), 0);
|
||||
ASSERT_EQ(lhs.get(2), 0);
|
||||
ASSERT_EQ(lhs.get(3), 0);
|
||||
ASSERT_EQ(lhs.get(4), 0);
|
||||
ASSERT_EQ(lhs.get(5), 0);
|
||||
|
||||
rhs.construct(5, 0);
|
||||
rhs.construct(4, 0);
|
||||
rhs.construct(3, 0);
|
||||
rhs.construct(2, 0);
|
||||
rhs.construct(1, 0);
|
||||
rhs.construct(6, 0);
|
||||
|
||||
ASSERT_EQ(rhs.get(5), 0);
|
||||
ASSERT_EQ(rhs.get(4), 0);
|
||||
ASSERT_EQ(rhs.get(3), 0);
|
||||
ASSERT_EQ(rhs.get(2), 0);
|
||||
ASSERT_EQ(rhs.get(1), 0);
|
||||
ASSERT_EQ(rhs.get(6), 0);
|
||||
|
||||
rhs.respect(lhs);
|
||||
|
||||
@@ -332,18 +588,31 @@ TEST(SparseSetWithType, RespectUnordered) {
|
||||
entt::SparseSet<unsigned int, int> lhs;
|
||||
entt::SparseSet<unsigned int, int> rhs;
|
||||
|
||||
ASSERT_EQ(lhs.construct(1, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(2, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(3, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(4, 0), 0);
|
||||
ASSERT_EQ(lhs.construct(5, 0), 0);
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 0);
|
||||
|
||||
ASSERT_EQ(rhs.construct(3, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(2, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(6, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(1, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(4, 0), 0);
|
||||
ASSERT_EQ(rhs.construct(5, 0), 0);
|
||||
ASSERT_EQ(lhs.get(1), 0);
|
||||
ASSERT_EQ(lhs.get(2), 0);
|
||||
ASSERT_EQ(lhs.get(3), 0);
|
||||
ASSERT_EQ(lhs.get(4), 0);
|
||||
ASSERT_EQ(lhs.get(5), 0);
|
||||
|
||||
rhs.construct(3, 0);
|
||||
rhs.construct(2, 0);
|
||||
rhs.construct(6, 0);
|
||||
rhs.construct(1, 0);
|
||||
rhs.construct(4, 0);
|
||||
rhs.construct(5, 0);
|
||||
|
||||
ASSERT_EQ(rhs.get(3), 0);
|
||||
ASSERT_EQ(rhs.get(2), 0);
|
||||
ASSERT_EQ(rhs.get(6), 0);
|
||||
ASSERT_EQ(rhs.get(1), 0);
|
||||
ASSERT_EQ(rhs.get(4), 0);
|
||||
ASSERT_EQ(rhs.get(5), 0);
|
||||
|
||||
rhs.respect(lhs);
|
||||
|
||||
@@ -360,3 +629,33 @@ TEST(SparseSetWithType, RespectUnordered) {
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, ReferencesGuaranteed) {
|
||||
struct Type { int value; };
|
||||
|
||||
entt::SparseSet<unsigned int, Type> set;
|
||||
|
||||
set.construct(0, 0);
|
||||
set.construct(1, 1);
|
||||
|
||||
ASSERT_EQ(set.get(0).value, 0);
|
||||
ASSERT_EQ(set.get(1).value, 1);
|
||||
|
||||
for(auto &&type: set) {
|
||||
if(type.value) {
|
||||
type.value = 42;
|
||||
}
|
||||
}
|
||||
|
||||
ASSERT_EQ(set.get(0).value, 0);
|
||||
ASSERT_EQ(set.get(1).value, 42);
|
||||
|
||||
auto begin = set.begin();
|
||||
|
||||
while(begin != set.end()) {
|
||||
(begin++)->value = 3;
|
||||
}
|
||||
|
||||
ASSERT_EQ(set.get(0).value, 3);
|
||||
ASSERT_EQ(set.get(1).value, 3);
|
||||
}
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
TEST(View, SingleComponent) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e1 = registry.create();
|
||||
auto e2 = registry.create<int, char>();
|
||||
auto e0 = registry.create();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
ASSERT_NO_THROW(registry.view<char>().begin()++);
|
||||
ASSERT_NO_THROW(++registry.view<char>().begin());
|
||||
@@ -16,30 +16,44 @@ TEST(View, SingleComponent) {
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
registry.assign<char>(e1);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
|
||||
|
||||
view.get(e1) = '1';
|
||||
view.get(e2) = '2';
|
||||
view.get(e0) = '1';
|
||||
view.get(e1) = '2';
|
||||
|
||||
for(auto entity: view) {
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
ASSERT_TRUE(cview.get(entity) == '1' || cview.get(entity) == '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(view.data() + 0), e2);
|
||||
ASSERT_EQ(*(view.data() + 1), e1);
|
||||
ASSERT_EQ(*(view.data() + 0), e1);
|
||||
ASSERT_EQ(*(view.data() + 1), e0);
|
||||
|
||||
ASSERT_EQ(*(view.raw() + 0), '2');
|
||||
ASSERT_EQ(*(static_cast<const decltype(view) &>(view).raw() + 1), '1');
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
registry.remove<char>(e2);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(View, SingleComponentContains) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e0 = registry.create<int>();
|
||||
auto e1 = registry.create<int>();
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
auto view = registry.view<int>();
|
||||
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
ASSERT_TRUE(view.contains(e1));
|
||||
}
|
||||
|
||||
TEST(View, SingleComponentEmpty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
@@ -78,8 +92,14 @@ TEST(View, SingleComponentEach) {
|
||||
TEST(View, MultipleComponent) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e1 = registry.create<char>();
|
||||
auto e2 = registry.create<int, char>();
|
||||
auto e0 = registry.create<char>();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
auto it = registry.view<char>().begin();
|
||||
|
||||
ASSERT_EQ(*it, e1);
|
||||
ASSERT_EQ(*(it+1), e0);
|
||||
ASSERT_EQ(it += 2, registry.view<char>().end());
|
||||
|
||||
ASSERT_NO_THROW((registry.view<int, char>().begin()++));
|
||||
ASSERT_NO_THROW((++registry.view<int, char>().begin()));
|
||||
@@ -87,22 +107,41 @@ TEST(View, MultipleComponent) {
|
||||
auto view = registry.view<int, char>();
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.begin()+1, view.end());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){1});
|
||||
|
||||
view.get<char>(e1) = '1';
|
||||
view.get<char>(e2) = '2';
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
|
||||
for(auto entity: view) {
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
ASSERT_TRUE(cview.get<char>(entity) == '2');
|
||||
ASSERT_EQ(std::get<0>(cview.get<int, char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
|
||||
ASSERT_EQ(cview.get<char>(entity), '2');
|
||||
}
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
registry.remove<char>(e2);
|
||||
view.reset();
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(View, MultipleComponentContains) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e0 = registry.create<int, char>();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
auto view = registry.view<int, char>();
|
||||
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
ASSERT_TRUE(view.contains(e1));
|
||||
}
|
||||
|
||||
TEST(View, MultipleComponentEmpty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
@@ -140,8 +179,8 @@ TEST(PersistentView, Prepare) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, char>();
|
||||
|
||||
auto e1 = registry.create<char>();
|
||||
auto e2 = registry.create<int, char>();
|
||||
auto e0 = registry.create<char>();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
ASSERT_NO_THROW((registry.persistent<int, char>().begin()++));
|
||||
ASSERT_NO_THROW((++registry.persistent<int, char>().begin()));
|
||||
@@ -151,26 +190,29 @@ TEST(PersistentView, Prepare) {
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
|
||||
|
||||
registry.remove<int>(e1);
|
||||
registry.remove<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
view.get<char>(e1) = '1';
|
||||
view.get<char>(e2) = '2';
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
|
||||
for(auto entity: view) {
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
ASSERT_TRUE(cview.get<char>(entity) == '2');
|
||||
ASSERT_EQ(std::get<0>(cview.get<int, char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
|
||||
ASSERT_EQ(cview.get<char>(entity), '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(view.data() + 0), e2);
|
||||
ASSERT_EQ(*(view.data() + 0), e1);
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
registry.remove<char>(e2);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
@@ -178,8 +220,8 @@ TEST(PersistentView, Prepare) {
|
||||
TEST(PersistentView, NoPrepare) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e1 = registry.create<char>();
|
||||
auto e2 = registry.create<int, char>();
|
||||
auto e0 = registry.create<char>();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
ASSERT_NO_THROW((registry.persistent<int, char>().begin()++));
|
||||
ASSERT_NO_THROW((++registry.persistent<int, char>().begin()));
|
||||
@@ -189,30 +231,47 @@ TEST(PersistentView, NoPrepare) {
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
|
||||
|
||||
registry.remove<int>(e1);
|
||||
registry.remove<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
view.get<char>(e1) = '1';
|
||||
view.get<char>(e2) = '2';
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
|
||||
for(auto entity: view) {
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
ASSERT_TRUE(cview.get<char>(entity) == '2');
|
||||
ASSERT_EQ(std::get<0>(cview.get<int, char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
|
||||
ASSERT_EQ(cview.get<char>(entity), '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(view.data() + 0), e2);
|
||||
ASSERT_EQ(*(view.data() + 0), e1);
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
registry.remove<char>(e2);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(PersistentView, Contains) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e0 = registry.create<int, char>();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
auto view = registry.persistent<int, char>();
|
||||
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
ASSERT_TRUE(view.contains(e1));
|
||||
}
|
||||
|
||||
TEST(PersistentView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
@@ -254,20 +313,20 @@ TEST(PersistentView, Sort) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, unsigned int>();
|
||||
|
||||
auto e0 = registry.create();
|
||||
auto e1 = registry.create();
|
||||
auto e2 = registry.create();
|
||||
auto e3 = registry.create();
|
||||
|
||||
auto uval = 0u;
|
||||
auto ival = 0;
|
||||
|
||||
registry.assign<unsigned int>(e0, uval++);
|
||||
registry.assign<unsigned int>(e1, uval++);
|
||||
registry.assign<unsigned int>(e2, uval++);
|
||||
registry.assign<unsigned int>(e3, uval++);
|
||||
|
||||
registry.assign<int>(e0, ival++);
|
||||
registry.assign<int>(e1, ival++);
|
||||
registry.assign<int>(e2, ival++);
|
||||
registry.assign<int>(e3, ival++);
|
||||
|
||||
auto view = registry.persistent<int, unsigned int>();
|
||||
|
||||
@@ -284,3 +343,65 @@ TEST(PersistentView, Sort) {
|
||||
ASSERT_EQ(view.get<int>(entity), ival++);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RawView, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e0 = registry.create();
|
||||
auto e1 = registry.create<int, char>();
|
||||
|
||||
ASSERT_NO_THROW(registry.raw<char>().begin()++);
|
||||
ASSERT_NO_THROW(++registry.raw<char>().begin());
|
||||
|
||||
auto view = registry.raw<char>();
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
registry.assign<char>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
|
||||
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
|
||||
for(auto &&component: view) {
|
||||
ASSERT_TRUE(component == '1' || component == '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(view.data() + 0), e1);
|
||||
ASSERT_EQ(*(view.data() + 1), e0);
|
||||
|
||||
ASSERT_EQ(*(view.raw() + 0), '2');
|
||||
ASSERT_EQ(*(static_cast<const decltype(view) &>(view).raw() + 1), '1');
|
||||
|
||||
for(auto &&component: view) {
|
||||
// verifies that iterators return references to components
|
||||
component = '0';
|
||||
}
|
||||
|
||||
for(auto &&component: view) {
|
||||
ASSERT_TRUE(component == '0');
|
||||
}
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(RawView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
registry.create<char, double>();
|
||||
registry.create<char>();
|
||||
|
||||
auto view = registry.raw<int>();
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{0});
|
||||
|
||||
for(auto &&component: view) {
|
||||
(void)component;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
206
test/entt/process/process.cpp
Normal file
206
test/entt/process/process.cpp
Normal file
@@ -0,0 +1,206 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cstdint>
|
||||
#include <entt/process/process.hpp>
|
||||
|
||||
struct FakeProcess: entt::Process<FakeProcess, int> {
|
||||
using process_type = entt::Process<FakeProcess, int>;
|
||||
|
||||
void succeed() noexcept { process_type::succeed(); }
|
||||
void fail() noexcept { process_type::fail(); }
|
||||
void pause() noexcept { process_type::pause(); }
|
||||
void unpause() noexcept { process_type::unpause(); }
|
||||
|
||||
void init(void *) { initInvoked = true; }
|
||||
void succeeded() { succeededInvoked = true; }
|
||||
void failed() { failedInvoked = true; }
|
||||
void aborted() { abortedInvoked = true; }
|
||||
|
||||
void update(delta_type, void *data) {
|
||||
if(data) {
|
||||
(*static_cast<int *>(data))++;
|
||||
}
|
||||
|
||||
updateInvoked = true;
|
||||
}
|
||||
|
||||
bool initInvoked{false};
|
||||
bool updateInvoked{false};
|
||||
bool succeededInvoked{false};
|
||||
bool failedInvoked{false};
|
||||
bool abortedInvoked{false};
|
||||
};
|
||||
|
||||
TEST(Process, Basics) {
|
||||
FakeProcess process;
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_FALSE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
process.succeed();
|
||||
process.fail();
|
||||
process.abort();
|
||||
process.pause();
|
||||
process.unpause();
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_FALSE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_TRUE(process.alive());
|
||||
ASSERT_FALSE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
process.pause();
|
||||
|
||||
ASSERT_TRUE(process.alive());
|
||||
ASSERT_FALSE(process.dead());
|
||||
ASSERT_TRUE(process.paused());
|
||||
|
||||
process.unpause();
|
||||
|
||||
ASSERT_TRUE(process.alive());
|
||||
ASSERT_FALSE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
}
|
||||
|
||||
TEST(Process, Succeeded) {
|
||||
FakeProcess process;
|
||||
|
||||
process.tick(0);
|
||||
process.succeed();
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
ASSERT_TRUE(process.initInvoked);
|
||||
ASSERT_TRUE(process.updateInvoked);
|
||||
ASSERT_TRUE(process.succeededInvoked);
|
||||
ASSERT_FALSE(process.failedInvoked);
|
||||
ASSERT_FALSE(process.abortedInvoked);
|
||||
}
|
||||
|
||||
TEST(Process, Fail) {
|
||||
FakeProcess process;
|
||||
|
||||
process.tick(0);
|
||||
process.fail();
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
ASSERT_TRUE(process.initInvoked);
|
||||
ASSERT_TRUE(process.updateInvoked);
|
||||
ASSERT_FALSE(process.succeededInvoked);
|
||||
ASSERT_TRUE(process.failedInvoked);
|
||||
ASSERT_FALSE(process.abortedInvoked);
|
||||
}
|
||||
|
||||
TEST(Process, Data) {
|
||||
FakeProcess process;
|
||||
int value = 0;
|
||||
|
||||
process.tick(0, &value);
|
||||
process.succeed();
|
||||
process.tick(0, &value);
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
ASSERT_EQ(value, 1);
|
||||
ASSERT_TRUE(process.initInvoked);
|
||||
ASSERT_TRUE(process.updateInvoked);
|
||||
ASSERT_TRUE(process.succeededInvoked);
|
||||
ASSERT_FALSE(process.failedInvoked);
|
||||
ASSERT_FALSE(process.abortedInvoked);
|
||||
}
|
||||
|
||||
TEST(Process, AbortNextTick) {
|
||||
FakeProcess process;
|
||||
|
||||
process.tick(0);
|
||||
process.abort();
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
ASSERT_TRUE(process.initInvoked);
|
||||
ASSERT_TRUE(process.updateInvoked);
|
||||
ASSERT_FALSE(process.succeededInvoked);
|
||||
ASSERT_FALSE(process.failedInvoked);
|
||||
ASSERT_TRUE(process.abortedInvoked);
|
||||
}
|
||||
|
||||
TEST(Process, AbortImmediately) {
|
||||
FakeProcess process;
|
||||
|
||||
process.tick(0);
|
||||
process.abort(true);
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_FALSE(process.paused());
|
||||
|
||||
ASSERT_TRUE(process.initInvoked);
|
||||
ASSERT_TRUE(process.updateInvoked);
|
||||
ASSERT_FALSE(process.succeededInvoked);
|
||||
ASSERT_FALSE(process.failedInvoked);
|
||||
ASSERT_TRUE(process.abortedInvoked);
|
||||
}
|
||||
|
||||
TEST(ProcessAdaptor, Resolved) {
|
||||
bool updated = false;
|
||||
auto lambda = [&updated](std::uint64_t, void *, auto resolve, auto) {
|
||||
ASSERT_FALSE(updated);
|
||||
updated = true;
|
||||
resolve();
|
||||
};
|
||||
|
||||
auto process = entt::ProcessAdaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_TRUE(updated);
|
||||
}
|
||||
|
||||
TEST(ProcessAdaptor, Rejected) {
|
||||
bool updated = false;
|
||||
auto lambda = [&updated](std::uint64_t, void *, auto, auto rejected) {
|
||||
ASSERT_FALSE(updated);
|
||||
updated = true;
|
||||
rejected();
|
||||
};
|
||||
|
||||
auto process = entt::ProcessAdaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_TRUE(process.rejected());
|
||||
ASSERT_TRUE(updated);
|
||||
}
|
||||
|
||||
TEST(ProcessAdaptor, Data) {
|
||||
int value = 0;
|
||||
|
||||
auto lambda = [](std::uint64_t, void *data, auto resolve, auto) {
|
||||
*static_cast<int *>(data) = 42;
|
||||
resolve();
|
||||
};
|
||||
|
||||
auto process = entt::ProcessAdaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
|
||||
process.tick(0, &value);
|
||||
|
||||
ASSERT_TRUE(process.dead());
|
||||
ASSERT_EQ(value, 42);
|
||||
}
|
||||
113
test/entt/process/scheduler.cpp
Normal file
113
test/entt/process/scheduler.cpp
Normal file
@@ -0,0 +1,113 @@
|
||||
#include <functional>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/process/scheduler.hpp>
|
||||
#include <entt/process/process.hpp>
|
||||
|
||||
struct FooProcess: entt::Process<FooProcess, int> {
|
||||
FooProcess(std::function<void()> onUpdate, std::function<void()> onAborted)
|
||||
: onUpdate{onUpdate}, onAborted{onAborted}
|
||||
{}
|
||||
|
||||
void update(delta_type, void *) { onUpdate(); }
|
||||
void aborted() { onAborted(); }
|
||||
|
||||
std::function<void()> onUpdate;
|
||||
std::function<void()> onAborted;
|
||||
};
|
||||
|
||||
struct SucceededProcess: entt::Process<SucceededProcess, int> {
|
||||
void update(delta_type, void *) {
|
||||
ASSERT_FALSE(updated);
|
||||
updated = true;
|
||||
++invoked;
|
||||
succeed();
|
||||
}
|
||||
|
||||
static unsigned int invoked;
|
||||
bool updated = false;
|
||||
};
|
||||
|
||||
unsigned int SucceededProcess::invoked = 0;
|
||||
|
||||
struct FailedProcess: entt::Process<FailedProcess, int> {
|
||||
void update(delta_type, void *) {
|
||||
ASSERT_FALSE(updated);
|
||||
updated = true;
|
||||
fail();
|
||||
}
|
||||
|
||||
bool updated = false;
|
||||
};
|
||||
|
||||
TEST(Scheduler, Functionalities) {
|
||||
entt::Scheduler<int> scheduler{};
|
||||
|
||||
bool updated = false;
|
||||
bool aborted = false;
|
||||
|
||||
ASSERT_EQ(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
ASSERT_TRUE(scheduler.empty());
|
||||
|
||||
scheduler.attach<FooProcess>(
|
||||
[&updated](){ updated = true; },
|
||||
[&aborted](){ aborted = true; }
|
||||
);
|
||||
|
||||
ASSERT_NE(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
ASSERT_FALSE(scheduler.empty());
|
||||
|
||||
scheduler.update(0);
|
||||
scheduler.abort(true);
|
||||
|
||||
ASSERT_TRUE(updated);
|
||||
ASSERT_TRUE(aborted);
|
||||
|
||||
ASSERT_NE(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
ASSERT_FALSE(scheduler.empty());
|
||||
|
||||
scheduler.clear();
|
||||
|
||||
ASSERT_EQ(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
ASSERT_TRUE(scheduler.empty());
|
||||
}
|
||||
|
||||
TEST(Scheduler, Then) {
|
||||
entt::Scheduler<int> scheduler;
|
||||
|
||||
scheduler.attach<SucceededProcess>()
|
||||
.then<SucceededProcess>()
|
||||
.then<FailedProcess>()
|
||||
.then<SucceededProcess>();
|
||||
|
||||
for(auto i = 0; i < 8; ++i) {
|
||||
scheduler.update(0);
|
||||
}
|
||||
|
||||
ASSERT_EQ(SucceededProcess::invoked, 2u);
|
||||
}
|
||||
|
||||
TEST(Scheduler, Functor) {
|
||||
entt::Scheduler<int> scheduler;
|
||||
|
||||
bool firstFunctor = false;
|
||||
bool secondFunctor = false;
|
||||
|
||||
scheduler.attach([&firstFunctor](auto, void *, auto resolve, auto){
|
||||
ASSERT_FALSE(firstFunctor);
|
||||
firstFunctor = true;
|
||||
resolve();
|
||||
}).then([&secondFunctor](auto, void *, auto, auto reject){
|
||||
ASSERT_FALSE(secondFunctor);
|
||||
secondFunctor = true;
|
||||
reject();
|
||||
}).then([](auto...){
|
||||
FAIL();
|
||||
});
|
||||
|
||||
for(auto i = 0; i < 8; ++i) {
|
||||
scheduler.update(0);
|
||||
}
|
||||
|
||||
ASSERT_TRUE(firstFunctor);
|
||||
ASSERT_TRUE(secondFunctor);
|
||||
}
|
||||
85
test/entt/resource/resource.cpp
Normal file
85
test/entt/resource/resource.cpp
Normal file
@@ -0,0 +1,85 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/resource/cache.hpp>
|
||||
|
||||
struct Resource { const int value; };
|
||||
|
||||
struct Loader: entt::ResourceLoader<Loader, Resource> {
|
||||
std::shared_ptr<Resource> load(int value) const {
|
||||
return std::shared_ptr<Resource>(new Resource{ value });
|
||||
}
|
||||
};
|
||||
|
||||
struct BrokenLoader: entt::ResourceLoader<BrokenLoader, Resource> {
|
||||
std::shared_ptr<Resource> load(int) const {
|
||||
return nullptr;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(ResourceCache, Functionalities) {
|
||||
entt::ResourceCache<Resource> cache;
|
||||
|
||||
constexpr auto hs1 = entt::HashedString{"res1"};
|
||||
constexpr auto hs2 = entt::HashedString{"res2"};
|
||||
|
||||
ASSERT_EQ(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_TRUE(cache.empty());
|
||||
ASSERT_FALSE(cache.contains(hs1));
|
||||
ASSERT_FALSE(cache.contains(hs2));
|
||||
|
||||
ASSERT_FALSE(cache.load<BrokenLoader>(hs1, 42));
|
||||
ASSERT_FALSE(cache.reload<BrokenLoader>(hs1, 42));
|
||||
|
||||
ASSERT_EQ(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_TRUE(cache.empty());
|
||||
ASSERT_FALSE(cache.contains(hs1));
|
||||
ASSERT_FALSE(cache.contains(hs2));
|
||||
|
||||
ASSERT_TRUE(cache.load<Loader>(hs1, 42));
|
||||
ASSERT_TRUE(cache.reload<Loader>(hs1, 42));
|
||||
|
||||
ASSERT_NE(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_FALSE(cache.empty());
|
||||
ASSERT_TRUE(cache.contains(hs1));
|
||||
ASSERT_FALSE(cache.contains(hs2));
|
||||
ASSERT_EQ((*cache.handle(hs1)).value, 42);
|
||||
|
||||
ASSERT_TRUE(cache.load<Loader>(hs2, 42));
|
||||
|
||||
ASSERT_NE(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_FALSE(cache.empty());
|
||||
ASSERT_TRUE(cache.contains(hs1));
|
||||
ASSERT_TRUE(cache.contains(hs2));
|
||||
ASSERT_EQ((*cache.handle(hs1)).value, 42);
|
||||
ASSERT_EQ(cache.handle(hs2)->value, 42);
|
||||
|
||||
ASSERT_NO_THROW(cache.discard(hs1));
|
||||
|
||||
ASSERT_FALSE(cache.contains(hs1));
|
||||
ASSERT_TRUE(cache.contains(hs2));
|
||||
ASSERT_EQ(cache.handle(hs2)->value, 42);
|
||||
|
||||
ASSERT_TRUE(cache.load<Loader>(hs1, 42));
|
||||
ASSERT_NO_THROW(cache.clear());
|
||||
|
||||
ASSERT_EQ(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_TRUE(cache.empty());
|
||||
ASSERT_FALSE(cache.contains(hs1));
|
||||
ASSERT_FALSE(cache.contains(hs2));
|
||||
|
||||
ASSERT_TRUE(cache.load<Loader>(hs1, 42));
|
||||
|
||||
ASSERT_NE(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_FALSE(cache.empty());
|
||||
ASSERT_TRUE(cache.handle(hs1));
|
||||
ASSERT_FALSE(cache.handle(hs2));
|
||||
|
||||
ASSERT_TRUE(cache.handle(hs1));
|
||||
ASSERT_EQ(&cache.handle(hs1).get(), &static_cast<const Resource &>(cache.handle(hs1)));
|
||||
ASSERT_NO_THROW(cache.clear());
|
||||
|
||||
ASSERT_EQ(cache.size(), entt::ResourceCache<Resource>::size_type{});
|
||||
ASSERT_TRUE(cache.empty());
|
||||
|
||||
ASSERT_TRUE(cache.temp<Loader>(42));
|
||||
ASSERT_TRUE(cache.empty());
|
||||
}
|
||||
@@ -86,6 +86,21 @@ TEST(SigH, Clear) {
|
||||
ASSERT_TRUE(sigh.empty());
|
||||
}
|
||||
|
||||
TEST(SigH, Swap) {
|
||||
entt::SigH<void(int &)> sigh1;
|
||||
entt::SigH<void(int &)> sigh2;
|
||||
|
||||
sigh1.connect<&S::f>();
|
||||
|
||||
ASSERT_FALSE(sigh1.empty());
|
||||
ASSERT_TRUE(sigh2.empty());
|
||||
|
||||
std::swap(sigh1, sigh2);
|
||||
|
||||
ASSERT_TRUE(sigh1.empty());
|
||||
ASSERT_FALSE(sigh2.empty());
|
||||
}
|
||||
|
||||
TEST(SigH, Functions) {
|
||||
entt::SigH<void(int &)> sigh;
|
||||
int v = 0;
|
||||
|
||||
@@ -91,6 +91,21 @@ TEST(Signal, Clear) {
|
||||
ASSERT_TRUE(signal.empty());
|
||||
}
|
||||
|
||||
TEST(Signal, Swap) {
|
||||
entt::Signal<void(const int &)> sig1;
|
||||
entt::Signal<void(const int &)> sig2;
|
||||
|
||||
sig1.connect<&S::f>();
|
||||
|
||||
ASSERT_FALSE(sig1.empty());
|
||||
ASSERT_TRUE(sig2.empty());
|
||||
|
||||
std::swap(sig1, sig2);
|
||||
|
||||
ASSERT_TRUE(sig1.empty());
|
||||
ASSERT_FALSE(sig2.empty());
|
||||
}
|
||||
|
||||
TEST(Signal, Functions) {
|
||||
entt::Signal<void(const int &)> signal;
|
||||
auto val = S::i + 1;
|
||||
|
||||
426
test/mod/mod.cpp
Normal file
426
test/mod/mod.cpp
Normal file
@@ -0,0 +1,426 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <cassert>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <duktape.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
template<typename Type>
|
||||
struct tag { using type = Type; };
|
||||
|
||||
struct Position {
|
||||
double x;
|
||||
double y;
|
||||
};
|
||||
|
||||
struct Renderable {};
|
||||
|
||||
struct DuktapeRuntime {
|
||||
std::map<duk_uint_t, std::string> components;
|
||||
};
|
||||
|
||||
template<typename Comp>
|
||||
duk_ret_t set(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
registry.accommodate<Comp>(entity);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<>
|
||||
duk_ret_t set<Position>(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
const auto x = duk_require_number(ctx, 2);
|
||||
const auto y = duk_require_number(ctx, 3);
|
||||
registry.accommodate<Position>(entity, x, y);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<>
|
||||
duk_ret_t set<DuktapeRuntime>(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
const auto type = duk_require_uint(ctx, 1);
|
||||
|
||||
duk_dup(ctx, 2);
|
||||
|
||||
if(!registry.has<DuktapeRuntime>(entity)) {
|
||||
registry.assign<DuktapeRuntime>(entity).components[type] = duk_json_encode(ctx, -1);
|
||||
} else {
|
||||
registry.get<DuktapeRuntime>(entity).components[type] = duk_json_encode(ctx, -1);
|
||||
}
|
||||
|
||||
duk_pop(ctx);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
duk_ret_t unset(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
registry.remove<Comp>(entity);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<>
|
||||
duk_ret_t unset<DuktapeRuntime>(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
const auto type = duk_require_uint(ctx, 1);
|
||||
|
||||
auto &components = registry.get<DuktapeRuntime>(entity).components;
|
||||
assert(components.find(type) != components.cend());
|
||||
components.erase(type);
|
||||
|
||||
if(components.empty()) {
|
||||
registry.remove<DuktapeRuntime>(entity);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
duk_ret_t has(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
duk_push_boolean(ctx, registry.has<Comp>(entity));
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<>
|
||||
duk_ret_t has<DuktapeRuntime>(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
duk_push_boolean(ctx, registry.has<DuktapeRuntime>(entity));
|
||||
|
||||
if(registry.has<DuktapeRuntime>(entity)) {
|
||||
const auto type = duk_require_uint(ctx, 1);
|
||||
const auto &components = registry.get<DuktapeRuntime>(entity).components;
|
||||
duk_push_boolean(ctx, components.find(type) != components.cend());
|
||||
} else {
|
||||
duk_push_false(ctx);
|
||||
}
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename Comp>
|
||||
duk_ret_t get(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
assert(registry.has<Comp>(duk_require_uint(ctx, 0)));
|
||||
duk_push_object(ctx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<>
|
||||
duk_ret_t get<Position>(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
const auto &position = registry.get<Position>(entity);
|
||||
|
||||
const auto idx = duk_push_object(ctx);
|
||||
|
||||
duk_push_string(ctx, "x");
|
||||
duk_push_number(ctx, position.x);
|
||||
duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE);
|
||||
|
||||
duk_push_string(ctx, "y");
|
||||
duk_push_number(ctx, position.y);
|
||||
duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE);
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<>
|
||||
duk_ret_t get<DuktapeRuntime>(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
const auto entity = duk_require_uint(ctx, 0);
|
||||
const auto type = duk_require_uint(ctx, 1);
|
||||
|
||||
auto &runtime = registry.get<DuktapeRuntime>(entity);
|
||||
assert(runtime.components.find(type) != runtime.components.cend());
|
||||
|
||||
duk_push_string(ctx, runtime.components[type].c_str());
|
||||
duk_json_decode(ctx, -1);
|
||||
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
class DuktapeRegistry {
|
||||
// I'm pretty sure I won't have more than 99 components in the example
|
||||
static constexpr entt::DefaultRegistry::component_type udef = 100;
|
||||
|
||||
struct Func {
|
||||
using func_type = duk_ret_t(*)(duk_context *, entt::DefaultRegistry &);
|
||||
using test_type = bool(entt::DefaultRegistry:: *)(entt::DefaultRegistry::entity_type) const;
|
||||
|
||||
func_type set;
|
||||
func_type unset;
|
||||
func_type has;
|
||||
func_type get;
|
||||
test_type test;
|
||||
};
|
||||
|
||||
template<typename... Comp>
|
||||
void reg() {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type acc = { (func[registry.component<Comp>()] = {
|
||||
&::set<Comp>,
|
||||
&::unset<Comp>,
|
||||
&::has<Comp>,
|
||||
&::get<Comp>,
|
||||
&entt::DefaultRegistry::has<Comp>
|
||||
}, 0)... };
|
||||
(void)acc;
|
||||
}
|
||||
|
||||
static DuktapeRegistry & instance(duk_context *ctx) {
|
||||
duk_push_this(ctx);
|
||||
|
||||
duk_push_string(ctx, DUK_HIDDEN_SYMBOL("dreg"));
|
||||
duk_get_prop(ctx, -2);
|
||||
auto &dreg = *static_cast<DuktapeRegistry *>(duk_require_pointer(ctx, -1));
|
||||
duk_pop_2(ctx);
|
||||
|
||||
return dreg;
|
||||
}
|
||||
|
||||
template<Func::func_type Func::*Op>
|
||||
static duk_ret_t invoke(duk_context *ctx) {
|
||||
auto &dreg = instance(ctx);
|
||||
auto &func = dreg.func;
|
||||
auto ®istry = dreg.registry;
|
||||
auto type = duk_require_uint(ctx, 1);
|
||||
|
||||
if(type >= udef) {
|
||||
type = registry.component<DuktapeRuntime>();
|
||||
}
|
||||
|
||||
assert(func.find(type) != func.cend());
|
||||
|
||||
return (func[type].*Op)(ctx, registry);
|
||||
}
|
||||
|
||||
public:
|
||||
DuktapeRegistry(entt::DefaultRegistry ®istry)
|
||||
: registry{registry}
|
||||
{
|
||||
reg<Position, Renderable, DuktapeRuntime>();
|
||||
}
|
||||
|
||||
static duk_ret_t identifier(duk_context *ctx) {
|
||||
static auto next = udef;
|
||||
duk_push_uint(ctx, next++);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static duk_ret_t create(duk_context *ctx) {
|
||||
auto &dreg = instance(ctx);
|
||||
duk_push_uint(ctx, dreg.registry.create());
|
||||
return 1;
|
||||
}
|
||||
|
||||
static duk_ret_t set(duk_context *ctx) {
|
||||
return invoke<&Func::set>(ctx);
|
||||
}
|
||||
|
||||
static duk_ret_t unset(duk_context *ctx) {
|
||||
return invoke<&Func::unset>(ctx);
|
||||
}
|
||||
|
||||
static duk_ret_t has(duk_context *ctx) {
|
||||
return invoke<&Func::has>(ctx);
|
||||
}
|
||||
|
||||
static duk_ret_t get(duk_context *ctx) {
|
||||
return invoke<&Func::get>(ctx);
|
||||
}
|
||||
|
||||
static duk_ret_t entities(duk_context *ctx) {
|
||||
const duk_idx_t nargs = duk_get_top(ctx);
|
||||
auto &dreg = instance(ctx);
|
||||
duk_uarridx_t pos = 0;
|
||||
|
||||
duk_push_array(ctx);
|
||||
|
||||
dreg.registry.each([ctx, nargs, &pos, &dreg](auto entity) {
|
||||
auto ®istry = dreg.registry;
|
||||
auto &func = dreg.func;
|
||||
bool match = true;
|
||||
|
||||
for (duk_idx_t arg = 0; match && arg < nargs; arg++) {
|
||||
auto type = duk_require_uint(ctx, arg);
|
||||
|
||||
if(type < udef) {
|
||||
assert(func.find(type) != func.cend());
|
||||
match = (registry.*func[type].test)(entity);
|
||||
} else {
|
||||
const auto ctype = registry.component<DuktapeRuntime>();
|
||||
assert(func.find(ctype) != func.cend());
|
||||
match = (registry.*func[ctype].test)(entity);
|
||||
|
||||
if(match) {
|
||||
auto &components = registry.get<DuktapeRuntime>(entity).components;
|
||||
match = (components.find(type) != components.cend());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if(match) {
|
||||
duk_push_uint(ctx, entity);
|
||||
duk_put_prop_index(ctx, -2, pos++);
|
||||
}
|
||||
});
|
||||
|
||||
return 1;
|
||||
}
|
||||
|
||||
private:
|
||||
std::map<duk_uint_t, Func> func;
|
||||
entt::DefaultRegistry ®istry;
|
||||
};
|
||||
|
||||
const duk_function_list_entry js_DuktapeRegistry_methods[] = {
|
||||
{ "identifier", &DuktapeRegistry::identifier, 0 },
|
||||
{ "create", &DuktapeRegistry::create, 0 },
|
||||
{ "set", &DuktapeRegistry::set, DUK_VARARGS },
|
||||
{ "unset", &DuktapeRegistry::unset, 2 },
|
||||
{ "has", &DuktapeRegistry::has, 2 },
|
||||
{ "get", &DuktapeRegistry::get, 2 },
|
||||
{ "entities", &DuktapeRegistry::entities, DUK_VARARGS },
|
||||
{ nullptr, nullptr, 0 }
|
||||
};
|
||||
|
||||
void exportTypes(duk_context *ctx, entt::DefaultRegistry ®istry) {
|
||||
auto exportType = [](auto *ctx, auto ®istry, auto idx, auto type, const auto *name) {
|
||||
duk_push_string(ctx, name);
|
||||
duk_push_uint(ctx, registry.template component<typename decltype(type)::type>());
|
||||
duk_def_prop(ctx, idx, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_CLEAR_WRITABLE);
|
||||
};
|
||||
|
||||
auto idx = duk_push_object(ctx);
|
||||
|
||||
exportType(ctx, registry, idx, tag<Position>{}, "POSITION");
|
||||
exportType(ctx, registry, idx, tag<Renderable>{}, "RENDERABLE");
|
||||
|
||||
duk_put_global_string(ctx, "Types");
|
||||
}
|
||||
|
||||
void exportDuktapeRegistry(duk_context *ctx, DuktapeRegistry &dreg) {
|
||||
auto idx = duk_push_object(ctx);
|
||||
|
||||
duk_push_string(ctx, DUK_HIDDEN_SYMBOL("dreg"));
|
||||
duk_push_pointer(ctx, &dreg);
|
||||
duk_put_prop(ctx, idx);
|
||||
|
||||
duk_put_function_list(ctx, idx, js_DuktapeRegistry_methods);
|
||||
duk_put_global_string(ctx, "Registry");
|
||||
}
|
||||
|
||||
TEST(Mod, Duktape) {
|
||||
entt::DefaultRegistry registry;
|
||||
DuktapeRegistry dreg{registry};
|
||||
duk_context *ctx = duk_create_heap_default();
|
||||
|
||||
if(!ctx) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
exportTypes(ctx, registry);
|
||||
exportDuktapeRegistry(ctx, dreg);
|
||||
|
||||
const char *s0 = ""
|
||||
"Types[\"PLAYING_CHARACTER\"] = Registry.identifier();"
|
||||
"Types[\"VELOCITY\"] = Registry.identifier();"
|
||||
"";
|
||||
|
||||
if(duk_peval_string(ctx, s0)) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
registry.create(Position{ 0., 0. }, Renderable{});
|
||||
registry.create(Position{ 0., 0. });
|
||||
|
||||
const char *s1 = ""
|
||||
"Registry.entities(Types.POSITION, Types.RENDERABLE).forEach(function(entity) {"
|
||||
"Registry.set(entity, Types.POSITION, 100., 100.);"
|
||||
"});"
|
||||
"var entity = Registry.create();"
|
||||
"Registry.set(entity, Types.POSITION, 100., 100.);"
|
||||
"Registry.set(entity, Types.RENDERABLE);"
|
||||
"";
|
||||
|
||||
if(duk_peval_string(ctx, s1)) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
ASSERT_EQ(registry.view<DuktapeRuntime>().size(), 0u);
|
||||
ASSERT_EQ(registry.view<Position>().size(), 3u);
|
||||
ASSERT_EQ(registry.view<Renderable>().size(), 2u);
|
||||
|
||||
registry.view<Position>().each([®istry](auto entity, const auto &position) {
|
||||
ASSERT_FALSE(registry.has<DuktapeRuntime>(entity));
|
||||
|
||||
if(registry.has<Renderable>(entity)) {
|
||||
ASSERT_EQ(position.x, 100.);
|
||||
ASSERT_EQ(position.y, 100.);
|
||||
} else {
|
||||
ASSERT_EQ(position.x, 0.);
|
||||
ASSERT_EQ(position.y, 0.);
|
||||
}
|
||||
});
|
||||
|
||||
const char *s2 = ""
|
||||
"Registry.entities(Types.POSITION).forEach(function(entity) {"
|
||||
"if(!Registry.has(entity, Types.RENDERABLE)) {"
|
||||
"Registry.set(entity, Types.VELOCITY, { \"dx\": -100., \"dy\": -100. });"
|
||||
"Registry.set(entity, Types.PLAYING_CHARACTER, {});"
|
||||
"}"
|
||||
"});"
|
||||
"";
|
||||
|
||||
if(duk_peval_string(ctx, s2)) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
ASSERT_EQ(registry.view<DuktapeRuntime>().size(), 1u);
|
||||
ASSERT_EQ(registry.view<Position>().size(), 3u);
|
||||
ASSERT_EQ(registry.view<Renderable>().size(), 2u);
|
||||
|
||||
registry.view<DuktapeRuntime>().each([](auto, const DuktapeRuntime &runtime) {
|
||||
ASSERT_EQ(runtime.components.size(), 2u);
|
||||
});
|
||||
|
||||
const char *s3 = ""
|
||||
"Registry.entities(Types.POSITION, Types.RENDERABLE, Types.VELOCITY, Types.PLAYING_CHARACTER).forEach(function(entity) {"
|
||||
"var velocity = Registry.get(entity, Types.VELOCITY);"
|
||||
"Registry.set(entity, Types.POSITION, velocity.dx, velocity.dy)"
|
||||
"});"
|
||||
"";
|
||||
|
||||
if(duk_peval_string(ctx, s3)) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
ASSERT_EQ(registry.view<DuktapeRuntime>().size(), 1u);
|
||||
ASSERT_EQ(registry.view<Position>().size(), 3u);
|
||||
ASSERT_EQ(registry.view<Renderable>().size(), 2u);
|
||||
|
||||
registry.view<Position, Renderable, DuktapeRuntime>().each([](auto, const Position &position, const auto &...) {
|
||||
ASSERT_EQ(position.x, -100.);
|
||||
ASSERT_EQ(position.y, -100.);
|
||||
});
|
||||
|
||||
const char *s4 = ""
|
||||
"Registry.entities(Types.VELOCITY, Types.PLAYING_CHARACTER).forEach(function(entity) {"
|
||||
"Registry.unset(entity, Types.VELOCITY);"
|
||||
"Registry.unset(entity, Types.PLAYING_CHARACTER);"
|
||||
"});"
|
||||
"Registry.entities(Types.POSITION).forEach(function(entity) {"
|
||||
"Registry.unset(entity, Types.POSITION);"
|
||||
"});"
|
||||
"";
|
||||
|
||||
if(duk_peval_string(ctx, s4)) {
|
||||
FAIL();
|
||||
}
|
||||
|
||||
ASSERT_EQ(registry.view<DuktapeRuntime>().size(), 0u);
|
||||
ASSERT_EQ(registry.view<Position>().size(), 0u);
|
||||
ASSERT_EQ(registry.view<Renderable>().size(), 2u);
|
||||
|
||||
duk_destroy_heap(ctx);
|
||||
}
|
||||
182
test/snapshot/snapshot.cpp
Normal file
182
test/snapshot/snapshot.cpp
Normal file
@@ -0,0 +1,182 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <cereal/archives/json.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
struct Position {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct Timer {
|
||||
int duration;
|
||||
int elapsed{0};
|
||||
};
|
||||
|
||||
struct Relationship {
|
||||
entt::DefaultRegistry::entity_type parent;
|
||||
};
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &archive, Position &position) {
|
||||
archive(position.x, position.y);
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &archive, Timer &timer) {
|
||||
archive(timer.duration);
|
||||
}
|
||||
|
||||
template<class Archive>
|
||||
void serialize(Archive &archive, Relationship &relationship) {
|
||||
archive(relationship.parent);
|
||||
}
|
||||
|
||||
TEST(Snapshot, Full) {
|
||||
std::stringstream storage;
|
||||
|
||||
entt::DefaultRegistry source;
|
||||
entt::DefaultRegistry destination;
|
||||
|
||||
auto e0 = source.create();
|
||||
source.assign<Position>(e0, 16.f, 16.f);
|
||||
|
||||
source.destroy(source.create());
|
||||
|
||||
auto e1 = source.create();
|
||||
source.assign<Position>(e1, .8f, .0f);
|
||||
source.assign<Relationship>(e1, e0);
|
||||
|
||||
auto e2 = source.create();
|
||||
|
||||
auto e3 = source.create();
|
||||
source.assign<Timer>(e3, 1000, 100);
|
||||
|
||||
source.destroy(e2);
|
||||
auto v2 = source.current(e2);
|
||||
|
||||
{
|
||||
// output finishes flushing its contents when it goes out of scope
|
||||
cereal::JSONOutputArchive output{storage};
|
||||
source.snapshot().entities(output).destroyed(output)
|
||||
.component<Position, Timer, Relationship>(output);
|
||||
}
|
||||
|
||||
cereal::JSONInputArchive input{storage};
|
||||
destination.restore().entities(input).destroyed(input)
|
||||
.component<Position, Timer, Relationship>(input);
|
||||
|
||||
ASSERT_TRUE(destination.valid(e0));
|
||||
ASSERT_TRUE(destination.has<Position>(e0));
|
||||
ASSERT_EQ(destination.get<Position>(e0).x, 16.f);
|
||||
ASSERT_EQ(destination.get<Position>(e0).y, 16.f);
|
||||
|
||||
ASSERT_TRUE(destination.valid(e1));
|
||||
ASSERT_TRUE(destination.has<Position>(e1));
|
||||
ASSERT_EQ(destination.get<Position>(e1).x, .8f);
|
||||
ASSERT_EQ(destination.get<Position>(e1).y, .0f);
|
||||
ASSERT_TRUE(destination.has<Relationship>(e1));
|
||||
ASSERT_EQ(destination.get<Relationship>(e1).parent, e0);
|
||||
|
||||
ASSERT_FALSE(destination.valid(e2));
|
||||
ASSERT_EQ(destination.current(e2), v2);
|
||||
|
||||
ASSERT_TRUE(destination.valid(e3));
|
||||
ASSERT_TRUE(destination.has<Timer>(e3));
|
||||
ASSERT_EQ(destination.get<Timer>(e3).duration, 1000);
|
||||
ASSERT_EQ(destination.get<Timer>(e3).elapsed, 0);
|
||||
}
|
||||
|
||||
TEST(Snapshot, Continuous) {
|
||||
std::stringstream storage;
|
||||
|
||||
entt::DefaultRegistry source;
|
||||
entt::DefaultRegistry destination;
|
||||
|
||||
std::vector<entt::DefaultRegistry::entity_type> entities;
|
||||
for(auto i = 0; i < 10; ++i) {
|
||||
entities.push_back(source.create());
|
||||
}
|
||||
|
||||
for(auto entity: entities) {
|
||||
source.destroy(entity);
|
||||
}
|
||||
|
||||
auto e0 = source.create();
|
||||
source.assign<Position>(e0, 0.f, 0.f);
|
||||
source.assign<Relationship>(e0, e0);
|
||||
|
||||
auto e1 = source.create();
|
||||
source.assign<Position>(e1, 1.f, 1.f);
|
||||
source.assign<Relationship>(e1, e0);
|
||||
|
||||
auto e2 = source.create();
|
||||
source.assign<Position>(e2, .2f, .2f);
|
||||
source.assign<Relationship>(e2, e0);
|
||||
|
||||
auto e3 = source.create();
|
||||
source.assign<Timer>(e3, 1000, 1000);
|
||||
source.assign<Relationship>(e3, e2);
|
||||
|
||||
{
|
||||
// output finishes flushing its contents when it goes out of scope
|
||||
cereal::JSONOutputArchive output{storage};
|
||||
source.snapshot().entities(output).component<Position, Relationship, Timer>(output);
|
||||
}
|
||||
|
||||
cereal::JSONInputArchive input{storage};
|
||||
entt::ContinuousLoader<entt::DefaultRegistry::entity_type> loader{destination};
|
||||
loader.entities(input)
|
||||
.component<Position>(input)
|
||||
.component<Relationship>(input, &Relationship::parent)
|
||||
.component<Timer>(input);
|
||||
|
||||
ASSERT_FALSE(destination.valid(e0));
|
||||
ASSERT_TRUE(loader.has(e0));
|
||||
|
||||
auto l0 = loader.map(e0);
|
||||
|
||||
ASSERT_TRUE(destination.valid(l0));
|
||||
ASSERT_TRUE(destination.has<Position>(l0));
|
||||
ASSERT_EQ(destination.get<Position>(l0).x, 0.f);
|
||||
ASSERT_EQ(destination.get<Position>(l0).y, 0.f);
|
||||
ASSERT_TRUE(destination.has<Relationship>(l0));
|
||||
ASSERT_EQ(destination.get<Relationship>(l0).parent, l0);
|
||||
|
||||
ASSERT_FALSE(destination.valid(e1));
|
||||
ASSERT_TRUE(loader.has(e1));
|
||||
|
||||
auto l1 = loader.map(e1);
|
||||
|
||||
ASSERT_TRUE(destination.valid(l1));
|
||||
ASSERT_TRUE(destination.has<Position>(l1));
|
||||
ASSERT_EQ(destination.get<Position>(l1).x, 1.f);
|
||||
ASSERT_EQ(destination.get<Position>(l1).y, 1.f);
|
||||
ASSERT_TRUE(destination.has<Relationship>(l1));
|
||||
ASSERT_EQ(destination.get<Relationship>(l1).parent, l0);
|
||||
|
||||
ASSERT_FALSE(destination.valid(e2));
|
||||
ASSERT_TRUE(loader.has(e2));
|
||||
|
||||
auto l2 = loader.map(e2);
|
||||
|
||||
ASSERT_TRUE(destination.valid(l2));
|
||||
ASSERT_TRUE(destination.has<Position>(l2));
|
||||
ASSERT_EQ(destination.get<Position>(l2).x, .2f);
|
||||
ASSERT_EQ(destination.get<Position>(l2).y, .2f);
|
||||
ASSERT_TRUE(destination.has<Relationship>(l2));
|
||||
ASSERT_EQ(destination.get<Relationship>(l2).parent, l0);
|
||||
|
||||
ASSERT_FALSE(destination.valid(e3));
|
||||
ASSERT_TRUE(loader.has(e3));
|
||||
|
||||
auto l3 = loader.map(e3);
|
||||
|
||||
ASSERT_TRUE(destination.valid(l3));
|
||||
ASSERT_TRUE(destination.has<Timer>(l3));
|
||||
ASSERT_EQ(destination.get<Timer>(l3).duration, 1000);
|
||||
ASSERT_EQ(destination.get<Timer>(l3).elapsed, 0);
|
||||
ASSERT_TRUE(destination.has<Relationship>(l3));
|
||||
ASSERT_EQ(destination.get<Relationship>(l3).parent, l2);
|
||||
}
|
||||
Reference in New Issue
Block a user