Compare commits
326 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
85152bac34 | ||
|
|
c832888d56 | ||
|
|
cac1b87b44 | ||
|
|
30f71b2ed6 | ||
|
|
9f8e9a1916 | ||
|
|
8519fe57ae | ||
|
|
1442a3853c | ||
|
|
2658ddf868 | ||
|
|
f95eecfcc6 | ||
|
|
1e598cca31 | ||
|
|
f3eaeb96f0 | ||
|
|
91fac8049c | ||
|
|
b1325a7514 | ||
|
|
150b83b4f3 | ||
|
|
3a3a5dc071 | ||
|
|
9b76a5bd66 | ||
|
|
850a1f9155 | ||
|
|
ef6c8a38d0 | ||
|
|
e228cb6648 | ||
|
|
3a53cac607 | ||
|
|
df4bd57c82 | ||
|
|
de737fc72d | ||
|
|
ccea7a5783 | ||
|
|
5b84e1528a | ||
|
|
7382addfe4 | ||
|
|
b8dd6822ac | ||
|
|
8c47b85b9b | ||
|
|
4d2666a0e3 | ||
|
|
fc18eaa292 | ||
|
|
22199cdd87 | ||
|
|
76820f25c6 | ||
|
|
bac4984496 | ||
|
|
1414c5feac | ||
|
|
c344f63154 | ||
|
|
77ea28bef2 | ||
|
|
bf63b15e07 | ||
|
|
ce05504278 | ||
|
|
95b90974a2 | ||
|
|
1adbef7352 | ||
|
|
3109928dcd | ||
|
|
24dd63789c | ||
|
|
27225e6b5e | ||
|
|
2994978fdb | ||
|
|
ffd864dc72 | ||
|
|
53a2c88d4b | ||
|
|
444ae73ffb | ||
|
|
9f5f4b9c91 | ||
|
|
ffd2af0257 | ||
|
|
b983845745 | ||
|
|
cfcc6e1029 | ||
|
|
6fea4377b2 | ||
|
|
e01230e425 | ||
|
|
f66db81edd | ||
|
|
a47471d0ae | ||
|
|
06b5a91cd3 | ||
|
|
8aacf3497e | ||
|
|
7fc5e304e3 | ||
|
|
65536febd5 | ||
|
|
60f7910840 | ||
|
|
829c3c612e | ||
|
|
355c7b59aa | ||
|
|
6508cdc823 | ||
|
|
9ef9f603a3 | ||
|
|
e76f881c67 | ||
|
|
4292d2ce86 | ||
|
|
ae927c5600 | ||
|
|
57e2eed771 | ||
|
|
ca807b892f | ||
|
|
9bc015d10e | ||
|
|
59b204c3bc | ||
|
|
4ac2472a62 | ||
|
|
054b9c2402 | ||
|
|
96f267cbfd | ||
|
|
2018163107 | ||
|
|
253b5b1fb7 | ||
|
|
0be2f1b295 | ||
|
|
a8838fc611 | ||
|
|
d02636e370 | ||
|
|
f3c1b09188 | ||
|
|
7bac69f360 | ||
|
|
5a3fdd225e | ||
|
|
4660f58653 | ||
|
|
5fabca3616 | ||
|
|
edb74dae87 | ||
|
|
31b833b46a | ||
|
|
21bc1c51ba | ||
|
|
4f6cab9a45 | ||
|
|
33626fa47c | ||
|
|
9fcc1e17f2 | ||
|
|
a5f48f8af8 | ||
|
|
3dfa526ec6 | ||
|
|
dc28066017 | ||
|
|
2f2edfbde8 | ||
|
|
3beac98499 | ||
|
|
f70ce9dd26 | ||
|
|
e301b9d399 | ||
|
|
42f92ecd7a | ||
|
|
27eb0d04b9 | ||
|
|
112d13c127 | ||
|
|
16afc9d0f8 | ||
|
|
5b4889b46b | ||
|
|
f7eed0e2af | ||
|
|
cce287e8a6 | ||
|
|
9a001ebacc | ||
|
|
79ab4c02bb | ||
|
|
b6de70a3a6 | ||
|
|
15cafe2d78 | ||
|
|
90197abc5c | ||
|
|
2147436a2a | ||
|
|
8ef0c66966 | ||
|
|
2779ad6c6f | ||
|
|
f30fe3c746 | ||
|
|
f71496693f | ||
|
|
e4cd5dbf1f | ||
|
|
ffd5defcb6 | ||
|
|
0b81421124 | ||
|
|
0f73b9e07f | ||
|
|
d3078699d6 | ||
|
|
cc39f9f891 | ||
|
|
70608df843 | ||
|
|
e7f2c6b5ba | ||
|
|
420fc9aa24 | ||
|
|
200012fd41 | ||
|
|
e3c858278b | ||
|
|
1ec7c3afa4 | ||
|
|
6ab0b60f12 | ||
|
|
5ec38b44b0 | ||
|
|
39c2294d43 | ||
|
|
3b92481133 | ||
|
|
6b8d24d2f5 | ||
|
|
62d6ff61ab | ||
|
|
f7c056145a | ||
|
|
451f54b036 | ||
|
|
ef378de883 | ||
|
|
2f2b63da4a | ||
|
|
01d8f2c7e6 | ||
|
|
330d553ffb | ||
|
|
f1d0895eb1 | ||
|
|
4eaf6415b7 | ||
|
|
f0650bf64f | ||
|
|
44856c6207 | ||
|
|
dcbf6e43c7 | ||
|
|
0d22da672d | ||
|
|
a62471b9b8 | ||
|
|
f673f2c5bc | ||
|
|
a7caae5c17 | ||
|
|
d0deefd0d7 | ||
|
|
9810da6982 | ||
|
|
cb93a3bee3 | ||
|
|
aeacc3e39d | ||
|
|
e1c861cc8a | ||
|
|
37ee05a576 | ||
|
|
07a2bd817d | ||
|
|
78c171e0c2 | ||
|
|
1ae436cb08 | ||
|
|
b0d3a84d1e | ||
|
|
1acd9ca981 | ||
|
|
72467b6b9c | ||
|
|
862c85387c | ||
|
|
eeeca3e21c | ||
|
|
4ee4af7fd4 | ||
|
|
d131cc1871 | ||
|
|
bd21ee318d | ||
|
|
02e7fc0bca | ||
|
|
51ba46970e | ||
|
|
9d65b44ab7 | ||
|
|
350d51a92d | ||
|
|
2a8202caa7 | ||
|
|
9e4591554f | ||
|
|
e98ecfd1f9 | ||
|
|
1cbf5c4359 | ||
|
|
0446faeb6f | ||
|
|
0417ea23c3 | ||
|
|
de9886e011 | ||
|
|
770e57c361 | ||
|
|
d80a00701d | ||
|
|
62d3cd8795 | ||
|
|
ad9ec22fd1 | ||
|
|
293985ec10 | ||
|
|
105598ac8e | ||
|
|
ff93f2220a | ||
|
|
33fddcb289 | ||
|
|
6a09bf2c54 | ||
|
|
2913f5bbbe | ||
|
|
671bdb09b8 | ||
|
|
353a0d14a4 | ||
|
|
e0fd83211b | ||
|
|
2c94d9cc37 | ||
|
|
f7dec412ec | ||
|
|
f7c756d215 | ||
|
|
617635a989 | ||
|
|
ea3b2c4713 | ||
|
|
15455a7e68 | ||
|
|
ad5cedc08c | ||
|
|
328f0c6da8 | ||
|
|
8ef818f581 | ||
|
|
48dace64e4 | ||
|
|
ab5837c7c4 | ||
|
|
9fbdbc1844 | ||
|
|
54129cecc2 | ||
|
|
69c514d1a4 | ||
|
|
7047d12eba | ||
|
|
8ab61637f0 | ||
|
|
7e0bd92593 | ||
|
|
99d9a9f44e | ||
|
|
9c55111e14 | ||
|
|
b1d6ba57ad | ||
|
|
47e9330646 | ||
|
|
e16a3d503c | ||
|
|
dc9c93b347 | ||
|
|
faceff620a | ||
|
|
a5a6a58556 | ||
|
|
45fdab27c9 | ||
|
|
8b360479f4 | ||
|
|
b170f2f69a | ||
|
|
7f1abab9ae | ||
|
|
2ec7bc878e | ||
|
|
e97d9bb095 | ||
|
|
2f3b02e870 | ||
|
|
11b7a45c32 | ||
|
|
fb10d2f9c2 | ||
|
|
60bb7f66cf | ||
|
|
ad5143169a | ||
|
|
d68c90a429 | ||
|
|
8a51549395 | ||
|
|
9f997fe1fa | ||
|
|
00f1f6d86b | ||
|
|
be72728000 | ||
|
|
f558126854 | ||
|
|
08ed1500ac | ||
|
|
f0926f1cc7 | ||
|
|
fe3f6aa22b | ||
|
|
1325f75e81 | ||
|
|
4b1d3a7b6f | ||
|
|
404afcdfd6 | ||
|
|
99793180f1 | ||
|
|
c91f9beddb | ||
|
|
c9c5ccaa6d | ||
|
|
da42de7ac8 | ||
|
|
3c9a6ad218 | ||
|
|
f0389d6b47 | ||
|
|
76eab21738 | ||
|
|
3b32c57d94 | ||
|
|
d119032cb9 | ||
|
|
397a04b49c | ||
|
|
d32c8e9457 | ||
|
|
ebb2974ca7 | ||
|
|
cbb1131a5d | ||
|
|
d9de7fe3b0 | ||
|
|
24ba692884 | ||
|
|
960bbbde29 | ||
|
|
eb79b79c35 | ||
|
|
8212ed6d87 | ||
|
|
c639cb5285 | ||
|
|
a88fd1c669 | ||
|
|
b053f23d15 | ||
|
|
9d32a89491 | ||
|
|
cb9a147fff | ||
|
|
c9fddedbf1 | ||
|
|
530bbbe4c5 | ||
|
|
73e5a9f45b | ||
|
|
72d4e947b6 | ||
|
|
41750c3639 | ||
|
|
52b36f38a0 | ||
|
|
58ceb454dd | ||
|
|
9e5a2db4c5 | ||
|
|
781f283c89 | ||
|
|
f1cd085727 | ||
|
|
3aba855bd6 | ||
|
|
7e3abb27a4 | ||
|
|
f0c11daa37 | ||
|
|
167c174f4d | ||
|
|
0173060eaf | ||
|
|
4edd25ff5d | ||
|
|
2a4e098645 | ||
|
|
7c5019c30f | ||
|
|
c13fe3feb6 | ||
|
|
0ced60e712 | ||
|
|
d0764d5854 | ||
|
|
f7905e3bc2 | ||
|
|
3e770792a4 | ||
|
|
8f9934a7f4 | ||
|
|
b921e7d595 | ||
|
|
a9cb2d2efd | ||
|
|
fe519b3acf | ||
|
|
30cc4a1668 | ||
|
|
a71e4d8970 | ||
|
|
66e7141415 | ||
|
|
6f0cdc864f | ||
|
|
4f9deaaf09 | ||
|
|
380745a814 | ||
|
|
ce4e335412 | ||
|
|
274b8181d1 | ||
|
|
767bdcc2ce | ||
|
|
c00cde8bcc | ||
|
|
5859a18b3a | ||
|
|
375124642d | ||
|
|
af9e6dea68 | ||
|
|
78f2267b06 | ||
|
|
0a34f22240 | ||
|
|
b0651fcaed | ||
|
|
d59faa2a08 | ||
|
|
6a3bc37dc1 | ||
|
|
2ea7796b26 | ||
|
|
cf0057bac4 | ||
|
|
fafb325a88 | ||
|
|
6b69b4671f | ||
|
|
eaa95ee840 | ||
|
|
a86ba1fdf6 | ||
|
|
09e0d2d15b | ||
|
|
6d9fa6418d | ||
|
|
6010f5b7a6 | ||
|
|
9c164e1cea | ||
|
|
880fba8dce | ||
|
|
17d57ee49a | ||
|
|
234949a627 | ||
|
|
ff34671526 | ||
|
|
6e22983d98 | ||
|
|
18254c30ec | ||
|
|
922c955239 | ||
|
|
618a325057 | ||
|
|
d81cb7f866 | ||
|
|
f71a4d5381 | ||
|
|
282cb60a66 | ||
|
|
58dbac4422 | ||
|
|
53e228465b |
4
.gitignore
vendored
4
.gitignore
vendored
@@ -1 +1,5 @@
|
||||
*.user
|
||||
conan/test_package/build
|
||||
|
||||
# IDEs
|
||||
.idea
|
||||
|
||||
67
.travis.yml
67
.travis.yml
@@ -2,15 +2,40 @@ language: cpp
|
||||
dist: trusty
|
||||
sudo: false
|
||||
|
||||
env:
|
||||
global:
|
||||
- CONAN_USERNAME="skypjack"
|
||||
- CONAN_PACKAGE_NAME="entt"
|
||||
- CONAN_HEADER_ONLY="True"
|
||||
- NON_CONAN_DEPLOYMENT="True"
|
||||
|
||||
conan-buildsteps: &conan-buildsteps
|
||||
before_install:
|
||||
# use this step if you desire to manipulate CONAN variables programmatically
|
||||
- NON_CONAN_DEPLOYMENT="False"
|
||||
install:
|
||||
- chmod +x ./conan/ci/install.sh
|
||||
- ./conan/ci/install.sh
|
||||
script:
|
||||
- chmod +x ./conan/ci/build.sh
|
||||
- ./conan/ci/build.sh
|
||||
# the following are dummies to overwrite default build steps
|
||||
before_script:
|
||||
- true
|
||||
after_success:
|
||||
- true
|
||||
if: tag IS present
|
||||
conan-linux: &conan-linux
|
||||
os: linux
|
||||
sudo: required
|
||||
language: python
|
||||
python: "3.6"
|
||||
services:
|
||||
- docker
|
||||
<<: *conan-buildsteps
|
||||
|
||||
matrix:
|
||||
include:
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test']
|
||||
packages: ['g++-6']
|
||||
env: COMPILER=g++-6
|
||||
- os: linux
|
||||
compiler: gcc
|
||||
addons:
|
||||
@@ -22,22 +47,11 @@ matrix:
|
||||
compiler: clang
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-4.0']
|
||||
packages: ['clang-4.0', 'libstdc++-4.9-dev']
|
||||
env: COMPILER=clang++-4.0
|
||||
- os: linux
|
||||
compiler: clang
|
||||
addons:
|
||||
apt:
|
||||
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-5.0']
|
||||
packages: ['clang-5.0', 'libstdc++-4.9-dev']
|
||||
env: COMPILER=clang++-5.0
|
||||
sources: ['ubuntu-toolchain-r-test', 'llvm-toolchain-trusty-6.0']
|
||||
packages: ['clang-6.0', 'g++-7']
|
||||
env: COMPILER=clang++-6.0
|
||||
- os: osx
|
||||
osx_image: xcode8.3
|
||||
compiler: clang
|
||||
env: COMPILER=clang++
|
||||
- os: osx
|
||||
osx_image: xcode9.4
|
||||
osx_image: xcode10
|
||||
compiler: clang
|
||||
env: COMPILER=clang++
|
||||
- os: linux
|
||||
@@ -53,6 +67,9 @@ matrix:
|
||||
- pip install --user cpp-coveralls
|
||||
after_success:
|
||||
- coveralls --gcov gcov-7 --gcov-options '\-lp' --root ${TRAVIS_BUILD_DIR} --build-root ${TRAVIS_BUILD_DIR}/build --extension cpp --extension hpp --exclude deps --include src
|
||||
# Conan testing and uploading
|
||||
- <<: *conan-linux
|
||||
env: CONAN_GCC_VERSIONS=8 CONAN_DOCKER_IMAGE=conanio/gcc8
|
||||
|
||||
notifications:
|
||||
email:
|
||||
@@ -69,12 +86,12 @@ install:
|
||||
|
||||
script:
|
||||
- mkdir -p build && cd build
|
||||
- cmake .. && make -j4
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 make test
|
||||
- cmake -DBUILD_TESTING=ON -DCMAKE_BUILD_TYPE=Release .. && make -j4
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 ctest -j4
|
||||
|
||||
deploy:
|
||||
provider: script
|
||||
script: scripts/update_packages.sh $TRAVIS_TAG
|
||||
on:
|
||||
tags: true
|
||||
condition: “$TRAVIS_BRANCH” = “$TRAVIS_TAG”
|
||||
condition: “$NON_CONAN_DEPLOYMENT = “True”
|
||||
|
||||
38
AUTHORS
38
AUTHORS
@@ -1,11 +1,37 @@
|
||||
# Author
|
||||
|
||||
Michele Caini aka skypjack
|
||||
skypjack
|
||||
|
||||
# Contributors
|
||||
|
||||
Paolo Monteverde aka morbo84
|
||||
David Nerjes aka DavidHamburg
|
||||
Indi Kernick aka Kerndog73
|
||||
Malte Müller-Rowold aka m-waka
|
||||
Richard Caseres aka richardbmx
|
||||
BenediktConze
|
||||
bjadamson
|
||||
ceeac
|
||||
ColinH
|
||||
corystegel
|
||||
Croydon
|
||||
dbacchet
|
||||
dBagrat
|
||||
djarek
|
||||
DonKult
|
||||
drglove
|
||||
eugeneko
|
||||
gale83
|
||||
ghost
|
||||
Green-Sky
|
||||
Milerius
|
||||
morbo84
|
||||
m-waka
|
||||
Kerndog73
|
||||
Paolo-Oliverio
|
||||
pgruenbacher
|
||||
prowolf
|
||||
vblanco20-1
|
||||
willtunnels
|
||||
WizardIke
|
||||
w1th0utnam3
|
||||
|
||||
# Special thanks for patrons
|
||||
|
||||
Arn
|
||||
tpisto
|
||||
|
||||
@@ -16,7 +16,7 @@ endif()
|
||||
# Project configuration
|
||||
#
|
||||
|
||||
project(EnTT VERSION 2.7.3)
|
||||
project(EnTT VERSION 3.0.0)
|
||||
|
||||
include(GNUInstallDirs)
|
||||
|
||||
@@ -31,7 +31,7 @@ set(PROJECT_AUTHOR_EMAIL "michele.caini@gmail.com")
|
||||
|
||||
message("*")
|
||||
message("* ${PROJECT_NAME} v${PROJECT_VERSION} (${CMAKE_BUILD_TYPE})")
|
||||
message("* Copyright (c) 2017-2018 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
|
||||
message("* Copyright (c) 2017-2019 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
|
||||
message("*")
|
||||
|
||||
option(USE_LIBCPP "Use libc++ by adding -stdlib=libc++ flag if availbale." ON)
|
||||
@@ -52,7 +52,7 @@ if(NOT MSVC AND USE_LIBCPP)
|
||||
|
||||
check_cxx_source_compiles("
|
||||
#include<type_traits>
|
||||
int main() { return std::is_same<int, int>::value ? 0 : 1; }
|
||||
int main() { return std::is_same_v<int, int> ? 0 : 1; }
|
||||
" HAS_LIBCPP)
|
||||
|
||||
if(NOT HAS_LIBCPP)
|
||||
@@ -68,6 +68,8 @@ endif()
|
||||
|
||||
add_library(EnTT INTERFACE)
|
||||
|
||||
configure_file(${EnTT_SOURCE_DIR}/cmake/in/version.h.in ${EnTT_SOURCE_DIR}/src/entt/config/version.h @ONLY)
|
||||
|
||||
target_include_directories(
|
||||
EnTT INTERFACE
|
||||
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src>
|
||||
@@ -100,7 +102,7 @@ if(HAS_LIBCPP)
|
||||
target_compile_options(EnTT BEFORE INTERFACE -stdlib=libc++)
|
||||
endif()
|
||||
|
||||
target_compile_features(EnTT INTERFACE cxx_std_14)
|
||||
target_compile_features(EnTT INTERFACE cxx_std_17)
|
||||
|
||||
#
|
||||
# Install EnTT
|
||||
@@ -140,7 +142,6 @@ configure_package_config_file(
|
||||
${CUSTOM_INSTALL_CONFIGDIR}/EnTTConfig.cmake
|
||||
INSTALL_DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
|
||||
PATH_VARS CMAKE_INSTALL_INCLUDEDIR
|
||||
NO_CHECK_REQUIRED_COMPONENTS_MACRO
|
||||
)
|
||||
|
||||
write_basic_package_version_file(
|
||||
@@ -162,7 +163,7 @@ export(PACKAGE EnTT)
|
||||
# Tests
|
||||
#
|
||||
|
||||
option(BUILD_TESTING "Enable testing with ctest." ON)
|
||||
option(BUILD_TESTING "Enable testing with ctest." OFF)
|
||||
|
||||
if(BUILD_TESTING)
|
||||
set(THREADS_PREFER_PTHREAD_FLAG ON)
|
||||
@@ -180,10 +181,13 @@ if(BUILD_TESTING)
|
||||
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
|
||||
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
|
||||
add_subdirectory(${GOOGLETEST_DEPS_DIR}/src ${GOOGLETEST_DEPS_DIR}/build)
|
||||
target_compile_features(gmock_main PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_FEATURES>)
|
||||
target_compile_features(gmock PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_FEATURES>)
|
||||
add_library(GTest::Main ALIAS gtest_main)
|
||||
endif()
|
||||
|
||||
option(BUILD_BENCHMARK "Build benchmark." OFF)
|
||||
option(BUILD_LIB "Build lib example." OFF)
|
||||
option(BUILD_MOD "Build mod example." OFF)
|
||||
option(BUILD_SNAPSHOT "Build snapshot example." OFF)
|
||||
|
||||
|
||||
2
LICENSE
2
LICENSE
@@ -1,6 +1,6 @@
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2017-2018 Michele Caini
|
||||
Copyright (c) 2017-2019 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
|
||||
|
||||
251
README.md
251
README.md
@@ -3,11 +3,34 @@
|
||||
<!--
|
||||
@cond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
[](http://badge.fury.io/gh/skypjack%2Fentt)
|
||||
[](https://github.com/skypjack/entt)
|
||||
[](https://travis-ci.org/skypjack/entt)
|
||||
[](https://ci.appveyor.com/project/skypjack/entt)
|
||||
[](https://coveralls.io/github/skypjack/entt?branch=master)
|
||||
[](https://gitter.im/skypjack/entt)
|
||||
[](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2HF9FESD5LJY&lc=IT&item_name=Michele%20Caini¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted)
|
||||
[](https://www.paypal.me/skypjack)
|
||||
|
||||
`EnTT` is a header-only, tiny and easy to use library for game programming and
|
||||
much more written in **modern C++**, mainly known for its innovative
|
||||
**entity-component-system (ECS)** model.<br/>
|
||||
[Among others](https://github.com/skypjack/entt/wiki/EnTT-in-Action), it's used
|
||||
in [**Minecraft**](https://minecraft.net/en-us/attribution/) by Mojang, the
|
||||
[**ArcGIS Runtime SDKs**](https://developers.arcgis.com/arcgis-runtime/) by Esri
|
||||
and [**The Forge**](https://github.com/ConfettiFX/The-Forge) by Confetti. Read
|
||||
on to find out what it can offer you.
|
||||
|
||||
---
|
||||
|
||||
Do you want to **keep up with changes** or do you have a **question** that
|
||||
doesn't require you to open an issue?<br/>
|
||||
Join the [gitter channel](https://gitter.im/skypjack/entt) and meet other users
|
||||
like you. The more we are, the better for everyone.
|
||||
|
||||
If you use `EnTT` and you want to say thanks or support the project, please
|
||||
**consider becoming a patron**:
|
||||
|
||||
[](https://www.patreon.com/bePatron?c=1772573)
|
||||
|
||||
# Table of Contents
|
||||
|
||||
@@ -25,6 +48,7 @@
|
||||
* [Contributors](#contributors)
|
||||
* [License](#license)
|
||||
* [Support](#support)
|
||||
* [Patreon](#patreon)
|
||||
* [Donation](#donation)
|
||||
* [Hire me](#hire-me)
|
||||
<!--
|
||||
@@ -33,9 +57,6 @@
|
||||
|
||||
# Introduction
|
||||
|
||||
`EnTT` is a header-only, tiny and easy to use entity-component system (and much
|
||||
more) written in modern C++ and even
|
||||
[used by Mojang in Minecraft](https://minecraft.net/en-us/attribution/).<br/>
|
||||
The entity-component-system (also known as _ECS_) is an architectural pattern
|
||||
used mostly in game development. For further details:
|
||||
|
||||
@@ -43,38 +64,40 @@ used mostly in game development. For further details:
|
||||
* [Evolve Your Hierarchy](http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/)
|
||||
* [ECS on Wikipedia](https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system)
|
||||
|
||||
A long time ago, the sole entity-component system was part of the project. After
|
||||
a while the codebase has grown and more and more classes have become part of the
|
||||
repository.<br/>
|
||||
This project started off as a pure entity-component system. Over time the
|
||||
codebase has grown as more and more classes and functionalities were added.<br/>
|
||||
Here is a brief, yet incomplete list of what it offers today:
|
||||
|
||||
* Statically generated integer identifiers for types (assigned either at
|
||||
* Statically generated integer **identifiers for types** (assigned either at
|
||||
compile-time or at runtime).
|
||||
* A constexpr utility for human readable resource identifiers.
|
||||
* A minimal configuration system built on top of the monostate pattern.
|
||||
* An incredibly fast entity-component system based on sparse sets, with its own
|
||||
views and a _pay for what you use_ policy to adjust performance and memory
|
||||
usage according to users' requirements.
|
||||
* A lot of facilities built on top of the entity-component system to help
|
||||
developers and avoid reinventing the wheel (ie dependencies, snapshot, actor
|
||||
class for those who aren't confident with the architecture and so on).
|
||||
* The smallest and most basic implementation of a service locator ever seen.
|
||||
* A cooperative scheduler for processes of any type.
|
||||
* All what is needed for resource management (cache, loaders, handles).
|
||||
* Delegates, signal handlers (with built-in support for collectors) and a tiny
|
||||
event dispatcher.
|
||||
* A general purpose event emitter, that is a CRTP idiom based class template.
|
||||
* An event dispatcher for immediate and delayed events to integrate in loops.
|
||||
* ...
|
||||
* Any other business.
|
||||
* A `constexpr` utility for **human readable resource identifiers**.
|
||||
* A minimal **configuration system** built using the monostate pattern.
|
||||
* An incredibly fast **entity-component system** based on sparse sets, with its
|
||||
own _pay for what you use_ policy to adjust performance and memory usage
|
||||
according to the users' requirements.
|
||||
* Views and groups to iterate entities and components and allow different access
|
||||
patterns, from **perfect SoA** to fully random.
|
||||
* A lot of **facilities** built on top of the entity-component system to support
|
||||
the users and avoid reinventing the wheel (dependencies, snapshot, actor class
|
||||
for those who aren't confident with the architecture and so on).
|
||||
* The smallest and most basic implementation of a **service locator** ever seen.
|
||||
* A built-in, non-intrusive and macro-free **runtime reflection system**.
|
||||
* A **cooperative scheduler** for processes of any type.
|
||||
* All that is needed for **resource management** (cache, loaders, handles).
|
||||
* **Delegates**, **signal handlers** (with built-in support for collectors) and
|
||||
a tiny **event dispatcher** for immediate and delayed events to integrate in
|
||||
loops.
|
||||
* A general purpose **event emitter** as a CRTP idiom based class template.
|
||||
* And **much more**! Check out the
|
||||
[**wiki**](https://github.com/skypjack/entt/wiki).
|
||||
|
||||
Consider it a work in progress. The whole API is also fully documented in-code
|
||||
for those who are brave enough to read it.
|
||||
Consider this list a work in progress as well as the project. The whole API is
|
||||
fully documented in-code for those who are brave enough to read it.
|
||||
|
||||
Currently, `EnTT` is tested on Linux, Microsoft Windows and OS X. It has proven
|
||||
Currently, `EnTT` is tested on Linux, Microsoft Windows and OSX. It has proven
|
||||
to work also on both Android and iOS.<br/>
|
||||
Most likely it will not be problematic on other systems as well, but has not
|
||||
been sufficiently tested so far.
|
||||
Most likely it won't be problematic on other systems as well, but it hasn't been
|
||||
sufficiently tested so far.
|
||||
|
||||
## Code Example
|
||||
|
||||
@@ -82,50 +105,50 @@ been sufficiently tested so far.
|
||||
#include <entt/entt.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
struct Position {
|
||||
struct position {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct Velocity {
|
||||
struct velocity {
|
||||
float dx;
|
||||
float dy;
|
||||
};
|
||||
|
||||
void update(entt::DefaultRegistry ®istry) {
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
void update(entt::registry ®istry) {
|
||||
auto view = registry.view<position, velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
// gets only the components that are going to be used ...
|
||||
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &vel = view.get<velocity>(entity);
|
||||
|
||||
velocity.dx = 0.;
|
||||
velocity.dy = 0.;
|
||||
vel.dx = 0.;
|
||||
vel.dy = 0.;
|
||||
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
void update(std::uint64_t dt, entt::DefaultRegistry ®istry) {
|
||||
registry.view<Position, Velocity>().each([dt](auto entity, auto &position, auto &velocity) {
|
||||
void update(std::uint64_t dt, entt::registry ®istry) {
|
||||
registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
|
||||
// gets all the components of the view at once ...
|
||||
|
||||
position.x += velocity.dx * dt;
|
||||
position.y += velocity.dy * dt;
|
||||
pos.x += vel.dx * dt;
|
||||
pos.y += vel.dy * dt;
|
||||
|
||||
// ...
|
||||
});
|
||||
}
|
||||
|
||||
int main() {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
std::uint64_t dt = 16;
|
||||
|
||||
for(auto i = 0; i < 10; ++i) {
|
||||
auto entity = registry.create();
|
||||
registry.assign<Position>(entity, i * 1.f, i * 1.f);
|
||||
if(i % 2 == 0) { registry.assign<Velocity>(entity, i * .1f, i * .1f); }
|
||||
registry.assign<position>(entity, i * 1.f, i * 1.f);
|
||||
if(i % 2 == 0) { registry.assign<velocity>(entity, i * .1f, i * .1f); }
|
||||
}
|
||||
|
||||
update(dt, registry);
|
||||
@@ -137,11 +160,10 @@ int main() {
|
||||
|
||||
## Motivation
|
||||
|
||||
I started working on `EnTT` because of the wrong reason: my goal was to design
|
||||
an entity-component system that beated another well known open source solution
|
||||
in terms of performance and used (possibly) less memory in the average
|
||||
case.<br/>
|
||||
In the end, I did it, but it wasn't much satisfying. Actually it wasn't
|
||||
I started developing `EnTT` for the _wrong_ reason: my goal was to design an
|
||||
entity-component system to beat another well known open source solution both in
|
||||
terms of performance and possibly memory usage.<br/>
|
||||
In the end, I did it, but it wasn't very satisfying. Actually it wasn't
|
||||
satisfying at all. The fastest and nothing more, fairly little indeed. When I
|
||||
realized it, I tried hard to keep intact the great performance of `EnTT` and to
|
||||
add all the features I wanted to see in *my own library* at the same time.
|
||||
@@ -152,10 +174,10 @@ amazing set of features. And even more, of course.
|
||||
|
||||
## Performance
|
||||
|
||||
As it stands right now, `EnTT` is just fast enough for my requirements if
|
||||
As it stands right now, `EnTT` is just fast enough for my requirements when
|
||||
compared to my first choice (it was already amazingly fast actually).<br/>
|
||||
Below is a comparison between the two (both of them compiled with GCC 7.3.0 on a
|
||||
Dell XPS 13 out of the mid 2014):
|
||||
Dell XPS 13 from mid 2014):
|
||||
|
||||
| Benchmark | EntityX (compile-time) | EnTT |
|
||||
|-----------|-------------|-------------|
|
||||
@@ -177,27 +199,27 @@ Dell XPS 13 out of the mid 2014):
|
||||
Note: The default version of `EntityX` (`master` branch) wasn't added to the
|
||||
comparison because it's already much slower than its compile-time counterpart.
|
||||
|
||||
Pretty interesting, aren't them? In fact, these benchmarks are the same used by
|
||||
`EntityX` to show _how fast it is_. To be honest, they aren't so good and these
|
||||
results shouldn't be taken much seriously (they are completely unrealistic
|
||||
indeed).<br/>
|
||||
Pretty interesting results, aren't them? In fact, these benchmarks are the ones
|
||||
used by `EntityX` to show _how fast it is_. To be honest, they aren't so good
|
||||
and these results shouldn't be taken too seriously (indeed they are completely
|
||||
unrealistic).<br/>
|
||||
The proposed entity-component system is incredibly fast to iterate entities,
|
||||
this is a fact. The compiler can make a lot of optimizations because of how
|
||||
`EnTT` works, even more when components aren't used at all. This is exactly the
|
||||
case for these benchmarks. On the other hand and if we consider real world
|
||||
cases, `EnTT` is in the middle between a bit and much faster than the other
|
||||
solutions around when users also access the components and not just the
|
||||
entities, although it is not as fast as reported by these benchmarks.<br/>
|
||||
case for these benchmarks. On the other hand, if we consider real world cases,
|
||||
`EnTT` is somewhere between a bit and much faster than the other solutions
|
||||
around when users also access the components and not just the entities, although
|
||||
it isn't as fast as reported by these benchmarks.<br/>
|
||||
This is why they are completely wrong and cannot be used to evaluate any of the
|
||||
entity-component systems.
|
||||
entity-component-system libraries out there.
|
||||
|
||||
If you decide to use `EnTT`, choose it because of its API, features and
|
||||
performance, not because there is a benchmark somewhere that makes it seem the
|
||||
fastest.
|
||||
The choice to use `EnTT` should be based on its carefully designed API, its
|
||||
set of features and the general performance, not because some single benchmark
|
||||
shows it to be the fastest tool available.
|
||||
|
||||
Probably I'll try to get out of `EnTT` more features and even better performance
|
||||
in the future, mainly for fun.<br/>
|
||||
If you want to contribute and/or have any suggestion, feel free to make a PR or
|
||||
In the future I'll likely try to get even better performance while still adding
|
||||
new features, mainly for fun.<br/>
|
||||
If you want to contribute and/or have suggestions, feel free to make a PR or
|
||||
open an issue to discuss your idea.
|
||||
|
||||
# Build Instructions
|
||||
@@ -205,13 +227,15 @@ open an issue to discuss your idea.
|
||||
## Requirements
|
||||
|
||||
To be able to use `EnTT`, users must provide a full-featured compiler that
|
||||
supports at least C++14.<br/>
|
||||
supports at least C++17.<br/>
|
||||
The requirements below are mandatory to compile the tests and to extract the
|
||||
documentation:
|
||||
|
||||
* CMake version 3.2 or later.
|
||||
* Doxygen version 1.8 or later.
|
||||
|
||||
If you are looking for a C++14 version of `EnTT`, check out the git tag `cpp14`.
|
||||
|
||||
## Library
|
||||
|
||||
`EnTT` is a header-only library. This means that including the `entt.hpp` header
|
||||
@@ -235,7 +259,7 @@ the include paths.
|
||||
|
||||
## Documentation
|
||||
|
||||
The documentation is based on [doxygen](http://www.stack.nl/~dimitri/doxygen/).
|
||||
The documentation is based on [doxygen](http://www.doxygen.nl/).
|
||||
To build it:
|
||||
|
||||
$ cd build
|
||||
@@ -251,10 +275,10 @@ The API reference will be created in HTML format within the directory
|
||||
<!--
|
||||
@cond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
The API reference is also available [online](https://skypjack.github.io/entt/)
|
||||
for the latest version.<br/>
|
||||
There exists also a [wiki](https://github.com/skypjack/entt/wiki) dedicated to
|
||||
the project where users can find all related documentation pages.
|
||||
It's also available [online](https://skypjack.github.io/entt/) for the latest
|
||||
version.<br/>
|
||||
Finally, there exists a [wiki](https://github.com/skypjack/entt/wiki) dedicated
|
||||
to the project where users can find all related documentation pages.
|
||||
<!--
|
||||
@endcond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
@@ -263,12 +287,12 @@ the project where users can find all related documentation pages.
|
||||
|
||||
To compile and run the tests, `EnTT` requires *googletest*.<br/>
|
||||
`cmake` will download and compile the library before compiling anything else.
|
||||
In order to build without tests set CMake option `BUILD_TESTING=OFF`.
|
||||
In order to build the tests, set the CMake option `BUILD_TESTING` to `ON`.
|
||||
|
||||
To build the most basic set of tests:
|
||||
|
||||
* `$ cd build`
|
||||
* `$ cmake ..`
|
||||
* `$ cmake -DBUILD_TESTING=ON ..`
|
||||
* `$ make`
|
||||
* `$ make test`
|
||||
|
||||
@@ -278,59 +302,37 @@ Note that benchmarks are not part of this set.
|
||||
|
||||
`EnTT` is available for some of the most known packaging tools. In particular:
|
||||
|
||||
* [`vcpkg`](https://github.com/Microsoft/vcpkg/tree/master/ports/entt),
|
||||
Microsoft VC++ Packaging Tool.
|
||||
* [`Conan`](https://bintray.com/skypjack/conan/entt%3Askypjack/_latestVersion),
|
||||
the C/C++ Package Manager for Developers.
|
||||
* [`Homebrew`](https://github.com/skypjack/homebrew-entt), the missing package
|
||||
manager for macOS.<br/>
|
||||
Available as a homebrew formula. Just type the following to install it:
|
||||
```
|
||||
brew install skypjack/entt/entt
|
||||
```
|
||||
* [`vcpkg`](https://github.com/Microsoft/vcpkg/tree/master/ports/entt),
|
||||
Microsoft VC++ Packaging Tool.
|
||||
|
||||
Consider this list a work in progress and help me to make it longer.
|
||||
|
||||
# EnTT in Action
|
||||
|
||||
`EnTT` is widely used in private and commercial applications. I cannot even
|
||||
mention most of them because of some signatures I put on some documents time
|
||||
ago.<br/>
|
||||
Fortunately, there are also people who took the time to implement open source
|
||||
projects based on EnTT and did not hold back when it came to documenting them.
|
||||
|
||||
Below an incomplete list of projects and articles:
|
||||
|
||||
* [Minecraft](https://minecraft.net/en-us/attribution/): of course, **that**
|
||||
Minecraft, by Mojang (see the open source attributions page).
|
||||
* [Face Smash](https://play.google.com/store/apps/details?id=com.gamee.facesmash):
|
||||
the emojis dominate the world, destroy them all with your facial expressions.
|
||||
* [shiva](https://github.com/Milerius/shiva): modern C++ Engine with modularity.
|
||||
* [Classic Tower Defence](https://github.com/kerndog73/Classic-Tower-Defence):
|
||||
a tiny little tower defence game featuring a homemade font.
|
||||
[Check it out](https://indi-kernick.itch.io/classic-tower-defence).
|
||||
* [The Machine](https://github.com/Kerndog73/The-Machine): a box pushing puzzler
|
||||
with logic gates and other cool stuff.
|
||||
[Check it out](https://indi-kernick.itch.io/the-machine-web-version).
|
||||
* [EnttPong](https://github.com/reworks/EnttPong): example game with `EnTT`.
|
||||
* [Space Battle: Huge edition](http://victor.madtriangles.com/code%20experiment/2018/06/11/post-ecs-battle-huge.html):
|
||||
huge space battle built entirely from scratch.
|
||||
* [Space Battle](https://github.com/vblanco20-1/ECS_SpaceBattle): huge space
|
||||
battle built on `UE4`.
|
||||
* [Experimenting with ECS in UE4](http://victor.madtriangles.com/code%20experiment/2018/03/25/post-ue4-ecs-battle.html):
|
||||
interesting article about `UE4` and `EnTT`.
|
||||
* [Implementing ECS architecture in UE4](https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1449913-implementing-ecs-architecture-in-ue4-giant-space-battle):
|
||||
giant space battle.
|
||||
* [MatchOneEntt](https://github.com/mhaemmerle/MatchOneEntt): port of
|
||||
[Match One](https://github.com/sschmid/Match-One) for `Entitas-CSharp`.
|
||||
* [Randballs](https://github.com/gale93/randballs): simple `SFML` and `EnTT`
|
||||
playground.
|
||||
* ...
|
||||
|
||||
If you know of other resources out there that are about `EnTT`, feel free to
|
||||
open an issue or a PR and I'll be glad to add them to the list.
|
||||
|
||||
<!--
|
||||
@cond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
# EnTT in Action
|
||||
|
||||
`EnTT` is widely used in private and commercial applications. I cannot even
|
||||
mention most of them because of some signatures I put on some documents time
|
||||
ago. Fortunately, there are also people who took the time to implement open
|
||||
source projects based on `EnTT` and did not hold back when it came to
|
||||
documenting them.
|
||||
|
||||
[Here](https://github.com/skypjack/entt/wiki/EnTT-in-Action) you can find an
|
||||
incomplete list of games, applications and articles that can be used as a
|
||||
reference.
|
||||
|
||||
If you know of other resources out there that are about `EnTT`, feel free to
|
||||
open an issue or a PR and I'll be glad to add them to the list.
|
||||
|
||||
# Contributors
|
||||
|
||||
`EnTT` was written initially as a faster alternative to other well known and
|
||||
@@ -347,7 +349,8 @@ I can't promise that each and every contribution will be accepted, but I can
|
||||
assure that I'll do my best to take them all seriously.
|
||||
|
||||
If you decide to participate, please see the guidelines for
|
||||
[contributing](docs/CONTRIBUTING.md) before to create issues or pull requests.<br/>
|
||||
[contributing](docs/md/CONTRIBUTING.md) before to create issues or pull
|
||||
requests.<br/>
|
||||
Take also a look at the
|
||||
[contributors list](https://github.com/skypjack/entt/blob/master/AUTHORS) to
|
||||
know who has participated so far.
|
||||
@@ -357,8 +360,8 @@ know who has participated so far.
|
||||
|
||||
# License
|
||||
|
||||
Code and documentation Copyright (c) 2017-2018 Michele Caini.<br/>
|
||||
Logo Copyright (c) 2018 Richard Caseres.
|
||||
Code and documentation Copyright (c) 2017-2019 Michele Caini.<br/>
|
||||
Logo Copyright (c) 2018-2019 Richard Caseres.
|
||||
|
||||
Code released under
|
||||
[the MIT license](https://github.com/skypjack/entt/blob/master/LICENSE).
|
||||
@@ -372,6 +375,16 @@ Logo released under
|
||||
-->
|
||||
# Support
|
||||
|
||||
## Patreon
|
||||
|
||||
Become a [patron](https://www.patreon.com/bePatron?c=1772573) and get access to
|
||||
extra content, help me spend more time on the projects you love and create new
|
||||
ones for you. Your support will help me to continue the work done so far and
|
||||
make it more professional and feature-rich every day.<br/>
|
||||
It takes very little to
|
||||
[become a patron](https://www.patreon.com/bePatron?c=1772573) and thus help the
|
||||
software you use every day. Don't miss the chance.
|
||||
|
||||
## Donation
|
||||
|
||||
Developing and maintaining `EnTT` takes some time and lots of coffee. I'd like
|
||||
@@ -381,7 +394,7 @@ If you want to support this project, you can offer me an espresso. I'm from
|
||||
Italy, we're used to turning the best coffee ever in code. If you find that
|
||||
it's not enough, feel free to support me the way you prefer.<br/>
|
||||
Take a look at the donation button at the top of the page for more details or
|
||||
just click [here](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=W2HF9FESD5LJY&lc=IT&item_name=Michele%20Caini¤cy_code=EUR&bn=PP%2dDonationsBF%3abtn_donateCC_LG%2egif%3aNonHosted).
|
||||
just click [here](https://www.paypal.me/skypjack).
|
||||
|
||||
## Hire me
|
||||
|
||||
|
||||
39
TODO
39
TODO
@@ -1,17 +1,28 @@
|
||||
* long term feature: templated generic vm
|
||||
* long term feature: shared_ptr less locator
|
||||
* long term feature: shared_ptr less resource cache
|
||||
* custom allocators and EnTT allocator-aware in general (long term feature, I don't actually need it at the moment) - see #22
|
||||
* scene management (I prefer the concept of spaces, that is a kind of scene anyway)
|
||||
* debugging tools (#60): the issue online already contains interesting tips on this, look at it
|
||||
* define basic reactive systems (track entities to which component is attached, track entities from which component is removed, and so on)
|
||||
* define systems as composable mixins (initializazion, reactive, update, whatever) with flexible auto-detected arguments (registry, views, etc)
|
||||
* registry::create with a "hint" on the entity identifier to use, it should ease combining multiple registries
|
||||
* deep copy of a registry (or use the snapshot stuff to copy components and keep intact ids at least)
|
||||
* is it possible to iterate all the components assigned to an entity through a common base class?
|
||||
* optimize for empty components, it would be a mid improvement in terms of memory usage
|
||||
* can we do more for shared libraries? who knows... see #144
|
||||
* runner proposal: https://en.wikipedia.org/wiki/Fork%E2%80%93join_model https://slide-rs.github.io/specs/03_dispatcher.html
|
||||
* work stealing job system (see #100)
|
||||
* make view copyable/moveable
|
||||
* reflection system (maybe)
|
||||
* C++17. That's all.
|
||||
* AOB
|
||||
* lower case names (?)
|
||||
* tag_t and the others, create constexpr var
|
||||
* meta: sort of meta view based on meta stuff to iterate entities, void * and meta info objects
|
||||
* allow for built-in parallel each if possible
|
||||
* allow to replace std:: with custom implementations
|
||||
* remove runtime views, welcome reflection and what about snapshot?
|
||||
* empty components model allows for shared components and prefabs unity-like
|
||||
- each with entity return the shared component multiple times, one per entity that refers to it
|
||||
- each components only return actual component, so shared components are returned only once
|
||||
* types defined at runtime that refer to the same compile-time type (but to different pools) are possible, the library is almost there
|
||||
* add take functionality, eg registry.take(entity, other); where it takes the entity and all its components from registry and move them to other
|
||||
* add opaque input iterators to views and groups that return tuples <entity, T &...> (proxy), multi-pass guaranteed
|
||||
* add fast lane for raw iterations, extend mt doc to describe allowed add/remove with pre-allocations on fast lanes
|
||||
* review sparse set to allow customization (mix pack in the spec, base is position only)
|
||||
- non-owning groups can iterate pages and skip empty ones, this should mitigate the lack of the packed array
|
||||
* review 64 bit id: user defined area + dedicated member on the registry to set it
|
||||
* early out in views using bitmasks with bloom filter like access based on modulus
|
||||
- standard each, use bitmask to speed up the whole thing and avoid accessing the pools to test for the page
|
||||
- iterator based each with a couple of iterators passed from outside (use bitmask + has)
|
||||
* stable component handle that isn't affected by reallocations
|
||||
* multi component registry::remove and some others?
|
||||
* move CONTRIBUTING.md within doc (for GitHub)
|
||||
* reactive systems
|
||||
|
||||
@@ -14,7 +14,10 @@ configuration:
|
||||
|
||||
before_build:
|
||||
- cd %BUILD_DIR%
|
||||
- cmake .. -DCMAKE_CXX_FLAGS=/W1 -G"Visual Studio 15 2017"
|
||||
- cmake .. -DBUILD_TESTING=ON -DBUILD_LIB=ON -DCMAKE_CXX_FLAGS=/W1 -G"Visual Studio 15 2017"
|
||||
|
||||
after_build:
|
||||
- ctest -C Release -j4
|
||||
|
||||
build:
|
||||
parallel: true
|
||||
|
||||
11
cmake/in/version.h.in
Normal file
11
cmake/in/version.h.in
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef ENTT_CONFIG_VERSION_H
|
||||
#define ENTT_CONFIG_VERSION_H
|
||||
|
||||
|
||||
#define ENTT_VERSION "@PROJECT_VERSION@"
|
||||
#define ENTT_VERSION_MAJOR @PROJECT_VERSION_MAJOR@
|
||||
#define ENTT_VERSION_MINOR @PROJECT_VERSION_MINOR@
|
||||
#define ENTT_VERSION_PATCH @PROJECT_VERSION_PATCH@
|
||||
|
||||
|
||||
#endif // ENTT_CONFIG_VERSION_H
|
||||
45
conan/build.py
Normal file
45
conan/build.py
Normal file
@@ -0,0 +1,45 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from cpt.packager import ConanMultiPackager
|
||||
import os
|
||||
|
||||
if __name__ == "__main__":
|
||||
login_username = os.getenv("CONAN_LOGIN_USERNAME")
|
||||
username = os.getenv("CONAN_USERNAME")
|
||||
tag_version = os.getenv("CONAN_PACKAGE_VERSION", os.getenv("TRAVIS_TAG"))
|
||||
package_version = tag_version.replace("v", "")
|
||||
package_name_unset = "SET-CONAN_PACKAGE_NAME-OR-CONAN_REFERENCE"
|
||||
package_name = os.getenv("CONAN_PACKAGE_NAME", package_name_unset)
|
||||
reference = "{}/{}".format(package_name, package_version)
|
||||
channel = os.getenv("CONAN_CHANNEL", "stable")
|
||||
upload = os.getenv("CONAN_UPLOAD")
|
||||
stable_branch_pattern = os.getenv("CONAN_STABLE_BRANCH_PATTERN", r"v\d+\.\d+\.\d+.*")
|
||||
test_folder = os.getenv("CPT_TEST_FOLDER", os.path.join("conan", "test_package"))
|
||||
upload_only_when_stable = os.getenv("CONAN_UPLOAD_ONLY_WHEN_STABLE", True)
|
||||
header_only = os.getenv("CONAN_HEADER_ONLY", False)
|
||||
pure_c = os.getenv("CONAN_PURE_C", False)
|
||||
|
||||
disable_shared = os.getenv("CONAN_DISABLE_SHARED_BUILD", "False")
|
||||
if disable_shared == "True" and package_name == package_name_unset:
|
||||
raise Exception("CONAN_DISABLE_SHARED_BUILD: True is only supported when you define CONAN_PACKAGE_NAME")
|
||||
|
||||
builder = ConanMultiPackager(username=username,
|
||||
reference=reference,
|
||||
channel=channel,
|
||||
login_username=login_username,
|
||||
upload=upload,
|
||||
stable_branch_pattern=stable_branch_pattern,
|
||||
upload_only_when_stable=upload_only_when_stable,
|
||||
test_folder=test_folder)
|
||||
if header_only == "False":
|
||||
builder.add_common_builds(pure_c=pure_c)
|
||||
else:
|
||||
builder.add()
|
||||
|
||||
filtered_builds = []
|
||||
for settings, options, env_vars, build_requires, reference in builder.items:
|
||||
if disable_shared == "False" or not options["{}:shared".format(package_name)]:
|
||||
filtered_builds.append([settings, options, env_vars, build_requires])
|
||||
builder.builds = filtered_builds
|
||||
|
||||
builder.run()
|
||||
7
conan/ci/build.sh
Normal file
7
conan/ci/build.sh
Normal file
@@ -0,0 +1,7 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set -x
|
||||
|
||||
conan user
|
||||
python conan/build.py
|
||||
6
conan/ci/install.sh
Normal file
6
conan/ci/install.sh
Normal file
@@ -0,0 +1,6 @@
|
||||
#!/bin/bash
|
||||
|
||||
set -e
|
||||
set -x
|
||||
|
||||
pip install -U conan_package_tools conan
|
||||
13
conan/test_package/CMakeLists.txt
Normal file
13
conan/test_package/CMakeLists.txt
Normal file
@@ -0,0 +1,13 @@
|
||||
cmake_minimum_required(VERSION 3.7.2)
|
||||
project(test_package)
|
||||
|
||||
set(CMAKE_VERBOSE_MAKEFILE TRUE)
|
||||
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
|
||||
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
|
||||
conan_basic_setup()
|
||||
|
||||
add_executable(${PROJECT_NAME} test_package.cpp)
|
||||
target_link_libraries(${PROJECT_NAME} ${CONAN_LIBS})
|
||||
19
conan/test_package/conanfile.py
Normal file
19
conan/test_package/conanfile.py
Normal file
@@ -0,0 +1,19 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
from conans import ConanFile, CMake
|
||||
import os
|
||||
|
||||
|
||||
class TestPackageConan(ConanFile):
|
||||
settings = "os", "compiler", "build_type", "arch"
|
||||
generators = "cmake"
|
||||
|
||||
def build(self):
|
||||
cmake = CMake(self)
|
||||
cmake.configure()
|
||||
cmake.build()
|
||||
|
||||
def test(self):
|
||||
bin_path = os.path.join("bin", "test_package")
|
||||
self.run(bin_path, run_environment=True)
|
||||
56
conan/test_package/test_package.cpp
Normal file
56
conan/test_package/test_package.cpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#include <entt/entt.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
struct position {
|
||||
float x;
|
||||
float y;
|
||||
};
|
||||
|
||||
struct velocity {
|
||||
float dx;
|
||||
float dy;
|
||||
};
|
||||
|
||||
void update(entt::registry ®istry) {
|
||||
auto view = registry.view<position, velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
// gets only the components that are going to be used ...
|
||||
|
||||
auto &vel = view.get<velocity>(entity);
|
||||
|
||||
vel.dx = 0.;
|
||||
vel.dy = 0.;
|
||||
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
void update(std::uint64_t dt, entt::registry ®istry) {
|
||||
registry.view<position, velocity>().each([dt](auto &pos, auto &vel) {
|
||||
// gets all the components of the view at once ...
|
||||
|
||||
pos.x += vel.dx * dt;
|
||||
pos.y += vel.dy * dt;
|
||||
|
||||
// ...
|
||||
});
|
||||
}
|
||||
|
||||
int main() {
|
||||
entt::registry registry;
|
||||
std::uint64_t dt = 16;
|
||||
|
||||
for(auto i = 0; i < 10; ++i) {
|
||||
auto entity = registry.create();
|
||||
registry.assign<position>(entity, i * 1.f, i * 1.f);
|
||||
if(i % 2 == 0) { registry.assign<velocity>(entity, i * .1f, i * .1f); }
|
||||
}
|
||||
|
||||
update(dt, registry);
|
||||
update(registry);
|
||||
|
||||
// ...
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
}
|
||||
23
conanfile.py
Normal file
23
conanfile.py
Normal file
@@ -0,0 +1,23 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
from conans import ConanFile
|
||||
|
||||
|
||||
class EnttConan(ConanFile):
|
||||
name = "entt"
|
||||
description = "Gaming meets modern C++ - a fast and reliable entity-component system (ECS) and much more "
|
||||
topics = ("conan," "entt", "gaming", "entity", "ecs")
|
||||
url = "https://github.com/skypjack/entt"
|
||||
homepage = url
|
||||
author = "Michele Caini <michele.caini@gmail.com>"
|
||||
license = "MIT"
|
||||
exports = ["LICENSE"]
|
||||
exports_sources = ["src/*"]
|
||||
no_copy_source = True
|
||||
|
||||
def package(self):
|
||||
self.copy(pattern="LICENSE", dst="licenses")
|
||||
self.copy(pattern="*", dst="include", src="src", keep_path=True)
|
||||
|
||||
def package_id(self):
|
||||
self.info.header_only()
|
||||
@@ -24,13 +24,16 @@ install(
|
||||
add_custom_target(
|
||||
docs_aob
|
||||
SOURCES
|
||||
CONTRIBUTING.md
|
||||
core.md
|
||||
entity.md
|
||||
locator.md
|
||||
process.md
|
||||
resource.md
|
||||
shared.md
|
||||
signal.md
|
||||
extra.dox
|
||||
md/CONTRIBUTING.md
|
||||
md/core.md
|
||||
md/entity.md
|
||||
md/faq.md
|
||||
md/lib.md
|
||||
md/links.md
|
||||
md/locator.md
|
||||
md/meta.md
|
||||
md/process.md
|
||||
md/resource.md
|
||||
md/signal.md
|
||||
dox/extra.dox
|
||||
)
|
||||
|
||||
@@ -780,7 +780,9 @@ WARN_LOGFILE =
|
||||
# spaces. See also FILE_PATTERNS and EXTENSION_MAPPING
|
||||
# Note: If this tag is empty the current directory is searched.
|
||||
|
||||
INPUT = @DOXY_SOURCE_DIRECTORY@ @DOXY_DOCS_DIRECTORY@ @PROJECT_SOURCE_DIR@/README.md
|
||||
INPUT = @DOXY_SOURCE_DIRECTORY@ \
|
||||
@DOXY_DOCS_DIRECTORY@ \
|
||||
@PROJECT_SOURCE_DIR@/README.md
|
||||
|
||||
# This tag can be used to specify the character encoding of the source files
|
||||
# that doxygen parses. Internally doxygen uses the UTF-8 encoding. Doxygen uses
|
||||
@@ -805,9 +807,7 @@ INPUT_ENCODING = UTF-8
|
||||
# *.m, *.markdown, *.md, *.mm, *.dox, *.py, *.pyw, *.f90, *.f95, *.f03, *.f08,
|
||||
# *.f, *.for, *.tcl, *.vhd, *.vhdl, *.ucf and *.qsf.
|
||||
|
||||
FILE_PATTERNS = *.cpp \
|
||||
*.c++ \
|
||||
*.h \
|
||||
FILE_PATTERNS = *.h \
|
||||
*.hpp \
|
||||
*.md \
|
||||
*.dox
|
||||
|
||||
1400
docs/entity.md
1400
docs/entity.md
File diff suppressed because it is too large
Load Diff
@@ -22,8 +22,8 @@ How to do it mostly depends on the type of contribution:
|
||||
If you are unable to find an open issue addressing the problem, feel free to
|
||||
[open a new one](https://github.com/skypjack/entt/issues/new). **Please**, do
|
||||
not forget to carefully describe how to reproduce the problem, then add all
|
||||
the informaion about the system on which you are experiencing it and point out
|
||||
the version of `EnTT` you are using (tag or commit).
|
||||
the information about the system on which you are experiencing it and point
|
||||
out the version of `EnTT` you are using (tag or commit).
|
||||
|
||||
* If you found a bug and you wrote a patch to fix it, open a new
|
||||
[pull request](https://github.com/skypjack/entt/pulls) with your code.
|
||||
@@ -30,21 +30,21 @@ There are plenty of different solutions out there and I could have used one of
|
||||
them. However, I decided to spend my time to define a compact and versatile tool
|
||||
that fully embraces what the modern C++ has to offer.
|
||||
|
||||
The _result of my efforts_ is the `Identifier` class template:
|
||||
The _result of my efforts_ is the `identifier` class template:
|
||||
|
||||
```cpp
|
||||
#include <ident.hpp>
|
||||
|
||||
// defines the identifiers for the given types
|
||||
using ID = entt::Identifier<AType, AnotherType>;
|
||||
using id = entt::identifier<a_type, another_type>;
|
||||
|
||||
// ...
|
||||
|
||||
switch(aTypeIdentifier) {
|
||||
case ID::get<AType>():
|
||||
switch(a_type_identifier) {
|
||||
case id::type<a_type>:
|
||||
// ...
|
||||
break;
|
||||
case ID::get<AnotherType>():
|
||||
case id::type<another_type>:
|
||||
// ...
|
||||
break;
|
||||
default:
|
||||
@@ -52,8 +52,8 @@ default:
|
||||
}
|
||||
```
|
||||
|
||||
This is all what the class template has to offer: a static `get` member function
|
||||
that returns a numerical identifier for the given type. It can be used in any
|
||||
This is all what the class template has to offer: a `type` inline variable that
|
||||
contains a numerical identifier for the given type. It can be used in any
|
||||
context where constant expressions are required.
|
||||
|
||||
As long as the list remains unchanged, identifiers are also guaranteed to be the
|
||||
@@ -62,12 +62,12 @@ a type has to be removed, one can just use a placeholder to left the other
|
||||
identifiers unchanged:
|
||||
|
||||
```cpp
|
||||
template<typename> struct IgnoreType {};
|
||||
template<typename> struct ignore_type {};
|
||||
|
||||
using ID = entt::Identifier<
|
||||
ATypeStillValid,
|
||||
IgnoreType<ATypeNoLongerValid>,
|
||||
AnotherTypeStillValid
|
||||
using id = entt::identifier<
|
||||
a_type_still_valid,
|
||||
ignore_type<a_type_no_longer_valid>,
|
||||
another_type_still_valid
|
||||
>;
|
||||
```
|
||||
|
||||
@@ -81,21 +81,21 @@ There are plenty of different solutions out there and I could have used one of
|
||||
them. In fact, I adapted the most common one to my requirements and used it
|
||||
extensively within the entire library.
|
||||
|
||||
It's the `Family` class. Here is an example of use directly from the
|
||||
It's the `family` class. Here is an example of use directly from the
|
||||
entity-component system:
|
||||
|
||||
```cpp
|
||||
using component_family = entt::Family<struct InternalRegistryComponentFamily>;
|
||||
using component_family = entt::family<struct internal_registry_component_family>;
|
||||
|
||||
// ...
|
||||
|
||||
template<typename Component>
|
||||
component_type component() const noexcept {
|
||||
return component_family::type<Component>();
|
||||
return component_family::type<Component>;
|
||||
}
|
||||
```
|
||||
|
||||
This is all what a _family_ has to offer: a `type` member function that returns
|
||||
This is all what a _family_ has to offer: a `type` inline variable that contains
|
||||
a numerical identifier for the given type.
|
||||
|
||||
Please, note that identifiers aren't guaranteed to be the same for every run.
|
||||
@@ -103,7 +103,7 @@ Indeed it mostly depends on the flow of execution.
|
||||
|
||||
# Hashed strings
|
||||
|
||||
A hashed string is a zero overhead resource identifier. Users can use
|
||||
A hashed string is a zero overhead unique identifier. Users can use
|
||||
human-readable identifiers in the codebase while using their numeric
|
||||
counterparts at runtime, thus without affecting performance.<br/>
|
||||
The class has an implicit `constexpr` constructor that chews a bunch of
|
||||
@@ -116,11 +116,11 @@ used carefully.
|
||||
Example of use:
|
||||
|
||||
```cpp
|
||||
auto load(entt::HashedString::hash_type resource) {
|
||||
auto load(entt::hashed_string::hash_type resource) {
|
||||
// uses the numeric representation of the resource to load and return it
|
||||
}
|
||||
|
||||
auto resource = load(entt::HashedString{"gui/background"});
|
||||
auto resource = load(entt::hashed_string{"gui/background"});
|
||||
```
|
||||
|
||||
There is also a _user defined literal_ dedicated to hashed strings to make them
|
||||
@@ -138,7 +138,7 @@ possible. This is a fact.<br/>
|
||||
There is no silver bullet to solve the problem of conflicts when dealing with
|
||||
hashing functions. In this case, the best solution seemed to be to give up.
|
||||
That's all.<br/>
|
||||
After all, human-readable resource identifiers aren't something strictly defined
|
||||
After all, human-readable unique identifiers aren't something strictly defined
|
||||
and over which users have not the control. Choosing a slightly different
|
||||
identifier is probably the best solution to make the conflict disappear in this
|
||||
case.
|
||||
@@ -157,11 +157,11 @@ they will probably incur in unexpected results.
|
||||
Example of use:
|
||||
|
||||
```cpp
|
||||
entt::Monostate<entt::HashedString{"mykey"}>{} = true;
|
||||
entt::Monostate<"mykey"_hs>{} = 42;
|
||||
entt::monostate<entt::hashed_string{"mykey"}>{} = true;
|
||||
entt::monostate<"mykey"_hs>{} = 42;
|
||||
|
||||
// ...
|
||||
|
||||
const bool b = entt::Monostate<"mykey"_hs>{};
|
||||
const int i = entt::Monostate<entt::HashedString{"mykey"}>{};
|
||||
const bool b = entt::monostate<"mykey"_hs>{};
|
||||
const int i = entt::monostate<entt::hashed_string{"mykey"}>{};
|
||||
```
|
||||
1549
docs/md/entity.md
Normal file
1549
docs/md/entity.md
Normal file
File diff suppressed because it is too large
Load Diff
55
docs/md/faq.md
Normal file
55
docs/md/faq.md
Normal file
@@ -0,0 +1,55 @@
|
||||
# Frequently Asked Questions
|
||||
|
||||
<!--
|
||||
@cond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
# Table of Contents
|
||||
|
||||
* [Introduction](#introduction)
|
||||
* [FAQ](#faq)
|
||||
* [Why is my debug build on Windows so slow?](#why-is-my-debug-build-on-windows-so-slow)
|
||||
<!--
|
||||
@endcond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
|
||||
# Introduction
|
||||
|
||||
This is a constantly updated section where I'll try to put the answers to the
|
||||
most frequently asked questions.<br/>
|
||||
If you don't find your answer here, there are two cases: nobody has done it yet
|
||||
or this section needs updating. In both cases, try to
|
||||
[open a new issue](https://github.com/skypjack/entt/issues/new) or enter the
|
||||
[gitter channel](https://gitter.im/skypjack/entt) and ask your question.
|
||||
Probably someone already has an answer for you and we can then integrate this
|
||||
part of the documentation.
|
||||
|
||||
# FAQ
|
||||
|
||||
## Why is my debug build on Windows so slow?
|
||||
|
||||
`EnTT` is an experimental project that I also use to keep me up-to-date with the
|
||||
latest revision of the language and the standard library. For this reason, it's
|
||||
likely that some classes you're working with are using standard containers under
|
||||
the hood.<br/>
|
||||
Unfortunately, it's known that the standard containers aren't particularly
|
||||
performing in debugging (the reasons for this go beyond this document) and are
|
||||
even less so on Windows apparently. Fortunately this can also be mitigated a
|
||||
lot, achieving good results in many cases.
|
||||
|
||||
First of all, there are two things to do in a Windows project:
|
||||
|
||||
* Disable the [`/JMC`](https://docs.microsoft.com/cpp/build/reference/jmc)
|
||||
option (_Just My Code_ debugging), available starting in Visual Studio 2017
|
||||
version 15.8.
|
||||
|
||||
* Set the [`_ITERATOR_DEBUG_LEVEL`](https://docs.microsoft.com/cpp/standard-library/iterator-debug-level)
|
||||
macro to 0. This will disable checked iterators and iterator debugging.
|
||||
|
||||
Moreover, the macro `ENTT_DISABLE_ASSERT` should be defined to disable internal
|
||||
checks made by `EnTT` in debug. These are asserts introduced to help the users,
|
||||
but require to access to the underlying containers and therefore risk ruining
|
||||
the performance in some cases.
|
||||
|
||||
With these changes, debug performance should increase enough for most cases. If
|
||||
you want something more, you can can also switch to an optimization level `O0`
|
||||
or preferably `O1`.
|
||||
155
docs/md/lib.md
Normal file
155
docs/md/lib.md
Normal file
@@ -0,0 +1,155 @@
|
||||
# Push EnTT across boundaries
|
||||
|
||||
<!--
|
||||
@cond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
# Table of Contents
|
||||
|
||||
* [Introduction](#introduction)
|
||||
* [Named types and traits class](#named-types-and-traits-class)
|
||||
* [Do not mix types](#do-not-mix-types)
|
||||
* [Macros, macros everywhere](#macros-macros-everywhere)
|
||||
* [Conflicts](#conflicts)
|
||||
* [Allocations: the dark side of the force](#allocations-the-dark-side-of-the-force)
|
||||
<!--
|
||||
@endcond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
|
||||
# Introduction
|
||||
|
||||
`EnTT` has historically had a limit when used across boundaries on Windows in
|
||||
general and on GNU/Linux when default visibility was set to _hidden_. The
|
||||
limitation is due mainly to a custom utility used to assign unique, sequential
|
||||
identifiers to different types. Unfortunately, this tool is used by several core
|
||||
classes (the `registry` among the others) that are thus almost unusable across
|
||||
boundaries.<br/>
|
||||
The reasons for that are beyond the purposes of this document. However, the good
|
||||
news is that `EnTT` also offers now a way to overcome this limit and to push
|
||||
things across boundaries without problems when needed.
|
||||
|
||||
# Named types and traits class
|
||||
|
||||
To allow a type to work properly across boundaries when used by a class that
|
||||
requires to assign unique identifiers to types, users must specialize a class
|
||||
template to literally give a compile-time name to the type itself.<br/>
|
||||
The name of the class template is `name_type_traits` and the specialization must
|
||||
be such that it exposes a static constexpr data member named `value` having type
|
||||
either `ENTT_ID_TYPE` or `entt::hashed_string::hash_type`. Its value is the user
|
||||
defined unique identifier assigned to the specific type.<br/>
|
||||
Identifiers are not to be sequentially generated in this case.
|
||||
|
||||
As an example:
|
||||
|
||||
```cpp
|
||||
struct my_type { /* ... */ };
|
||||
|
||||
template<>
|
||||
struct entt::named_type_traits<my_type> {
|
||||
static constexpr auto value = "my_type"_hs;
|
||||
};
|
||||
```
|
||||
|
||||
Because of the rules of the language, the specialization must reside in the
|
||||
global namespace or in the `entt` namespace. There is no way to change this rule
|
||||
unfortunately, because it doesn't depend on the library itself.
|
||||
|
||||
The good aspect of this approach is that it's not intrusive at all. The other
|
||||
way around was in fact forcing users to inherit all their classes from a common
|
||||
base. Something to avoid, at least from my point of view.<br/>
|
||||
However, despite the fact that it's not intrusive, it would be great if it was
|
||||
also easier to use and a bit less error-prone. This is why a bunch of macros
|
||||
exist to ease defining named types.
|
||||
|
||||
## Do not mix types
|
||||
|
||||
Someone might think that this trick is valid only for the types to push across
|
||||
boundaries. This isn't how things work. In fact, the problem is more complex
|
||||
than that.<br/>
|
||||
As a rule of thumb, users should never mix named and non-named types. Whenever
|
||||
a type is given a name, all the types must be given a name. As an example,
|
||||
consider the `registry` class template: in case it is pushed across boundaries,
|
||||
all the types of components should be assigned a name to avoid subtle bugs.
|
||||
|
||||
Indeed, this constraint can be relaxed in many cases. However, it is difficult
|
||||
to define a general rule to follow that is not the most stringent, unless users
|
||||
know exactly what they are doing. Therefore, I won't elaborate on giving further
|
||||
details on the topic.
|
||||
|
||||
# Macros, macros everywhere
|
||||
|
||||
The library comes with a set of predefined macros to use to declare named types
|
||||
or export already existing ones. In particular:
|
||||
|
||||
* `ENTT_NAMED_TYPE` can be used to assign a name to already existing types. This
|
||||
macro must be used in the global namespace even when the types to be named are
|
||||
not.
|
||||
|
||||
```cpp
|
||||
ENTT_NAMED_TYPE(my_type)
|
||||
ENTT_NAMED_TYPE(ns::another_type)
|
||||
```
|
||||
|
||||
* `ENTT_NAMED_STRUCT` can be used to define and export a struct at the same
|
||||
time. It accepts also an optional namespace in which to define the given type.
|
||||
This macro must be used in the global namespace.
|
||||
|
||||
```cpp
|
||||
ENTT_NAMED_STRUCT(my_type, { /* struct definition */})
|
||||
ENTT_NAMED_STRUCT(ns, another_type, { /* struct definition */})
|
||||
```
|
||||
|
||||
* `ENTT_NAMED_CLASS` can be used to define and export a class at the same
|
||||
time. It accepts also an optional namespace in which to define the given type.
|
||||
This macro must be used in the global namespace.
|
||||
|
||||
```cpp
|
||||
ENTT_NAMED_CLASS(my_type, { /* class definition */})
|
||||
ENTT_NAMED_CLASS(ns, another_type, { /* class definition */})
|
||||
```
|
||||
|
||||
Nested namespaces are supported out of the box as well in all cases. As an
|
||||
example:
|
||||
|
||||
```cpp
|
||||
ENTT_NAMED_STRUCT(nested::ns, my_type, { /* struct definition */})
|
||||
```
|
||||
|
||||
These macros can be used to avoid specializing the `named_type_traits` class
|
||||
template. In all cases, the name of the class is used also as a seed to generate
|
||||
the compile-time unique identifier.
|
||||
|
||||
## Conflicts
|
||||
|
||||
When using macros, unique identifiers are 32/64 bit integers generated by
|
||||
hashing strings during compilation. Therefore, conflicts are rare but still
|
||||
possible. In case of conflicts, everything simply will get broken at runtime and
|
||||
the strangest things will probably take place.<br/>
|
||||
Unfortunately, there is no safe way to prevent it. If this happens, it will be
|
||||
enough to give a different value to one of the conflicting types to solve the
|
||||
problem. To do this, users can either assign a different name to the class or
|
||||
directly define a specialization for the `named_type_traits` class template.
|
||||
|
||||
# Allocations: the dark side of the force
|
||||
|
||||
As long as `EnTT` won't support custom allocators, another problem with
|
||||
allocations will remain alive instead. This is in fact easily solved, or at
|
||||
least it is if one knows it.
|
||||
|
||||
To allow users to add types dynamically, the library makes extensive use of type
|
||||
erasure techniques and dynamic allocations for pools (whether they are for
|
||||
components, events or anything else). The problem occurs when, for example, a
|
||||
registry is created on one side of a boundary and a pool is dynamically created
|
||||
on the other side. In the best case, everything will crash at the exit, while at
|
||||
worst it will do so at runtime.<br/>
|
||||
To avoid problems, the pools must be generated from the same side of the
|
||||
boundary where the object that owns them is also created. As an example, when
|
||||
the registry is created in the main executable and used across boundaries for a
|
||||
given type of component, the pool for that type must be created before passing
|
||||
around the registry itself. To do this is fortunately quite easy, since it is
|
||||
sufficient to invoke any of the methods that involve the given type (continuing
|
||||
the example with the registry, a call to `reserve` or `size` is more than
|
||||
enough).
|
||||
|
||||
Maybe one day some dedicated methods will be added that do nothing but create a
|
||||
pool for a given type. Until now it has been preferred to keep the API cleaner
|
||||
as they are not strictly necessary.
|
||||
93
docs/md/links.md
Normal file
93
docs/md/links.md
Normal file
@@ -0,0 +1,93 @@
|
||||
# EnTT in Action
|
||||
|
||||
`EnTT` is widely used in private and commercial applications. I cannot even
|
||||
mention most of them because of some signatures I put on some documents time
|
||||
ago. Fortunately, there are also people who took the time to implement open
|
||||
source projects based on `EnTT` and did not hold back when it came to
|
||||
documenting them.
|
||||
|
||||
Below an incomplete list of games, applications and articles that can be used as
|
||||
a reference. Where I put the word _apparently_ means that the use of `EnTT` is
|
||||
documented but the authors didn't make explicit announcements or contacted me
|
||||
directly.
|
||||
|
||||
I hope this list can grow much more in the future:
|
||||
|
||||
* Games:
|
||||
* [Minecraft](https://minecraft.net/en-us/attribution/) by
|
||||
[Mojang](https://mojang.com/): of course, **that** Minecraft, see the
|
||||
open source attributions page for more details.
|
||||
* [Face Smash](https://play.google.com/store/apps/details?id=com.gamee.facesmash):
|
||||
a game to play with your face.
|
||||
* [EnTT Pacman](https://github.com/Kerndog73/EnTT-Pacman): an example of how
|
||||
to make Pacman with `EnTT`.
|
||||
* [Classic Tower Defence](https://github.com/kerndog73/Classic-Tower-Defence):
|
||||
a tiny little tower defence game featuring a homemade font.
|
||||
[Check it out](https://indi-kernick.itch.io/classic-tower-defence).
|
||||
* [The Machine](https://github.com/Kerndog73/The-Machine): a box pushing
|
||||
puzzler with logic gates and other cool stuff.
|
||||
[Check it out](https://indi-kernick.itch.io/the-machine-web-version).
|
||||
* [EnttPong](https://github.com/reworks/EnttPong): an example of how to make
|
||||
Pong with `EnTT`.
|
||||
* [Randballs](https://github.com/gale93/randballs): simple `SFML` and `EnTT`
|
||||
playground.
|
||||
* [EnTT Tower Defense](https://github.com/Daivuk/tddod): a data oriented tower
|
||||
defense example.
|
||||
* [EnTT Breakout](https://github.com/vblanco20-1/entt-breakout): simple
|
||||
example of a breakout game, using `SDL` and `EnTT`.
|
||||
|
||||
* Engines/Frameworks:
|
||||
* [The Forge](https://github.com/ConfettiFX/The-Forge) by
|
||||
[Confett](http://www.confettispecialfx.com/): a cross-platform rendering
|
||||
framework.
|
||||
* [Apparently](https://teamwisp.github.io/credits/)
|
||||
[Wisp](https://teamwisp.github.io/product/) by
|
||||
[Team Wisp](https://teamwisp.github.io/): an advanced real-time ray tracing
|
||||
renderer built for the demands of video game artists.
|
||||
* [starlight](https://github.com/DomRe/starlight): game programming framework
|
||||
using `Allegro`, `Lua` and modern C++.
|
||||
* [Apparently](https://github.com/JosiahWI/qub3d-libdeps)
|
||||
[Qub3d](https://qub3d.org/): because blocks should be open source.
|
||||
* [shiva](https://github.com/Milerius/shiva): modern C++ engine with
|
||||
modularity.
|
||||
|
||||
* Emulators:
|
||||
* [NovusCore](https://github.com/novuscore/NovusCore): A modern take on World
|
||||
of Warcraft emulation.
|
||||
|
||||
* Articles and blog posts
|
||||
* [Some posts](https://skypjack.github.io/tags/#entt) on my personal
|
||||
[blog](https://skypjack.github.io/) are about `EnTT`, for those who want to
|
||||
know **more** on this project.
|
||||
* [Space Battle: Huge edition](http://victor.madtriangles.com/code%20experiment/2018/06/11/post-ecs-battle-huge.html):
|
||||
huge space battle built entirely from scratch.
|
||||
* [Space Battle](https://github.com/vblanco20-1/ECS_SpaceBattle): huge space
|
||||
battle built on `UE4`.
|
||||
* [Experimenting with ECS in UE4](http://victor.madtriangles.com/code%20experiment/2018/03/25/post-ue4-ecs-battle.html):
|
||||
interesting article about `UE4` and `EnTT`.
|
||||
* [Implementing ECS architecture in UE4](https://forums.unrealengine.com/development-discussion/c-gameplay-programming/1449913-implementing-ecs-architecture-in-ue4-giant-space-battle):
|
||||
giant space battle.
|
||||
* [Conan Adventures (SFML and EnTT in C++)](https://leinnan.github.io/blog/conan-adventuressfml-and-entt-in-c.html):
|
||||
create projects in modern C++ using `SFML`, `EnTT`, `Conan` and `CMake`.
|
||||
|
||||
* Any Other Business:
|
||||
* The [ArcGIS Runtime SDKs](https://developers.arcgis.com/arcgis-runtime/)
|
||||
by [Esri](https://www.esri.com/): they use `EnTT` for the internal ECS and
|
||||
the cross platform C++ rendering engine. The SDKs are utilized by a lot of
|
||||
enterprise custom apps, as well as by Esri for its own public applications
|
||||
such as
|
||||
[Explorer](https://play.google.com/store/apps/details?id=com.esri.explorer),
|
||||
[Collector](https://play.google.com/store/apps/details?id=com.esri.arcgis.collector)
|
||||
and
|
||||
[Navigator](https://play.google.com/store/apps/details?id=com.esri.navigator).
|
||||
* [Apparently](https://www.linkedin.com/in/skypjack/)
|
||||
[NIO](https://www.nio.io/): there was a collaboration to make some changes
|
||||
to `EnTT`, at the time used for internal projects.
|
||||
* [MatchOneEntt](https://github.com/mhaemmerle/MatchOneEntt): port of
|
||||
[Match One](https://github.com/sschmid/Match-One) for `Entitas-CSharp`.
|
||||
* GitHub contains also
|
||||
[many other examples](https://github.com/search?o=desc&q=%22skypjack%2Fentt%22&s=indexed&type=Code)
|
||||
of use of `EnTT` from which to take inspiration if interested.
|
||||
|
||||
If you know of other resources out there that are about `EnTT`, feel free to
|
||||
open an issue or a PR and I'll be glad to add them to this page.
|
||||
@@ -32,13 +32,13 @@ initialize it. As an example:
|
||||
|
||||
```cpp
|
||||
// the service has no base type, a locator is used to treat it as a kind of singleton
|
||||
entt::ServiceLocator<MyService>::set(params...);
|
||||
entt::service_locator<my_service>::set(params...);
|
||||
|
||||
// sets up an opaque service
|
||||
entt::ServiceLocator<AudioInterface>::set<AudioImplementation>(params...);
|
||||
entt::service_locator<audio_interface>::set<audio_implementation>(params...);
|
||||
|
||||
// resets (destroys) the service
|
||||
entt::ServiceLocator<AudioInterface>::reset();
|
||||
entt::service_locator<audio_interface>::reset();
|
||||
```
|
||||
|
||||
The locator can also be queried to know if an active service is currently set
|
||||
@@ -46,30 +46,30 @@ and to retrieve it if necessary (either as a pointer or as a reference):
|
||||
|
||||
```cpp
|
||||
// no service currently set
|
||||
auto empty = entt::ServiceLocator<AudioInterface>::empty();
|
||||
auto empty = entt::service_locator<audio_interface>::empty();
|
||||
|
||||
// gets a (possibly empty) shared pointer to the service ...
|
||||
std::shared_ptr<AudioInterface> ptr = entt::ServiceLocator<AudioInterface>::get();
|
||||
std::shared_ptr<audio_interface> ptr = entt::service_locator<audio_interface>::get();
|
||||
|
||||
// ... or a reference, but it's undefined behaviour if the service isn't set yet
|
||||
AudioInterface &ref = entt::ServiceLocator<AudioInterface>::ref();
|
||||
audio_interface &ref = entt::service_locator<audio_interface>::ref();
|
||||
```
|
||||
|
||||
A common use is to wrap the different locators in a container class, creating
|
||||
aliases for the various services:
|
||||
|
||||
```cpp
|
||||
struct Locator {
|
||||
using Camera = entt::ServiceLocator<CameraInterface>;
|
||||
using Audio = entt::ServiceLocator<AudioInterface>;
|
||||
struct locator {
|
||||
using camera = entt::service_locator<camera_interface>;
|
||||
using audio = entt::service_locator<audio_interface>;
|
||||
// ...
|
||||
};
|
||||
|
||||
// ...
|
||||
|
||||
void init() {
|
||||
Locator::Camera::set<CameraNull>();
|
||||
Locator::Audio::set<AudioImplementation>(params...);
|
||||
locator::camera::set<camera_null>();
|
||||
locator::audio::set<audio_implementation>(params...);
|
||||
// ...
|
||||
}
|
||||
```
|
||||
414
docs/md/meta.md
Normal file
414
docs/md/meta.md
Normal file
@@ -0,0 +1,414 @@
|
||||
# Crash Course: reflection system
|
||||
|
||||
<!--
|
||||
@cond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
# Table of Contents
|
||||
|
||||
* [Introduction](#introduction)
|
||||
* [Reflection in a nutshell](#reflection-in-a-nutshell)
|
||||
* [Any as in any type](#any-as-in-any-type)
|
||||
* [Enjoy the runtime](#enjoy-the-runtime)
|
||||
* [Named constants and enums](#named-constants-and-enums)
|
||||
* [Properties and meta objects](#properties-and-meta-objects)
|
||||
* [Unregister types](#unregister-types)
|
||||
<!--
|
||||
@endcond TURN_OFF_DOXYGEN
|
||||
-->
|
||||
|
||||
# Introduction
|
||||
|
||||
Reflection (or rather, its lack) is a trending topic in the C++ world and, in
|
||||
the specific case of `EnTT`, a tool that can unlock a lot of other features. I
|
||||
looked for a third-party library that met my needs on the subject, but I always
|
||||
came across some details that I didn't like: macros, being intrusive, too many
|
||||
allocations. In one word: unsatisfactory.<br/>
|
||||
I finally decided to write a built-in, non-intrusive and macro-free runtime
|
||||
reflection system for `EnTT`. Maybe I didn't do better than others or maybe yes,
|
||||
time will tell me, but at least I can model this tool around the library to
|
||||
which it belongs and not vice versa.
|
||||
|
||||
# Reflection in a nutshell
|
||||
|
||||
Reflection always starts from real types (users cannot reflect imaginary types
|
||||
and it would not make much sense, we wouldn't be talking about reflection
|
||||
anymore).<br/>
|
||||
To _reflect_ a type, the library provides the `reflect` function:
|
||||
|
||||
```cpp
|
||||
auto factory = entt::reflect<my_type>("reflected_type");
|
||||
```
|
||||
|
||||
It accepts the type to reflect as a template parameter and an optional name as
|
||||
an argument. Names are important because users can retrieve meta types at
|
||||
runtime by searching for them by name. However, there are cases in which users
|
||||
can be interested in adding features to a reflected type so that the reflection
|
||||
system can use it correctly under the hood, but they don't want to allow
|
||||
searching the type by name.<br/>
|
||||
In both cases, the returned value is a factory object to use to continue
|
||||
building the meta type.
|
||||
|
||||
A factory is such that all its member functions returns the factory itself.
|
||||
It can be used to extend the reflected type and add the following:
|
||||
|
||||
* _Constructors_. Actual constructors can be assigned to a reflected type by
|
||||
specifying their list of arguments. Free functions (namely, factories) can be
|
||||
used as well, as long as the return type is the expected one. From a client's
|
||||
point of view, nothing changes if a constructor is a free function or an
|
||||
actual constructor.<br/>
|
||||
Use the `ctor` member function for this purpose:
|
||||
|
||||
```cpp
|
||||
entt::reflect<my_type>("reflected").ctor<int, char>().ctor<&factory>();
|
||||
```
|
||||
|
||||
* _Destructors_. Free functions can be set as destructors of reflected types.
|
||||
The purpose is to give users the ability to free up resources that require
|
||||
special treatment before an object is actually destroyed.<br/>
|
||||
Use the `dtor` member function for this purpose:
|
||||
|
||||
```cpp
|
||||
entt::reflect<my_type>("reflected").dtor<&destroy>();
|
||||
```
|
||||
|
||||
* _Data members_. Both real data members of the underlying type and static and
|
||||
global variables, as well as constants of any kind, can be attached to a meta
|
||||
type. From a client's point of view, all the variables associated with the
|
||||
reflected type will appear as if they were part of the type itself.<br/>
|
||||
Use the `data` member function for this purpose:
|
||||
|
||||
```cpp
|
||||
entt::reflect<my_type>("reflected")
|
||||
.data<&my_type::static_variable>("static")
|
||||
.data<&my_type::data_member>("member")
|
||||
.data<&global_variable>("global");
|
||||
```
|
||||
|
||||
This function requires as an argument the name to give to the meta data once
|
||||
created. Users can then access meta data at runtime by searching for them by
|
||||
name.<br/>
|
||||
Data members can be set also by means of a couple of functions, namely a
|
||||
setter and a getter. Setters and getters can be either free functions, member
|
||||
functions or mixed ones, as long as they respect the required signatures.<br/>
|
||||
Refer to the inline documentation for all the details.
|
||||
|
||||
* _Member functions_. Both real member functions of the underlying type and free
|
||||
functions can be attached to a meta type. From a client's point of view, all
|
||||
the functions associated with the reflected type will appear as if they were
|
||||
part of the type itself.<br/>
|
||||
Use the `func` member function for this purpose:
|
||||
|
||||
```cpp
|
||||
entt::reflect<my_type>("reflected")
|
||||
.func<&my_type::static_function>("static")
|
||||
.func<&my_type::member_function>("member")
|
||||
.func<&free_function>("free");
|
||||
```
|
||||
|
||||
This function requires as an argument the name to give to the meta function
|
||||
once created. Users can then access meta functions at runtime by searching for
|
||||
them by name.
|
||||
|
||||
* _Base classes_. A base class is such that the underlying type is actually
|
||||
derived from it. In this case, the reflection system tracks the relationship
|
||||
and allows for implicit casts at runtime when required.<br/>
|
||||
Use the `base` member function for this purpose:
|
||||
|
||||
```cpp
|
||||
entt::reflect<derived_type>("derived").base<base_type>();
|
||||
```
|
||||
|
||||
From now on, wherever a `base_type` is required, an instance of `derived_type`
|
||||
will also be accepted.
|
||||
|
||||
* _Conversion functions_. Actual types can be converted, this is a fact. Just
|
||||
think of the relationship between a `double` and an `int` to see it. Similar
|
||||
to bases, conversion functions allow users to define conversions that will be
|
||||
implicitly performed by the reflection system when required.<br/>
|
||||
Use the `conv` member function for this purpose:
|
||||
|
||||
```cpp
|
||||
entt::reflect<double>().conv<int>();
|
||||
```
|
||||
|
||||
That's all, everything users need to create meta types and enjoy the reflection
|
||||
system. At first glance it may not seem that much, but users usually learn to
|
||||
appreciate it over time.<br/>
|
||||
Also, do not forget what these few lines hide under the hood: a built-in,
|
||||
non-intrusive and macro-free system for reflection in C++. Features that are
|
||||
definitely worth the price, at least for me.
|
||||
|
||||
# Any as in any type
|
||||
|
||||
The reflection system comes with its own meta any type. It may seem redundant
|
||||
since C++17 introduced `std::any`, but it is not.<br/>
|
||||
In fact, the _type_ returned by an `std::any` is a const reference to an
|
||||
`std::type_info`, an implementation defined class that's not something everyone
|
||||
wants to see in a software. Furthermore, the class `std::type_info` suffers from
|
||||
some design flaws and there is even no way to _convert_ an `std::type_info` into
|
||||
a meta type, thus linking the two worlds.
|
||||
|
||||
A meta any object provides an API similar to that of its most famous counterpart
|
||||
and serves the same purpose of being an opaque container for any type of
|
||||
value.<br/>
|
||||
It minimizes the allocations required, which are almost absent thanks to _SBO_
|
||||
techniques. In fact, unless users deal with _fat types_ and create instances of
|
||||
them though the reflection system, allocations are at zero.
|
||||
|
||||
A meta any object can be created by any other object or as an empty container
|
||||
to initialize later:
|
||||
|
||||
```cpp
|
||||
// a meta any object that contains an int
|
||||
entt::meta_any any{0};
|
||||
|
||||
// an empty meta any object
|
||||
entt::meta_any empty{};
|
||||
```
|
||||
|
||||
It can be constructed or assigned by copy and move and it takes the burden of
|
||||
destroying the contained object when required.<br/>
|
||||
A meta any object has a `type` member function that returns the meta type of the
|
||||
contained value, if any. The member functions `can_cast` and `can_convert` are
|
||||
used to know if the underlying object has a given type as a base or if it can be
|
||||
converted implicitly to it. Similarly, `cast` and `convert` do what they promise
|
||||
and return the expected value.
|
||||
|
||||
# Enjoy the runtime
|
||||
|
||||
Once the web of reflected types has been constructed, it's a matter of using it
|
||||
at runtime where required.<br/>
|
||||
All this has the great merit that, unlike the vast majority of the things
|
||||
present in this library and closely linked to the compile-time, the reflection
|
||||
system stands in fact as a non-intrusive tool for the runtime.
|
||||
|
||||
To search for a reflected type there are two options: by type or by name. In
|
||||
both cases, the search can be done by means of the `resolve` function:
|
||||
|
||||
```cpp
|
||||
// search for a reflected type by type
|
||||
auto by_type = entt::resolve<my_type>();
|
||||
|
||||
// search for a reflected type by name
|
||||
auto by_name = entt::resolve("reflected_type");
|
||||
```
|
||||
|
||||
There exits also a third overload of the `resolve` function to use to iterate
|
||||
all the reflected types at once:
|
||||
|
||||
```cpp
|
||||
resolve([](auto type) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
In all cases, the returned value is an instance of `meta_type`. This type of
|
||||
objects offer an API to know the _runtime name_ of the type, to iterate all the
|
||||
meta objects associated with them and even to build or destroy instances of the
|
||||
underlying type.<br/>
|
||||
Refer to the inline documentation for all the details.
|
||||
|
||||
The meta objects that compose a meta type are accessed in the following ways:
|
||||
|
||||
* _Meta constructors_. They are accessed by types of arguments:
|
||||
|
||||
```cpp
|
||||
auto ctor = entt::resolve<my_type>().ctor<int, char>();
|
||||
```
|
||||
|
||||
The returned type is `meta_ctor` and may be invalid if there is no constructor
|
||||
that accepts the supplied arguments or at least some types from which they are
|
||||
derived or to which they can be converted.<br/>
|
||||
A meta constructor offers an API to know the number of arguments, the expected
|
||||
meta types and to invoke it, therefore to construct a new instance of the
|
||||
underlying type.
|
||||
|
||||
* _Meta destructor_. It's returned by a dedicated function:
|
||||
|
||||
```cpp
|
||||
auto dtor = entt::resolve<my_type>().dtor();
|
||||
```
|
||||
|
||||
The returned type is `meta_dtor` and may be invalid if there is no custom
|
||||
destructor set for the given meta type.<br/>
|
||||
All what a meta destructor has to offer is a way to invoke it on a given
|
||||
instance. Be aware that the result may not be what is expected.
|
||||
|
||||
* _Meta data_. They are accessed by name:
|
||||
|
||||
```cpp
|
||||
auto data = entt::resolve<my_type>().data("member");
|
||||
```
|
||||
|
||||
The returned type is `meta_data` and may be invalid if there is no meta data
|
||||
object associated with the given name.<br/>
|
||||
A meta data object offers an API to query the underlying type (ie to know if
|
||||
it's a const or a static one), to get the meta type of the variable and to set
|
||||
or get the contained value.
|
||||
|
||||
* _Meta functions_. They are accessed by name:
|
||||
|
||||
```cpp
|
||||
auto func = entt::resolve<my_type>().func("member");
|
||||
```
|
||||
|
||||
The returned type is `meta_func` and may be invalid if there is no meta
|
||||
function object associated with the given name.<br/>
|
||||
A meta function object offers an API to query the underlying type (ie to know
|
||||
if it's a const or a static function), to know the number of arguments, the
|
||||
meta return type and the meta types of the parameters. In addition, a meta
|
||||
function object can be used to invoke the underlying function and then get the
|
||||
return value in the form of meta any object.
|
||||
|
||||
|
||||
* _Meta bases_. They are accessed through the name of the base types:
|
||||
|
||||
```cpp
|
||||
auto base = entt::resolve<derived_type>().base("base");
|
||||
```
|
||||
|
||||
The returned type is `meta_base` and may be invalid if there is no meta base
|
||||
object associated with the given name.<br/>
|
||||
Meta bases aren't meant to be used directly, even though they are freely
|
||||
accessible. They expose only a few methods to use to know the meta type of the
|
||||
base class and to convert a raw pointer between types.
|
||||
|
||||
* _Meta conversion functions_. They are accessed by type:
|
||||
|
||||
```cpp
|
||||
auto conv = entt::resolve<double>().conv<int>();
|
||||
```
|
||||
|
||||
The returned type is `meta_conv` and may be invalid if there is no meta
|
||||
conversion function associated with the given type.<br/>
|
||||
The meta conversion functions are as thin as the meta bases and with a very
|
||||
similar interface. The sole difference is that they return a newly created
|
||||
instance wrapped in a meta any object when they convert between different
|
||||
types.
|
||||
|
||||
All the objects thus obtained as well as the meta types can be explicitly
|
||||
converted to a boolean value to check if they are valid:
|
||||
|
||||
```cpp
|
||||
auto func = entt::resolve<my_type>().func("member");
|
||||
|
||||
if(func) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Furthermore, all meta objects with the exception of meta destructors can be
|
||||
iterated through an overload that accepts a callback through which to return
|
||||
them. As an example:
|
||||
|
||||
```cpp
|
||||
entt::resolve<my_type>().data([](auto data) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
A meta type can also be used to `construct` or `destroy` actual instances of the
|
||||
underlying type.<br/>
|
||||
In particular, the `construct` member function accepts a variable number of
|
||||
arguments and searches for a match. It returns a `meta_any` object that may or
|
||||
may not be initialized, depending on whether a suitable constructor has been
|
||||
found or not. On the other side, the `destroy` member function accepts instances
|
||||
of `meta_any` as well as actual objects by reference and invokes the registered
|
||||
destructor if any or a default one.<br/>
|
||||
Be aware that the result of a call to `destroy` may not be what is expected.
|
||||
|
||||
Meta types and meta objects in general contain much more than what is said: a
|
||||
plethora of functions in addition to those listed whose purposes and uses go
|
||||
unfortunately beyond the scope of this document.<br/>
|
||||
I invite anyone interested in the subject to look at the code, experiment and
|
||||
read the official documentation to get the best out of this powerful tool.
|
||||
|
||||
# Named constants and enums
|
||||
|
||||
A special mention should be made for constant values and enums. It wouldn't be
|
||||
necessary, but it will help distracted readers.
|
||||
|
||||
As mentioned, the `data` member function can be used to reflect constants of any
|
||||
type among the other things.<br/>
|
||||
This allows users to create meta types for enums that will work exactly like any
|
||||
other meta type built from a class. Similarly, arithmetic types can be enriched
|
||||
with constants of special meaning where required.<br/>
|
||||
Personally, I find it very useful not to export what is the difference between
|
||||
enums and classes in C++ directly in the space of the reflected types.
|
||||
|
||||
All the values thus exported will appear to users as if they were constant data
|
||||
members of the reflected types.
|
||||
|
||||
Exporting constant values or elements from an enum is as simple as ever:
|
||||
|
||||
```cpp
|
||||
entt::reflect<my_enum>()
|
||||
.data<my_enum::a_value>("a_value")
|
||||
.data<my_enum::another_value>("another_value");
|
||||
|
||||
entt::reflect<int>().data<2048>("max_int");
|
||||
```
|
||||
|
||||
It goes without saying that accessing them is trivial as well. It's a matter of
|
||||
doing the following, as with any other data member of a meta type:
|
||||
|
||||
```cpp
|
||||
auto value = entt::resolve<my_enum>().data("a_value").get({}).cast<my_enum>();
|
||||
auto max = entt::resolve<int>().data("max_int").get({}).cast<int>();
|
||||
```
|
||||
|
||||
As a side note, remember that all this happens behind the scenes without any
|
||||
allocation because of the small object optimization performed by the meta any
|
||||
class.
|
||||
|
||||
# Properties and meta objects
|
||||
|
||||
Sometimes (ie when it comes to creating an editor) it might be useful to be able
|
||||
to attach properties to the meta objects created. Fortunately, this is possible
|
||||
for most of them.<br/>
|
||||
To attach a property to a meta object, no matter what as long as it supports
|
||||
properties, it is sufficient to provide an object at the time of construction
|
||||
such that `std::get<0>` and `std::get<1>` are valid for it. In other terms, the
|
||||
properties are nothing more than key/value pairs users can put in an
|
||||
`std::pair`. As an example:
|
||||
|
||||
```cpp
|
||||
entt::reflect<my_type>("reflected", std::make_pair("tooltip"_hs, "message"));
|
||||
```
|
||||
|
||||
The meta objects that support properties offer then a couple of member functions
|
||||
named `prop` to iterate them at once and to search a specific property by key:
|
||||
|
||||
```cpp
|
||||
// iterate all the properties of a meta type
|
||||
entt::resolve<my_type>().prop([](auto prop) {
|
||||
// ...
|
||||
});
|
||||
|
||||
// search for a given property by name
|
||||
auto prop = entt::resolve<my_type>().prop("tooltip"_hs);
|
||||
```
|
||||
|
||||
Meta properties are objects having a fairly poor interface, all in all. They
|
||||
only provide the `key` and the `value` member functions to be used to retrieve
|
||||
the key and the value contained in the form of meta any objects, respectively.
|
||||
|
||||
# Unregister types
|
||||
|
||||
A type registered with the reflection system can also be unregistered. This
|
||||
means unregistering all its data members, member functions, conversion functions
|
||||
and so on. However, the base classes won't be unregistered, since they don't
|
||||
necessarily depend on it. Similarly, implicitly generated types (as an example,
|
||||
the meta types implicitly generated for function parameters when needed) won't
|
||||
be unregistered.
|
||||
|
||||
To unregister a type, users can use the `unregister` function from the global
|
||||
namespace:
|
||||
|
||||
```cpp
|
||||
entt::unregister<my_type>();
|
||||
```
|
||||
|
||||
This function returns a boolean value that is true if the type is actually
|
||||
registered with the reflection system, false otherwise.<br/>
|
||||
The type can be re-registered later with a completely different name and form.
|
||||
@@ -24,7 +24,7 @@ that users can use to define and execute cooperative processes.
|
||||
|
||||
# The process
|
||||
|
||||
A typical process must inherit from the `Process` class template that stays true
|
||||
A typical process must inherit from the `process` class template that stays true
|
||||
to the CRTP idiom. Moreover, derived classes must specify what's the intended
|
||||
type for elapsed times.
|
||||
|
||||
@@ -40,11 +40,12 @@ class wants to _override_ the default behavior):
|
||||
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.
|
||||
|
||||
* `void init(void *);`
|
||||
* `void init();`
|
||||
|
||||
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.
|
||||
It's invoked when the process joins the running queue of a scheduler. This
|
||||
happens as soon as it's attached to the scheduler if the process is a top
|
||||
level one, otherwise when it replaces its parent if the process is a
|
||||
continuation.
|
||||
|
||||
* `void succeeded();`
|
||||
|
||||
@@ -70,11 +71,11 @@ life cycle of a process from a derived class.
|
||||
Here is a minimal example for the sake of curiosity:
|
||||
|
||||
```cpp
|
||||
struct MyProcess: entt::Process<MyProcess, std::uint32_t> {
|
||||
struct my_process: entt::process<my_process, std::uint32_t> {
|
||||
using delta_type = std::uint32_t;
|
||||
|
||||
void update(delta_type delta, void *) {
|
||||
remaining = delta > remaining ? delta_type{] : (remaining - delta);
|
||||
remaining -= std::min(remaining, delta);
|
||||
|
||||
// ...
|
||||
|
||||
@@ -83,12 +84,8 @@ struct MyProcess: entt::Process<MyProcess, std::uint32_t> {
|
||||
}
|
||||
}
|
||||
|
||||
void init(void *data) {
|
||||
remaining = *static_cast<delta_type *>(data);
|
||||
}
|
||||
|
||||
private:
|
||||
delta_type remaining;
|
||||
delta_type remaining{1000u};
|
||||
};
|
||||
```
|
||||
|
||||
@@ -127,17 +124,17 @@ cycles.
|
||||
|
||||
Each process is invoked once per tick. If it terminates, it's removed
|
||||
automatically from the scheduler and it's never invoked again. Otherwise it's
|
||||
a good candidate to run once more the next tick.<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. This way, it's easy to create
|
||||
chain of processes to run sequentially.
|
||||
a good candidate to run one more time the next tick.<br/>
|
||||
A process can also have a child. In this case, the parent process is replaced
|
||||
with its child when it terminates and only if it returns with success. In case
|
||||
of errors, both the parent process and its child are discarded. This way, it's
|
||||
easy to create chain of processes to run sequentially.
|
||||
|
||||
Using a scheduler is straightforward. To create it, users must provide only the
|
||||
type for the elapsed times and no arguments at all:
|
||||
|
||||
```cpp
|
||||
Scheduler<std::uint32_t> scheduler;
|
||||
entt::scheduler<std::uint32_t> scheduler;
|
||||
```
|
||||
|
||||
It has member functions to query its internal data structures, like `empty` or
|
||||
@@ -148,7 +145,7 @@ It has member functions to query its internal data structures, like `empty` or
|
||||
const auto empty = scheduler.empty();
|
||||
|
||||
// gets the number of processes still running
|
||||
Scheduler<std::uint32_t>::size_type size = scheduler.size();
|
||||
entt::scheduler<std::uint32_t>::size_type size = scheduler.size();
|
||||
|
||||
// resets the scheduler to its initial state and discards all the processes
|
||||
scheduler.clear();
|
||||
@@ -156,12 +153,12 @@ scheduler.clear();
|
||||
|
||||
To attach a process to a scheduler there are mainly two ways:
|
||||
|
||||
* If the process inherits from the `Process` class template, it's enough to
|
||||
* If the process inherits from the `process` class template, it's enough to
|
||||
indicate its type and submit all the parameters required to construct it to
|
||||
the `attach` member function:
|
||||
|
||||
```cpp
|
||||
scheduler.attach<MyProcess>("foobar");
|
||||
scheduler.attach<my_process>("foobar");
|
||||
```
|
||||
|
||||
* Otherwise, in case of a lambda or a functor, it's enough to provide an
|
||||
@@ -185,11 +182,11 @@ scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
|
||||
// ...
|
||||
})
|
||||
// appends a child in the form of a process class
|
||||
.then<MyProcess>();
|
||||
.then<my_process>();
|
||||
```
|
||||
|
||||
To update a scheduler and thus all its processes, the `update` member function
|
||||
is the way to go:
|
||||
To update a scheduler and therefore all its processes, the `update` member
|
||||
function is the way to go:
|
||||
|
||||
```cpp
|
||||
// updates all the processes, no user data are provided
|
||||
@@ -38,29 +38,29 @@ whatever. There are no limits.<br/>
|
||||
As a minimal example:
|
||||
|
||||
```cpp
|
||||
struct MyResource { const int value; };
|
||||
struct my_resource { const int value; };
|
||||
```
|
||||
|
||||
A _loader_ is a class the aim of which is to load a specific resource. It has to
|
||||
inherit directly from the dedicated base class as in the following example:
|
||||
|
||||
```cpp
|
||||
struct MyLoader final: entt::ResourceLoader<MyLoader, MyResource> {
|
||||
struct my_loader final: entt::resource_loader<my_loader, my_resource> {
|
||||
// ...
|
||||
};
|
||||
```
|
||||
|
||||
Where `MyResource` is the type of resources it creates.<br/>
|
||||
Where `my_resource` is the type of resources it creates.<br/>
|
||||
A resource loader must also expose a public const member function named `load`
|
||||
that accepts a variable number of arguments and returns a shared pointer to a
|
||||
resource.<br/>
|
||||
As an example:
|
||||
|
||||
```cpp
|
||||
struct MyLoader: entt::ResourceLoader<MyLoader, MyResource> {
|
||||
std::shared_ptr<MyResource> load(int value) const {
|
||||
struct my_loader: entt::resource_loader<my_loader, my_resource> {
|
||||
std::shared_ptr<my_resource> load(int value) const {
|
||||
// ...
|
||||
return std::shared_ptr<MyResource>(new MyResource{ value });
|
||||
return std::shared_ptr<my_resource>(new my_resource{ value });
|
||||
}
|
||||
};
|
||||
```
|
||||
@@ -76,15 +76,15 @@ Finally, a cache is a specialization of a class template tailored to a specific
|
||||
resource:
|
||||
|
||||
```cpp
|
||||
using MyResourceCache = entt::ResourceCache<MyResource>;
|
||||
using my_resource_cache = entt::resource_cache<my_resource>;
|
||||
|
||||
// ...
|
||||
|
||||
MyResourceCache cache{};
|
||||
my_resource_cache cache{};
|
||||
```
|
||||
|
||||
The idea is to create different caches for different types of resources and to
|
||||
manage each one independently and in the most appropriate way.<br/>
|
||||
manage each one independently in the most appropriate way.<br/>
|
||||
As a (very) trivial example, audio tracks can survive in most of the scenes of
|
||||
an application while meshes can be associated with a single scene and then
|
||||
discarded when users leave it.
|
||||
@@ -103,25 +103,26 @@ const auto empty = cache.empty();
|
||||
cache.clear();
|
||||
```
|
||||
|
||||
Besides these member functions, it contains what is needed to load, use and
|
||||
Besides these member functions, a cache contains what is needed to load, use and
|
||||
discard resources of the given type.<br/>
|
||||
Before to explore this part of the interface, it makes sense to mention how
|
||||
resources are identified. The type of the identifiers to use is defined as:
|
||||
|
||||
```cpp
|
||||
entt::ResourceCache<Resource>::resource_type
|
||||
entt::resource_cache<resource>::resource_type
|
||||
```
|
||||
|
||||
Where `resource_type` is an alias for `entt::HashedString`. Therefore, resource
|
||||
identifiers are created explicitly as in the following example:
|
||||
Where `resource_type` is an alias for `entt::hashed_string::hash_type`.
|
||||
Therefore, resource identifiers are created explicitly as in the following
|
||||
example:
|
||||
|
||||
```cpp
|
||||
constexpr auto identifier = entt::ResourceCache<Resource>::resource_type{"my/resource/identifier"};
|
||||
constexpr auto identifier = entt::resource_cache<resource>::resource_type{"my/resource/identifier"_hs};
|
||||
// this is equivalent to the following
|
||||
constexpr auto hs = entt::HashedString{"my/resource/identifier"};
|
||||
constexpr auto hs = entt::hashed_string{"my/resource/identifier"};
|
||||
```
|
||||
|
||||
The class `HashedString` is described in a dedicated section, so I won't do in
|
||||
The class `hashed_string` is described in a dedicated section, so I won't go in
|
||||
details here.
|
||||
|
||||
Resources are loaded and thus stored in a cache through the `load` member
|
||||
@@ -130,66 +131,62 @@ identifier and the parameters used to construct the resource as arguments:
|
||||
|
||||
```cpp
|
||||
// uses the identifier declared above
|
||||
cache.load<MyLoader>(identifier, 0);
|
||||
cache.load<my_loader>(identifier, 0);
|
||||
|
||||
// uses a const char * directly as an identifier
|
||||
cache.load<MyLoader>("another/identifier", 42);
|
||||
cache.load<my_loader>("another/identifier"_hs, 42);
|
||||
```
|
||||
|
||||
The return value can be used to know if the resource has been loaded correctly.
|
||||
In case the loader returns an invalid pointer or the resource already exists in
|
||||
the cache, a false value is returned:
|
||||
The function returns a handle to the resource, whether it already exists or is
|
||||
loaded. In case the loader returns an invalid pointer, the handle is invalid as
|
||||
well and therefore it can be easily used with an `if` statement:
|
||||
|
||||
```cpp
|
||||
if(!cache.load<MyLoader>("another/identifier", 42)) {
|
||||
if(auto handle = cache.load<my_loader>("another/identifier"_hs, 42); handle) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Unfortunately, in this case there is no way to know what was the problem
|
||||
exactly. However, before trying to load a resource or after an error, one can
|
||||
use the `contains` member function to know if a cache already contains a
|
||||
specific resource:
|
||||
Before trying to load a resource, the `contains` member function can be used to
|
||||
know if a cache already contains a specific resource:
|
||||
|
||||
```cpp
|
||||
auto exists = cache.contains("my/identifier");
|
||||
auto exists = cache.contains("my/identifier"_hs);
|
||||
```
|
||||
|
||||
There exists also a member function to use to force a reload of an already
|
||||
existing resource if needed:
|
||||
|
||||
```cpp
|
||||
auto result = cache.reload<MyLoader>("another/identifier", 42);
|
||||
auto handle = cache.reload<my_loader>("another/identifier"_hs, 42);
|
||||
```
|
||||
|
||||
As above, the function returns true in case of success, false otherwise. The
|
||||
sole difference in this case is that an error necessarily means that the loader
|
||||
has failed for some reasons to load the resource.<br/>
|
||||
Note that the `reload` member function is a kind of alias of the following
|
||||
As above, the function returns a handle to the resource that is invalid in case
|
||||
of errors. The `reload` member function is a kind of alias of the following
|
||||
snippet:
|
||||
|
||||
```cpp
|
||||
cache.discard(identifier);
|
||||
cache.load<MyLoader>(identifier, 42);
|
||||
cache.load<my_loader>(identifier, 42);
|
||||
```
|
||||
|
||||
Where the `discard` member function is used to get rid of a resource if loaded.
|
||||
In case the cache doesn't contain a resource for the given identifier, the
|
||||
function does nothing and returns immediately.
|
||||
In case the cache doesn't contain a resource for the given identifier, `discard`
|
||||
does nothing and returns immediately.
|
||||
|
||||
So far, so good. Resources are finally loaded and stored within the cache.<br/>
|
||||
They are returned to users in the form of handles. To get one of them:
|
||||
They are returned to users in the form of handles. To get one of them later on:
|
||||
|
||||
```cpp
|
||||
auto handle = cache.handle("my/identifier");
|
||||
auto handle = cache.handle("my/identifier"_hs);
|
||||
```
|
||||
|
||||
The idea behind a handle is the same of the flyweight pattern. In other terms,
|
||||
resources aren't copied around. Instead, instances are shared between handles.
|
||||
Users of a resource owns a handle and it guarantees that a resource isn't
|
||||
destroyed until all the handles are destroyed, even if the resource itself is
|
||||
removed from the cache.<br/>
|
||||
Handles are tiny objects both movable and copyable. They returns the contained
|
||||
Users of a resource own a handle that guarantees that a resource isn't destroyed
|
||||
until all the handles are destroyed, even if the resource itself is removed from
|
||||
the cache.<br/>
|
||||
Handles are tiny objects both movable and copyable. They return the contained
|
||||
resource as a const reference on request:
|
||||
|
||||
* By means of the `get` member function:
|
||||
@@ -228,13 +225,14 @@ if(handle) {
|
||||
Finally, in case there is the need to load a resource and thus to get a handle
|
||||
without storing the resource itself in the cache, users can rely on the `temp`
|
||||
member function template.<br/>
|
||||
The declaration is similar to the one of `load` but for the fact that it doesn't
|
||||
return a boolean value. Instead, it returns a (possibly invalid) handle for the
|
||||
resource:
|
||||
The declaration is similar to that of `load`, a (possibly invalid) handle for
|
||||
the resource is returned also in this case:
|
||||
|
||||
```cpp
|
||||
auto handle = cache.temp<MyLoader>("another/identifier", 42);
|
||||
if(auto handle = cache.temp<my_loader>(42); handle) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Do not forget to test the handle for validity. Otherwise, getting the reference
|
||||
to the resource it points may result in undefined behavior.
|
||||
Do not forget to test the handle for validity. Otherwise, getting a reference to
|
||||
the resource it points may result in undefined behavior.
|
||||
@@ -6,8 +6,8 @@
|
||||
# Table of Contents
|
||||
|
||||
* [Introduction](#introduction)
|
||||
* [Signals](#signals)
|
||||
* [Delegate](#delegate)
|
||||
* [Signals](#signals)
|
||||
* [Event dispatcher](#event-dispatcher)
|
||||
* [Event emitter](#event-emitter)
|
||||
<!--
|
||||
@@ -32,6 +32,102 @@ In case that the flexibility and potential of an `std::function` are not
|
||||
required or where you are looking for something different, `EnTT` offers a full
|
||||
set of classes to solve completely different problems.
|
||||
|
||||
# Delegate
|
||||
|
||||
A delegate can be used as a general purpose invoker with no memory overhead for
|
||||
free functions and members provided along with an instance on which to invoke
|
||||
them.<br/>
|
||||
It does not claim to be a drop-in replacement for an `std::function`, so do not
|
||||
expect to use it whenever an `std::function` fits well. However, it can be used
|
||||
to send opaque delegates around to be used to invoke functions as needed.
|
||||
|
||||
The interface is trivial. It offers a default constructor to create empty
|
||||
delegates:
|
||||
|
||||
```cpp
|
||||
entt::delegate<int(int)> delegate{};
|
||||
```
|
||||
|
||||
All what is needed to create an instance is to specify the type of the function
|
||||
the delegate will _contain_, that is the signature of the free function or the
|
||||
member function one wants to assign to it.
|
||||
|
||||
Attempting to use an empty delegate by invoking its function call operator
|
||||
results in undefined behavior or most likely a crash. Before to use a delegate,
|
||||
it must be initialized.<br/>
|
||||
There exists a bunch of overloads of the `connect` member function to do that.
|
||||
As an example of use:
|
||||
|
||||
```cpp
|
||||
int f(int i) { return i; }
|
||||
|
||||
struct my_struct {
|
||||
int f(const int &i) { return i }
|
||||
};
|
||||
|
||||
// bind a free function to the delegate
|
||||
delegate.connect<&f>();
|
||||
|
||||
// bind a member function to the delegate
|
||||
my_struct instance;
|
||||
delegate.connect<&my_struct::f>(&instance);
|
||||
```
|
||||
|
||||
The delegate class accepts also data members, if needed. In this case, the
|
||||
function type of the delegate is such that the parameter list is empty and the
|
||||
value of the data member is at least convertible to the return type.<br/>
|
||||
Functions having type equivalent to `void(T *, args...)` are accepted as well.
|
||||
In this case, `T *` is considered a payload and the function will receive it
|
||||
back every time it's invoked. In other terms, this works just fine with the
|
||||
above definition:
|
||||
|
||||
```cpp
|
||||
void g(const char *c, int i) { /* ... */ }
|
||||
const char c = 'c';
|
||||
|
||||
delegate.connect<&g>(&c);
|
||||
delegate(42);
|
||||
```
|
||||
|
||||
The function `g` will be invoked with a pointer to `c` and `42`. However, the
|
||||
function type of the delegate is still `void(int)`, mainly because this is also
|
||||
the signature of its function call operator.
|
||||
|
||||
To create and initialize a delegate at once, there are also some specialized
|
||||
constructors. Because of the rules of the language, the listener is provided by
|
||||
means of the `entt::connect_arg` variable template:
|
||||
|
||||
```cpp
|
||||
entt::delegate<int(int)> func{entt::connect_arg<&f>};
|
||||
```
|
||||
|
||||
Aside `connect`, a `disconnect` counterpart isn't provided. Instead, there
|
||||
exists a `reset` member function to use to clear a delegate.<br/>
|
||||
To know if a delegate is empty, it can be used explicitly in every conditional
|
||||
statement:
|
||||
|
||||
```cpp
|
||||
if(delegate) {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
|
||||
Finally, to invoke a delegate, the function call operator is the way to go as
|
||||
usual:
|
||||
|
||||
```cpp
|
||||
auto ret = delegate(42);
|
||||
```
|
||||
|
||||
As shown above, listeners do not have to strictly follow the signature of the
|
||||
delegate. As long as a listener can be invoked with the given arguments to yield
|
||||
a result that is convertible to the given result type, everything works just
|
||||
fine.
|
||||
|
||||
Probably too much small and pretty poor of functionalities, but the delegate
|
||||
class can help in a lot of cases and it has shown that it is worth keeping it
|
||||
within the library.
|
||||
|
||||
# Signals
|
||||
|
||||
Signal handlers work with naked pointers, function pointers and pointers to
|
||||
@@ -57,10 +153,10 @@ To create instances of signal handlers there exist mainly two ways:
|
||||
|
||||
```cpp
|
||||
// no collector type
|
||||
entt::SigH<void(int, char)> signal;
|
||||
entt::sigh<void(int, char)> signal;
|
||||
|
||||
// explicit collector type
|
||||
entt::SigH<void(int, char), MyCollector<bool>> collector;
|
||||
entt::sigh<void(int, char), my_collector<bool>> collector;
|
||||
```
|
||||
|
||||
As expected, they offer all the basic functionalities required to know how many
|
||||
@@ -73,32 +169,33 @@ listeners in all their forms by means of a sink:
|
||||
```cpp
|
||||
void foo(int, char) { /* ... */ }
|
||||
|
||||
struct S {
|
||||
void bar(int, char) { /* ... */ }
|
||||
struct listener {
|
||||
void bar(const int &, char) { /* ... */ }
|
||||
};
|
||||
|
||||
// ...
|
||||
|
||||
S instance;
|
||||
listener instance;
|
||||
|
||||
signal.sink().connect<&foo>();
|
||||
signal.sink().connect<S, &S::bar>(&instance);
|
||||
signal.sink().connect<&listener::bar>(&instance);
|
||||
|
||||
// ...
|
||||
|
||||
// disconnects a free function
|
||||
signal.sink().disconnect<&foo>();
|
||||
|
||||
// disconnect a specific member function of an instance ...
|
||||
signal.sink().disconnect<S, &S::bar>(&instance);
|
||||
|
||||
// ... or an instance as a whole
|
||||
signal.sink().disconnect(&instance);
|
||||
// disconnect a member function of an instance
|
||||
signal.sink().disconnect<&listener::bar>(&instance);
|
||||
|
||||
// discards all the listeners at once
|
||||
signal.sink().disconnect();
|
||||
```
|
||||
|
||||
As shown above, listeners do not have to strictly follow the signature of the
|
||||
signal. As long as a listener can be invoked with the given arguments to yield a
|
||||
result that is convertible to the given result type, everything works just fine.
|
||||
|
||||
Once listeners are attached (or even if there are no listeners at all), events
|
||||
and data in general can be published through a signal by means of the `publish`
|
||||
member function:
|
||||
@@ -111,7 +208,7 @@ To collect data, the `collect` member function should be used instead. Below is
|
||||
a minimal example to show how to use it:
|
||||
|
||||
```cpp
|
||||
struct MyCollector {
|
||||
struct my_collector {
|
||||
std::vector<int> vec{};
|
||||
|
||||
bool operator()(int v) noexcept {
|
||||
@@ -125,81 +222,21 @@ int g() { return 1; }
|
||||
|
||||
// ...
|
||||
|
||||
entt::SigH<int(), MyCollector<int>> signal;
|
||||
entt::sigh<int(), my_collector<int>> signal;
|
||||
|
||||
signal.sink().connect<&f>();
|
||||
signal.sink().connect<&g>();
|
||||
|
||||
MyCollector collector = signal.collect();
|
||||
my_collector collector = signal.collect();
|
||||
|
||||
assert(collector.vec[0] == 0);
|
||||
assert(collector.vec[1] == 1);
|
||||
```
|
||||
|
||||
As shown above, a collector must expose a function operator that accepts as an
|
||||
argument a type to which the return type of the listeners can be converted.
|
||||
Moreover, it has to return a boolean value that is false to stop collecting
|
||||
data, true otherwise. This way one can avoid calling all the listeners in case
|
||||
it isn't necessary.
|
||||
|
||||
# Delegate
|
||||
|
||||
A delegate can be used as general purpose invoker with no memory overhead for
|
||||
free functions and member functions provided along with an instance on which
|
||||
to invoke them.<br/>
|
||||
It does not claim to be a drop-in replacement for an `std::function`, so do not
|
||||
expect to use it whenever an `std::function` fits well. However, it can be used
|
||||
to send opaque delegates around to be used to invoke functions as needed.
|
||||
|
||||
The interface is trivial. It offers a default constructor to create empty
|
||||
delegates:
|
||||
|
||||
```cpp
|
||||
entt::Delegate<int(int)> delegate{};
|
||||
```
|
||||
|
||||
All what is needed to create an instance is to specify the type of the function
|
||||
the delegate will _contain_, that is the signature of the free function or the
|
||||
member function one wants to assign to it.
|
||||
|
||||
Attempting to use an empty delegate by invoking its function call operator
|
||||
results in undefined behavior, most likely a crash actually. Before to use a
|
||||
delegate, it must be initialized.<br/>
|
||||
There exist two functions to do that, both named `connect`:
|
||||
|
||||
```cpp
|
||||
int f(int i) { return i; }
|
||||
|
||||
struct MyStruct {
|
||||
int f(int i) { return i }
|
||||
};
|
||||
|
||||
// bind a free function to the delegate
|
||||
delegate.connect<&f>();
|
||||
|
||||
// bind a member function to the delegate
|
||||
MyStruct instance;
|
||||
delegate.connect<MyStruct, &MyStruct::f>(&instance);
|
||||
```
|
||||
|
||||
It hasn't a `disconnect` counterpart. Instead, there exists a `reset` member
|
||||
function to clear it.<br/>
|
||||
The `empty` member function can be used to know if a delegate is empty:
|
||||
|
||||
```cpp
|
||||
const auto empty = delegate.empty();
|
||||
```
|
||||
|
||||
Finally, to invoke a delegate, the function call operator is the way to go as
|
||||
usual:
|
||||
|
||||
```cpp
|
||||
auto ret = delegate(42);
|
||||
```
|
||||
|
||||
Probably too much small and pretty poor of functionalities, but the delegate
|
||||
class can help in a lot of cases and it has shown that it is worth keeping it
|
||||
within the library.
|
||||
A collector must expose a function operator that accepts as an argument a type
|
||||
to which the return type of the listeners can be converted. Moreover, it has to
|
||||
return a boolean value that is false to stop collecting data, true otherwise.
|
||||
This way one can avoid calling all the listeners in case it isn't necessary.
|
||||
|
||||
# Event dispatcher
|
||||
|
||||
@@ -211,41 +248,39 @@ doesn't require that all the types of events are specified when declared:
|
||||
|
||||
```cpp
|
||||
// define a general purpose dispatcher that works with naked pointers
|
||||
entt::Dispatcher dispatcher{};
|
||||
entt::dispatcher dispatcher{};
|
||||
```
|
||||
|
||||
In order to register an instance of a class to a dispatcher, its type must
|
||||
expose one or more member functions of which the return types are `void` and the
|
||||
argument lists are `const E &`, for each type of event `E`.<br/>
|
||||
To ease the development, member functions that are named `receive` are
|
||||
automatically detected and have not to be explicitly specified when registered.
|
||||
In all the other cases, the name of the member function aimed to receive the
|
||||
event must be provided to the `connect` member function of the sink bound to the
|
||||
specific event:
|
||||
expose one or more member functions the arguments of which are such that
|
||||
`const E &` can be converted to them for each type of event `E`, no matter what
|
||||
the return value is.<br/>
|
||||
The name of the member function aimed to receive the event must be provided to
|
||||
the `connect` member function of the sink in charge for the specific event:
|
||||
|
||||
```cpp
|
||||
struct AnEvent { int value; };
|
||||
struct AnotherEvent {};
|
||||
struct an_event { int value; };
|
||||
struct another_event {};
|
||||
|
||||
struct Listener
|
||||
struct listener
|
||||
{
|
||||
void receive(const AnEvent &) { /* ... */ }
|
||||
void method(const AnotherEvent &) { /* ... */ }
|
||||
void receive(const an_event &) { /* ... */ }
|
||||
void method(const another_event &) { /* ... */ }
|
||||
};
|
||||
|
||||
// ...
|
||||
|
||||
Listener listener;
|
||||
dispatcher.sink<AnEvent>().connect(&listener);
|
||||
dispatcher.sink<AnotherEvent>().connect<Listener, &Listener::method>(&listener);
|
||||
listener listener;
|
||||
dispatcher.sink<an_event>().connect<&listener::receive>(&listener);
|
||||
dispatcher.sink<another_event>().connect<&listener::method>(&listener);
|
||||
```
|
||||
|
||||
The `disconnect` member function follows the same pattern and can be used to
|
||||
selectively remove listeners:
|
||||
|
||||
```cpp
|
||||
dispatcher.sink<AnEvent>().disconnect(&listener);
|
||||
dispatcher.sink<AnotherEvent>().disconnect<Listener, &Listener::method>(&listener);
|
||||
dispatcher.sink<an_event>().disconnect<&listener::receive>(&listener);
|
||||
dispatcher.sink<another_event>().disconnect<&listener::method>(&listener);
|
||||
```
|
||||
|
||||
The `trigger` member function serves the purpose of sending an immediate event
|
||||
@@ -256,8 +291,8 @@ it.<br/>
|
||||
As an example:
|
||||
|
||||
```cpp
|
||||
dispatcher.trigger<AnEvent>(42);
|
||||
dispatcher.trigger<AnotherEvent>();
|
||||
dispatcher.trigger<an_event>(42);
|
||||
dispatcher.trigger<another_event>();
|
||||
```
|
||||
|
||||
Listeners are invoked immediately, order of execution isn't guaranteed. This
|
||||
@@ -269,8 +304,8 @@ allows to maintain control over the moment they are sent to listeners. The
|
||||
signature of this method is more or less the same of `trigger`:
|
||||
|
||||
```cpp
|
||||
dispatcher.enqueue<AnEvent>(42);
|
||||
dispatcher.enqueue<AnotherEvent>();
|
||||
dispatcher.enqueue<an_event>(42);
|
||||
dispatcher.enqueue<another_event>();
|
||||
```
|
||||
|
||||
Events are stored aside until the `update` member function is invoked, then all
|
||||
@@ -278,7 +313,7 @@ the messages that are still pending are sent to the listeners at once:
|
||||
|
||||
```cpp
|
||||
// emits all the events of the given type at once
|
||||
dispatcher.update<MyEvent>();
|
||||
dispatcher.update<my_event>();
|
||||
|
||||
// emits all the events queued so far at once
|
||||
dispatcher.update();
|
||||
@@ -299,7 +334,7 @@ To create a custom emitter type, derived classes must inherit directly from the
|
||||
base class as:
|
||||
|
||||
```cpp
|
||||
struct MyEmitter: Emitter<MyEmitter> {
|
||||
struct my_emitter: emitter<my_emitter> {
|
||||
// ...
|
||||
}
|
||||
```
|
||||
@@ -320,14 +355,14 @@ the other side, an event emitter is movable but not copyable by default.
|
||||
To create new instances of an emitter, no arguments are required:
|
||||
|
||||
```cpp
|
||||
MyEmitter emitter{};
|
||||
my_emitter emitter{};
|
||||
```
|
||||
|
||||
Listeners must be movable and callable objects (free functions, lambdas,
|
||||
functors, `std::function`s, whatever) whose function type is:
|
||||
|
||||
```cpp
|
||||
void(const Event &, MyEmitter &)
|
||||
void(const Event &, my_emitter &)
|
||||
```
|
||||
|
||||
Where `Event` is the type of event they want to listen.<br/>
|
||||
@@ -340,7 +375,7 @@ slightly from each other:
|
||||
As an example:
|
||||
|
||||
```cpp
|
||||
auto conn = emitter.on<MyEvent>([](const MyEvent &event, MyEmitter &emitter) {
|
||||
auto conn = emitter.on<my_event>([](const my_event &event, my_emitter &emitter) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
@@ -355,7 +390,7 @@ slightly from each other:
|
||||
As an example:
|
||||
|
||||
```cpp
|
||||
auto conn = emitter.once<MyEvent>([](const MyEvent &event, MyEmitter &emitter) {
|
||||
auto conn = emitter.once<my_event>([](const my_event &event, my_emitter &emitter) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
@@ -375,7 +410,7 @@ listeners for a given type of event or to clear the emitter:
|
||||
|
||||
```cpp
|
||||
// removes all the listener for the specific event
|
||||
emitter.clear<MyEvent>();
|
||||
emitter.clear<my_event>();
|
||||
|
||||
// removes all the listeners registered so far
|
||||
emitter.clear();
|
||||
@@ -386,11 +421,11 @@ member function offers a convenient approach that relieves users from having to
|
||||
create the event:
|
||||
|
||||
```cpp
|
||||
struct MyEvent { int i; };
|
||||
struct my_event { int i; };
|
||||
|
||||
// ...
|
||||
|
||||
emitter.publish<MyEvent>(42);
|
||||
emitter.publish<my_event>(42);
|
||||
```
|
||||
|
||||
Finally, the `empty` member function tests if there exists at least either a
|
||||
@@ -400,7 +435,7 @@ listener registered with the event emitter or to a given type of event:
|
||||
bool empty;
|
||||
|
||||
// checks if there is any listener registered for the specific event
|
||||
empty = emitter.empty<MyEvent>();
|
||||
empty = emitter.empty<my_event>();
|
||||
|
||||
// checks it there are listeners registered with the event emitter
|
||||
empty = emitter.empty();
|
||||
@@ -1,39 +0,0 @@
|
||||
### EnTT and shared libraries
|
||||
|
||||
To make sure that an application and a shared library that use both `EnTT` can
|
||||
interact correctly when symbols are hidden by default, there are some tricks to
|
||||
follow.<br/>
|
||||
In particular and in order to avoid undefined behaviors, all the instantiation
|
||||
of the `Family` class template shall be made explicit along with the system-wide
|
||||
specifier to use to export them.
|
||||
|
||||
At the time I'm writing this document, the classes that use internally the above
|
||||
mentioned class template are `Dispatcher`, `Emitter` and `Registry`. Therefore
|
||||
and as an example, if you use the `Registry` class template in your shared
|
||||
library and want to set symbols visibility to _hidden_ by default, the following
|
||||
lines are required to allow it to function properly with a client that also uses
|
||||
the `Registry` somehow:
|
||||
|
||||
* On GNU/Linux:
|
||||
|
||||
```cpp
|
||||
namespace entt {
|
||||
template class __attribute__((visibility("default"))) Family<struct InternalRegistryTagFamily>;
|
||||
template class __attribute__((visibility("default"))) Family<struct InternalRegistryComponentFamily>;
|
||||
template class __attribute__((visibility("default"))) Family<struct InternalRegistryHandlerFamily>;
|
||||
}
|
||||
```
|
||||
|
||||
* On Windows:
|
||||
|
||||
```cpp
|
||||
namespace entt {
|
||||
template class __declspec(dllexport) Family<struct InternalRegistryTagFamily>;
|
||||
template class __declspec(dllexport) Family<struct InternalRegistryComponentFamily>;
|
||||
template class __declspec(dllexport) Family<struct InternalRegistryHandlerFamily>;
|
||||
}
|
||||
```
|
||||
|
||||
Otherwise, the risk is that type identifiers are different between the shared
|
||||
library and the application and this will prevent the whole thing from
|
||||
functioning correctly for obvious reasons.
|
||||
299
scripts/amalgamate.py
Normal file
299
scripts/amalgamate.py
Normal file
@@ -0,0 +1,299 @@
|
||||
#!/usr/bin/env python
|
||||
# coding=utf-8
|
||||
|
||||
# amalgamate.py - Amalgamate C source and header files.
|
||||
# Copyright (c) 2012, Erik Edlund <erik.edlund@32767.se>
|
||||
#
|
||||
# Redistribution and use in source and binary forms, with or without modification,
|
||||
# are permitted provided that the following conditions are met:
|
||||
#
|
||||
# * Redistributions of source code must retain the above copyright notice,
|
||||
# this list of conditions and the following disclaimer.
|
||||
#
|
||||
# * Redistributions in binary form must reproduce the above copyright notice,
|
||||
# this list of conditions and the following disclaimer in the documentation
|
||||
# and/or other materials provided with the distribution.
|
||||
#
|
||||
# * Neither the name of Erik Edlund, nor the names of its contributors may
|
||||
# be used to endorse or promote products derived from this software without
|
||||
# specific prior written permission.
|
||||
#
|
||||
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
# ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
# ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
from __future__ import division
|
||||
from __future__ import print_function
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import argparse
|
||||
import datetime
|
||||
import json
|
||||
import os
|
||||
import re
|
||||
|
||||
|
||||
class Amalgamation(object):
|
||||
|
||||
# Prepends self.source_path to file_path if needed.
|
||||
def actual_path(self, file_path):
|
||||
if not os.path.isabs(file_path):
|
||||
file_path = os.path.join(self.source_path, file_path)
|
||||
return file_path
|
||||
|
||||
# Search included file_path in self.include_paths and
|
||||
# in source_dir if specified.
|
||||
def find_included_file(self, file_path, source_dir):
|
||||
search_dirs = self.include_paths[:]
|
||||
if source_dir:
|
||||
search_dirs.insert(0, source_dir)
|
||||
|
||||
for search_dir in search_dirs:
|
||||
search_path = os.path.join(search_dir, file_path)
|
||||
if os.path.isfile(self.actual_path(search_path)):
|
||||
return search_path
|
||||
return None
|
||||
|
||||
def __init__(self, args):
|
||||
with open(args.config, 'r') as f:
|
||||
config = json.loads(f.read())
|
||||
for key in config:
|
||||
setattr(self, key, config[key])
|
||||
|
||||
self.verbose = args.verbose == "yes"
|
||||
self.prologue = args.prologue
|
||||
self.source_path = args.source_path
|
||||
self.included_files = []
|
||||
|
||||
# Generate the amalgamation and write it to the target file.
|
||||
def generate(self):
|
||||
amalgamation = ""
|
||||
|
||||
if self.prologue:
|
||||
with open(self.prologue, 'r') as f:
|
||||
amalgamation += datetime.datetime.now().strftime(f.read())
|
||||
|
||||
if self.verbose:
|
||||
print("Config:")
|
||||
print(" target = {0}".format(self.target))
|
||||
print(" working_dir = {0}".format(os.getcwd()))
|
||||
print(" include_paths = {0}".format(self.include_paths))
|
||||
print("Creating amalgamation:")
|
||||
for file_path in self.sources:
|
||||
# Do not check the include paths while processing the source
|
||||
# list, all given source paths must be correct.
|
||||
# actual_path = self.actual_path(file_path)
|
||||
print(" - processing \"{0}\"".format(file_path))
|
||||
t = TranslationUnit(file_path, self, True)
|
||||
amalgamation += t.content
|
||||
|
||||
with open(self.target, 'w') as f:
|
||||
f.write(amalgamation)
|
||||
|
||||
print("...done!\n")
|
||||
if self.verbose:
|
||||
print("Files processed: {0}".format(self.sources))
|
||||
print("Files included: {0}".format(self.included_files))
|
||||
print("")
|
||||
|
||||
|
||||
def _is_within(match, matches):
|
||||
for m in matches:
|
||||
if match.start() > m.start() and \
|
||||
match.end() < m.end():
|
||||
return True
|
||||
return False
|
||||
|
||||
|
||||
class TranslationUnit(object):
|
||||
# // C++ comment.
|
||||
cpp_comment_pattern = re.compile(r"//.*?\n")
|
||||
|
||||
# /* C comment. */
|
||||
c_comment_pattern = re.compile(r"/\*.*?\*/", re.S)
|
||||
|
||||
# "complex \"stri\\\ng\" value".
|
||||
string_pattern = re.compile("[^']" r'".*?(?<=[^\\])"', re.S)
|
||||
|
||||
# Handle simple include directives. Support for advanced
|
||||
# directives where macros and defines needs to expanded is
|
||||
# not a concern right now.
|
||||
include_pattern = re.compile(
|
||||
r'#\s*include\s+(<|")(?P<path>.*?)("|>)', re.S)
|
||||
|
||||
# #pragma once
|
||||
pragma_once_pattern = re.compile(r'#\s*pragma\s+once', re.S)
|
||||
|
||||
# Search for pattern in self.content, add the match to
|
||||
# contexts if found and update the index accordingly.
|
||||
def _search_content(self, index, pattern, contexts):
|
||||
match = pattern.search(self.content, index)
|
||||
if match:
|
||||
contexts.append(match)
|
||||
return match.end()
|
||||
return index + 2
|
||||
|
||||
# Return all the skippable contexts, i.e., comments and strings
|
||||
def _find_skippable_contexts(self):
|
||||
# Find contexts in the content in which a found include
|
||||
# directive should not be processed.
|
||||
skippable_contexts = []
|
||||
|
||||
# Walk through the content char by char, and try to grab
|
||||
# skippable contexts using regular expressions when found.
|
||||
i = 1
|
||||
content_len = len(self.content)
|
||||
while i < content_len:
|
||||
j = i - 1
|
||||
current = self.content[i]
|
||||
previous = self.content[j]
|
||||
|
||||
if current == '"':
|
||||
# String value.
|
||||
i = self._search_content(j, self.string_pattern,
|
||||
skippable_contexts)
|
||||
elif current == '*' and previous == '/':
|
||||
# C style comment.
|
||||
i = self._search_content(j, self.c_comment_pattern,
|
||||
skippable_contexts)
|
||||
elif current == '/' and previous == '/':
|
||||
# C++ style comment.
|
||||
i = self._search_content(j, self.cpp_comment_pattern,
|
||||
skippable_contexts)
|
||||
else:
|
||||
# Skip to the next char.
|
||||
i += 1
|
||||
|
||||
return skippable_contexts
|
||||
|
||||
# Returns True if the match is within list of other matches
|
||||
|
||||
# Removes pragma once from content
|
||||
def _process_pragma_once(self):
|
||||
content_len = len(self.content)
|
||||
if content_len < len("#include <x>"):
|
||||
return 0
|
||||
|
||||
# Find contexts in the content in which a found include
|
||||
# directive should not be processed.
|
||||
skippable_contexts = self._find_skippable_contexts()
|
||||
|
||||
pragmas = []
|
||||
pragma_once_match = self.pragma_once_pattern.search(self.content)
|
||||
while pragma_once_match:
|
||||
if not _is_within(pragma_once_match, skippable_contexts):
|
||||
pragmas.append(pragma_once_match)
|
||||
|
||||
pragma_once_match = self.pragma_once_pattern.search(self.content,
|
||||
pragma_once_match.end())
|
||||
|
||||
# Handle all collected pragma once directives.
|
||||
prev_end = 0
|
||||
tmp_content = ''
|
||||
for pragma_match in pragmas:
|
||||
tmp_content += self.content[prev_end:pragma_match.start()]
|
||||
prev_end = pragma_match.end()
|
||||
tmp_content += self.content[prev_end:]
|
||||
self.content = tmp_content
|
||||
|
||||
# Include all trivial #include directives into self.content.
|
||||
def _process_includes(self):
|
||||
content_len = len(self.content)
|
||||
if content_len < len("#include <x>"):
|
||||
return 0
|
||||
|
||||
# Find contexts in the content in which a found include
|
||||
# directive should not be processed.
|
||||
skippable_contexts = self._find_skippable_contexts()
|
||||
|
||||
# Search for include directives in the content, collect those
|
||||
# which should be included into the content.
|
||||
includes = []
|
||||
include_match = self.include_pattern.search(self.content)
|
||||
while include_match:
|
||||
if not _is_within(include_match, skippable_contexts):
|
||||
include_path = include_match.group("path")
|
||||
search_same_dir = include_match.group(1) == '"'
|
||||
found_included_path = self.amalgamation.find_included_file(
|
||||
include_path, self.file_dir if search_same_dir else None)
|
||||
if found_included_path:
|
||||
includes.append((include_match, found_included_path))
|
||||
|
||||
include_match = self.include_pattern.search(self.content,
|
||||
include_match.end())
|
||||
|
||||
# Handle all collected include directives.
|
||||
prev_end = 0
|
||||
tmp_content = ''
|
||||
for include in includes:
|
||||
include_match, found_included_path = include
|
||||
tmp_content += self.content[prev_end:include_match.start()]
|
||||
tmp_content += "// {0}\n".format(include_match.group(0))
|
||||
if found_included_path not in self.amalgamation.included_files:
|
||||
t = TranslationUnit(found_included_path, self.amalgamation, False)
|
||||
tmp_content += t.content
|
||||
prev_end = include_match.end()
|
||||
tmp_content += self.content[prev_end:]
|
||||
self.content = tmp_content
|
||||
|
||||
return len(includes)
|
||||
|
||||
# Make all content processing
|
||||
def _process(self):
|
||||
if not self.is_root:
|
||||
self._process_pragma_once()
|
||||
self._process_includes()
|
||||
|
||||
def __init__(self, file_path, amalgamation, is_root):
|
||||
self.file_path = file_path
|
||||
self.file_dir = os.path.dirname(file_path)
|
||||
self.amalgamation = amalgamation
|
||||
self.is_root = is_root
|
||||
|
||||
self.amalgamation.included_files.append(self.file_path)
|
||||
|
||||
actual_path = self.amalgamation.actual_path(file_path)
|
||||
if not os.path.isfile(actual_path):
|
||||
raise IOError("File not found: \"{0}\"".format(file_path))
|
||||
with open(actual_path, 'r') as f:
|
||||
self.content = f.read()
|
||||
self._process()
|
||||
|
||||
|
||||
def main():
|
||||
description = "Amalgamate C source and header files."
|
||||
usage = " ".join([
|
||||
"amalgamate.py",
|
||||
"[-v]",
|
||||
"-c path/to/config.json",
|
||||
"-s path/to/source/dir",
|
||||
"[-p path/to/prologue.(c|h)]"
|
||||
])
|
||||
argsparser = argparse.ArgumentParser(
|
||||
description=description, usage=usage)
|
||||
|
||||
argsparser.add_argument("-v", "--verbose", dest="verbose",
|
||||
choices=["yes", "no"], metavar="", help="be verbose")
|
||||
|
||||
argsparser.add_argument("-c", "--config", dest="config",
|
||||
required=True, metavar="", help="path to a JSON config file")
|
||||
|
||||
argsparser.add_argument("-s", "--source", dest="source_path",
|
||||
required=True, metavar="", help="source code path")
|
||||
|
||||
argsparser.add_argument("-p", "--prologue", dest="prologue",
|
||||
required=False, metavar="", help="path to a C prologue file")
|
||||
|
||||
amalgamation = Amalgamation(argsparser.parse_args())
|
||||
amalgamation.generate()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
8
scripts/config.json
Normal file
8
scripts/config.json
Normal file
@@ -0,0 +1,8 @@
|
||||
{
|
||||
"project": "entt",
|
||||
"target": "single_include/entt/entt.hpp",
|
||||
"sources": [
|
||||
"src/entt/entt.hpp"
|
||||
],
|
||||
"include_paths": ["src"]
|
||||
}
|
||||
15427
single_include/entt/entt.hpp
Normal file
15427
single_include/entt/entt.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -12,5 +12,33 @@
|
||||
#endif // ENTT_HS_SUFFIX
|
||||
|
||||
|
||||
#ifndef ENTT_NO_ATOMIC
|
||||
#include <atomic>
|
||||
template<typename Type>
|
||||
using maybe_atomic_t = std::atomic<Type>;
|
||||
#else // ENTT_NO_ATOMIC
|
||||
template<typename Type>
|
||||
using maybe_atomic_t = Type;
|
||||
#endif // ENTT_NO_ATOMIC
|
||||
|
||||
|
||||
#ifndef ENTT_ID_TYPE
|
||||
#include <cstdint>
|
||||
#define ENTT_ID_TYPE std::uint32_t
|
||||
#endif // ENTT_ID_TYPE
|
||||
|
||||
|
||||
#ifndef ENTT_PAGE_SIZE
|
||||
#define ENTT_PAGE_SIZE 32768
|
||||
#endif
|
||||
|
||||
|
||||
#ifndef ENTT_DISABLE_ASSERT
|
||||
#include <cassert>
|
||||
#define ENTT_ASSERT(condition) assert(condition)
|
||||
#else // ENTT_DISABLE_ASSERT
|
||||
#define ENTT_ASSERT(...) ((void)0)
|
||||
#endif // ENTT_DISABLE_ASSERT
|
||||
|
||||
|
||||
#endif // ENTT_CONFIG_CONFIG_H
|
||||
|
||||
11
src/entt/config/version.h
Normal file
11
src/entt/config/version.h
Normal file
@@ -0,0 +1,11 @@
|
||||
#ifndef ENTT_CONFIG_VERSION_H
|
||||
#define ENTT_CONFIG_VERSION_H
|
||||
|
||||
|
||||
#define ENTT_VERSION "3.0.0"
|
||||
#define ENTT_VERSION_MAJOR 3
|
||||
#define ENTT_VERSION_MINOR 0
|
||||
#define ENTT_VERSION_PATCH 0
|
||||
|
||||
|
||||
#endif // ENTT_CONFIG_VERSION_H
|
||||
@@ -18,7 +18,7 @@ namespace entt {
|
||||
* This class fills the gap by wrapping some flavors of `std::sort` in a
|
||||
* function object.
|
||||
*/
|
||||
struct StdSort final {
|
||||
struct std_sort {
|
||||
/**
|
||||
* @brief Sorts the elements in a range.
|
||||
*
|
||||
@@ -40,7 +40,7 @@ struct StdSort final {
|
||||
|
||||
|
||||
/*! @brief Function object for performing insertion sort. */
|
||||
struct InsertionSort final {
|
||||
struct insertion_sort {
|
||||
/**
|
||||
* @brief Sorts the elements in a range.
|
||||
*
|
||||
@@ -53,52 +53,19 @@ struct InsertionSort final {
|
||||
* @param compare A valid comparison function object.
|
||||
*/
|
||||
template<typename It, typename Compare = std::less<>>
|
||||
void operator()(It first, It last, Compare compare = Compare{}) const {
|
||||
auto it = first + 1;
|
||||
|
||||
while(it != last) {
|
||||
auto value = *it;
|
||||
auto pre = it;
|
||||
|
||||
while(pre != first && compare(value, *(pre-1))) {
|
||||
*pre = *(pre-1);
|
||||
--pre;
|
||||
}
|
||||
|
||||
*pre = value;
|
||||
++it;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/*! @brief Function object for performing bubble sort (single iteration). */
|
||||
struct OneShotBubbleSort final {
|
||||
/**
|
||||
* @brief Tries to sort the elements in a range.
|
||||
*
|
||||
* Performs a single iteration to sort the elements in a range using the
|
||||
* given binary comparison function. The range may not be completely sorted
|
||||
* after running this function.
|
||||
*
|
||||
* @tparam It Type of random access iterator.
|
||||
* @tparam Compare Type of comparison function object.
|
||||
* @param first An iterator to the first element of the range to sort.
|
||||
* @param last An iterator past the last element of the range to sort.
|
||||
* @param compare A valid comparison function object.
|
||||
*/
|
||||
template<typename It, typename Compare = std::less<>>
|
||||
void operator()(It first, It last, Compare compare = Compare{}) const {
|
||||
if(first != last) {
|
||||
auto it = first++;
|
||||
auto it = first + 1;
|
||||
|
||||
while(first != last) {
|
||||
if(compare(*first, *it)) {
|
||||
using std::swap;
|
||||
std::swap(*first, *it);
|
||||
while(it != last) {
|
||||
auto pre = it++;
|
||||
auto value = *pre;
|
||||
|
||||
while(pre-- != first && compare(value, *pre)) {
|
||||
*(pre+1) = *pre;
|
||||
}
|
||||
|
||||
it = first++;
|
||||
*(pre+1) = value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,8 +3,6 @@
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include <cstddef>
|
||||
#include <atomic>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
@@ -19,34 +17,23 @@ namespace entt {
|
||||
* identifiers.
|
||||
*/
|
||||
template<typename...>
|
||||
class Family {
|
||||
static std::atomic<std::size_t> identifier;
|
||||
class family {
|
||||
inline static maybe_atomic_t<ENTT_ID_TYPE> identifier;
|
||||
|
||||
template<typename...>
|
||||
static std::size_t family() ENTT_NOEXCEPT {
|
||||
static const std::size_t value = identifier.fetch_add(1);
|
||||
return value;
|
||||
}
|
||||
inline static const auto inner = identifier++;
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using family_type = std::size_t;
|
||||
using family_type = ENTT_ID_TYPE;
|
||||
|
||||
/**
|
||||
* @brief Returns an unique identifier for the given type.
|
||||
* @return Statically generated unique identifier for the given type.
|
||||
*/
|
||||
/*! @brief Statically generated unique identifier for the given type. */
|
||||
template<typename... Type>
|
||||
inline static family_type type() ENTT_NOEXCEPT {
|
||||
return family<std::decay_t<Type>...>();
|
||||
}
|
||||
// at the time I'm writing, clang crashes during compilation if auto is used in place of family_type here
|
||||
inline static const family_type type = inner<std::decay_t<Type>...>;
|
||||
};
|
||||
|
||||
|
||||
template<typename... Types>
|
||||
std::atomic<std::size_t> Family<Types...>::identifier{};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -3,7 +3,6 @@
|
||||
|
||||
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
@@ -11,7 +10,43 @@ namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Zero overhead resource identifier.
|
||||
* @cond TURN_OFF_DOXYGEN
|
||||
* Internal details not to be documented.
|
||||
*/
|
||||
|
||||
|
||||
namespace internal {
|
||||
|
||||
|
||||
template<typename>
|
||||
struct fnv1a_traits;
|
||||
|
||||
|
||||
template<>
|
||||
struct fnv1a_traits<std::uint32_t> {
|
||||
static constexpr std::uint32_t offset = 2166136261;
|
||||
static constexpr std::uint32_t prime = 16777619;
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct fnv1a_traits<std::uint64_t> {
|
||||
static constexpr std::uint64_t offset = 14695981039346656037ull;
|
||||
static constexpr std::uint64_t prime = 1099511628211ull;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal details not to be documented.
|
||||
* @endcond TURN_OFF_DOXYGEN
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @brief Zero overhead unique identifier.
|
||||
*
|
||||
* A hashed string is a compile-time tool that allows users to use
|
||||
* human-readable identifers in the codebase while using their numeric
|
||||
@@ -19,24 +54,69 @@ namespace entt {
|
||||
* Because of that, a hashed string can also be used in constant expressions if
|
||||
* required.
|
||||
*/
|
||||
class HashedString final {
|
||||
struct ConstCharWrapper final {
|
||||
class hashed_string {
|
||||
using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
|
||||
|
||||
struct const_wrapper {
|
||||
// non-explicit constructor on purpose
|
||||
constexpr ConstCharWrapper(const char *str) ENTT_NOEXCEPT: str{str} {}
|
||||
constexpr const_wrapper(const char *curr) ENTT_NOEXCEPT: str{curr} {}
|
||||
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) ENTT_NOEXCEPT {
|
||||
return str[0] == 0 ? partial : helper((partial^str[0])*prime, str+1);
|
||||
inline static constexpr ENTT_ID_TYPE helper(ENTT_ID_TYPE partial, const char *curr) ENTT_NOEXCEPT {
|
||||
return curr[0] == 0 ? partial : helper((partial^curr[0])*traits_type::prime, curr+1);
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using hash_type = std::uint64_t;
|
||||
using hash_type = ENTT_ID_TYPE;
|
||||
|
||||
/**
|
||||
* @brief Returns directly the numeric representation of a string.
|
||||
*
|
||||
* 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}
|
||||
* const auto value = hashed_string::to_value("my.png");
|
||||
* @endcode
|
||||
*
|
||||
* @tparam N Number of characters of the identifier.
|
||||
* @param str Human-readable identifer.
|
||||
* @return The numeric representation of the string.
|
||||
*/
|
||||
template<std::size_t N>
|
||||
inline static constexpr hash_type to_value(const char (&str)[N]) ENTT_NOEXCEPT {
|
||||
return helper(traits_type::offset, str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns directly the numeric representation of a string.
|
||||
* @param wrapper Helps achieving the purpose by relying on overloading.
|
||||
* @return The numeric representation of the string.
|
||||
*/
|
||||
inline static hash_type to_value(const_wrapper wrapper) ENTT_NOEXCEPT {
|
||||
return helper(traits_type::offset, wrapper.str);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns directly the numeric representation of a string view.
|
||||
* @param str Human-readable identifer.
|
||||
* @param size Length of the string to hash.
|
||||
* @return The numeric representation of the string.
|
||||
*/
|
||||
inline static hash_type to_value(const char *str, std::size_t size) ENTT_NOEXCEPT {
|
||||
ENTT_ID_TYPE partial{traits_type::offset};
|
||||
while(size--) { partial = (partial^(str++)[0])*traits_type::prime; }
|
||||
return partial;
|
||||
}
|
||||
|
||||
/*! @brief Constructs an empty hashed string. */
|
||||
constexpr hashed_string() ENTT_NOEXCEPT
|
||||
: str{nullptr}, hash{}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Constructs a hashed string from an array of const chars.
|
||||
@@ -46,37 +126,49 @@ public:
|
||||
* characters.<br/>
|
||||
* Example of use:
|
||||
* @code{.cpp}
|
||||
* HashedString sh{"my.png"};
|
||||
* hashed_string hs{"my.png"};
|
||||
* @endcode
|
||||
*
|
||||
* @tparam N Number of characters of the identifier.
|
||||
* @param str Human-readable identifer.
|
||||
* @param curr Human-readable identifer.
|
||||
*/
|
||||
template<std::size_t N>
|
||||
constexpr HashedString(const char (&str)[N]) ENTT_NOEXCEPT
|
||||
: hash{helper(offset, str)}, str{str}
|
||||
constexpr hashed_string(const char (&curr)[N]) ENTT_NOEXCEPT
|
||||
: str{curr}, hash{helper(traits_type::offset, curr)}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @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) ENTT_NOEXCEPT
|
||||
: hash{helper(offset, wrapper.str)}, str{wrapper.str}
|
||||
explicit constexpr hashed_string(const_wrapper wrapper) ENTT_NOEXCEPT
|
||||
: str{wrapper.str}, hash{helper(traits_type::offset, wrapper.str)}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Returns the human-readable representation of a hashed string.
|
||||
* @return The string used to initialize the instance.
|
||||
*/
|
||||
constexpr const char * data() const ENTT_NOEXCEPT {
|
||||
return str;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the numeric representation of a hashed string.
|
||||
* @return The numeric representation of the instance.
|
||||
*/
|
||||
constexpr hash_type value() const ENTT_NOEXCEPT {
|
||||
return hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the human-readable representation of a hashed string.
|
||||
* @return The string used to initialize the instance.
|
||||
*/
|
||||
constexpr operator const char *() const ENTT_NOEXCEPT { return str; }
|
||||
|
||||
/**
|
||||
* @brief Returns the numeric representation of a hashed string.
|
||||
* @return The numeric representation of the instance.
|
||||
*/
|
||||
/*! @copydoc value */
|
||||
constexpr operator hash_type() const ENTT_NOEXCEPT { return hash; }
|
||||
|
||||
/**
|
||||
@@ -84,13 +176,13 @@ public:
|
||||
* @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 ENTT_NOEXCEPT {
|
||||
constexpr bool operator==(const hashed_string &other) const ENTT_NOEXCEPT {
|
||||
return hash == other.hash;
|
||||
}
|
||||
|
||||
private:
|
||||
const hash_type hash;
|
||||
const char *str;
|
||||
hash_type hash;
|
||||
};
|
||||
|
||||
|
||||
@@ -100,7 +192,7 @@ private:
|
||||
* @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) ENTT_NOEXCEPT {
|
||||
constexpr bool operator!=(const hashed_string &lhs, const hashed_string &rhs) ENTT_NOEXCEPT {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
@@ -113,8 +205,8 @@ constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) ENTT
|
||||
* @param str The literal without its suffix.
|
||||
* @return A properly initialized hashed string.
|
||||
*/
|
||||
constexpr entt::HashedString operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
|
||||
return entt::HashedString{str};
|
||||
constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
|
||||
return entt::hashed_string{str};
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -2,44 +2,15 @@
|
||||
#define ENTT_CORE_IDENT_HPP
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @cond TURN_OFF_DOXYGEN
|
||||
* Internal details not to be documented.
|
||||
*/
|
||||
|
||||
|
||||
namespace internal {
|
||||
|
||||
|
||||
template<typename...>
|
||||
struct IsPartOf;
|
||||
|
||||
template<typename Type, typename Current, typename... Other>
|
||||
struct IsPartOf<Type, Current, Other...>: std::conditional_t<std::is_same<Type, Current>::value, std::true_type, IsPartOf<Type, Other...>> {};
|
||||
|
||||
template<typename Type>
|
||||
struct IsPartOf<Type>: std::false_type {};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal details not to be documented.
|
||||
* @endcond TURN_OFF_DOXYGEN
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @brief Types identifiers.
|
||||
*
|
||||
@@ -51,13 +22,13 @@ struct IsPartOf<Type>: std::false_type {};
|
||||
* Identifiers are constant expression and can be used in any context where such
|
||||
* an expression is required. As an example:
|
||||
* @code{.cpp}
|
||||
* using ID = entt::Identifier<AType, AnotherType>;
|
||||
* using id = entt::identifier<a_type, another_type>;
|
||||
*
|
||||
* switch(aTypeIdentifier) {
|
||||
* case ID::get<AType>():
|
||||
* switch(a_type_identifier) {
|
||||
* case id::type<a_type>:
|
||||
* // ...
|
||||
* break;
|
||||
* case ID::get<AnotherType>():
|
||||
* case id::type<another_type>:
|
||||
* // ...
|
||||
* break;
|
||||
* default:
|
||||
@@ -68,33 +39,22 @@ struct IsPartOf<Type>: std::false_type {};
|
||||
* @tparam Types List of types for which to generate identifiers.
|
||||
*/
|
||||
template<typename... Types>
|
||||
class Identifier final {
|
||||
class identifier {
|
||||
using tuple_type = std::tuple<std::decay_t<Types>...>;
|
||||
|
||||
template<typename Type, std::size_t... Indexes>
|
||||
static constexpr std::size_t get(std::index_sequence<Indexes...>) ENTT_NOEXCEPT {
|
||||
static_assert(internal::IsPartOf<Type, Types...>::value, "!");
|
||||
|
||||
std::size_t max{};
|
||||
using accumulator_type = std::size_t[];
|
||||
accumulator_type accumulator = { (max = std::is_same<Type, std::tuple_element_t<Indexes, tuple_type>>::value ? Indexes : max)... };
|
||||
(void)accumulator;
|
||||
return max;
|
||||
static constexpr ENTT_ID_TYPE get(std::index_sequence<Indexes...>) ENTT_NOEXCEPT {
|
||||
static_assert(std::disjunction_v<std::is_same<Type, Types>...>);
|
||||
return (0 + ... + (std::is_same_v<Type, std::tuple_element_t<Indexes, tuple_type>> ? ENTT_ID_TYPE(Indexes) : ENTT_ID_TYPE{}));
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using identifier_type = std::size_t;
|
||||
using identifier_type = ENTT_ID_TYPE;
|
||||
|
||||
/**
|
||||
* @brief Returns the identifier associated with a given type.
|
||||
* @tparam Type of which to return the identifier.
|
||||
* @return The identifier associated with the given type.
|
||||
*/
|
||||
/*! @brief Statically generated unique identifier for the given type. */
|
||||
template<typename Type>
|
||||
static constexpr identifier_type get() ENTT_NOEXCEPT {
|
||||
return get<std::decay_t<Type>>(std::make_index_sequence<sizeof...(Types)>{});
|
||||
}
|
||||
static constexpr identifier_type type = get<std::decay_t<Type>>(std::make_index_sequence<sizeof...(Types)>{});
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -2,9 +2,8 @@
|
||||
#define ENTT_CORE_MONOSTATE_HPP
|
||||
|
||||
|
||||
#include <atomic>
|
||||
#include <cassert>
|
||||
#include "family.hpp"
|
||||
#include "../config/config.h"
|
||||
#include "hashed_string.hpp"
|
||||
|
||||
|
||||
@@ -22,8 +21,8 @@ namespace entt {
|
||||
* both during an assignment and when they try to read back their data.
|
||||
* Otherwise, they can incur in unexpected results.
|
||||
*/
|
||||
template<HashedString::hash_type>
|
||||
struct Monostate {
|
||||
template<hashed_string::hash_type>
|
||||
struct monostate {
|
||||
/**
|
||||
* @brief Assigns a value of a specific type to a given key.
|
||||
* @tparam Type Type of the value to assign.
|
||||
@@ -31,7 +30,7 @@ struct Monostate {
|
||||
*/
|
||||
template<typename Type>
|
||||
void operator=(Type val) const ENTT_NOEXCEPT {
|
||||
Monostate::value<Type> = val;
|
||||
value<Type> = val;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -41,18 +40,21 @@ struct Monostate {
|
||||
*/
|
||||
template<typename Type>
|
||||
operator Type() const ENTT_NOEXCEPT {
|
||||
return Monostate::value<Type>;
|
||||
return value<Type>;
|
||||
}
|
||||
|
||||
private:
|
||||
template<typename Type>
|
||||
static std::atomic<Type> value;
|
||||
inline static maybe_atomic_t<Type> value{};
|
||||
};
|
||||
|
||||
|
||||
template<HashedString::hash_type ID>
|
||||
template<typename Type>
|
||||
std::atomic<Type> Monostate<ID>::value{};
|
||||
/**
|
||||
* @brief Helper variable template.
|
||||
* @tparam Value Value used to differentiate between different variables.
|
||||
*/
|
||||
template<hashed_string::hash_type Value>
|
||||
inline monostate<Value> monostate_v = {};
|
||||
|
||||
|
||||
}
|
||||
|
||||
228
src/entt/core/type_traits.hpp
Normal file
228
src/entt/core/type_traits.hpp
Normal file
@@ -0,0 +1,228 @@
|
||||
#ifndef ENTT_CORE_TYPE_TRAITS_HPP
|
||||
#define ENTT_CORE_TYPE_TRAITS_HPP
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include "../core/hashed_string.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief A class to use to push around lists of types, nothing more.
|
||||
* @tparam Type Types provided by the given type list.
|
||||
*/
|
||||
template<typename... Type>
|
||||
struct type_list {
|
||||
/*! @brief Unsigned integer type. */
|
||||
static constexpr auto size = sizeof...(Type);
|
||||
};
|
||||
|
||||
|
||||
/*! @brief Primary template isn't defined on purpose. */
|
||||
template<typename...>
|
||||
struct type_list_cat;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Concatenates multiple type lists.
|
||||
* @tparam Type Types provided by the first type list.
|
||||
* @tparam Other Types provided by the second type list.
|
||||
* @tparam List Other type lists, if any.
|
||||
*/
|
||||
template<typename... Type, typename... Other, typename... List>
|
||||
struct type_list_cat<type_list<Type...>, type_list<Other...>, List...> {
|
||||
/*! @brief A type list composed by the types of all the type lists. */
|
||||
using type = typename type_list_cat<type_list<Type..., Other...>, List...>::type;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Concatenates multiple type lists.
|
||||
* @tparam Type Types provided by the type list.
|
||||
*/
|
||||
template<typename... Type>
|
||||
struct type_list_cat<type_list<Type...>> {
|
||||
/*! @brief A type list composed by the types of all the type lists. */
|
||||
using type = type_list<Type...>;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Helper type.
|
||||
* @tparam List Type lists to concatenate.
|
||||
*/
|
||||
template<typename... List>
|
||||
using type_list_cat_t = typename type_list_cat<List...>::type;
|
||||
|
||||
|
||||
/*! @brief Primary template isn't defined on purpose. */
|
||||
template<typename>
|
||||
struct type_list_unique;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Removes duplicates types from a type list.
|
||||
* @tparam Type One of the types provided by the given type list.
|
||||
* @tparam Other The other types provided by the given type list.
|
||||
*/
|
||||
template<typename Type, typename... Other>
|
||||
struct type_list_unique<type_list<Type, Other...>> {
|
||||
/*! @brief A type list without duplicate types. */
|
||||
using type = std::conditional_t<
|
||||
std::disjunction_v<std::is_same<Type, Other>...>,
|
||||
typename type_list_unique<type_list<Other...>>::type,
|
||||
type_list_cat_t<type_list<Type>, typename type_list_unique<type_list<Other...>>::type>
|
||||
>;
|
||||
};
|
||||
|
||||
|
||||
/*! @brief Removes duplicates types from a type list. */
|
||||
template<>
|
||||
struct type_list_unique<type_list<>> {
|
||||
/*! @brief A type list without duplicate types. */
|
||||
using type = type_list<>;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Helper type.
|
||||
* @tparam Type A type list.
|
||||
*/
|
||||
template<typename Type>
|
||||
using type_list_unique_t = typename type_list_unique<Type>::type;
|
||||
|
||||
|
||||
/*! @brief Traits class used mainly to push things across boundaries. */
|
||||
template<typename>
|
||||
struct named_type_traits;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Specialization used to get rid of constness.
|
||||
* @tparam Type Named type.
|
||||
*/
|
||||
template<typename Type>
|
||||
struct named_type_traits<const Type>
|
||||
: named_type_traits<Type>
|
||||
{};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Helper type.
|
||||
* @tparam Type Potentially named type.
|
||||
*/
|
||||
template<typename Type>
|
||||
using named_type_traits_t = typename named_type_traits<Type>::type;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Provides the member constant `value` to true if a given type has a
|
||||
* name. In all other cases, `value` is false.
|
||||
*/
|
||||
template<typename, typename = std::void_t<>>
|
||||
struct is_named_type: std::false_type {};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Provides the member constant `value` to true if a given type has a
|
||||
* name. In all other cases, `value` is false.
|
||||
* @tparam Type Potentially named type.
|
||||
*/
|
||||
template<typename Type>
|
||||
struct is_named_type<Type, std::void_t<named_type_traits_t<std::decay_t<Type>>>>: std::true_type {};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Helper variable template.
|
||||
*
|
||||
* True if a given type has a name, false otherwise.
|
||||
*
|
||||
* @tparam Type Potentially named type.
|
||||
*/
|
||||
template<class Type>
|
||||
constexpr auto is_named_type_v = is_named_type<Type>::value;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Utility macro to deal with an issue of MSVC.
|
||||
*
|
||||
* See _msvc-doesnt-expand-va-args-correctly_ on SO for all the details.
|
||||
*
|
||||
* @param args Argument to expand.
|
||||
*/
|
||||
#define ENTT_EXPAND(args) args
|
||||
|
||||
|
||||
/**
|
||||
* @brief Makes an already existing type a named type.
|
||||
* @param type Type to assign a name to.
|
||||
*/
|
||||
#define ENTT_NAMED_TYPE(type)\
|
||||
template<>\
|
||||
struct entt::named_type_traits<type>\
|
||||
: std::integral_constant<typename entt::hashed_string::hash_type, entt::hashed_string::to_value(#type)>\
|
||||
{\
|
||||
static_assert(std::is_same_v<std::decay_t<type>, type>);\
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Defines a named type (to use for structs).
|
||||
* @param clazz Name of the type to define.
|
||||
* @param body Body of the type to define.
|
||||
*/
|
||||
#define ENTT_NAMED_STRUCT_ONLY(clazz, body)\
|
||||
struct clazz body;\
|
||||
ENTT_NAMED_TYPE(clazz)
|
||||
|
||||
|
||||
/**
|
||||
* @brief Defines a named type (to use for structs).
|
||||
* @param ns Namespace where to define the named type.
|
||||
* @param clazz Name of the type to define.
|
||||
* @param body Body of the type to define.
|
||||
*/
|
||||
#define ENTT_NAMED_STRUCT_WITH_NAMESPACE(ns, clazz, body)\
|
||||
namespace ns { struct clazz body; }\
|
||||
ENTT_NAMED_TYPE(ns::clazz)
|
||||
|
||||
|
||||
/*! @brief Utility function to simulate macro overloading. */
|
||||
#define ENTT_NAMED_STRUCT_OVERLOAD(_1, _2, _3, FUNC, ...) FUNC
|
||||
/*! @brief Defines a named type (to use for structs). */
|
||||
#define ENTT_NAMED_STRUCT(...) ENTT_EXPAND(ENTT_NAMED_STRUCT_OVERLOAD(__VA_ARGS__, ENTT_NAMED_STRUCT_WITH_NAMESPACE, ENTT_NAMED_STRUCT_ONLY,)(__VA_ARGS__))
|
||||
|
||||
|
||||
/**
|
||||
* @brief Defines a named type (to use for classes).
|
||||
* @param clazz Name of the type to define.
|
||||
* @param body Body of the type to define.
|
||||
*/
|
||||
#define ENTT_NAMED_CLASS_ONLY(clazz, body)\
|
||||
class clazz body;\
|
||||
ENTT_NAMED_TYPE(clazz)
|
||||
|
||||
|
||||
/**
|
||||
* @brief Defines a named type (to use for classes).
|
||||
* @param ns Namespace where to define the named type.
|
||||
* @param clazz Name of the type to define.
|
||||
* @param body Body of the type to define.
|
||||
*/
|
||||
#define ENTT_NAMED_CLASS_WITH_NAMESPACE(ns, clazz, body)\
|
||||
namespace ns { class clazz body; }\
|
||||
ENTT_NAMED_TYPE(ns::clazz)
|
||||
|
||||
|
||||
/*! @brief Utility function to simulate macro overloading. */
|
||||
#define ENTT_NAMED_CLASS_MACRO(_1, _2, _3, FUNC, ...) FUNC
|
||||
/*! @brief Defines a named type (to use for classes). */
|
||||
#define ENTT_NAMED_CLASS(...) ENTT_EXPAND(ENTT_NAMED_CLASS_MACRO(__VA_ARGS__, ENTT_NAMED_CLASS_WITH_NAMESPACE, ENTT_NAMED_CLASS_ONLY,)(__VA_ARGS__))
|
||||
|
||||
|
||||
#endif // ENTT_CORE_TYPE_TRAITS_HPP
|
||||
32
src/entt/core/utility.hpp
Normal file
32
src/entt/core/utility.hpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#ifndef ENTT_CORE_UTILITY_HPP
|
||||
#define ENTT_CORE_UTILITY_HPP
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constant utility to disambiguate overloaded member functions.
|
||||
* @tparam Type Function type of the desired overload.
|
||||
* @tparam Class Type of class to which the member functions belong.
|
||||
* @param member A valid pointer to a member function.
|
||||
* @return Pointer to the member function.
|
||||
*/
|
||||
template<typename Type, typename Class>
|
||||
constexpr auto overload(Type Class:: *member) { return member; }
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constant utility to disambiguate overloaded functions.
|
||||
* @tparam Type Function type of the desired overload.
|
||||
* @param func A valid pointer to a function.
|
||||
* @return Pointer to the function.
|
||||
*/
|
||||
template<typename Type>
|
||||
constexpr auto overload(Type *func) { return func; }
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_CORE_UTILITY_HPP
|
||||
@@ -4,9 +4,11 @@
|
||||
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "registry.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -21,28 +23,25 @@ namespace entt {
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
struct Actor {
|
||||
struct basic_actor {
|
||||
/*! @brief Type of registry used internally. */
|
||||
using registry_type = Registry<Entity>;
|
||||
using registry_type = basic_registry<Entity>;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = Entity;
|
||||
|
||||
/**
|
||||
* @brief Constructs an actor by using the given registry.
|
||||
* @param reg An entity-component system properly initialized.
|
||||
* @param ref An entity-component system properly initialized.
|
||||
*/
|
||||
Actor(Registry<Entity> ®)
|
||||
: reg{®}, entt{reg.create()}
|
||||
basic_actor(registry_type &ref)
|
||||
: reg{&ref}, entt{ref.create()}
|
||||
{}
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Actor() {
|
||||
virtual ~basic_actor() {
|
||||
reg->destroy(entt);
|
||||
}
|
||||
|
||||
/*! @brief Copying an actor isn't allowed. */
|
||||
Actor(const Actor &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*
|
||||
@@ -52,15 +51,12 @@ struct Actor {
|
||||
*
|
||||
* @param other The instance to move from.
|
||||
*/
|
||||
Actor(Actor &&other)
|
||||
basic_actor(basic_actor &&other)
|
||||
: reg{other.reg}, entt{other.entt}
|
||||
{
|
||||
other.entt = entt::null;
|
||||
other.entt = null;
|
||||
}
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This actor. */
|
||||
Actor & operator=(const Actor &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Move assignment operator.
|
||||
*
|
||||
@@ -71,7 +67,7 @@ struct Actor {
|
||||
* @param other The instance to move from.
|
||||
* @return This actor.
|
||||
*/
|
||||
Actor & operator=(Actor &&other) {
|
||||
basic_actor & operator=(basic_actor &&other) {
|
||||
if(this != &other) {
|
||||
auto tmp{std::move(other)};
|
||||
std::swap(reg, tmp.reg);
|
||||
@@ -81,24 +77,6 @@ struct Actor {
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns the given tag to an actor.
|
||||
*
|
||||
* A new instance of the given tag is created and initialized with the
|
||||
* arguments provided (the tag must have a proper constructor or be of
|
||||
* aggregate type). Then the tag is removed from its previous owner (if any)
|
||||
* and assigned to the actor.
|
||||
*
|
||||
* @tparam Tag Type of the tag to create.
|
||||
* @tparam Args Types of arguments to use to construct the tag.
|
||||
* @param args Parameters to use to initialize the tag.
|
||||
* @return A reference to the newly created tag.
|
||||
*/
|
||||
template<typename Tag, typename... Args>
|
||||
Tag & assign(tag_t, Args &&... args) {
|
||||
return (reg->template remove<Tag>(), reg->template assign<Tag>(tag_t{}, entt, std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns the given component to an actor.
|
||||
*
|
||||
@@ -114,18 +92,8 @@ struct Actor {
|
||||
* @return A reference to the newly created component.
|
||||
*/
|
||||
template<typename Component, typename... Args>
|
||||
Component & assign(Args &&... args) {
|
||||
return reg->template accommodate<Component>(entt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes the given tag from an actor.
|
||||
* @tparam Tag Type of the tag to remove.
|
||||
*/
|
||||
template<typename Tag>
|
||||
void remove(tag_t) {
|
||||
assert(has<Tag>(tag_t{}));
|
||||
reg->template remove<Tag>();
|
||||
decltype(auto) assign(Args &&... args) {
|
||||
return reg->template assign_or_replace<Component>(entt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,16 +105,6 @@ struct Actor {
|
||||
reg->template remove<Component>(entt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if an actor owns the given tag.
|
||||
* @tparam Tag Type of the tag for which to perform the check.
|
||||
* @return True if the actor owns the tag, false otherwise.
|
||||
*/
|
||||
template<typename Tag>
|
||||
bool has(tag_t) const ENTT_NOEXCEPT {
|
||||
return (reg->template has<Tag>() && (reg->template attachee<Tag>() == entt));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if an actor has the given component.
|
||||
* @tparam Component Type of the component for which to perform the check.
|
||||
@@ -158,60 +116,48 @@ struct Actor {
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given tag for an actor.
|
||||
* @tparam Tag Type of the tag to get.
|
||||
* @return A reference to the instance of the tag owned by the actor.
|
||||
* @brief Returns references to the given components for an actor.
|
||||
* @tparam Component Types of components to get.
|
||||
* @return References to the components owned by the actor.
|
||||
*/
|
||||
template<typename Tag>
|
||||
const Tag & get(tag_t) const ENTT_NOEXCEPT {
|
||||
assert(has<Tag>(tag_t{}));
|
||||
return reg->template get<Tag>();
|
||||
template<typename... Component>
|
||||
decltype(auto) get() const ENTT_NOEXCEPT {
|
||||
return std::as_const(*reg).template get<Component...>(entt);
|
||||
}
|
||||
|
||||
/*! @copydoc get */
|
||||
template<typename... Component>
|
||||
decltype(auto) get() ENTT_NOEXCEPT {
|
||||
return reg->template get<Component...>(entt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given tag for an actor.
|
||||
* @tparam Tag Type of the tag to get.
|
||||
* @return A reference to the instance of the tag owned by the actor.
|
||||
* @brief Returns pointers to the given components for an actor.
|
||||
* @tparam Component Types of components to get.
|
||||
* @return Pointers to the components owned by the actor.
|
||||
*/
|
||||
template<typename Tag>
|
||||
inline Tag & get(tag_t) ENTT_NOEXCEPT {
|
||||
return const_cast<Tag &>(const_cast<const Actor *>(this)->get<Tag>(tag_t{}));
|
||||
template<typename... Component>
|
||||
auto try_get() const ENTT_NOEXCEPT {
|
||||
return std::as_const(*reg).template try_get<Component...>(entt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 actor.
|
||||
*/
|
||||
template<typename Component>
|
||||
const Component & get() const ENTT_NOEXCEPT {
|
||||
return reg->template get<Component>(entt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 actor.
|
||||
*/
|
||||
template<typename Component>
|
||||
inline Component & get() ENTT_NOEXCEPT {
|
||||
return const_cast<Component &>(const_cast<const Actor *>(this)->get<Component>());
|
||||
/*! @copydoc try_get */
|
||||
template<typename... Component>
|
||||
auto try_get() ENTT_NOEXCEPT {
|
||||
return reg->template try_get<Component...>(entt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the underlying registry.
|
||||
* @return A reference to the underlying registry.
|
||||
*/
|
||||
inline const registry_type & registry() const ENTT_NOEXCEPT {
|
||||
inline const registry_type & backend() const ENTT_NOEXCEPT {
|
||||
return *reg;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the underlying registry.
|
||||
* @return A reference to the underlying registry.
|
||||
*/
|
||||
inline registry_type & registry() ENTT_NOEXCEPT {
|
||||
return const_cast<registry_type &>(const_cast<const Actor *>(this)->registry());
|
||||
/*! @copydoc backend */
|
||||
inline registry_type & backend() ENTT_NOEXCEPT {
|
||||
return const_cast<registry_type &>(std::as_const(*this).backend());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -223,20 +169,11 @@ struct Actor {
|
||||
}
|
||||
|
||||
private:
|
||||
registry_type * reg;
|
||||
registry_type *reg;
|
||||
Entity entt;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
using DefaultActor = Actor<DefaultRegistry::entity_type>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,230 +0,0 @@
|
||||
#ifndef ENTT_ENTITY_ATTACHEE_HPP
|
||||
#define ENTT_ENTITY_ATTACHEE_HPP
|
||||
|
||||
|
||||
#include <cassert>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "entity.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Attachee.
|
||||
*
|
||||
* Primary template isn't defined on purpose. All the specializations give a
|
||||
* compile-time error, but for a few reasonable cases.
|
||||
*/
|
||||
template<typename...>
|
||||
class Attachee;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic attachee implementation.
|
||||
*
|
||||
* Convenience data structure used to store single instance components.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class Attachee<Entity> {
|
||||
public:
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = Entity;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Attachee() ENTT_NOEXCEPT
|
||||
: owner{null}
|
||||
{}
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Attachee(const Attachee &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Attachee(Attachee &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This attachee. */
|
||||
Attachee & operator=(const Attachee &) = default;
|
||||
/*! @brief Default move assignment operator. @return This attachee. */
|
||||
Attachee & operator=(Attachee &&) = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Attachee() ENTT_NOEXCEPT = default;
|
||||
|
||||
/**
|
||||
* @brief Returns the owner of an attachee.
|
||||
* @return A valid entity identifier if an owner exists, the null entity
|
||||
* identifier otherwise.
|
||||
*/
|
||||
inline entity_type get() const ENTT_NOEXCEPT {
|
||||
return owner;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns an entity to an attachee.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to assigns an entity to an attachee that already has an owner
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode in case
|
||||
* the attachee already has an owner.
|
||||
*
|
||||
* @param entity A valid entity identifier.
|
||||
*/
|
||||
inline void construct(const entity_type entity) ENTT_NOEXCEPT {
|
||||
assert(owner == null);
|
||||
owner = entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an entity from an attachee.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to free an empty attachee results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* attachee is already empty.
|
||||
*/
|
||||
virtual void destroy() ENTT_NOEXCEPT {
|
||||
assert(owner != null);
|
||||
owner = null;
|
||||
}
|
||||
|
||||
private:
|
||||
entity_type owner;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Extended attachee implementation.
|
||||
*
|
||||
* This specialization of an attachee associates an object to an entity. The
|
||||
* main purpose of this class is to use attachees to store tags in a Registry.
|
||||
* It guarantees fast access both to the element and to the entity.
|
||||
*
|
||||
* @sa Attachee<Entity>
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Type Type of object assigned to the entity.
|
||||
*/
|
||||
template<typename Entity, typename Type>
|
||||
class Attachee<Entity, Type>: public Attachee<Entity> {
|
||||
using underlying_type = Attachee<Entity>;
|
||||
|
||||
public:
|
||||
/*! @brief Type of the object associated to the attachee. */
|
||||
using object_type = Type;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename underlying_type::entity_type;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Attachee() ENTT_NOEXCEPT = default;
|
||||
|
||||
/*! @brief Copying an attachee isn't allowed. */
|
||||
Attachee(const Attachee &) = delete;
|
||||
/*! @brief Moving an attachee isn't allowed. */
|
||||
Attachee(Attachee &&) = delete;
|
||||
|
||||
/*! @brief Copying an attachee isn't allowed. @return This attachee. */
|
||||
Attachee & operator=(const Attachee &) = delete;
|
||||
/*! @brief Moving an attachee isn't allowed. @return This attachee. */
|
||||
Attachee & operator=(Attachee &&) = delete;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Attachee() {
|
||||
if(underlying_type::get() != null) {
|
||||
reinterpret_cast<Type *>(&storage)->~Type();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated to an attachee.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to query an empty attachee results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* attachee is empty.
|
||||
*
|
||||
* @return The object associated to the attachee.
|
||||
*/
|
||||
const Type & get() const ENTT_NOEXCEPT {
|
||||
assert(underlying_type::get() != null);
|
||||
return *reinterpret_cast<const Type *>(&storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated to an attachee.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to query an empty attachee results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* attachee is empty.
|
||||
*
|
||||
* @return The object associated to the attachee.
|
||||
*/
|
||||
Type & get() ENTT_NOEXCEPT {
|
||||
return const_cast<Type &>(const_cast<const Attachee *>(this)->get());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns an entity to an attachee and constructs its object.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to assigns an entity to an attachee that already has an owner
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode in case
|
||||
* the attachee already has an owner.
|
||||
*
|
||||
* @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 attachee.
|
||||
*/
|
||||
template<typename... Args>
|
||||
Type & construct(entity_type entity, Args &&... args) ENTT_NOEXCEPT {
|
||||
underlying_type::construct(entity);
|
||||
new (&storage) Type{std::forward<Args>(args)...};
|
||||
return *reinterpret_cast<Type *>(&storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an entity from an attachee and destroies its object.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to free an empty attachee results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* attachee is already empty.
|
||||
*/
|
||||
void destroy() ENTT_NOEXCEPT override {
|
||||
reinterpret_cast<Type *>(&storage)->~Type();
|
||||
underlying_type::destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Changes the owner of an attachee.
|
||||
*
|
||||
* The ownership of the attachee is transferred from one entity to another.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to transfer the ownership of an attachee that hasn't an owner
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode in case
|
||||
* the attachee hasn't an owner yet.
|
||||
*
|
||||
* @param entity A valid entity identifier.
|
||||
*/
|
||||
void move(const entity_type entity) ENTT_NOEXCEPT {
|
||||
underlying_type::destroy();
|
||||
underlying_type::construct(entity);
|
||||
}
|
||||
|
||||
private:
|
||||
std::aligned_storage_t<sizeof(Type), alignof(Type)> storage;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_ATTACHEE_HPP
|
||||
@@ -3,12 +3,99 @@
|
||||
|
||||
|
||||
#include "../config/config.h"
|
||||
#include "entt_traits.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Entity traits.
|
||||
*
|
||||
* Primary template isn't defined on purpose. All the specializations give a
|
||||
* compile-time error unless the template parameter is an accepted entity type.
|
||||
*/
|
||||
template<typename>
|
||||
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]).
|
||||
*/
|
||||
template<>
|
||||
struct entt_traits<std::uint16_t> {
|
||||
/*! @brief Underlying entity type. */
|
||||
using entity_type = std::uint16_t;
|
||||
/*! @brief Underlying version type. */
|
||||
using version_type = std::uint8_t;
|
||||
/*! @brief Difference type. */
|
||||
using difference_type = std::int32_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr std::uint16_t entity_mask = 0xFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr std::uint16_t version_mask = 0xF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto entity_shift = 12;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Entity traits for a 32 bits entity identifier.
|
||||
*
|
||||
* A 32 bits entity identifier guarantees:
|
||||
*
|
||||
* * 20 bits for the entity number (suitable for almost all the games).
|
||||
* * 12 bit for the version (resets in [0-4095]).
|
||||
*/
|
||||
template<>
|
||||
struct entt_traits<std::uint32_t> {
|
||||
/*! @brief Underlying entity type. */
|
||||
using entity_type = std::uint32_t;
|
||||
/*! @brief Underlying version type. */
|
||||
using version_type = std::uint16_t;
|
||||
/*! @brief Difference type. */
|
||||
using difference_type = std::int64_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr std::uint32_t entity_mask = 0xFFFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr std::uint32_t version_mask = 0xFFF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto entity_shift = 20;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Entity traits for a 64 bits entity identifier.
|
||||
*
|
||||
* A 64 bits entity identifier guarantees:
|
||||
*
|
||||
* * 32 bits for the entity number (an indecently large number).
|
||||
* * 32 bit for the version (an indecently large number).
|
||||
*/
|
||||
template<>
|
||||
struct entt_traits<std::uint64_t> {
|
||||
/*! @brief Underlying entity type. */
|
||||
using entity_type = std::uint64_t;
|
||||
/*! @brief Underlying version type. */
|
||||
using version_type = std::uint32_t;
|
||||
/*! @brief Difference type. */
|
||||
using difference_type = std::int64_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr std::uint64_t entity_mask = 0xFFFFFFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr std::uint64_t version_mask = 0xFFFFFFFF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto entity_shift = 32;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @cond TURN_OFF_DOXYGEN
|
||||
* Internal details not to be documented.
|
||||
@@ -18,20 +105,18 @@ namespace entt {
|
||||
namespace internal {
|
||||
|
||||
|
||||
struct Null {
|
||||
explicit constexpr Null() = default;
|
||||
|
||||
struct null {
|
||||
template<typename Entity>
|
||||
constexpr operator Entity() const ENTT_NOEXCEPT {
|
||||
using traits_type = entt::entt_traits<Entity>;
|
||||
using traits_type = entt_traits<Entity>;
|
||||
return traits_type::entity_mask | (traits_type::version_mask << traits_type::entity_shift);
|
||||
}
|
||||
|
||||
constexpr bool operator==(Null) const ENTT_NOEXCEPT {
|
||||
constexpr bool operator==(null) const ENTT_NOEXCEPT {
|
||||
return true;
|
||||
}
|
||||
|
||||
constexpr bool operator!=(Null) const ENTT_NOEXCEPT {
|
||||
constexpr bool operator!=(null) const ENTT_NOEXCEPT {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -48,14 +133,14 @@ struct Null {
|
||||
|
||||
|
||||
template<typename Entity>
|
||||
constexpr bool operator==(const Entity entity, Null null) ENTT_NOEXCEPT {
|
||||
return null == entity;
|
||||
constexpr bool operator==(const Entity entity, null other) ENTT_NOEXCEPT {
|
||||
return other == entity;
|
||||
}
|
||||
|
||||
|
||||
template<typename Entity>
|
||||
constexpr bool operator!=(const Entity entity, Null null) ENTT_NOEXCEPT {
|
||||
return null != entity;
|
||||
constexpr bool operator!=(const Entity entity, null other) ENTT_NOEXCEPT {
|
||||
return other != entity;
|
||||
}
|
||||
|
||||
|
||||
@@ -75,7 +160,7 @@ constexpr bool operator!=(const Entity entity, Null null) ENTT_NOEXCEPT {
|
||||
* any allowed type. Similarly, there exist comparision operators between the
|
||||
* null entity and any other entity identifier.
|
||||
*/
|
||||
constexpr auto null = internal::Null{};
|
||||
constexpr auto null = internal::null{};
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,102 +0,0 @@
|
||||
#ifndef ENTT_ENTITY_ENTT_TRAITS_HPP
|
||||
#define ENTT_ENTITY_ENTT_TRAITS_HPP
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Entity traits.
|
||||
*
|
||||
* Primary template isn't defined on purpose. All the specializations give a
|
||||
* compile-time error unless the template parameter is an accepted entity type.
|
||||
*/
|
||||
template<typename>
|
||||
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]).
|
||||
*/
|
||||
template<>
|
||||
struct entt_traits<std::uint16_t> {
|
||||
/*! @brief Underlying entity type. */
|
||||
using entity_type = std::uint16_t;
|
||||
/*! @brief Underlying version type. */
|
||||
using version_type = std::uint8_t;
|
||||
/*! @brief Difference type. */
|
||||
using difference_type = std::int32_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr std::uint16_t entity_mask = 0xFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr std::uint16_t version_mask = 0xF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto entity_shift = 12;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Entity traits for a 32 bits entity identifier.
|
||||
*
|
||||
* A 32 bits entity identifier guarantees:
|
||||
*
|
||||
* * 20 bits for the entity number (suitable for almost all the games).
|
||||
* * 12 bit for the version (resets in [0-4095]).
|
||||
*/
|
||||
template<>
|
||||
struct entt_traits<std::uint32_t> {
|
||||
/*! @brief Underlying entity type. */
|
||||
using entity_type = std::uint32_t;
|
||||
/*! @brief Underlying version type. */
|
||||
using version_type = std::uint16_t;
|
||||
/*! @brief Difference type. */
|
||||
using difference_type = std::int64_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr std::uint32_t entity_mask = 0xFFFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr std::uint32_t version_mask = 0xFFF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto entity_shift = 20;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Entity traits for a 64 bits entity identifier.
|
||||
*
|
||||
* A 64 bits entity identifier guarantees:
|
||||
*
|
||||
* * 32 bits for the entity number (an indecently large number).
|
||||
* * 32 bit for the version (an indecently large number).
|
||||
*/
|
||||
template<>
|
||||
struct entt_traits<std::uint64_t> {
|
||||
/*! @brief Underlying entity type. */
|
||||
using entity_type = std::uint64_t;
|
||||
/*! @brief Underlying version type. */
|
||||
using version_type = std::uint32_t;
|
||||
/*! @brief Difference type. */
|
||||
using difference_type = std::int64_t;
|
||||
|
||||
/*! @brief Mask to use to get the entity number out of an identifier. */
|
||||
static constexpr std::uint64_t entity_mask = 0xFFFFFFFF;
|
||||
/*! @brief Mask to use to get the version out of an identifier. */
|
||||
static constexpr std::uint64_t version_mask = 0xFFFFFFFF;
|
||||
/*! @brief Extent of the entity number within an identifier. */
|
||||
static constexpr auto entity_shift = 32;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_ENTT_TRAITS_HPP
|
||||
89
src/entt/entity/fwd.hpp
Normal file
89
src/entt/entity/fwd.hpp
Normal file
@@ -0,0 +1,89 @@
|
||||
#ifndef ENTT_ENTITY_FWD_HPP
|
||||
#define ENTT_ENTITY_FWD_HPP
|
||||
|
||||
|
||||
#include <cstdint>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
/*! @class basic_registry */
|
||||
template <typename>
|
||||
class basic_registry;
|
||||
|
||||
/*! @class basic_view */
|
||||
template<typename, typename...>
|
||||
class basic_view;
|
||||
|
||||
/*! @class basic_runtime_view */
|
||||
template<typename>
|
||||
class basic_runtime_view;
|
||||
|
||||
/*! @class basic_group */
|
||||
template<typename...>
|
||||
class basic_group;
|
||||
|
||||
/*! @class basic_actor */
|
||||
template <typename>
|
||||
struct basic_actor;
|
||||
|
||||
/*! @class basic_prototype */
|
||||
template<typename>
|
||||
class basic_prototype;
|
||||
|
||||
/*! @class basic_snapshot */
|
||||
template<typename>
|
||||
class basic_snapshot;
|
||||
|
||||
/*! @class basic_snapshot_loader */
|
||||
template<typename>
|
||||
class basic_snapshot_loader;
|
||||
|
||||
/*! @class basic_continuous_loader */
|
||||
template<typename>
|
||||
class basic_continuous_loader;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using entity = std::uint32_t;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using registry = basic_registry<entity>;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using actor = basic_actor<entity>;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using prototype = basic_prototype<entity>;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using snapshot = basic_snapshot<entity>;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using snapshot_loader = basic_snapshot_loader<entity>;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using continuous_loader = basic_continuous_loader<entity>;
|
||||
|
||||
/**
|
||||
* @brief Alias declaration for the most common use case.
|
||||
* @tparam Component Types of components iterated by the view.
|
||||
*/
|
||||
template<typename... Types>
|
||||
using view = basic_view<entity, Types...>;
|
||||
|
||||
/*! @brief Alias declaration for the most common use case. */
|
||||
using runtime_view = basic_runtime_view<entity>;
|
||||
|
||||
/**
|
||||
* @brief Alias declaration for the most common use case.
|
||||
* @tparam Types Types of components iterated by the group.
|
||||
*/
|
||||
template<typename... Types>
|
||||
using group = basic_group<entity, Types...>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_FWD_HPP
|
||||
779
src/entt/entity/group.hpp
Normal file
779
src/entt/entity/group.hpp
Normal file
@@ -0,0 +1,779 @@
|
||||
#ifndef ENTT_ENTITY_GROUP_HPP
|
||||
#define ENTT_ENTITY_GROUP_HPP
|
||||
|
||||
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "../core/type_traits.hpp"
|
||||
#include "sparse_set.hpp"
|
||||
#include "storage.hpp"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Alias for lists of observed components.
|
||||
* @tparam Type List of types.
|
||||
*/
|
||||
template<typename... Type>
|
||||
struct get_t: type_list<Type...> {};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Variable template for lists of observed components.
|
||||
* @tparam Type List of types.
|
||||
*/
|
||||
template<typename... Type>
|
||||
constexpr get_t<Type...> get{};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Group.
|
||||
*
|
||||
* Primary template isn't defined on purpose. All the specializations give a
|
||||
* compile-time error, but for a few reasonable cases.
|
||||
*/
|
||||
template<typename...>
|
||||
class basic_group;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Non-owning group.
|
||||
*
|
||||
* A non-owning group returns all the entities and only the entities that have
|
||||
* at least the given components. Moreover, it's guaranteed that the entity list
|
||||
* is tightly packed in memory for fast iterations.<br/>
|
||||
* In general, non-owning groups don't stay true to the order of any set of
|
||||
* components unless users explicitly sort them.
|
||||
*
|
||||
* @b Important
|
||||
*
|
||||
* Iterators aren't invalidated if:
|
||||
*
|
||||
* * New instances of the given components are created and assigned to entities.
|
||||
* * The entity currently pointed is modified (as an example, if one of the
|
||||
* given components is removed from the entity to which the iterator points).
|
||||
*
|
||||
* In all the other cases, modifying the pools of the given components in any
|
||||
* way invalidates all the iterators and using them results in undefined
|
||||
* behavior.
|
||||
*
|
||||
* @note
|
||||
* Groups share references to the underlying data structures of the registry
|
||||
* that generated them. Therefore any change to the entities and to the
|
||||
* components made by means of the registry are immediately reflected by all the
|
||||
* groups.<br/>
|
||||
* Moreover, sorting a non-owning group affects all the instance of the same
|
||||
* group (it means that users don't have to call `sort` on each instance to sort
|
||||
* all of them because they share the set of entities).
|
||||
*
|
||||
* @warning
|
||||
* Lifetime of a group must overcome the one of the registry that generated it.
|
||||
* In any other case, attempting to use a group results in undefined behavior.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Get Types of components observed by the group.
|
||||
*/
|
||||
template<typename Entity, typename... Get>
|
||||
class basic_group<Entity, get_t<Get...>> {
|
||||
static_assert(sizeof...(Get) > 0);
|
||||
|
||||
/*! @brief A registry is allowed to create groups. */
|
||||
friend class basic_registry<Entity>;
|
||||
|
||||
template<typename Component>
|
||||
using pool_type = std::conditional_t<std::is_const_v<Component>, const storage<Entity, std::remove_const_t<Component>>, storage<Entity, Component>>;
|
||||
|
||||
// we could use pool_type<Get> *..., but vs complains about it and refuses to compile for unknown reasons (likely a bug)
|
||||
basic_group(sparse_set<Entity> *ref, storage<Entity, std::remove_const_t<Get>> *... get) ENTT_NOEXCEPT
|
||||
: handler{ref},
|
||||
pools{get...}
|
||||
{}
|
||||
|
||||
public:
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename sparse_set<Entity>::entity_type;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename sparse_set<Entity>::size_type;
|
||||
/*! @brief Input iterator type. */
|
||||
using iterator_type = typename sparse_set<Entity>::iterator_type;
|
||||
|
||||
/**
|
||||
* @brief Returns the number of existing components of the given type.
|
||||
* @tparam Component Type of component of which to return the size.
|
||||
* @return Number of existing components of the given type.
|
||||
*/
|
||||
template<typename Component>
|
||||
size_type size() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the number of entities that have the given components.
|
||||
* @return Number of entities that have the given components.
|
||||
*/
|
||||
size_type size() const ENTT_NOEXCEPT {
|
||||
return handler->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the number of elements that a group has currently
|
||||
* allocated space for.
|
||||
* @return Capacity of the group.
|
||||
*/
|
||||
size_type capacity() const ENTT_NOEXCEPT {
|
||||
return handler->capacity();
|
||||
}
|
||||
|
||||
/*! @brief Requests the removal of unused capacity. */
|
||||
void shrink_to_fit() {
|
||||
handler->shrink_to_fit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the pool of a given component is empty.
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return True if the pool of the given component is empty, false
|
||||
* otherwise.
|
||||
*/
|
||||
template<typename Component>
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the group is empty.
|
||||
* @return True if the group is empty, false otherwise.
|
||||
*/
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return handler->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the list of components of a given pool.
|
||||
*
|
||||
* The returned pointer is such that range
|
||||
* `[raw<Component>(), raw<Component>() + size<Component>()]` is always a
|
||||
* valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order of the components. Use `begin` and
|
||||
* `end` if you want to iterate the group in the expected order.
|
||||
*
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return A pointer to the array of components.
|
||||
*/
|
||||
template<typename Component>
|
||||
Component * raw() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->raw();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the list of entities of a given pool.
|
||||
*
|
||||
* The returned pointer is such that range
|
||||
* `[data<Component>(), data<Component>() + size<Component>()]` is always a
|
||||
* valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order of the entities. Use `begin` and
|
||||
* `end` if you want to iterate the group in the expected order.
|
||||
*
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return A pointer to the array of entities.
|
||||
*/
|
||||
template<typename Component>
|
||||
const entity_type * data() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->data();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the list of entities.
|
||||
*
|
||||
* The returned pointer is such that range `[data(), data() + size()]` is
|
||||
* always a valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order of the entities. Use `begin` and
|
||||
* `end` if you want to iterate the group in the expected order.
|
||||
*
|
||||
* @return A pointer to the array of entities.
|
||||
*/
|
||||
const entity_type * data() const ENTT_NOEXCEPT {
|
||||
return handler->data();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the first entity that has the given
|
||||
* components.
|
||||
*
|
||||
* The returned iterator points to the first entity that has the given
|
||||
* components. If the group is empty, the returned iterator will be equal to
|
||||
* `end()`.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed to the underlying data
|
||||
* structures.
|
||||
*
|
||||
* @return An iterator to the first entity that has the given components.
|
||||
*/
|
||||
iterator_type begin() const ENTT_NOEXCEPT {
|
||||
return handler->begin();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator that is past the last entity that has the
|
||||
* given components.
|
||||
*
|
||||
* The returned iterator points to the entity following the last entity that
|
||||
* has the given components. Attempting to dereference the returned iterator
|
||||
* results in undefined behavior.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed to the underlying data
|
||||
* structures.
|
||||
*
|
||||
* @return An iterator to the entity following the last entity that has the
|
||||
* given components.
|
||||
*/
|
||||
iterator_type end() const ENTT_NOEXCEPT {
|
||||
return handler->end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Finds an entity.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return An iterator to the given entity if it's found, past the end
|
||||
* iterator otherwise.
|
||||
*/
|
||||
iterator_type find(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
const auto it = handler->find(entt);
|
||||
return it != end() && *it == entt ? it : end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the identifier that occupies the given position.
|
||||
* @param pos Position of the element to return.
|
||||
* @return The identifier that occupies the given position.
|
||||
*/
|
||||
entity_type operator[](const size_type pos) const ENTT_NOEXCEPT {
|
||||
return begin()[pos];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if a group contains an entity.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return True if the group contains the given entity, false otherwise.
|
||||
*/
|
||||
bool contains(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
return find(entt) != end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the components assigned to the given entity.
|
||||
*
|
||||
* Prefer this function instead of `registry::get` during iterations. It has
|
||||
* far better performance than its companion function.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an invalid component type results in a compilation
|
||||
* error. Attempting to use an entity that doesn't belong to the group
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* group doesn't contain the given entity.
|
||||
*
|
||||
* @tparam Component Types of components to get.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return The components assigned to the entity.
|
||||
*/
|
||||
template<typename... Component>
|
||||
decltype(auto) get([[maybe_unused]] const entity_type entt) const ENTT_NOEXCEPT {
|
||||
ENTT_ASSERT(contains(entt));
|
||||
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
|
||||
} else {
|
||||
return std::tuple<decltype(get<Component>(entt))...>{get<Component>(entt)...};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterates entities and components and applies the given function
|
||||
* object to them.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a set of references to all its components. The
|
||||
* _constness_ of the components is as requested.<br/>
|
||||
* The signature of the function must be equivalent to one of the following
|
||||
* forms:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(const entity_type, Get &...);
|
||||
* void(Get &...);
|
||||
* @endcode
|
||||
*
|
||||
* @note
|
||||
* Empty types aren't explicitly instantiated. Therefore, temporary objects
|
||||
* are returned during iterations. They can be caught only by copy or with
|
||||
* const references.
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
inline void each(Func func) const {
|
||||
for(const auto entt: *handler) {
|
||||
if constexpr(std::is_invocable_v<Func, decltype(get<Get>({}))...>) {
|
||||
func(std::get<pool_type<Get> *>(pools)->get(entt)...);
|
||||
} else {
|
||||
func(entt, std::get<pool_type<Get> *>(pools)->get(entt)...);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sort the shared pool of entities according to the given component.
|
||||
*
|
||||
* Non-owning groups of the same type share with the registry a pool of
|
||||
* entities with its own order that doesn't depend on the order of any pool
|
||||
* of components. Users can order the underlying data structure so that it
|
||||
* respects the order of the pool of the given component.
|
||||
*
|
||||
* @note
|
||||
* The shared pool of entities and thus its order is affected by the changes
|
||||
* to each and every pool that it tracks. Therefore changes to those pools
|
||||
* can quickly ruin the order imposed to the pool of entities shared between
|
||||
* the non-owning groups.
|
||||
*
|
||||
* @tparam Component Type of component to use to impose the order.
|
||||
*/
|
||||
template<typename Component>
|
||||
void sort() const {
|
||||
handler->respect(*std::get<pool_type<Component> *>(pools));
|
||||
}
|
||||
|
||||
private:
|
||||
sparse_set<entity_type> *handler;
|
||||
const std::tuple<pool_type<Get> *...> pools;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Owning group.
|
||||
*
|
||||
* Owning groups return all the entities and only the entities that have at
|
||||
* least the given components. Moreover:
|
||||
*
|
||||
* * It's guaranteed that the entity list is tightly packed in memory for fast
|
||||
* iterations.
|
||||
* * It's guaranteed that the lists of owned components are tightly packed in
|
||||
* memory for even faster iterations and to allow direct access.
|
||||
* * They stay true to the order of the owned components and all the owned
|
||||
* components have the same order in memory.
|
||||
*
|
||||
* The more types of components are owned by a group, the faster it is to
|
||||
* iterate them.
|
||||
*
|
||||
* @b Important
|
||||
*
|
||||
* Iterators aren't invalidated if:
|
||||
*
|
||||
* * New instances of the given components are created and assigned to entities.
|
||||
* * The entity currently pointed is modified (as an example, if one of the
|
||||
* given components is removed from the entity to which the iterator points).
|
||||
*
|
||||
* In all the other cases, modifying the pools of the given components in any
|
||||
* way invalidates all the iterators and using them results in undefined
|
||||
* behavior.
|
||||
*
|
||||
* @note
|
||||
* Groups share references to the underlying data structures of the registry
|
||||
* that generated them. Therefore any change to the entities and to the
|
||||
* components made by means of the registry are immediately reflected by all the
|
||||
* groups.
|
||||
* Moreover, sorting an owning group affects all the instance of the same group
|
||||
* (it means that users don't have to call `sort` on each instance to sort all
|
||||
* of them because they share the underlying data structure).
|
||||
*
|
||||
* @warning
|
||||
* Lifetime of a group must overcome the one of the registry that generated it.
|
||||
* In any other case, attempting to use a group results in undefined behavior.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Get Types of components observed by the group.
|
||||
* @tparam Owned Types of components owned by the group.
|
||||
*/
|
||||
template<typename Entity, typename... Get, typename... Owned>
|
||||
class basic_group<Entity, get_t<Get...>, Owned...> {
|
||||
static_assert(sizeof...(Get) + sizeof...(Owned) > 0);
|
||||
|
||||
/*! @brief A registry is allowed to create groups. */
|
||||
friend class basic_registry<Entity>;
|
||||
|
||||
template<typename Component>
|
||||
using pool_type = std::conditional_t<std::is_const_v<Component>, const storage<Entity, std::remove_const_t<Component>>, storage<Entity, Component>>;
|
||||
|
||||
template<typename Component>
|
||||
using component_iterator_type = decltype(std::declval<pool_type<Component>>().begin());
|
||||
|
||||
template<typename Component>
|
||||
decltype(auto) from_index(const typename sparse_set<Entity>::size_type index) {
|
||||
static_assert(!std::is_empty_v<Component>);
|
||||
|
||||
if constexpr(std::disjunction_v<std::is_same<Component, Owned>...>) {
|
||||
return std::as_const(*std::get<pool_type<Component> *>(pools)).raw()[index];
|
||||
} else {
|
||||
return std::as_const(*std::get<pool_type<Component> *>(pools)).get(data()[index]);
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
inline auto swap(int, pool_type<Component> *cpool, const std::size_t lhs, const std::size_t rhs)
|
||||
-> decltype(cpool->raw(), void()) {
|
||||
std::swap(cpool->raw()[lhs], cpool->raw()[rhs]);
|
||||
cpool->swap(lhs, rhs);
|
||||
}
|
||||
|
||||
template<typename Component>
|
||||
inline void swap(char, pool_type<Component> *cpool, const std::size_t lhs, const std::size_t rhs) {
|
||||
cpool->swap(lhs, rhs);
|
||||
}
|
||||
|
||||
// we could use pool_type<Type> *..., but vs complains about it and refuses to compile for unknown reasons (likely a bug)
|
||||
basic_group(const typename basic_registry<Entity>::size_type *sz, storage<Entity, std::remove_const_t<Owned>> *... owned, storage<Entity, std::remove_const_t<Get>> *... get) ENTT_NOEXCEPT
|
||||
: length{sz},
|
||||
pools{owned..., get...}
|
||||
{}
|
||||
|
||||
public:
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename sparse_set<Entity>::entity_type;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename sparse_set<Entity>::size_type;
|
||||
/*! @brief Input iterator type. */
|
||||
using iterator_type = typename sparse_set<Entity>::iterator_type;
|
||||
|
||||
/**
|
||||
* @brief Returns the number of existing components of the given type.
|
||||
* @tparam Component Type of component of which to return the size.
|
||||
* @return Number of existing components of the given type.
|
||||
*/
|
||||
template<typename Component>
|
||||
size_type size() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the number of entities that have the given components.
|
||||
* @return Number of entities that have the given components.
|
||||
*/
|
||||
size_type size() const ENTT_NOEXCEPT {
|
||||
return *length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the pool of a given component is empty.
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return True if the pool of the given component is empty, false
|
||||
* otherwise.
|
||||
*/
|
||||
template<typename Component>
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks whether the group is empty.
|
||||
* @return True if the group is empty, false otherwise.
|
||||
*/
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return !*length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the list of components of a given pool.
|
||||
*
|
||||
* The returned pointer is such that range
|
||||
* `[raw<Component>(), raw<Component>() + size<Component>()]` is always a
|
||||
* valid range, even if the container is empty.<br/>
|
||||
* Moreover, in case the group owns the given component, the range
|
||||
* `[raw<Component>(), raw<Component>() + size()]` is such that it contains
|
||||
* the instances that are part of the group itself.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order of the components. Use `begin` and
|
||||
* `end` if you want to iterate the group in the expected order.
|
||||
*
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return A pointer to the array of components.
|
||||
*/
|
||||
template<typename Component>
|
||||
Component * raw() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->raw();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the list of entities of a given pool.
|
||||
*
|
||||
* The returned pointer is such that range
|
||||
* `[data<Component>(), data<Component>() + size<Component>()]` is always a
|
||||
* valid range, even if the container is empty.<br/>
|
||||
* Moreover, in case the group owns the given component, the range
|
||||
* `[data<Component>(), data<Component>() + size()]` is such that it
|
||||
* contains the entities that are part of the group itself.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order of the entities. Use `begin` and
|
||||
* `end` if you want to iterate the group in the expected order.
|
||||
*
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return A pointer to the array of entities.
|
||||
*/
|
||||
template<typename Component>
|
||||
const entity_type * data() const ENTT_NOEXCEPT {
|
||||
return std::get<pool_type<Component> *>(pools)->data();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the list of entities.
|
||||
*
|
||||
* The returned pointer is such that range `[data(), data() + size()]` is
|
||||
* always a valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* There are no guarantees on the order of the entities. Use `begin` and
|
||||
* `end` if you want to iterate the group in the expected order.
|
||||
*
|
||||
* @return A pointer to the array of entities.
|
||||
*/
|
||||
const entity_type * data() const ENTT_NOEXCEPT {
|
||||
return std::get<0>(pools)->data();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the first entity that has the given
|
||||
* components.
|
||||
*
|
||||
* The returned iterator points to the first entity that has the given
|
||||
* components. If the group is empty, the returned iterator will be equal to
|
||||
* `end()`.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed to the underlying data
|
||||
* structures.
|
||||
*
|
||||
* @return An iterator to the first entity that has the given components.
|
||||
*/
|
||||
iterator_type begin() const ENTT_NOEXCEPT {
|
||||
return std::get<0>(pools)->sparse_set<entity_type>::end() - *length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator that is past the last entity that has the
|
||||
* given components.
|
||||
*
|
||||
* The returned iterator points to the entity following the last entity that
|
||||
* has the given components. Attempting to dereference the returned iterator
|
||||
* results in undefined behavior.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed to the underlying data
|
||||
* structures.
|
||||
*
|
||||
* @return An iterator to the entity following the last entity that has the
|
||||
* given components.
|
||||
*/
|
||||
iterator_type end() const ENTT_NOEXCEPT {
|
||||
return std::get<0>(pools)->sparse_set<entity_type>::end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Finds an entity.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return An iterator to the given entity if it's found, past the end
|
||||
* iterator otherwise.
|
||||
*/
|
||||
iterator_type find(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
const auto it = std::get<0>(pools)->find(entt);
|
||||
return it != end() && it >= begin() && *it == entt ? it : end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the identifier that occupies the given position.
|
||||
* @param pos Position of the element to return.
|
||||
* @return The identifier that occupies the given position.
|
||||
*/
|
||||
entity_type operator[](const size_type pos) const ENTT_NOEXCEPT {
|
||||
return begin()[pos];
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if a group contains an entity.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return True if the group contains the given entity, false otherwise.
|
||||
*/
|
||||
bool contains(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
return find(entt) != end();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the components assigned to the given entity.
|
||||
*
|
||||
* Prefer this function instead of `registry::get` during iterations. It has
|
||||
* far better performance than its companion function.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an invalid component type results in a compilation
|
||||
* error. Attempting to use an entity that doesn't belong to the group
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* group doesn't contain the given entity.
|
||||
*
|
||||
* @tparam Component Types of components to get.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return The components assigned to the entity.
|
||||
*/
|
||||
template<typename... Component>
|
||||
decltype(auto) get([[maybe_unused]] const entity_type entt) const ENTT_NOEXCEPT {
|
||||
ENTT_ASSERT(contains(entt));
|
||||
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
return (std::get<pool_type<Component> *>(pools)->get(entt), ...);
|
||||
} else {
|
||||
return std::tuple<decltype(get<Component>(entt))...>{get<Component>(entt)...};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterates entities and components and applies the given function
|
||||
* object to them.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a set of references to all its components. The
|
||||
* _constness_ of the components is as requested.<br/>
|
||||
* The signature of the function must be equivalent to one of the following
|
||||
* forms:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(const entity_type, Owned &..., Get &...);
|
||||
* void(Owned &..., Get &...);
|
||||
* @endcode
|
||||
*
|
||||
* @note
|
||||
* Empty types aren't explicitly instantiated. Therefore, temporary objects
|
||||
* are returned during iterations. They can be caught only by copy or with
|
||||
* const references.
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
inline void each(Func func) const {
|
||||
auto raw = std::make_tuple((std::get<pool_type<Owned> *>(pools)->end() - *length)...);
|
||||
[[maybe_unused]] auto data = std::get<0>(pools)->sparse_set<entity_type>::end() - *length;
|
||||
|
||||
for(auto next = *length; next; --next) {
|
||||
if constexpr(std::is_invocable_v<Func, decltype(get<Owned>({}))..., decltype(get<Get>({}))...>) {
|
||||
if constexpr(sizeof...(Get) == 0) {
|
||||
func(*(std::get<component_iterator_type<Owned>>(raw)++)...);
|
||||
} else {
|
||||
const auto entt = *(data++);
|
||||
func(*(std::get<component_iterator_type<Owned>>(raw)++)..., std::get<pool_type<Get> *>(pools)->get(entt)...);
|
||||
}
|
||||
} else {
|
||||
const auto entt = *(data++);
|
||||
func(entt, *(std::get<component_iterator_type<Owned>>(raw)++)..., std::get<pool_type<Get> *>(pools)->get(entt)...);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sort a group according to the given comparison function.
|
||||
*
|
||||
* Sort the group so that iterating it with a couple of iterators returns
|
||||
* entities and components 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 one of the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* bool(const Component &..., const Component &...);
|
||||
* bool(const Entity, const Entity);
|
||||
* @endcode
|
||||
*
|
||||
* Where `Component` are either owned types or not but still such that they
|
||||
* are iterated by the group.<br/>
|
||||
* Moreover, the comparison function object shall induce a
|
||||
* _strict weak ordering_ on the values.
|
||||
*
|
||||
* The sort function oject must offer a member function template
|
||||
* `operator()` that accepts three arguments:
|
||||
*
|
||||
* * An iterator to the first element of the range to sort.
|
||||
* * An iterator past the last element of the range to sort.
|
||||
* * A comparison function to use to compare the elements.
|
||||
*
|
||||
* The comparison function object received by the sort function object
|
||||
* hasn't necessarily the type of the one passed along with the other
|
||||
* parameters to this member function.
|
||||
*
|
||||
* @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
|
||||
* `sort` has been invoked.
|
||||
*
|
||||
* @tparam Component Optional types of components to compare.
|
||||
* @tparam Compare Type of comparison function object.
|
||||
* @tparam Sort Type of sort function object.
|
||||
* @tparam Args Types of arguments to forward to the sort function object.
|
||||
* @param compare A valid comparison function object.
|
||||
* @param algo A valid sort function object.
|
||||
* @param args Arguments to forward to the sort function object, if any.
|
||||
*/
|
||||
template<typename... Component, typename Compare, typename Sort = std_sort, typename... Args>
|
||||
void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
|
||||
std::vector<size_type> copy(*length);
|
||||
std::iota(copy.begin(), copy.end(), 0);
|
||||
|
||||
if constexpr(sizeof...(Component) == 0) {
|
||||
algo(copy.rbegin(), copy.rend(), [compare = std::move(compare), data = data()](const auto lhs, const auto rhs) {
|
||||
return compare(data[lhs], data[rhs]);
|
||||
}, std::forward<Args>(args)...);
|
||||
} else {
|
||||
algo(copy.rbegin(), copy.rend(), [compare = std::move(compare), this](const auto lhs, const auto rhs) {
|
||||
// useless this-> used to suppress a warning with clang
|
||||
return compare(this->from_index<Component>(lhs)..., this->from_index<Component>(rhs)...);
|
||||
}, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
for(size_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];
|
||||
(swap<Owned>(0, std::get<pool_type<Owned> *>(pools), lhs, rhs), ...);
|
||||
copy[curr] = curr;
|
||||
curr = next;
|
||||
next = copy[curr];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
const typename basic_registry<Entity>::size_type *length;
|
||||
const std::tuple<pool_type<Owned> *..., pool_type<Get> *...> pools;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_GROUP_HPP
|
||||
@@ -3,15 +3,116 @@
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "../core/hashed_string.hpp"
|
||||
#include "../signal/sigh.hpp"
|
||||
#include "registry.hpp"
|
||||
#include "utility.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Converts a registry to a view.
|
||||
* @tparam Const Constness of the accepted registry.
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<bool Const, typename Entity>
|
||||
struct as_view {
|
||||
/*! @brief Type of registry to convert. */
|
||||
using registry_type = std::conditional_t<Const, const entt::basic_registry<Entity>, entt::basic_registry<Entity>>;
|
||||
|
||||
/**
|
||||
* @brief Constructs a converter for a given registry.
|
||||
* @param source A valid reference to a registry.
|
||||
*/
|
||||
as_view(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
|
||||
|
||||
/**
|
||||
* @brief Conversion function from a registry to a view.
|
||||
* @tparam Component Type of components used to construct the view.
|
||||
* @return A newly created view.
|
||||
*/
|
||||
template<typename... Component>
|
||||
inline operator entt::basic_view<Entity, Component...>() const {
|
||||
return reg.template view<Component...>();
|
||||
}
|
||||
|
||||
private:
|
||||
registry_type ®
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Deduction guide.
|
||||
*
|
||||
* It allows to deduce the constness of a registry directly from the instance
|
||||
* provided to the constructor.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
as_view(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<false, Entity>;
|
||||
|
||||
|
||||
/*! @copydoc as_view */
|
||||
template<typename Entity>
|
||||
as_view(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_view<true, Entity>;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Converts a registry to a group.
|
||||
* @tparam Const Constness of the accepted registry.
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<bool Const, typename Entity>
|
||||
struct as_group {
|
||||
/*! @brief Type of registry to convert. */
|
||||
using registry_type = std::conditional_t<Const, const entt::basic_registry<Entity>, entt::basic_registry<Entity>>;
|
||||
|
||||
/**
|
||||
* @brief Constructs a converter for a given registry.
|
||||
* @param source A valid reference to a registry.
|
||||
*/
|
||||
as_group(registry_type &source) ENTT_NOEXCEPT: reg{source} {}
|
||||
|
||||
/**
|
||||
* @brief Conversion function from a registry to a group.
|
||||
*
|
||||
* @note
|
||||
* Unfortunately, only full owning groups are supported because of an issue
|
||||
* with msvc that doesn't manage to correctly deduce types.
|
||||
*
|
||||
* @tparam Owned Types of components owned by the group.
|
||||
* @return A newly created group.
|
||||
*/
|
||||
template<typename... Owned>
|
||||
inline operator entt::basic_group<Entity, get_t<>, Owned...>() const {
|
||||
return reg.template group<Owned...>();
|
||||
}
|
||||
|
||||
private:
|
||||
registry_type ®
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Deduction guide.
|
||||
*
|
||||
* It allows to deduce the constness of a registry directly from the instance
|
||||
* provided to the constructor.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
as_group(basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<false, Entity>;
|
||||
|
||||
|
||||
/*! @copydoc as_group */
|
||||
template<typename Entity>
|
||||
as_group(const basic_registry<Entity> &) ENTT_NOEXCEPT -> as_group<true, Entity>;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Dependency function prototype.
|
||||
*
|
||||
@@ -22,15 +123,14 @@ namespace entt {
|
||||
* It isn't intended for direct use, although nothing forbids using it freely.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Component Types of components to assign to an entity if triggered.
|
||||
* @param registry A valid reference to a registry.
|
||||
* @param entity A valid entity identifier.
|
||||
* @tparam Component Type of component that triggers the dependency handler.
|
||||
* @tparam Dependency Types of components to assign to an entity if triggered.
|
||||
* @param reg A valid reference to a registry.
|
||||
* @param entt A valid entity identifier.
|
||||
*/
|
||||
template<typename Entity, typename... Component>
|
||||
void dependency(Registry<Entity> ®istry, const Entity entity) {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { ((registry.template has<Component>(entity) ? void() : (registry.template assign<Component>(entity), void())), 0)... };
|
||||
(void)accumulator;
|
||||
template<typename Entity, typename Component, typename... Dependency>
|
||||
void dependency(basic_registry<Entity> ®, const Entity entt, const Component &) {
|
||||
((reg.template has<Dependency>(entt) ? void() : (reg.template assign<Dependency>(entt), void())), ...);
|
||||
}
|
||||
|
||||
|
||||
@@ -40,20 +140,21 @@ void dependency(Registry<Entity> ®istry, const Entity entity) {
|
||||
* A _dependency function_ is a built-in listener to use to automatically assign
|
||||
* components to an entity when a type has a dependency on some other types.
|
||||
*
|
||||
* The following adds components `AType` and `AnotherType` whenever `MyType` is
|
||||
* assigned to an entity:
|
||||
* The following adds components `a_type` and `another_type` whenever `my_type`
|
||||
* is assigned to an entity:
|
||||
* @code{.cpp}
|
||||
* entt::DefaultRegistry registry;
|
||||
* entt::connect<AType, AnotherType>(registry.construction<MyType>());
|
||||
* entt::registry registry;
|
||||
* entt::connect<a_type, another_type>(registry.construction<my_type>());
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Dependency Types of components to assign to an entity if triggered.
|
||||
* @tparam Component Type of component that triggers the dependency handler.
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @param sink A sink object properly initialized.
|
||||
*/
|
||||
template<typename... Dependency, typename Entity>
|
||||
inline void connect(Sink<void(Registry<Entity> &, const Entity)> sink) {
|
||||
sink.template connect<dependency<Entity, Dependency...>>();
|
||||
template<typename... Dependency, typename Component, typename Entity>
|
||||
inline void connect(sink<void(basic_registry<Entity> &, const Entity, Component &)> sink) {
|
||||
sink.template connect<dependency<Entity, Component, Dependency...>>();
|
||||
}
|
||||
|
||||
|
||||
@@ -63,40 +164,45 @@ inline void connect(Sink<void(Registry<Entity> &, const Entity)> sink) {
|
||||
* A _dependency function_ is a built-in listener to use to automatically assign
|
||||
* components to an entity when a type has a dependency on some other types.
|
||||
*
|
||||
* The following breaks the dependency between the component `MyType` and the
|
||||
* components `AType` and `AnotherType`:
|
||||
* The following breaks the dependency between the component `my_type` and the
|
||||
* components `a_type` and `another_type`:
|
||||
* @code{.cpp}
|
||||
* entt::DefaultRegistry registry;
|
||||
* entt::disconnect<AType, AnotherType>(registry.construction<MyType>());
|
||||
* entt::registry registry;
|
||||
* entt::disconnect<a_type, another_type>(registry.construction<my_type>());
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Dependency Types of components used to create the dependency.
|
||||
* @tparam Component Type of component that triggers the dependency handler.
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @param sink A sink object properly initialized.
|
||||
*/
|
||||
template<typename... Dependency, typename Entity>
|
||||
inline void disconnect(Sink<void(Registry<Entity> &, const Entity)> sink) {
|
||||
sink.template disconnect<dependency<Entity, Dependency...>>();
|
||||
template<typename... Dependency, typename Component, typename Entity>
|
||||
inline void disconnect(sink<void(basic_registry<Entity> &, const Entity, Component &)> sink) {
|
||||
sink.template disconnect<dependency<Entity, Component, Dependency...>>();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Alias template to ease the assignment of labels to entities.
|
||||
* @brief Alias template to ease the assignment of tags to entities.
|
||||
*
|
||||
* If used in combination with hashed strings, it simplifies the assignment of
|
||||
* labels to entities and the use of labels in general where a type would be
|
||||
* tags to entities and the use of tags in general where a type would be
|
||||
* required otherwise.<br/>
|
||||
* As an example and where the user defined literal for hashed strings hasn't
|
||||
* been changed:
|
||||
* @code{.cpp}
|
||||
* entt::DefaultRegistry registry;
|
||||
* registry.assign<entt::label<"enemy"_hs>>(entity);
|
||||
* entt::registry registry;
|
||||
* registry.assign<entt::tag<"enemy"_hs>>(entity);
|
||||
* @endcode
|
||||
*
|
||||
* @note
|
||||
* Tags are empty components and therefore candidates for the empty component
|
||||
* optimization.
|
||||
*
|
||||
* @tparam Value The numeric representation of an instance of hashed string.
|
||||
*/
|
||||
template<typename HashedString::hash_type Value>
|
||||
using label = std::integral_constant<typename HashedString::hash_type, Value>;
|
||||
template<typename hashed_string::hash_type Value>
|
||||
using tag = std::integral_constant<typename hashed_string::hash_type, Value>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "../config/config.h"
|
||||
#include "registry.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -37,27 +38,27 @@ namespace entt {
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class Prototype final {
|
||||
using basic_fn_type = void(const Prototype &, Registry<Entity> &, const Entity);
|
||||
using component_type = typename Registry<Entity>::component_type;
|
||||
class basic_prototype {
|
||||
using basic_fn_type = void(const basic_prototype &, basic_registry<Entity> &, const Entity);
|
||||
using component_type = typename basic_registry<Entity>::component_type;
|
||||
|
||||
template<typename Component>
|
||||
struct Wrapper { Component component; };
|
||||
struct component_wrapper { Component component; };
|
||||
|
||||
struct Handler {
|
||||
basic_fn_type *accommodate;
|
||||
struct component_handler {
|
||||
basic_fn_type *assign_or_replace;
|
||||
basic_fn_type *assign;
|
||||
};
|
||||
|
||||
void release() {
|
||||
if(registry->valid(entity)) {
|
||||
registry->destroy(entity);
|
||||
if(reg->valid(entity)) {
|
||||
reg->destroy(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Registry type. */
|
||||
using registry_type = Registry<Entity>;
|
||||
using registry_type = basic_registry<Entity>;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = Entity;
|
||||
/*! @brief Unsigned integer type. */
|
||||
@@ -65,23 +66,20 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Constructs a prototype that is bound to a given registry.
|
||||
* @param registry A valid reference to a registry.
|
||||
* @param ref A valid reference to a registry.
|
||||
*/
|
||||
Prototype(Registry<Entity> ®istry)
|
||||
: registry{®istry},
|
||||
entity{registry.create()}
|
||||
basic_prototype(registry_type &ref)
|
||||
: reg{&ref},
|
||||
entity{ref.create()}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Releases all its resources.
|
||||
*/
|
||||
~Prototype() {
|
||||
~basic_prototype() {
|
||||
release();
|
||||
}
|
||||
|
||||
/*! @brief Copying a prototype isn't allowed. */
|
||||
Prototype(const Prototype &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Move constructor.
|
||||
*
|
||||
@@ -91,17 +89,14 @@ public:
|
||||
*
|
||||
* @param other The instance to move from.
|
||||
*/
|
||||
Prototype(Prototype &&other)
|
||||
basic_prototype(basic_prototype &&other)
|
||||
: handlers{std::move(other.handlers)},
|
||||
registry{other.registry},
|
||||
reg{other.reg},
|
||||
entity{other.entity}
|
||||
{
|
||||
other.entity = entt::null;
|
||||
other.entity = null;
|
||||
}
|
||||
|
||||
/*! @brief Copying a prototype isn't allowed. @return This Prototype. */
|
||||
Prototype & operator=(const Prototype &) = delete;
|
||||
|
||||
/**
|
||||
* @brief Move assignment operator.
|
||||
*
|
||||
@@ -110,13 +105,13 @@ public:
|
||||
* continue using them.
|
||||
*
|
||||
* @param other The instance to move from.
|
||||
* @return This Prototype.
|
||||
* @return This prototype.
|
||||
*/
|
||||
Prototype & operator=(Prototype &&other) {
|
||||
basic_prototype & operator=(basic_prototype &&other) {
|
||||
if(this != &other) {
|
||||
auto tmp{std::move(other)};
|
||||
handlers.swap(tmp.handlers);
|
||||
std::swap(registry, tmp.registry);
|
||||
std::swap(reg, tmp.reg);
|
||||
std::swap(entity, tmp.entity);
|
||||
}
|
||||
|
||||
@@ -132,20 +127,22 @@ public:
|
||||
*/
|
||||
template<typename Component, typename... Args>
|
||||
Component & set(Args &&... args) {
|
||||
basic_fn_type *accommodate = [](const Prototype &prototype, Registry<Entity> &other, const Entity dst) {
|
||||
const auto &wrapper = prototype.registry->template get<Wrapper<Component>>(prototype.entity);
|
||||
other.template accommodate<Component>(dst, wrapper.component);
|
||||
component_handler handler;
|
||||
|
||||
handler.assign_or_replace = [](const basic_prototype &proto, registry_type &other, const Entity dst) {
|
||||
const auto &wrapper = proto.reg->template get<component_wrapper<Component>>(proto.entity);
|
||||
other.template assign_or_replace<Component>(dst, wrapper.component);
|
||||
};
|
||||
|
||||
basic_fn_type *assign = [](const Prototype &prototype, Registry<Entity> &other, const Entity dst) {
|
||||
handler.assign = [](const basic_prototype &proto, registry_type &other, const Entity dst) {
|
||||
if(!other.template has<Component>(dst)) {
|
||||
const auto &wrapper = prototype.registry->template get<Wrapper<Component>>(prototype.entity);
|
||||
const auto &wrapper = proto.reg->template get<component_wrapper<Component>>(proto.entity);
|
||||
other.template assign<Component>(dst, wrapper.component);
|
||||
}
|
||||
};
|
||||
|
||||
handlers[registry->template type<Component>()] = Handler{accommodate, assign};
|
||||
auto &wrapper = registry->template accommodate<Wrapper<Component>>(entity, Component{std::forward<Args>(args)...});
|
||||
handlers[reg->template type<Component>()] = handler;
|
||||
auto &wrapper = reg->template assign_or_replace<component_wrapper<Component>>(entity, Component{std::forward<Args>(args)...});
|
||||
return wrapper.component;
|
||||
}
|
||||
|
||||
@@ -155,8 +152,8 @@ public:
|
||||
*/
|
||||
template<typename Component>
|
||||
void unset() ENTT_NOEXCEPT {
|
||||
registry->template reset<Wrapper<Component>>(entity);
|
||||
handlers.erase(registry->template type<Component>());
|
||||
reg->template reset<component_wrapper<Component>>(entity);
|
||||
handlers.erase(reg->template type<Component>());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,11 +163,11 @@ public:
|
||||
*/
|
||||
template<typename... Component>
|
||||
bool has() const ENTT_NOEXCEPT {
|
||||
return registry->template has<Wrapper<Component>...>(entity);
|
||||
return reg->template has<component_wrapper<Component>...>(entity);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given component.
|
||||
* @brief Returns references to the given components.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to get a component from a prototype that doesn't own it
|
||||
@@ -178,65 +175,51 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* prototype doesn't own an instance of the given component.
|
||||
*
|
||||
* @tparam Component Type of component to get.
|
||||
* @return A reference to the component owned by the prototype.
|
||||
*/
|
||||
template<typename Component>
|
||||
const Component & get() const ENTT_NOEXCEPT {
|
||||
return registry->template get<Wrapper<Component>>(entity).component;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given component.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to get a component from a prototype that doesn't own it
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* prototype doesn't own an instance of the given component.
|
||||
*
|
||||
* @tparam Component Type of component to get.
|
||||
* @return A reference to the component owned by the prototype.
|
||||
*/
|
||||
template<typename Component>
|
||||
inline Component & get() ENTT_NOEXCEPT {
|
||||
return const_cast<Component &>(const_cast<const Prototype *>(this)->get<Component>());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given components.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to get components from a prototype that doesn't own them
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* prototype doesn't own instances of the given components.
|
||||
*
|
||||
* @tparam Component Type of components to get.
|
||||
* @tparam Component Types of components to get.
|
||||
* @return References to the components owned by the prototype.
|
||||
*/
|
||||
template<typename... Component>
|
||||
inline std::enable_if_t<(sizeof...(Component) > 1), std::tuple<const Component &...>>
|
||||
get() const ENTT_NOEXCEPT {
|
||||
return std::tuple<const Component &...>{get<Component>()...};
|
||||
decltype(auto) get() const ENTT_NOEXCEPT {
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
return (std::as_const(*reg).template get<component_wrapper<Component...>>(entity).component);
|
||||
} else {
|
||||
return std::tuple<std::add_const_t<Component> &...>{get<Component>()...};
|
||||
}
|
||||
}
|
||||
|
||||
/*! @copydoc get */
|
||||
template<typename... Component>
|
||||
inline decltype(auto) get() ENTT_NOEXCEPT {
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
return (const_cast<Component &>(std::as_const(*this).template get<Component>()), ...);
|
||||
} else {
|
||||
return std::tuple<Component &...>{get<Component>()...};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the given components.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to get components from a prototype that doesn't own them
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* prototype doesn't own instances of the given components.
|
||||
*
|
||||
* @tparam Component Type of components to get.
|
||||
* @return References to the components owned by the prototype.
|
||||
* @brief Returns pointers to the given components.
|
||||
* @tparam Component Types of components to get.
|
||||
* @return Pointers to the components owned by the prototype.
|
||||
*/
|
||||
template<typename... Component>
|
||||
inline std::enable_if_t<(sizeof...(Component) > 1), std::tuple<Component &...>>
|
||||
get() ENTT_NOEXCEPT {
|
||||
return std::tuple<Component &...>{get<Component>()...};
|
||||
auto try_get() const ENTT_NOEXCEPT {
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
const auto *wrapper = reg->template try_get<component_wrapper<Component...>>(entity);
|
||||
return wrapper ? &wrapper->component : nullptr;
|
||||
} else {
|
||||
return std::tuple<std::add_const_t<Component> *...>{try_get<Component>()...};
|
||||
}
|
||||
}
|
||||
|
||||
/*! @copydoc try_get */
|
||||
template<typename... Component>
|
||||
inline auto try_get() ENTT_NOEXCEPT {
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
return (const_cast<Component *>(std::as_const(*this).template try_get<Component>()), ...);
|
||||
} else {
|
||||
return std::tuple<Component *...>{try_get<Component>()...};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,9 +241,9 @@ public:
|
||||
* @return A valid entity identifier.
|
||||
*/
|
||||
entity_type create(registry_type &other) const {
|
||||
const auto entity = other.create();
|
||||
assign(other, entity);
|
||||
return entity;
|
||||
const auto entt = other.create();
|
||||
assign(other, entt);
|
||||
return entt;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -281,7 +264,7 @@ public:
|
||||
* @return A valid entity identifier.
|
||||
*/
|
||||
inline entity_type create() const {
|
||||
return create(*registry);
|
||||
return create(*reg);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -332,7 +315,7 @@ public:
|
||||
* @param dst A valid entity identifier.
|
||||
*/
|
||||
inline void assign(const entity_type dst) const {
|
||||
assign(*registry, dst);
|
||||
assign(*reg, dst);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -354,9 +337,9 @@ public:
|
||||
* @param other A valid reference to a registry.
|
||||
* @param dst A valid entity identifier.
|
||||
*/
|
||||
void accommodate(registry_type &other, const entity_type dst) const {
|
||||
void assign_or_replace(registry_type &other, const entity_type dst) const {
|
||||
for(auto &handler: handlers) {
|
||||
handler.second.accommodate(*this, other, dst);
|
||||
handler.second.assign_or_replace(*this, other, dst);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -378,8 +361,8 @@ public:
|
||||
*
|
||||
* @param dst A valid entity identifier.
|
||||
*/
|
||||
inline void accommodate(const entity_type dst) const {
|
||||
accommodate(*registry, dst);
|
||||
inline void assign_or_replace(const entity_type dst) const {
|
||||
assign_or_replace(*reg, dst);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -428,7 +411,7 @@ public:
|
||||
* @param dst A valid entity identifier.
|
||||
*/
|
||||
inline void operator()(const entity_type dst) const ENTT_NOEXCEPT {
|
||||
assign(*registry, dst);
|
||||
assign(*reg, dst);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -471,26 +454,29 @@ public:
|
||||
* @return A valid entity identifier.
|
||||
*/
|
||||
inline entity_type operator()() const ENTT_NOEXCEPT {
|
||||
return create(*registry);
|
||||
return create(*reg);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a reference to the underlying registry.
|
||||
* @return A reference to the underlying registry.
|
||||
*/
|
||||
inline const registry_type & backend() const ENTT_NOEXCEPT {
|
||||
return *reg;
|
||||
}
|
||||
|
||||
/*! @copydoc backend */
|
||||
inline registry_type & backend() ENTT_NOEXCEPT {
|
||||
return const_cast<registry_type &>(std::as_const(*this).backend());
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<component_type, Handler> handlers;
|
||||
Registry<Entity> *registry;
|
||||
std::unordered_map<component_type, component_handler> handlers;
|
||||
registry_type *reg;
|
||||
entity_type entity;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Default prototype
|
||||
*
|
||||
* The default prototype is the best choice for almost all the
|
||||
* applications.<br/>
|
||||
* Users should have a really good reason to choose something different.
|
||||
*/
|
||||
using DefaultPrototype = Prototype<DefaultRegistry::entity_type>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
275
src/entt/entity/runtime_view.hpp
Normal file
275
src/entt/entity/runtime_view.hpp
Normal file
@@ -0,0 +1,275 @@
|
||||
#ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
|
||||
#define ENTT_ENTITY_RUNTIME_VIEW_HPP
|
||||
|
||||
|
||||
#include <iterator>
|
||||
#include <cassert>
|
||||
#include <vector>
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
#include "../config/config.h"
|
||||
#include "sparse_set.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Runtime view.
|
||||
*
|
||||
* Runtime views iterate over those entities that have at least all the given
|
||||
* components in their bags. During initialization, a runtime view looks at the
|
||||
* number of entities available for each component and picks up a reference to
|
||||
* the smallest set of candidate entities in order to get a performance boost
|
||||
* when iterate.<br/>
|
||||
* Order of elements during iterations are highly dependent on the order of the
|
||||
* underlying data structures. See sparse_set and its specializations for more
|
||||
* details.
|
||||
*
|
||||
* @b Important
|
||||
*
|
||||
* Iterators aren't invalidated if:
|
||||
*
|
||||
* * New instances of the given components are created and assigned to entities.
|
||||
* * The entity currently pointed is modified (as an example, if one of the
|
||||
* given components is removed from the entity to which the iterator points).
|
||||
*
|
||||
* In all the other cases, modifying the pools of the given components in any
|
||||
* way invalidates all the iterators and using them results in undefined
|
||||
* behavior.
|
||||
*
|
||||
* @note
|
||||
* Views share references to the underlying data structures of the registry that
|
||||
* generated them. Therefore any change to the entities and to the components
|
||||
* made by means of the registry are immediately reflected by the views, unless
|
||||
* a pool was missing when the view was built (in this case, the view won't
|
||||
* have a valid reference and won't be updated accordingly).
|
||||
*
|
||||
* @warning
|
||||
* Lifetime of a view must overcome the one of the registry that generated it.
|
||||
* In any other case, attempting to use a view results in undefined behavior.
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class basic_runtime_view {
|
||||
/*! @brief A registry is allowed to create views. */
|
||||
friend class basic_registry<Entity>;
|
||||
|
||||
using underlying_iterator_type = typename sparse_set<Entity>::iterator_type;
|
||||
using extent_type = typename sparse_set<Entity>::size_type;
|
||||
using traits_type = entt_traits<Entity>;
|
||||
|
||||
class iterator {
|
||||
friend class basic_runtime_view<Entity>;
|
||||
|
||||
iterator(underlying_iterator_type first, underlying_iterator_type last, const sparse_set<Entity> * const *others, const sparse_set<Entity> * const *length, extent_type ext) ENTT_NOEXCEPT
|
||||
: begin{first},
|
||||
end{last},
|
||||
from{others},
|
||||
to{length},
|
||||
extent{ext}
|
||||
{
|
||||
if(begin != end && !valid()) {
|
||||
++(*this);
|
||||
}
|
||||
}
|
||||
|
||||
bool valid() const ENTT_NOEXCEPT {
|
||||
const auto entt = *begin;
|
||||
const auto sz = size_type(entt & traits_type::entity_mask);
|
||||
|
||||
return sz < extent && std::all_of(from, to, [entt](const auto *view) {
|
||||
return view->has(entt);
|
||||
});
|
||||
}
|
||||
|
||||
public:
|
||||
using difference_type = typename underlying_iterator_type::difference_type;
|
||||
using value_type = typename underlying_iterator_type::value_type;
|
||||
using pointer = typename underlying_iterator_type::pointer;
|
||||
using reference = typename underlying_iterator_type::reference;
|
||||
using iterator_category = std::forward_iterator_tag;
|
||||
|
||||
iterator() ENTT_NOEXCEPT = default;
|
||||
|
||||
iterator & operator++() ENTT_NOEXCEPT {
|
||||
return (++begin != end && !valid()) ? ++(*this) : *this;
|
||||
}
|
||||
|
||||
iterator operator++(int) ENTT_NOEXCEPT {
|
||||
iterator orig = *this;
|
||||
return ++(*this), orig;
|
||||
}
|
||||
|
||||
bool operator==(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return other.begin == begin;
|
||||
}
|
||||
|
||||
inline bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
pointer operator->() const ENTT_NOEXCEPT {
|
||||
return begin.operator->();
|
||||
}
|
||||
|
||||
inline reference operator*() const ENTT_NOEXCEPT {
|
||||
return *operator->();
|
||||
}
|
||||
|
||||
private:
|
||||
underlying_iterator_type begin;
|
||||
underlying_iterator_type end;
|
||||
const sparse_set<Entity> * const *from;
|
||||
const sparse_set<Entity> * const *to;
|
||||
extent_type extent;
|
||||
};
|
||||
|
||||
basic_runtime_view(std::vector<const sparse_set<Entity> *> others) ENTT_NOEXCEPT
|
||||
: pools{std::move(others)}
|
||||
{
|
||||
const auto it = std::min_element(pools.begin(), pools.end(), [](const auto *lhs, const auto *rhs) {
|
||||
return (!lhs && rhs) || (lhs && rhs && lhs->size() < rhs->size());
|
||||
});
|
||||
|
||||
// brings the best candidate (if any) on front of the vector
|
||||
std::rotate(pools.begin(), it, pools.end());
|
||||
}
|
||||
|
||||
extent_type min() const ENTT_NOEXCEPT {
|
||||
extent_type extent{};
|
||||
|
||||
if(valid()) {
|
||||
const auto it = std::min_element(pools.cbegin(), pools.cend(), [](const auto *lhs, const auto *rhs) {
|
||||
return lhs->extent() < rhs->extent();
|
||||
});
|
||||
|
||||
extent = (*it)->extent();
|
||||
}
|
||||
|
||||
return extent;
|
||||
}
|
||||
|
||||
inline bool valid() const ENTT_NOEXCEPT {
|
||||
return !pools.empty() && pools.front();
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename sparse_set<Entity>::entity_type;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename sparse_set<Entity>::size_type;
|
||||
/*! @brief Input iterator type. */
|
||||
using iterator_type = iterator;
|
||||
|
||||
/**
|
||||
* @brief Estimates the number of entities that have the given components.
|
||||
* @return Estimated number of entities that have the given components.
|
||||
*/
|
||||
size_type size() const ENTT_NOEXCEPT {
|
||||
return valid() ? pools.front()->size() : size_type{};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the view is definitely empty.
|
||||
* @return True if the view is definitely empty, false otherwise.
|
||||
*/
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return !valid() || pools.front()->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the first entity that has the given
|
||||
* components.
|
||||
*
|
||||
* The returned iterator points to the first entity that has the given
|
||||
* components. If the view is empty, the returned iterator will be equal to
|
||||
* `end()`.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed to the underlying data
|
||||
* structures.
|
||||
*
|
||||
* @return An iterator to the first entity that has the given components.
|
||||
*/
|
||||
iterator_type begin() const ENTT_NOEXCEPT {
|
||||
iterator_type it{};
|
||||
|
||||
if(valid()) {
|
||||
const auto &pool = *pools.front();
|
||||
const auto * const *data = pools.data();
|
||||
it = { pool.begin(), pool.end(), data + 1, data + pools.size(), min() };
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator that is past the last entity that has the
|
||||
* given components.
|
||||
*
|
||||
* The returned iterator points to the entity following the last entity that
|
||||
* has the given components. Attempting to dereference the returned iterator
|
||||
* results in undefined behavior.
|
||||
*
|
||||
* @note
|
||||
* Input iterators stay true to the order imposed to the underlying data
|
||||
* structures.
|
||||
*
|
||||
* @return An iterator to the entity following the last entity that has the
|
||||
* given components.
|
||||
*/
|
||||
iterator_type end() const ENTT_NOEXCEPT {
|
||||
iterator_type it{};
|
||||
|
||||
if(valid()) {
|
||||
const auto &pool = *pools.front();
|
||||
it = { pool.end(), pool.end(), nullptr, nullptr, min() };
|
||||
}
|
||||
|
||||
return it;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if a view contains an entity.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return True if the view contains the given entity, false otherwise.
|
||||
*/
|
||||
bool contains(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
return valid() && std::all_of(pools.cbegin(), pools.cend(), [entt](const auto *view) {
|
||||
return view->has(entt) && view->data()[view->get(entt)] == entt;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterates entities and applies the given function object to them.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided only with
|
||||
* the entity itself. To get the components, users can use the registry with
|
||||
* which the view was built.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(const entity_type);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func func) const {
|
||||
std::for_each(begin(), end(), func);
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<const sparse_set<Entity> *> pools;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_RUNTIME_VIEW_HPP
|
||||
@@ -5,46 +5,38 @@
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <unordered_map>
|
||||
#include "../config/config.h"
|
||||
#include "entt_traits.hpp"
|
||||
#include "utility.hpp"
|
||||
#include "entity.hpp"
|
||||
#include "fwd.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/>
|
||||
* selection of components 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 {
|
||||
class basic_snapshot {
|
||||
/*! @brief A registry is allowed to create snapshots. */
|
||||
friend class Registry<Entity>;
|
||||
friend class basic_registry<Entity>;
|
||||
|
||||
using follow_fn_type = Entity(const Registry<Entity> &, const Entity);
|
||||
using follow_fn_type = Entity(const basic_registry<Entity> &, const Entity);
|
||||
|
||||
Snapshot(const Registry<Entity> ®istry, Entity seed, follow_fn_type *follow) ENTT_NOEXCEPT
|
||||
: registry{registry},
|
||||
seed{seed},
|
||||
follow{follow}
|
||||
basic_snapshot(const basic_registry<Entity> *source, Entity init, follow_fn_type *fn) ENTT_NOEXCEPT
|
||||
: reg{source},
|
||||
seed{init},
|
||||
follow{fn}
|
||||
{}
|
||||
|
||||
template<typename Component, typename Archive, typename It>
|
||||
@@ -52,10 +44,14 @@ class Snapshot final {
|
||||
archive(static_cast<Entity>(sz));
|
||||
|
||||
while(first != last) {
|
||||
const auto entity = *(first++);
|
||||
const auto entt = *(first++);
|
||||
|
||||
if(registry.template has<Component>(entity)) {
|
||||
archive(entity, registry.template get<Component>(entity));
|
||||
if(reg->template has<Component>(entt)) {
|
||||
if constexpr(std::is_empty_v<Component>) {
|
||||
archive(entt);
|
||||
} else {
|
||||
archive(entt, reg->template get<Component>(entt));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -66,27 +62,19 @@ class Snapshot final {
|
||||
auto begin = first;
|
||||
|
||||
while(begin != last) {
|
||||
const auto entity = *(begin++);
|
||||
using accumulator_type = std::size_t[];
|
||||
accumulator_type accumulator = { (registry.template has<Component>(entity) ? ++size[Indexes] : size[Indexes])... };
|
||||
(void)accumulator;
|
||||
const auto entt = *(begin++);
|
||||
((reg->template has<Component>(entt) ? ++size[Indexes] : size[Indexes]), ...);
|
||||
}
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { (get<Component>(archive, size[Indexes], first, last), 0)... };
|
||||
(void)accumulator;
|
||||
(get<Component>(archive, size[Indexes], first, last), ...);
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Copying a snapshot isn't allowed. */
|
||||
Snapshot(const Snapshot &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Snapshot(Snapshot &&) = default;
|
||||
basic_snapshot(basic_snapshot &&) = default;
|
||||
|
||||
/*! @brief Copying a snapshot isn't allowed. @return This snapshot. */
|
||||
Snapshot & operator=(const Snapshot &) = delete;
|
||||
/*! @brief Default move assignment operator. @return This snapshot. */
|
||||
Snapshot & operator=(Snapshot &&) = default;
|
||||
basic_snapshot & operator=(basic_snapshot &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Puts aside all the entities that are still in use.
|
||||
@@ -99,9 +87,9 @@ public:
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename Archive>
|
||||
const Snapshot & entities(Archive &archive) const {
|
||||
archive(static_cast<Entity>(registry.alive()));
|
||||
registry.each([&archive](const auto entity) { archive(entity); });
|
||||
const basic_snapshot & entities(Archive &archive) const {
|
||||
archive(static_cast<Entity>(reg->alive()));
|
||||
reg->each([&archive](const auto entt) { archive(entt); });
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -116,8 +104,8 @@ public:
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename Archive>
|
||||
const Snapshot & destroyed(Archive &archive) const {
|
||||
auto size = registry.size() - registry.alive();
|
||||
const basic_snapshot & destroyed(Archive &archive) const {
|
||||
auto size = reg->size() - reg->alive();
|
||||
archive(static_cast<Entity>(size));
|
||||
|
||||
if(size) {
|
||||
@@ -125,7 +113,7 @@ public:
|
||||
archive(curr);
|
||||
|
||||
for(--size; size; --size) {
|
||||
curr = follow(registry, curr);
|
||||
curr = follow(*reg, curr);
|
||||
archive(curr);
|
||||
}
|
||||
}
|
||||
@@ -133,32 +121,6 @@ public:
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside the given component.
|
||||
*
|
||||
* Each instance is serialized together with the entity to which it belongs.
|
||||
* Entities are serialized along with their versions.
|
||||
*
|
||||
* @tparam Component Type of component 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>
|
||||
const Snapshot & component(Archive &archive) const {
|
||||
const auto sz = registry.template size<Component>();
|
||||
const auto *entities = registry.template data<Component>();
|
||||
|
||||
archive(static_cast<Entity>(sz));
|
||||
|
||||
for(std::remove_const_t<decltype(sz)> i{}; i < sz; ++i) {
|
||||
const auto entity = entities[i];
|
||||
archive(entity, registry.template get<Component>(entity));
|
||||
};
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside the given components.
|
||||
*
|
||||
@@ -171,11 +133,26 @@ public:
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename... Component, typename Archive>
|
||||
std::enable_if_t<(sizeof...(Component) > 1), const Snapshot &>
|
||||
component(Archive &archive) const {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (component<Component>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
const basic_snapshot & component(Archive &archive) const {
|
||||
if constexpr(sizeof...(Component) == 1) {
|
||||
const auto sz = reg->template size<Component...>();
|
||||
const auto *entities = reg->template data<Component...>();
|
||||
|
||||
archive(static_cast<Entity>(sz));
|
||||
|
||||
for(std::remove_const_t<decltype(sz)> i{}; i < sz; ++i) {
|
||||
const auto entt = entities[i];
|
||||
|
||||
if constexpr(std::is_empty_v<Component...>) {
|
||||
archive(entt);
|
||||
} else {
|
||||
archive(entt, reg->template get<Component...>(entt));
|
||||
}
|
||||
};
|
||||
} else {
|
||||
(component<Component>(archive), ...);
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -194,58 +171,13 @@ public:
|
||||
* @return An object of this type to continue creating the snapshot.
|
||||
*/
|
||||
template<typename... Component, typename Archive, typename It>
|
||||
const Snapshot & component(Archive &archive, It first, It last) const {
|
||||
const basic_snapshot & component(Archive &archive, It first, It last) const {
|
||||
component<Component...>(archive, first, last, std::make_index_sequence<sizeof...(Component)>{});
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside the given tag.
|
||||
*
|
||||
* Each instance is serialized together with the entity to which it belongs.
|
||||
* Entities are serialized along with their versions.
|
||||
*
|
||||
* @tparam Tag Type of tag 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>
|
||||
const Snapshot & tag(Archive &archive) const {
|
||||
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>(), registry.template get<Tag>());
|
||||
}
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Puts aside the given tags.
|
||||
*
|
||||
* Each instance 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>
|
||||
std::enable_if_t<(sizeof...(Tag) > 1), const Snapshot &>
|
||||
tag(Archive &archive) const {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (tag<Tag>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
const Registry<Entity> ®istry;
|
||||
const basic_registry<Entity> *reg;
|
||||
const Entity seed;
|
||||
follow_fn_type *follow;
|
||||
};
|
||||
@@ -262,18 +194,18 @@ private:
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class SnapshotLoader final {
|
||||
class basic_snapshot_loader {
|
||||
/*! @brief A registry is allowed to create snapshot loaders. */
|
||||
friend class Registry<Entity>;
|
||||
friend class basic_registry<Entity>;
|
||||
|
||||
using assure_fn_type = void(Registry<Entity> &, const Entity, const bool);
|
||||
using force_fn_type = void(basic_registry<Entity> &, const Entity, const bool);
|
||||
|
||||
SnapshotLoader(Registry<Entity> ®istry, assure_fn_type *assure_fn) ENTT_NOEXCEPT
|
||||
: registry{registry},
|
||||
assure_fn{assure_fn}
|
||||
basic_snapshot_loader(basic_registry<Entity> *source, force_fn_type *fn) ENTT_NOEXCEPT
|
||||
: reg{source},
|
||||
force{fn}
|
||||
{
|
||||
// restore a snapshot as a whole requires a clean registry
|
||||
assert(!registry.capacity());
|
||||
// to restore a snapshot as a whole requires a clean registry
|
||||
ENTT_ASSERT(reg->empty());
|
||||
}
|
||||
|
||||
template<typename Archive>
|
||||
@@ -282,9 +214,9 @@ class SnapshotLoader final {
|
||||
archive(length);
|
||||
|
||||
while(length--) {
|
||||
Entity entity{};
|
||||
archive(entity);
|
||||
assure_fn(registry, entity, destroyed);
|
||||
Entity entt{};
|
||||
archive(entt);
|
||||
force(*reg, entt, destroyed);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -294,25 +226,28 @@ class SnapshotLoader final {
|
||||
archive(length);
|
||||
|
||||
while(length--) {
|
||||
Entity entity{};
|
||||
Type instance{};
|
||||
archive(entity, instance);
|
||||
static constexpr auto destroyed = false;
|
||||
assure_fn(registry, entity, destroyed);
|
||||
registry.template assign<Type>(args..., entity, static_cast<const Type &>(instance));
|
||||
Entity entt{};
|
||||
|
||||
if constexpr(std::is_empty_v<Type>) {
|
||||
archive(entt);
|
||||
force(*reg, entt, destroyed);
|
||||
reg->template assign<Type>(args..., entt);
|
||||
} else {
|
||||
Type instance{};
|
||||
archive(entt, instance);
|
||||
force(*reg, entt, destroyed);
|
||||
reg->template assign<Type>(args..., entt, std::as_const(instance));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Copying a snapshot loader isn't allowed. */
|
||||
SnapshotLoader(const SnapshotLoader &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
SnapshotLoader(SnapshotLoader &&) = default;
|
||||
basic_snapshot_loader(basic_snapshot_loader &&) = default;
|
||||
|
||||
/*! @brief Copying a snapshot loader isn't allowed. @return This loader. */
|
||||
SnapshotLoader & operator=(const SnapshotLoader &) = delete;
|
||||
/*! @brief Default move assignment operator. @return This loader. */
|
||||
SnapshotLoader & operator=(SnapshotLoader &&) = default;
|
||||
basic_snapshot_loader & operator=(basic_snapshot_loader &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Restores entities that were in use during serialization.
|
||||
@@ -325,7 +260,7 @@ public:
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename Archive>
|
||||
const SnapshotLoader & entities(Archive &archive) const {
|
||||
const basic_snapshot_loader & entities(Archive &archive) const {
|
||||
static constexpr auto destroyed = false;
|
||||
assure(archive, destroyed);
|
||||
return *this;
|
||||
@@ -342,7 +277,7 @@ public:
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename Archive>
|
||||
const SnapshotLoader & destroyed(Archive &archive) const {
|
||||
const basic_snapshot_loader & destroyed(Archive &archive) const {
|
||||
static constexpr auto destroyed = true;
|
||||
assure(archive, destroyed);
|
||||
return *this;
|
||||
@@ -362,55 +297,32 @@ public:
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
template<typename... Component, typename Archive>
|
||||
const SnapshotLoader & component(Archive &archive) const {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (assign<Component>(archive), 0)... };
|
||||
(void)accumulator;
|
||||
const basic_snapshot_loader & component(Archive &archive) const {
|
||||
(assign<Component>(archive), ...);
|
||||
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>
|
||||
const SnapshotLoader & tag(Archive &archive) const {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (assign<Tag>(archive, tag_t{}), 0)... };
|
||||
(void)accumulator;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destroys those entities that have neither components nor tags.
|
||||
* @brief Destroys those entities that have no components.
|
||||
*
|
||||
* 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/>
|
||||
* was saved, it could happen that some of the entities have no components
|
||||
* once restored.<br/>
|
||||
* This functions helps to identify and destroy those entities.
|
||||
*
|
||||
* @return A valid loader to continue restoring data.
|
||||
*/
|
||||
const SnapshotLoader & orphans() const {
|
||||
registry.orphans([this](const auto entity) {
|
||||
registry.destroy(entity);
|
||||
const basic_snapshot_loader & orphans() const {
|
||||
reg->orphans([this](const auto entt) {
|
||||
reg->destroy(entt);
|
||||
});
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
Registry<Entity> ®istry;
|
||||
assure_fn_type *assure_fn;
|
||||
basic_registry<Entity> *reg;
|
||||
force_fn_type *force;
|
||||
};
|
||||
|
||||
|
||||
@@ -431,63 +343,55 @@ private:
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
*/
|
||||
template<typename Entity>
|
||||
class ContinuousLoader final {
|
||||
class basic_continuous_loader {
|
||||
using traits_type = entt_traits<Entity>;
|
||||
|
||||
void destroy(Entity entity) {
|
||||
const auto it = remloc.find(entity);
|
||||
void destroy(Entity entt) {
|
||||
const auto it = remloc.find(entt);
|
||||
|
||||
if(it == remloc.cend()) {
|
||||
const auto local = registry.create();
|
||||
remloc.emplace(entity, std::make_pair(local, true));
|
||||
registry.destroy(local);
|
||||
const auto local = reg->create();
|
||||
remloc.emplace(entt, std::make_pair(local, true));
|
||||
reg->destroy(local);
|
||||
}
|
||||
}
|
||||
|
||||
void restore(Entity entity) {
|
||||
const auto it = remloc.find(entity);
|
||||
void restore(Entity entt) {
|
||||
const auto it = remloc.find(entt);
|
||||
|
||||
if(it == remloc.cend()) {
|
||||
const auto local = registry.create();
|
||||
remloc.emplace(entity, std::make_pair(local, true));
|
||||
const auto local = reg->create();
|
||||
remloc.emplace(entt, std::make_pair(local, true));
|
||||
} else {
|
||||
remloc[entity].first =
|
||||
registry.valid(remloc[entity].first)
|
||||
? remloc[entity].first
|
||||
: registry.create();
|
||||
|
||||
remloc[entt].first = reg->valid(remloc[entt].first) ? remloc[entt].first : reg->create();
|
||||
// set the dirty flag
|
||||
remloc[entity].second = true;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Type, typename Member>
|
||||
std::enable_if_t<std::is_same<Member, Entity>::value>
|
||||
update(Type &instance, Member Type:: *member) {
|
||||
instance.*member = map(instance.*member);
|
||||
}
|
||||
|
||||
template<typename Type, typename Member>
|
||||
std::enable_if_t<std::is_same<typename std::iterator_traits<typename Member::iterator>::value_type, Entity>::value>
|
||||
update(Type &instance, Member Type:: *member) {
|
||||
for(auto &entity: instance.*member) {
|
||||
entity = map(entity);
|
||||
remloc[entt].second = true;
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Other, typename Type, typename Member>
|
||||
std::enable_if_t<!std::is_same<Other, Type>::value>
|
||||
update(Other &, Member Type:: *) {}
|
||||
void update(Other &instance, Member Type:: *member) {
|
||||
if constexpr(!std::is_same_v<Other, Type>) {
|
||||
return;
|
||||
} else if constexpr(std::is_same_v<Member, Entity>) {
|
||||
instance.*member = map(instance.*member);
|
||||
} else {
|
||||
// maybe a container? let's try...
|
||||
for(auto &entt: instance.*member) {
|
||||
entt = map(entt);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Archive>
|
||||
void assure(Archive &archive, void(ContinuousLoader:: *member)(Entity)) {
|
||||
void assure(Archive &archive, void(basic_continuous_loader:: *member)(Entity)) {
|
||||
Entity length{};
|
||||
archive(length);
|
||||
|
||||
while(length--) {
|
||||
Entity entity{};
|
||||
archive(entity);
|
||||
(this->*member)(entity);
|
||||
Entity entt{};
|
||||
archive(entt);
|
||||
(this->*member)(entt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -496,29 +400,31 @@ class ContinuousLoader final {
|
||||
for(auto &&ref: remloc) {
|
||||
const auto local = ref.second.first;
|
||||
|
||||
if(registry.valid(local)) {
|
||||
registry.template reset<Component>(local);
|
||||
if(reg->valid(local)) {
|
||||
reg->template reset<Component>(local);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
template<typename Other, typename Archive, typename Func, typename... Type, typename... Member>
|
||||
void assign(Archive &archive, Func func, Member Type:: *... member) {
|
||||
template<typename Other, typename Archive, typename... Type, typename... Member>
|
||||
void assign(Archive &archive, [[maybe_unused]] Member Type:: *... member) {
|
||||
Entity length{};
|
||||
archive(length);
|
||||
|
||||
while(length--) {
|
||||
Entity entity{};
|
||||
Other instance{};
|
||||
Entity entt{};
|
||||
|
||||
archive(entity, instance);
|
||||
restore(entity);
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (update(instance, member), 0)... };
|
||||
(void)accumulator;
|
||||
|
||||
func(map(entity), instance);
|
||||
if constexpr(std::is_empty_v<Other>) {
|
||||
archive(entt);
|
||||
restore(entt);
|
||||
reg->template assign_or_replace<Other>(map(entt));
|
||||
} else {
|
||||
Other instance{};
|
||||
archive(entt, instance);
|
||||
(update(instance, member), ...);
|
||||
restore(entt);
|
||||
reg->template assign_or_replace<Other>(map(entt), std::as_const(instance));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -528,21 +434,17 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Constructs a loader that is bound to a given registry.
|
||||
* @param registry A valid reference to a registry.
|
||||
* @param source A valid reference to a registry.
|
||||
*/
|
||||
ContinuousLoader(Registry<entity_type> ®istry) ENTT_NOEXCEPT
|
||||
: registry{registry}
|
||||
basic_continuous_loader(basic_registry<entity_type> &source) ENTT_NOEXCEPT
|
||||
: reg{&source}
|
||||
{}
|
||||
|
||||
/*! @brief Copying a snapshot loader isn't allowed. */
|
||||
ContinuousLoader(const ContinuousLoader &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
ContinuousLoader(ContinuousLoader &&) = default;
|
||||
basic_continuous_loader(basic_continuous_loader &&) = default;
|
||||
|
||||
/*! @brief Copying a snapshot loader isn't allowed. @return This loader. */
|
||||
ContinuousLoader & operator=(const ContinuousLoader &) = delete;
|
||||
/*! @brief Default move assignment operator. @return This loader. */
|
||||
ContinuousLoader & operator=(ContinuousLoader &&) = default;
|
||||
basic_continuous_loader & operator=(basic_continuous_loader &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Restores entities that were in use during serialization.
|
||||
@@ -555,8 +457,8 @@ public:
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename Archive>
|
||||
ContinuousLoader & entities(Archive &archive) {
|
||||
assure(archive, &ContinuousLoader::restore);
|
||||
basic_continuous_loader & entities(Archive &archive) {
|
||||
assure(archive, &basic_continuous_loader::restore);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -571,8 +473,8 @@ public:
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename Archive>
|
||||
ContinuousLoader & destroyed(Archive &archive) {
|
||||
assure(archive, &ContinuousLoader::destroy);
|
||||
basic_continuous_loader & destroyed(Archive &archive) {
|
||||
assure(archive, &basic_continuous_loader::destroy);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -596,45 +498,9 @@ public:
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
template<typename... Component, typename Archive, typename... Type, typename... Member>
|
||||
ContinuousLoader & component(Archive &archive, Member Type:: *... member) {
|
||||
auto apply = [this](const auto entity, const auto &component) {
|
||||
registry.template accommodate<std::decay_t<decltype(component)>>(entity, component);
|
||||
};
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (reset<Component>(), assign<Component>(archive, apply, member...), 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 components to update with local counterparts.
|
||||
* @tparam Member 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, typename... Member>
|
||||
ContinuousLoader & tag(Archive &archive, Member Type:: *... member) {
|
||||
auto apply = [this](const auto entity, const auto &tag) {
|
||||
registry.template assign<std::decay_t<decltype(tag)>>(tag_t{}, entity, tag);
|
||||
};
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { 0, (registry.template remove<Tag>(), assign<Tag>(archive, apply, member...), 0)... };
|
||||
(void)accumulator;
|
||||
basic_continuous_loader & component(Archive &archive, Member Type:: *... member) {
|
||||
(reset<Component>(), ...);
|
||||
(assign<Component>(archive, member...), ...);
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -646,7 +512,7 @@ public:
|
||||
*
|
||||
* @return A non-const reference to this loader.
|
||||
*/
|
||||
ContinuousLoader & shrink() {
|
||||
basic_continuous_loader & shrink() {
|
||||
auto it = remloc.begin();
|
||||
|
||||
while(it != remloc.cend()) {
|
||||
@@ -657,8 +523,8 @@ public:
|
||||
dirty = false;
|
||||
++it;
|
||||
} else {
|
||||
if(registry.valid(local)) {
|
||||
registry.destroy(local);
|
||||
if(reg->valid(local)) {
|
||||
reg->destroy(local);
|
||||
}
|
||||
|
||||
it = remloc.erase(it);
|
||||
@@ -669,18 +535,18 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Destroys those entities that have neither components nor tags.
|
||||
* @brief Destroys those entities that have no components.
|
||||
*
|
||||
* 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/>
|
||||
* was saved, it could happen that some of the entities have no components
|
||||
* 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](const auto entity) {
|
||||
registry.destroy(entity);
|
||||
basic_continuous_loader & orphans() {
|
||||
reg->orphans([this](const auto entt) {
|
||||
reg->destroy(entt);
|
||||
});
|
||||
|
||||
return *this;
|
||||
@@ -688,33 +554,32 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Tests if a loader knows about a given entity.
|
||||
* @param entity An entity identifier.
|
||||
* @param entt An entity identifier.
|
||||
* @return True if `entity` is managed by the loader, false otherwise.
|
||||
*/
|
||||
bool has(entity_type entity) const ENTT_NOEXCEPT {
|
||||
return (remloc.find(entity) != remloc.cend());
|
||||
bool has(entity_type entt) const ENTT_NOEXCEPT {
|
||||
return (remloc.find(entt) != 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.
|
||||
* @param entt An entity identifier.
|
||||
* @return The local identifier if any, the null entity otherwise.
|
||||
*/
|
||||
entity_type map(entity_type entity) const ENTT_NOEXCEPT {
|
||||
assert(has(entity));
|
||||
return remloc.find(entity)->second.first;
|
||||
entity_type map(entity_type entt) const ENTT_NOEXCEPT {
|
||||
const auto it = remloc.find(entt);
|
||||
entity_type other = null;
|
||||
|
||||
if(it != remloc.cend()) {
|
||||
other = it->second.first;
|
||||
}
|
||||
|
||||
return other;
|
||||
}
|
||||
|
||||
private:
|
||||
std::unordered_map<Entity, std::pair<Entity, bool>> remloc;
|
||||
Registry<Entity> ®istry;
|
||||
basic_registry<Entity> *reg;
|
||||
};
|
||||
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
698
src/entt/entity/storage.hpp
Normal file
698
src/entt/entity/storage.hpp
Normal file
@@ -0,0 +1,698 @@
|
||||
#ifndef ENTT_ENTITY_STORAGE_HPP
|
||||
#define ENTT_ENTITY_STORAGE_HPP
|
||||
|
||||
|
||||
#include <algorithm>
|
||||
#include <iterator>
|
||||
#include <numeric>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <cstddef>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "../core/algorithm.hpp"
|
||||
#include "sparse_set.hpp"
|
||||
#include "entity.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic storage implementation.
|
||||
*
|
||||
* This class is a refinement of a sparse set that associates an object to an
|
||||
* entity. The main purpose of this class is to extend sparse sets to store
|
||||
* components in a registry. It guarantees fast access both to the elements and
|
||||
* to the entities.
|
||||
*
|
||||
* @note
|
||||
* Entities and objects have the same order. It's guaranteed both in case of raw
|
||||
* access (either to entities or objects) and when using input iterators.
|
||||
*
|
||||
* @note
|
||||
* Internal data structures arrange elements to maximize performance. Because of
|
||||
* that, there are no guarantees that elements have the expected order when
|
||||
* iterate directly the internal packed array (see `raw` and `size` member
|
||||
* functions for that). Use `begin` and `end` instead.
|
||||
*
|
||||
* @warning
|
||||
* Empty types aren't explicitly instantiated. Temporary objects are returned in
|
||||
* place of the instances of the components and raw access isn't available for
|
||||
* them.
|
||||
*
|
||||
* @sa sparse_set<Entity>
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Type Type of objects assigned to the entities.
|
||||
*/
|
||||
template<typename Entity, typename Type, typename = std::void_t<>>
|
||||
class basic_storage: public sparse_set<Entity> {
|
||||
using underlying_type = sparse_set<Entity>;
|
||||
using traits_type = entt_traits<Entity>;
|
||||
|
||||
template<bool Const>
|
||||
class iterator {
|
||||
friend class basic_storage<Entity, Type>;
|
||||
|
||||
using instance_type = std::conditional_t<Const, const std::vector<Type>, std::vector<Type>>;
|
||||
using index_type = typename traits_type::difference_type;
|
||||
|
||||
iterator(instance_type *ref, const index_type idx) ENTT_NOEXCEPT
|
||||
: instances{ref}, index{idx}
|
||||
{}
|
||||
|
||||
public:
|
||||
using difference_type = index_type;
|
||||
using value_type = Type;
|
||||
using pointer = std::conditional_t<Const, const value_type *, value_type *>;
|
||||
using reference = std::conditional_t<Const, const value_type &, value_type &>;
|
||||
using iterator_category = std::random_access_iterator_tag;
|
||||
|
||||
iterator() ENTT_NOEXCEPT = default;
|
||||
|
||||
iterator & operator++() ENTT_NOEXCEPT {
|
||||
return --index, *this;
|
||||
}
|
||||
|
||||
iterator operator++(int) ENTT_NOEXCEPT {
|
||||
iterator orig = *this;
|
||||
return ++(*this), orig;
|
||||
}
|
||||
|
||||
iterator & operator--() ENTT_NOEXCEPT {
|
||||
return ++index, *this;
|
||||
}
|
||||
|
||||
iterator operator--(int) ENTT_NOEXCEPT {
|
||||
iterator orig = *this;
|
||||
return --(*this), orig;
|
||||
}
|
||||
|
||||
iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
|
||||
index -= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
|
||||
return iterator{instances, index-value};
|
||||
}
|
||||
|
||||
inline iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
|
||||
return (*this += -value);
|
||||
}
|
||||
|
||||
inline iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
|
||||
return (*this + -value);
|
||||
}
|
||||
|
||||
difference_type operator-(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return other.index - index;
|
||||
}
|
||||
|
||||
reference operator[](const difference_type value) const ENTT_NOEXCEPT {
|
||||
const auto pos = size_type(index-value-1);
|
||||
return (*instances)[pos];
|
||||
}
|
||||
|
||||
bool operator==(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return other.index == index;
|
||||
}
|
||||
|
||||
inline bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool operator<(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return index > other.index;
|
||||
}
|
||||
|
||||
bool operator>(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return index < other.index;
|
||||
}
|
||||
|
||||
inline bool operator<=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this > other);
|
||||
}
|
||||
|
||||
inline bool operator>=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this < other);
|
||||
}
|
||||
|
||||
pointer operator->() const ENTT_NOEXCEPT {
|
||||
const auto pos = size_type(index-1);
|
||||
return &(*instances)[pos];
|
||||
}
|
||||
|
||||
inline reference operator*() const ENTT_NOEXCEPT {
|
||||
return *operator->();
|
||||
}
|
||||
|
||||
private:
|
||||
instance_type *instances;
|
||||
index_type index;
|
||||
};
|
||||
|
||||
public:
|
||||
/*! @brief Type of the objects associated with the entities. */
|
||||
using object_type = Type;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename underlying_type::entity_type;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename underlying_type::size_type;
|
||||
/*! @brief Random access iterator type. */
|
||||
using iterator_type = iterator<false>;
|
||||
/*! @brief Constant random access iterator type. */
|
||||
using const_iterator_type = iterator<true>;
|
||||
|
||||
/**
|
||||
* @brief Increases the capacity of a storage.
|
||||
*
|
||||
* 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(const size_type cap) override {
|
||||
underlying_type::reserve(cap);
|
||||
instances.reserve(cap);
|
||||
}
|
||||
|
||||
/*! @brief Requests the removal of unused capacity. */
|
||||
void shrink_to_fit() override {
|
||||
underlying_type::shrink_to_fit();
|
||||
instances.shrink_to_fit();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Direct access to the array of objects.
|
||||
*
|
||||
* The returned pointer is such that range `[raw(), raw() + size()]` is
|
||||
* always a valid range, even if the container is empty.
|
||||
*
|
||||
* @note
|
||||
* 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 storage in the expected order.
|
||||
*
|
||||
* @return A pointer to the array of objects.
|
||||
*/
|
||||
const object_type * raw() const ENTT_NOEXCEPT {
|
||||
return instances.data();
|
||||
}
|
||||
|
||||
/*! @copydoc raw */
|
||||
object_type * raw() ENTT_NOEXCEPT {
|
||||
return const_cast<object_type *>(std::as_const(*this).raw());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the beginning.
|
||||
*
|
||||
* The returned iterator points to the first instance of the given type. If
|
||||
* the storage 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.
|
||||
*/
|
||||
const_iterator_type cbegin() const ENTT_NOEXCEPT {
|
||||
const typename traits_type::difference_type pos = underlying_type::size();
|
||||
return const_iterator_type{&instances, pos};
|
||||
}
|
||||
|
||||
/*! @copydoc cbegin */
|
||||
inline const_iterator_type begin() const ENTT_NOEXCEPT {
|
||||
return cbegin();
|
||||
}
|
||||
|
||||
/*! @copydoc begin */
|
||||
iterator_type begin() ENTT_NOEXCEPT {
|
||||
const typename traits_type::difference_type pos = underlying_type::size();
|
||||
return iterator_type{&instances, pos};
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
const_iterator_type cend() const ENTT_NOEXCEPT {
|
||||
return const_iterator_type{&instances, {}};
|
||||
}
|
||||
|
||||
/*! @copydoc cend */
|
||||
inline const_iterator_type end() const ENTT_NOEXCEPT {
|
||||
return cend();
|
||||
}
|
||||
|
||||
/*! @copydoc end */
|
||||
iterator_type end() ENTT_NOEXCEPT {
|
||||
return iterator_type{&instances, {}};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated with an entity.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the storage results in
|
||||
* undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* storage doesn't contain the given entity.
|
||||
*
|
||||
* @param entt A valid entity identifier.
|
||||
* @return The object associated with the entity.
|
||||
*/
|
||||
const object_type & get(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
return instances[underlying_type::get(entt)];
|
||||
}
|
||||
|
||||
/*! @copydoc get */
|
||||
inline object_type & get(const entity_type entt) ENTT_NOEXCEPT {
|
||||
return const_cast<object_type &>(std::as_const(*this).get(entt));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a pointer to the object associated with an entity, if any.
|
||||
* @param entt A valid entity identifier.
|
||||
* @return The object associated with the entity, if any.
|
||||
*/
|
||||
const object_type * try_get(const entity_type entt) const ENTT_NOEXCEPT {
|
||||
return underlying_type::has(entt) ? (instances.data() + underlying_type::get(entt)) : nullptr;
|
||||
}
|
||||
|
||||
/*! @copydoc try_get */
|
||||
inline object_type * try_get(const entity_type entt) ENTT_NOEXCEPT {
|
||||
return const_cast<object_type *>(std::as_const(*this).try_get(entt));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns an entity to a storage and constructs its object.
|
||||
*
|
||||
* This version accept both types that can be constructed in place directly
|
||||
* and types like aggregates 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 storage results
|
||||
* in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* storage already contains the given entity.
|
||||
*
|
||||
* @tparam Args Types of arguments to use to construct the object.
|
||||
* @param entt A valid entity identifier.
|
||||
* @param args Parameters to use to construct an object for the entity.
|
||||
* @return The object associated with the entity.
|
||||
*/
|
||||
template<typename... Args>
|
||||
object_type & construct(const entity_type entt, Args &&... args) {
|
||||
if constexpr(std::is_aggregate_v<object_type>) {
|
||||
instances.emplace_back(Type{std::forward<Args>(args)...});
|
||||
} else {
|
||||
instances.emplace_back(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// entity goes after component in case constructor throws
|
||||
underlying_type::construct(entt);
|
||||
return instances.back();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns one or more entities to a storage and constructs their
|
||||
* objects.
|
||||
*
|
||||
* The object type must be at least default constructible.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to assign an entity that already belongs to the storage
|
||||
* results in undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* storage already contains the given entity.
|
||||
*
|
||||
* @tparam It Type of forward iterator.
|
||||
* @param first An iterator to the first element of the range of entities.
|
||||
* @param last An iterator past the last element of the range of entities.
|
||||
* @return A pointer to the array of instances just created and sorted the
|
||||
* same of the entities.
|
||||
*/
|
||||
template<typename It>
|
||||
object_type * batch(It first, It last) {
|
||||
static_assert(std::is_default_constructible_v<object_type>);
|
||||
const auto skip = instances.size();
|
||||
instances.insert(instances.end(), last-first, {});
|
||||
// entity goes after component in case constructor throws
|
||||
underlying_type::batch(first, last);
|
||||
return instances.data() + skip;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes an entity from a storage and destroys its object.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the storage results in
|
||||
* undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* storage doesn't contain the given entity.
|
||||
*
|
||||
* @param entt A valid entity identifier.
|
||||
*/
|
||||
void destroy(const entity_type entt) override {
|
||||
std::swap(instances[underlying_type::get(entt)], instances.back());
|
||||
instances.pop_back();
|
||||
underlying_type::destroy(entt);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sort instances according to the given comparison function.
|
||||
*
|
||||
* Sort the elements so that iterating the storage 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 one of the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* bool(const Entity, const Entity);
|
||||
* bool(const Type &, const Type &);
|
||||
* @endcode
|
||||
*
|
||||
* Moreover, the comparison function object shall induce a
|
||||
* _strict weak ordering_ on the values.
|
||||
*
|
||||
* The sort function oject must offer a member function template
|
||||
* `operator()` that accepts three arguments:
|
||||
*
|
||||
* * An iterator to the first element of the range to sort.
|
||||
* * An iterator past the last element of the range to sort.
|
||||
* * A comparison function to use to compare the elements.
|
||||
*
|
||||
* The comparison function object received by the sort function object
|
||||
* hasn't necessarily the type of the one passed along with the other
|
||||
* parameters to this member function.
|
||||
*
|
||||
* @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
|
||||
* `sort` has been invoked.
|
||||
*
|
||||
* @tparam Compare Type of comparison function object.
|
||||
* @tparam Sort Type of sort function object.
|
||||
* @tparam Args Types of arguments to forward to the sort function object.
|
||||
* @param compare A valid comparison function object.
|
||||
* @param algo A valid sort function object.
|
||||
* @param args Arguments to forward to the sort function object, if any.
|
||||
*/
|
||||
template<typename Compare, typename Sort = std_sort, typename... Args>
|
||||
void sort(Compare compare, Sort algo = Sort{}, Args &&... args) {
|
||||
std::vector<size_type> copy(instances.size());
|
||||
std::iota(copy.begin(), copy.end(), 0);
|
||||
|
||||
if constexpr(std::is_invocable_v<Compare, const object_type &, const object_type &>) {
|
||||
static_assert(!std::is_empty_v<object_type>);
|
||||
|
||||
algo(copy.rbegin(), copy.rend(), [this, compare = std::move(compare)](const auto lhs, const auto rhs) {
|
||||
return compare(std::as_const(instances[lhs]), std::as_const(instances[rhs]));
|
||||
}, std::forward<Args>(args)...);
|
||||
} else {
|
||||
algo(copy.rbegin(), copy.rend(), [compare = std::move(compare), data = underlying_type::data()](const auto lhs, const auto rhs) {
|
||||
return compare(data[lhs], data[rhs]);
|
||||
}, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
for(size_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 Sort instances according to the order of the entities in another
|
||||
* sparse set.
|
||||
*
|
||||
* Entities that are part of both the storage 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.
|
||||
* Instances 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 storage 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 sparse_set<Entity> &other) ENTT_NOEXCEPT override {
|
||||
const auto to = other.end();
|
||||
auto from = other.begin();
|
||||
|
||||
size_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 storage. */
|
||||
void reset() override {
|
||||
underlying_type::reset();
|
||||
instances.clear();
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<object_type> instances;
|
||||
};
|
||||
|
||||
|
||||
/*! @copydoc basic_storage */
|
||||
template<typename Entity, typename Type>
|
||||
class basic_storage<Entity, Type, std::enable_if_t<std::is_empty_v<Type>>>: public sparse_set<Entity> {
|
||||
using underlying_type = sparse_set<Entity>;
|
||||
using traits_type = entt_traits<Entity>;
|
||||
|
||||
class iterator {
|
||||
friend class basic_storage<Entity, Type>;
|
||||
|
||||
using index_type = typename traits_type::difference_type;
|
||||
|
||||
iterator(const index_type idx) ENTT_NOEXCEPT
|
||||
: index{idx}
|
||||
{}
|
||||
|
||||
public:
|
||||
using difference_type = index_type;
|
||||
using value_type = Type;
|
||||
using pointer = const value_type *;
|
||||
using reference = value_type;
|
||||
using iterator_category = std::input_iterator_tag;
|
||||
|
||||
iterator() ENTT_NOEXCEPT = default;
|
||||
|
||||
iterator & operator++() ENTT_NOEXCEPT {
|
||||
return --index, *this;
|
||||
}
|
||||
|
||||
iterator operator++(int) ENTT_NOEXCEPT {
|
||||
iterator orig = *this;
|
||||
return ++(*this), orig;
|
||||
}
|
||||
|
||||
iterator & operator--() ENTT_NOEXCEPT {
|
||||
return ++index, *this;
|
||||
}
|
||||
|
||||
iterator operator--(int) ENTT_NOEXCEPT {
|
||||
iterator orig = *this;
|
||||
return --(*this), orig;
|
||||
}
|
||||
|
||||
iterator & operator+=(const difference_type value) ENTT_NOEXCEPT {
|
||||
index -= value;
|
||||
return *this;
|
||||
}
|
||||
|
||||
iterator operator+(const difference_type value) const ENTT_NOEXCEPT {
|
||||
return iterator{index-value};
|
||||
}
|
||||
|
||||
inline iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
|
||||
return (*this += -value);
|
||||
}
|
||||
|
||||
inline iterator operator-(const difference_type value) const ENTT_NOEXCEPT {
|
||||
return (*this + -value);
|
||||
}
|
||||
|
||||
difference_type operator-(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return other.index - index;
|
||||
}
|
||||
|
||||
reference operator[](const difference_type) const ENTT_NOEXCEPT {
|
||||
return {};
|
||||
}
|
||||
|
||||
bool operator==(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return other.index == index;
|
||||
}
|
||||
|
||||
inline bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this == other);
|
||||
}
|
||||
|
||||
bool operator<(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return index > other.index;
|
||||
}
|
||||
|
||||
bool operator>(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return index < other.index;
|
||||
}
|
||||
|
||||
inline bool operator<=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this > other);
|
||||
}
|
||||
|
||||
inline bool operator>=(const iterator &other) const ENTT_NOEXCEPT {
|
||||
return !(*this < other);
|
||||
}
|
||||
|
||||
pointer operator->() const ENTT_NOEXCEPT {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
inline reference operator*() const ENTT_NOEXCEPT {
|
||||
return {};
|
||||
}
|
||||
|
||||
private:
|
||||
index_type index;
|
||||
};
|
||||
|
||||
public:
|
||||
/*! @brief Type of the objects associated with the entities. */
|
||||
using object_type = Type;
|
||||
/*! @brief Underlying entity identifier. */
|
||||
using entity_type = typename underlying_type::entity_type;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename underlying_type::size_type;
|
||||
/*! @brief Random access iterator type. */
|
||||
using iterator_type = iterator;
|
||||
|
||||
/**
|
||||
* @brief Returns an iterator to the beginning.
|
||||
*
|
||||
* The returned iterator points to the first instance of the given type. If
|
||||
* the storage 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 cbegin() const ENTT_NOEXCEPT {
|
||||
const typename traits_type::difference_type pos = underlying_type::size();
|
||||
return iterator_type{pos};
|
||||
}
|
||||
|
||||
/*! @copydoc cbegin */
|
||||
inline iterator_type begin() const ENTT_NOEXCEPT {
|
||||
return cbegin();
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 cend() const ENTT_NOEXCEPT {
|
||||
return iterator_type{};
|
||||
}
|
||||
|
||||
/*! @copydoc cend */
|
||||
inline iterator_type end() const ENTT_NOEXCEPT {
|
||||
return cend();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the object associated with an entity.
|
||||
*
|
||||
* @note
|
||||
* Empty types aren't explicitly instantiated. Therefore, this function
|
||||
* always returns a temporary object.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to use an entity that doesn't belong to the storage results in
|
||||
* undefined behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* storage doesn't contain the given entity.
|
||||
*
|
||||
* @param entt A valid entity identifier.
|
||||
* @return The object associated with the entity.
|
||||
*/
|
||||
object_type get([[maybe_unused]] const entity_type entt) const ENTT_NOEXCEPT {
|
||||
ENTT_ASSERT(underlying_type::has(entt));
|
||||
return {};
|
||||
}
|
||||
};
|
||||
|
||||
/*! @copydoc basic_storage */
|
||||
template<typename Entity, typename Type>
|
||||
struct storage: basic_storage<Entity, Type> {};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_STORAGE_HPP
|
||||
@@ -1,23 +0,0 @@
|
||||
#ifndef ENTT_ENTITY_UTILITY_HPP
|
||||
#define ENTT_ENTITY_UTILITY_HPP
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/*! @brief Tag class type used to disambiguate overloads. */
|
||||
struct tag_t final {};
|
||||
|
||||
|
||||
/*! @brief Persistent view type used to disambiguate overloads. */
|
||||
struct persistent_t final {};
|
||||
|
||||
|
||||
/*! @brief Raw view type used to disambiguate overloads. */
|
||||
struct raw_t final {};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_ENTITY_UTILITY_HPP
|
||||
File diff suppressed because it is too large
Load Diff
@@ -3,18 +3,22 @@
|
||||
#include "core/hashed_string.hpp"
|
||||
#include "core/ident.hpp"
|
||||
#include "core/monostate.hpp"
|
||||
#include "core/type_traits.hpp"
|
||||
#include "core/utility.hpp"
|
||||
#include "entity/actor.hpp"
|
||||
#include "entity/attachee.hpp"
|
||||
#include "entity/entity.hpp"
|
||||
#include "entity/entt_traits.hpp"
|
||||
#include "entity/group.hpp"
|
||||
#include "entity/helper.hpp"
|
||||
#include "entity/prototype.hpp"
|
||||
#include "entity/registry.hpp"
|
||||
#include "entity/runtime_view.hpp"
|
||||
#include "entity/snapshot.hpp"
|
||||
#include "entity/sparse_set.hpp"
|
||||
#include "entity/utility.hpp"
|
||||
#include "entity/storage.hpp"
|
||||
#include "entity/view.hpp"
|
||||
#include "locator/locator.hpp"
|
||||
#include "meta/factory.hpp"
|
||||
#include "meta/meta.hpp"
|
||||
#include "process/process.hpp"
|
||||
#include "process/scheduler.hpp"
|
||||
#include "resource/cache.hpp"
|
||||
|
||||
3
src/entt/fwd.hpp
Normal file
3
src/entt/fwd.hpp
Normal file
@@ -0,0 +1,3 @@
|
||||
#include "entity/fwd.hpp"
|
||||
#include "resource/fwd.hpp"
|
||||
#include "signal/fwd.hpp"
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
@@ -23,14 +22,14 @@ namespace entt {
|
||||
* @tparam Service Type of service managed by the locator.
|
||||
*/
|
||||
template<typename Service>
|
||||
struct ServiceLocator final {
|
||||
struct service_locator {
|
||||
/*! @brief Type of service offered. */
|
||||
using service_type = Service;
|
||||
|
||||
/*! @brief Default constructor, deleted on purpose. */
|
||||
ServiceLocator() = delete;
|
||||
service_locator() = delete;
|
||||
/*! @brief Default destructor, deleted on purpose. */
|
||||
~ServiceLocator() = delete;
|
||||
~service_locator() = delete;
|
||||
|
||||
/**
|
||||
* @brief Tests if a valid service implementation is set.
|
||||
@@ -88,7 +87,7 @@ struct ServiceLocator final {
|
||||
* @param ptr Service to use to replace the current one.
|
||||
*/
|
||||
inline static void set(std::shared_ptr<Service> ptr) {
|
||||
assert(static_cast<bool>(ptr));
|
||||
ENTT_ASSERT(static_cast<bool>(ptr));
|
||||
service = std::move(ptr);
|
||||
}
|
||||
|
||||
@@ -102,14 +101,10 @@ struct ServiceLocator final {
|
||||
}
|
||||
|
||||
private:
|
||||
static std::shared_ptr<Service> service;
|
||||
inline static std::shared_ptr<Service> service = nullptr;
|
||||
};
|
||||
|
||||
|
||||
template<typename Service>
|
||||
std::shared_ptr<Service> ServiceLocator<Service>::service{};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
687
src/entt/meta/factory.hpp
Normal file
687
src/entt/meta/factory.hpp
Normal file
@@ -0,0 +1,687 @@
|
||||
#ifndef ENTT_META_FACTORY_HPP
|
||||
#define ENTT_META_FACTORY_HPP
|
||||
|
||||
|
||||
#include <utility>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "../core/hashed_string.hpp"
|
||||
#include "meta.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
template<typename>
|
||||
class meta_factory;
|
||||
|
||||
|
||||
template<typename Type, typename... Property>
|
||||
meta_factory<Type> reflect(const char *str, Property &&... property) ENTT_NOEXCEPT;
|
||||
|
||||
|
||||
template<typename Type>
|
||||
bool unregister() ENTT_NOEXCEPT;
|
||||
|
||||
|
||||
/**
|
||||
* @brief A meta factory to be used for reflection purposes.
|
||||
*
|
||||
* A meta factory is an utility class used to reflect types, data and functions
|
||||
* of all sorts. This class ensures that the underlying web of types is built
|
||||
* correctly and performs some checks in debug mode to ensure that there are no
|
||||
* subtle errors at runtime.
|
||||
*
|
||||
* @tparam Type Reflected type for which the factory was created.
|
||||
*/
|
||||
template<typename Type>
|
||||
class meta_factory {
|
||||
static_assert(std::is_object_v<Type> && !(std::is_const_v<Type> || std::is_volatile_v<Type>));
|
||||
|
||||
template<typename Node>
|
||||
inline bool duplicate(const hashed_string &name, const Node *node) ENTT_NOEXCEPT {
|
||||
return node ? node->name == name || duplicate(name, node->next) : false;
|
||||
}
|
||||
|
||||
inline bool duplicate(const meta_any &key, const internal::meta_prop_node *node) ENTT_NOEXCEPT {
|
||||
return node ? node->key() == key || duplicate(key, node->next) : false;
|
||||
}
|
||||
|
||||
template<typename>
|
||||
internal::meta_prop_node * properties() {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
template<typename Owner, typename Property, typename... Other>
|
||||
internal::meta_prop_node * properties(Property &&property, Other &&... other) {
|
||||
static std::remove_cv_t<std::remove_reference_t<Property>> prop{};
|
||||
|
||||
static internal::meta_prop_node node{
|
||||
nullptr,
|
||||
[]() -> meta_any {
|
||||
return std::get<0>(prop);
|
||||
},
|
||||
[]() -> meta_any {
|
||||
return std::get<1>(prop);
|
||||
},
|
||||
[]() -> meta_prop {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
prop = std::forward<Property>(property);
|
||||
node.next = properties<Owner>(std::forward<Other>(other)...);
|
||||
ENTT_ASSERT(!duplicate(meta_any{std::get<0>(prop)}, node.next));
|
||||
return &node;
|
||||
}
|
||||
|
||||
template<typename... Property>
|
||||
meta_factory type(hashed_string name, Property &&... property) ENTT_NOEXCEPT {
|
||||
static internal::meta_type_node node{
|
||||
{},
|
||||
nullptr,
|
||||
nullptr,
|
||||
std::is_void_v<Type>,
|
||||
std::is_integral_v<Type>,
|
||||
std::is_floating_point_v<Type>,
|
||||
std::is_array_v<Type>,
|
||||
std::is_enum_v<Type>,
|
||||
std::is_union_v<Type>,
|
||||
std::is_class_v<Type>,
|
||||
std::is_pointer_v<Type>,
|
||||
std::is_function_v<Type>,
|
||||
std::is_member_object_pointer_v<Type>,
|
||||
std::is_member_function_pointer_v<Type>,
|
||||
std::extent_v<Type>,
|
||||
[]() -> meta_type {
|
||||
return internal::meta_info<std::remove_pointer_t<Type>>::resolve();
|
||||
},
|
||||
&internal::destroy<Type>,
|
||||
[]() -> meta_type {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.name = name;
|
||||
node.next = internal::meta_info<>::type;
|
||||
node.prop = properties<Type>(std::forward<Property>(property)...);
|
||||
ENTT_ASSERT(!duplicate(name, node.next));
|
||||
ENTT_ASSERT(!internal::meta_info<Type>::type);
|
||||
internal::meta_info<Type>::type = &node;
|
||||
internal::meta_info<>::type = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
void unregister_prop(internal::meta_prop_node **prop) {
|
||||
while(*prop) {
|
||||
auto *node = *prop;
|
||||
*prop = node->next;
|
||||
node->next = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void unregister_dtor() {
|
||||
if(auto node = internal::meta_info<Type>::type->dtor; node) {
|
||||
internal::meta_info<Type>::type->dtor = nullptr;
|
||||
*node->underlying = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template<auto Member>
|
||||
auto unregister_all(int)
|
||||
-> decltype((internal::meta_info<Type>::type->*Member)->prop, void()) {
|
||||
while(internal::meta_info<Type>::type->*Member) {
|
||||
auto node = internal::meta_info<Type>::type->*Member;
|
||||
internal::meta_info<Type>::type->*Member = node->next;
|
||||
unregister_prop(&node->prop);
|
||||
node->next = nullptr;
|
||||
*node->underlying = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
template<auto Member>
|
||||
void unregister_all(char) {
|
||||
while(internal::meta_info<Type>::type->*Member) {
|
||||
auto node = internal::meta_info<Type>::type->*Member;
|
||||
internal::meta_info<Type>::type->*Member = node->next;
|
||||
node->next = nullptr;
|
||||
*node->underlying = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
bool unregister() ENTT_NOEXCEPT {
|
||||
const auto registered = internal::meta_info<Type>::type;
|
||||
|
||||
if(registered) {
|
||||
if(auto *curr = internal::meta_info<>::type; curr == internal::meta_info<Type>::type) {
|
||||
internal::meta_info<>::type = internal::meta_info<Type>::type->next;
|
||||
} else {
|
||||
while(curr && curr->next != internal::meta_info<Type>::type) {
|
||||
curr = curr->next;
|
||||
}
|
||||
|
||||
if(curr) {
|
||||
curr->next = internal::meta_info<Type>::type->next;
|
||||
}
|
||||
}
|
||||
|
||||
unregister_prop(&internal::meta_info<Type>::type->prop);
|
||||
unregister_all<&internal::meta_type_node::base>(0);
|
||||
unregister_all<&internal::meta_type_node::conv>(0);
|
||||
unregister_all<&internal::meta_type_node::ctor>(0);
|
||||
unregister_all<&internal::meta_type_node::data>(0);
|
||||
unregister_all<&internal::meta_type_node::func>(0);
|
||||
unregister_dtor();
|
||||
|
||||
internal::meta_info<Type>::type->name = {};
|
||||
internal::meta_info<Type>::type->next = nullptr;
|
||||
internal::meta_info<Type>::type = nullptr;
|
||||
}
|
||||
|
||||
return registered;
|
||||
}
|
||||
|
||||
meta_factory() ENTT_NOEXCEPT = default;
|
||||
|
||||
public:
|
||||
template<typename Other, typename... Property>
|
||||
friend meta_factory<Other> reflect(const char *str, Property &&... property) ENTT_NOEXCEPT;
|
||||
|
||||
template<typename Other>
|
||||
friend bool unregister() ENTT_NOEXCEPT;
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta base to a meta type.
|
||||
*
|
||||
* A reflected base class must be a real base class of the reflected type.
|
||||
*
|
||||
* @tparam Base Type of the base class to assign to the meta type.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<typename Base>
|
||||
meta_factory base() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_base_of_v<Base, Type>);
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_base_node node{
|
||||
&internal::meta_info<Type>::template base<Base>,
|
||||
type,
|
||||
nullptr,
|
||||
&internal::meta_info<Base>::resolve,
|
||||
[](void *instance) -> void * {
|
||||
return static_cast<Base *>(static_cast<Type *>(instance));
|
||||
},
|
||||
[]() -> meta_base {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.next = type->base;
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template base<Base>));
|
||||
internal::meta_info<Type>::template base<Base> = &node;
|
||||
type->base = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta conversion function to a meta type.
|
||||
*
|
||||
* The given type must be such that an instance of the reflected type can be
|
||||
* converted to it.
|
||||
*
|
||||
* @tparam To Type of the conversion function to assign to the meta type.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<typename To>
|
||||
meta_factory conv() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_convertible_v<Type, To>);
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_conv_node node{
|
||||
&internal::meta_info<Type>::template conv<To>,
|
||||
type,
|
||||
nullptr,
|
||||
&internal::meta_info<To>::resolve,
|
||||
[](void *instance) -> meta_any {
|
||||
return static_cast<To>(*static_cast<Type *>(instance));
|
||||
},
|
||||
[]() -> meta_conv {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.next = type->conv;
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template conv<To>));
|
||||
internal::meta_info<Type>::template conv<To> = &node;
|
||||
type->conv = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta constructor to a meta type.
|
||||
*
|
||||
* Free functions can be assigned to meta types in the role of
|
||||
* constructors. All that is required is that they return an instance of the
|
||||
* underlying type.<br/>
|
||||
* From a client's point of view, nothing changes if a constructor of a meta
|
||||
* type is a built-in one or a free function.
|
||||
*
|
||||
* @tparam Func The actual function to use as a constructor.
|
||||
* @tparam Property Types of properties to assign to the meta data.
|
||||
* @param property Properties to assign to the meta data.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<auto Func, typename... Property>
|
||||
meta_factory ctor(Property &&... property) ENTT_NOEXCEPT {
|
||||
using helper_type = internal::meta_function_helper<std::integral_constant<decltype(Func), Func>>;
|
||||
static_assert(std::is_same_v<typename helper_type::return_type, Type>);
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_ctor_node node{
|
||||
&internal::meta_info<Type>::template ctor<typename helper_type::args_type>,
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
helper_type::size,
|
||||
&helper_type::arg,
|
||||
[](meta_any * const any) {
|
||||
return internal::invoke<Type, Func>(nullptr, any, std::make_index_sequence<helper_type::size>{});
|
||||
},
|
||||
[]() -> meta_ctor {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.next = type->ctor;
|
||||
node.prop = properties<typename helper_type::args_type>(std::forward<Property>(property)...);
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template ctor<typename helper_type::args_type>));
|
||||
internal::meta_info<Type>::template ctor<typename helper_type::args_type> = &node;
|
||||
type->ctor = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta constructor to a meta type.
|
||||
*
|
||||
* A meta constructor is uniquely identified by the types of its arguments
|
||||
* and is such that there exists an actual constructor of the underlying
|
||||
* type that can be invoked with parameters whose types are those given.
|
||||
*
|
||||
* @tparam Args Types of arguments to use to construct an instance.
|
||||
* @tparam Property Types of properties to assign to the meta data.
|
||||
* @param property Properties to assign to the meta data.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<typename... Args, typename... Property>
|
||||
meta_factory ctor(Property &&... property) ENTT_NOEXCEPT {
|
||||
using helper_type = internal::meta_function_helper<Type(Args...)>;
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_ctor_node node{
|
||||
&internal::meta_info<Type>::template ctor<typename helper_type::args_type>,
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
helper_type::size,
|
||||
&helper_type::arg,
|
||||
[](meta_any * const any) {
|
||||
return internal::construct<Type, Args...>(any, std::make_index_sequence<helper_type::size>{});
|
||||
},
|
||||
[]() -> meta_ctor {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.next = type->ctor;
|
||||
node.prop = properties<typename helper_type::args_type>(std::forward<Property>(property)...);
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template ctor<typename helper_type::args_type>));
|
||||
internal::meta_info<Type>::template ctor<typename helper_type::args_type> = &node;
|
||||
type->ctor = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta destructor to a meta type.
|
||||
*
|
||||
* Free functions can be assigned to meta types in the role of destructors.
|
||||
* The signature of the function should identical to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(Type *);
|
||||
* @endcode
|
||||
*
|
||||
* From a client's point of view, nothing changes if the destructor of a
|
||||
* meta type is the default one or a custom one.
|
||||
*
|
||||
* @tparam Func The actual function to use as a destructor.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<auto *Func>
|
||||
meta_factory dtor() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_invocable_v<decltype(Func), Type *>);
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_dtor_node node{
|
||||
&internal::meta_info<Type>::template dtor<Func>,
|
||||
type,
|
||||
[](meta_handle handle) {
|
||||
return handle.type() == internal::meta_info<Type>::resolve()->meta()
|
||||
? ((*Func)(static_cast<Type *>(handle.data())), true)
|
||||
: false;
|
||||
},
|
||||
[]() -> meta_dtor {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
ENTT_ASSERT(!internal::meta_info<Type>::type->dtor);
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template dtor<Func>));
|
||||
internal::meta_info<Type>::template dtor<Func> = &node;
|
||||
internal::meta_info<Type>::type->dtor = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta data to a meta type.
|
||||
*
|
||||
* Both data members and static and global variables, as well as constants
|
||||
* of any kind, can be assigned to a meta type.<br/>
|
||||
* From a client's point of view, all the variables associated with the
|
||||
* reflected object will appear as if they were part of the type itself.
|
||||
*
|
||||
* @tparam Data The actual variable to attach to the meta type.
|
||||
* @tparam Property Types of properties to assign to the meta data.
|
||||
* @param str The name to assign to the meta data.
|
||||
* @param property Properties to assign to the meta data.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<auto Data, typename... Property>
|
||||
meta_factory data(const char *str, Property &&... property) ENTT_NOEXCEPT {
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
internal::meta_data_node *curr = nullptr;
|
||||
|
||||
if constexpr(std::is_same_v<Type, decltype(Data)>) {
|
||||
static internal::meta_data_node node{
|
||||
&internal::meta_info<Type>::template data<Data>,
|
||||
{},
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
true,
|
||||
true,
|
||||
&internal::meta_info<Type>::resolve,
|
||||
[](meta_handle, meta_any, meta_any) { return false; },
|
||||
[](meta_handle, meta_any) -> meta_any { return Data; },
|
||||
[]() -> meta_data {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.prop = properties<std::integral_constant<Type, Data>>(std::forward<Property>(property)...);
|
||||
curr = &node;
|
||||
} else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
|
||||
using data_type = std::remove_reference_t<decltype(std::declval<Type>().*Data)>;
|
||||
|
||||
static internal::meta_data_node node{
|
||||
&internal::meta_info<Type>::template data<Data>,
|
||||
{},
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
std::is_const_v<data_type>,
|
||||
!std::is_member_object_pointer_v<decltype(Data)>,
|
||||
&internal::meta_info<data_type>::resolve,
|
||||
&internal::setter<std::is_const_v<data_type>, Type, Data>,
|
||||
&internal::getter<Type, Data>,
|
||||
[]() -> meta_data {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.prop = properties<std::integral_constant<decltype(Data), Data>>(std::forward<Property>(property)...);
|
||||
curr = &node;
|
||||
} else {
|
||||
static_assert(std::is_pointer_v<decltype(Data)>);
|
||||
using data_type = std::remove_pointer_t<decltype(Data)>;
|
||||
|
||||
static internal::meta_data_node node{
|
||||
&internal::meta_info<Type>::template data<Data>,
|
||||
{},
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
std::is_const_v<data_type>,
|
||||
!std::is_member_object_pointer_v<decltype(Data)>,
|
||||
&internal::meta_info<data_type>::resolve,
|
||||
&internal::setter<std::is_const_v<data_type>, Type, Data>,
|
||||
&internal::getter<Type, Data>,
|
||||
[]() -> meta_data {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.prop = properties<std::integral_constant<decltype(Data), Data>>(std::forward<Property>(property)...);
|
||||
curr = &node;
|
||||
}
|
||||
|
||||
curr->name = hashed_string{str};
|
||||
curr->next = type->data;
|
||||
ENTT_ASSERT(!duplicate(hashed_string{str}, curr->next));
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template data<Data>));
|
||||
internal::meta_info<Type>::template data<Data> = curr;
|
||||
type->data = curr;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta data to a meta type by means of its setter and
|
||||
* getter.
|
||||
*
|
||||
* Setters and getters can be either free functions, member functions or a
|
||||
* mix of them.<br/>
|
||||
* In case of free functions, setters and getters must accept a pointer to
|
||||
* an instance of the parent type as their first argument. A setter has then
|
||||
* an extra argument of a type convertible to that of the parameter to
|
||||
* set.<br/>
|
||||
* In case of member functions, getters have no arguments at all, while
|
||||
* setters has an argument of a type convertible to that of the parameter to
|
||||
* set.
|
||||
*
|
||||
* @tparam Setter The actual function to use as a setter.
|
||||
* @tparam Getter The actual function to use as a getter.
|
||||
* @tparam Property Types of properties to assign to the meta data.
|
||||
* @param str The name to assign to the meta data.
|
||||
* @param property Properties to assign to the meta data.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<auto Setter, auto Getter, typename... Property>
|
||||
meta_factory data(const char *str, Property &&... property) ENTT_NOEXCEPT {
|
||||
using owner_type = std::tuple<std::integral_constant<decltype(Setter), Setter>, std::integral_constant<decltype(Getter), Getter>>;
|
||||
using underlying_type = std::invoke_result_t<decltype(Getter), Type *>;
|
||||
static_assert(std::is_invocable_v<decltype(Setter), Type *, underlying_type>);
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_data_node node{
|
||||
&internal::meta_info<Type>::template data<Setter, Getter>,
|
||||
{},
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
false,
|
||||
false,
|
||||
&internal::meta_info<underlying_type>::resolve,
|
||||
&internal::setter<false, Type, Setter>,
|
||||
&internal::getter<Type, Getter>,
|
||||
[]() -> meta_data {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.name = hashed_string{str};
|
||||
node.next = type->data;
|
||||
node.prop = properties<owner_type>(std::forward<Property>(property)...);
|
||||
ENTT_ASSERT(!duplicate(hashed_string{str}, node.next));
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template data<Setter, Getter>));
|
||||
internal::meta_info<Type>::template data<Setter, Getter> = &node;
|
||||
type->data = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assigns a meta funcion to a meta type.
|
||||
*
|
||||
* Both member functions and free functions can be assigned to a meta
|
||||
* type.<br/>
|
||||
* From a client's point of view, all the functions associated with the
|
||||
* reflected object will appear as if they were part of the type itself.
|
||||
*
|
||||
* @tparam Func The actual function to attach to the meta type.
|
||||
* @tparam Property Types of properties to assign to the meta function.
|
||||
* @param str The name to assign to the meta function.
|
||||
* @param property Properties to assign to the meta function.
|
||||
* @return A meta factory for the parent type.
|
||||
*/
|
||||
template<auto Func, typename... Property>
|
||||
meta_factory func(const char *str, Property &&... property) ENTT_NOEXCEPT {
|
||||
using owner_type = std::integral_constant<decltype(Func), Func>;
|
||||
using func_type = internal::meta_function_helper<std::integral_constant<decltype(Func), Func>>;
|
||||
auto * const type = internal::meta_info<Type>::resolve();
|
||||
|
||||
static internal::meta_func_node node{
|
||||
&internal::meta_info<Type>::template func<Func>,
|
||||
{},
|
||||
type,
|
||||
nullptr,
|
||||
nullptr,
|
||||
func_type::size,
|
||||
func_type::is_const,
|
||||
func_type::is_static,
|
||||
&internal::meta_info<typename func_type::return_type>::resolve,
|
||||
&func_type::arg,
|
||||
[](meta_handle handle, meta_any *any) {
|
||||
return internal::invoke<Type, Func>(handle, any, std::make_index_sequence<func_type::size>{});
|
||||
},
|
||||
[]() -> meta_func {
|
||||
return &node;
|
||||
}
|
||||
};
|
||||
|
||||
node.name = hashed_string{str};
|
||||
node.next = type->func;
|
||||
node.prop = properties<owner_type>(std::forward<Property>(property)...);
|
||||
ENTT_ASSERT(!duplicate(hashed_string{str}, node.next));
|
||||
ENTT_ASSERT((!internal::meta_info<Type>::template func<Func>));
|
||||
internal::meta_info<Type>::template func<Func> = &node;
|
||||
type->func = &node;
|
||||
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic function to use for reflection.
|
||||
*
|
||||
* This is the point from which everything starts.<br/>
|
||||
* By invoking this function with a type that is not yet reflected, a meta type
|
||||
* is created to which it will be possible to attach data and functions through
|
||||
* a dedicated factory.
|
||||
*
|
||||
* @tparam Type Type to reflect.
|
||||
* @tparam Property Types of properties to assign to the reflected type.
|
||||
* @param str The name to assign to the reflected type.
|
||||
* @param property Properties to assign to the reflected type.
|
||||
* @return A meta factory for the given type.
|
||||
*/
|
||||
template<typename Type, typename... Property>
|
||||
inline meta_factory<Type> reflect(const char *str, Property &&... property) ENTT_NOEXCEPT {
|
||||
return meta_factory<Type>{}.type(hashed_string{str}, std::forward<Property>(property)...);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic function to use for reflection.
|
||||
*
|
||||
* This is the point from which everything starts.<br/>
|
||||
* By invoking this function with a type that is not yet reflected, a meta type
|
||||
* is created to which it will be possible to attach data and functions through
|
||||
* a dedicated factory.
|
||||
*
|
||||
* @tparam Type Type to reflect.
|
||||
* @return A meta factory for the given type.
|
||||
*/
|
||||
template<typename Type>
|
||||
inline meta_factory<Type> reflect() ENTT_NOEXCEPT {
|
||||
return meta_factory<Type>{};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic function to unregister a type.
|
||||
*
|
||||
* This function unregisters a type and all its data members, member functions
|
||||
* and properties, as well as its constructors, destructors and conversion
|
||||
* functions if any.<br/>
|
||||
* Base classes aren't unregistered but the link between the two types is
|
||||
* removed.
|
||||
*
|
||||
* @tparam Type Type to unregister.
|
||||
* @return True if the type to unregister exists, false otherwise.
|
||||
*/
|
||||
template<typename Type>
|
||||
inline bool unregister() ENTT_NOEXCEPT {
|
||||
return meta_factory<Type>().unregister();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the meta type associated with a given type.
|
||||
* @tparam Type Type to use to search for a meta type.
|
||||
* @return The meta type associated with the given type, if any.
|
||||
*/
|
||||
template<typename Type>
|
||||
inline meta_type resolve() ENTT_NOEXCEPT {
|
||||
return internal::meta_info<Type>::resolve()->meta();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Returns the meta type associated with a given name.
|
||||
* @param str The name to use to search for a meta type.
|
||||
* @return The meta type associated with the given name, if any.
|
||||
*/
|
||||
inline meta_type resolve(const char *str) ENTT_NOEXCEPT {
|
||||
const auto *curr = internal::find_if([name = hashed_string{str}](auto *node) {
|
||||
return node->name == name;
|
||||
}, internal::meta_info<>::type);
|
||||
|
||||
return curr ? curr->meta() : meta_type{};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Iterates all the reflected types.
|
||||
* @tparam Op Type of the function object to invoke.
|
||||
* @param op A valid function object.
|
||||
*/
|
||||
template<typename Op>
|
||||
void resolve(Op op) ENTT_NOEXCEPT {
|
||||
internal::iterate([op = std::move(op)](auto *node) {
|
||||
op(node->meta());
|
||||
}, internal::meta_info<>::type);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_META_FACTORY_HPP
|
||||
2275
src/entt/meta/meta.hpp
Normal file
2275
src/entt/meta/meta.hpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,9 +2,8 @@
|
||||
#define ENTT_PROCESS_PROCESS_HPP
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
#include <utility>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
@@ -31,12 +30,13 @@ namespace entt {
|
||||
* update.
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void init(void *);
|
||||
* void init();
|
||||
* @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.
|
||||
* It's invoked when the process joins the running queue of a scheduler. This
|
||||
* happens as soon as it's attached to the scheduler if the process is a top
|
||||
* level one, otherwise when it replaces its parent if the process is a
|
||||
* continuation.
|
||||
*
|
||||
* * @code{.cpp}
|
||||
* void succeeded();
|
||||
@@ -64,14 +64,14 @@ namespace entt {
|
||||
* `succeed` and `fail` protected member functions and even pause or unpause the
|
||||
* process itself.
|
||||
*
|
||||
* @sa Scheduler
|
||||
* @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 {
|
||||
class process {
|
||||
enum class state: unsigned int {
|
||||
UNINITIALIZED = 0,
|
||||
RUNNING,
|
||||
PAUSED,
|
||||
@@ -81,41 +81,41 @@ class Process {
|
||||
FINISHED
|
||||
};
|
||||
|
||||
template<State state>
|
||||
using tag = std::integral_constant<State, state>;
|
||||
template<state value>
|
||||
using state_value_t = std::integral_constant<state, value>;
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::UNINITIALIZED>, void *data)
|
||||
-> decltype(std::declval<Target>().init(data)) {
|
||||
static_cast<Target *>(this)->init(data);
|
||||
auto tick(int, state_value_t<state::UNINITIALIZED>)
|
||||
-> decltype(std::declval<Target>().init()) {
|
||||
static_cast<Target *>(this)->init();
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::RUNNING>, Delta delta, void *data)
|
||||
auto tick(int, state_value_t<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>)
|
||||
auto tick(int, state_value_t<state::SUCCEEDED>)
|
||||
-> decltype(std::declval<Target>().succeeded()) {
|
||||
static_cast<Target *>(this)->succeeded();
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::FAILED>)
|
||||
auto tick(int, state_value_t<state::FAILED>)
|
||||
-> decltype(std::declval<Target>().failed()) {
|
||||
static_cast<Target *>(this)->failed();
|
||||
}
|
||||
|
||||
template<typename Target = Derived>
|
||||
auto tick(int, tag<State::ABORTED>)
|
||||
auto tick(int, state_value_t<state::ABORTED>)
|
||||
-> decltype(std::declval<Target>().aborted()) {
|
||||
static_cast<Target *>(this)->aborted();
|
||||
}
|
||||
|
||||
template<State S, typename... Args>
|
||||
void tick(char, tag<S>, Args &&...) const ENTT_NOEXCEPT {}
|
||||
template<state value, typename... Args>
|
||||
void tick(char, state_value_t<value>, Args &&...) const ENTT_NOEXCEPT {}
|
||||
|
||||
protected:
|
||||
/**
|
||||
@@ -126,7 +126,7 @@ protected:
|
||||
*/
|
||||
void succeed() ENTT_NOEXCEPT {
|
||||
if(alive()) {
|
||||
current = State::SUCCEEDED;
|
||||
current = state::SUCCEEDED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +138,7 @@ protected:
|
||||
*/
|
||||
void fail() ENTT_NOEXCEPT {
|
||||
if(alive()) {
|
||||
current = State::FAILED;
|
||||
current = state::FAILED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -149,8 +149,8 @@ protected:
|
||||
* running.
|
||||
*/
|
||||
void pause() ENTT_NOEXCEPT {
|
||||
if(current == State::RUNNING) {
|
||||
current = State::PAUSED;
|
||||
if(current == state::RUNNING) {
|
||||
current = state::PAUSED;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -161,8 +161,8 @@ protected:
|
||||
* paused.
|
||||
*/
|
||||
void unpause() ENTT_NOEXCEPT {
|
||||
if(current == State::PAUSED) {
|
||||
current = State::RUNNING;
|
||||
if(current == state::PAUSED) {
|
||||
current = state::RUNNING;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -171,8 +171,8 @@ public:
|
||||
using delta_type = Delta;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Process() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_base_of<Process, Derived>::value, "!");
|
||||
virtual ~process() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_base_of_v<process, Derived>);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -185,7 +185,7 @@ public:
|
||||
*/
|
||||
void abort(const bool immediately = false) ENTT_NOEXCEPT {
|
||||
if(alive()) {
|
||||
current = State::ABORTED;
|
||||
current = state::ABORTED;
|
||||
|
||||
if(immediately) {
|
||||
tick(0);
|
||||
@@ -198,7 +198,7 @@ public:
|
||||
* @return True if the process is still alive, false otherwise.
|
||||
*/
|
||||
bool alive() const ENTT_NOEXCEPT {
|
||||
return current == State::RUNNING || current == State::PAUSED;
|
||||
return current == state::RUNNING || current == state::PAUSED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -206,7 +206,7 @@ public:
|
||||
* @return True if the process is terminated, false otherwise.
|
||||
*/
|
||||
bool dead() const ENTT_NOEXCEPT {
|
||||
return current == State::FINISHED;
|
||||
return current == state::FINISHED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -214,7 +214,7 @@ public:
|
||||
* @return True if the process is paused, false otherwise.
|
||||
*/
|
||||
bool paused() const ENTT_NOEXCEPT {
|
||||
return current == State::PAUSED;
|
||||
return current == state::PAUSED;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -232,12 +232,13 @@ public:
|
||||
*/
|
||||
void tick(const 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);
|
||||
case state::UNINITIALIZED:
|
||||
tick(0, state_value_t<state::UNINITIALIZED>{});
|
||||
current = state::RUNNING;
|
||||
break;
|
||||
case state::RUNNING:
|
||||
tick(0, state_value_t<state::RUNNING>{}, delta, data);
|
||||
break;
|
||||
default:
|
||||
// suppress warnings
|
||||
break;
|
||||
@@ -245,18 +246,18 @@ public:
|
||||
|
||||
// if it's dead, it must be notified and removed immediately
|
||||
switch(current) {
|
||||
case State::SUCCEEDED:
|
||||
tick(0, tag<State::SUCCEEDED>{});
|
||||
current = State::FINISHED;
|
||||
case state::SUCCEEDED:
|
||||
tick(0, state_value_t<state::SUCCEEDED>{});
|
||||
current = state::FINISHED;
|
||||
break;
|
||||
case State::FAILED:
|
||||
tick(0, tag<State::FAILED>{});
|
||||
current = State::FINISHED;
|
||||
case state::FAILED:
|
||||
tick(0, state_value_t<state::FAILED>{});
|
||||
current = state::FINISHED;
|
||||
stopped = true;
|
||||
break;
|
||||
case State::ABORTED:
|
||||
tick(0, tag<State::ABORTED>{});
|
||||
current = State::FINISHED;
|
||||
case state::ABORTED:
|
||||
tick(0, state_value_t<state::ABORTED>{});
|
||||
current = state::FINISHED;
|
||||
stopped = true;
|
||||
break;
|
||||
default:
|
||||
@@ -266,7 +267,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
State current{State::UNINITIALIZED};
|
||||
state current{state::UNINITIALIZED};
|
||||
bool stopped{false};
|
||||
};
|
||||
|
||||
@@ -304,21 +305,21 @@ private:
|
||||
* create them internally each and avery time a lambda or a functor is used as
|
||||
* a process.
|
||||
*
|
||||
* @sa Process
|
||||
* @sa Scheduler
|
||||
* @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 {
|
||||
struct process_adaptor: process<process_adaptor<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)
|
||||
process_adaptor(Args &&... args)
|
||||
: Func{std::forward<Args>(args)...}
|
||||
{}
|
||||
|
||||
|
||||
@@ -30,23 +30,23 @@ namespace entt {
|
||||
* @code{.cpp}
|
||||
* scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
|
||||
* // code
|
||||
* }).then<MyProcess>(arguments...);
|
||||
* }).then<my_process>(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
|
||||
* @sa process
|
||||
*
|
||||
* @tparam Delta Type to use to provide elapsed time.
|
||||
*/
|
||||
template<typename Delta>
|
||||
class Scheduler final {
|
||||
struct ProcessHandler final {
|
||||
class scheduler {
|
||||
struct process_handler {
|
||||
using instance_type = std::unique_ptr<void, void(*)(void *)>;
|
||||
using update_fn_type = bool(ProcessHandler &, Delta, void *);
|
||||
using abort_fn_type = void(ProcessHandler &, bool);
|
||||
using next_type = std::unique_ptr<ProcessHandler>;
|
||||
using update_fn_type = bool(process_handler &, Delta, void *);
|
||||
using abort_fn_type = void(process_handler &, bool);
|
||||
using next_type = std::unique_ptr<process_handler>;
|
||||
|
||||
instance_type instance;
|
||||
update_fn_type *update;
|
||||
@@ -54,30 +54,33 @@ class Scheduler final {
|
||||
next_type next;
|
||||
};
|
||||
|
||||
struct Then final {
|
||||
Then(ProcessHandler *handler)
|
||||
: handler{handler}
|
||||
{}
|
||||
struct continuation {
|
||||
continuation(process_handler *ref)
|
||||
: handler{ref}
|
||||
{
|
||||
ENTT_ASSERT(handler);
|
||||
}
|
||||
|
||||
template<typename Proc, typename... Args>
|
||||
decltype(auto) then(Args &&... args) && {
|
||||
static_assert(std::is_base_of<Process<Proc, Delta>, Proc>::value, "!");
|
||||
handler = Scheduler::then<Proc>(handler, std::forward<Args>(args)...);
|
||||
return std::move(*this);
|
||||
continuation then(Args &&... args) {
|
||||
static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>);
|
||||
auto proc = typename process_handler::instance_type{new Proc{std::forward<Args>(args)...}, &scheduler::deleter<Proc>};
|
||||
handler->next.reset(new process_handler{std::move(proc), &scheduler::update<Proc>, &scheduler::abort<Proc>, nullptr});
|
||||
handler = handler->next.get();
|
||||
return *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));
|
||||
continuation then(Func &&func) {
|
||||
return then<process_adaptor<std::decay_t<Func>, Delta>>(std::forward<Func>(func));
|
||||
}
|
||||
|
||||
private:
|
||||
ProcessHandler *handler;
|
||||
process_handler *handler;
|
||||
};
|
||||
|
||||
template<typename Proc>
|
||||
static bool update(ProcessHandler &handler, const Delta delta, void *data) {
|
||||
static bool update(process_handler &handler, const Delta delta, void *data) {
|
||||
auto *process = static_cast<Proc *>(handler.instance.get());
|
||||
process->tick(delta, data);
|
||||
|
||||
@@ -86,7 +89,8 @@ class Scheduler final {
|
||||
if(dead) {
|
||||
if(handler.next && !process->rejected()) {
|
||||
handler = std::move(*handler.next);
|
||||
dead = handler.update(handler, delta, data);
|
||||
// forces the process to exit the uninitialized state
|
||||
dead = handler.update(handler, {}, nullptr);
|
||||
} else {
|
||||
handler.instance.reset();
|
||||
}
|
||||
@@ -96,7 +100,7 @@ class Scheduler final {
|
||||
}
|
||||
|
||||
template<typename Proc>
|
||||
static void abort(ProcessHandler &handler, const bool immediately) {
|
||||
static void abort(process_handler &handler, const bool immediately) {
|
||||
static_cast<Proc *>(handler.instance.get())->abort(immediately);
|
||||
}
|
||||
|
||||
@@ -105,33 +109,18 @@ class Scheduler final {
|
||||
delete static_cast<Proc *>(proc);
|
||||
}
|
||||
|
||||
template<typename Proc, typename... Args>
|
||||
static auto then(ProcessHandler *handler, Args &&... args) {
|
||||
if(handler) {
|
||||
auto proc = typename ProcessHandler::instance_type{new Proc{std::forward<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;
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename std::vector<ProcessHandler>::size_type;
|
||||
using size_type = typename std::vector<process_handler>::size_type;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Scheduler() ENTT_NOEXCEPT = default;
|
||||
scheduler() ENTT_NOEXCEPT = default;
|
||||
|
||||
/*! @brief Copying a scheduler isn't allowed. */
|
||||
Scheduler(const Scheduler &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Scheduler(Scheduler &&) = default;
|
||||
scheduler(scheduler &&) = default;
|
||||
|
||||
/*! @brief Copying a scheduler isn't allowed. @return This scheduler. */
|
||||
Scheduler & operator=(const Scheduler &) = delete;
|
||||
/*! @brief Default move assignment operator. @return This scheduler. */
|
||||
Scheduler & operator=(Scheduler &&) = default;
|
||||
scheduler & operator=(scheduler &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Number of processes currently scheduled.
|
||||
@@ -170,13 +159,13 @@ public:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* // schedules a task in the form of a process class
|
||||
* scheduler.attach<MyProcess>(arguments...)
|
||||
* scheduler.attach<my_process>(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>();
|
||||
* .then<my_other_process>();
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Proc Type of process to schedule.
|
||||
@@ -186,13 +175,12 @@ public:
|
||||
*/
|
||||
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()};
|
||||
static_assert(std::is_base_of_v<process<Proc, Delta>, Proc>);
|
||||
auto proc = typename process_handler::instance_type{new Proc{std::forward<Args>(args)...}, &scheduler::deleter<Proc>};
|
||||
process_handler handler{std::move(proc), &scheduler::update<Proc>, &scheduler::abort<Proc>, nullptr};
|
||||
// forces the process to exit the uninitialized state
|
||||
handler.update(handler, {}, nullptr);
|
||||
return continuation{&handlers.emplace_back(std::move(handler))};
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -204,12 +192,13 @@ public:
|
||||
* following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(Delta delta, auto succeed, auto fail);
|
||||
* 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.
|
||||
*
|
||||
@@ -236,10 +225,10 @@ public:
|
||||
* // code
|
||||
* })
|
||||
* // appends a child in the form of a process class
|
||||
* .then<MyProcess>(arguments...);
|
||||
* .then<my_process>(arguments...);
|
||||
* @endcode
|
||||
*
|
||||
* @sa ProcessAdaptor
|
||||
* @sa process_adaptor
|
||||
*
|
||||
* @tparam Func Type of process to schedule.
|
||||
* @param func Either a lambda or a functor to use as a process.
|
||||
@@ -247,7 +236,7 @@ public:
|
||||
*/
|
||||
template<typename Func>
|
||||
auto attach(Func &&func) {
|
||||
using Proc = ProcessAdaptor<std::decay_t<Func>, Delta>;
|
||||
using Proc = process_adaptor<std::decay_t<Func>, Delta>;
|
||||
return attach<Proc>(std::forward<Func>(func));
|
||||
}
|
||||
|
||||
@@ -301,7 +290,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<ProcessHandler> handlers{};
|
||||
std::vector<process_handler> handlers{};
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -10,6 +10,7 @@
|
||||
#include "../core/hashed_string.hpp"
|
||||
#include "handle.hpp"
|
||||
#include "loader.hpp"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -26,27 +27,23 @@ namespace entt {
|
||||
* @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>>;
|
||||
class resource_cache {
|
||||
using container_type = std::unordered_map<hashed_string::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;
|
||||
using resource_type = typename hashed_string::hash_type;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
ResourceCache() = default;
|
||||
resource_cache() = default;
|
||||
|
||||
/*! @brief Copying a cache isn't allowed. */
|
||||
ResourceCache(const ResourceCache &) ENTT_NOEXCEPT = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
ResourceCache(ResourceCache &&) ENTT_NOEXCEPT = default;
|
||||
resource_cache(resource_cache &&) = default;
|
||||
|
||||
/*! @brief Copying a cache isn't allowed. @return This cache. */
|
||||
ResourceCache & operator=(const ResourceCache &) ENTT_NOEXCEPT = delete;
|
||||
/*! @brief Default move assignment operator. @return This cache. */
|
||||
ResourceCache & operator=(ResourceCache &&) ENTT_NOEXCEPT = default;
|
||||
resource_cache & operator=(resource_cache &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Number of resources managed by a cache.
|
||||
@@ -86,24 +83,31 @@ public:
|
||||
* If the identifier is already present in the cache, this function does
|
||||
* nothing and the arguments are simply discarded.
|
||||
*
|
||||
* @warning
|
||||
* If the resource cannot be loaded correctly, the returned handle will be
|
||||
* invalid and any use of it will result in undefined behavior.
|
||||
*
|
||||
* @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.
|
||||
* @return A handle for the given resource.
|
||||
*/
|
||||
template<typename Loader, typename... Args>
|
||||
bool load(const resource_type id, Args &&... args) {
|
||||
static_assert(std::is_base_of<ResourceLoader<Loader, Resource>, Loader>::value, "!");
|
||||
resource_handle<Resource> load(const resource_type id, Args &&... args) {
|
||||
static_assert(std::is_base_of_v<resource_loader<Loader, Resource>, Loader>);
|
||||
resource_handle<Resource> handle{};
|
||||
|
||||
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);
|
||||
if(auto it = resources.find(id); it == resources.cend()) {
|
||||
if(auto resource = Loader{}.get(std::forward<Args>(args)...); resource) {
|
||||
resources[id] = resource;
|
||||
handle = std::move(resource);
|
||||
}
|
||||
} else {
|
||||
handle = it->second;
|
||||
}
|
||||
|
||||
return loaded;
|
||||
return handle;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -119,14 +123,18 @@ public:
|
||||
* Arguments are forwarded directly to the loader in order to construct
|
||||
* properly the requested resource.
|
||||
*
|
||||
* @warning
|
||||
* If the resource cannot be loaded correctly, the returned handle will be
|
||||
* invalid and any use of it will result in undefined behavior.
|
||||
*
|
||||
* @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.
|
||||
* @return A handle for the given resource.
|
||||
*/
|
||||
template<typename Loader, typename... Args>
|
||||
bool reload(const resource_type id, Args &&... args) {
|
||||
resource_handle<Resource> reload(const resource_type id, Args &&... args) {
|
||||
return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
@@ -143,7 +151,7 @@ public:
|
||||
* @return A handle for the given resource.
|
||||
*/
|
||||
template<typename Loader, typename... Args>
|
||||
ResourceHandle<Resource> temp(Args &&... args) const {
|
||||
resource_handle<Resource> temp(Args &&... args) const {
|
||||
return { Loader{}.get(std::forward<Args>(args)...) };
|
||||
}
|
||||
|
||||
@@ -155,12 +163,12 @@ public:
|
||||
* cache contains the resource itself. Otherwise the returned handle is
|
||||
* uninitialized and accessing it results in undefined behavior.
|
||||
*
|
||||
* @sa ResourceHandle
|
||||
* @sa resource_handle
|
||||
*
|
||||
* @param id Unique resource identifier.
|
||||
* @return A handle for the given resource.
|
||||
*/
|
||||
ResourceHandle<Resource> handle(const resource_type id) const {
|
||||
resource_handle<Resource> handle(const resource_type id) const {
|
||||
auto it = resources.find(id);
|
||||
return { it == resources.end() ? nullptr : it->second };
|
||||
}
|
||||
@@ -183,9 +191,7 @@ public:
|
||||
* @param id Unique resource identifier.
|
||||
*/
|
||||
void discard(const resource_type id) ENTT_NOEXCEPT {
|
||||
auto it = resources.find(id);
|
||||
|
||||
if(it != resources.end()) {
|
||||
if(auto it = resources.find(id); it != resources.end()) {
|
||||
resources.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
27
src/entt/resource/fwd.hpp
Normal file
27
src/entt/resource/fwd.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef ENTT_RESOURCE_FWD_HPP
|
||||
#define ENTT_RESOURCE_FWD_HPP
|
||||
|
||||
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/*! @class resource_cache */
|
||||
template<typename>
|
||||
class resource_cache;
|
||||
|
||||
/*! @class resource_handle */
|
||||
template<typename>
|
||||
class resource_handle;
|
||||
|
||||
/*! @class resource_loader */
|
||||
template<typename, typename>
|
||||
class resource_loader;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_RESOURCE_FWD_HPP
|
||||
@@ -4,17 +4,13 @@
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
#include "../config/config.h"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
template<typename Resource>
|
||||
class ResourceCache;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Shared resource handle.
|
||||
*
|
||||
@@ -28,24 +24,17 @@ class ResourceCache;
|
||||
* @tparam Resource Type of resource managed by a handle.
|
||||
*/
|
||||
template<typename Resource>
|
||||
class ResourceHandle final {
|
||||
class resource_handle {
|
||||
/*! @brief Resource handles are friends of their caches. */
|
||||
friend class ResourceCache<Resource>;
|
||||
friend class resource_cache<Resource>;
|
||||
|
||||
ResourceHandle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
|
||||
resource_handle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
|
||||
: resource{std::move(res)}
|
||||
{}
|
||||
|
||||
public:
|
||||
/*! @brief Default copy constructor. */
|
||||
ResourceHandle(const ResourceHandle &) ENTT_NOEXCEPT = default;
|
||||
/*! @brief Default move constructor. */
|
||||
ResourceHandle(ResourceHandle &&) ENTT_NOEXCEPT = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This handle. */
|
||||
ResourceHandle & operator=(const ResourceHandle &) ENTT_NOEXCEPT = default;
|
||||
/*! @brief Default move assignment operator. @return This handle. */
|
||||
ResourceHandle & operator=(ResourceHandle &&) ENTT_NOEXCEPT = default;
|
||||
/*! @brief Default constructor. */
|
||||
resource_handle() ENTT_NOEXCEPT = default;
|
||||
|
||||
/**
|
||||
* @brief Gets a reference to the managed resource.
|
||||
@@ -58,7 +47,7 @@ public:
|
||||
* @return A reference to the managed resource.
|
||||
*/
|
||||
const Resource & get() const ENTT_NOEXCEPT {
|
||||
assert(static_cast<bool>(resource));
|
||||
ENTT_ASSERT(static_cast<bool>(resource));
|
||||
return *resource;
|
||||
}
|
||||
|
||||
@@ -95,13 +84,14 @@ public:
|
||||
* @return A pointer to the managed resource or `nullptr` if the handle
|
||||
* contains no resource at all.
|
||||
*/
|
||||
inline const Resource * operator ->() const ENTT_NOEXCEPT {
|
||||
assert(static_cast<bool>(resource));
|
||||
inline const Resource * operator->() const ENTT_NOEXCEPT {
|
||||
ENTT_ASSERT(static_cast<bool>(resource));
|
||||
return resource.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true if the handle contains a resource, false otherwise.
|
||||
* @brief Returns true if a handle contains a resource, false otherwise.
|
||||
* @return True if the handle contains a resource, false otherwise.
|
||||
*/
|
||||
explicit operator bool() const { return static_cast<bool>(resource); }
|
||||
|
||||
|
||||
@@ -3,15 +3,12 @@
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
template<typename Resource>
|
||||
class ResourceCache;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Base class for resource loaders.
|
||||
*
|
||||
@@ -22,12 +19,12 @@ class ResourceCache;
|
||||
* As an example:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* struct MyResource {};
|
||||
* struct my_resource {};
|
||||
*
|
||||
* struct MyLoader: entt::ResourceLoader<MyLoader, MyResource> {
|
||||
* std::shared_ptr<MyResource> load(int) const {
|
||||
* struct my_loader: entt::resource_loader<my_loader, my_resource> {
|
||||
* std::shared_ptr<my_resource> load(int) const {
|
||||
* // use the integer value somehow
|
||||
* return std::make_shared<MyResource>();
|
||||
* return std::make_shared<my_resource>();
|
||||
* }
|
||||
* };
|
||||
* @endcode
|
||||
@@ -45,10 +42,16 @@ class ResourceCache;
|
||||
* @tparam Resource Type of resource for which to use the loader.
|
||||
*/
|
||||
template<typename Loader, typename Resource>
|
||||
class ResourceLoader {
|
||||
class resource_loader {
|
||||
/*! @brief Resource loaders are friends of their caches. */
|
||||
friend class ResourceCache<Resource>;
|
||||
friend class resource_cache<Resource>;
|
||||
|
||||
/**
|
||||
* @brief Loads the resource and returns it.
|
||||
* @tparam Args Types of arguments for the loader.
|
||||
* @param args Arguments for the loader.
|
||||
* @return The resource just loaded or an empty pointer in case of errors.
|
||||
*/
|
||||
template<typename... Args>
|
||||
std::shared_ptr<Resource> get(Args &&... args) const {
|
||||
return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
|
||||
|
||||
@@ -2,13 +2,60 @@
|
||||
#define ENTT_SIGNAL_DELEGATE_HPP
|
||||
|
||||
|
||||
#include <utility>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @cond TURN_OFF_DOXYGEN
|
||||
* Internal details not to be documented.
|
||||
*/
|
||||
|
||||
|
||||
namespace internal {
|
||||
|
||||
|
||||
template<typename Ret, typename... Args>
|
||||
auto to_function_pointer(Ret(*)(Args...)) -> Ret(*)(Args...);
|
||||
|
||||
|
||||
template<typename Ret, typename... Args, typename Type>
|
||||
auto to_function_pointer(Ret(*)(Type *, Args...), Type *) -> Ret(*)(Args...);
|
||||
|
||||
|
||||
template<typename Class, typename Ret, typename... Args>
|
||||
auto to_function_pointer(Ret(Class:: *)(Args...), Class *) -> Ret(*)(Args...);
|
||||
|
||||
|
||||
template<typename Class, typename Ret, typename... Args>
|
||||
auto to_function_pointer(Ret(Class:: *)(Args...) const, Class *) -> Ret(*)(Args...);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Internal details not to be documented.
|
||||
* @endcond TURN_OFF_DOXYGEN
|
||||
*/
|
||||
|
||||
|
||||
/*! @brief Used to wrap a function or a member of a specified type. */
|
||||
template<auto>
|
||||
struct connect_arg_t {};
|
||||
|
||||
|
||||
/*! @brief Constant of type connect_arg_t used to disambiguate calls. */
|
||||
template<auto Func>
|
||||
constexpr connect_arg_t<Func> connect_arg{};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic delegate implementation.
|
||||
*
|
||||
@@ -16,150 +63,219 @@ namespace entt {
|
||||
* compile-time error unless the template parameter is a function type.
|
||||
*/
|
||||
template<typename>
|
||||
class Delegate;
|
||||
class delegate;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Utility class to send around functions and member functions.
|
||||
* @brief Utility class to use to send around functions and members.
|
||||
*
|
||||
* Unmanaged delegate for function pointers and member functions. Users of this
|
||||
* class are in charge of disconnecting instances before deleting them.
|
||||
* Unmanaged delegate for function pointers and members. Users of this class are
|
||||
* in charge of disconnecting instances before deleting them.
|
||||
*
|
||||
* A delegate can be used as general purpose invoker with no memory overhead for
|
||||
* free functions and member functions provided along with an instance on which
|
||||
* to invoke them.
|
||||
* free functions (with or without payload) and members provided along with an
|
||||
* instance on which to invoke them.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of arguments of a function type.
|
||||
*/
|
||||
template<typename Ret, typename... Args>
|
||||
class Delegate<Ret(Args...)> final {
|
||||
using proto_fn_type = Ret(void *, Args...);
|
||||
using stub_type = std::pair<void *, proto_fn_type *>;
|
||||
|
||||
template<Ret(*Function)(Args...)>
|
||||
static Ret proto(void *, Args... args) {
|
||||
return (Function)(args...);
|
||||
}
|
||||
|
||||
template<typename Class, Ret(Class:: *Member)(Args...) const>
|
||||
static Ret proto(void *instance, Args... args) {
|
||||
return (static_cast<const Class *>(instance)->*Member)(args...);
|
||||
}
|
||||
|
||||
template<typename Class, Ret(Class:: *Member)(Args...)>
|
||||
static Ret proto(void *instance, Args... args) {
|
||||
return (static_cast<Class *>(instance)->*Member)(args...);
|
||||
}
|
||||
class delegate<Ret(Args...)> {
|
||||
using proto_fn_type = Ret(const void *, Args...);
|
||||
|
||||
public:
|
||||
/*! @brief Function type of the delegate. */
|
||||
using function_type = Ret(Args...);
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Delegate() ENTT_NOEXCEPT
|
||||
: stub{}
|
||||
delegate() ENTT_NOEXCEPT
|
||||
: fn{nullptr}, data{nullptr}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Checks whether a delegate actually stores a listener.
|
||||
* @return True if the delegate is empty, false otherwise.
|
||||
*/
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
// no need to test also stub.first
|
||||
return !stub.second;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Binds a free function to a delegate.
|
||||
* @brief Constructs a delegate and connects a free function to it.
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<Ret(*Function)(Args...)>
|
||||
template<auto Function>
|
||||
delegate(connect_arg_t<Function>) ENTT_NOEXCEPT
|
||||
: delegate{}
|
||||
{
|
||||
connect<Function>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Constructs a delegate and connects a member for a given instance
|
||||
* or a free function with payload.
|
||||
* @tparam Candidate Member or free function to connect to the delegate.
|
||||
* @tparam Type Type of class or type of payload.
|
||||
* @param value_or_instance A valid pointer that fits the purpose.
|
||||
*/
|
||||
template<auto Candidate, typename Type>
|
||||
delegate(connect_arg_t<Candidate>, Type *value_or_instance) ENTT_NOEXCEPT
|
||||
: delegate{}
|
||||
{
|
||||
connect<Candidate>(value_or_instance);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects a free function to a delegate.
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<auto Function>
|
||||
void connect() ENTT_NOEXCEPT {
|
||||
stub = std::make_pair(nullptr, &proto<Function>);
|
||||
static_assert(std::is_invocable_r_v<Ret, decltype(Function), Args...>);
|
||||
data = nullptr;
|
||||
|
||||
fn = [](const void *, Args... args) -> Ret {
|
||||
// this allows void(...) to eat return values and avoid errors
|
||||
return Ret(std::invoke(Function, args...));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects a member function for a given instance to a delegate.
|
||||
* @brief Connects a member function for a given instance or a free function
|
||||
* with payload to a delegate.
|
||||
*
|
||||
* The delegate isn't responsible for the connected object. Users must
|
||||
* guarantee that the lifetime of the instance overcomes the one of the
|
||||
* delegate.
|
||||
* The delegate isn't responsible for the connected object or the payload.
|
||||
* Users must always guarantee that the lifetime of the instance overcomes
|
||||
* the one of the delegate.<br/>
|
||||
* When used to connect a free function with payload, its signature must be
|
||||
* such that the instance is the first argument before the ones used to
|
||||
* define the delegate itself.
|
||||
*
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the delegate.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
* @tparam Candidate Member or free function to connect to the delegate.
|
||||
* @tparam Type Type of class or type of payload.
|
||||
* @param value_or_instance A valid pointer that fits the purpose.
|
||||
*/
|
||||
template<typename Class, Ret(Class:: *Member)(Args...) const>
|
||||
void connect(Class *instance) ENTT_NOEXCEPT {
|
||||
stub = std::make_pair(instance, &proto<Class, Member>);
|
||||
}
|
||||
template<auto Candidate, typename Type>
|
||||
void connect(Type *value_or_instance) ENTT_NOEXCEPT {
|
||||
static_assert(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, Args...>);
|
||||
data = value_or_instance;
|
||||
|
||||
/**
|
||||
* @brief Connects a member function for a given instance to a delegate.
|
||||
*
|
||||
* The delegate isn't responsible for the connected object. Users must
|
||||
* guarantee that the lifetime of the instance overcomes the one of the
|
||||
* delegate.
|
||||
*
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the delegate.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
*/
|
||||
template<typename Class, Ret(Class:: *Member)(Args...)>
|
||||
void connect(Class *instance) ENTT_NOEXCEPT {
|
||||
stub = std::make_pair(instance, &proto<Class, Member>);
|
||||
fn = [](const void *payload, Args... args) -> Ret {
|
||||
Type *curr = nullptr;
|
||||
|
||||
if constexpr(std::is_const_v<Type>) {
|
||||
curr = static_cast<Type *>(payload);
|
||||
} else {
|
||||
curr = static_cast<Type *>(const_cast<void *>(payload));
|
||||
}
|
||||
|
||||
// this allows void(...) to eat return values and avoid errors
|
||||
return Ret(std::invoke(Candidate, curr, args...));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets a delegate.
|
||||
*
|
||||
* After a reset, a delegate can be safely invoked with no effect.
|
||||
* After a reset, a delegate cannot be invoked anymore.
|
||||
*/
|
||||
void reset() ENTT_NOEXCEPT {
|
||||
stub.second = nullptr;
|
||||
fn = nullptr;
|
||||
data = nullptr;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns the instance linked to a delegate, if any.
|
||||
* @return An opaque pointer to the instance linked to the delegate, if any.
|
||||
*/
|
||||
const void * instance() const ENTT_NOEXCEPT {
|
||||
return data;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Triggers a delegate.
|
||||
*
|
||||
* The delegate invokes the underlying function and returns the result.
|
||||
*
|
||||
* @warning
|
||||
* Attempting to trigger an invalid delegate results in undefined
|
||||
* behavior.<br/>
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* delegate has not yet been set.
|
||||
*
|
||||
* @param args Arguments to use to invoke the underlying function.
|
||||
* @return The value returned by the underlying function.
|
||||
*/
|
||||
Ret operator()(Args... args) const {
|
||||
return stub.second(stub.first, args...);
|
||||
ENTT_ASSERT(fn);
|
||||
return fn(data, args...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the contents of the two delegates are different.
|
||||
* @brief Checks whether a delegate actually stores a listener.
|
||||
* @return False if the delegate is empty, true otherwise.
|
||||
*/
|
||||
explicit operator bool() const ENTT_NOEXCEPT {
|
||||
// no need to test also data
|
||||
return fn;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the connected functions differ.
|
||||
*
|
||||
* Two delegates are identical if they contain the same listener.
|
||||
* Instances connected to delegates are ignored by this operator. Use the
|
||||
* `instance` member function instead.
|
||||
*
|
||||
* @param other Delegate with which to compare.
|
||||
* @return True if the two delegates are identical, false otherwise.
|
||||
* @return False if the connected functions differ, true otherwise.
|
||||
*/
|
||||
bool operator==(const Delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
|
||||
return stub.first == other.stub.first && stub.second == other.stub.second;
|
||||
bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
|
||||
return fn == other.fn;
|
||||
}
|
||||
|
||||
private:
|
||||
stub_type stub;
|
||||
proto_fn_type *fn;
|
||||
const void *data;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Checks if the contents of the two delegates are different.
|
||||
* @brief Checks if the connected functions differ.
|
||||
*
|
||||
* Two delegates are identical if they contain the same listener.
|
||||
* Instances connected to delegates are ignored by this operator. Use the
|
||||
* `instance` member function instead.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of arguments of a function type.
|
||||
* @param lhs A valid delegate object.
|
||||
* @param rhs A valid delegate object.
|
||||
* @return True if the two delegates are different, false otherwise.
|
||||
* @return True if the connected functions differ, false otherwise.
|
||||
*/
|
||||
template<typename Ret, typename... Args>
|
||||
bool operator!=(const Delegate<Ret(Args...)> &lhs, const Delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
|
||||
bool operator!=(const delegate<Ret(Args...)> &lhs, const delegate<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Deduction guide.
|
||||
*
|
||||
* It allows to deduce the function type of the delegate directly from a
|
||||
* function provided to the constructor.
|
||||
*
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<auto Function>
|
||||
delegate(connect_arg_t<Function>) ENTT_NOEXCEPT
|
||||
-> delegate<std::remove_pointer_t<decltype(internal::to_function_pointer(Function))>>;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Deduction guide.
|
||||
*
|
||||
* It allows to deduce the function type of the delegate directly from a member
|
||||
* or a free function with payload provided to the constructor.
|
||||
*
|
||||
* @tparam Candidate Member or free function to connect to the delegate.
|
||||
* @tparam Type Type of class or type of payload.
|
||||
*/
|
||||
template<auto Candidate, typename Type>
|
||||
delegate(connect_arg_t<Candidate>, Type *) ENTT_NOEXCEPT
|
||||
-> delegate<std::remove_pointer_t<decltype(internal::to_function_pointer(Candidate, std::declval<Type *>()))>>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,11 +5,10 @@
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "../core/family.hpp"
|
||||
#include "../core/type_traits.hpp"
|
||||
#include "sigh.hpp"
|
||||
|
||||
|
||||
@@ -22,36 +21,36 @@ namespace entt {
|
||||
* A dispatcher can be used either to trigger an immediate event or to enqueue
|
||||
* events to be published all together once per tick.<br/>
|
||||
* Listeners are provided in the form of member functions. For each event of
|
||||
* type `Event`, listeners must have the following function type:
|
||||
* @code{.cpp}
|
||||
* void(const Event &)
|
||||
* @endcode
|
||||
* type `Event`, listeners are such that they can be invoked with an argument of
|
||||
* type `const Event &`, no matter what the return type is.
|
||||
*
|
||||
* Member functions named `receive` are automatically detected and registered or
|
||||
* unregistered by the dispatcher. The type of the instances is `Class *` (a
|
||||
* naked pointer). It means that users must guarantee that the lifetimes of the
|
||||
* instances overcome the one of the dispatcher itself to avoid crashes.
|
||||
* The type of the instances is `Class *` (a naked pointer). It means that users
|
||||
* must guarantee that the lifetimes of the instances overcome the one of the
|
||||
* dispatcher itself to avoid crashes.
|
||||
*/
|
||||
class Dispatcher final {
|
||||
using event_family = Family<struct InternalDispatcherEventFamily>;
|
||||
class dispatcher {
|
||||
using event_family = family<struct internal_dispatcher_event_family>;
|
||||
|
||||
template<typename Class, typename Event>
|
||||
using instance_type = typename SigH<void(const Event &)>::template instance_type<Class>;
|
||||
using instance_type = typename sigh<void(const Event &)>::template instance_type<Class>;
|
||||
|
||||
struct BaseSignalWrapper {
|
||||
virtual ~BaseSignalWrapper() = default;
|
||||
struct base_wrapper {
|
||||
virtual ~base_wrapper() = default;
|
||||
virtual void publish() = 0;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
struct SignalWrapper final: BaseSignalWrapper {
|
||||
using sink_type = typename SigH<void(const Event &)>::sink_type;
|
||||
struct signal_wrapper: base_wrapper {
|
||||
using signal_type = sigh<void(const Event &)>;
|
||||
using sink_type = typename signal_type::sink_type;
|
||||
|
||||
void publish() override {
|
||||
const auto &curr = current++;
|
||||
for(const auto &event: events[current]) {
|
||||
signal.publish(event);
|
||||
}
|
||||
|
||||
events[current++].clear();
|
||||
current %= std::extent<decltype(events)>::value;
|
||||
std::for_each(events[curr].cbegin(), events[curr].cend(), [this](const auto &event) { signal.publish(event); });
|
||||
events[curr].clear();
|
||||
}
|
||||
|
||||
inline sink_type sink() ENTT_NOEXCEPT {
|
||||
@@ -65,34 +64,66 @@ class Dispatcher final {
|
||||
|
||||
template<typename... Args>
|
||||
inline void enqueue(Args &&... args) {
|
||||
events[current].push_back({ std::forward<Args>(args)... });
|
||||
events[current].emplace_back(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
private:
|
||||
SigH<void(const Event &)> signal{};
|
||||
signal_type signal{};
|
||||
std::vector<Event> events[2];
|
||||
int current{};
|
||||
};
|
||||
|
||||
struct wrapper_data {
|
||||
std::unique_ptr<base_wrapper> wrapper;
|
||||
ENTT_ID_TYPE runtime_type;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
SignalWrapper<Event> & wrapper() {
|
||||
const auto type = event_family::type<Event>();
|
||||
static auto type() ENTT_NOEXCEPT {
|
||||
if constexpr(is_named_type_v<Event>) {
|
||||
return named_type_traits<Event>::value;
|
||||
} else {
|
||||
return event_family::type<Event>;
|
||||
}
|
||||
}
|
||||
|
||||
if(!(type < wrappers.size())) {
|
||||
wrappers.resize(type + 1);
|
||||
template<typename Event>
|
||||
signal_wrapper<Event> & assure() {
|
||||
const auto wtype = type<Event>();
|
||||
wrapper_data *wdata = nullptr;
|
||||
|
||||
if constexpr(is_named_type_v<Event>) {
|
||||
const auto it = std::find_if(wrappers.begin(), wrappers.end(), [wtype](const auto &candidate) {
|
||||
return candidate.wrapper && candidate.runtime_type == wtype;
|
||||
});
|
||||
|
||||
wdata = (it == wrappers.cend() ? &wrappers.emplace_back() : &(*it));
|
||||
} else {
|
||||
if(!(wtype < wrappers.size())) {
|
||||
wrappers.resize(wtype+1);
|
||||
}
|
||||
|
||||
wdata = &wrappers[wtype];
|
||||
|
||||
if(wdata->wrapper && wdata->runtime_type != wtype) {
|
||||
wrappers.emplace_back();
|
||||
std::swap(wrappers[wtype], wrappers.back());
|
||||
wdata = &wrappers[wtype];
|
||||
}
|
||||
}
|
||||
|
||||
if(!wrappers[type]) {
|
||||
wrappers[type] = std::make_unique<SignalWrapper<Event>>();
|
||||
if(!wdata->wrapper) {
|
||||
wdata->wrapper = std::make_unique<signal_wrapper<Event>>();
|
||||
wdata->runtime_type = wtype;
|
||||
}
|
||||
|
||||
return static_cast<SignalWrapper<Event> &>(*wrappers[type]);
|
||||
return static_cast<signal_wrapper<Event> &>(*wdata->wrapper);
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Type of sink for the given event. */
|
||||
template<typename Event>
|
||||
using sink_type = typename SignalWrapper<Event>::sink_type;
|
||||
using sink_type = typename signal_wrapper<Event>::sink_type;
|
||||
|
||||
/**
|
||||
* @brief Returns a sink object for the given event.
|
||||
@@ -101,19 +132,19 @@ public:
|
||||
*
|
||||
* The function type for a listener is:
|
||||
* @code{.cpp}
|
||||
* void(const Event &)
|
||||
* void(const Event &);
|
||||
* @endcode
|
||||
*
|
||||
* The order of invocation of the listeners isn't guaranteed.
|
||||
*
|
||||
* @sa SigH::Sink
|
||||
* @sa sink
|
||||
*
|
||||
* @tparam Event Type of event of which to get the sink.
|
||||
* @return A temporary sink object.
|
||||
*/
|
||||
template<typename Event>
|
||||
inline sink_type<Event> sink() ENTT_NOEXCEPT {
|
||||
return wrapper<Event>().sink();
|
||||
return assure<Event>().sink();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -128,7 +159,21 @@ public:
|
||||
*/
|
||||
template<typename Event, typename... Args>
|
||||
inline void trigger(Args &&... args) {
|
||||
wrapper<Event>().trigger(std::forward<Args>(args)...);
|
||||
assure<Event>().trigger(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Triggers an immediate event of the given type.
|
||||
*
|
||||
* All the listeners registered for the given type are immediately notified.
|
||||
* The event is discarded after the execution.
|
||||
*
|
||||
* @tparam Event Type of event to trigger.
|
||||
* @param event An instance of the given type of event.
|
||||
*/
|
||||
template<typename Event>
|
||||
inline void trigger(Event &&event) {
|
||||
assure<std::decay_t<Event>>().trigger(std::forward<Event>(event));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -137,13 +182,27 @@ public:
|
||||
* An event of the given type is queued. No listener is invoked. Use the
|
||||
* `update` member function to notify listeners when ready.
|
||||
*
|
||||
* @tparam Event Type of event to trigger.
|
||||
* @tparam Event Type of event to enqueue.
|
||||
* @tparam Args Types of arguments to use to construct the event.
|
||||
* @param args Arguments to use to construct the event.
|
||||
*/
|
||||
template<typename Event, typename... Args>
|
||||
inline void enqueue(Args &&... args) {
|
||||
wrapper<Event>().enqueue(std::forward<Args>(args)...);
|
||||
assure<Event>().enqueue(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Enqueues an event of the given type.
|
||||
*
|
||||
* An event of the given type is queued. No listener is invoked. Use the
|
||||
* `update` member function to notify listeners when ready.
|
||||
*
|
||||
* @tparam Event Type of event to enqueue.
|
||||
* @param event An instance of the given type of event.
|
||||
*/
|
||||
template<typename Event>
|
||||
inline void enqueue(Event &&event) {
|
||||
assure<std::decay_t<Event>>().enqueue(std::forward<Event>(event));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -157,7 +216,7 @@ public:
|
||||
*/
|
||||
template<typename Event>
|
||||
inline void update() {
|
||||
wrapper<Event>().publish();
|
||||
assure<Event>().publish();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,16 +228,16 @@ public:
|
||||
*/
|
||||
inline void update() const {
|
||||
for(auto pos = wrappers.size(); pos; --pos) {
|
||||
auto &wrapper = wrappers[pos-1];
|
||||
auto &wdata = wrappers[pos-1];
|
||||
|
||||
if(wrapper) {
|
||||
wrapper->publish();
|
||||
if(wdata.wrapper) {
|
||||
wdata.wrapper->publish();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<BaseSignalWrapper>> wrappers;
|
||||
std::vector<wrapper_data> wrappers;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -6,12 +6,12 @@
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
#include "../config/config.h"
|
||||
#include "../core/family.hpp"
|
||||
#include "../core/type_traits.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -23,11 +23,11 @@ namespace entt {
|
||||
* The emitter class template follows the CRTP idiom. To create a custom emitter
|
||||
* type, derived classes must inherit directly from the base class as:
|
||||
*
|
||||
* ```cpp
|
||||
* struct MyEmitter: Emitter<MyEmitter> {
|
||||
* @code{.cpp}
|
||||
* struct my_emitter: emitter<my_emitter> {
|
||||
* // ...
|
||||
* }
|
||||
* ```
|
||||
* @endcode
|
||||
*
|
||||
* Handlers for the type of events are created internally on the fly. It's not
|
||||
* required to specify in advance the full list of accepted types.<br/>
|
||||
@@ -39,17 +39,17 @@ namespace entt {
|
||||
* @tparam Derived Actual type of emitter that extends the class template.
|
||||
*/
|
||||
template<typename Derived>
|
||||
class Emitter {
|
||||
using handler_family = Family<struct InternalEmitterHandlerFamily>;
|
||||
class emitter {
|
||||
using handler_family = family<struct internal_emitter_handler_family>;
|
||||
|
||||
struct BaseHandler {
|
||||
virtual ~BaseHandler() = default;
|
||||
struct base_handler {
|
||||
virtual ~base_handler() = default;
|
||||
virtual bool empty() const ENTT_NOEXCEPT = 0;
|
||||
virtual void clear() ENTT_NOEXCEPT = 0;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
struct Handler final: BaseHandler {
|
||||
struct event_handler: base_handler {
|
||||
using listener_type = std::function<void(const Event &, Derived &)>;
|
||||
using element_type = std::pair<bool, listener_type>;
|
||||
using container_type = std::list<element_type>;
|
||||
@@ -58,27 +58,27 @@ class Emitter {
|
||||
bool empty() const ENTT_NOEXCEPT override {
|
||||
auto pred = [](auto &&element) { return element.first; };
|
||||
|
||||
return std::all_of(onceL.cbegin(), onceL.cend(), pred) &&
|
||||
std::all_of(onL.cbegin(), onL.cend(), pred);
|
||||
return std::all_of(once_list.cbegin(), once_list.cend(), pred) &&
|
||||
std::all_of(on_list.cbegin(), on_list.cend(), pred);
|
||||
}
|
||||
|
||||
void clear() ENTT_NOEXCEPT override {
|
||||
if(publishing) {
|
||||
auto func = [](auto &&element) { element.first = true; };
|
||||
std::for_each(onceL.begin(), onceL.end(), func);
|
||||
std::for_each(onL.begin(), onL.end(), func);
|
||||
std::for_each(once_list.begin(), once_list.end(), func);
|
||||
std::for_each(on_list.begin(), on_list.end(), func);
|
||||
} else {
|
||||
onceL.clear();
|
||||
onL.clear();
|
||||
once_list.clear();
|
||||
on_list.clear();
|
||||
}
|
||||
}
|
||||
|
||||
inline connection_type once(listener_type listener) {
|
||||
return onceL.emplace(onceL.cend(), false, std::move(listener));
|
||||
return once_list.emplace(once_list.cend(), false, std::move(listener));
|
||||
}
|
||||
|
||||
inline connection_type on(listener_type listener) {
|
||||
return onL.emplace(onL.cend(), false, std::move(listener));
|
||||
return on_list.emplace(on_list.cend(), false, std::move(listener));
|
||||
}
|
||||
|
||||
void erase(connection_type conn) ENTT_NOEXCEPT {
|
||||
@@ -86,14 +86,14 @@ class Emitter {
|
||||
|
||||
if(!publishing) {
|
||||
auto pred = [](auto &&element) { return element.first; };
|
||||
onceL.remove_if(pred);
|
||||
onL.remove_if(pred);
|
||||
once_list.remove_if(pred);
|
||||
on_list.remove_if(pred);
|
||||
}
|
||||
}
|
||||
|
||||
void publish(const Event &event, Derived &ref) {
|
||||
container_type currentL;
|
||||
onceL.swap(currentL);
|
||||
container_type swap_list;
|
||||
once_list.swap(swap_list);
|
||||
|
||||
auto func = [&event, &ref](auto &&element) {
|
||||
return element.first ? void() : element.second(event, ref);
|
||||
@@ -101,39 +101,71 @@ class Emitter {
|
||||
|
||||
publishing = true;
|
||||
|
||||
std::for_each(onL.rbegin(), onL.rend(), func);
|
||||
std::for_each(currentL.rbegin(), currentL.rend(), func);
|
||||
std::for_each(on_list.rbegin(), on_list.rend(), func);
|
||||
std::for_each(swap_list.rbegin(), swap_list.rend(), func);
|
||||
|
||||
publishing = false;
|
||||
|
||||
onL.remove_if([](auto &&element) { return element.first; });
|
||||
on_list.remove_if([](auto &&element) { return element.first; });
|
||||
}
|
||||
|
||||
private:
|
||||
bool publishing{false};
|
||||
container_type onceL{};
|
||||
container_type onL{};
|
||||
container_type once_list{};
|
||||
container_type on_list{};
|
||||
};
|
||||
|
||||
struct handler_data {
|
||||
std::unique_ptr<base_handler> handler;
|
||||
ENTT_ID_TYPE runtime_type;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
Handler<Event> & handler() ENTT_NOEXCEPT {
|
||||
const std::size_t family = handler_family::type<Event>();
|
||||
static auto type() ENTT_NOEXCEPT {
|
||||
if constexpr(is_named_type_v<Event>) {
|
||||
return named_type_traits<Event>::value;
|
||||
} else {
|
||||
return handler_family::type<Event>;
|
||||
}
|
||||
}
|
||||
|
||||
if(!(family < handlers.size())) {
|
||||
handlers.resize(family+1);
|
||||
template<typename Event>
|
||||
event_handler<Event> * assure() const ENTT_NOEXCEPT {
|
||||
const auto htype = type<Event>();
|
||||
handler_data *hdata = nullptr;
|
||||
|
||||
if constexpr(is_named_type_v<Event>) {
|
||||
const auto it = std::find_if(handlers.begin(), handlers.end(), [htype](const auto &candidate) {
|
||||
return candidate.handler && candidate.runtime_type == htype;
|
||||
});
|
||||
|
||||
hdata = (it == handlers.cend() ? &handlers.emplace_back() : &(*it));
|
||||
} else {
|
||||
if(!(htype < handlers.size())) {
|
||||
handlers.resize(htype+1);
|
||||
}
|
||||
|
||||
hdata = &handlers[htype];
|
||||
|
||||
if(hdata->handler && hdata->runtime_type != htype) {
|
||||
handlers.emplace_back();
|
||||
std::swap(handlers[htype], handlers.back());
|
||||
hdata = &handlers[htype];
|
||||
}
|
||||
}
|
||||
|
||||
if(!handlers[family]) {
|
||||
handlers[family] = std::make_unique<Handler<Event>>();
|
||||
if(!hdata->handler) {
|
||||
hdata->handler = std::make_unique<event_handler<Event>>();
|
||||
hdata->runtime_type = htype;
|
||||
}
|
||||
|
||||
return static_cast<Handler<Event> &>(*handlers[family]);
|
||||
return static_cast<event_handler<Event> *>(hdata->handler.get());
|
||||
}
|
||||
|
||||
public:
|
||||
/** @brief Type of listeners accepted for the given event. */
|
||||
template<typename Event>
|
||||
using Listener = typename Handler<Event>::listener_type;
|
||||
using listener = typename event_handler<Event>::listener_type;
|
||||
|
||||
/**
|
||||
* @brief Generic connection type for events.
|
||||
@@ -145,56 +177,35 @@ public:
|
||||
* @tparam Event Type of event for which the connection is created.
|
||||
*/
|
||||
template<typename Event>
|
||||
struct Connection final: private Handler<Event>::connection_type {
|
||||
struct connection: private event_handler<Event>::connection_type {
|
||||
/** @brief Event emitters are friend classes of connections. */
|
||||
friend class Emitter;
|
||||
friend class emitter;
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Connection() ENTT_NOEXCEPT = default;
|
||||
connection() ENTT_NOEXCEPT = default;
|
||||
|
||||
/**
|
||||
* @brief Creates a connection that wraps its underlying instance.
|
||||
* @param conn A connection object to wrap.
|
||||
*/
|
||||
Connection(typename Handler<Event>::connection_type conn)
|
||||
: Handler<Event>::connection_type{std::move(conn)}
|
||||
connection(typename event_handler<Event>::connection_type conn)
|
||||
: event_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 assignment operator.
|
||||
* @return This connection.
|
||||
*/
|
||||
Connection & operator=(const Connection &) = default;
|
||||
|
||||
/**
|
||||
* @brief Default move assignment operator.
|
||||
* @return This connection.
|
||||
*/
|
||||
Connection & operator=(Connection &&) = default;
|
||||
};
|
||||
|
||||
/*! @brief Default constructor. */
|
||||
Emitter() ENTT_NOEXCEPT = default;
|
||||
emitter() ENTT_NOEXCEPT = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Emitter() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_base_of<Emitter<Derived>, Derived>::value, "!");
|
||||
virtual ~emitter() ENTT_NOEXCEPT {
|
||||
static_assert(std::is_base_of_v<emitter<Derived>, Derived>);
|
||||
}
|
||||
|
||||
/*! @brief Copying an emitter isn't allowed. */
|
||||
Emitter(const Emitter &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Emitter(Emitter &&) = default;
|
||||
emitter(emitter &&) = default;
|
||||
|
||||
/*! @brief Copying an emitter isn't allowed. @return This emitter. */
|
||||
Emitter & operator=(const Emitter &) = delete;
|
||||
/*! @brief Default move assignment operator. @return This emitter. */
|
||||
Emitter & operator=(Emitter &&) = default;
|
||||
emitter & operator=(emitter &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Emits the given event.
|
||||
@@ -209,7 +220,7 @@ public:
|
||||
*/
|
||||
template<typename Event, typename... Args>
|
||||
void publish(Args &&... args) {
|
||||
handler<Event>().publish({ std::forward<Args>(args)... }, *static_cast<Derived *>(this));
|
||||
assure<Event>()->publish({ std::forward<Args>(args)... }, *static_cast<Derived *>(this));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -229,12 +240,12 @@ public:
|
||||
* instances for later uses.
|
||||
*
|
||||
* @tparam Event Type of event to which to connect the listener.
|
||||
* @param listener The listener to register.
|
||||
* @param instance The listener to register.
|
||||
* @return Connection object that can be used to disconnect the listener.
|
||||
*/
|
||||
template<typename Event>
|
||||
Connection<Event> on(Listener<Event> listener) {
|
||||
return handler<Event>().on(std::move(listener));
|
||||
connection<Event> on(listener<Event> instance) {
|
||||
return assure<Event>()->on(std::move(instance));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -254,12 +265,12 @@ public:
|
||||
* instances for later uses.
|
||||
*
|
||||
* @tparam Event Type of event to which to connect the listener.
|
||||
* @param listener The listener to register.
|
||||
* @param instance The listener to register.
|
||||
* @return Connection object that can be used to disconnect the listener.
|
||||
*/
|
||||
template<typename Event>
|
||||
Connection<Event> once(Listener<Event> listener) {
|
||||
return handler<Event>().once(std::move(listener));
|
||||
connection<Event> once(listener<Event> instance) {
|
||||
return assure<Event>()->once(std::move(instance));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -272,8 +283,8 @@ public:
|
||||
* @param conn A valid connection.
|
||||
*/
|
||||
template<typename Event>
|
||||
void erase(Connection<Event> conn) ENTT_NOEXCEPT {
|
||||
handler<Event>().erase(std::move(conn));
|
||||
void erase(connection<Event> conn) ENTT_NOEXCEPT {
|
||||
assure<Event>()->erase(std::move(conn));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -286,7 +297,7 @@ public:
|
||||
*/
|
||||
template<typename Event>
|
||||
void clear() ENTT_NOEXCEPT {
|
||||
handler<Event>().clear();
|
||||
assure<Event>()->clear();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -296,8 +307,8 @@ public:
|
||||
* results in undefined behavior.
|
||||
*/
|
||||
void clear() ENTT_NOEXCEPT {
|
||||
std::for_each(handlers.begin(), handlers.end(), [](auto &&handler) {
|
||||
return handler ? handler->clear() : void();
|
||||
std::for_each(handlers.begin(), handlers.end(), [](auto &&hdata) {
|
||||
return hdata.handler ? hdata.handler->clear() : void();
|
||||
});
|
||||
}
|
||||
|
||||
@@ -308,11 +319,7 @@ public:
|
||||
*/
|
||||
template<typename Event>
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
const std::size_t family = handler_family::type<Event>();
|
||||
|
||||
return (!(family < handlers.size()) ||
|
||||
!handlers[family] ||
|
||||
static_cast<Handler<Event> &>(*handlers[family]).empty());
|
||||
return assure<Event>()->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -320,13 +327,13 @@ public:
|
||||
* @return True if there are no listeners registered, false otherwise.
|
||||
*/
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return std::all_of(handlers.cbegin(), handlers.cend(), [](auto &&handler) {
|
||||
return !handler || handler->empty();
|
||||
return std::all_of(handlers.cbegin(), handlers.cend(), [](auto &&hdata) {
|
||||
return !hdata.handler || hdata.handler->empty();
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<BaseHandler>> handlers{};
|
||||
mutable std::vector<handler_data> handlers{};
|
||||
};
|
||||
|
||||
|
||||
|
||||
27
src/entt/signal/fwd.hpp
Normal file
27
src/entt/signal/fwd.hpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#ifndef ENTT_SIGNAL_FWD_HPP
|
||||
#define ENTT_SIGNAL_FWD_HPP
|
||||
|
||||
|
||||
#include "../config/config.h"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/*! @class delegate */
|
||||
template<typename>
|
||||
class delegate;
|
||||
|
||||
/*! @class sink */
|
||||
template<typename>
|
||||
class sink;
|
||||
|
||||
/*! @class sigh */
|
||||
template<typename, typename>
|
||||
struct sigh;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_SIGNAL_FWD_HPP
|
||||
@@ -5,7 +5,11 @@
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <type_traits>
|
||||
#include "../config/config.h"
|
||||
#include "delegate.hpp"
|
||||
#include "fwd.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -20,71 +24,56 @@ namespace entt {
|
||||
namespace internal {
|
||||
|
||||
|
||||
template<typename>
|
||||
struct sigh_traits;
|
||||
|
||||
|
||||
template<typename Ret, typename... Args>
|
||||
struct sigh_traits<Ret(Args...)> {
|
||||
using proto_fn_type = Ret(void *, Args...);
|
||||
using call_type = std::pair<void *, proto_fn_type *>;
|
||||
};
|
||||
|
||||
|
||||
template<typename, typename>
|
||||
struct Invoker;
|
||||
struct invoker;
|
||||
|
||||
|
||||
template<typename Ret, typename... Args, typename Collector>
|
||||
struct Invoker<Ret(Args...), Collector> {
|
||||
using proto_fn_type = typename sigh_traits<Ret(Args...)>::proto_fn_type;
|
||||
struct invoker<Ret(Args...), Collector> {
|
||||
virtual ~invoker() = default;
|
||||
|
||||
virtual ~Invoker() = default;
|
||||
|
||||
bool invoke(Collector &collector, proto_fn_type *proto, void *instance, Args... args) const {
|
||||
return collector(proto(instance, args...));
|
||||
bool invoke(Collector &collector, const delegate<Ret(Args...)> &delegate, Args... args) const {
|
||||
return collector(delegate(args...));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename... Args, typename Collector>
|
||||
struct Invoker<void(Args...), Collector> {
|
||||
using proto_fn_type = typename sigh_traits<void(Args...)>::proto_fn_type;
|
||||
struct invoker<void(Args...), Collector> {
|
||||
virtual ~invoker() = default;
|
||||
|
||||
virtual ~Invoker() = default;
|
||||
|
||||
bool invoke(Collector &, proto_fn_type *proto, void *instance, Args... args) const {
|
||||
return (proto(instance, args...), true);
|
||||
bool invoke(Collector &, const delegate<void(Args...)> &delegate, Args... args) const {
|
||||
return (delegate(args...), true);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
template<typename Ret>
|
||||
struct NullCollector final {
|
||||
struct null_collector {
|
||||
using result_type = Ret;
|
||||
bool operator()(result_type) const ENTT_NOEXCEPT { return true; }
|
||||
};
|
||||
|
||||
|
||||
template<>
|
||||
struct NullCollector<void> final {
|
||||
struct null_collector<void> {
|
||||
using result_type = void;
|
||||
bool operator()() const ENTT_NOEXCEPT { return true; }
|
||||
};
|
||||
|
||||
|
||||
template<typename>
|
||||
struct DefaultCollector;
|
||||
struct default_collector;
|
||||
|
||||
|
||||
template<typename Ret, typename... Args>
|
||||
struct DefaultCollector<Ret(Args...)> final {
|
||||
using collector_type = NullCollector<Ret>;
|
||||
struct default_collector<Ret(Args...)> {
|
||||
using collector_type = null_collector<Ret>;
|
||||
};
|
||||
|
||||
|
||||
template<typename Function>
|
||||
using DefaultCollectorType = typename DefaultCollector<Function>::collector_type;
|
||||
using default_collector_type = typename default_collector<Function>::collector_type;
|
||||
|
||||
|
||||
}
|
||||
@@ -105,7 +94,7 @@ using DefaultCollectorType = typename DefaultCollector<Function>::collector_type
|
||||
* @tparam Function A valid function type.
|
||||
*/
|
||||
template<typename Function>
|
||||
class Sink;
|
||||
class sink;
|
||||
|
||||
|
||||
/**
|
||||
@@ -117,8 +106,8 @@ class Sink;
|
||||
* @tparam Function A valid function type.
|
||||
* @tparam Collector Type of collector to use, if any.
|
||||
*/
|
||||
template<typename Function, typename Collector = internal::DefaultCollectorType<Function>>
|
||||
class SigH;
|
||||
template<typename Function, typename Collector = internal::default_collector_type<Function>>
|
||||
struct sigh;
|
||||
|
||||
|
||||
/**
|
||||
@@ -128,136 +117,112 @@ class SigH;
|
||||
* The function type for a listener is the one of the signal to which it
|
||||
* belongs.
|
||||
*
|
||||
* The clear separation between a signal and a sink permits to store the
|
||||
* former as private data member without exposing the publish functionality
|
||||
* to the users of a class.
|
||||
* The clear separation between a signal and a sink permits to store the former
|
||||
* as private data member without exposing the publish functionality to the
|
||||
* users of a class.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of arguments of a function type.
|
||||
*/
|
||||
template<typename Ret, typename... Args>
|
||||
class Sink<Ret(Args...)> final {
|
||||
class sink<Ret(Args...)> {
|
||||
/*! @brief A signal is allowed to create sinks. */
|
||||
template<typename, typename>
|
||||
friend class SigH;
|
||||
friend struct sigh;
|
||||
|
||||
using call_type = typename internal::sigh_traits<Ret(Args...)>::call_type;
|
||||
template<typename Type>
|
||||
Type * payload_type(Ret(*)(Type *, Args...));
|
||||
|
||||
template<Ret(*Function)(Args...)>
|
||||
static Ret proto(void *, Args... args) {
|
||||
return (Function)(args...);
|
||||
}
|
||||
|
||||
template<typename Class, Ret(Class:: *Member)(Args... args) const>
|
||||
static Ret proto(void *instance, Args... args) {
|
||||
return (static_cast<const Class *>(instance)->*Member)(args...);
|
||||
}
|
||||
|
||||
template<typename Class, Ret(Class:: *Member)(Args... args)>
|
||||
static Ret proto(void *instance, Args... args) {
|
||||
return (static_cast<Class *>(instance)->*Member)(args...);
|
||||
}
|
||||
|
||||
Sink(std::vector<call_type> *calls) ENTT_NOEXCEPT
|
||||
: calls{calls}
|
||||
sink(std::vector<delegate<Ret(Args...)>> *ref) ENTT_NOEXCEPT
|
||||
: calls{ref}
|
||||
{}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Returns false if at least a listener is connected to the sink.
|
||||
* @return True if the sink has no listeners connected, false otherwise.
|
||||
*/
|
||||
bool empty() const ENTT_NOEXCEPT {
|
||||
return calls->empty();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects a free function to a signal.
|
||||
*
|
||||
* The signal handler performs checks to avoid multiple connections for
|
||||
* free functions.
|
||||
* The signal handler performs checks to avoid multiple connections for free
|
||||
* functions.
|
||||
*
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<Ret(*Function)(Args...)>
|
||||
template<auto Function>
|
||||
void connect() {
|
||||
disconnect<Function>();
|
||||
calls->emplace_back(nullptr, &proto<Function>);
|
||||
delegate<Ret(Args...)> delegate{};
|
||||
delegate.template connect<Function>();
|
||||
calls->emplace_back(std::move(delegate));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects a member function for a given instance to a signal.
|
||||
* @brief Connects a member function or a free function with payload to a
|
||||
* signal.
|
||||
*
|
||||
* The signal isn't responsible for the connected object. Users must
|
||||
* guarantee that the lifetime of the instance overcomes the one of the
|
||||
* signal. On the other side, the signal handler performs checks to
|
||||
* avoid multiple connections for the same member function of a given
|
||||
* instance.
|
||||
* The signal isn't responsible for the connected object or the payload.
|
||||
* Users must always guarantee that the lifetime of the instance overcomes
|
||||
* the one of the delegate. On the other side, the signal handler performs
|
||||
* checks to avoid multiple connections for the same function.<br/>
|
||||
* When used to connect a free function with payload, its signature must be
|
||||
* such that the instance is the first argument before the ones used to
|
||||
* define the delegate itself.
|
||||
*
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the signal.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
* @tparam Candidate Member or free function to connect to the delegate.
|
||||
* @tparam Type Type of class or type of payload.
|
||||
* @param value_or_instance A valid pointer that fits the purpose.
|
||||
*/
|
||||
template<typename Class, Ret(Class:: *Member)(Args...) const = &Class::receive>
|
||||
void connect(Class *instance) {
|
||||
disconnect<Class, Member>(instance);
|
||||
calls->emplace_back(instance, &proto<Class, Member>);
|
||||
}
|
||||
template<auto Candidate, typename Type>
|
||||
void connect(Type *value_or_instance) {
|
||||
if constexpr(std::is_member_function_pointer_v<decltype(Candidate)>) {
|
||||
disconnect<Candidate>(value_or_instance);
|
||||
} else {
|
||||
disconnect<Candidate>();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects a member function for a given instance to a signal.
|
||||
*
|
||||
* The signal isn't responsible for the connected object. Users must
|
||||
* guarantee that the lifetime of the instance overcomes the one of the
|
||||
* signal. On the other side, the signal handler performs checks to
|
||||
* avoid multiple connections for the same member function of a given
|
||||
* instance.
|
||||
*
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the signal.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
*/
|
||||
template<typename Class, Ret(Class:: *Member)(Args...) = &Class::receive>
|
||||
void connect(Class *instance) {
|
||||
disconnect<Class, Member>(instance);
|
||||
calls->emplace_back(instance, &proto<Class, Member>);
|
||||
delegate<Ret(Args...)> delegate{};
|
||||
delegate.template connect<Candidate>(value_or_instance);
|
||||
calls->emplace_back(std::move(delegate));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects a free function from a signal.
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<Ret(*Function)(Args...)>
|
||||
template<auto Function>
|
||||
void disconnect() {
|
||||
call_type target{nullptr, &proto<Function>};
|
||||
calls->erase(std::remove(calls->begin(), calls->end(), std::move(target)), calls->end());
|
||||
delegate<Ret(Args...)> delegate{};
|
||||
|
||||
if constexpr(std::is_invocable_r_v<Ret, decltype(Function), Args...>) {
|
||||
delegate.template connect<Function>();
|
||||
} else {
|
||||
decltype(payload_type(Function)) payload = nullptr;
|
||||
delegate.template connect<Function>(payload);
|
||||
}
|
||||
|
||||
calls->erase(std::remove(calls->begin(), calls->end(), std::move(delegate)), calls->end());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects the given member function from a signal.
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the signal.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
*/
|
||||
template<typename Class, Ret(Class:: *Member)(Args...) const>
|
||||
void disconnect(Class *instance) {
|
||||
call_type target{instance, &proto<Class, Member>};
|
||||
calls->erase(std::remove(calls->begin(), calls->end(), std::move(target)), calls->end());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects the given member function from a signal.
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the signal.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
*/
|
||||
template<typename Class, Ret(Class:: *Member)(Args...)>
|
||||
void disconnect(Class *instance) {
|
||||
call_type target{instance, &proto<Class, Member>};
|
||||
calls->erase(std::remove(calls->begin(), calls->end(), std::move(target)), calls->end());
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Removes all existing connections for the given instance.
|
||||
* @brief Disconnects a given member function from a signal.
|
||||
* @tparam Member Member function to disconnect from the signal.
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
*/
|
||||
template<typename Class>
|
||||
template<auto Member, typename Class>
|
||||
void disconnect(Class *instance) {
|
||||
auto func = [instance](const call_type &call) { return call.first == instance; };
|
||||
calls->erase(std::remove_if(calls->begin(), calls->end(), std::move(func)), calls->end());
|
||||
static_assert(std::is_member_function_pointer_v<decltype(Member)>);
|
||||
delegate<Ret(Args...)> delegate{};
|
||||
delegate.template connect<Member>(instance);
|
||||
calls->erase(std::remove_if(calls->begin(), calls->end(), [&delegate](const auto &other) {
|
||||
return other == delegate && other.instance() == delegate.instance();
|
||||
}), calls->end());
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -268,7 +233,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<call_type> *calls;
|
||||
std::vector<delegate<Ret(Args...)>> *calls;
|
||||
};
|
||||
|
||||
|
||||
@@ -296,16 +261,13 @@ private:
|
||||
* @tparam Collector Type of collector to use, if any.
|
||||
*/
|
||||
template<typename Ret, typename... Args, typename Collector>
|
||||
class SigH<Ret(Args...), Collector> final: private internal::Invoker<Ret(Args...), Collector> {
|
||||
using call_type = typename internal::sigh_traits<Ret(Args...)>::call_type;
|
||||
|
||||
public:
|
||||
struct sigh<Ret(Args...), Collector>: private internal::invoker<Ret(Args...), Collector> {
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename std::vector<call_type>::size_type;
|
||||
using size_type = typename std::vector<delegate<Ret(Args...)>>::size_type;
|
||||
/*! @brief Collector type. */
|
||||
using collector_type = Collector;
|
||||
/*! @brief Sink type. */
|
||||
using sink_type = Sink<Ret(Args...)>;
|
||||
using sink_type = entt::sink<Ret(Args...)>;
|
||||
|
||||
/**
|
||||
* @brief Instance type when it comes to connecting member functions.
|
||||
@@ -353,7 +315,7 @@ public:
|
||||
void publish(Args... args) const {
|
||||
for(auto pos = calls.size(); pos; --pos) {
|
||||
auto &call = calls[pos-1];
|
||||
call.second(call.first, args...);
|
||||
call(args...);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -366,7 +328,7 @@ public:
|
||||
collector_type collector;
|
||||
|
||||
for(auto &&call: calls) {
|
||||
if(!this->invoke(collector, call.second, call.first, args...)) {
|
||||
if(!this->invoke(collector, call, args...)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -379,47 +341,16 @@ public:
|
||||
* @param lhs A valid signal object.
|
||||
* @param rhs A valid signal object.
|
||||
*/
|
||||
friend void swap(SigH &lhs, SigH &rhs) {
|
||||
friend void swap(sigh &lhs, sigh &rhs) {
|
||||
using std::swap;
|
||||
swap(lhs.calls, rhs.calls);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if the contents of the two signals are identical.
|
||||
*
|
||||
* Two signals are identical if they have the same size and the same
|
||||
* listeners registered exactly in the same order.
|
||||
*
|
||||
* @param other Signal with which to compare.
|
||||
* @return True if the two signals are identical, false otherwise.
|
||||
*/
|
||||
bool operator==(const SigH &other) const ENTT_NOEXCEPT {
|
||||
return std::equal(calls.cbegin(), calls.cend(), other.calls.cbegin(), other.calls.cend());
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<call_type> calls;
|
||||
std::vector<delegate<Ret(Args...)>> calls;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Checks if the contents of the two signals are different.
|
||||
*
|
||||
* Two signals are identical if they have the same size and the same
|
||||
* listeners registered exactly in the same order.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of arguments of a function type.
|
||||
* @param lhs A valid signal object.
|
||||
* @param rhs A valid signal object.
|
||||
* @return True if the two signals are different, false otherwise.
|
||||
*/
|
||||
template<typename Ret, typename... Args>
|
||||
bool operator!=(const SigH<Ret(Args...)> &lhs, const SigH<Ret(Args...)> &rhs) ENTT_NOEXCEPT {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -5,12 +5,16 @@
|
||||
include_directories($<TARGET_PROPERTY:EnTT,INTERFACE_INCLUDE_DIRECTORIES>)
|
||||
add_compile_options($<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_OPTIONS>)
|
||||
|
||||
macro(SETUP_LIBRARY_TARGET LIB_TARGET)
|
||||
set_target_properties(${LIB_TARGET} PROPERTIES CXX_EXTENSIONS OFF)
|
||||
target_compile_definitions(${LIB_TARGET} PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_DEFINITIONS>)
|
||||
target_compile_features(${LIB_TARGET} PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_FEATURES>)
|
||||
target_compile_options(${LIB_TARGET} PRIVATE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-pedantic -Wall>)
|
||||
target_compile_options(${LIB_TARGET} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>)
|
||||
endmacro()
|
||||
|
||||
add_library(odr OBJECT odr.cpp)
|
||||
set_target_properties(odr PROPERTIES CXX_EXTENSIONS OFF)
|
||||
target_compile_definitions(odr PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_DEFINITIONS>)
|
||||
target_compile_features(odr PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_FEATURES>)
|
||||
target_compile_options(odr PRIVATE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-pedantic -Wall>)
|
||||
target_compile_options(odr PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc>)
|
||||
SETUP_LIBRARY_TARGET(odr)
|
||||
|
||||
macro(SETUP_AND_ADD_TEST TEST_NAME TEST_SOURCE)
|
||||
add_executable(${TEST_NAME} $<TARGET_OBJECTS:odr> ${TEST_SOURCE})
|
||||
@@ -29,6 +33,26 @@ if(BUILD_BENCHMARK)
|
||||
SETUP_AND_ADD_TEST(benchmark benchmark/benchmark.cpp)
|
||||
endif()
|
||||
|
||||
# Test lib
|
||||
|
||||
if(BUILD_LIB)
|
||||
add_library(a_module_shared SHARED lib/a_module.cpp)
|
||||
add_library(a_module_static STATIC lib/a_module.cpp)
|
||||
add_library(another_module_shared SHARED lib/another_module.cpp)
|
||||
add_library(another_module_static STATIC lib/another_module.cpp)
|
||||
|
||||
SETUP_LIBRARY_TARGET(a_module_shared)
|
||||
SETUP_LIBRARY_TARGET(a_module_static)
|
||||
SETUP_LIBRARY_TARGET(another_module_shared)
|
||||
SETUP_LIBRARY_TARGET(another_module_static)
|
||||
|
||||
SETUP_AND_ADD_TEST(lib_shared lib/lib.cpp)
|
||||
target_link_libraries(lib_shared PRIVATE a_module_shared another_module_shared)
|
||||
|
||||
SETUP_AND_ADD_TEST(lib_static lib/lib.cpp)
|
||||
target_link_libraries(lib_static PRIVATE a_module_static another_module_static)
|
||||
endif()
|
||||
|
||||
# Test mod
|
||||
|
||||
if(BUILD_MOD)
|
||||
@@ -63,23 +87,31 @@ SETUP_AND_ADD_TEST(family entt/core/family.cpp)
|
||||
SETUP_AND_ADD_TEST(hashed_string entt/core/hashed_string.cpp)
|
||||
SETUP_AND_ADD_TEST(ident entt/core/ident.cpp)
|
||||
SETUP_AND_ADD_TEST(monostate entt/core/monostate.cpp)
|
||||
SETUP_AND_ADD_TEST(type_traits entt/core/type_traits.cpp)
|
||||
SETUP_AND_ADD_TEST(utility entt/core/utility.cpp)
|
||||
|
||||
# Test entity
|
||||
|
||||
SETUP_AND_ADD_TEST(actor entt/entity/actor.cpp)
|
||||
SETUP_AND_ADD_TEST(attachee entt/entity/attachee.cpp)
|
||||
SETUP_AND_ADD_TEST(entity entt/entity/entity.cpp)
|
||||
SETUP_AND_ADD_TEST(group entt/entity/group.cpp)
|
||||
SETUP_AND_ADD_TEST(helper entt/entity/helper.cpp)
|
||||
SETUP_AND_ADD_TEST(prototype entt/entity/prototype.cpp)
|
||||
SETUP_AND_ADD_TEST(registry entt/entity/registry.cpp)
|
||||
SETUP_AND_ADD_TEST(runtime_view entt/entity/runtime_view.cpp)
|
||||
SETUP_AND_ADD_TEST(snapshot entt/entity/snapshot.cpp)
|
||||
SETUP_AND_ADD_TEST(sparse_set entt/entity/sparse_set.cpp)
|
||||
SETUP_AND_ADD_TEST(storage entt/entity/storage.cpp)
|
||||
SETUP_AND_ADD_TEST(view entt/entity/view.cpp)
|
||||
|
||||
# Test locator
|
||||
|
||||
SETUP_AND_ADD_TEST(locator entt/locator/locator.cpp)
|
||||
|
||||
# Test meta
|
||||
|
||||
SETUP_AND_ADD_TEST(meta entt/meta/meta.cpp)
|
||||
|
||||
# Test process
|
||||
|
||||
SETUP_AND_ADD_TEST(process entt/process/process.cpp)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -5,7 +5,7 @@
|
||||
TEST(Algorithm, StdSort) {
|
||||
// well, I'm pretty sure it works, it's std::sort!!
|
||||
std::array<int, 5> arr{{4, 1, 3, 2, 0}};
|
||||
entt::StdSort sort;
|
||||
entt::std_sort sort;
|
||||
|
||||
sort(arr.begin(), arr.end());
|
||||
|
||||
@@ -16,7 +16,7 @@ TEST(Algorithm, StdSort) {
|
||||
|
||||
TEST(Algorithm, InsertionSort) {
|
||||
std::array<int, 5> arr{{4, 1, 3, 2, 0}};
|
||||
entt::InsertionSort sort;
|
||||
entt::insertion_sort sort;
|
||||
|
||||
sort(arr.begin(), arr.end());
|
||||
|
||||
@@ -25,16 +25,9 @@ TEST(Algorithm, InsertionSort) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Algorithm, OneShotBubbleSort) {
|
||||
std::array<int, 5> arr{{4, 1, 3, 2, 0}};
|
||||
entt::OneShotBubbleSort sort;
|
||||
|
||||
sort(arr.begin(), arr.end());
|
||||
sort(arr.begin(), arr.end());
|
||||
sort(arr.begin(), arr.end());
|
||||
sort(arr.begin(), arr.end());
|
||||
|
||||
for(typename decltype(arr)::size_type i = 0; i < (arr.size() - 1); ++i) {
|
||||
ASSERT_LT(arr[i], arr[i+1]);
|
||||
}
|
||||
TEST(Algorithm, InsertionSortEmptyContainer) {
|
||||
std::vector<int> vec{};
|
||||
entt::insertion_sort sort;
|
||||
// this should crash with asan enabled if we break the constraint
|
||||
sort(vec.begin(), vec.end());
|
||||
}
|
||||
|
||||
@@ -1,22 +1,22 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/family.hpp>
|
||||
|
||||
using my_family = entt::Family<struct MyFamily>;
|
||||
using your_family = entt::Family<struct YourFamily>;
|
||||
using a_family = entt::family<struct a_family_type>;
|
||||
using another_family = entt::family<struct another_family_type>;
|
||||
|
||||
TEST(Family, Functionalities) {
|
||||
auto myFamilyType = my_family::type<struct MyFamilyType>();
|
||||
auto mySameFamilyType = my_family::type<struct MyFamilyType>();
|
||||
auto myOtherFamilyType = my_family::type<struct MyOtherFamilyType>();
|
||||
auto yourFamilyType = your_family::type<struct YourFamilyType>();
|
||||
auto t1 = a_family::type<int>;
|
||||
auto t2 = a_family::type<int>;
|
||||
auto t3 = a_family::type<char>;
|
||||
auto t4 = another_family::type<double>;
|
||||
|
||||
ASSERT_EQ(myFamilyType, mySameFamilyType);
|
||||
ASSERT_NE(myFamilyType, myOtherFamilyType);
|
||||
ASSERT_EQ(myFamilyType, yourFamilyType);
|
||||
ASSERT_EQ(t1, t2);
|
||||
ASSERT_NE(t1, t3);
|
||||
ASSERT_EQ(t1, t4);
|
||||
}
|
||||
|
||||
TEST(Family, Uniqueness) {
|
||||
ASSERT_EQ(my_family::type<int>(), my_family::type<int &>());
|
||||
ASSERT_EQ(my_family::type<int>(), my_family::type<int &&>());
|
||||
ASSERT_EQ(my_family::type<int>(), my_family::type<const int &>());
|
||||
ASSERT_EQ(a_family::type<int>, a_family::type<int &>);
|
||||
ASSERT_EQ(a_family::type<int>, a_family::type<int &&>);
|
||||
ASSERT_EQ(a_family::type<int>, a_family::type<const int &>);
|
||||
}
|
||||
|
||||
@@ -1,34 +1,64 @@
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <type_traits>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
|
||||
TEST(HashedString, Functionalities) {
|
||||
using hash_type = entt::HashedString::hash_type;
|
||||
using hash_type = entt::hashed_string::hash_type;
|
||||
|
||||
const char *bar = "bar";
|
||||
|
||||
auto fooHs = entt::HashedString{"foo"};
|
||||
auto barHs = entt::HashedString{bar};
|
||||
auto foo_hs = entt::hashed_string{"foo"};
|
||||
auto bar_hs = entt::hashed_string{bar};
|
||||
|
||||
ASSERT_NE(static_cast<hash_type>(fooHs), static_cast<hash_type>(barHs));
|
||||
ASSERT_STREQ(static_cast<const char *>(fooHs), "foo");
|
||||
ASSERT_STREQ(static_cast<const char *>(barHs), bar);
|
||||
ASSERT_NE(static_cast<hash_type>(foo_hs), static_cast<hash_type>(bar_hs));
|
||||
ASSERT_STREQ(static_cast<const char *>(foo_hs), "foo");
|
||||
ASSERT_STREQ(static_cast<const char *>(bar_hs), bar);
|
||||
ASSERT_STREQ(foo_hs.data(), "foo");
|
||||
ASSERT_STREQ(bar_hs.data(), bar);
|
||||
|
||||
ASSERT_EQ(fooHs, fooHs);
|
||||
ASSERT_NE(fooHs, barHs);
|
||||
ASSERT_EQ(foo_hs, foo_hs);
|
||||
ASSERT_NE(foo_hs, bar_hs);
|
||||
|
||||
entt::HashedString hs{"foobar"};
|
||||
entt::hashed_string hs{"foobar"};
|
||||
|
||||
ASSERT_EQ(static_cast<hash_type>(hs), 0x85944171f73967e8);
|
||||
ASSERT_EQ(static_cast<hash_type>(hs), 0xbf9cf968);
|
||||
ASSERT_EQ(hs.value(), 0xbf9cf968);
|
||||
|
||||
ASSERT_EQ(fooHs, "foo"_hs);
|
||||
ASSERT_NE(barHs, "foo"_hs);
|
||||
ASSERT_EQ(foo_hs, "foo"_hs);
|
||||
ASSERT_NE(bar_hs, "foo"_hs);
|
||||
}
|
||||
|
||||
TEST(HashedString, Empty) {
|
||||
using hash_type = entt::hashed_string::hash_type;
|
||||
|
||||
entt::hashed_string hs{};
|
||||
|
||||
ASSERT_EQ(static_cast<hash_type>(hs), hash_type{});
|
||||
ASSERT_EQ(static_cast<const char *>(hs), nullptr);
|
||||
}
|
||||
|
||||
TEST(HashedString, Constexprness) {
|
||||
using hash_type = entt::HashedString::hash_type;
|
||||
using hash_type = entt::hashed_string::hash_type;
|
||||
// how would you test a constexpr otherwise?
|
||||
(void)std::integral_constant<hash_type, entt::HashedString{"quux"}>{};
|
||||
(void)std::integral_constant<hash_type, entt::hashed_string{"quux"}>{};
|
||||
(void)std::integral_constant<hash_type, "quux"_hs>{};
|
||||
ASSERT_TRUE(true);
|
||||
}
|
||||
|
||||
TEST(HashedString, ToValue) {
|
||||
using hash_type = entt::hashed_string::hash_type;
|
||||
|
||||
const char *foobar = "foobar";
|
||||
|
||||
ASSERT_EQ(entt::hashed_string::to_value(foobar), 0xbf9cf968);
|
||||
// how would you test a constexpr otherwise?
|
||||
(void)std::integral_constant<hash_type, entt::hashed_string::to_value("quux")>{};
|
||||
}
|
||||
|
||||
TEST(HashedString, StringView) {
|
||||
std::string str{"__foobar__"};
|
||||
std::string_view view{str.data()+2, 6};
|
||||
ASSERT_EQ(entt::hashed_string::to_value(view.data(), view.size()), 0xbf9cf968);
|
||||
}
|
||||
|
||||
@@ -2,31 +2,31 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/ident.hpp>
|
||||
|
||||
struct AType {};
|
||||
struct AnotherType {};
|
||||
struct a_type {};
|
||||
struct another_type {};
|
||||
|
||||
TEST(Identifier, Uniqueness) {
|
||||
using ID = entt::Identifier<AType, AnotherType>;
|
||||
constexpr AType anInstance;
|
||||
constexpr AnotherType anotherInstance;
|
||||
using id = entt::identifier<a_type, another_type>;
|
||||
constexpr a_type an_instance;
|
||||
constexpr another_type another_instance;
|
||||
|
||||
ASSERT_NE(ID::get<AType>(), ID::get<AnotherType>());
|
||||
ASSERT_EQ(ID::get<AType>(), ID::get<decltype(anInstance)>());
|
||||
ASSERT_NE(ID::get<AType>(), ID::get<decltype(anotherInstance)>());
|
||||
ASSERT_EQ(ID::get<AType>(), ID::get<AType>());
|
||||
ASSERT_EQ(ID::get<AnotherType>(), ID::get<AnotherType>());
|
||||
ASSERT_NE(id::type<a_type>, id::type<another_type>);
|
||||
ASSERT_EQ(id::type<a_type>, id::type<decltype(an_instance)>);
|
||||
ASSERT_NE(id::type<a_type>, id::type<decltype(another_instance)>);
|
||||
ASSERT_EQ(id::type<a_type>, id::type<a_type>);
|
||||
ASSERT_EQ(id::type<another_type>, id::type<another_type>);
|
||||
|
||||
// test uses in constant expressions
|
||||
switch(ID::get<AnotherType>()) {
|
||||
case ID::get<AType>():
|
||||
switch(id::type<another_type>) {
|
||||
case id::type<a_type>:
|
||||
FAIL();
|
||||
case ID::get<AnotherType>():
|
||||
case id::type<another_type>:
|
||||
SUCCEED();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Identifier, SingleType) {
|
||||
using ID = entt::Identifier<AType>;
|
||||
std::integral_constant<ID::identifier_type, ID::get<AType>()> ic;
|
||||
using id = entt::identifier<a_type>;
|
||||
std::integral_constant<id::identifier_type, id::type<a_type>> ic;
|
||||
(void)ic;
|
||||
}
|
||||
|
||||
@@ -3,18 +3,18 @@
|
||||
#include <entt/core/monostate.hpp>
|
||||
|
||||
TEST(Monostate, Functionalities) {
|
||||
const bool bPre = entt::Monostate<entt::HashedString{"foobar"}>{};
|
||||
const int iPre = entt::Monostate<"foobar"_hs>{};
|
||||
const bool b_pre = entt::monostate<entt::hashed_string{"foobar"}>{};
|
||||
const int i_pre = entt::monostate<"foobar"_hs>{};
|
||||
|
||||
ASSERT_FALSE(bPre);
|
||||
ASSERT_EQ(iPre, int{});
|
||||
ASSERT_FALSE(b_pre);
|
||||
ASSERT_EQ(i_pre, int{});
|
||||
|
||||
entt::Monostate<"foobar"_hs>{} = true;
|
||||
entt::Monostate<"foobar"_hs>{} = 42;
|
||||
entt::monostate<"foobar"_hs>{} = true;
|
||||
entt::monostate_v<"foobar"_hs> = 42;
|
||||
|
||||
const bool &bPost = entt::Monostate<"foobar"_hs>{};
|
||||
const int &iPost = entt::Monostate<entt::HashedString{"foobar"}>{};
|
||||
const bool &b_post = entt::monostate<"foobar"_hs>{};
|
||||
const int &i_post = entt::monostate_v<entt::hashed_string{"foobar"}>;
|
||||
|
||||
ASSERT_TRUE(bPost);
|
||||
ASSERT_EQ(iPost, 42);
|
||||
ASSERT_TRUE(b_post);
|
||||
ASSERT_EQ(i_post, 42);
|
||||
}
|
||||
|
||||
14
test/entt/core/type_traits.cpp
Normal file
14
test/entt/core/type_traits.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/type_traits.hpp>
|
||||
|
||||
TEST(TypeList, Functionalities) {
|
||||
using type = entt::type_list<int, char>;
|
||||
using other = entt::type_list<double>;
|
||||
|
||||
ASSERT_EQ((type::size), (decltype(type::size){2}));
|
||||
ASSERT_EQ((other::size), (decltype(other::size){1}));
|
||||
ASSERT_TRUE((std::is_same_v<entt::type_list_cat_t<type, other, type, other>, entt::type_list<int, char, double, int, char, double>>));
|
||||
ASSERT_TRUE((std::is_same_v<entt::type_list_cat_t<type, other>, entt::type_list<int, char, double>>));
|
||||
ASSERT_TRUE((std::is_same_v<entt::type_list_cat_t<type, type>, entt::type_list<int, char, int, char>>));
|
||||
ASSERT_TRUE((std::is_same_v<entt::type_list_unique_t<entt::type_list_cat_t<type, type>>, entt::type_list<int, char>>));
|
||||
}
|
||||
19
test/entt/core/utility.cpp
Normal file
19
test/entt/core/utility.cpp
Normal file
@@ -0,0 +1,19 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/utility.hpp>
|
||||
|
||||
struct Functions {
|
||||
static void foo(int) {}
|
||||
static void foo() {}
|
||||
|
||||
void bar(int) {}
|
||||
void bar() {}
|
||||
};
|
||||
|
||||
|
||||
TEST(Utility, Overload) {
|
||||
ASSERT_EQ(entt::overload<void(int)>(&Functions::foo), static_cast<void(*)(int)>(&Functions::foo));
|
||||
ASSERT_EQ(entt::overload<void()>(&Functions::foo), static_cast<void(*)()>(&Functions::foo));
|
||||
|
||||
ASSERT_EQ(entt::overload<void(int)>(&Functions::bar), static_cast<void(Functions:: *)(int)>(&Functions::bar));
|
||||
ASSERT_EQ(entt::overload<void()>(&Functions::bar), static_cast<void(Functions:: *)()>(&Functions::bar));
|
||||
}
|
||||
@@ -3,66 +3,49 @@
|
||||
#include <entt/entity/actor.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
struct ActorComponent final {};
|
||||
struct ActorTag final {};
|
||||
|
||||
TEST(Actor, Component) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultActor actor{registry};
|
||||
const auto &cactor = actor;
|
||||
entt::registry registry;
|
||||
entt::actor actor{registry};
|
||||
|
||||
ASSERT_EQ(®istry, &actor.registry());
|
||||
ASSERT_EQ(®istry, &cactor.registry());
|
||||
ASSERT_TRUE(registry.empty<ActorComponent>());
|
||||
ASSERT_EQ(®istry, &actor.backend());
|
||||
ASSERT_EQ(®istry, &std::as_const(actor).backend());
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE(actor.has<ActorComponent>());
|
||||
ASSERT_FALSE(actor.has<int>());
|
||||
|
||||
const auto &component = actor.assign<ActorComponent>();
|
||||
const auto &cint = actor.assign<int>();
|
||||
const auto &cchar = actor.assign<char>();
|
||||
|
||||
ASSERT_EQ(&component, &actor.get<ActorComponent>());
|
||||
ASSERT_EQ(&component, &cactor.get<ActorComponent>());
|
||||
ASSERT_FALSE(registry.empty<ActorComponent>());
|
||||
ASSERT_EQ(&cint, &actor.get<int>());
|
||||
ASSERT_EQ(&cchar, &std::as_const(actor).get<char>());
|
||||
ASSERT_EQ(&cint, &std::get<0>(actor.get<int, char>()));
|
||||
ASSERT_EQ(&cchar, &std::get<1>(actor.get<int, char>()));
|
||||
ASSERT_EQ(&cint, std::get<0>(actor.try_get<int, char, double>()));
|
||||
ASSERT_EQ(&cchar, std::get<1>(actor.try_get<int, char, double>()));
|
||||
ASSERT_EQ(nullptr, std::get<2>(actor.try_get<int, char, double>()));
|
||||
ASSERT_EQ(nullptr, actor.try_get<double>());
|
||||
ASSERT_EQ(&cchar, actor.try_get<char>());
|
||||
ASSERT_EQ(&cint, actor.try_get<int>());
|
||||
|
||||
ASSERT_FALSE(registry.empty<int>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_TRUE(actor.has<ActorComponent>());
|
||||
ASSERT_TRUE(actor.has<int>());
|
||||
ASSERT_TRUE(actor.has<char>());
|
||||
ASSERT_FALSE(actor.has<double>());
|
||||
|
||||
actor.remove<ActorComponent>();
|
||||
actor.remove<int>();
|
||||
|
||||
ASSERT_TRUE(registry.empty<ActorComponent>());
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE(actor.has<ActorComponent>());
|
||||
}
|
||||
|
||||
TEST(Actor, Tag) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultActor actor{registry};
|
||||
const auto &cactor = actor;
|
||||
|
||||
ASSERT_EQ(®istry, &actor.registry());
|
||||
ASSERT_EQ(®istry, &cactor.registry());
|
||||
ASSERT_FALSE(registry.has<ActorTag>());
|
||||
ASSERT_FALSE(actor.has<ActorTag>(entt::tag_t{}));
|
||||
|
||||
const auto &tag = actor.assign<ActorTag>(entt::tag_t{});
|
||||
|
||||
ASSERT_EQ(&tag, &actor.get<ActorTag>(entt::tag_t{}));
|
||||
ASSERT_EQ(&tag, &cactor.get<ActorTag>(entt::tag_t{}));
|
||||
ASSERT_TRUE(registry.has<ActorTag>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_TRUE(actor.has<ActorTag>(entt::tag_t{}));
|
||||
|
||||
actor.remove<ActorTag>(entt::tag_t{});
|
||||
|
||||
ASSERT_FALSE(registry.has<ActorTag>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE(actor.has<ActorTag>(entt::tag_t{}));
|
||||
ASSERT_FALSE(actor.has<int>());
|
||||
}
|
||||
|
||||
TEST(Actor, EntityLifetime) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto *actor = new entt::DefaultActor{registry};
|
||||
actor->assign<ActorComponent>();
|
||||
entt::registry registry;
|
||||
auto *actor = new entt::actor{registry};
|
||||
actor->assign<int>();
|
||||
|
||||
ASSERT_FALSE(registry.empty<ActorComponent>());
|
||||
ASSERT_FALSE(registry.empty<int>());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
|
||||
registry.each([actor](const auto entity) {
|
||||
@@ -71,6 +54,6 @@ TEST(Actor, EntityLifetime) {
|
||||
|
||||
delete actor;
|
||||
|
||||
ASSERT_TRUE(registry.empty<ActorComponent>());
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
ASSERT_TRUE(registry.empty());
|
||||
}
|
||||
|
||||
@@ -1,69 +0,0 @@
|
||||
#include <unordered_set>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/attachee.hpp>
|
||||
|
||||
TEST(AttacheeNoType, Functionalities) {
|
||||
entt::Attachee<std::uint64_t> attachee;
|
||||
|
||||
attachee.construct(42u);
|
||||
|
||||
ASSERT_EQ(attachee.get(), 42u);
|
||||
|
||||
attachee.destroy();
|
||||
|
||||
ASSERT_NE(attachee.get(), 42u);
|
||||
|
||||
(void)entt::Attachee<std::uint64_t>{std::move(attachee)};
|
||||
entt::Attachee<std::uint64_t> other;
|
||||
other = std::move(attachee);
|
||||
}
|
||||
|
||||
TEST(AttacheeWithType, Functionalities) {
|
||||
entt::Attachee<std::uint64_t, int> attachee;
|
||||
const auto &cattachee = attachee;
|
||||
|
||||
attachee.construct(42u, 3);
|
||||
|
||||
ASSERT_EQ(attachee.get(), 3);
|
||||
ASSERT_EQ(cattachee.get(), 3);
|
||||
ASSERT_EQ(attachee.Attachee<std::uint64_t>::get(), 42u);
|
||||
|
||||
attachee.move(0u);
|
||||
|
||||
ASSERT_EQ(attachee.get(), 3);
|
||||
ASSERT_EQ(cattachee.get(), 3);
|
||||
ASSERT_EQ(attachee.Attachee<std::uint64_t>::get(), 0u);
|
||||
|
||||
attachee.destroy();
|
||||
|
||||
ASSERT_NE(attachee.Attachee<std::uint64_t>::get(), 0u);
|
||||
ASSERT_NE(attachee.Attachee<std::uint64_t>::get(), 42u);
|
||||
}
|
||||
|
||||
TEST(AttacheeWithType, AggregatesMustWork) {
|
||||
struct AggregateType { int value; };
|
||||
// the goal of this test is to enforce the requirements for aggregate types
|
||||
entt::Attachee<std::uint64_t, AggregateType>{}.construct(0, 42);
|
||||
}
|
||||
|
||||
TEST(AttacheeWithType, TypesFromStandardTemplateLibraryMustWork) {
|
||||
// see #37 - this test shouldn't crash, that's all
|
||||
entt::Attachee<std::uint64_t, std::unordered_set<int>> attachee;
|
||||
attachee.construct(0).insert(42);
|
||||
attachee.destroy();
|
||||
}
|
||||
|
||||
TEST(AttacheeWithType, MoveOnlyComponent) {
|
||||
struct MoveOnlyComponent {
|
||||
MoveOnlyComponent() = default;
|
||||
~MoveOnlyComponent() = default;
|
||||
MoveOnlyComponent(const MoveOnlyComponent &) = delete;
|
||||
MoveOnlyComponent(MoveOnlyComponent &&) = default;
|
||||
MoveOnlyComponent & operator=(const MoveOnlyComponent &) = delete;
|
||||
MoveOnlyComponent & operator=(MoveOnlyComponent &&) = default;
|
||||
};
|
||||
|
||||
// it's purpose is to ensure that move only components are always accepted
|
||||
entt::Attachee<std::uint64_t, MoveOnlyComponent> attachee;
|
||||
(void)attachee;
|
||||
}
|
||||
@@ -3,16 +3,13 @@
|
||||
#include <entt/entity/entity.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
template<bool>
|
||||
struct S {};
|
||||
|
||||
TEST(Traits, Null) {
|
||||
entt::DefaultRegistry registry{};
|
||||
entt::registry registry{};
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity, 42);
|
||||
|
||||
ASSERT_TRUE(~typename entt::DefaultRegistry::entity_type{} == entt::null);
|
||||
ASSERT_TRUE(~entt::entity{} == entt::null);
|
||||
|
||||
ASSERT_TRUE(entt::null == entt::null);
|
||||
ASSERT_FALSE(entt::null != entt::null);
|
||||
|
||||
892
test/entt/entity/group.cpp
Normal file
892
test/entt/entity/group.cpp
Normal file
@@ -0,0 +1,892 @@
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <algorithm>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/group.hpp>
|
||||
|
||||
struct empty_type {};
|
||||
struct boxed_int { int value; };
|
||||
|
||||
TEST(NonOwningGroup, Functionalities) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, char>);
|
||||
auto cgroup = std::as_const(registry).group<>(entt::get<const int, const char>);
|
||||
|
||||
ASSERT_TRUE(group.empty());
|
||||
ASSERT_TRUE(group.empty<int>());
|
||||
ASSERT_TRUE(cgroup.empty<const char>());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
ASSERT_FALSE(group.empty());
|
||||
ASSERT_FALSE(group.empty<int>());
|
||||
ASSERT_FALSE(cgroup.empty<const char>());
|
||||
ASSERT_NO_THROW((group.begin()++));
|
||||
ASSERT_NO_THROW((++cgroup.begin()));
|
||||
|
||||
ASSERT_NE(group.begin(), group.end());
|
||||
ASSERT_NE(cgroup.begin(), cgroup.end());
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(group.size<int>(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(cgroup.size<const char>(), typename decltype(group)::size_type{2});
|
||||
|
||||
registry.assign<int>(e0);
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{2});
|
||||
ASSERT_EQ(group.size<int>(), typename decltype(group)::size_type{2});
|
||||
ASSERT_EQ(cgroup.size<const char>(), typename decltype(group)::size_type{2});
|
||||
|
||||
registry.remove<int>(e0);
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(group.size<int>(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(cgroup.size<const char>(), typename decltype(group)::size_type{2});
|
||||
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
|
||||
for(auto entity: group) {
|
||||
ASSERT_EQ(std::get<0>(cgroup.get<const int, const char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<1>(group.get<int, char>(entity)), '2');
|
||||
ASSERT_EQ(cgroup.get<const char>(entity), '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(group.data() + 0), e1);
|
||||
|
||||
ASSERT_EQ(*(group.data<int>() + 0), e1);
|
||||
ASSERT_EQ(*(group.data<char>() + 0), e0);
|
||||
ASSERT_EQ(*(cgroup.data<const char>() + 1), e1);
|
||||
|
||||
ASSERT_EQ(*(group.raw<int>() + 0), 42);
|
||||
ASSERT_EQ(*(group.raw<char>() + 0), '1');
|
||||
ASSERT_EQ(*(cgroup.raw<const char>() + 1), '2');
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
|
||||
ASSERT_EQ(group.begin(), group.end());
|
||||
ASSERT_EQ(cgroup.begin(), cgroup.end());
|
||||
ASSERT_TRUE(group.empty());
|
||||
|
||||
ASSERT_TRUE(group.capacity());
|
||||
|
||||
group.shrink_to_fit();
|
||||
|
||||
ASSERT_FALSE(group.capacity());
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, ElementAccess) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, char>);
|
||||
auto cgroup = std::as_const(registry).group<>(entt::get<const int, const char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
for(typename decltype(group)::size_type i{}; i < group.size(); ++i) {
|
||||
ASSERT_EQ(group[i], i ? e0 : e1);
|
||||
ASSERT_EQ(cgroup[i], i ? e0 : e1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, Contains) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
ASSERT_FALSE(group.contains(e0));
|
||||
ASSERT_TRUE(group.contains(e1));
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, Empty) {
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<double>(e0);
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<float>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<char>(e1);
|
||||
registry.assign<float>(e1);
|
||||
|
||||
for(auto entity: registry.group<>(entt::get<char, int, float>)) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
|
||||
for(auto entity: registry.group<>(entt::get<double, char, int, float>)) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, Each) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto cgroup = std::as_const(registry).group<>(entt::get<const int, const char>);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
group.each([&cnt](auto, int &, char &) { ++cnt; });
|
||||
group.each([&cnt](int &, char &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{4});
|
||||
|
||||
cgroup.each([&cnt](auto, const int &, const char &) { --cnt; });
|
||||
cgroup.each([&cnt](const int &, const char &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, Sort) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<const int, unsigned int>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
const auto e2 = 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<int>(e0, ival++);
|
||||
registry.assign<int>(e1, ival++);
|
||||
registry.assign<int>(e2, ival++);
|
||||
|
||||
for(auto entity: group) {
|
||||
ASSERT_EQ(group.get<unsigned int>(entity), --uval);
|
||||
ASSERT_EQ(group.get<const int>(entity), --ival);
|
||||
}
|
||||
|
||||
registry.sort<unsigned int>(std::less<unsigned int>{});
|
||||
group.sort<unsigned int>();
|
||||
|
||||
for(auto entity: group) {
|
||||
ASSERT_EQ(group.get<unsigned int>(entity), uval++);
|
||||
ASSERT_EQ(group.get<const int>(entity), ival++);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, IndexRebuiltOnDestroy) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, unsigned int>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
|
||||
registry.assign<unsigned int>(e0, 0u);
|
||||
registry.assign<unsigned int>(e1, 1u);
|
||||
|
||||
registry.assign<int>(e0, 0);
|
||||
registry.assign<int>(e1, 1);
|
||||
|
||||
registry.destroy(e0);
|
||||
registry.assign<int>(registry.create(), 42);
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(group[{}], e1);
|
||||
ASSERT_EQ(group.get<int>(e1), 1);
|
||||
ASSERT_EQ(group.get<unsigned int>(e1), 1u);
|
||||
|
||||
group.each([e1](auto entity, auto ivalue, auto uivalue) {
|
||||
ASSERT_EQ(entity, e1);
|
||||
ASSERT_EQ(ivalue, 1);
|
||||
ASSERT_EQ(uivalue, 1u);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, ConstNonConstAndAllInBetween) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, const char>);
|
||||
|
||||
ASSERT_EQ(group.size(), decltype(group.size()){0});
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity, 0);
|
||||
registry.assign<char>(entity, 'c');
|
||||
|
||||
ASSERT_EQ(group.size(), decltype(group.size()){1});
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<int>(0)), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<const char>(0)), const char &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<int, const char>(0)), std::tuple<int &, const char &>>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.raw<const char>()), const char *>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.raw<int>()), int *>));
|
||||
|
||||
group.each([](auto, auto &&i, auto &&c) {
|
||||
ASSERT_TRUE((std::is_same_v<decltype(i), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(c), const char &>));
|
||||
});
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, Find) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<>(entt::get<int, const char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
const auto e2 = registry.create();
|
||||
registry.assign<int>(e2);
|
||||
registry.assign<char>(e2);
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<int>(e3);
|
||||
registry.assign<char>(e3);
|
||||
|
||||
registry.remove<int>(e1);
|
||||
|
||||
ASSERT_NE(group.find(e0), group.end());
|
||||
ASSERT_EQ(group.find(e1), group.end());
|
||||
ASSERT_NE(group.find(e2), group.end());
|
||||
ASSERT_NE(group.find(e3), group.end());
|
||||
|
||||
auto it = group.find(e2);
|
||||
|
||||
ASSERT_EQ(*it, e2);
|
||||
ASSERT_EQ(*(++it), e3);
|
||||
ASSERT_EQ(*(++it), e0);
|
||||
ASSERT_EQ(++it, group.end());
|
||||
ASSERT_EQ(++group.find(e0), group.end());
|
||||
|
||||
const auto e4 = registry.create();
|
||||
registry.destroy(e4);
|
||||
const auto e5 = registry.create();
|
||||
registry.assign<int>(e5);
|
||||
registry.assign<char>(e5);
|
||||
|
||||
ASSERT_NE(group.find(e5), group.end());
|
||||
ASSERT_EQ(group.find(e4), group.end());
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, ExcludedComponents) {
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1, 1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
const auto group = registry.group<>(entt::get<int>, entt::exclude<char>);
|
||||
|
||||
const auto e2 = registry.create();
|
||||
registry.assign<int>(e2, 2);
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<int>(e3, 3);
|
||||
registry.assign<char>(e3);
|
||||
|
||||
for(const auto entity: group) {
|
||||
if(entity == e0) {
|
||||
ASSERT_EQ(group.get<int>(e0), 0);
|
||||
} else if(entity == e2) {
|
||||
ASSERT_EQ(group.get<int>(e2), 2);
|
||||
} else {
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
registry.assign<char>(e0);
|
||||
registry.assign<char>(e2);
|
||||
|
||||
ASSERT_TRUE(group.empty());
|
||||
|
||||
registry.remove<char>(e1);
|
||||
registry.remove<char>(e3);
|
||||
|
||||
for(const auto entity: group) {
|
||||
if(entity == e1) {
|
||||
ASSERT_EQ(group.get<int>(e1), 1);
|
||||
} else if(entity == e3) {
|
||||
ASSERT_EQ(group.get<int>(e3), 3);
|
||||
} else {
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, EmptyAndNonEmptyTypes) {
|
||||
entt::registry registry;
|
||||
const auto group = registry.group<>(entt::get<int, empty_type>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<empty_type>(e0);
|
||||
registry.assign<int>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<empty_type>(e1);
|
||||
registry.assign<int>(e1);
|
||||
|
||||
registry.assign<int>(registry.create());
|
||||
|
||||
for(const auto entity: group) {
|
||||
ASSERT_TRUE(entity == e0 || entity == e1);
|
||||
}
|
||||
|
||||
group.each([e0, e1](const auto entity, const int &, empty_type) {
|
||||
ASSERT_TRUE(entity == e0 || entity == e1);
|
||||
});
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{2});
|
||||
}
|
||||
|
||||
TEST(NonOwningGroup, TrackEntitiesOnComponentDestruction) {
|
||||
entt::registry registry;
|
||||
const auto group = registry.group<>(entt::get<int>, entt::exclude<char>);
|
||||
const auto cgroup = std::as_const(registry).group<>(entt::get<const int>, entt::exclude<char>);
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
|
||||
ASSERT_TRUE(group.empty());
|
||||
ASSERT_TRUE(cgroup.empty());
|
||||
|
||||
registry.remove<char>(entity);
|
||||
|
||||
ASSERT_FALSE(group.empty());
|
||||
ASSERT_FALSE(cgroup.empty());
|
||||
}
|
||||
|
||||
TEST(OwningGroup, Functionalities) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int>(entt::get<char>);
|
||||
auto cgroup = std::as_const(registry).group<const int>(entt::get<const char>);
|
||||
|
||||
ASSERT_TRUE(group.empty());
|
||||
ASSERT_TRUE(group.empty<int>());
|
||||
ASSERT_TRUE(cgroup.empty<const char>());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
ASSERT_FALSE(group.empty());
|
||||
ASSERT_FALSE(group.empty<int>());
|
||||
ASSERT_FALSE(cgroup.empty<const char>());
|
||||
ASSERT_NO_THROW((group.begin()++));
|
||||
ASSERT_NO_THROW((++cgroup.begin()));
|
||||
|
||||
ASSERT_NE(group.begin(), group.end());
|
||||
ASSERT_NE(cgroup.begin(), cgroup.end());
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(group.size<int>(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(cgroup.size<const char>(), typename decltype(group)::size_type{2});
|
||||
|
||||
registry.assign<int>(e0);
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{2});
|
||||
ASSERT_EQ(group.size<int>(), typename decltype(group)::size_type{2});
|
||||
ASSERT_EQ(cgroup.size<const char>(), typename decltype(group)::size_type{2});
|
||||
|
||||
registry.remove<int>(e0);
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(group.size<int>(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(cgroup.size<const char>(), typename decltype(group)::size_type{2});
|
||||
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
|
||||
ASSERT_EQ(*(cgroup.raw<const int>() + 0), 42);
|
||||
ASSERT_EQ(*(group.raw<int>() + 0), 42);
|
||||
|
||||
for(auto entity: group) {
|
||||
ASSERT_EQ(std::get<0>(cgroup.get<const int, const char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<1>(group.get<int, char>(entity)), '2');
|
||||
ASSERT_EQ(cgroup.get<const char>(entity), '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(group.data() + 0), e1);
|
||||
|
||||
ASSERT_EQ(*(group.data<int>() + 0), e1);
|
||||
ASSERT_EQ(*(group.data<char>() + 0), e0);
|
||||
ASSERT_EQ(*(cgroup.data<const char>() + 1), e1);
|
||||
|
||||
ASSERT_EQ(*(group.raw<int>() + 0), 42);
|
||||
ASSERT_EQ(*(group.raw<char>() + 0), '1');
|
||||
ASSERT_EQ(*(cgroup.raw<const char>() + 1), '2');
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
|
||||
ASSERT_EQ(group.begin(), group.end());
|
||||
ASSERT_EQ(cgroup.begin(), cgroup.end());
|
||||
ASSERT_TRUE(group.empty());
|
||||
}
|
||||
|
||||
TEST(OwningGroup, ElementAccess) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int>(entt::get<char>);
|
||||
auto cgroup = std::as_const(registry).group<const int>(entt::get<const char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
for(typename decltype(group)::size_type i{}; i < group.size(); ++i) {
|
||||
ASSERT_EQ(group[i], i ? e0 : e1);
|
||||
ASSERT_EQ(cgroup[i], i ? e0 : e1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(OwningGroup, Contains) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int>(entt::get<char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
ASSERT_FALSE(group.contains(e0));
|
||||
ASSERT_TRUE(group.contains(e1));
|
||||
}
|
||||
|
||||
TEST(OwningGroup, Empty) {
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<double>(e0);
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<float>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<char>(e1);
|
||||
registry.assign<float>(e1);
|
||||
|
||||
for(auto entity: registry.group<char, int>(entt::get<float>)) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
|
||||
for(auto entity: registry.group<double, float>(entt::get<char, int>)) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(OwningGroup, Each) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int>(entt::get<char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto cgroup = std::as_const(registry).group<const int>(entt::get<const char>);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
group.each([&cnt](auto, int &, char &) { ++cnt; });
|
||||
group.each([&cnt](int &, char &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{4});
|
||||
|
||||
cgroup.each([&cnt](auto, const int &, const char &) { --cnt; });
|
||||
cgroup.each([&cnt](const int &, const char &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(OwningGroup, SortOrdered) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<boxed_int, char>();
|
||||
|
||||
entt::entity entities[5] = {
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create()
|
||||
};
|
||||
|
||||
registry.assign<boxed_int>(entities[0], 12);
|
||||
registry.assign<char>(entities[0], 'a');
|
||||
|
||||
registry.assign<boxed_int>(entities[1], 9);
|
||||
registry.assign<char>(entities[1], 'b');
|
||||
|
||||
registry.assign<boxed_int>(entities[2], 6);
|
||||
registry.assign<char>(entities[2], 'c');
|
||||
|
||||
registry.assign<boxed_int>(entities[3], 1);
|
||||
registry.assign<boxed_int>(entities[4], 2);
|
||||
|
||||
group.sort([&group](const auto lhs, const auto rhs) {
|
||||
return group.get<boxed_int>(lhs).value < group.get<boxed_int>(rhs).value;
|
||||
});
|
||||
|
||||
ASSERT_EQ(*(group.data() + 0u), entities[0]);
|
||||
ASSERT_EQ(*(group.data() + 1u), entities[1]);
|
||||
ASSERT_EQ(*(group.data() + 2u), entities[2]);
|
||||
ASSERT_EQ(*(group.data() + 3u), entities[3]);
|
||||
ASSERT_EQ(*(group.data() + 4u), entities[4]);
|
||||
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 0u)->value, 12);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 1u)->value, 9);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 2u)->value, 6);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 3u)->value, 1);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 4u)->value, 2);
|
||||
|
||||
ASSERT_EQ(*(group.raw<char>() + 0u), 'a');
|
||||
ASSERT_EQ(*(group.raw<char>() + 1u), 'b');
|
||||
ASSERT_EQ(*(group.raw<char>() + 2u), 'c');
|
||||
}
|
||||
|
||||
TEST(OwningGroup, SortReverse) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<boxed_int, char>();
|
||||
|
||||
entt::entity entities[5] = {
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create()
|
||||
};
|
||||
|
||||
registry.assign<boxed_int>(entities[0], 6);
|
||||
registry.assign<char>(entities[0], 'a');
|
||||
|
||||
registry.assign<boxed_int>(entities[1], 9);
|
||||
registry.assign<char>(entities[1], 'b');
|
||||
|
||||
registry.assign<boxed_int>(entities[2], 12);
|
||||
registry.assign<char>(entities[2], 'c');
|
||||
|
||||
registry.assign<boxed_int>(entities[3], 1);
|
||||
registry.assign<boxed_int>(entities[4], 2);
|
||||
|
||||
group.sort<boxed_int>([](const auto &lhs, const auto &rhs) {
|
||||
return lhs.value < rhs.value;
|
||||
});
|
||||
|
||||
ASSERT_EQ(*(group.data() + 0u), entities[2]);
|
||||
ASSERT_EQ(*(group.data() + 1u), entities[1]);
|
||||
ASSERT_EQ(*(group.data() + 2u), entities[0]);
|
||||
ASSERT_EQ(*(group.data() + 3u), entities[3]);
|
||||
ASSERT_EQ(*(group.data() + 4u), entities[4]);
|
||||
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 0u)->value, 12);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 1u)->value, 9);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 2u)->value, 6);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 3u)->value, 1);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 4u)->value, 2);
|
||||
|
||||
ASSERT_EQ(*(group.raw<char>() + 0u), 'c');
|
||||
ASSERT_EQ(*(group.raw<char>() + 1u), 'b');
|
||||
ASSERT_EQ(*(group.raw<char>() + 2u), 'a');
|
||||
}
|
||||
|
||||
TEST(OwningGroup, SortUnordered) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<boxed_int>(entt::get<char>);
|
||||
|
||||
entt::entity entities[7] = {
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create(),
|
||||
registry.create()
|
||||
};
|
||||
|
||||
registry.assign<boxed_int>(entities[0], 6);
|
||||
registry.assign<char>(entities[0], 'c');
|
||||
|
||||
registry.assign<boxed_int>(entities[1], 3);
|
||||
registry.assign<char>(entities[1], 'b');
|
||||
|
||||
registry.assign<boxed_int>(entities[2], 1);
|
||||
registry.assign<char>(entities[2], 'a');
|
||||
|
||||
registry.assign<boxed_int>(entities[3], 9);
|
||||
registry.assign<char>(entities[3], 'd');
|
||||
|
||||
registry.assign<boxed_int>(entities[4], 12);
|
||||
registry.assign<char>(entities[4], 'e');
|
||||
|
||||
registry.assign<boxed_int>(entities[5], 4);
|
||||
registry.assign<boxed_int>(entities[6], 5);
|
||||
|
||||
group.sort<char>([](const auto lhs, const auto rhs) {
|
||||
return lhs < rhs;
|
||||
});
|
||||
|
||||
ASSERT_EQ(*(group.data() + 0u), entities[4]);
|
||||
ASSERT_EQ(*(group.data() + 1u), entities[3]);
|
||||
ASSERT_EQ(*(group.data() + 2u), entities[0]);
|
||||
ASSERT_EQ(*(group.data() + 3u), entities[1]);
|
||||
ASSERT_EQ(*(group.data() + 4u), entities[2]);
|
||||
ASSERT_EQ(*(group.data() + 5u), entities[5]);
|
||||
ASSERT_EQ(*(group.data() + 6u), entities[6]);
|
||||
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 0u)->value, 12);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 1u)->value, 9);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 2u)->value, 6);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 3u)->value, 3);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 4u)->value, 1);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 5u)->value, 4);
|
||||
ASSERT_EQ((group.raw<boxed_int>() + 6u)->value, 5);
|
||||
|
||||
ASSERT_EQ(*(group.raw<char>() + 0u), 'c');
|
||||
ASSERT_EQ(*(group.raw<char>() + 1u), 'b');
|
||||
ASSERT_EQ(*(group.raw<char>() + 2u), 'a');
|
||||
ASSERT_EQ(*(group.raw<char>() + 3u), 'd');
|
||||
ASSERT_EQ(*(group.raw<char>() + 4u), 'e');
|
||||
}
|
||||
|
||||
TEST(OwningGroup, IndexRebuiltOnDestroy) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int>(entt::get<unsigned int>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
|
||||
registry.assign<unsigned int>(e0, 0u);
|
||||
registry.assign<unsigned int>(e1, 1u);
|
||||
|
||||
registry.assign<int>(e0, 0);
|
||||
registry.assign<int>(e1, 1);
|
||||
|
||||
registry.destroy(e0);
|
||||
registry.assign<int>(registry.create(), 42);
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{1});
|
||||
ASSERT_EQ(group[{}], e1);
|
||||
ASSERT_EQ(group.get<int>(e1), 1);
|
||||
ASSERT_EQ(group.get<unsigned int>(e1), 1u);
|
||||
|
||||
group.each([e1](auto entity, auto ivalue, auto uivalue) {
|
||||
ASSERT_EQ(entity, e1);
|
||||
ASSERT_EQ(ivalue, 1);
|
||||
ASSERT_EQ(uivalue, 1u);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OwningGroup, ConstNonConstAndAllInBetween) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int, const char>(entt::get<double, const float>);
|
||||
|
||||
ASSERT_EQ(group.size(), decltype(group.size()){0});
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity, 0);
|
||||
registry.assign<char>(entity, 'c');
|
||||
registry.assign<double>(entity, 0.);
|
||||
registry.assign<float>(entity, 0.f);
|
||||
|
||||
ASSERT_EQ(group.size(), decltype(group.size()){1});
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<int>(0)), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<const char>(0)), const char &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<double>(0)), double &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<const float>(0)), const float &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.get<int, const char, double, const float>(0)), std::tuple<int &, const char &, double &, const float &>>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.raw<const float>()), const float *>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.raw<double>()), double *>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.raw<const char>()), const char *>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(group.raw<int>()), int *>));
|
||||
|
||||
group.each([](auto, auto &&i, auto &&c, auto &&d, auto &&f) {
|
||||
ASSERT_TRUE((std::is_same_v<decltype(i), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(c), const char &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(d), double &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(f), const float &>));
|
||||
});
|
||||
}
|
||||
|
||||
TEST(OwningGroup, Find) {
|
||||
entt::registry registry;
|
||||
auto group = registry.group<int>(entt::get<const char>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
const auto e2 = registry.create();
|
||||
registry.assign<int>(e2);
|
||||
registry.assign<char>(e2);
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<int>(e3);
|
||||
registry.assign<char>(e3);
|
||||
|
||||
registry.remove<int>(e1);
|
||||
|
||||
ASSERT_NE(group.find(e0), group.end());
|
||||
ASSERT_EQ(group.find(e1), group.end());
|
||||
ASSERT_NE(group.find(e2), group.end());
|
||||
ASSERT_NE(group.find(e3), group.end());
|
||||
|
||||
auto it = group.find(e2);
|
||||
|
||||
ASSERT_EQ(*it, e2);
|
||||
ASSERT_EQ(*(++it), e3);
|
||||
ASSERT_EQ(*(++it), e0);
|
||||
ASSERT_EQ(++it, group.end());
|
||||
ASSERT_EQ(++group.find(e0), group.end());
|
||||
|
||||
const auto e4 = registry.create();
|
||||
registry.destroy(e4);
|
||||
const auto e5 = registry.create();
|
||||
registry.assign<int>(e5);
|
||||
registry.assign<char>(e5);
|
||||
|
||||
ASSERT_NE(group.find(e5), group.end());
|
||||
ASSERT_EQ(group.find(e4), group.end());
|
||||
}
|
||||
|
||||
TEST(OwningGroup, ExcludedComponents) {
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1, 1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
const auto group = registry.group<int>(entt::exclude<char, double>);
|
||||
|
||||
const auto e2 = registry.create();
|
||||
registry.assign<int>(e2, 2);
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<int>(e3, 3);
|
||||
registry.assign<double>(e3);
|
||||
|
||||
for(const auto entity: group) {
|
||||
if(entity == e0) {
|
||||
ASSERT_EQ(group.get<int>(e0), 0);
|
||||
} else if(entity == e2) {
|
||||
ASSERT_EQ(group.get<int>(e2), 2);
|
||||
} else {
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
registry.assign<char>(e0);
|
||||
registry.assign<double>(e2);
|
||||
|
||||
ASSERT_TRUE(group.empty());
|
||||
|
||||
registry.remove<char>(e1);
|
||||
registry.remove<double>(e3);
|
||||
|
||||
for(const auto entity: group) {
|
||||
if(entity == e1) {
|
||||
ASSERT_EQ(group.get<int>(e1), 1);
|
||||
} else if(entity == e3) {
|
||||
ASSERT_EQ(group.get<int>(e3), 3);
|
||||
} else {
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(OwningGroup, EmptyAndNonEmptyTypes) {
|
||||
entt::registry registry;
|
||||
const auto group = registry.group<empty_type>(entt::get<int>);
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<empty_type>(e0);
|
||||
registry.assign<int>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<empty_type>(e1);
|
||||
registry.assign<int>(e1);
|
||||
|
||||
registry.assign<int>(registry.create());
|
||||
|
||||
for(const auto entity: group) {
|
||||
ASSERT_TRUE(entity == e0 || entity == e1);
|
||||
}
|
||||
|
||||
group.each([e0, e1](const auto entity, empty_type, const int &) {
|
||||
ASSERT_TRUE(entity == e0 || entity == e1);
|
||||
});
|
||||
|
||||
ASSERT_EQ(group.size(), typename decltype(group)::size_type{2});
|
||||
}
|
||||
|
||||
TEST(OwningGroup, TrackEntitiesOnComponentDestruction) {
|
||||
entt::registry registry;
|
||||
const auto group = registry.group<int>(entt::exclude<char>);
|
||||
const auto cgroup = std::as_const(registry).group<const int>(entt::exclude<char>);
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
|
||||
ASSERT_TRUE(group.empty());
|
||||
ASSERT_TRUE(cgroup.empty());
|
||||
|
||||
registry.remove<char>(entity);
|
||||
|
||||
ASSERT_FALSE(group.empty());
|
||||
ASSERT_FALSE(cgroup.empty());
|
||||
}
|
||||
@@ -2,11 +2,30 @@
|
||||
#include <entt/core/hashed_string.hpp>
|
||||
#include <entt/entity/helper.hpp>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/core/type_traits.hpp>
|
||||
|
||||
TEST(Helper, AsView) {
|
||||
entt::registry registry;
|
||||
const entt::registry cregistry;
|
||||
|
||||
([](entt::view<int, char>) {})(entt::as_view{registry});
|
||||
([](entt::view<const int, char>) {})(entt::as_view{registry});
|
||||
([](entt::view<const double>) {})(entt::as_view{cregistry});
|
||||
}
|
||||
|
||||
TEST(Helper, AsGroup) {
|
||||
entt::registry registry;
|
||||
const entt::registry cregistry;
|
||||
|
||||
([](entt::group<entt::get_t<>, double, float>) {})(entt::as_group{registry});
|
||||
([](entt::group<entt::get_t<>, const double, float>) {})(entt::as_group{registry});
|
||||
([](entt::group<entt::get_t<>, const double, const float>) {})(entt::as_group{cregistry});
|
||||
}
|
||||
|
||||
TEST(Helper, Dependency) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
const auto entity = registry.create();
|
||||
entt::connect<double, float>(registry.construction<int>());
|
||||
entt::connect<double, float>(registry.on_construct<int>());
|
||||
|
||||
ASSERT_FALSE(registry.has<double>(entity));
|
||||
ASSERT_FALSE(registry.has<float>(entity));
|
||||
@@ -42,32 +61,44 @@ TEST(Helper, Dependency) {
|
||||
registry.remove<int>(entity);
|
||||
registry.remove<double>(entity);
|
||||
registry.remove<float>(entity);
|
||||
entt::disconnect<double, float>(registry.construction<int>());
|
||||
entt::disconnect<double, float>(registry.on_construct<int>());
|
||||
registry.assign<int>(entity);
|
||||
|
||||
ASSERT_FALSE(registry.has<double>(entity));
|
||||
ASSERT_FALSE(registry.has<float>(entity));
|
||||
}
|
||||
|
||||
TEST(Helper, Label) {
|
||||
entt::DefaultRegistry registry;
|
||||
TEST(Dependency, MultipleListenersOnTheSameType) {
|
||||
entt::registry registry;
|
||||
entt::connect<double>(registry.on_construct<int>());
|
||||
entt::connect<char>(registry.on_construct<int>());
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<entt::label<"foobar"_hs>>(entity);
|
||||
registry.assign<int>(entity);
|
||||
|
||||
ASSERT_TRUE(registry.has<double>(entity));
|
||||
ASSERT_TRUE(registry.has<char>(entity));
|
||||
}
|
||||
|
||||
TEST(Helper, Tag) {
|
||||
entt::registry registry;
|
||||
const auto entity = registry.create();
|
||||
registry.assign<entt::tag<"foobar"_hs>>(entity);
|
||||
registry.assign<int>(entity, 42);
|
||||
int counter{};
|
||||
|
||||
ASSERT_FALSE(registry.has<entt::label<"barfoo"_hs>>(entity));
|
||||
ASSERT_TRUE(registry.has<entt::label<"foobar"_hs>>(entity));
|
||||
ASSERT_FALSE(registry.has<entt::tag<"barfoo"_hs>>(entity));
|
||||
ASSERT_TRUE(registry.has<entt::tag<"foobar"_hs>>(entity));
|
||||
|
||||
for(auto entity: registry.view<int, entt::label<"foobar"_hs>>()) {
|
||||
(void)entity;
|
||||
for(auto entt: registry.view<int, entt::tag<"foobar"_hs>>()) {
|
||||
(void)entt;
|
||||
++counter;
|
||||
}
|
||||
|
||||
ASSERT_NE(counter, 0);
|
||||
|
||||
for(auto entity: registry.view<entt::label<"foobar"_hs>>()) {
|
||||
(void)entity;
|
||||
for(auto entt: registry.view<entt::tag<"foobar"_hs>>()) {
|
||||
(void)entt;
|
||||
--counter;
|
||||
}
|
||||
|
||||
|
||||
@@ -3,10 +3,11 @@
|
||||
#include <entt/entity/registry.hpp>
|
||||
|
||||
TEST(Prototype, SameRegistry) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultPrototype prototype{registry};
|
||||
const auto &cprototype = prototype;
|
||||
entt::registry registry;
|
||||
entt::prototype prototype{registry};
|
||||
|
||||
ASSERT_EQ(®istry, &prototype.backend());
|
||||
ASSERT_EQ(®istry, &std::as_const(prototype).backend());
|
||||
ASSERT_FALSE(registry.empty());
|
||||
ASSERT_FALSE((prototype.has<int, char>()));
|
||||
|
||||
@@ -15,9 +16,17 @@ TEST(Prototype, SameRegistry) {
|
||||
ASSERT_EQ(prototype.set<char>('c'), 'c');
|
||||
|
||||
ASSERT_EQ(prototype.get<int>(), 3);
|
||||
ASSERT_EQ(cprototype.get<char>(), 'c');
|
||||
ASSERT_EQ(std::as_const(prototype).get<char>(), 'c');
|
||||
ASSERT_EQ(std::get<0>(prototype.get<int, char>()), 3);
|
||||
ASSERT_EQ(std::get<1>(cprototype.get<int, char>()), 'c');
|
||||
ASSERT_EQ(std::get<1>(std::as_const(prototype).get<int, char>()), 'c');
|
||||
|
||||
ASSERT_NE(prototype.try_get<int>(), nullptr);
|
||||
ASSERT_NE(prototype.try_get<char>(), nullptr);
|
||||
ASSERT_EQ(prototype.try_get<double>(), nullptr);
|
||||
ASSERT_EQ(*prototype.try_get<int>(), 3);
|
||||
ASSERT_EQ(*std::as_const(prototype).try_get<char>(), 'c');
|
||||
ASSERT_EQ(*std::get<0>(prototype.try_get<int, char, double>()), 3);
|
||||
ASSERT_EQ(*std::get<1>(std::as_const(prototype).try_get<int, char, double>()), 'c');
|
||||
|
||||
const auto e0 = prototype.create();
|
||||
|
||||
@@ -56,16 +65,15 @@ TEST(Prototype, SameRegistry) {
|
||||
ASSERT_EQ(registry.get<char>(e0), '*');
|
||||
|
||||
registry.get<char>(e1) = '*';
|
||||
prototype.accommodate(e1);
|
||||
prototype.assign_or_replace(e1);
|
||||
|
||||
ASSERT_EQ(registry.get<char>(e1), 'c');
|
||||
}
|
||||
|
||||
TEST(Prototype, OtherRegistry) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::DefaultRegistry repository;
|
||||
entt::DefaultPrototype prototype{repository};
|
||||
const auto &cprototype = prototype;
|
||||
entt::registry registry;
|
||||
entt::registry repository;
|
||||
entt::prototype prototype{repository};
|
||||
|
||||
ASSERT_TRUE(registry.empty());
|
||||
ASSERT_FALSE((prototype.has<int, char>()));
|
||||
@@ -75,9 +83,9 @@ TEST(Prototype, OtherRegistry) {
|
||||
ASSERT_EQ(prototype.set<char>('c'), 'c');
|
||||
|
||||
ASSERT_EQ(prototype.get<int>(), 3);
|
||||
ASSERT_EQ(cprototype.get<char>(), 'c');
|
||||
ASSERT_EQ(std::as_const(prototype).get<char>(), 'c');
|
||||
ASSERT_EQ(std::get<0>(prototype.get<int, char>()), 3);
|
||||
ASSERT_EQ(std::get<1>(cprototype.get<int, char>()), 'c');
|
||||
ASSERT_EQ(std::get<1>(std::as_const(prototype).get<int, char>()), 'c');
|
||||
|
||||
const auto e0 = prototype.create(registry);
|
||||
|
||||
@@ -116,16 +124,16 @@ TEST(Prototype, OtherRegistry) {
|
||||
ASSERT_EQ(registry.get<char>(e0), '*');
|
||||
|
||||
registry.get<char>(e1) = '*';
|
||||
prototype.accommodate(registry, e1);
|
||||
prototype.assign_or_replace(registry, e1);
|
||||
|
||||
ASSERT_EQ(registry.get<char>(e1), 'c');
|
||||
}
|
||||
|
||||
TEST(Prototype, RAII) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
{
|
||||
entt::DefaultPrototype prototype{registry};
|
||||
entt::prototype prototype{registry};
|
||||
prototype.set<int>(0);
|
||||
|
||||
ASSERT_FALSE(registry.empty());
|
||||
@@ -135,19 +143,19 @@ TEST(Prototype, RAII) {
|
||||
}
|
||||
|
||||
TEST(Prototype, MoveConstructionAssignment) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
entt::DefaultPrototype prototype{registry};
|
||||
entt::prototype prototype{registry};
|
||||
prototype.set<int>(0);
|
||||
auto other{std::move(prototype)};
|
||||
const auto e0 = other();
|
||||
|
||||
ASSERT_EQ(registry.size(), entt::DefaultRegistry::size_type{2});
|
||||
ASSERT_EQ(registry.size(), entt::registry::size_type{2});
|
||||
ASSERT_TRUE(registry.has<int>(e0));
|
||||
|
||||
prototype = std::move(other);
|
||||
const auto e1 = prototype();
|
||||
|
||||
ASSERT_EQ(registry.size(), entt::DefaultRegistry::size_type{3});
|
||||
ASSERT_EQ(registry.size(), entt::registry::size_type{3});
|
||||
ASSERT_TRUE(registry.has<int>(e1));
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
208
test/entt/entity/runtime_view.cpp
Normal file
208
test/entt/entity/runtime_view.cpp
Normal file
@@ -0,0 +1,208 @@
|
||||
#include <iterator>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/runtime_view.hpp>
|
||||
|
||||
TEST(RuntimeView, Functionalities) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
// forces the creation of the pools
|
||||
registry.reserve<int>(0);
|
||||
registry.reserve<char>(0);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
|
||||
ASSERT_FALSE(view.empty());
|
||||
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto it = registry.runtime_view(std::begin(types), std::end(types)).begin();
|
||||
|
||||
ASSERT_EQ(*it, e1);
|
||||
ASSERT_EQ(++it, (registry.runtime_view(std::begin(types), std::end(types)).end()));
|
||||
|
||||
ASSERT_NO_THROW((registry.runtime_view(std::begin(types), std::end(types)).begin()++));
|
||||
ASSERT_NO_THROW((++registry.runtime_view(std::begin(types), std::end(types)).begin()));
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){1});
|
||||
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
|
||||
for(auto entity: view) {
|
||||
ASSERT_EQ(registry.get<int>(entity), 42);
|
||||
ASSERT_EQ(registry.get<char>(entity), '2');
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Iterator) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
using iterator_type = typename decltype(view)::iterator_type;
|
||||
|
||||
iterator_type end{view.begin()};
|
||||
iterator_type begin{};
|
||||
begin = view.end();
|
||||
std::swap(begin, end);
|
||||
|
||||
ASSERT_EQ(begin, view.begin());
|
||||
ASSERT_EQ(end, view.end());
|
||||
ASSERT_NE(begin, end);
|
||||
|
||||
ASSERT_EQ(view.begin()++, view.begin());
|
||||
ASSERT_EQ(++view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Contains) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
ASSERT_TRUE(view.contains(e1));
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Empty) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<double>(e0);
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<float>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<char>(e1);
|
||||
registry.assign<float>(e1);
|
||||
|
||||
component_type types[] = { registry.type<char>(), registry.type<int>(), registry.type<float>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Each) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
}
|
||||
|
||||
TEST(RuntimeView, EachWithHoles) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
const auto e2 = registry.create();
|
||||
|
||||
registry.assign<char>(e0, '0');
|
||||
registry.assign<char>(e1, '1');
|
||||
|
||||
registry.assign<int>(e0, 0);
|
||||
registry.assign<int>(e2, 2);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
|
||||
view.each([e0](auto entity) {
|
||||
ASSERT_EQ(e0, entity);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(RuntimeView, MissingPool) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.runtime_view(std::begin(types), std::end(types));
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
|
||||
registry.assign<char>(e0);
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
|
||||
view.each([](auto) { FAIL(); });
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RuntimeView, EmptyRange) {
|
||||
entt::registry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
|
||||
const component_type *ptr = nullptr;
|
||||
auto view = registry.runtime_view(ptr, ptr);
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
|
||||
view.each([](auto) { FAIL(); });
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
@@ -3,18 +3,17 @@
|
||||
#include <vector>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/entity.hpp>
|
||||
|
||||
template<typename Storage>
|
||||
struct OutputArchive {
|
||||
OutputArchive(Storage &storage)
|
||||
: storage{storage}
|
||||
struct output_archive {
|
||||
output_archive(Storage &instance)
|
||||
: storage{instance}
|
||||
{}
|
||||
|
||||
template<typename... Value>
|
||||
void operator()(const Value &... value) {
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { (std::get<std::queue<Value>>(storage).push(value), 0)... };
|
||||
(void)accumulator;
|
||||
(std::get<std::queue<Value>>(storage).push(value), ...);
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -22,42 +21,40 @@ private:
|
||||
};
|
||||
|
||||
template<typename Storage>
|
||||
struct InputArchive {
|
||||
InputArchive(Storage &storage)
|
||||
: storage{storage}
|
||||
struct input_archive {
|
||||
input_archive(Storage &instance)
|
||||
: storage{instance}
|
||||
{}
|
||||
|
||||
template<typename... Value>
|
||||
void operator()(Value &... value) {
|
||||
auto assign = [this](auto &value) {
|
||||
auto &queue = std::get<std::queue<std::decay_t<decltype(value)>>>(storage);
|
||||
value = queue.front();
|
||||
auto assign = [this](auto &val) {
|
||||
auto &queue = std::get<std::queue<std::decay_t<decltype(val)>>>(storage);
|
||||
val = queue.front();
|
||||
queue.pop();
|
||||
};
|
||||
|
||||
using accumulator_type = int[];
|
||||
accumulator_type accumulator = { (assign(value), 0)... };
|
||||
(void)accumulator;
|
||||
(assign(value), ...);
|
||||
}
|
||||
|
||||
private:
|
||||
Storage &storage;
|
||||
};
|
||||
|
||||
struct AComponent {};
|
||||
struct a_component {};
|
||||
|
||||
struct AnotherComponent {
|
||||
struct another_component {
|
||||
int key;
|
||||
int value;
|
||||
};
|
||||
|
||||
struct WhatAComponent {
|
||||
entt::DefaultRegistry::entity_type bar;
|
||||
std::vector<entt::DefaultRegistry::entity_type> quux;
|
||||
struct what_a_component {
|
||||
entt::entity bar;
|
||||
std::vector<entt::entity> quux;
|
||||
};
|
||||
|
||||
TEST(Snapshot, Dump) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 42);
|
||||
@@ -70,36 +67,29 @@ TEST(Snapshot, Dump) {
|
||||
registry.assign<int>(e2, 3);
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<a_component>(e3);
|
||||
registry.assign<char>(e3, '0');
|
||||
registry.assign<float>(entt::tag_t{}, e3, .3f);
|
||||
|
||||
const auto e4 = registry.create();
|
||||
registry.assign<AComponent>(entt::tag_t{}, e4);
|
||||
|
||||
registry.destroy(e1);
|
||||
auto v1 = registry.current(e1);
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entt::DefaultRegistry::entity_type>,
|
||||
std::queue<entt::entity>,
|
||||
std::queue<int>,
|
||||
std::queue<char>,
|
||||
std::queue<double>,
|
||||
std::queue<float>,
|
||||
std::queue<bool>,
|
||||
std::queue<AComponent>,
|
||||
std::queue<AnotherComponent>,
|
||||
std::queue<WhatAComponent>
|
||||
std::queue<a_component>,
|
||||
std::queue<another_component>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
output_archive<storage_type> output{storage};
|
||||
input_archive<storage_type> input{storage};
|
||||
|
||||
registry.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<int, char, AnotherComponent, double>(output)
|
||||
.tag<float, bool, AComponent>(output);
|
||||
.component<int, char, double, a_component, another_component>(output);
|
||||
|
||||
registry.reset();
|
||||
|
||||
@@ -107,25 +97,21 @@ TEST(Snapshot, Dump) {
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
ASSERT_FALSE(registry.valid(e4));
|
||||
|
||||
registry.restore()
|
||||
registry.loader()
|
||||
.entities(input)
|
||||
.destroyed(input)
|
||||
.component<int, char, AnotherComponent, double>(input)
|
||||
.tag<float, bool, AComponent>(input)
|
||||
.component<int, char, double, a_component, another_component>(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');
|
||||
@@ -133,20 +119,13 @@ TEST(Snapshot, Dump) {
|
||||
ASSERT_EQ(registry.current(e1), v1);
|
||||
ASSERT_EQ(registry.get<int>(e2), 3);
|
||||
ASSERT_EQ(registry.get<char>(e3), '0');
|
||||
ASSERT_TRUE(registry.has<a_component>(e3));
|
||||
|
||||
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>());
|
||||
ASSERT_TRUE(registry.empty<another_component>());
|
||||
}
|
||||
|
||||
TEST(Snapshot, Partial) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 42);
|
||||
@@ -160,34 +139,25 @@ TEST(Snapshot, Partial) {
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<char>(e3, '0');
|
||||
registry.assign<float>(entt::tag_t{}, e3, .3f);
|
||||
|
||||
const auto e4 = registry.create();
|
||||
registry.assign<AComponent>(entt::tag_t{}, e4);
|
||||
|
||||
registry.destroy(e1);
|
||||
auto v1 = registry.current(e1);
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entt::DefaultRegistry::entity_type>,
|
||||
std::queue<entt::entity>,
|
||||
std::queue<int>,
|
||||
std::queue<char>,
|
||||
std::queue<double>,
|
||||
std::queue<float>,
|
||||
std::queue<bool>,
|
||||
std::queue<AComponent>,
|
||||
std::queue<WhatAComponent>
|
||||
std::queue<double>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
output_archive<storage_type> output{storage};
|
||||
input_archive<storage_type> input{storage};
|
||||
|
||||
registry.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<char, int>(output)
|
||||
.tag<bool, float>(output);
|
||||
.component<char, int>(output);
|
||||
|
||||
registry.reset();
|
||||
|
||||
@@ -195,19 +165,16 @@ TEST(Snapshot, Partial) {
|
||||
ASSERT_FALSE(registry.valid(e1));
|
||||
ASSERT_FALSE(registry.valid(e2));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
ASSERT_FALSE(registry.valid(e4));
|
||||
|
||||
registry.restore()
|
||||
registry.loader()
|
||||
.entities(input)
|
||||
.destroyed(input)
|
||||
.component<char, int>(input)
|
||||
.tag<bool, float>(input);
|
||||
.component<char, int>(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');
|
||||
@@ -215,17 +182,10 @@ TEST(Snapshot, Partial) {
|
||||
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);
|
||||
.entities(output)
|
||||
.destroyed(output);
|
||||
|
||||
registry.reset();
|
||||
|
||||
@@ -233,78 +193,72 @@ TEST(Snapshot, Partial) {
|
||||
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)
|
||||
registry.loader()
|
||||
.entities(input)
|
||||
.destroyed(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));
|
||||
ASSERT_FALSE(registry.valid(e3));
|
||||
}
|
||||
|
||||
TEST(Snapshot, Iterator) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
for(auto i = 0; i < 50; ++i) {
|
||||
const auto entity = registry.create();
|
||||
registry.assign<AnotherComponent>(entity, i, i);
|
||||
registry.assign<another_component>(entity, i, i);
|
||||
|
||||
if(i % 2) {
|
||||
registry.assign<AComponent>(entity);
|
||||
registry.assign<a_component>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entt::DefaultRegistry::entity_type>,
|
||||
std::queue<AnotherComponent>
|
||||
std::queue<entt::entity>,
|
||||
std::queue<another_component>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
output_archive<storage_type> output{storage};
|
||||
input_archive<storage_type> input{storage};
|
||||
|
||||
const auto view = registry.view<AComponent>();
|
||||
const auto view = registry.view<a_component>();
|
||||
const auto size = view.size();
|
||||
|
||||
registry.snapshot().component<AnotherComponent>(output, view.cbegin(), view.cend());
|
||||
registry.snapshot().component<another_component>(output, view.begin(), view.end());
|
||||
registry.reset();
|
||||
registry.restore().component<AnotherComponent>(input);
|
||||
registry.loader().component<another_component>(input);
|
||||
|
||||
ASSERT_EQ(registry.view<AnotherComponent>().size(), size);
|
||||
ASSERT_EQ(registry.view<another_component>().size(), size);
|
||||
|
||||
registry.view<AnotherComponent>().each([](const auto entity, auto &&...) {
|
||||
registry.view<another_component>().each([](const auto entity, const auto &) {
|
||||
ASSERT_TRUE(entity % 2);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(Snapshot, Continuous) {
|
||||
using entity_type = entt::DefaultRegistry::entity_type;
|
||||
entt::registry src;
|
||||
entt::registry dst;
|
||||
|
||||
entt::DefaultRegistry src;
|
||||
entt::DefaultRegistry dst;
|
||||
entt::continuous_loader loader{dst};
|
||||
|
||||
entt::ContinuousLoader<entity_type> loader{dst};
|
||||
|
||||
std::vector<entity_type> entities;
|
||||
entity_type entity;
|
||||
std::vector<entt::entity> entities;
|
||||
entt::entity entity;
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entity_type>,
|
||||
std::queue<AComponent>,
|
||||
std::queue<AnotherComponent>,
|
||||
std::queue<WhatAComponent>,
|
||||
std::queue<entt::entity>,
|
||||
std::queue<another_component>,
|
||||
std::queue<what_a_component>,
|
||||
std::queue<double>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
output_archive<storage_type> output{storage};
|
||||
input_archive<storage_type> input{storage};
|
||||
|
||||
for(int i = 0; i < 10; ++i) {
|
||||
src.create();
|
||||
@@ -316,64 +270,57 @@ TEST(Snapshot, Continuous) {
|
||||
entity = src.create();
|
||||
entities.push_back(entity);
|
||||
|
||||
src.assign<AComponent>(entity);
|
||||
src.assign<AnotherComponent>(entity, i, i);
|
||||
src.assign<a_component>(entity);
|
||||
src.assign<another_component>(entity, i, i);
|
||||
|
||||
if(i % 2) {
|
||||
src.assign<WhatAComponent>(entity, entity);
|
||||
} else if(i == 2) {
|
||||
src.assign<double>(entt::tag_t{}, entity, .3);
|
||||
src.assign<what_a_component>(entity, entity);
|
||||
}
|
||||
}
|
||||
|
||||
src.view<WhatAComponent>().each([&entities](auto, auto &whatAComponent) {
|
||||
whatAComponent.quux.insert(whatAComponent.quux.begin(), entities.begin(), entities.end());
|
||||
src.view<what_a_component>().each([&entities](auto, auto &what_a_component) {
|
||||
what_a_component.quux.insert(what_a_component.quux.begin(), entities.begin(), entities.end());
|
||||
});
|
||||
|
||||
entity = dst.create();
|
||||
dst.assign<AComponent>(entity);
|
||||
dst.assign<AnotherComponent>(entity, -1, -1);
|
||||
dst.assign<a_component>(entity);
|
||||
dst.assign<another_component>(entity, -1, -1);
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, WhatAComponent>(output)
|
||||
.tag<double>(output);
|
||||
.component<a_component, another_component, what_a_component>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent, WhatAComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<double>(input)
|
||||
.component<a_component, another_component, what_a_component>(input, &what_a_component::bar, &what_a_component::quux)
|
||||
.orphans();
|
||||
|
||||
decltype(dst.size()) aComponentCnt{};
|
||||
decltype(dst.size()) anotherComponentCnt{};
|
||||
decltype(dst.size()) whatAComponentCnt{};
|
||||
decltype(dst.size()) a_component_cnt{};
|
||||
decltype(dst.size()) another_component_cnt{};
|
||||
decltype(dst.size()) what_a_component_cnt{};
|
||||
|
||||
dst.each([&dst, &aComponentCnt](auto entity) {
|
||||
ASSERT_TRUE(dst.has<AComponent>(entity));
|
||||
++aComponentCnt;
|
||||
dst.each([&dst, &a_component_cnt](auto entt) {
|
||||
ASSERT_TRUE(dst.has<a_component>(entt));
|
||||
++a_component_cnt;
|
||||
});
|
||||
|
||||
dst.view<AnotherComponent>().each([&anotherComponentCnt](auto, const auto &component) {
|
||||
dst.view<another_component>().each([&another_component_cnt](auto, const auto &component) {
|
||||
ASSERT_EQ(component.value, component.key < 0 ? -1 : component.key);
|
||||
++anotherComponentCnt;
|
||||
++another_component_cnt;
|
||||
});
|
||||
|
||||
dst.view<WhatAComponent>().each([&dst, &whatAComponentCnt](auto entity, const auto &component) {
|
||||
ASSERT_EQ(entity, component.bar);
|
||||
dst.view<what_a_component>().each([&dst, &what_a_component_cnt](auto entt, const auto &component) {
|
||||
ASSERT_EQ(entt, component.bar);
|
||||
|
||||
for(auto entity: component.quux) {
|
||||
ASSERT_TRUE(dst.valid(entity));
|
||||
for(auto child: component.quux) {
|
||||
ASSERT_TRUE(dst.valid(child));
|
||||
}
|
||||
|
||||
++whatAComponentCnt;
|
||||
++what_a_component_cnt;
|
||||
});
|
||||
|
||||
ASSERT_TRUE(dst.has<double>());
|
||||
ASSERT_EQ(dst.get<double>(), .3);
|
||||
|
||||
src.view<AnotherComponent>().each([](auto, auto &component) {
|
||||
src.view<another_component>().each([](auto, auto &component) {
|
||||
component.value = 2 * component.key;
|
||||
});
|
||||
|
||||
@@ -382,51 +329,46 @@ TEST(Snapshot, Continuous) {
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, WhatAComponent, AnotherComponent>(output)
|
||||
.tag<double>(output);
|
||||
.component<a_component, what_a_component, another_component>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, WhatAComponent, AnotherComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<double>(input)
|
||||
.component<a_component, what_a_component, another_component>(input, &what_a_component::bar, &what_a_component::quux)
|
||||
.orphans();
|
||||
|
||||
ASSERT_EQ(size, dst.size());
|
||||
|
||||
ASSERT_EQ(dst.size<AComponent>(), aComponentCnt);
|
||||
ASSERT_EQ(dst.size<AnotherComponent>(), anotherComponentCnt);
|
||||
ASSERT_EQ(dst.size<WhatAComponent>(), whatAComponentCnt);
|
||||
ASSERT_TRUE(dst.has<double>());
|
||||
ASSERT_EQ(dst.size<a_component>(), a_component_cnt);
|
||||
ASSERT_EQ(dst.size<another_component>(), another_component_cnt);
|
||||
ASSERT_EQ(dst.size<what_a_component>(), what_a_component_cnt);
|
||||
|
||||
dst.view<AnotherComponent>().each([](auto, auto &component) {
|
||||
dst.view<another_component>().each([](auto, auto &component) {
|
||||
ASSERT_EQ(component.value, component.key < 0 ? -1 : (2 * component.key));
|
||||
});
|
||||
|
||||
entity = src.create();
|
||||
|
||||
src.view<WhatAComponent>().each([entity](auto, auto &component) {
|
||||
src.view<what_a_component>().each([entity](auto, auto &component) {
|
||||
component.bar = entity;
|
||||
});
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<WhatAComponent, AComponent, AnotherComponent>(output)
|
||||
.tag<double>(output);
|
||||
.component<what_a_component, a_component, another_component>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<WhatAComponent, AComponent, AnotherComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<double>(input)
|
||||
.component<what_a_component, a_component, another_component>(input, &what_a_component::bar, &what_a_component::quux)
|
||||
.orphans();
|
||||
|
||||
dst.view<WhatAComponent>().each([&loader, entity](auto, auto &component) {
|
||||
dst.view<what_a_component>().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);
|
||||
for(auto entt: src.view<a_component>()) {
|
||||
entities.push_back(entt);
|
||||
}
|
||||
|
||||
src.destroy(entity);
|
||||
@@ -435,17 +377,15 @@ TEST(Snapshot, Continuous) {
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, AnotherComponent, WhatAComponent>(output)
|
||||
.tag<double>(output);
|
||||
.component<a_component, another_component, what_a_component>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, AnotherComponent, WhatAComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<double>(input)
|
||||
.component<a_component, another_component, what_a_component>(input, &what_a_component::bar, &what_a_component::quux)
|
||||
.orphans()
|
||||
.shrink();
|
||||
|
||||
dst.view<WhatAComponent>().each([&dst](auto, auto &component) {
|
||||
dst.view<what_a_component>().each([&dst](auto, auto &component) {
|
||||
ASSERT_FALSE(dst.valid(component.bar));
|
||||
});
|
||||
|
||||
@@ -453,64 +393,52 @@ TEST(Snapshot, Continuous) {
|
||||
|
||||
entity = src.create();
|
||||
|
||||
src.view<WhatAComponent>().each([entity](auto, auto &component) {
|
||||
src.view<what_a_component>().each([entity](auto, auto &component) {
|
||||
component.bar = entity;
|
||||
});
|
||||
|
||||
dst.reset<AComponent>();
|
||||
aComponentCnt = src.size<AComponent>();
|
||||
dst.reset<a_component>();
|
||||
a_component_cnt = src.size<a_component>();
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<AComponent, WhatAComponent, AnotherComponent>(output)
|
||||
.tag<double>(output);
|
||||
.component<a_component, what_a_component, another_component>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<AComponent, WhatAComponent, AnotherComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<double>(input)
|
||||
.component<a_component, what_a_component, another_component>(input, &what_a_component::bar, &what_a_component::quux)
|
||||
.orphans();
|
||||
|
||||
ASSERT_EQ(dst.size<AComponent>(), aComponentCnt);
|
||||
ASSERT_TRUE(dst.has<double>());
|
||||
ASSERT_EQ(dst.size<a_component>(), a_component_cnt);
|
||||
|
||||
src.reset<AComponent>();
|
||||
src.remove<double>();
|
||||
aComponentCnt = {};
|
||||
src.reset<a_component>();
|
||||
a_component_cnt = {};
|
||||
|
||||
src.snapshot()
|
||||
.entities(output)
|
||||
.destroyed(output)
|
||||
.component<WhatAComponent, AComponent, AnotherComponent>(output)
|
||||
.tag<double>(output);
|
||||
.component<what_a_component, a_component, another_component>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.destroyed(input)
|
||||
.component<WhatAComponent, AComponent, AnotherComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<double>(input)
|
||||
.component<what_a_component, a_component, another_component>(input, &what_a_component::bar, &what_a_component::quux)
|
||||
.orphans();
|
||||
|
||||
ASSERT_EQ(dst.size<AComponent>(), aComponentCnt);
|
||||
ASSERT_FALSE(dst.has<double>());
|
||||
ASSERT_EQ(dst.size<a_component>(), a_component_cnt);
|
||||
}
|
||||
|
||||
TEST(Snapshot, ContinuousMoreOnShrink) {
|
||||
using entity_type = entt::DefaultRegistry::entity_type;
|
||||
TEST(Snapshot, MoreOnShrink) {
|
||||
entt::registry src;
|
||||
entt::registry dst;
|
||||
|
||||
entt::DefaultRegistry src;
|
||||
entt::DefaultRegistry dst;
|
||||
entt::continuous_loader loader{dst};
|
||||
|
||||
entt::ContinuousLoader<entity_type> loader{dst};
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entity_type>,
|
||||
std::queue<AComponent>
|
||||
>;
|
||||
using storage_type = std::tuple<std::queue<entt::entity>>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
output_archive<storage_type> output{storage};
|
||||
input_archive<storage_type> input{storage};
|
||||
|
||||
auto entity = src.create();
|
||||
src.snapshot().entities(output);
|
||||
@@ -524,21 +452,19 @@ TEST(Snapshot, ContinuousMoreOnShrink) {
|
||||
}
|
||||
|
||||
TEST(Snapshot, SyncDataMembers) {
|
||||
using entity_type = entt::DefaultRegistry::entity_type;
|
||||
entt::registry src;
|
||||
entt::registry dst;
|
||||
|
||||
entt::DefaultRegistry src;
|
||||
entt::DefaultRegistry dst;
|
||||
|
||||
entt::ContinuousLoader<entity_type> loader{dst};
|
||||
entt::continuous_loader loader{dst};
|
||||
|
||||
using storage_type = std::tuple<
|
||||
std::queue<entity_type>,
|
||||
std::queue<WhatAComponent>
|
||||
std::queue<entt::entity>,
|
||||
std::queue<what_a_component>
|
||||
>;
|
||||
|
||||
storage_type storage;
|
||||
OutputArchive<storage_type> output{storage};
|
||||
InputArchive<storage_type> input{storage};
|
||||
output_archive<storage_type> output{storage};
|
||||
input_archive<storage_type> input{storage};
|
||||
|
||||
src.create();
|
||||
src.create();
|
||||
@@ -548,28 +474,22 @@ TEST(Snapshot, SyncDataMembers) {
|
||||
auto parent = src.create();
|
||||
auto child = src.create();
|
||||
|
||||
src.assign<WhatAComponent>(entt::tag_t{}, child, parent).quux.push_back(parent);
|
||||
src.assign<WhatAComponent>(child, child).quux.push_back(child);
|
||||
src.assign<what_a_component>(parent, entt::null);
|
||||
src.assign<what_a_component>(child, parent).quux.push_back(child);
|
||||
|
||||
src.snapshot().entities(output)
|
||||
.component<WhatAComponent>(output)
|
||||
.tag<WhatAComponent>(output);
|
||||
|
||||
loader.entities(input)
|
||||
.component<WhatAComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux)
|
||||
.tag<WhatAComponent>(input, &WhatAComponent::bar, &WhatAComponent::quux);
|
||||
src.snapshot().entities(output).component<what_a_component>(output);
|
||||
loader.entities(input).component<what_a_component>(input, &what_a_component::bar, &what_a_component::quux);
|
||||
|
||||
ASSERT_FALSE(dst.valid(parent));
|
||||
ASSERT_FALSE(dst.valid(child));
|
||||
|
||||
ASSERT_TRUE(dst.has<WhatAComponent>());
|
||||
ASSERT_EQ(dst.attachee<WhatAComponent>(), loader.map(child));
|
||||
ASSERT_EQ(dst.get<WhatAComponent>().bar, loader.map(parent));
|
||||
ASSERT_EQ(dst.get<WhatAComponent>().quux[0], loader.map(parent));
|
||||
ASSERT_TRUE(dst.has<WhatAComponent>(loader.map(child)));
|
||||
ASSERT_TRUE(dst.has<what_a_component>(loader.map(parent)));
|
||||
ASSERT_TRUE(dst.has<what_a_component>(loader.map(child)));
|
||||
|
||||
const auto &component = dst.get<WhatAComponent>(loader.map(child));
|
||||
ASSERT_EQ(dst.get<what_a_component>(loader.map(parent)).bar, static_cast<entt::entity>(entt::null));
|
||||
|
||||
ASSERT_EQ(component.bar, loader.map(child));
|
||||
const auto &component = dst.get<what_a_component>(loader.map(child));
|
||||
|
||||
ASSERT_EQ(component.bar, loader.map(parent));
|
||||
ASSERT_EQ(component.quux[0], loader.map(child));
|
||||
}
|
||||
|
||||
@@ -1,17 +1,22 @@
|
||||
#include <unordered_set>
|
||||
#include <cstdint>
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/sparse_set.hpp>
|
||||
|
||||
TEST(SparseSetNoType, Functionalities) {
|
||||
entt::SparseSet<std::uint64_t> set;
|
||||
const auto &cset = set;
|
||||
struct empty_type {};
|
||||
struct boxed_int { int value; };
|
||||
|
||||
TEST(SparseSet, Functionalities) {
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
|
||||
set.reserve(42);
|
||||
|
||||
ASSERT_EQ(set.capacity(), 42);
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(cset.begin(), cset.end());
|
||||
ASSERT_EQ(std::as_const(set).begin(), std::as_const(set).end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
@@ -22,57 +27,122 @@ TEST(SparseSetNoType, Functionalities) {
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 1u);
|
||||
ASSERT_NE(cset.begin(), cset.end());
|
||||
ASSERT_NE(std::as_const(set).begin(), std::as_const(set).end());
|
||||
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);
|
||||
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(cset.begin(), cset.end());
|
||||
ASSERT_EQ(std::as_const(set).begin(), std::as_const(set).end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
set.construct(42);
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.get(42), 0u);
|
||||
|
||||
set.reset();
|
||||
ASSERT_TRUE(std::is_move_constructible_v<decltype(set)>);
|
||||
ASSERT_TRUE(std::is_move_assignable_v<decltype(set)>);
|
||||
|
||||
entt::sparse_set<std::uint64_t> cpy{set};
|
||||
set = cpy;
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_FALSE(cpy.empty());
|
||||
ASSERT_EQ(set.get(42), 0u);
|
||||
ASSERT_EQ(cpy.get(42), 0u);
|
||||
|
||||
entt::sparse_set<std::uint64_t> other{std::move(set)};
|
||||
|
||||
set = std::move(other);
|
||||
other = std::move(set);
|
||||
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(cset.begin(), cset.end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(other.empty());
|
||||
ASSERT_EQ(other.get(42), 0u);
|
||||
|
||||
other.reset();
|
||||
|
||||
ASSERT_TRUE(other.empty());
|
||||
ASSERT_EQ(other.size(), 0u);
|
||||
ASSERT_EQ(std::as_const(other).begin(), std::as_const(other).end());
|
||||
ASSERT_EQ(other.begin(), other.end());
|
||||
ASSERT_FALSE(other.has(0));
|
||||
ASSERT_FALSE(other.has(42));
|
||||
}
|
||||
|
||||
TEST(SparseSet, Pagination) {
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
constexpr auto entt_per_page = ENTT_PAGE_SIZE / sizeof(std::uint64_t);
|
||||
|
||||
ASSERT_EQ(set.extent(), 0);
|
||||
|
||||
set.construct(entt_per_page-1);
|
||||
|
||||
ASSERT_EQ(set.extent(), entt_per_page);
|
||||
ASSERT_TRUE(set.has(entt_per_page-1));
|
||||
|
||||
set.construct(entt_per_page);
|
||||
|
||||
ASSERT_EQ(set.extent(), 2 * entt_per_page);
|
||||
ASSERT_TRUE(set.has(entt_per_page-1));
|
||||
ASSERT_TRUE(set.has(entt_per_page));
|
||||
ASSERT_FALSE(set.has(entt_per_page+1));
|
||||
|
||||
set.destroy(entt_per_page-1);
|
||||
|
||||
ASSERT_EQ(set.extent(), 2 * entt_per_page);
|
||||
ASSERT_FALSE(set.has(entt_per_page-1));
|
||||
ASSERT_TRUE(set.has(entt_per_page));
|
||||
|
||||
set.shrink_to_fit();
|
||||
set.destroy(entt_per_page);
|
||||
|
||||
ASSERT_EQ(set.extent(), 2 * entt_per_page);
|
||||
ASSERT_FALSE(set.has(entt_per_page-1));
|
||||
ASSERT_FALSE(set.has(entt_per_page));
|
||||
|
||||
set.shrink_to_fit();
|
||||
|
||||
ASSERT_EQ(set.extent(), 0);
|
||||
}
|
||||
|
||||
TEST(SparseSet, BatchAdd) {
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
entt::sparse_set<std::uint64_t>::entity_type entities[2];
|
||||
|
||||
entities[0] = 3;
|
||||
entities[1] = 42;
|
||||
|
||||
set.construct(12);
|
||||
set.batch(std::begin(entities), std::end(entities));
|
||||
set.construct(24);
|
||||
|
||||
ASSERT_TRUE(set.has(entities[0]));
|
||||
ASSERT_TRUE(set.has(entities[1]));
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
ASSERT_FALSE(set.has(9));
|
||||
ASSERT_TRUE(set.has(12));
|
||||
ASSERT_TRUE(set.has(24));
|
||||
|
||||
(void)entt::SparseSet<std::uint64_t>{std::move(set)};
|
||||
entt::SparseSet<std::uint64_t> other;
|
||||
other = std::move(set);
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 4u);
|
||||
ASSERT_EQ(set.get(12), 0u);
|
||||
ASSERT_EQ(set.get(entities[0]), 1u);
|
||||
ASSERT_EQ(set.get(entities[1]), 2u);
|
||||
ASSERT_EQ(set.get(24), 3u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, ElementAccess) {
|
||||
entt::SparseSet<std::uint64_t> set;
|
||||
const auto &cset = set;
|
||||
TEST(SparseSet, Iterator) {
|
||||
using iterator_type = typename entt::sparse_set<std::uint64_t>::iterator_type;
|
||||
|
||||
set.construct(42);
|
||||
set.construct(3);
|
||||
|
||||
for(typename entt::SparseSet<std::uint64_t>::size_type i{}; i < set.size(); ++i) {
|
||||
ASSERT_EQ(set[i], i ? 42 : 3);
|
||||
ASSERT_EQ(cset[i], i ? 42 : 3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, Iterator) {
|
||||
using iterator_type = typename entt::SparseSet<std::uint64_t>::iterator_type;
|
||||
|
||||
entt::SparseSet<std::uint64_t> set;
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
set.construct(3);
|
||||
|
||||
iterator_type end{set.begin()};
|
||||
@@ -114,53 +184,28 @@ TEST(SparseSetNoType, Iterator) {
|
||||
ASSERT_EQ(*begin.operator->(), 3);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, ConstIterator) {
|
||||
using iterator_type = typename entt::SparseSet<std::uint64_t>::const_iterator_type;
|
||||
|
||||
entt::SparseSet<std::uint64_t> set;
|
||||
TEST(SparseSet, Find) {
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
set.construct(3);
|
||||
set.construct(42);
|
||||
set.construct(99);
|
||||
|
||||
iterator_type cend{set.cbegin()};
|
||||
iterator_type cbegin{};
|
||||
cbegin = set.cend();
|
||||
std::swap(cbegin, cend);
|
||||
ASSERT_NE(set.find(3), set.end());
|
||||
ASSERT_NE(set.find(42), set.end());
|
||||
ASSERT_NE(set.find(99), set.end());
|
||||
ASSERT_EQ(set.find(0), set.end());
|
||||
|
||||
ASSERT_EQ(cbegin, set.cbegin());
|
||||
ASSERT_EQ(cend, set.cend());
|
||||
ASSERT_NE(cbegin, cend);
|
||||
auto it = set.find(99);
|
||||
|
||||
ASSERT_EQ(cbegin++, set.cbegin());
|
||||
ASSERT_EQ(cbegin--, set.cend());
|
||||
|
||||
ASSERT_EQ(cbegin+1, set.cend());
|
||||
ASSERT_EQ(cend-1, set.cbegin());
|
||||
|
||||
ASSERT_EQ(++cbegin, set.cend());
|
||||
ASSERT_EQ(--cbegin, set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin += 1, set.cend());
|
||||
ASSERT_EQ(cbegin -= 1, set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin + (cend - cbegin), set.cend());
|
||||
ASSERT_EQ(cbegin - (cbegin - cend), set.cend());
|
||||
|
||||
ASSERT_EQ(cend - (cend - cbegin), set.cbegin());
|
||||
ASSERT_EQ(cend + (cbegin - cend), set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin[0], *set.cbegin());
|
||||
|
||||
ASSERT_LT(cbegin, cend);
|
||||
ASSERT_LE(cbegin, set.cbegin());
|
||||
|
||||
ASSERT_GT(cend, cbegin);
|
||||
ASSERT_GE(cend, set.cend());
|
||||
|
||||
ASSERT_EQ(*cbegin, 3);
|
||||
ASSERT_EQ(*cbegin.operator->(), 3);
|
||||
ASSERT_EQ(*it, 99);
|
||||
ASSERT_EQ(*(++it), 42);
|
||||
ASSERT_EQ(*(++it), 3);
|
||||
ASSERT_EQ(++it, set.end());
|
||||
ASSERT_EQ(++set.find(3), set.end());
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, Data) {
|
||||
entt::SparseSet<std::uint64_t> set;
|
||||
TEST(SparseSet, Data) {
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
|
||||
set.construct(3);
|
||||
set.construct(12);
|
||||
@@ -175,10 +220,9 @@ TEST(SparseSetNoType, Data) {
|
||||
ASSERT_EQ(*(set.data() + 2u), 42u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectDisjoint) {
|
||||
entt::SparseSet<std::uint64_t> lhs;
|
||||
entt::SparseSet<std::uint64_t> rhs;
|
||||
const auto &clhs = lhs;
|
||||
TEST(SparseSet, RespectDisjoint) {
|
||||
entt::sparse_set<std::uint64_t> lhs;
|
||||
entt::sparse_set<std::uint64_t> rhs;
|
||||
|
||||
lhs.construct(3);
|
||||
lhs.construct(12);
|
||||
@@ -190,15 +234,14 @@ TEST(SparseSetNoType, RespectDisjoint) {
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(clhs.get(3), 0u);
|
||||
ASSERT_EQ(clhs.get(12), 1u);
|
||||
ASSERT_EQ(clhs.get(42), 2u);
|
||||
ASSERT_EQ(std::as_const(lhs).get(3), 0u);
|
||||
ASSERT_EQ(std::as_const(lhs).get(12), 1u);
|
||||
ASSERT_EQ(std::as_const(lhs).get(42), 2u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectOverlap) {
|
||||
entt::SparseSet<std::uint64_t> lhs;
|
||||
entt::SparseSet<std::uint64_t> rhs;
|
||||
const auto &clhs = lhs;
|
||||
TEST(SparseSet, RespectOverlap) {
|
||||
entt::sparse_set<std::uint64_t> lhs;
|
||||
entt::sparse_set<std::uint64_t> rhs;
|
||||
|
||||
lhs.construct(3);
|
||||
lhs.construct(12);
|
||||
@@ -212,14 +255,14 @@ TEST(SparseSetNoType, RespectOverlap) {
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(clhs.get(3), 0u);
|
||||
ASSERT_EQ(clhs.get(12), 2u);
|
||||
ASSERT_EQ(clhs.get(42), 1u);
|
||||
ASSERT_EQ(std::as_const(lhs).get(3), 0u);
|
||||
ASSERT_EQ(std::as_const(lhs).get(12), 2u);
|
||||
ASSERT_EQ(std::as_const(lhs).get(42), 1u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectOrdered) {
|
||||
entt::SparseSet<std::uint64_t> lhs;
|
||||
entt::SparseSet<std::uint64_t> rhs;
|
||||
TEST(SparseSet, RespectOrdered) {
|
||||
entt::sparse_set<std::uint64_t> lhs;
|
||||
entt::sparse_set<std::uint64_t> rhs;
|
||||
|
||||
lhs.construct(1);
|
||||
lhs.construct(2);
|
||||
@@ -257,9 +300,9 @@ TEST(SparseSetNoType, RespectOrdered) {
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectReverse) {
|
||||
entt::SparseSet<std::uint64_t> lhs;
|
||||
entt::SparseSet<std::uint64_t> rhs;
|
||||
TEST(SparseSet, RespectReverse) {
|
||||
entt::sparse_set<std::uint64_t> lhs;
|
||||
entt::sparse_set<std::uint64_t> rhs;
|
||||
|
||||
lhs.construct(1);
|
||||
lhs.construct(2);
|
||||
@@ -297,9 +340,9 @@ TEST(SparseSetNoType, RespectReverse) {
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, RespectUnordered) {
|
||||
entt::SparseSet<std::uint64_t> lhs;
|
||||
entt::SparseSet<std::uint64_t> rhs;
|
||||
TEST(SparseSet, RespectUnordered) {
|
||||
entt::sparse_set<std::uint64_t> lhs;
|
||||
entt::sparse_set<std::uint64_t> rhs;
|
||||
|
||||
lhs.construct(1);
|
||||
lhs.construct(2);
|
||||
@@ -337,565 +380,18 @@ TEST(SparseSetNoType, RespectUnordered) {
|
||||
ASSERT_EQ(rhs.get(5), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetNoType, CanModifyDuringIteration) {
|
||||
entt::SparseSet<std::uint64_t> set;
|
||||
TEST(SparseSet, CanModifyDuringIteration) {
|
||||
entt::sparse_set<std::uint64_t> set;
|
||||
set.construct(0);
|
||||
|
||||
ASSERT_EQ(set.capacity(), entt::SparseSet<std::uint64_t>::size_type{1});
|
||||
ASSERT_EQ(set.capacity(), entt::sparse_set<std::uint64_t>::size_type{1});
|
||||
|
||||
const auto it = set.cbegin();
|
||||
set.reserve(entt::SparseSet<std::uint64_t>::size_type{2});
|
||||
const auto it = set.begin();
|
||||
set.reserve(entt::sparse_set<std::uint64_t>::size_type{2});
|
||||
|
||||
ASSERT_EQ(set.capacity(), entt::SparseSet<std::uint64_t>::size_type{2});
|
||||
ASSERT_EQ(set.capacity(), entt::sparse_set<std::uint64_t>::size_type{2});
|
||||
|
||||
// this should crash with asan enabled if we break the constraint
|
||||
const auto entity = *it;
|
||||
(void)entity;
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, Functionalities) {
|
||||
entt::SparseSet<std::uint64_t, int> set;
|
||||
const auto &cset = set;
|
||||
|
||||
set.reserve(42);
|
||||
|
||||
ASSERT_EQ(set.capacity(), 42);
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(cset.begin(), cset.end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
set.construct(42, 3);
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 1u);
|
||||
ASSERT_NE(cset.begin(), cset.end());
|
||||
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);
|
||||
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(cset.begin(), cset.end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
set.construct(42, 12);
|
||||
|
||||
ASSERT_EQ(set.get(42), 12);
|
||||
|
||||
set.reset();
|
||||
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(cset.begin(), cset.end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(42));
|
||||
|
||||
(void)entt::SparseSet<std::uint64_t, int>{std::move(set)};
|
||||
entt::SparseSet<std::uint64_t, int> other;
|
||||
other = std::move(set);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, ElementAccess) {
|
||||
entt::SparseSet<std::uint64_t, int> set;
|
||||
const auto &cset = set;
|
||||
|
||||
set.construct(42, 1);
|
||||
set.construct(3, 0);
|
||||
|
||||
for(typename entt::SparseSet<std::uint64_t, int>::size_type i{}; i < set.size(); ++i) {
|
||||
ASSERT_EQ(set[i], i);
|
||||
ASSERT_EQ(cset[i], i);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, AggregatesMustWork) {
|
||||
struct AggregateType { int value; };
|
||||
// the goal of this test is to enforce the requirements for aggregate types
|
||||
entt::SparseSet<std::uint64_t, AggregateType>{}.construct(0, 42);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, TypesFromStandardTemplateLibraryMustWork) {
|
||||
// see #37 - this test shouldn't crash, that's all
|
||||
entt::SparseSet<std::uint64_t, std::unordered_set<int>> set;
|
||||
set.construct(0).insert(42);
|
||||
set.destroy(0);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, Iterator) {
|
||||
struct InternalType { int value; };
|
||||
|
||||
using iterator_type = typename entt::SparseSet<std::uint64_t, InternalType>::iterator_type;
|
||||
|
||||
entt::SparseSet<std::uint64_t, InternalType> set;
|
||||
set.construct(3, 42);
|
||||
|
||||
iterator_type end{set.begin()};
|
||||
iterator_type begin{};
|
||||
begin = set.end();
|
||||
std::swap(begin, end);
|
||||
|
||||
ASSERT_EQ(begin, set.begin());
|
||||
ASSERT_EQ(end, set.end());
|
||||
ASSERT_NE(begin, end);
|
||||
|
||||
ASSERT_EQ(begin++, set.begin());
|
||||
ASSERT_EQ(begin--, set.end());
|
||||
|
||||
ASSERT_EQ(begin+1, set.end());
|
||||
ASSERT_EQ(end-1, set.begin());
|
||||
|
||||
ASSERT_EQ(++begin, set.end());
|
||||
ASSERT_EQ(--begin, set.begin());
|
||||
|
||||
ASSERT_EQ(begin += 1, set.end());
|
||||
ASSERT_EQ(begin -= 1, set.begin());
|
||||
|
||||
ASSERT_EQ(begin + (end - begin), set.end());
|
||||
ASSERT_EQ(begin - (begin - end), set.end());
|
||||
|
||||
ASSERT_EQ(end - (end - begin), set.begin());
|
||||
ASSERT_EQ(end + (begin - end), set.begin());
|
||||
|
||||
ASSERT_EQ(begin[0].value, set.begin()->value);
|
||||
|
||||
ASSERT_LT(begin, end);
|
||||
ASSERT_LE(begin, set.begin());
|
||||
|
||||
ASSERT_GT(end, begin);
|
||||
ASSERT_GE(end, set.end());
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, ConstIterator) {
|
||||
struct InternalType { int value; };
|
||||
|
||||
using iterator_type = typename entt::SparseSet<std::uint64_t, InternalType>::const_iterator_type;
|
||||
|
||||
entt::SparseSet<std::uint64_t, InternalType> set;
|
||||
set.construct(3, 42);
|
||||
|
||||
iterator_type cend{set.cbegin()};
|
||||
iterator_type cbegin{};
|
||||
cbegin = set.cend();
|
||||
std::swap(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(cbegin, set.cbegin());
|
||||
ASSERT_EQ(cend, set.cend());
|
||||
ASSERT_NE(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(cbegin++, set.cbegin());
|
||||
ASSERT_EQ(cbegin--, set.cend());
|
||||
|
||||
ASSERT_EQ(cbegin+1, set.cend());
|
||||
ASSERT_EQ(cend-1, set.cbegin());
|
||||
|
||||
ASSERT_EQ(++cbegin, set.cend());
|
||||
ASSERT_EQ(--cbegin, set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin += 1, set.cend());
|
||||
ASSERT_EQ(cbegin -= 1, set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin + (cend - cbegin), set.cend());
|
||||
ASSERT_EQ(cbegin - (cbegin - cend), set.cend());
|
||||
|
||||
ASSERT_EQ(cend - (cend - cbegin), set.cbegin());
|
||||
ASSERT_EQ(cend + (cbegin - cend), set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin[0].value, set.cbegin()->value);
|
||||
|
||||
ASSERT_LT(cbegin, cend);
|
||||
ASSERT_LE(cbegin, set.cbegin());
|
||||
|
||||
ASSERT_GT(cend, cbegin);
|
||||
ASSERT_GE(cend, set.cend());
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, Raw) {
|
||||
entt::SparseSet<std::uint64_t, int> set;
|
||||
|
||||
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);
|
||||
ASSERT_EQ(*(set.raw() + 2u), 9);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, SortOrdered) {
|
||||
entt::SparseSet<std::uint64_t, int> set;
|
||||
|
||||
set.construct(12, 12);
|
||||
set.construct(42, 9);
|
||||
set.construct(7, 6);
|
||||
set.construct(3, 3);
|
||||
set.construct(9, 1);
|
||||
|
||||
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);
|
||||
ASSERT_EQ(*(set.raw() + 1u), 9);
|
||||
ASSERT_EQ(*(set.raw() + 2u), 6);
|
||||
ASSERT_EQ(*(set.raw() + 3u), 3);
|
||||
ASSERT_EQ(*(set.raw() + 4u), 1);
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
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<std::uint64_t, int> set;
|
||||
|
||||
set.construct(12, 1);
|
||||
set.construct(42, 3);
|
||||
set.construct(7, 6);
|
||||
set.construct(3, 9);
|
||||
set.construct(9, 12);
|
||||
|
||||
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);
|
||||
ASSERT_EQ(*(set.raw() + 1u), 9);
|
||||
ASSERT_EQ(*(set.raw() + 2u), 6);
|
||||
ASSERT_EQ(*(set.raw() + 3u), 3);
|
||||
ASSERT_EQ(*(set.raw() + 4u), 1);
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
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<std::uint64_t, int> set;
|
||||
|
||||
set.construct(12, 6);
|
||||
set.construct(42, 3);
|
||||
set.construct(7, 1);
|
||||
set.construct(3, 9);
|
||||
set.construct(9, 12);
|
||||
|
||||
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);
|
||||
ASSERT_EQ(*(set.raw() + 1u), 9);
|
||||
ASSERT_EQ(*(set.raw() + 2u), 6);
|
||||
ASSERT_EQ(*(set.raw() + 3u), 3);
|
||||
ASSERT_EQ(*(set.raw() + 4u), 1);
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
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, RespectDisjoint) {
|
||||
entt::SparseSet<std::uint64_t, int> lhs;
|
||||
entt::SparseSet<std::uint64_t, int> rhs;
|
||||
const auto &clhs = lhs;
|
||||
|
||||
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);
|
||||
|
||||
ASSERT_EQ(*(clhs.raw() + 0u), 3);
|
||||
ASSERT_EQ(*(clhs.raw() + 1u), 6);
|
||||
ASSERT_EQ(*(clhs.raw() + 2u), 9);
|
||||
|
||||
auto begin = lhs.begin();
|
||||
auto end = lhs.end();
|
||||
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, RespectOverlap) {
|
||||
entt::SparseSet<std::uint64_t, int> lhs;
|
||||
entt::SparseSet<std::uint64_t, int> rhs;
|
||||
const auto &clhs = lhs;
|
||||
|
||||
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);
|
||||
|
||||
ASSERT_EQ(*(clhs.raw() + 0u), 3);
|
||||
ASSERT_EQ(*(clhs.raw() + 1u), 9);
|
||||
ASSERT_EQ(*(clhs.raw() + 2u), 6);
|
||||
|
||||
auto begin = lhs.begin();
|
||||
auto end = lhs.end();
|
||||
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, RespectOrdered) {
|
||||
entt::SparseSet<std::uint64_t, int> lhs;
|
||||
entt::SparseSet<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 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);
|
||||
|
||||
ASSERT_EQ(*(lhs.data() + 0u), 1u);
|
||||
ASSERT_EQ(*(lhs.data() + 1u), 2u);
|
||||
ASSERT_EQ(*(lhs.data() + 2u), 3u);
|
||||
ASSERT_EQ(*(lhs.data() + 3u), 4u);
|
||||
ASSERT_EQ(*(lhs.data() + 4u), 5u);
|
||||
|
||||
ASSERT_EQ(*(rhs.data() + 0u), 6u);
|
||||
ASSERT_EQ(*(rhs.data() + 1u), 1u);
|
||||
ASSERT_EQ(*(rhs.data() + 2u), 2u);
|
||||
ASSERT_EQ(*(rhs.data() + 3u), 3u);
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, RespectReverse) {
|
||||
entt::SparseSet<std::uint64_t, int> lhs;
|
||||
entt::SparseSet<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 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);
|
||||
|
||||
ASSERT_EQ(*(lhs.data() + 0u), 1u);
|
||||
ASSERT_EQ(*(lhs.data() + 1u), 2u);
|
||||
ASSERT_EQ(*(lhs.data() + 2u), 3u);
|
||||
ASSERT_EQ(*(lhs.data() + 3u), 4u);
|
||||
ASSERT_EQ(*(lhs.data() + 4u), 5u);
|
||||
|
||||
ASSERT_EQ(*(rhs.data() + 0u), 6u);
|
||||
ASSERT_EQ(*(rhs.data() + 1u), 1u);
|
||||
ASSERT_EQ(*(rhs.data() + 2u), 2u);
|
||||
ASSERT_EQ(*(rhs.data() + 3u), 3u);
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, RespectUnordered) {
|
||||
entt::SparseSet<std::uint64_t, int> lhs;
|
||||
entt::SparseSet<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 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);
|
||||
|
||||
ASSERT_EQ(*(lhs.data() + 0u), 1u);
|
||||
ASSERT_EQ(*(lhs.data() + 1u), 2u);
|
||||
ASSERT_EQ(*(lhs.data() + 2u), 3u);
|
||||
ASSERT_EQ(*(lhs.data() + 3u), 4u);
|
||||
ASSERT_EQ(*(lhs.data() + 4u), 5u);
|
||||
|
||||
ASSERT_EQ(*(rhs.data() + 0u), 6u);
|
||||
ASSERT_EQ(*(rhs.data() + 1u), 1u);
|
||||
ASSERT_EQ(*(rhs.data() + 2u), 2u);
|
||||
ASSERT_EQ(*(rhs.data() + 3u), 3u);
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, CanModifyDuringIteration) {
|
||||
entt::SparseSet<std::uint64_t, int> set;
|
||||
set.construct(0, 42);
|
||||
|
||||
ASSERT_EQ(set.capacity(), entt::SparseSet<std::uint64_t>::size_type{1});
|
||||
|
||||
const auto it = set.cbegin();
|
||||
set.reserve(entt::SparseSet<std::uint64_t>::size_type{2});
|
||||
|
||||
ASSERT_EQ(set.capacity(), entt::SparseSet<std::uint64_t>::size_type{2});
|
||||
|
||||
// this should crash with asan enabled if we break the constraint
|
||||
const auto entity = *it;
|
||||
(void)entity;
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, ReferencesGuaranteed) {
|
||||
struct InternalType { int value; };
|
||||
|
||||
entt::SparseSet<std::uint64_t, InternalType> 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);
|
||||
}
|
||||
|
||||
TEST(SparseSetWithType, MoveOnlyComponent) {
|
||||
struct MoveOnlyComponent {
|
||||
MoveOnlyComponent() = default;
|
||||
~MoveOnlyComponent() = default;
|
||||
MoveOnlyComponent(const MoveOnlyComponent &) = delete;
|
||||
MoveOnlyComponent(MoveOnlyComponent &&) = default;
|
||||
MoveOnlyComponent & operator=(const MoveOnlyComponent &) = delete;
|
||||
MoveOnlyComponent & operator=(MoveOnlyComponent &&) = default;
|
||||
};
|
||||
|
||||
// it's purpose is to ensure that move only components are always accepted
|
||||
entt::SparseSet<std::uint64_t, MoveOnlyComponent> set;
|
||||
(void)set;
|
||||
}
|
||||
|
||||
697
test/entt/entity/storage.cpp
Normal file
697
test/entt/entity/storage.cpp
Normal file
@@ -0,0 +1,697 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <exception>
|
||||
#include <type_traits>
|
||||
#include <unordered_set>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/storage.hpp>
|
||||
|
||||
struct empty_type {};
|
||||
struct boxed_int { int value; };
|
||||
|
||||
struct throwing_component {
|
||||
struct constructor_exception: std::exception {};
|
||||
|
||||
[[noreturn]] throwing_component() { throw constructor_exception{}; }
|
||||
|
||||
// necessary to avoid the short-circuit construct() logic for empty objects
|
||||
int data;
|
||||
};
|
||||
|
||||
TEST(Storage, Functionalities) {
|
||||
entt::storage<std::uint64_t, int> set;
|
||||
|
||||
set.reserve(42);
|
||||
|
||||
ASSERT_EQ(set.capacity(), 42);
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(std::as_const(set).begin(), std::as_const(set).end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(41));
|
||||
|
||||
set.construct(41, 3);
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 1u);
|
||||
ASSERT_NE(std::as_const(set).begin(), std::as_const(set).end());
|
||||
ASSERT_NE(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_TRUE(set.has(41));
|
||||
ASSERT_EQ(set.get(41), 3);
|
||||
ASSERT_EQ(*set.try_get(41), 3);
|
||||
ASSERT_EQ(set.try_get(99), nullptr);
|
||||
|
||||
set.destroy(41);
|
||||
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(std::as_const(set).begin(), std::as_const(set).end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(41));
|
||||
|
||||
set.construct(41, 12);
|
||||
|
||||
ASSERT_EQ(set.get(41), 12);
|
||||
ASSERT_EQ(*set.try_get(41), 12);
|
||||
ASSERT_EQ(set.try_get(99), nullptr);
|
||||
|
||||
set.reset();
|
||||
|
||||
ASSERT_TRUE(set.empty());
|
||||
ASSERT_EQ(set.size(), 0u);
|
||||
ASSERT_EQ(std::as_const(set).begin(), std::as_const(set).end());
|
||||
ASSERT_EQ(set.begin(), set.end());
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(41));
|
||||
|
||||
ASSERT_EQ(set.capacity(), 42);
|
||||
|
||||
set.shrink_to_fit();
|
||||
|
||||
ASSERT_EQ(set.capacity(), 0);
|
||||
|
||||
(void)entt::storage<std::uint64_t, int>{std::move(set)};
|
||||
entt::storage<std::uint64_t, int> other;
|
||||
other = std::move(set);
|
||||
}
|
||||
|
||||
TEST(Storage, EmptyType) {
|
||||
entt::storage<std::uint64_t, empty_type> set;
|
||||
|
||||
set.construct(42);
|
||||
set.construct(99);
|
||||
|
||||
ASSERT_TRUE(set.has(42));
|
||||
ASSERT_TRUE(set.has(99));
|
||||
|
||||
auto &&component = set.get(42);
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<decltype(component), empty_type &&>));
|
||||
}
|
||||
|
||||
TEST(Storage, BatchAdd) {
|
||||
entt::storage<std::uint64_t, int> set;
|
||||
entt::storage<std::uint64_t, int>::entity_type entities[2];
|
||||
|
||||
entities[0] = 3;
|
||||
entities[1] = 42;
|
||||
|
||||
set.reserve(4);
|
||||
set.construct(12, 21);
|
||||
auto *component = set.batch(std::begin(entities), std::end(entities));
|
||||
set.construct(24, 42);
|
||||
|
||||
ASSERT_TRUE(set.has(entities[0]));
|
||||
ASSERT_TRUE(set.has(entities[1]));
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(9));
|
||||
ASSERT_TRUE(set.has(12));
|
||||
ASSERT_TRUE(set.has(24));
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 4u);
|
||||
ASSERT_EQ(set.get(12), 21);
|
||||
ASSERT_EQ(set.get(entities[0]), 0);
|
||||
ASSERT_EQ(set.get(entities[1]), 0);
|
||||
ASSERT_EQ(set.get(24), 42);
|
||||
|
||||
component[0] = 1;
|
||||
component[1] = 2;
|
||||
|
||||
ASSERT_EQ(set.get(entities[0]), 1);
|
||||
ASSERT_EQ(set.get(entities[1]), 2);
|
||||
}
|
||||
|
||||
TEST(Storage, BatchAddEmptyType) {
|
||||
entt::storage<std::uint64_t, empty_type> set;
|
||||
entt::storage<std::uint64_t, empty_type>::entity_type entities[2];
|
||||
|
||||
entities[0] = 3;
|
||||
entities[1] = 42;
|
||||
|
||||
set.reserve(4);
|
||||
set.construct(12);
|
||||
set.batch(std::begin(entities), std::end(entities));
|
||||
set.construct(24);
|
||||
|
||||
ASSERT_TRUE(set.has(entities[0]));
|
||||
ASSERT_TRUE(set.has(entities[1]));
|
||||
ASSERT_FALSE(set.has(0));
|
||||
ASSERT_FALSE(set.has(9));
|
||||
ASSERT_TRUE(set.has(12));
|
||||
ASSERT_TRUE(set.has(24));
|
||||
|
||||
ASSERT_FALSE(set.empty());
|
||||
ASSERT_EQ(set.size(), 4u);
|
||||
|
||||
auto &&component = set.get(entities[0]);
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<decltype(component), empty_type &&>));
|
||||
}
|
||||
|
||||
TEST(Storage, AggregatesMustWork) {
|
||||
struct aggregate_type { int value; };
|
||||
// the goal of this test is to enforce the requirements for aggregate types
|
||||
entt::storage<std::uint64_t, aggregate_type>{}.construct(0, 42);
|
||||
}
|
||||
|
||||
TEST(Storage, TypesFromStandardTemplateLibraryMustWork) {
|
||||
// see #37 - this test shouldn't crash, that's all
|
||||
entt::storage<std::uint64_t, std::unordered_set<int>> set;
|
||||
set.construct(0).insert(42);
|
||||
set.destroy(0);
|
||||
}
|
||||
|
||||
TEST(Storage, Iterator) {
|
||||
using iterator_type = typename entt::storage<std::uint64_t, boxed_int>::iterator_type;
|
||||
|
||||
entt::storage<std::uint64_t, boxed_int> set;
|
||||
set.construct(3, 42);
|
||||
|
||||
iterator_type end{set.begin()};
|
||||
iterator_type begin{};
|
||||
begin = set.end();
|
||||
std::swap(begin, end);
|
||||
|
||||
ASSERT_EQ(begin, set.begin());
|
||||
ASSERT_EQ(end, set.end());
|
||||
ASSERT_NE(begin, end);
|
||||
|
||||
ASSERT_EQ(begin++, set.begin());
|
||||
ASSERT_EQ(begin--, set.end());
|
||||
|
||||
ASSERT_EQ(begin+1, set.end());
|
||||
ASSERT_EQ(end-1, set.begin());
|
||||
|
||||
ASSERT_EQ(++begin, set.end());
|
||||
ASSERT_EQ(--begin, set.begin());
|
||||
|
||||
ASSERT_EQ(begin += 1, set.end());
|
||||
ASSERT_EQ(begin -= 1, set.begin());
|
||||
|
||||
ASSERT_EQ(begin + (end - begin), set.end());
|
||||
ASSERT_EQ(begin - (begin - end), set.end());
|
||||
|
||||
ASSERT_EQ(end - (end - begin), set.begin());
|
||||
ASSERT_EQ(end + (begin - end), set.begin());
|
||||
|
||||
ASSERT_EQ(begin[0].value, set.begin()->value);
|
||||
|
||||
ASSERT_LT(begin, end);
|
||||
ASSERT_LE(begin, set.begin());
|
||||
|
||||
ASSERT_GT(end, begin);
|
||||
ASSERT_GE(end, set.end());
|
||||
}
|
||||
|
||||
TEST(Storage, ConstIterator) {
|
||||
using iterator_type = typename entt::storage<std::uint64_t, boxed_int>::const_iterator_type;
|
||||
|
||||
entt::storage<std::uint64_t, boxed_int> set;
|
||||
set.construct(3, 42);
|
||||
|
||||
iterator_type cend{set.cbegin()};
|
||||
iterator_type cbegin{};
|
||||
cbegin = set.cend();
|
||||
std::swap(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(cbegin, set.cbegin());
|
||||
ASSERT_EQ(cend, set.cend());
|
||||
ASSERT_NE(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(cbegin++, set.cbegin());
|
||||
ASSERT_EQ(cbegin--, set.cend());
|
||||
|
||||
ASSERT_EQ(cbegin+1, set.cend());
|
||||
ASSERT_EQ(cend-1, set.cbegin());
|
||||
|
||||
ASSERT_EQ(++cbegin, set.cend());
|
||||
ASSERT_EQ(--cbegin, set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin += 1, set.cend());
|
||||
ASSERT_EQ(cbegin -= 1, set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin + (cend - cbegin), set.cend());
|
||||
ASSERT_EQ(cbegin - (cbegin - cend), set.cend());
|
||||
|
||||
ASSERT_EQ(cend - (cend - cbegin), set.cbegin());
|
||||
ASSERT_EQ(cend + (cbegin - cend), set.cbegin());
|
||||
|
||||
ASSERT_EQ(cbegin[0].value, set.cbegin()->value);
|
||||
|
||||
ASSERT_LT(cbegin, cend);
|
||||
ASSERT_LE(cbegin, set.cbegin());
|
||||
|
||||
ASSERT_GT(cend, cbegin);
|
||||
ASSERT_GE(cend, set.cend());
|
||||
}
|
||||
|
||||
TEST(Storage, IteratorEmptyType) {
|
||||
using iterator_type = typename entt::storage<std::uint64_t, empty_type>::iterator_type;
|
||||
entt::storage<std::uint64_t, empty_type> set;
|
||||
set.construct(3);
|
||||
|
||||
iterator_type end{set.begin()};
|
||||
iterator_type begin{};
|
||||
begin = set.end();
|
||||
std::swap(begin, end);
|
||||
|
||||
ASSERT_EQ(begin, set.begin());
|
||||
ASSERT_EQ(end, set.end());
|
||||
ASSERT_NE(begin, end);
|
||||
|
||||
ASSERT_EQ(begin++, set.begin());
|
||||
ASSERT_EQ(begin--, set.end());
|
||||
|
||||
ASSERT_EQ(begin+1, set.end());
|
||||
ASSERT_EQ(end-1, set.begin());
|
||||
|
||||
ASSERT_EQ(++begin, set.end());
|
||||
ASSERT_EQ(--begin, set.begin());
|
||||
|
||||
ASSERT_EQ(begin += 1, set.end());
|
||||
ASSERT_EQ(begin -= 1, set.begin());
|
||||
|
||||
ASSERT_EQ(begin + (end - begin), set.end());
|
||||
ASSERT_EQ(begin - (begin - end), set.end());
|
||||
|
||||
ASSERT_EQ(end - (end - begin), set.begin());
|
||||
ASSERT_EQ(end + (begin - end), set.begin());
|
||||
|
||||
ASSERT_EQ(set.begin().operator->(), nullptr);
|
||||
|
||||
ASSERT_LT(begin, end);
|
||||
ASSERT_LE(begin, set.begin());
|
||||
|
||||
ASSERT_GT(end, begin);
|
||||
ASSERT_GE(end, set.end());
|
||||
|
||||
set.construct(33);
|
||||
auto &&component = *begin;
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<decltype(component), empty_type &&>));
|
||||
}
|
||||
|
||||
TEST(Storage, Raw) {
|
||||
entt::storage<std::uint64_t, int> set;
|
||||
|
||||
set.construct(3, 3);
|
||||
set.construct(12, 6);
|
||||
set.construct(42, 9);
|
||||
|
||||
ASSERT_EQ(set.get(3), 3);
|
||||
ASSERT_EQ(std::as_const(set).get(12), 6);
|
||||
ASSERT_EQ(set.get(42), 9);
|
||||
|
||||
ASSERT_EQ(*(set.raw() + 0u), 3);
|
||||
ASSERT_EQ(*(std::as_const(set).raw() + 1u), 6);
|
||||
ASSERT_EQ(*(set.raw() + 2u), 9);
|
||||
}
|
||||
|
||||
TEST(Storage, SortOrdered) {
|
||||
entt::storage<std::uint64_t, boxed_int> set;
|
||||
|
||||
set.construct(12, boxed_int{12});
|
||||
set.construct(42, boxed_int{9});
|
||||
set.construct(7, boxed_int{6});
|
||||
set.construct(3, boxed_int{3});
|
||||
set.construct(9, boxed_int{1});
|
||||
|
||||
ASSERT_EQ(set.get(12).value, 12);
|
||||
ASSERT_EQ(set.get(42).value, 9);
|
||||
ASSERT_EQ(set.get(7).value, 6);
|
||||
ASSERT_EQ(set.get(3).value, 3);
|
||||
ASSERT_EQ(set.get(9).value, 1);
|
||||
|
||||
set.sort([](auto lhs, auto rhs) {
|
||||
return lhs.value < rhs.value;
|
||||
});
|
||||
|
||||
ASSERT_EQ((set.raw() + 0u)->value, 12);
|
||||
ASSERT_EQ((set.raw() + 1u)->value, 9);
|
||||
ASSERT_EQ((set.raw() + 2u)->value, 6);
|
||||
ASSERT_EQ((set.raw() + 3u)->value, 3);
|
||||
ASSERT_EQ((set.raw() + 4u)->value, 1);
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ((begin++)->value, 1);
|
||||
ASSERT_EQ((begin++)->value, 3);
|
||||
ASSERT_EQ((begin++)->value, 6);
|
||||
ASSERT_EQ((begin++)->value, 9);
|
||||
ASSERT_EQ((begin++)->value, 12);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(Storage, SortReverse) {
|
||||
entt::storage<std::uint64_t, boxed_int> set;
|
||||
|
||||
set.construct(12, boxed_int{1});
|
||||
set.construct(42, boxed_int{3});
|
||||
set.construct(7, boxed_int{6});
|
||||
set.construct(3, boxed_int{9});
|
||||
set.construct(9, boxed_int{12});
|
||||
|
||||
ASSERT_EQ(set.get(12).value, 1);
|
||||
ASSERT_EQ(set.get(42).value, 3);
|
||||
ASSERT_EQ(set.get(7).value, 6);
|
||||
ASSERT_EQ(set.get(3).value, 9);
|
||||
ASSERT_EQ(set.get(9).value, 12);
|
||||
|
||||
set.sort([&set](std::uint64_t lhs, std::uint64_t rhs) {
|
||||
return set.get(lhs).value < set.get(rhs).value;
|
||||
});
|
||||
|
||||
ASSERT_EQ((set.raw() + 0u)->value, 12);
|
||||
ASSERT_EQ((set.raw() + 1u)->value, 9);
|
||||
ASSERT_EQ((set.raw() + 2u)->value, 6);
|
||||
ASSERT_EQ((set.raw() + 3u)->value, 3);
|
||||
ASSERT_EQ((set.raw() + 4u)->value, 1);
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ((begin++)->value, 1);
|
||||
ASSERT_EQ((begin++)->value, 3);
|
||||
ASSERT_EQ((begin++)->value, 6);
|
||||
ASSERT_EQ((begin++)->value, 9);
|
||||
ASSERT_EQ((begin++)->value, 12);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(Storage, SortUnordered) {
|
||||
entt::storage<std::uint64_t, boxed_int> set;
|
||||
|
||||
set.construct(12, boxed_int{6});
|
||||
set.construct(42, boxed_int{3});
|
||||
set.construct(7, boxed_int{1});
|
||||
set.construct(3, boxed_int{9});
|
||||
set.construct(9, boxed_int{12});
|
||||
|
||||
ASSERT_EQ(set.get(12).value, 6);
|
||||
ASSERT_EQ(set.get(42).value, 3);
|
||||
ASSERT_EQ(set.get(7).value, 1);
|
||||
ASSERT_EQ(set.get(3).value, 9);
|
||||
ASSERT_EQ(set.get(9).value, 12);
|
||||
|
||||
set.sort([](auto lhs, auto rhs) {
|
||||
return lhs.value < rhs.value;
|
||||
});
|
||||
|
||||
ASSERT_EQ((set.raw() + 0u)->value, 12);
|
||||
ASSERT_EQ((set.raw() + 1u)->value, 9);
|
||||
ASSERT_EQ((set.raw() + 2u)->value, 6);
|
||||
ASSERT_EQ((set.raw() + 3u)->value, 3);
|
||||
ASSERT_EQ((set.raw() + 4u)->value, 1);
|
||||
|
||||
auto begin = set.begin();
|
||||
auto end = set.end();
|
||||
|
||||
ASSERT_EQ((begin++)->value, 1);
|
||||
ASSERT_EQ((begin++)->value, 3);
|
||||
ASSERT_EQ((begin++)->value, 6);
|
||||
ASSERT_EQ((begin++)->value, 9);
|
||||
ASSERT_EQ((begin++)->value, 12);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(Storage, RespectDisjoint) {
|
||||
entt::storage<std::uint64_t, int> lhs;
|
||||
entt::storage<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(3, 3);
|
||||
lhs.construct(12, 6);
|
||||
lhs.construct(42, 9);
|
||||
|
||||
ASSERT_EQ(std::as_const(lhs).get(3), 3);
|
||||
ASSERT_EQ(std::as_const(lhs).get(12), 6);
|
||||
ASSERT_EQ(std::as_const(lhs).get(42), 9);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(*(std::as_const(lhs).raw() + 0u), 3);
|
||||
ASSERT_EQ(*(std::as_const(lhs).raw() + 1u), 6);
|
||||
ASSERT_EQ(*(std::as_const(lhs).raw() + 2u), 9);
|
||||
|
||||
auto begin = lhs.begin();
|
||||
auto end = lhs.end();
|
||||
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(Storage, RespectOverlap) {
|
||||
entt::storage<std::uint64_t, int> lhs;
|
||||
entt::storage<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(3, 3);
|
||||
lhs.construct(12, 6);
|
||||
lhs.construct(42, 9);
|
||||
rhs.construct(12, 6);
|
||||
|
||||
ASSERT_EQ(std::as_const(lhs).get(3), 3);
|
||||
ASSERT_EQ(std::as_const(lhs).get(12), 6);
|
||||
ASSERT_EQ(std::as_const(lhs).get(42), 9);
|
||||
ASSERT_EQ(rhs.get(12), 6);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(*(std::as_const(lhs).raw() + 0u), 3);
|
||||
ASSERT_EQ(*(std::as_const(lhs).raw() + 1u), 9);
|
||||
ASSERT_EQ(*(std::as_const(lhs).raw() + 2u), 6);
|
||||
|
||||
auto begin = lhs.begin();
|
||||
auto end = lhs.end();
|
||||
|
||||
ASSERT_EQ(*(begin++), 6);
|
||||
ASSERT_EQ(*(begin++), 9);
|
||||
ASSERT_EQ(*(begin++), 3);
|
||||
ASSERT_EQ(begin, end);
|
||||
}
|
||||
|
||||
TEST(Storage, RespectOrdered) {
|
||||
entt::storage<std::uint64_t, int> lhs;
|
||||
entt::storage<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 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);
|
||||
|
||||
ASSERT_EQ(*(lhs.data() + 0u), 1u);
|
||||
ASSERT_EQ(*(lhs.data() + 1u), 2u);
|
||||
ASSERT_EQ(*(lhs.data() + 2u), 3u);
|
||||
ASSERT_EQ(*(lhs.data() + 3u), 4u);
|
||||
ASSERT_EQ(*(lhs.data() + 4u), 5u);
|
||||
|
||||
ASSERT_EQ(*(rhs.data() + 0u), 6u);
|
||||
ASSERT_EQ(*(rhs.data() + 1u), 1u);
|
||||
ASSERT_EQ(*(rhs.data() + 2u), 2u);
|
||||
ASSERT_EQ(*(rhs.data() + 3u), 3u);
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(Storage, RespectReverse) {
|
||||
entt::storage<std::uint64_t, int> lhs;
|
||||
entt::storage<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 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);
|
||||
|
||||
ASSERT_EQ(*(lhs.data() + 0u), 1u);
|
||||
ASSERT_EQ(*(lhs.data() + 1u), 2u);
|
||||
ASSERT_EQ(*(lhs.data() + 2u), 3u);
|
||||
ASSERT_EQ(*(lhs.data() + 3u), 4u);
|
||||
ASSERT_EQ(*(lhs.data() + 4u), 5u);
|
||||
|
||||
ASSERT_EQ(*(rhs.data() + 0u), 6u);
|
||||
ASSERT_EQ(*(rhs.data() + 1u), 1u);
|
||||
ASSERT_EQ(*(rhs.data() + 2u), 2u);
|
||||
ASSERT_EQ(*(rhs.data() + 3u), 3u);
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(Storage, RespectUnordered) {
|
||||
entt::storage<std::uint64_t, int> lhs;
|
||||
entt::storage<std::uint64_t, int> rhs;
|
||||
|
||||
lhs.construct(1, 0);
|
||||
lhs.construct(2, 0);
|
||||
lhs.construct(3, 0);
|
||||
lhs.construct(4, 0);
|
||||
lhs.construct(5, 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);
|
||||
|
||||
ASSERT_EQ(*(lhs.data() + 0u), 1u);
|
||||
ASSERT_EQ(*(lhs.data() + 1u), 2u);
|
||||
ASSERT_EQ(*(lhs.data() + 2u), 3u);
|
||||
ASSERT_EQ(*(lhs.data() + 3u), 4u);
|
||||
ASSERT_EQ(*(lhs.data() + 4u), 5u);
|
||||
|
||||
ASSERT_EQ(*(rhs.data() + 0u), 6u);
|
||||
ASSERT_EQ(*(rhs.data() + 1u), 1u);
|
||||
ASSERT_EQ(*(rhs.data() + 2u), 2u);
|
||||
ASSERT_EQ(*(rhs.data() + 3u), 3u);
|
||||
ASSERT_EQ(*(rhs.data() + 4u), 4u);
|
||||
ASSERT_EQ(*(rhs.data() + 5u), 5u);
|
||||
}
|
||||
|
||||
TEST(Storage, RespectOverlapEmptyType) {
|
||||
entt::storage<std::uint64_t, empty_type> lhs;
|
||||
entt::storage<std::uint64_t, empty_type> rhs;
|
||||
|
||||
lhs.construct(3);
|
||||
lhs.construct(12);
|
||||
lhs.construct(42);
|
||||
|
||||
rhs.construct(12);
|
||||
|
||||
ASSERT_EQ(lhs.sparse_set<std::uint64_t>::get(3), 0u);
|
||||
ASSERT_EQ(lhs.sparse_set<std::uint64_t>::get(12), 1u);
|
||||
ASSERT_EQ(lhs.sparse_set<std::uint64_t>::get(42), 2u);
|
||||
|
||||
lhs.respect(rhs);
|
||||
|
||||
ASSERT_EQ(std::as_const(lhs).sparse_set<std::uint64_t>::get(3), 0u);
|
||||
ASSERT_EQ(std::as_const(lhs).sparse_set<std::uint64_t>::get(12), 2u);
|
||||
ASSERT_EQ(std::as_const(lhs).sparse_set<std::uint64_t>::get(42), 1u);
|
||||
}
|
||||
|
||||
TEST(Storage, CanModifyDuringIteration) {
|
||||
entt::storage<std::uint64_t, int> set;
|
||||
set.construct(0, 42);
|
||||
|
||||
ASSERT_EQ(set.capacity(), (entt::storage<std::uint64_t, int>::size_type{1}));
|
||||
|
||||
const auto it = set.cbegin();
|
||||
set.reserve(entt::storage<std::uint64_t, int>::size_type{2});
|
||||
|
||||
ASSERT_EQ(set.capacity(), (entt::storage<std::uint64_t, int>::size_type{2}));
|
||||
|
||||
// this should crash with asan enabled if we break the constraint
|
||||
const auto entity = *it;
|
||||
(void)entity;
|
||||
}
|
||||
|
||||
TEST(Storage, ReferencesGuaranteed) {
|
||||
entt::storage<std::uint64_t, boxed_int> 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);
|
||||
}
|
||||
|
||||
TEST(Storage, MoveOnlyComponent) {
|
||||
// the purpose is to ensure that move only components are always accepted
|
||||
entt::storage<std::uint64_t, std::unique_ptr<int>> set;
|
||||
(void)set;
|
||||
}
|
||||
|
||||
TEST(Storage, ConstructorExceptionDoesNotAddToSet) {
|
||||
entt::storage<std::uint64_t, throwing_component> set;
|
||||
|
||||
try {
|
||||
set.construct(0);
|
||||
FAIL() << "Expected constructor_exception to be thrown";
|
||||
} catch (const throwing_component::constructor_exception &) {
|
||||
ASSERT_TRUE(set.empty());
|
||||
}
|
||||
}
|
||||
@@ -1,235 +1,13 @@
|
||||
#include <utility>
|
||||
#include <iterator>
|
||||
#include <type_traits>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/entity/registry.hpp>
|
||||
#include <entt/entity/view.hpp>
|
||||
|
||||
TEST(PersistentView, Prepare) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, char>();
|
||||
auto view = registry.view<int, char>(entt::persistent_t{});
|
||||
const auto &cview = view;
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
ASSERT_FALSE(view.empty());
|
||||
ASSERT_NO_THROW((registry.view<int, char>(entt::persistent_t{}).begin()++));
|
||||
ASSERT_NO_THROW((++registry.view<int, char>(entt::persistent_t{}).begin()));
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_NE(cview.begin(), cview.end());
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
registry.assign<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
|
||||
|
||||
registry.remove<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
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_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), e1);
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
ASSERT_EQ(view.cbegin(), view.cend());
|
||||
ASSERT_TRUE(view.empty());
|
||||
}
|
||||
|
||||
TEST(PersistentView, NoPrepare) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.view<int, char>(entt::persistent_t{});
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
ASSERT_FALSE(view.empty());
|
||||
ASSERT_NO_THROW((registry.view<int, char>(entt::persistent_t{}).begin()++));
|
||||
ASSERT_NO_THROW((++registry.view<int, char>(entt::persistent_t{}).begin()));
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
registry.assign<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{2});
|
||||
|
||||
registry.remove<int>(e0);
|
||||
|
||||
ASSERT_EQ(view.size(), typename decltype(view)::size_type{1});
|
||||
|
||||
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_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), e1);
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
ASSERT_EQ(view.cbegin(), view.cend());
|
||||
ASSERT_TRUE(view.empty());
|
||||
}
|
||||
|
||||
TEST(PersistentView, ElementAccess) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.view<int, char>(entt::persistent_t{});
|
||||
const auto &cview = view;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
for(typename decltype(view)::size_type i{}; i < view.size(); ++i) {
|
||||
ASSERT_EQ(view[i], i ? e0 : e1);
|
||||
ASSERT_EQ(cview[i], i ? e0 : e1);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PersistentView, Contains) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
auto view = registry.view<int, char>(entt::persistent_t{});
|
||||
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
ASSERT_TRUE(view.contains(e1));
|
||||
}
|
||||
|
||||
TEST(PersistentView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<double>(e0);
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<float>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<char>(e1);
|
||||
registry.assign<float>(e1);
|
||||
|
||||
for(auto entity: registry.view<char, int, float>(entt::persistent_t{})) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
|
||||
for(auto entity: registry.view<double, char, int, float>(entt::persistent_t{})) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PersistentView, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, char>();
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto view = registry.view<int, char>(entt::persistent_t{});
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto, int &, char &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
|
||||
cview.each([&cnt](auto, const int &, const char &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(PersistentView, Sort) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, unsigned int>();
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
const auto e2 = 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<int>(e0, ival++);
|
||||
registry.assign<int>(e1, ival++);
|
||||
registry.assign<int>(e2, ival++);
|
||||
|
||||
auto view = registry.view<int, unsigned int>(entt::persistent_t{});
|
||||
|
||||
for(auto entity: view) {
|
||||
ASSERT_EQ(view.get<unsigned int>(entity), --uval);
|
||||
ASSERT_EQ(view.get<int>(entity), --ival);
|
||||
}
|
||||
|
||||
registry.sort<unsigned int>(std::less<unsigned int>{});
|
||||
view.sort<unsigned int>();
|
||||
|
||||
for(auto entity: view) {
|
||||
ASSERT_EQ(view.get<unsigned int>(entity), uval++);
|
||||
ASSERT_EQ(view.get<int>(entity), ival++);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
auto view = registry.view<char>();
|
||||
const auto &cview = view;
|
||||
auto cview = std::as_const(registry).view<const char>();
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
@@ -255,7 +33,6 @@ TEST(SingleComponentView, Functionalities) {
|
||||
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');
|
||||
}
|
||||
|
||||
@@ -263,20 +40,19 @@ TEST(SingleComponentView, Functionalities) {
|
||||
ASSERT_EQ(*(view.data() + 1), e0);
|
||||
|
||||
ASSERT_EQ(*(view.raw() + 0), '2');
|
||||
ASSERT_EQ(*(static_cast<const decltype(view) &>(view).raw() + 1), '1');
|
||||
ASSERT_EQ(*(cview.raw() + 1), '1');
|
||||
|
||||
registry.remove<char>(e0);
|
||||
registry.remove<char>(e1);
|
||||
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
ASSERT_EQ(view.cbegin(), view.cend());
|
||||
ASSERT_TRUE(view.empty());
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, ElementAccess) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
auto view = registry.view<int>();
|
||||
const auto &cview = view;
|
||||
auto cview = std::as_const(registry).view<const int>();
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
@@ -291,7 +67,7 @@ TEST(SingleComponentView, ElementAccess) {
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, Contains) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
@@ -308,7 +84,7 @@ TEST(SingleComponentView, Contains) {
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
@@ -319,7 +95,7 @@ TEST(SingleComponentView, Empty) {
|
||||
|
||||
auto view = registry.view<int>();
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{0});
|
||||
ASSERT_EQ(view.size(), entt::registry::size_type{0});
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
@@ -328,30 +104,103 @@ TEST(SingleComponentView, Empty) {
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
registry.assign<int>(registry.create());
|
||||
registry.assign<int>(registry.create());
|
||||
|
||||
auto view = registry.view<int>();
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto, int &) { ++cnt; });
|
||||
view.each([&cnt](int &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
ASSERT_EQ(cnt, std::size_t{4});
|
||||
|
||||
cview.each([&cnt](auto, const int &) { --cnt; });
|
||||
std::as_const(view).each([&cnt](auto, const int &) { --cnt; });
|
||||
std::as_const(view).each([&cnt](const int &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, ConstNonConstAndAllInBetween) {
|
||||
entt::registry registry;
|
||||
auto view = registry.view<int>();
|
||||
auto cview = std::as_const(registry).view<const int>();
|
||||
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
ASSERT_EQ(cview.size(), decltype(cview.size()){0});
|
||||
|
||||
registry.assign<int>(registry.create(), 0);
|
||||
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){1});
|
||||
ASSERT_EQ(cview.size(), decltype(cview.size()){1});
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<typename decltype(view)::raw_type, int>));
|
||||
ASSERT_TRUE((std::is_same_v<typename decltype(cview)::raw_type, const int>));
|
||||
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.get(0)), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.raw()), int *>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(cview.get(0)), const int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(cview.raw()), const int *>));
|
||||
|
||||
view.each([](auto, auto &&i) {
|
||||
ASSERT_TRUE((std::is_same_v<decltype(i), int &>));
|
||||
});
|
||||
|
||||
cview.each([](auto, auto &&i) {
|
||||
ASSERT_TRUE((std::is_same_v<decltype(i), const int &>));
|
||||
});
|
||||
}
|
||||
|
||||
TEST(SingleComponentView, Find) {
|
||||
entt::registry registry;
|
||||
auto view = registry.view<int>();
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
|
||||
const auto e2 = registry.create();
|
||||
registry.assign<int>(e2);
|
||||
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<int>(e3);
|
||||
|
||||
registry.remove<int>(e1);
|
||||
|
||||
ASSERT_NE(view.find(e0), view.end());
|
||||
ASSERT_EQ(view.find(e1), view.end());
|
||||
ASSERT_NE(view.find(e2), view.end());
|
||||
ASSERT_NE(view.find(e3), view.end());
|
||||
|
||||
auto it = view.find(e2);
|
||||
|
||||
ASSERT_EQ(*it, e2);
|
||||
ASSERT_EQ(*(++it), e3);
|
||||
ASSERT_EQ(*(++it), e0);
|
||||
ASSERT_EQ(++it, view.end());
|
||||
ASSERT_EQ(++view.find(e0), view.end());
|
||||
|
||||
const auto e4 = registry.create();
|
||||
registry.destroy(e4);
|
||||
const auto e5 = registry.create();
|
||||
registry.assign<int>(e5);
|
||||
|
||||
ASSERT_NE(view.find(e5), view.end());
|
||||
ASSERT_EQ(view.find(e4), view.end());
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
auto view = registry.view<int, char>();
|
||||
const auto &cview = view;
|
||||
auto cview = std::as_const(registry).view<const int, const char>();
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_TRUE(view.empty<int>());
|
||||
ASSERT_TRUE(cview.empty<const char>());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
@@ -360,6 +209,8 @@ TEST(MultipleComponentView, Functionalities) {
|
||||
registry.assign<int>(e1);
|
||||
|
||||
ASSERT_FALSE(view.empty());
|
||||
ASSERT_FALSE(view.empty<int>());
|
||||
ASSERT_FALSE(cview.empty<const char>());
|
||||
|
||||
registry.assign<char>(e1);
|
||||
|
||||
@@ -374,21 +225,30 @@ TEST(MultipleComponentView, Functionalities) {
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_NE(cview.begin(), cview.end());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){1});
|
||||
ASSERT_EQ(view.size<int>(), decltype(view.size()){1});
|
||||
ASSERT_EQ(cview.size<const char>(), decltype(view.size()){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_EQ(std::get<0>(cview.get<int, char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<0>(cview.get<const int, const char>(entity)), 42);
|
||||
ASSERT_EQ(std::get<1>(view.get<int, char>(entity)), '2');
|
||||
ASSERT_EQ(cview.get<char>(entity), '2');
|
||||
ASSERT_EQ(cview.get<const char>(entity), '2');
|
||||
}
|
||||
|
||||
ASSERT_EQ(*(view.data<int>() + 0), e1);
|
||||
ASSERT_EQ(*(view.data<char>() + 0), e0);
|
||||
ASSERT_EQ(*(cview.data<const char>() + 1), e1);
|
||||
|
||||
ASSERT_EQ(*(view.raw<int>() + 0), 42);
|
||||
ASSERT_EQ(*(view.raw<char>() + 0), '1');
|
||||
ASSERT_EQ(*(cview.raw<const char>() + 1), '2');
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, Iterator) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
@@ -409,30 +269,8 @@ TEST(MultipleComponentView, Iterator) {
|
||||
ASSERT_EQ(++view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, ConstIterator) {
|
||||
entt::DefaultRegistry registry;
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
|
||||
const auto view = registry.view<int, char>();
|
||||
using iterator_type = typename decltype(view)::iterator_type;
|
||||
|
||||
iterator_type cend{view.cbegin()};
|
||||
iterator_type cbegin{};
|
||||
cbegin = view.cend();
|
||||
std::swap(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(cbegin, view.cbegin());
|
||||
ASSERT_EQ(cend, view.cend());
|
||||
ASSERT_NE(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(view.cbegin()++, view.cbegin());
|
||||
ASSERT_EQ(++view.cbegin(), view.cend());
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, Contains) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
@@ -451,7 +289,7 @@ TEST(MultipleComponentView, Contains) {
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<double>(e0);
|
||||
@@ -471,7 +309,7 @@ TEST(MultipleComponentView, Empty) {
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
@@ -482,20 +320,48 @@ TEST(MultipleComponentView, Each) {
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto view = registry.view<int, char>();
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
auto cview = std::as_const(registry).view<const int, const char>();
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto, int &, char &) { ++cnt; });
|
||||
view.each([&cnt](int &, char &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
ASSERT_EQ(cnt, std::size_t{4});
|
||||
|
||||
cview.each([&cnt](auto, const int &, const char &) { --cnt; });
|
||||
cview.each([&cnt](const int &, const char &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, EachWithType) {
|
||||
entt::registry registry;
|
||||
|
||||
for(auto i = 0; i < 3; ++i) {
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity, i);
|
||||
registry.assign<char>(entity);
|
||||
}
|
||||
|
||||
// makes char a better candidate during iterations
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity, 99);
|
||||
|
||||
registry.view<int, char>().each<int>([value = 2](const auto curr, const auto) mutable {
|
||||
ASSERT_EQ(curr, value--);
|
||||
});
|
||||
|
||||
registry.sort<int>([](const auto lhs, const auto rhs) {
|
||||
return lhs < rhs;
|
||||
});
|
||||
|
||||
registry.view<int, char>().each<int>([value = 0](const auto curr, const auto) mutable {
|
||||
ASSERT_EQ(curr, value++);
|
||||
});
|
||||
}
|
||||
|
||||
TEST(MultipleComponentView, EachWithHoles) {
|
||||
entt::DefaultRegistry registry;
|
||||
entt::registry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
@@ -519,344 +385,71 @@ TEST(MultipleComponentView, EachWithHoles) {
|
||||
});
|
||||
}
|
||||
|
||||
TEST(RawView, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.view<char>(entt::raw_t{});
|
||||
const auto &cview = view;
|
||||
TEST(MultipleComponentView, ConstNonConstAndAllInBetween) {
|
||||
entt::registry registry;
|
||||
auto view = registry.view<int, const char>();
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity, 0);
|
||||
registry.assign<char>(entity, 'c');
|
||||
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
ASSERT_FALSE(view.empty());
|
||||
ASSERT_NO_THROW(registry.view<char>(entt::raw_t{}).begin()++);
|
||||
ASSERT_NO_THROW(++registry.view<char>(entt::raw_t{}).begin());
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_NE(cview.begin(), cview.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());
|
||||
ASSERT_EQ(view.cbegin(), view.cend());
|
||||
ASSERT_TRUE(view.empty());
|
||||
}
|
||||
|
||||
TEST(RawView, ElementAccess) {
|
||||
entt::DefaultRegistry registry;
|
||||
auto view = registry.view<int>(entt::raw_t{});
|
||||
const auto &cview = view;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0, 42);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1, 3);
|
||||
|
||||
for(typename decltype(view)::size_type i{}; i < view.size(); ++i) {
|
||||
ASSERT_EQ(view[i], i ? 42 : 3);
|
||||
ASSERT_EQ(cview[i], i ? 42 : 3);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RawView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
registry.assign<double>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto view = registry.view<int>(entt::raw_t{});
|
||||
|
||||
ASSERT_EQ(view.size(), entt::DefaultRegistry::size_type{0});
|
||||
|
||||
for(auto &&component: view) {
|
||||
(void)component;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RawView, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
registry.assign<int>(registry.create(), 1);
|
||||
registry.assign<int>(registry.create(), 3);
|
||||
|
||||
auto view = registry.view<int>(entt::raw_t{});
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](int &v) { cnt += (v % 2); });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
|
||||
cview.each([&cnt](const int &v) { cnt -= (v % 2); });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Functionalities) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
// forces the creation of the pools
|
||||
registry.reserve<int>(0);
|
||||
registry.reserve<char>(0);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
const auto &cview = view;
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
|
||||
ASSERT_FALSE(view.empty());
|
||||
|
||||
registry.assign<char>(e1);
|
||||
|
||||
auto it = registry.view(std::begin(types), std::end(types)).begin();
|
||||
|
||||
ASSERT_EQ(*it, e1);
|
||||
ASSERT_EQ(++it, (registry.view(std::begin(types), std::end(types)).end()));
|
||||
|
||||
ASSERT_NO_THROW((registry.view(std::begin(types), std::end(types)).begin()++));
|
||||
ASSERT_NO_THROW((++registry.view(std::begin(types), std::end(types)).begin()));
|
||||
|
||||
ASSERT_NE(view.begin(), view.end());
|
||||
ASSERT_NE(cview.begin(), cview.end());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){1});
|
||||
|
||||
registry.get<char>(e0) = '1';
|
||||
registry.get<char>(e1) = '2';
|
||||
registry.get<int>(e1) = 42;
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.get<int>(0)), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.get<const char>(0)), const char &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.get<int, const char>(0)), std::tuple<int &, const char &>>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.raw<const char>()), const char *>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(view.raw<int>()), int *>));
|
||||
|
||||
for(auto entity: view) {
|
||||
ASSERT_EQ(registry.get<int>(entity), 42);
|
||||
ASSERT_EQ(registry.get<char>(entity), '2');
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Iterator) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
using iterator_type = typename decltype(view)::iterator_type;
|
||||
|
||||
iterator_type end{view.begin()};
|
||||
iterator_type begin{};
|
||||
begin = view.end();
|
||||
std::swap(begin, end);
|
||||
|
||||
ASSERT_EQ(begin, view.begin());
|
||||
ASSERT_EQ(end, view.end());
|
||||
ASSERT_NE(begin, end);
|
||||
|
||||
ASSERT_EQ(view.begin()++, view.begin());
|
||||
ASSERT_EQ(++view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(RuntimeView, ConstIterator) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto entity = registry.create();
|
||||
registry.assign<int>(entity);
|
||||
registry.assign<char>(entity);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
using iterator_type = typename decltype(view)::iterator_type;
|
||||
|
||||
iterator_type cend{view.cbegin()};
|
||||
iterator_type cbegin{};
|
||||
cbegin = view.cend();
|
||||
std::swap(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(cbegin, view.cbegin());
|
||||
ASSERT_EQ(cend, view.cend());
|
||||
ASSERT_NE(cbegin, cend);
|
||||
|
||||
ASSERT_EQ(view.cbegin()++, view.cbegin());
|
||||
ASSERT_EQ(++view.cbegin(), view.cend());
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Contains) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
registry.destroy(e0);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
ASSERT_TRUE(view.contains(e1));
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<double>(e0);
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<float>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<char>(e1);
|
||||
registry.assign<float>(e1);
|
||||
|
||||
component_type types[] = { registry.type<char>(), registry.type<int>(), registry.type<float>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RuntimeView, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
registry.assign<char>(e0);
|
||||
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
}
|
||||
|
||||
TEST(RuntimeView, EachWithHoles) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
const auto e1 = registry.create();
|
||||
const auto e2 = registry.create();
|
||||
|
||||
registry.assign<char>(e0, '0');
|
||||
registry.assign<char>(e1, '1');
|
||||
|
||||
registry.assign<int>(e0, 0);
|
||||
registry.assign<int>(e2, 2);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
|
||||
view.each([e0](auto entity) {
|
||||
ASSERT_EQ(e0, entity);
|
||||
view.each([](auto, auto &&i, auto &&c) {
|
||||
ASSERT_TRUE((std::is_same_v<decltype(i), int &>));
|
||||
ASSERT_TRUE((std::is_same_v<decltype(c), const char &>));
|
||||
});
|
||||
}
|
||||
|
||||
TEST(RuntimeView, MissingPool) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
TEST(MultipleComponentView, Find) {
|
||||
entt::registry registry;
|
||||
auto view = registry.view<int, const char>();
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
|
||||
component_type types[] = { registry.type<int>(), registry.type<char>() };
|
||||
auto view = registry.view(std::begin(types), std::end(types));
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
|
||||
registry.assign<char>(e0);
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
const auto e1 = registry.create();
|
||||
registry.assign<int>(e1);
|
||||
registry.assign<char>(e1);
|
||||
|
||||
view.each([](auto) { FAIL(); });
|
||||
const auto e2 = registry.create();
|
||||
registry.assign<int>(e2);
|
||||
registry.assign<char>(e2);
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(RuntimeView, EmptyRange) {
|
||||
entt::DefaultRegistry registry;
|
||||
using component_type = typename decltype(registry)::component_type;
|
||||
|
||||
const auto e0 = registry.create();
|
||||
registry.assign<int>(e0);
|
||||
|
||||
const component_type *ptr = nullptr;
|
||||
auto view = registry.view(ptr, ptr);
|
||||
|
||||
ASSERT_TRUE(view.empty());
|
||||
ASSERT_EQ(view.size(), decltype(view.size()){0});
|
||||
ASSERT_FALSE(view.contains(e0));
|
||||
|
||||
view.each([](auto) { FAIL(); });
|
||||
|
||||
for(auto entity: view) {
|
||||
(void)entity;
|
||||
FAIL();
|
||||
}
|
||||
const auto e3 = registry.create();
|
||||
registry.assign<int>(e3);
|
||||
registry.assign<char>(e3);
|
||||
|
||||
registry.remove<int>(e1);
|
||||
|
||||
ASSERT_NE(view.find(e0), view.end());
|
||||
ASSERT_EQ(view.find(e1), view.end());
|
||||
ASSERT_NE(view.find(e2), view.end());
|
||||
ASSERT_NE(view.find(e3), view.end());
|
||||
|
||||
auto it = view.find(e2);
|
||||
|
||||
ASSERT_EQ(*it, e2);
|
||||
ASSERT_EQ(*(++it), e3);
|
||||
ASSERT_EQ(*(++it), e0);
|
||||
ASSERT_EQ(++it, view.end());
|
||||
ASSERT_EQ(++view.find(e0), view.end());
|
||||
|
||||
const auto e4 = registry.create();
|
||||
registry.destroy(e4);
|
||||
const auto e5 = registry.create();
|
||||
registry.assign<int>(e5);
|
||||
registry.assign<char>(e5);
|
||||
|
||||
ASSERT_NE(view.find(e5), view.end());
|
||||
ASSERT_EQ(view.find(e4), view.end());
|
||||
}
|
||||
|
||||
@@ -1,50 +1,48 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/locator/locator.hpp>
|
||||
|
||||
struct AService {};
|
||||
struct a_service {};
|
||||
|
||||
struct AnotherService {
|
||||
virtual ~AnotherService() = default;
|
||||
struct another_service {
|
||||
virtual ~another_service() = default;
|
||||
virtual void f(bool) = 0;
|
||||
bool check{false};
|
||||
};
|
||||
|
||||
struct DerivedService: AnotherService {
|
||||
DerivedService(int): AnotherService{} {}
|
||||
struct derived_service: another_service {
|
||||
derived_service(int): another_service{} {}
|
||||
void f(bool b) override { check = b; }
|
||||
};
|
||||
|
||||
TEST(ServiceLocator, Functionalities) {
|
||||
using entt::ServiceLocator;
|
||||
ASSERT_TRUE(entt::service_locator<a_service>::empty());
|
||||
ASSERT_TRUE(entt::service_locator<another_service>::empty());
|
||||
|
||||
ASSERT_TRUE(ServiceLocator<AService>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<AnotherService>::empty());
|
||||
entt::service_locator<a_service>::set();
|
||||
|
||||
ServiceLocator<AService>::set();
|
||||
ASSERT_FALSE(entt::service_locator<a_service>::empty());
|
||||
ASSERT_TRUE(entt::service_locator<another_service>::empty());
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<AService>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<AnotherService>::empty());
|
||||
entt::service_locator<a_service>::reset();
|
||||
|
||||
ServiceLocator<AService>::reset();
|
||||
ASSERT_TRUE(entt::service_locator<a_service>::empty());
|
||||
ASSERT_TRUE(entt::service_locator<another_service>::empty());
|
||||
|
||||
ASSERT_TRUE(ServiceLocator<AService>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<AnotherService>::empty());
|
||||
entt::service_locator<a_service>::set(std::make_shared<a_service>());
|
||||
|
||||
ServiceLocator<AService>::set(std::make_shared<AService>());
|
||||
ASSERT_FALSE(entt::service_locator<a_service>::empty());
|
||||
ASSERT_TRUE(entt::service_locator<another_service>::empty());
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<AService>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<AnotherService>::empty());
|
||||
entt::service_locator<another_service>::set<derived_service>(42);
|
||||
|
||||
ServiceLocator<AnotherService>::set<DerivedService>(42);
|
||||
ASSERT_FALSE(entt::service_locator<a_service>::empty());
|
||||
ASSERT_FALSE(entt::service_locator<another_service>::empty());
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<AService>::empty());
|
||||
ASSERT_FALSE(ServiceLocator<AnotherService>::empty());
|
||||
entt::service_locator<another_service>::get().lock()->f(!entt::service_locator<another_service>::get().lock()->check);
|
||||
|
||||
ServiceLocator<AnotherService>::get().lock()->f(!ServiceLocator<AnotherService>::get().lock()->check);
|
||||
ASSERT_TRUE(entt::service_locator<another_service>::get().lock()->check);
|
||||
|
||||
ASSERT_TRUE(ServiceLocator<AnotherService>::get().lock()->check);
|
||||
entt::service_locator<another_service>::ref().f(!entt::service_locator<another_service>::get().lock()->check);
|
||||
|
||||
ServiceLocator<AnotherService>::ref().f(!ServiceLocator<AnotherService>::get().lock()->check);
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<AnotherService>::get().lock()->check);
|
||||
ASSERT_FALSE(entt::service_locator<another_service>::get().lock()->check);
|
||||
}
|
||||
|
||||
1627
test/entt/meta/meta.cpp
Normal file
1627
test/entt/meta/meta.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@@ -2,36 +2,36 @@
|
||||
#include <cstdint>
|
||||
#include <entt/process/process.hpp>
|
||||
|
||||
struct FakeProcess: entt::Process<FakeProcess, int> {
|
||||
using process_type = entt::Process<FakeProcess, int>;
|
||||
struct fake_process: entt::process<fake_process, int> {
|
||||
using process_type = entt::process<fake_process, 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 init() { init_invoked = true; }
|
||||
void succeeded() { succeeded_invoked = true; }
|
||||
void failed() { failed_invoked = true; }
|
||||
void aborted() { aborted_invoked = true; }
|
||||
|
||||
void update(delta_type, void *data) {
|
||||
if(data) {
|
||||
(*static_cast<int *>(data))++;
|
||||
}
|
||||
|
||||
updateInvoked = true;
|
||||
update_invoked = true;
|
||||
}
|
||||
|
||||
bool initInvoked{false};
|
||||
bool updateInvoked{false};
|
||||
bool succeededInvoked{false};
|
||||
bool failedInvoked{false};
|
||||
bool abortedInvoked{false};
|
||||
bool init_invoked{false};
|
||||
bool update_invoked{false};
|
||||
bool succeeded_invoked{false};
|
||||
bool failed_invoked{false};
|
||||
bool aborted_invoked{false};
|
||||
};
|
||||
|
||||
TEST(Process, Basics) {
|
||||
FakeProcess process;
|
||||
fake_process process;
|
||||
|
||||
ASSERT_FALSE(process.alive());
|
||||
ASSERT_FALSE(process.dead());
|
||||
@@ -67,8 +67,9 @@ TEST(Process, Basics) {
|
||||
}
|
||||
|
||||
TEST(Process, Succeeded) {
|
||||
FakeProcess process;
|
||||
fake_process process;
|
||||
|
||||
process.tick(0);
|
||||
process.tick(0);
|
||||
process.succeed();
|
||||
process.tick(0);
|
||||
@@ -77,16 +78,17 @@ TEST(Process, Succeeded) {
|
||||
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);
|
||||
ASSERT_TRUE(process.init_invoked);
|
||||
ASSERT_TRUE(process.update_invoked);
|
||||
ASSERT_TRUE(process.succeeded_invoked);
|
||||
ASSERT_FALSE(process.failed_invoked);
|
||||
ASSERT_FALSE(process.aborted_invoked);
|
||||
}
|
||||
|
||||
TEST(Process, Fail) {
|
||||
FakeProcess process;
|
||||
fake_process process;
|
||||
|
||||
process.tick(0);
|
||||
process.tick(0);
|
||||
process.fail();
|
||||
process.tick(0);
|
||||
@@ -95,17 +97,18 @@ TEST(Process, Fail) {
|
||||
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);
|
||||
ASSERT_TRUE(process.init_invoked);
|
||||
ASSERT_TRUE(process.update_invoked);
|
||||
ASSERT_FALSE(process.succeeded_invoked);
|
||||
ASSERT_TRUE(process.failed_invoked);
|
||||
ASSERT_FALSE(process.aborted_invoked);
|
||||
}
|
||||
|
||||
TEST(Process, Data) {
|
||||
FakeProcess process;
|
||||
fake_process process;
|
||||
int value = 0;
|
||||
|
||||
process.tick(0);
|
||||
process.tick(0, &value);
|
||||
process.succeed();
|
||||
process.tick(0, &value);
|
||||
@@ -115,15 +118,15 @@ TEST(Process, Data) {
|
||||
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);
|
||||
ASSERT_TRUE(process.init_invoked);
|
||||
ASSERT_TRUE(process.update_invoked);
|
||||
ASSERT_TRUE(process.succeeded_invoked);
|
||||
ASSERT_FALSE(process.failed_invoked);
|
||||
ASSERT_FALSE(process.aborted_invoked);
|
||||
}
|
||||
|
||||
TEST(Process, AbortNextTick) {
|
||||
FakeProcess process;
|
||||
fake_process process;
|
||||
|
||||
process.tick(0);
|
||||
process.abort();
|
||||
@@ -133,15 +136,15 @@ TEST(Process, AbortNextTick) {
|
||||
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);
|
||||
ASSERT_TRUE(process.init_invoked);
|
||||
ASSERT_FALSE(process.update_invoked);
|
||||
ASSERT_FALSE(process.succeeded_invoked);
|
||||
ASSERT_FALSE(process.failed_invoked);
|
||||
ASSERT_TRUE(process.aborted_invoked);
|
||||
}
|
||||
|
||||
TEST(Process, AbortImmediately) {
|
||||
FakeProcess process;
|
||||
fake_process process;
|
||||
|
||||
process.tick(0);
|
||||
process.abort(true);
|
||||
@@ -150,11 +153,11 @@ TEST(Process, AbortImmediately) {
|
||||
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);
|
||||
ASSERT_TRUE(process.init_invoked);
|
||||
ASSERT_FALSE(process.update_invoked);
|
||||
ASSERT_FALSE(process.succeeded_invoked);
|
||||
ASSERT_FALSE(process.failed_invoked);
|
||||
ASSERT_TRUE(process.aborted_invoked);
|
||||
}
|
||||
|
||||
TEST(ProcessAdaptor, Resolved) {
|
||||
@@ -165,8 +168,9 @@ TEST(ProcessAdaptor, Resolved) {
|
||||
resolve();
|
||||
};
|
||||
|
||||
auto process = entt::ProcessAdaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
auto process = entt::process_adaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
|
||||
process.tick(0);
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_TRUE(process.dead());
|
||||
@@ -181,8 +185,9 @@ TEST(ProcessAdaptor, Rejected) {
|
||||
rejected();
|
||||
};
|
||||
|
||||
auto process = entt::ProcessAdaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
auto process = entt::process_adaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
|
||||
process.tick(0);
|
||||
process.tick(0);
|
||||
|
||||
ASSERT_TRUE(process.rejected());
|
||||
@@ -197,8 +202,9 @@ TEST(ProcessAdaptor, Data) {
|
||||
resolve();
|
||||
};
|
||||
|
||||
auto process = entt::ProcessAdaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
auto process = entt::process_adaptor<decltype(lambda), std::uint64_t>{lambda};
|
||||
|
||||
process.tick(0);
|
||||
process.tick(0, &value);
|
||||
|
||||
ASSERT_TRUE(process.dead());
|
||||
|
||||
@@ -3,19 +3,19 @@
|
||||
#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}
|
||||
struct foo_process: entt::process<foo_process, int> {
|
||||
foo_process(std::function<void()> upd, std::function<void()> abort)
|
||||
: on_update{upd}, on_aborted{abort}
|
||||
{}
|
||||
|
||||
void update(delta_type, void *) { onUpdate(); }
|
||||
void aborted() { onAborted(); }
|
||||
void update(delta_type, void *) { on_update(); }
|
||||
void aborted() { on_aborted(); }
|
||||
|
||||
std::function<void()> onUpdate;
|
||||
std::function<void()> onAborted;
|
||||
std::function<void()> on_update;
|
||||
std::function<void()> on_aborted;
|
||||
};
|
||||
|
||||
struct SucceededProcess: entt::Process<SucceededProcess, int> {
|
||||
struct succeeded_process: entt::process<succeeded_process, int> {
|
||||
void update(delta_type, void *) {
|
||||
ASSERT_FALSE(updated);
|
||||
updated = true;
|
||||
@@ -27,9 +27,9 @@ struct SucceededProcess: entt::Process<SucceededProcess, int> {
|
||||
bool updated = false;
|
||||
};
|
||||
|
||||
unsigned int SucceededProcess::invoked = 0;
|
||||
unsigned int succeeded_process::invoked = 0;
|
||||
|
||||
struct FailedProcess: entt::Process<FailedProcess, int> {
|
||||
struct failed_process: entt::process<failed_process, int> {
|
||||
void update(delta_type, void *) {
|
||||
ASSERT_FALSE(updated);
|
||||
updated = true;
|
||||
@@ -40,20 +40,20 @@ struct FailedProcess: entt::Process<FailedProcess, int> {
|
||||
};
|
||||
|
||||
TEST(Scheduler, Functionalities) {
|
||||
entt::Scheduler<int> scheduler{};
|
||||
entt::scheduler<int> scheduler{};
|
||||
|
||||
bool updated = false;
|
||||
bool aborted = false;
|
||||
|
||||
ASSERT_EQ(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
ASSERT_EQ(scheduler.size(), entt::scheduler<int>::size_type{});
|
||||
ASSERT_TRUE(scheduler.empty());
|
||||
|
||||
scheduler.attach<FooProcess>(
|
||||
scheduler.attach<foo_process>(
|
||||
[&updated](){ updated = true; },
|
||||
[&aborted](){ aborted = true; }
|
||||
);
|
||||
|
||||
ASSERT_NE(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
ASSERT_NE(scheduler.size(), entt::scheduler<int>::size_type{});
|
||||
ASSERT_FALSE(scheduler.empty());
|
||||
|
||||
scheduler.update(0);
|
||||
@@ -62,43 +62,43 @@ TEST(Scheduler, Functionalities) {
|
||||
ASSERT_TRUE(updated);
|
||||
ASSERT_TRUE(aborted);
|
||||
|
||||
ASSERT_NE(scheduler.size(), entt::Scheduler<int>::size_type{});
|
||||
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_EQ(scheduler.size(), entt::scheduler<int>::size_type{});
|
||||
ASSERT_TRUE(scheduler.empty());
|
||||
}
|
||||
|
||||
TEST(Scheduler, Then) {
|
||||
entt::Scheduler<int> scheduler;
|
||||
entt::scheduler<int> scheduler;
|
||||
|
||||
scheduler.attach<SucceededProcess>()
|
||||
.then<SucceededProcess>()
|
||||
.then<FailedProcess>()
|
||||
.then<SucceededProcess>();
|
||||
scheduler.attach<succeeded_process>()
|
||||
.then<succeeded_process>()
|
||||
.then<failed_process>()
|
||||
.then<succeeded_process>();
|
||||
|
||||
for(auto i = 0; i < 8; ++i) {
|
||||
scheduler.update(0);
|
||||
}
|
||||
|
||||
ASSERT_EQ(SucceededProcess::invoked, 2u);
|
||||
ASSERT_EQ(succeeded_process::invoked, 2u);
|
||||
}
|
||||
|
||||
TEST(Scheduler, Functor) {
|
||||
entt::Scheduler<int> scheduler;
|
||||
entt::scheduler<int> scheduler;
|
||||
|
||||
bool firstFunctor = false;
|
||||
bool secondFunctor = false;
|
||||
bool first_functor = false;
|
||||
bool second_functor = false;
|
||||
|
||||
scheduler.attach([&firstFunctor](auto, void *, auto resolve, auto){
|
||||
ASSERT_FALSE(firstFunctor);
|
||||
firstFunctor = true;
|
||||
scheduler.attach([&first_functor](auto, void *, auto resolve, auto){
|
||||
ASSERT_FALSE(first_functor);
|
||||
first_functor = true;
|
||||
resolve();
|
||||
}).then([&secondFunctor](auto, void *, auto, auto reject){
|
||||
ASSERT_FALSE(secondFunctor);
|
||||
secondFunctor = true;
|
||||
}).then([&second_functor](auto, void *, auto, auto reject){
|
||||
ASSERT_FALSE(second_functor);
|
||||
second_functor = true;
|
||||
reject();
|
||||
}).then([](auto...){
|
||||
FAIL();
|
||||
@@ -108,6 +108,6 @@ TEST(Scheduler, Functor) {
|
||||
scheduler.update(0);
|
||||
}
|
||||
|
||||
ASSERT_TRUE(firstFunctor);
|
||||
ASSERT_TRUE(secondFunctor);
|
||||
ASSERT_TRUE(first_functor);
|
||||
ASSERT_TRUE(second_functor);
|
||||
}
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user