Compare commits

..

938 Commits

Author SHA1 Message Date
Michele Caini
ce3a0157cb updated version.h to v3.2.1 2019-11-26 11:20:54 +01:00
Michele Caini
a146b06e4b updated single include file to v3.2.1 (close #366) 2019-11-26 11:18:41 +01:00
Michele Caini
dc78a3e56d version: 3.2.1 2019-11-26 11:17:13 +01:00
Michele Caini
86c524dade fix: sorting empty types (close #365) 2019-11-26 11:16:02 +01:00
Innokentiy Alaytsev
6889edcfc4 meta, fix: meta_prop::next lazy initialization 2019-11-26 11:11:42 +01:00
Michele Caini
3cd5934bad typo 2019-11-26 11:11:26 +01:00
Michele Caini
064104f23a suppress **all** shadow warnings (close #346) 2019-11-26 11:11:19 +01:00
Michele Caini
e80b1c264c family: it no longer dacays types 2019-11-26 11:11:13 +01:00
Michele Caini
03c3ee514b updated doc 2019-10-25 00:03:58 +02:00
Michele Caini
c93d0f0d59 registry::prepare to force create pools explicitly 2019-10-24 23:10:03 +02:00
Michele Caini
85793d38bc meta: a static assertion forbids using non-copyable types with meta_any (close #336) 2019-10-24 22:55:07 +02:00
David Demelier
49b4a85d55 FAQ: add note about non-copyable objects (#344) 2019-10-24 14:59:22 +02:00
Michele Caini
5e8a9340f1 meta: added meta_type::remove_extent (close #345) 2019-10-23 23:29:25 +02:00
Michele Caini
2b97c01bd6 named_type_traits<Type>::value -> named_type_traits_v<Type> 2019-10-21 22:52:49 +02:00
Michele Caini
39e3a5a708 make macros require a trailing semicolon 2019-10-21 14:23:22 +02:00
Michele Caini
df13d993f7 test: more on type_traits 2019-10-21 13:43:51 +02:00
Michele Caini
36c62763e5 updated TODO 2019-10-20 18:45:49 +02:00
Michele Caini
ad5651ffb2 ENTT_DISABLE_ETO to disable empty type optimization (close #330) 2019-10-20 17:32:10 +02:00
Michele Caini
717b091b49 meta: internal and external indexing to disambiguate property overloads 2019-10-20 15:44:53 +02:00
Michele Caini
4376bbe40b workaround for an issue with clang 6 2019-10-19 22:08:11 +02:00
Michele Caini
2382d2e21c meta: invocable properties (aka annotations - see #299) 2019-10-19 16:25:35 +02:00
Michele Caini
520ccfcd06 meta: support tuple as properties 2019-10-19 16:09:03 +02:00
Michele Caini
0bdc5dfe95 minor changes: make_index_sequence -> index_sequence_for 2019-10-19 15:54:32 +02:00
Michele Caini
6a656a1b0a type_traits: updated choice utility (template variable) 2019-10-19 15:51:27 +02:00
Michele Caini
5ad4079dbf meta: std::get<1>-table properties are no longer accepted, only std::pair is supported 2019-10-19 15:19:58 +02:00
Innokentiy Alaytsev
bfa46b795f Property list (#338)
* Fix typo in comment
* Implemented a function for adding a list of properties to meta-item
2019-10-19 15:13:43 +02:00
Michele Caini
ecaa9c275c typo 2019-10-18 21:45:17 +02:00
Michele Caini
93fd1757f8 meta: more on properties (close #341) 2019-10-18 17:30:32 +02:00
Michele Caini
71d0958398 type_traits: added support for the choice trick 2019-10-18 16:38:23 +02:00
Michele Caini
ed89d94d7a meta: support for key-only properties 2019-10-17 17:23:25 +02:00
Stephan Z
135132e5f0 Snapshot loader can auto-update entities stored in maps now (#335) - close #334
* Detect if member variable is of type map and update the contained entities if needed
* issue 334: tests for identifier update on maps in snapshots
2019-10-16 23:35:29 +02:00
Michele Caini
1ad75f5c1b meta: updated doc (close #324) 2019-10-16 13:43:29 +02:00
Michele Caini
21c6f9e394 meta: updated documentation 2019-10-16 13:43:29 +02:00
Michele Caini
5a7e954aee meta: context review 2019-10-16 13:43:29 +02:00
Michele Caini
c1117e260c meta: meta_node<>::ctx -> meta_node<>::global 2019-10-16 13:43:29 +02:00
Michele Caini
b0ea150e94 meta: properties can be also runtime objects 2019-10-16 13:43:29 +02:00
Michele Caini
0e3bdc02ea meta: meta_factory::type supports named types (the identifier isn't required in this case) 2019-10-16 13:43:29 +02:00
Michele Caini
e5a075a329 meta: minor changes 2019-10-16 13:43:29 +02:00
Michele Caini
30c7a64619 meta: simplified T::prop 2019-10-16 13:43:29 +02:00
Michele Caini
ab12f29ebf meta: introduce extended meta factory 2019-10-16 13:43:29 +02:00
Michele Caini
a0c3a82c76 meta: cleanup and renaming 2019-10-16 13:43:29 +02:00
Michele Caini
085a281f8a meta: some tests across boundaries 2019-10-16 13:43:29 +02:00
Michele Caini
1f871c24b9 meta: plug-and-play mode 2019-10-16 13:43:29 +02:00
Michele Caini
9f46b51985 minor changes 2019-10-16 13:43:29 +02:00
Michele Caini
fc572b8717 updated TODO 2019-10-16 13:43:29 +02:00
Michele Caini
4404601fa3 meta: tests 2019-10-16 13:43:29 +02:00
Michele Caini
faf7e28119 meta: added meta context object 2019-10-16 13:43:29 +02:00
Michele Caini
0303facfd9 updated TODO 2019-10-16 13:43:29 +02:00
Michele Caini
86e056a736 meta: added underlying context 2019-10-16 13:43:29 +02:00
Michele Caini
75105dc1fc meta: cleanup 2019-10-16 13:43:29 +02:00
Michele Caini
14b4979c98 added is_equality_comparable[_v] trait 2019-10-16 13:43:29 +02:00
Michele Caini
f956c64765 meta: doc 2019-10-16 13:43:29 +02:00
Michele Caini
8b3f954ad8 meta: cleanup 2019-10-16 13:43:29 +02:00
Michele Caini
a013cfcd1e meta_type::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
65553e6c43 meta_type::remove_pointer returns a pointer to an underlying node now 2019-10-16 13:43:29 +02:00
Michele Caini
401c881c00 meta_prop::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
cf0e4d24a1 meta_func::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
21810f3b39 meta_data::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
0b211ab0ba meta_dtor::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
30c7cf3512 meta_ctor::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
bf8839a560 meta_base::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
818998c532 meta_conv::meta is no longer available 2019-10-16 13:43:29 +02:00
Michele Caini
98f54029d0 some consts here and there 2019-10-16 13:43:29 +02:00
Michele Caini
d41f2c138b minor changes 2019-10-16 13:43:29 +02:00
Michele Caini
50f07a6a0d meta: cleanup 2019-10-16 13:43:29 +02:00
Michele Caini
b10116febb review: cache resource_type and id_type 2019-10-16 13:43:29 +02:00
Michele Caini
17b5d732e4 cleanup 2019-10-16 13:43:29 +02:00
Michele Caini
5b16b1d827 entt::resource_cache -> entt::cache 2019-10-16 13:43:29 +02:00
Michele Caini
e5fe962130 entt::resource_handle -> entt::handle 2019-10-16 13:43:29 +02:00
Michele Caini
00f03d9bb7 entt::resource_loader -> entt::loader 2019-10-16 13:43:29 +02:00
Michele Caini
f7c285af20 minor changes 2019-10-16 09:14:08 +02:00
Michele Caini
f6e3121c76 GH sponsorship 2019-10-08 22:56:26 +02:00
Michele Caini
a95aa81850 utility: overloaded and y_combinator 2019-10-01 15:02:18 +02:00
Michele Caini
ffa5825658 cleanup 2019-10-01 15:01:56 +02:00
Michele Caini
df5b90cc7c nested groups: documentation 2019-09-30 00:20:02 +02:00
Michele Caini
9dde644fd0 nested groups: impl + tests 2019-09-30 00:19:17 +02:00
Michele Caini
d525a1536c sink::[before|disconnect]: const void * -> Type * 2019-09-30 00:11:07 +02:00
Michele Caini
54f18f3f0d delegate/sink: pointer payload 2019-09-29 23:40:03 +02:00
Michele Caini
ce4f26a5d5 minor changes 2019-09-29 21:43:30 +02:00
Guillaume Haerinck
177a7429f1 [doc] Add link to Mirror lasers & Robots project (#329) 2019-09-29 21:31:49 +02:00
Michele Caini
f672d734f9 delegate: built-in support for pointer payload 2019-09-29 00:46:29 +02:00
Michele Caini
faf5a38f1c sink: before/disconnect and const void * 2019-09-28 22:56:47 +02:00
Michele Caini
03c4267b84 process: tests with both a complex data type and a fundamental type (close #326) 2019-09-28 00:25:25 +02:00
Michele Caini
ab08621808 updated .gitignore 2019-09-28 00:17:41 +02:00
Michele Caini
d06c0e5c5d updated doc 2019-09-28 00:12:47 +02:00
Griffin Downs
32e76298e4 Add vcpkg installation instructions (#327) 2019-09-28 00:08:38 +02:00
Michele Caini
752163a5c0 workaround for an issue of VS2019 (close #314) 2019-09-27 23:49:43 +02:00
Michele Caini
6568fa23f8 updated TODO 2019-09-27 23:06:26 +02:00
Michele Caini
9c69288304 added more showcases (close #328) 2019-09-27 23:03:45 +02:00
Michele Caini
3a3346e750 typo 2019-09-26 23:32:25 +02:00
Michele Caini
47ab73d9a4 updated doc 2019-09-26 23:03:44 +02:00
Michele Caini
9555385325 updated list of contributors 2019-09-26 16:11:19 +02:00
xiss burg
f80a03afc7 This should allow any type to be used as Delta... (#325)
* This should allow any type to be used as Delta, beyond native numerical types.
* Why write more code than you have to?
2019-09-26 16:09:56 +02:00
Michele Caini
c1b64770d1 updated tests 2019-09-25 23:54:53 +02:00
Michele Caini
400f106d62 updated coverage.yml 2019-09-25 13:52:39 +02:00
Michele Caini
1cc20716ae codecov badge 2019-09-25 12:58:43 +02:00
Michele Caini
fc7d123871 tests: review 2019-09-25 12:54:17 +02:00
Michele Caini
2a8a014ad0 tests: review 2019-09-25 12:37:20 +02:00
Michele Caini
861f452d26 remove coveralls 2019-09-25 11:57:48 +02:00
Michele Caini
64a7d5e4fd codecov 2019-09-25 11:57:29 +02:00
Michele Caini
7c45423969 GH actions: reattach HEAD (workaround for an issue with the checkout action) 2019-09-25 10:13:13 +02:00
Michele Caini
480f70d7e2 GH actions: badges 2019-09-24 17:10:43 +02:00
Michele Caini
7f182896ca deleted appveyor/travis files 2019-09-24 17:01:45 +02:00
Michele Caini
1319fe2eb5 GH actions: conan 2019-09-24 16:45:26 +02:00
Michele Caini
f5c231aa80 GH actions: split CI and coveralls 2019-09-24 11:41:22 +02:00
Michele Caini
84d468333a github actions 2019-09-24 11:36:33 +02:00
Michele Caini
dfdbb0a061 class -> struct 2019-09-24 00:12:49 +02:00
Michele Caini
27365abbe3 registry: always assure and mutable list of pools 2019-09-23 23:44:14 +02:00
Michele Caini
f3aed46c94 minor changes 2019-09-23 23:16:29 +02:00
Michele Caini
7e0ea176cf cleanup 2019-09-23 22:58:47 +02:00
Michele Caini
791e13e7bf updated README 2019-09-23 16:01:50 +02:00
Michele Caini
9dff3ac749 sigh::disconnect and sigh::before support for pointers to instances 2019-09-22 23:42:04 +02:00
Michele Caini
45f207164d minor changes 2019-09-22 16:11:09 +02:00
Michele Caini
bea6f3ec14 sigh: filter out null opaque pointers 2019-09-22 15:19:27 +02:00
Michele Caini
1edc883ca2 cleanup 2019-09-21 17:00:44 +02:00
Michele Caini
116825d604 slightly faster sparse_set::arrange (apply permutation in O(N) now) 2019-09-21 15:29:11 +02:00
Michele Caini
26bc5ef091 workaround for an issue of VS 2019-09-20 17:27:52 +02:00
Michele Caini
8c228287aa doc: sink::before 2019-09-20 15:22:19 +02:00
Michele Caini
5c7c682d41 test: sink::before 2019-09-20 15:20:31 +02:00
Michele Caini
bfc2eca041 sink::before (overloaded function) 2019-09-20 14:53:10 +02:00
Michele Caini
b043b48e6e sparse_set::arrange: more tests (see #323) 2019-09-20 08:33:31 +02:00
Michele Caini
d7c03d7356 fixed an issue with sparse_set::arrange (close #323) 2019-09-19 23:50:30 +02:00
Michele Caini
c41f6aebf4 sorting: more tests 2019-09-19 22:39:13 +02:00
Michele Caini
5e6cda7c5e registry: fixed signal race on groups (close #320) 2019-09-19 14:09:53 +02:00
Carl Findahl
9c71b53abc Add namespace qualifier in CMake (close #321) (#322) 2019-09-19 13:28:33 +02:00
Michele Caini
3f96e04a7b workaround for an issue with clang 6 2019-09-18 23:16:45 +02:00
Michele Caini
90eeeedf52 view supports exclusion list 2019-09-18 14:32:58 +02:00
Michele Caini
6649362fec minor changes 2019-09-18 00:04:45 +02:00
Michele Caini
39c8889585 cleanup 2019-09-18 00:04:22 +02:00
Michele Caini
b5b1a64f0c typo 2019-09-18 00:04:07 +02:00
Michele Caini
cd108b5f57 prepare view for exclusion list 2019-09-17 23:26:54 +02:00
Michele Caini
780f3f8552 updated TODO list 2019-09-17 16:53:52 +02:00
Michele Caini
afea5eb69e added @suVrik to the list of contributors 2019-09-17 16:49:36 +02:00
Michele Caini
b72cf5d364 review: observer 2019-09-17 16:48:54 +02:00
Andrej Suvoraŭ
c133686dde Fix entt::collector::where (#319)
* fix observer
* add chained entt::collector::where test
2019-09-17 16:02:11 +02:00
Michele Caini
e216fa98ef cleanup 2019-09-16 22:37:38 +02:00
Michele Caini
8f9d8e188f updated README 2019-09-16 15:51:00 +02:00
Michele Caini
800751cbe0 sorting no longer requires allocations and is much faster 2019-09-16 15:38:34 +02:00
Michele Caini
14bc73cde9 swap accepts entities, no longer raw positions 2019-09-14 17:49:30 +02:00
Michele Caini
41307046be updated doc 2019-09-14 17:47:24 +02:00
Michele Caini
c259c79384 typo 2019-09-12 23:01:49 +02:00
Michele Caini
49f69facaf cleanup 2019-09-12 22:52:41 +02:00
Michele Caini
da2b6acf91 minor changes - code coverage 2019-09-12 22:34:03 +02:00
Michele Caini
fbdbe848c5 workaround for an issue of VS2017 (close #316) 2019-09-12 22:11:08 +02:00
Michele Caini
cc41caede9 multi-component [registry|view|group]::empty 2019-09-12 14:16:06 +02:00
Michele Caini
ee373eb6fb multi-component registry::shrink_to_fit 2019-09-11 23:12:14 +02:00
Michele Caini
d4b18b1e27 multi-component registry::reserve 2019-09-11 23:10:24 +02:00
Michele Caini
94e2fe2cbb make empty components eat arguments on construction 2019-09-11 22:56:35 +02:00
Michał Janiszewski
c93658ee04 Use Ninja on AppVeyor (#310)
This brings up improved compilation speed and saner error messages.
2019-09-11 22:24:05 +02:00
Michele Caini
ad7ee0b716 mark cmake project explicitly as C++ (see #309) 2019-09-10 23:02:22 +02:00
Michele Caini
b2fad7a567 added dispatch::discard to clear pools of events (close #312) 2019-09-10 22:55:03 +02:00
Michele Caini
e80adc2799 cmake support for clang users on Windows (see #311) 2019-09-10 16:38:38 +02:00
Michele Caini
ac111224ae meta: all function arguments are taken by alias (close #301) 2019-09-10 16:10:09 +02:00
Michele Caini
0813bb1a28 updated doc 2019-09-10 16:10:09 +02:00
Michele Caini
4d1ad8f749 meta_handle no longer exposes try_cast 2019-09-10 16:10:09 +02:00
Michele Caini
80b444e531 added meta_any constructor for the unmanaged object taken from a meta_handle 2019-09-10 16:10:09 +02:00
Michele Caini
3a6468cd0a disable BUILD_MOD for the CI 2019-09-10 15:18:28 +02:00
Michele Caini
c9fdb215f3 C language required for duktape (see BUILD_MOD option for more details) 2019-09-10 15:12:59 +02:00
Michał Janiszewski
85ff5f2d10 Mark CMake project explicitly as C++ (#309)
This prevents checking of C compiler
2019-09-10 14:31:12 +02:00
Michele Caini
be3597524f added a static_assert in a test to suppress a warning from GCC 2019-09-08 22:53:48 +02:00
Michele Caini
5717dbda4f added [[maybe_unused]] to suppress a warning from GCC 2019-09-08 22:50:51 +02:00
Michele Caini
588eec4672 added meta_type::compare, meta_any no longer has to store aside comparison functions 2019-09-08 22:44:06 +02:00
Michele Caini
cc3d0d8211 no more references to the prototype class (close #307) 2019-09-08 00:11:44 +02:00
Michele Caini
1550efaf7e stomp & spawn: const source registry (close #308) 2019-09-08 00:09:43 +02:00
Michele Caini
007300b881 sigh: ordered calls 2019-09-08 00:05:26 +02:00
Michele Caini
454f072f13 review stomp & spawn (close #306) 2019-09-06 17:21:07 +02:00
Michele Caini
b3be6c75c3 cleanup 2019-09-06 16:42:42 +02:00
Michele Caini
f8774f314b sink::disconnect accepts also opaque pointers 2019-09-06 08:19:28 +02:00
Michele Caini
3db35ad343 added openblack to the links 2019-09-06 08:19:28 +02:00
Michele Caini
0e42a779e2 meta_any uses reference_wrapper instead of as_alias_t for aliasing 2019-09-06 08:19:28 +02:00
Michele Caini
d34e829811 perf improvement 2019-09-06 08:19:28 +02:00
Michele Caini
ebd988702e relaxed constraints on the order of types in the group definition 2019-09-06 08:19:28 +02:00
Michele Caini
1232398742 now working on EnTT v3.2.0 2019-09-06 08:19:28 +02:00
Michele Caini
6dbcd47143 updated single file 2019-09-06 08:18:34 +02:00
Michele Caini
0e07482e26 ready to cut v3.1.1 2019-09-05 15:24:08 +02:00
Michele Caini
6bb4800ecd fix batch creation (close #305) 2019-09-05 15:24:03 +02:00
Michele Caini
37e1ac71b0 minor changes 2019-09-05 15:22:24 +02:00
Michele Caini
869f96816c updated doc 2019-09-05 15:21:45 +02:00
Michele Caini
35a7008444 added noexcept specifier 2019-09-03 23:39:38 +02:00
Michele Caini
67f80ee111 the default ctor of entt::meta_factory should be public 2019-09-03 23:31:43 +02:00
Michele Caini
083a58753b typo 2019-09-03 23:31:39 +02:00
Michele Caini
b652357a5c review: group::sort 2019-09-02 16:48:47 +02:00
Michele Caini
49a52140b0 minor changes 2019-09-02 16:34:38 +02:00
Michele Caini
e37f84a227 avoid taking the position twice 2019-09-02 16:23:11 +02:00
Nicki
50fc83d478 Update AUTHORS (#302)
He's one of contributors to the idea of nested groups!! :)
2019-08-30 09:49:46 +02:00
Michele Caini
a3da1c54eb updated single file 2019-08-29 16:01:26 +02:00
Michele Caini
b8471e3cf1 algorithm: radix sort 2019-08-27 23:42:58 +02:00
Michele Caini
4bd645d5c6 updated links 2019-08-26 16:57:30 +02:00
Michele Caini
83446c0694 updated sparse_set interface 2019-08-26 15:01:50 +02:00
Michele Caini
b4c30fc07b cleanup 2019-08-26 14:54:15 +02:00
Michele Caini
8325f5bbae runtime_view no longer uses sparse_set<...>::extent() 2019-08-26 14:54:15 +02:00
Michele Caini
9eb1196526 multi-component view no longer uses sparse_set<...>::extent() 2019-08-26 14:54:15 +02:00
Michele Caini
5fe5d4b6fc runtime_view doesn't use sparse_set<...>::data() anymore 2019-08-26 14:54:15 +02:00
Michele Caini
34af44b8d3 updated doc (close #298) 2019-08-26 14:53:21 +02:00
Michele Caini
643a20fec3 minor changes 2019-08-25 16:52:59 +02:00
Michele Caini
576b931090 meta: updated doc (close #297) 2019-08-25 14:50:12 +02:00
Michele Caini
2cc1f044df constructor to create actors from existing entities (close #296) 2019-08-23 15:54:53 +02:00
Michele Caini
c651392643 removed prototype and dependency (with connect/disconnect) 2019-08-23 15:02:55 +02:00
Michele Caini
9a669176ac workaround for an ICE with VS2019 2019-08-22 23:53:34 +02:00
Michele Caini
b902831db8 cleanup 2019-08-22 23:31:28 +02:00
Michele Caini
90a1de383a more tests 2019-08-22 18:35:23 +02:00
Michele Caini
83f9d42d31 registry: stomp and spawn 2019-08-22 18:03:29 +02:00
Michele Caini
94136531a5 added type_list_size and type_list_size_v 2019-08-22 17:59:15 +02:00
Michele Caini
b8b1e6ba62 registry::stomp: registry & -> basic_registry & 2019-08-21 23:27:35 +02:00
Michele Caini
d6911337f3 added storage::batch overload for copy construction 2019-08-21 23:23:21 +02:00
Michele Caini
d2cbb72490 cleanup 2019-08-21 23:22:32 +02:00
Michele Caini
c835b4d363 updated doc 2019-08-21 17:44:11 +02:00
Michele Caini
ece4480200 batch creation returns iterators and no longer raw pointers 2019-08-21 17:36:56 +02:00
Michele Caini
57f8d90e38 updated README 2019-08-21 17:30:56 +02:00
Michele Caini
da2b2701fe minor changes 2019-08-21 16:57:38 +02:00
Michele Caini
6d7fba3abf updated README 2019-08-21 15:27:29 +02:00
Ezekiel Warren
90798c161b Bazel Support (#287) (#291) 2019-08-21 15:11:29 +02:00
Michele Caini
65e270ff57 minor changes 2019-08-21 14:52:58 +02:00
Michele Caini
4912e19281 workaround for an issue of VS2019 2019-08-20 17:14:09 +02:00
Michele Caini
6402564f44 minor changes 2019-08-20 16:59:49 +02:00
Michele Caini
a06d5dd7bb registry: updated signatures for callbacks, deprecated dependency 2019-08-20 16:52:30 +02:00
Michele Caini
15e7486ca3 appveyor: support for VS2019 2019-08-19 17:58:22 +02:00
Michele Caini
ac462cfce9 workaround for an issue of VS2019 2019-08-19 17:56:28 +02:00
Michele Caini
d921306dd8 workaround for an issue of VS2019 2019-08-19 17:49:55 +02:00
Michele Caini
05417705ed removed useless move(s) 2019-08-19 17:39:46 +02:00
Michele Caini
1c7d9e3d75 signals meet references (close #289) 2019-08-18 15:21:29 +02:00
Ezekiel Warren
97dd928ca4 benchmark to use entt::component (#290)
* benchmark to use entt::component
* updated list of contributors
2019-08-18 15:04:10 +02:00
Michele Caini
03949f7d60 fixed registry::loader, destroyed is set to null as expected 2019-08-17 16:13:28 +02:00
Michele Caini
effe477351 perf improvement 2019-08-17 15:51:22 +02:00
Michele Caini
12ac858242 cleanup 2019-08-17 15:26:04 +02:00
Michele Caini
eec1937f52 updated entt::null comparison functions 2019-08-17 14:51:14 +02:00
Michele Caini
906cd48fb7 version-less entt::null (close #288) 2019-08-16 15:26:36 +02:00
Michele Caini
78beecf6f1 meta: removed wrong noexcept 2019-08-16 00:41:54 +02:00
Michele Caini
06a0646541 registry::stomp: tests + bug fixing 2019-08-15 15:38:47 +02:00
Michele Caini
0499a6c6f9 deprecated prototype/documented registry::stomp 2019-08-14 15:39:49 +02:00
Michele Caini
0e62f2ee85 updated list of showcases 2019-08-14 15:30:54 +02:00
Michele Caini
2ba4c36af0 registry::stomp (to be tested yet) 2019-08-13 15:58:26 +02:00
Michele Caini
688e6ad79f minor changes 2019-08-12 17:52:13 +02:00
Michele Caini
f8ee0ce185 fixed typo 2019-08-12 17:20:20 +02:00
Michele Caini
a226e06ae8 fixed typo 2019-08-12 15:57:59 +02:00
Michele Caini
e0e51b51b7 added exclusion list to registry::clone (close #286) 2019-08-12 15:43:28 +02:00
Michele Caini
9e9e3d43a8 updated doc 2019-08-11 23:46:20 +02:00
Michele Caini
7691a9bdad entt::component is now available 2019-08-11 17:21:30 +02:00
Michele Caini
22020cae49 doc: minor changes 2019-08-11 15:57:45 +02:00
Michele Caini
6cd910b0f6 added a test to meta to show how to set/get plain variables (close #284) 2019-08-11 15:37:48 +02:00
Michele Caini
b5e9811063 updated doc 2019-08-10 23:42:12 +02:00
Michele Caini
7668c7dd00 updated TODO 2019-08-09 16:12:55 +02:00
Michele Caini
d71f743e85 meta: updated documentation 2019-08-09 16:06:50 +02:00
Michele Caini
73ef0222ac tests for meta policies 2019-08-09 00:40:37 +02:00
Michele Caini
6152387096 meta: policies 2019-08-08 23:45:19 +02:00
Michele Caini
b93ffe3b44 workaround for a bug of clang (close #281) 2019-08-08 23:42:28 +02:00
Michele Caini
478eb7c5b5 minor changes 2019-08-08 17:04:45 +02:00
Michele Caini
6c40d8bd8b updated links 2019-08-08 15:28:33 +02:00
Michele Caini
7026fe4d5a meta: review dtor policy 2019-08-06 00:32:29 +02:00
Michele Caini
9a4491dc50 workaround for a bug of gcc7 2019-08-05 14:40:11 +02:00
Michele Caini
0d835e5014 workaround for an issue of gcc7 2019-08-05 14:29:55 +02:00
Michele Caini
29b5ffb58d rewritten self-contained lambda 2019-08-05 14:19:56 +02:00
Michele Caini
a179ec485b minor changes 2019-08-05 09:50:52 +02:00
Michele Caini
f220a577e0 meta: review 2019-08-05 09:50:45 +02:00
Michele Caini
b22b1d4794 meta: prevent aliasing on prop with future changes 2019-08-04 23:03:31 +02:00
Michele Caini
d2d19e3fbc fixed typo 2019-08-04 23:03:00 +02:00
Michele Caini
19ef290499 minor changes 2019-08-01 14:52:28 +02:00
Michele Caini
f6584b45ed doc: fixed typo 2019-07-31 14:17:24 +02:00
Michele Caini
bad97a8b5b meta system:
* perf improvements
* meta_any::try_cast
* meta_any is void-friendly
* meta_any support for unmanaged objects
* general cleanup
2019-07-28 17:08:56 +02:00
Michele Caini
7116df15d6 FAQ: fixed typo - thanks @huwpascoe and @Kerndog73 2019-07-28 17:08:31 +02:00
Michele Caini
ffbb018d12 updated FAQ 2019-07-22 14:47:08 +02:00
Michele Caini
a4dcdfc403 updated FAQ 2019-07-13 00:14:33 +02:00
Michele Caini
87d901b1ae is now possible to sort non-owning groups using a custom comparison function 2019-07-12 15:41:07 +02:00
Michele Caini
9126651cb9 added the possibility to sort free instances for owned types 2019-07-11 14:46:25 +02:00
Michele Caini
3996ce8ce5 test: storage::sort for ranges 2019-07-10 15:00:00 +02:00
Michele Caini
6340423b32 updated doc 2019-07-10 14:35:38 +02:00
Michele Caini
0d921dc1fc added support for free/member functions to meta_conv 2019-07-10 14:29:17 +02:00
Michele Caini
25afea4f9c meta: setters and getters receive instances as references now 2019-07-10 14:26:40 +02:00
Michele Caini
ba46bf3af5 review: sort/respect 2019-07-09 17:26:31 +02:00
Michele Caini
416b4885f9 updated doc (close #277) 2019-07-09 15:15:26 +02:00
Michele Caini
9a10ee0efa updated doc (see #277) 2019-07-09 14:56:32 +02:00
Michele Caini
d63b78f0e7 minor changes 2019-07-09 14:01:56 +02:00
Michele Caini
99411241f6 fixed typo 2019-07-09 13:59:28 +02:00
Michele Caini
941fb1349b added cache::each (close #275) 2019-07-09 13:58:44 +02:00
Michele Caini
a470925305 typo 2019-07-06 15:23:40 +02:00
Michele Caini
e3ac292af1 return types for meta_func are no longer ambiguous (close #274) 2019-07-06 00:10:37 +02:00
Michele Caini
260b9a8d0d meta_any meets void 2019-07-05 23:03:01 +02:00
Michele Caini
530407f9b2 minor changes 2019-07-05 14:13:10 +02:00
Michele Caini
52e7b50e9b sigh: more tests 2019-07-05 13:36:47 +02:00
Michele Caini
23aa097d0a fixed the clone functionality (close #273) 2019-07-05 08:41:24 +02:00
Michele Caini
0bea153aa0 more on sigh, connection and scoped_connection 2019-07-04 15:04:17 +02:00
Michele Caini
00be58e65d cleanup - thanks @Kerndog73 2019-07-03 15:28:11 +02:00
Michele Caini
a5cc985bc2 fixed doc for sigh - thanks @Kerndog73 2019-07-03 15:18:36 +02:00
Michele Caini
afba54fb5e updated doc for delegate/sigh (close #267) 2019-07-02 15:31:55 +02:00
Michele Caini
46150a2da4 delegate works now also with move only types (close #272) 2019-07-02 14:49:49 +02:00
Michele Caini
842988c2ad workaround for an issue of MSVC 2019-07-01 16:21:07 +02:00
Michele Caini
ebad34ae51 sigh: more tests 2019-07-01 15:26:11 +02:00
Michele Caini
bceb359438 minor changes 2019-07-01 15:16:38 +02:00
Michele Caini
19a32134ab tests: sigh connection/scoped_connection 2019-07-01 15:09:55 +02:00
Michele Caini
e44a71daf2 delegate::operator==/!= test the instance as a whole now 2019-06-30 22:19:44 +02:00
Michele Caini
573750a43d added connection/scoped_connection (#267 - test required) 2019-06-30 21:32:45 +02:00
Michele Caini
dd4c73b41f review: signal/sink 2019-06-30 21:32:45 +02:00
Michele Caini
094326220f cleanup 2019-06-30 21:32:45 +02:00
Michele Caini
729a071284 sigh: a more modern approach (as in modern C++) for collectors 2019-06-30 21:32:45 +02:00
Michele Caini
ee7857a560 sigh: removed invoker class 2019-06-30 21:32:45 +02:00
Michele Caini
4cba604712 updated list of projects (close #270) 2019-06-30 20:49:11 +02:00
Michele Caini
18a62797d9 updated FAQ 2019-06-30 17:11:40 +02:00
Michele Caini
5d4ac62c9e removed superfluous base class 2019-06-28 10:35:35 +02:00
Michele Caini
2ef8c41b48 updated list of contributors 2019-06-27 08:52:30 +02:00
Michele Caini
891831a96a Merge pull request #269 from Lawrencemm/conan_editable
Conan: Support package in editable mode
2019-06-27 08:51:38 +02:00
Michele Caini
11d18fa1af entity identifiers as enum classes (close #256) 2019-06-23 16:09:07 +02:00
Lawrence Millar-Madigan
ad69cbc681 Conan: Support package in editable mode
Add a method to the recipe that maps the include path to "src" when the package is put into "editable mode". See:
https://docs.conan.io/en/latest/developing_packages/editable_packages.html
2019-06-22 14:55:02 +10:00
Michele Caini
10995de7bd cleanup - thanks to @Kerndog73 (see #267) 2019-06-19 17:32:32 +02:00
Michele Caini
c4d78c06eb fixed group::sort with components 2019-06-19 15:25:30 +02:00
Michele Caini
eb252321a7 minor changes 2019-06-18 23:23:42 +02:00
Michele Caini
04dd1447ee minor changes 2019-06-17 16:14:20 +02:00
Michele Caini
b14edbe1f4 review: observer 2019-06-16 18:39:56 +02:00
Michele Caini
c697e52c0c fixed range destroy - it's probably slightly slower now but it works also with views and groups (close #261) 2019-06-16 17:14:32 +02:00
Michele Caini
b6911da8f0 CI runs now in debug mode (close #263) 2019-06-16 17:12:41 +02:00
Michele Caini
038448865b workaround for an issue with VS (close #264) 2019-06-16 17:10:25 +02:00
Michele Caini
9e44fdbc55 review: test 2019-06-16 17:08:37 +02:00
Michele Caini
e088f0f31e observer: when -> where (close #265) 2019-06-16 15:16:04 +02:00
Michele Caini
6101feda10 added default constructor to actor 2019-06-13 06:52:01 +02:00
Michele Caini
7a3fa68468 added actor::operator bool() + minor changes 2019-06-12 17:36:26 +02:00
Michele Caini
16a11638a7 updated observer (close #260) 2019-06-11 17:49:08 +02:00
Michele Caini
43932492a7 meta: overall improvements 2019-06-10 23:19:19 +02:00
Michele Caini
8d08ffc4ae slightly faster destroy 2019-06-10 00:24:19 +02:00
Michele Caini
a5a4ee4bfe doc: observer/reactive systems 2019-06-10 00:24:08 +02:00
Michele Caini
e66f3a80e8 observer (they call me reactive system) 2019-06-08 16:59:22 +02:00
Michele Caini
e69efacb47 updated links (close #258) 2019-06-08 14:06:54 +02:00
Michele Caini
528b361e34 meta any: in place construction/emplace 2019-06-08 14:03:31 +02:00
Michele Caini
a77bb3d341 Update FUNDING.yml 2019-06-07 09:52:53 +02:00
Michele Caini
6b17385ad7 updated tests 2019-06-06 14:58:25 +02:00
Michele Caini
33fc01ef4b Create FUNDING.yml 2019-06-06 12:08:59 +02:00
Michele Caini
482324c0bd avoid forcing conversions because of an issue with VS2019 (close #257) 2019-06-04 15:02:30 +02:00
Michele Caini
0ab13a3a70 test coverage 2019-06-03 23:50:09 +02:00
Michele Caini
699fb73726 bug fixing 2019-06-01 15:18:58 +02:00
Michele Caini
ddba29cc09 more on sigh/sink 2019-05-31 22:49:37 +02:00
Michele Caini
1cdb9ef47c updated TODO 2019-05-31 15:11:02 +02:00
Michele Caini
ade3305b4c removed useless references to components from dependencies 2019-05-31 15:10:46 +02:00
Michele Caini
bd2bd37bc4 updated sigh: instances and payload matter now 2019-05-31 15:10:06 +02:00
Michele Caini
ee6f55193e updated doc 2019-05-31 15:09:27 +02:00
Michele Caini
1868df61ea fixed typo 2019-05-31 15:08:49 +02:00
Michele Caini
dfa1fc7b09 review: group handler 2019-05-31 00:14:47 +02:00
Michele Caini
a52b0495f1 minor changes 2019-05-31 00:11:00 +02:00
Michele Caini
ce00eee29f the delegate accepts now also functions with a shorter list of parameters 2019-05-31 00:10:42 +02:00
Michele Caini
858bfade6a a more flexible delegate 2019-05-30 21:54:12 +02:00
Michele Caini
4535cf9274 perf improvement 2019-05-29 22:50:34 +02:00
Michele Caini
84d1fa54ec added the excluded components to the list of template parameters of a group 2019-05-28 15:03:28 +02:00
Michele Caini
7cf4721efa review: as_group 2019-05-27 22:05:16 +02:00
Michele Caini
6578838587 fixed include 2019-05-27 21:49:19 +02:00
Michele Caini
46b7c4717f workaround for an issue in clang 6 2019-05-27 12:08:25 +02:00
Michele Caini
52bcf63554 more on hashed strings and unique identifiers 2019-05-27 00:07:40 +02:00
Michele Caini
37ec97550e fixed typo 2019-05-26 15:38:37 +02:00
Michele Caini
55e20abea8 cleanup 2019-05-25 23:51:16 +02:00
Michele Caini
ec7d81dc7a added support for wstring to hashed_string 2019-05-25 23:42:06 +02:00
Michele Caini
e93387b719 maybe_atomic_t no longer exists 2019-05-25 22:21:44 +02:00
Michele Caini
050b476fc3 suppress warnings 2019-05-25 22:08:56 +02:00
Michele Caini
5b5ffd7eaf Remove superfluous inlines (was PR #249) 2019-05-25 22:03:34 +02:00
Michele Caini
39bf5fdc05 minor changes 2019-05-25 21:43:58 +02:00
Michele Caini
09ff43ef0a updated doc 2019-05-22 08:44:49 +02:00
Michele Caini
5a02aeaa89 raw() is no longer in use 2019-05-22 08:44:43 +02:00
Martin Hammerchmidt
2a3a91af42 Fix: remove garbage unicode U+FEFF failing compilation on Macos (#248, close #247) 2019-05-22 00:19:04 +02:00
Michele Caini
d4a64e93e0 mutable handle 2019-05-21 15:02:30 +02:00
Michele Caini
c126b64892 updated doc 2019-05-21 15:01:55 +02:00
Michele Caini
526814b9ed added registry::ctx_or_set 2019-05-19 23:40:15 +02:00
Michele Caini
bc3b790f13 removed wrong ENTT_NOEXCEPT 2019-05-19 23:25:05 +02:00
Michele Caini
1fb0765443 registry: internal changes 2019-05-19 00:23:48 +02:00
Michele Caini
c4b85f9bda minor changes 2019-05-18 23:53:10 +02:00
Michele Caini
161b5530b9 added [view|group]::less 2019-05-18 14:11:12 +02:00
Michele Caini
e03979ebdc cleanup 2019-05-18 14:05:04 +02:00
Michele Caini
356c0ab594 now working on v3.1.0 2019-05-18 14:04:41 +02:00
Michele Caini
85152bac34 doc 2019-05-15 23:41:51 +02:00
Michele Caini
c832888d56 doc 2019-05-15 23:27:44 +02:00
Michele Caini
cac1b87b44 assert -> ENTT_ASSERT 2019-05-15 23:23:06 +02:00
Michele Caini
30f71b2ed6 assert (has) on replace for empty components 2019-05-15 23:07:24 +02:00
Michele Caini
9f8e9a1916 code coverage 2019-05-15 22:58:46 +02:00
Michele Caini
8519fe57ae minor changes 2019-05-15 16:05:14 +02:00
Michele Caini
1442a3853c minor changes 2019-05-15 15:53:35 +02:00
Michele Caini
2658ddf868 code coverage 2019-05-15 15:25:06 +02:00
Michele Caini
f95eecfcc6 minor changes 2019-05-13 17:29:54 +02:00
Michele Caini
1e598cca31 added validity checks (close #245) 2019-05-12 11:50:46 +02:00
Michele Caini
f3eaeb96f0 review: instance-less pools for empty types 2019-05-10 23:45:26 +02:00
Michele Caini
91fac8049c perf improvement 2019-05-10 10:17:32 +02:00
Michele Caini
b1325a7514 suppress wrong warning 2019-05-09 16:37:41 +02:00
Michele Caini
150b83b4f3 instance-less pools for empty components 2019-05-09 16:20:44 +02:00
Michele Caini
3a3a5dc071 updated links (close #242) 2019-05-08 23:42:17 +02:00
Michele Caini
9b76a5bd66 (maybe) workaround for a bug in msvc 2019-05-07 23:48:12 +02:00
Michele Caini
850a1f9155 workaround for a bug in GCC7 2019-05-07 23:19:09 +02:00
Michele Caini
ef6c8a38d0 minor changes 2019-05-07 23:16:21 +02:00
Michele Caini
e228cb6648 prepare for instance-less pools for empty components 2019-05-07 16:29:31 +02:00
Michele Caini
3a53cac607 rework to easily allow storage specializations 2019-05-07 00:13:05 +02:00
Michele Caini
df4bd57c82 cleanup 2019-05-07 00:12:30 +02:00
Michele Caini
de737fc72d hashed string: support for string views 2019-05-06 00:07:24 +02:00
Michele Caini
ccea7a5783 review: cloning functionality 2019-05-05 22:34:15 +02:00
Michele Caini
5b84e1528a workaround for a bug (?) of msvc 2019-05-05 16:07:45 +02:00
Michele Caini
7382addfe4 minor changes 2019-05-04 18:06:03 +02:00
Michele Caini
b8dd6822ac type_list_cat[_t]/type_list_unique[_t] (close #238) 2019-05-02 22:54:34 +02:00
Sztergbaum Roman
8c47b85b9b add single include of EnTT framework (#236) 2019-05-01 16:39:01 +02:00
Michele Caini
4d2666a0e3 minor changes (close #235) 2019-05-01 15:30:12 +02:00
Michele Caini
fc18eaa292 updated README (close #228 - thanks to @ColinH for the review) 2019-04-30 23:38:26 +02:00
Michele Caini
22199cdd87 updated doc (close #232) 2019-04-30 23:06:35 +02:00
Michele Caini
76820f25c6 updated links 2019-04-30 22:42:12 +02:00
Michele Caini
bac4984496 updated doc (close #231) 2019-04-30 18:11:24 +02:00
Michele Caini
1414c5feac workaround for a wrong warning emitted by GCC8 (close #227) 2019-04-30 17:51:03 +02:00
Michele Caini
c344f63154 fixed cloning functionality (close #237) 2019-04-30 16:54:45 +02:00
Michele Caini
77ea28bef2 slightly improved registry::replace (thanks to ColinH for pointing it out) 2019-04-29 23:19:43 +02:00
Michele Caini
bf63b15e07 updated doc and links 2019-04-29 19:05:42 +02:00
Michele Caini
ce05504278 fixed typos (see #235) 2019-04-29 18:13:18 +02:00
Michele Caini
95b90974a2 removed wrong ENTT_NOEXCEPT (close #233) 2019-04-29 18:11:35 +02:00
Elias Ku
1adbef7352 benchmark fix (#225)
only one entity has all the components
2019-04-25 14:09:23 +02:00
Michele Caini
3109928dcd allows setting a continuous loader as registry context (close #224) 2019-04-24 15:01:25 +02:00
Michele Caini
24dd63789c updated conan test 2019-04-24 12:22:02 +02:00
Michele Caini
27225e6b5e updated README 2019-04-23 14:55:20 +02:00
Michele Caini
2994978fdb updated README 2019-04-23 14:52:37 +02:00
Michele Caini
ffd864dc72 review: registry internals 2019-04-23 14:50:31 +02:00
Michele Caini
53a2c88d4b preparing for v3 2019-04-22 18:00:07 +02:00
Michele Caini
444ae73ffb updated doc 2019-04-22 17:59:54 +02:00
Michele Caini
9f5f4b9c91 static registry::entity/version (close #223) 2019-04-22 17:31:25 +02:00
Michele Caini
ffd2af0257 minor changes 2019-04-21 17:03:06 +02:00
Michele Caini
b983845745 meta: built-in support for arrays 2019-04-19 14:01:00 +02:00
Michele Caini
cfcc6e1029 fix shadow warnigns and positive lambda explanation (#221) 2019-04-16 22:28:24 +02:00
Michele Caini
6fea4377b2 const correctness 2019-04-14 22:48:42 +02:00
Michele Caini
e01230e425 better sorting functionality for groups 2019-04-14 22:19:17 +02:00
Michele Caini
f66db81edd sort owning groups by owned components 2019-04-14 18:29:17 +02:00
Michele Caini
a47471d0ae updated doc 2019-04-14 16:51:42 +02:00
Michele Caini
06b5a91cd3 bug fixing 2019-04-14 15:39:09 +02:00
Michele Caini
8aacf3497e review: snapshot (close #219) 2019-04-14 15:31:00 +02:00
Michele Caini
7fc5e304e3 cleanup 2019-04-13 16:19:23 +02:00
Michele Caini
65536febd5 minor changes 2019-04-13 16:02:25 +02:00
Michele Caini
60f7910840 sligthly better perf 2019-04-13 15:23:26 +02:00
Michele Caini
829c3c612e cleanup 2019-04-12 23:03:30 +02:00
Michele Caini
355c7b59aa cleanup 2019-04-10 13:56:19 +02:00
Michele Caini
6508cdc823 doc: review 2019-04-09 13:44:43 +02:00
Michele Caini
9ef9f603a3 updated links 2019-04-09 09:54:42 +02:00
Michele Caini
e76f881c67 renaming things 2019-04-09 09:47:35 +02:00
Michele Caini
4292d2ce86 signal on replace 2019-04-09 08:54:19 +02:00
Michele Caini
ae927c5600 workaround to make it compile also with clang 5 2019-04-08 22:45:39 +02:00
Michele Caini
57e2eed771 cleanup 2019-04-08 22:13:10 +02:00
Michele Caini
ca807b892f added instance of component to construction signal 2019-04-08 17:48:09 +02:00
Michele Caini
9bc015d10e made the groups sortable 2019-04-06 00:58:47 +02:00
Michele Caini
59b204c3bc fixed typo 2019-04-06 00:57:18 +02:00
Michele Caini
4ac2472a62 minor changes 2019-04-05 23:47:20 +02:00
Michele Caini
054b9c2402 updated doc 2019-04-05 23:17:58 +02:00
Michele Caini
96f267cbfd sort allows to work with entities 2019-04-05 23:00:58 +02:00
Michele Caini
2018163107 Revert "let's see if vs solved its problems with aliases"
This reverts commit 0be2f1b295.
2019-04-04 22:30:24 +02:00
Michele Caini
253b5b1fb7 updated doc 2019-04-04 22:30:13 +02:00
Michele Caini
0be2f1b295 let's see if vs solved its problems with aliases 2019-04-04 22:23:55 +02:00
Michele Caini
a8838fc611 signals on replace 2019-04-04 22:20:24 +02:00
Michele Caini
d02636e370 rollback on signals, no need for these changes 2019-04-03 22:15:24 +02:00
Michele Caini
f3c1b09188 updated doc (close #216) 2019-04-03 14:26:23 +02:00
Michele Caini
7bac69f360 fixed typo 2019-04-01 17:32:52 +02:00
Michele Caini
5a3fdd225e meta: support to unregister types 2019-04-01 14:40:45 +02:00
Michele Caini
4660f58653 hashed string is copyable/moveable now 2019-04-01 14:40:24 +02:00
Michele Caini
5fabca3616 updated doc 2019-03-31 23:14:36 +02:00
Michele Caini
edb74dae87 review: cache (close #213) 2019-03-31 16:08:27 +02:00
Michele Caini
31b833b46a minor changes 2019-03-31 16:08:00 +02:00
Michele Caini
21bc1c51ba updated doc (see #213) 2019-03-31 15:43:19 +02:00
Michele Caini
4f6cab9a45 work around for a bug of msvc (close #212) 2019-03-31 15:16:16 +02:00
Michele Caini
33626fa47c mostly renaming 2019-03-30 15:27:33 +01:00
Michele Caini
9fcc1e17f2 signal handler: substitution 2019-03-30 14:56:43 +01:00
Michele Caini
a5f48f8af8 review: registry::loader 2019-03-30 00:46:38 +01:00
Michele Caini
3dfa526ec6 shrink_to_fit available also for non-owning groups 2019-03-30 00:37:49 +01:00
Michele Caini
dc28066017 shrink_to_fit to release pages 2019-03-30 00:09:49 +01:00
Michele Caini
2f2edfbde8 updated TODO 2019-03-29 16:10:56 +01:00
Michele Caini
3beac98499 updated doc 2019-03-29 14:03:12 +01:00
Michele Caini
f70ce9dd26 added faq doc 2019-03-29 00:23:16 +01:00
Michele Caini
e301b9d399 imnor changes 2019-03-28 23:36:05 +01:00
Michele Caini
42f92ecd7a updated TODO 2019-03-28 18:32:21 +01:00
Michele Caini
27eb0d04b9 introduced ENTT_DISABLE_ASSERT/ENTT_ASSERT macros 2019-03-28 17:49:34 +01:00
Michele Caini
112d13c127 updated doc 2019-03-28 14:56:50 +01:00
Michele Caini
16afc9d0f8 fixed link 2019-03-28 12:45:04 +01:00
Michele Caini
5b4889b46b removed unsafe_* functions 2019-03-28 12:12:48 +01:00
Michele Caini
f7eed0e2af paged reverse array 2019-03-28 00:09:49 +01:00
Michele Caini
cce287e8a6 page size macro/utility 2019-03-28 00:04:27 +01:00
Michele Caini
9a001ebacc review: process/scheduler (close #211) 2019-03-27 14:55:43 +01:00
Michele Caini
79ab4c02bb updated links (close #210) 2019-03-26 18:56:09 +01:00
Michele Caini
b6de70a3a6 Revert "cleanup"
This reverts commit 15cafe2d78.
2019-03-26 18:39:39 +01:00
Michele Caini
15cafe2d78 cleanup 2019-03-26 18:00:41 +01:00
Michele Caini
90197abc5c test coverage 2019-03-25 16:39:57 +01:00
Michele Caini
2147436a2a more on context variables 2019-03-25 15:21:13 +01:00
Michele Caini
8ef0c66966 fixed typo 2019-03-25 09:07:33 +01:00
Michele Caini
2779ad6c6f added unset for context variables 2019-03-24 23:12:03 +01:00
Michele Caini
f30fe3c746 fixed typo 2019-03-24 22:53:56 +01:00
Michele Caini
f71496693f updated example in the README file (close #209) 2019-03-23 13:51:18 +01:00
Michele Caini
e4cd5dbf1f fixed benchmark (thanks to Kerndog73) 2019-03-22 23:05:07 +01:00
Michele Caini
ffd5defcb6 updated benchmark 2019-03-22 22:17:58 +01:00
Michele Caini
0b81421124 updated TODO 2019-03-22 17:43:47 +01:00
Michele Caini
0f73b9e07f clean up 2019-03-22 16:49:19 +01:00
Michele Caini
d3078699d6 type_list is now an empty type 2019-03-22 15:56:19 +01:00
Michele Caini
cc39f9f891 clean up 2019-03-22 15:32:50 +01:00
Michele Caini
70608df843 improved multi component view 2019-03-22 10:12:04 +01:00
Michele Caini
e7f2c6b5ba more on context variables 2019-03-21 22:04:55 +01:00
Michele Caini
420fc9aa24 removed annoying/useless benchmark 2019-03-21 21:39:44 +01:00
Michele Caini
200012fd41 added component-to-entity functionality to the sparse sets + renamed some member functions 2019-03-21 17:25:13 +01:00
Michele Caini
e3c858278b minor changes 2019-03-21 15:06:10 +01:00
Michele Caini
1ec7c3afa4 context variables (aka tags revenge) 2019-03-21 14:52:44 +01:00
Michele Caini
6ab0b60f12 avoid global shadowing to an extent (close #207) 2019-03-20 08:33:26 +01:00
Michele Caini
5ec38b44b0 Update README.md 2019-03-20 08:24:22 +01:00
Michele Caini
39c2294d43 updated doc 2019-03-17 16:24:08 +01:00
Michele Caini
3b92481133 added utility functions to groups/views 2019-03-17 15:55:54 +01:00
Michele Caini
6b8d24d2f5 shared types -> named types 2019-03-15 14:42:50 +01:00
Michele Caini
62d6ff61ab updated links 2019-03-15 08:42:35 +01:00
Michele Caini
f7c056145a added fwd decl for runtime view 2019-03-13 22:20:25 +01:00
Michele Caini
451f54b036 added NovusCore to EnTT in Action (close #204) 2019-03-13 21:41:39 +01:00
Michele Caini
ef378de883 updated links 2019-03-13 10:47:50 +01:00
Michele Caini
2f2b63da4a renaming: label -> tag 2019-03-13 08:54:21 +01:00
Michele Caini
01d8f2c7e6 updated TODO 2019-03-12 23:23:25 +01:00
Michele Caini
330d553ffb removed potentially ambiguous call (fix #203) 2019-03-12 23:19:17 +01:00
Michele Caini
f1d0895eb1 added forward declaration for what matters (close #178) 2019-03-12 22:21:58 +01:00
Michele Caini
4eaf6415b7 cleanup 2019-03-12 15:08:53 +01:00
Michele Caini
f0650bf64f updated documentation 2019-03-12 14:58:07 +01:00
Michele Caini
44856c6207 removed useless remove_reference_t 2019-03-12 14:56:28 +01:00
Michele Caini
dcbf6e43c7 added get_or_assign (close #202) 2019-03-12 14:50:47 +01:00
Michele Caini
0d22da672d fixed test 2019-03-12 00:09:59 +01:00
Michele Caini
a62471b9b8 added default constructor to resource handle 2019-03-09 15:39:10 +01:00
Michele Caini
f673f2c5bc added each-with-type to multiple component views 2019-03-09 14:41:31 +01:00
Michele Caini
a7caae5c17 minor changes 2019-03-09 13:46:38 +01:00
Michele Caini
d0deefd0d7 review: registry (fix #199) 2019-03-09 00:35:11 +01:00
Michele Caini
9810da6982 batch add is now available 2019-03-08 22:49:14 +01:00
Michele Caini
cb93a3bee3 minor changes 2019-03-08 22:49:14 +01:00
Michele Caini
aeacc3e39d updated list of patrons 2019-03-08 15:29:47 +01:00
Michele Caini
e1c861cc8a updated links.md 2019-03-06 16:21:19 +01:00
Michele Caini
37ee05a576 updated TODO 2019-03-02 14:34:35 +01:00
Michele Caini
07a2bd817d minor changes 2019-03-01 17:35:54 +01:00
Michele Caini
78c171e0c2 meta: review 2019-03-01 17:07:08 +01:00
Michele Caini
1ae436cb08 added compile-time checks 2019-03-01 16:38:53 +01:00
Michele Caini
b0d3a84d1e setters/getters correctly handle references now (#fix 196) 2019-03-01 16:25:46 +01:00
Michele Caini
1acd9ca981 updated list of contributors 2019-03-01 08:45:43 +01:00
WizardIke
72467b6b9c Fix getting distance between forward iterators (#195) 2019-03-01 08:44:24 +01:00
Michele Caini
862c85387c Update README.md 2019-03-01 08:32:38 +01:00
Michele Caini
eeeca3e21c fix #194 2019-02-28 00:06:12 +01:00
Michele Caini
4ee4af7fd4 updated registry::destroy for ranges 2019-02-27 23:56:07 +01:00
Michele Caini
d131cc1871 fixed typo 2019-02-27 22:38:47 +01:00
Michele Caini
bd21ee318d fix #193 2019-02-27 22:12:55 +01:00
Michele Caini
02e7fc0bca updated README 2019-02-27 15:11:53 +01:00
Michele Caini
51ba46970e updated README (close #191) 2019-02-27 15:07:57 +01:00
Michele Caini
9d65b44ab7 removed pointless extra arguments from dispatcher (close #190) 2019-02-27 15:04:17 +01:00
Michele Caini
350d51a92d const T & = --> std::as_const 2019-02-27 15:03:39 +01:00
Michele Caini
2a8202caa7 bug fixing 2019-02-27 14:43:55 +01:00
Michele Caini
9e4591554f minor changes (close #192) 2019-02-27 14:04:40 +01:00
Michele Caini
e98ecfd1f9 cleanup 2019-02-27 13:57:24 +01:00
Michele Caini
1cbf5c4359 removed from the todo list some requests that would not bring benefits because they cannot be optimized 2019-02-25 23:56:18 +01:00
Michele Caini
0446faeb6f minor changes 2019-02-25 23:30:26 +01:00
Michele Caini
0417ea23c3 updated README 2019-02-25 23:11:59 +01:00
Michele Caini
de9886e011 more tests 2019-02-25 22:12:19 +01:00
Michele Caini
770e57c361 emitter works across boundaries now 2019-02-24 23:59:15 +01:00
Michele Caini
d80a00701d dispatcher works across boundaries now 2019-02-24 23:37:08 +01:00
Michele Caini
62d3cd8795 updated doc 2019-02-24 21:54:27 +01:00
Michele Caini
ad9ec22fd1 updated doc 2019-02-23 23:47:44 +01:00
Michele Caini
293985ec10 EXPAND -> ENTT_EXPAND 2019-02-23 23:47:41 +01:00
Michele Caini
105598ac8e (maybe) solved issue due to a bug of msvc 2019-02-23 23:47:38 +01:00
Michele Caini
ff93f2220a workaround for msvc that seems not to appreciate much macros 2019-02-23 23:47:35 +01:00
Michele Caini
33fddcb289 added a test that goes across boundaries 2019-02-23 23:47:31 +01:00
Michele Caini
6a09bf2c54 increase code coverage with minimum effort 2019-02-23 23:47:26 +01:00
Michele Caini
2913f5bbbe registry: refactoring 2019-02-23 23:47:22 +01:00
Michele Caini
671bdb09b8 macro review 2019-02-23 23:47:15 +01:00
Michele Caini
353a0d14a4 get rid of clang errors 2019-02-23 23:47:11 +01:00
Michele Caini
e0fd83211b lib support for registry (first draft) 2019-02-23 23:47:03 +01:00
Michele Caini
2c94d9cc37 slightly improved 2019-02-23 23:47:00 +01:00
Michele Caini
f7dec412ec minor changes 2019-02-23 23:46:52 +01:00
Michele Caini
f7c756d215 wip: make shared traits usable 2019-02-23 23:46:48 +01:00
Michele Caini
617635a989 wip: lib support 2019-02-23 23:46:42 +01:00
Michele Caini
ea3b2c4713 fixed link to patreon 2019-02-20 09:42:36 +01:00
Michele Caini
15455a7e68 updated AUTHORS 2019-02-20 09:14:58 +01:00
Michele Caini
ad5cedc08c cleanup 2019-02-19 14:43:45 +01:00
Michele Caini
328f0c6da8 fixed error with clang 2019-02-17 16:27:20 +01:00
Michele Caini
8ef818f581 hashed string: added possibility to use both uint32 and uint64 2019-02-17 16:19:58 +01:00
Michele Caini
48dace64e4 updated TODO 2019-02-17 16:17:32 +01:00
Michele Caini
ab5837c7c4 sigh: more tests 2019-02-17 16:17:21 +01:00
Michele Caini
9fbdbc1844 review: delegate/sigh 2019-02-17 16:17:13 +01:00
Michele Caini
54129cecc2 typo 2019-02-16 15:59:10 +01:00
Michele Caini
69c514d1a4 doc: fixed typo 2019-02-13 15:03:14 +01:00
Michele Caini
7047d12eba updated doc 2019-02-13 14:59:59 +01:00
Michele Caini
8ab61637f0 fixed get-or-assign (close #185) 2019-02-12 14:29:15 +01:00
Michele Caini
7e0bd92593 fix #184 - conflicts between partial-owning groups aren't correctly detected 2019-02-10 23:00:59 +01:00
Michele Caini
99d9a9f44e fixed tests 2019-02-10 13:21:45 +01:00
Erik Scholz
9c55111e14 [DOCS] removed excess "registry." in groups code sample (#181) 2019-02-09 19:00:23 +01:00
Michele Caini
b1d6ba57ad use the right family for groups 2019-02-09 14:33:15 +01:00
Michele Caini
47e9330646 groups 2019-02-09 14:04:09 +01:00
Michele Caini
e16a3d503c simplified sparse_set::clone 2019-02-08 17:21:03 +01:00
Michele Caini
dc9c93b347 cleanup 2019-02-08 17:20:39 +01:00
Michele Caini
faceff620a exclude is now a variable template 2019-02-08 17:20:21 +01:00
Michele Caini
a5a6a58556 removed indexes from persistent views 2019-02-08 17:20:14 +01:00
Michele Caini
45fdab27c9 meta_any: fixed #180 2019-02-01 23:48:00 +01:00
Michele Caini
8b360479f4 updated list of contributors 2019-02-01 22:30:08 +01:00
Michele Caini
b170f2f69a reduce the number of template instantiations 2019-02-01 22:29:24 +01:00
Michele Caini
7f1abab9ae bug fixing (find on views) 2019-01-29 09:00:34 +01:00
Michele Caini
2ec7bc878e minor changes 2019-01-29 08:42:37 +01:00
Michele Caini
e97d9bb095 maybe_atomic_type -> maybe_atomic_t 2019-01-29 08:40:49 +01:00
Michele Caini
2f3b02e870 typo 2019-01-29 08:40:27 +01:00
Michele Caini
11b7a45c32 improved registry::reset 2019-01-29 08:40:22 +01:00
Michele Caini
fb10d2f9c2 cleanup 2019-01-29 08:38:59 +01:00
Michele Caini
60bb7f66cf updated todo list 2019-01-29 08:38:48 +01:00
Michele Caini
ad5143169a removed registry::component_pool 2019-01-29 08:36:50 +01:00
Michele Caini
d68c90a429 removed registry ::pool/::managed 2019-01-29 08:36:42 +01:00
Michele Caini
8a51549395 updated list of contributors 2019-01-26 16:43:31 +01:00
corystegel
9f997fe1fa Fix sparse_set invalid state when component constructor throws (#176)
Fix sparse_set invalid state when component constructor throws

Previously, a component's constructor that throws would cause the sparse
set to think that the entity still exists in the set. This is because the underlying
sparse set that stores the entities will have its entry added before the component
is added to the component set.

This could cause a number of invalid memory access problems such as the following:

1) Exception triggers destructor of enclosing object that then tries to remove
   the component that was just added. SparseSet<entity_t, Component>::has() will
   return true for the entity but when destroy() is called "instances" will be empty
   so instances.back() will be invalid.

2) If the exception is handled then calling get(entity) for the same entity
   identifier that initially threw the exception will give a position for that
   entity even though it was not added. This can cause an invalid memory access
   or accessing the data of a different stored component.
2019-01-25 22:36:30 +01:00
Michele Caini
00f1f6d86b updated sigh + added future plans for this class to the todo list 2019-01-25 14:39:00 +01:00
Michele Caini
be72728000 updated doc 2019-01-24 14:30:16 +01:00
Michele Caini
f558126854 fixed typo + added tests (thanks to Kerndog73) 2019-01-24 08:36:55 +01:00
Michele Caini
08ed1500ac updated doc + minor changes 2019-01-23 16:06:53 +01:00
Michele Caini
f0926f1cc7 removed final specifier 2019-01-23 15:51:55 +01:00
Michele Caini
fe3f6aa22b signal: review 2019-01-23 15:42:29 +01:00
Michele Caini
1325f75e81 updated links 2019-01-23 15:37:15 +01:00
Michele Caini
4b1d3a7b6f allow for null entity members (#174) 2019-01-13 22:28:40 +01:00
Michele Caini
404afcdfd6 added default constructor to handle 2019-01-12 14:49:14 +01:00
Michele Caini
99793180f1 minor changes 2019-01-08 17:24:42 +01:00
Michele Caini
c91f9beddb removed raw view + added extended each member function to all the other views 2019-01-04 10:44:27 +01:00
Michele Caini
c9c5ccaa6d updated file links.md 2019-01-04 10:43:18 +01:00
Michele Caini
da42de7ac8 updated copyright 2019-01-02 15:36:14 +01:00
Michele Caini
3c9a6ad218 delegate: curried functions can accept linked parameters either by value or by reference 2019-01-01 15:01:13 +01:00
Michele Caini
f0389d6b47 updated TODO 2018-12-31 17:52:58 +01:00
Michele Caini
76eab21738 appveyor runs the tests now 2018-12-29 00:57:23 +01:00
Michele Caini
3b32c57d94 delegate: updated constraints for curried functions 2018-12-28 22:53:21 +01:00
Michele Caini
d119032cb9 added support for extra arguments to dispatcher 2018-12-28 16:10:09 +01:00
Michele Caini
397a04b49c typo 2018-12-26 15:00:46 +01:00
Michele Caini
d32c8e9457 empty type optimization 2018-12-26 14:50:30 +01:00
Michele Caini
ebb2974ca7 added exclude alias for type_list 2018-12-21 16:23:23 +01:00
Michele Caini
cbb1131a5d added maybe_atomic_type 2018-12-21 15:35:27 +01:00
Michele Caini
d9de7fe3b0 more tests 2018-12-19 15:26:21 +01:00
Michele Caini
24ba692884 delegate: support for curried functions 2018-12-19 14:18:59 +01:00
Michele Caini
960bbbde29 more on registry::clone (close #161) 2018-12-17 21:39:30 +01:00
Michele Caini
eb79b79c35 updated contributors list 2018-12-16 13:50:04 +01:00
Michele Caini
8212ed6d87 monostate: helper template variable 2018-12-16 13:49:13 +01:00
Michele Caini
c639cb5285 added hashed_string::to_value for direct computation 2018-12-16 13:49:09 +01:00
ceeac
a88fd1c669 Fix compile error when compiling example with GCC8 (#170) 2018-12-16 13:48:23 +01:00
Michele Caini
b053f23d15 registry::clone supports type list now (close #165) 2018-12-14 22:24:52 +01:00
Michele Caini
9d32a89491 doc: used @copydoc where possible 2018-12-13 22:50:23 +01:00
Michele Caini
cb9a147fff clone functionality (close #161) 2018-12-13 08:55:43 +01:00
Michele Caini
c9fddedbf1 UB/Crash in entt::insertion_sort (#167) 2018-12-11 14:17:39 +01:00
ProWolf
530bbbe4c5 added compile time version macros (#166) 2018-12-10 22:53:22 +01:00
Michele Caini
73e5a9f45b support for filtered persistent views 2018-12-09 13:50:41 +01:00
Michele Caini
72d4e947b6 Conan: Add CI setup #163 2018-12-05 14:51:56 +01:00
Michele Caini
41750c3639 typo 2018-11-30 14:56:48 +01:00
Michele Caini
52b36f38a0 delegate: a few more tests 2018-11-30 11:52:39 +01:00
Michele Caini
58ceb454dd review: delegate 2018-11-30 10:34:49 +01:00
Michele Caini
9e5a2db4c5 a few more links 2018-11-29 15:20:48 +01:00
Michele Caini
781f283c89 utility: as_view (resolve #157) 2018-11-29 11:49:15 +01:00
Michele Caini
f1cd085727 updated AUTHORS 2018-11-28 16:50:25 +01:00
Michael "Croydon" Keck
3aba855bd6 Add basic Conan support (#162)
* Add Conan support
* Conan: Add test_package
2018-11-28 11:25:49 +01:00
Michele Caini
7e3abb27a4 added find functionality to sparse sets/views 2018-11-28 10:47:09 +01:00
Michele Caini
f0c11daa37 cleanup 2018-11-27 23:44:40 +01:00
Michele Caini
167c174f4d added support for const instances * to the delegate 2018-11-27 23:16:55 +01:00
Michele Caini
0173060eaf typo 2018-11-27 22:20:52 +01:00
Michele Caini
4edd25ff5d review: registry 2018-11-27 22:13:19 +01:00
Michele Caini
2a4e098645 persistent views are now more reliable (fix #160) 2018-11-23 09:40:42 +01:00
willtunnels
7c5019c30f Fix find_package(EnTT) when EnTT is installed using vcpkg (#159)
This commit fixes a bug where find_package(EnTT) fails when EnTT is
installed using vcpkg.

The bug occurs because EnTTConfig.cmake.in calls
check_required_components(), yet the call to
configure_package_config_file() in CMakeLists.txt that generates
EnTTConfig.cmake is passed NO_CHECK_REQUIRED_COMPONENTS_MACRO. This
causes CMake to error when it tries to parse the output EnTTConfig.cmake
file if that file is generated from EnTTConfig.cmake.in e.g. when
installing EnTT using vcpkg.
2018-11-21 17:44:06 +01:00
Michele Caini
c13fe3feb6 views: const, non-const and all in between (fix #152) 2018-11-20 14:45:28 +01:00
Michele Caini
0ced60e712 added get overload with default value to registry (#152) 2018-11-19 16:08:26 +01:00
Michele Caini
d0764d5854 accommodate => assign_or_replace (#153) 2018-11-19 14:14:01 +01:00
Michele Caini
f7905e3bc2 get_if => try_get (#153) 2018-11-19 14:00:22 +01:00
Michele Caini
3e770792a4 meta: minor changes 2018-11-15 23:26:31 +01:00
Michele Caini
8f9934a7f4 boost persistent views (around 100x) 2018-11-12 23:13:23 +01:00
Michele Caini
b921e7d595 review: return values 2018-11-07 17:23:08 +01:00
Michele Caini
a9cb2d2efd updated TODO 2018-11-01 17:07:21 +01:00
Michele Caini
fe519b3acf (maybe) suppress error on vs2017 2018-10-31 22:34:27 +01:00
Michele Caini
30cc4a1668 re-enabled appveyor 2018-10-31 22:27:23 +01:00
Michele Caini
a71e4d8970 note/request 2018-10-31 17:42:24 +01:00
Michele Caini
66e7141415 meta: support for setters/getters 2018-10-31 17:39:40 +01:00
Michele Caini
6f0cdc864f added registry::create overload to create multiple entities at once 2018-10-30 15:14:57 +01:00
Michele Caini
4f9deaaf09 minor changes 2018-10-30 15:10:17 +01:00
Michele Caini
380745a814 fixed constness 2018-10-30 14:25:03 +01:00
Michele Caini
ce4e335412 doc 2018-10-29 22:57:07 +01:00
Michele Caini
274b8181d1 doc 2018-10-29 22:10:51 +01:00
Michele Caini
767bdcc2ce minor changes 2018-10-28 14:24:56 +01:00
Paul Gruenbacher
c00cde8bcc workaround for an issue with gcc 8.1.0 (#146 - fix #145) 2018-10-27 23:43:24 +02:00
Michele Caini
5859a18b3a added get_if where possible 2018-10-27 13:45:51 +02:00
Michele Caini
375124642d fixed warning 2018-10-25 23:02:31 +02:00
Michele Caini
af9e6dea68 review: doc 2018-10-25 22:28:56 +02:00
Michele Caini
78f2267b06 delegate: deduction guidelines 2018-10-22 15:32:18 +02:00
Michele Caini
0a34f22240 dispatcher: allow for deduced types for events 2018-10-21 22:48:06 +02:00
Michele Caini
b0651fcaed delegate: empty -> operator bool 2018-10-21 14:59:24 +02:00
Michele Caini
d59faa2a08 BUILD_TESTING set to OFF by default 2018-10-20 15:45:04 +02:00
Michele Caini
6a3bc37dc1 minor changes 2018-10-19 22:54:11 +02:00
Michele Caini
2ea7796b26 coverage purposes 2018-10-17 09:58:40 +02:00
Michele Caini
cf0057bac4 coverage purposes 2018-10-17 09:25:23 +02:00
Michele Caini
fafb325a88 fixed typo 2018-10-16 23:08:42 +02:00
Michele Caini
6b69b4671f fixed typo 2018-10-16 22:49:13 +02:00
Michele Caini
eaa95ee840 fixed typo 2018-10-16 22:21:36 +02:00
Michele Caini
a86ba1fdf6 a few changes to the meta system 2018-10-15 15:29:40 +02:00
Michele Caini
09e0d2d15b meta: inline variables 2018-10-13 13:19:34 +02:00
Michele Caini
6d9fa6418d fixed typo 2018-10-12 16:30:45 +02:00
Michele Caini
6010f5b7a6 meta: final review 2018-10-12 16:18:51 +02:00
Michele Caini
9c164e1cea meta: full support for constant values and enums 2018-10-12 00:09:58 +02:00
Michele Caini
880fba8dce fixed clang error with meta 2018-10-10 09:12:31 +02:00
Michele Caini
17d57ee49a value types and meta objects 2018-10-10 08:42:41 +02:00
Michele Caini
234949a627 updated contributors list 2018-10-08 22:52:17 +02:00
Michele Caini
ff34671526 typos 2018-10-07 22:33:39 +02:00
Michele Caini
6e22983d98 updated TODO list 2018-10-07 22:13:05 +02:00
Michele Caini
18254c30ec Update README.md 2018-10-07 22:07:27 +02:00
Michele Caini
922c955239 fixed doc + updated TODO list 2018-10-07 21:53:07 +02:00
Michele Caini
618a325057 reflection system 2018-10-05 13:38:22 +02:00
Michele Caini
d81cb7f866 EnTT v3 (draft) 2018-09-30 14:04:39 +02:00
Michele Caini
f71a4d5381 updated build system 2018-09-30 13:55:31 +02:00
Michele Caini
282cb60a66 added link to patreon 2018-09-29 23:02:10 +02:00
Michele Caini
58dbac4422 fixed typo 2018-09-03 10:24:05 +02:00
Michele Caini
53e228465b Update README.md 2018-09-02 23:28:48 +02:00
Michele Caini
06426e4fd7 updated version 2018-09-02 22:48:24 +02:00
Michele Caini
c55a97c24d updated TODO 2018-09-01 16:26:58 +02:00
Michele Caini
0d61289bf3 fixed #135 2018-09-01 16:21:59 +02:00
Michele Caini
bf10cbc70b review: documentation 2018-09-01 14:57:06 +02:00
Michele Caini
2d945e426b fixed #133 2018-08-29 23:10:03 +02:00
Michele Caini
13250887fa review: dependency 2018-08-22 15:51:13 +02:00
Michele Caini
3507c22968 bug fixing (Snapshot::destroyed - #128) 2018-08-22 14:22:54 +02:00
Michele Caini
cc3f98ebcd fixed tests (#129) 2018-08-22 14:00:11 +02:00
Michele Caini
4116e2d6ac added some projects to the entt-in-action list 2018-08-19 14:23:31 +02:00
Michele Caini
48eab6b4a7 minor changes 2018-08-11 14:44:41 +02:00
Michele Caini
25866b5369 fixed typo 2018-08-11 01:09:44 +02:00
Michele Caini
c4dd06fa45 delegate/signal: support for const member functions 2018-08-11 00:54:43 +02:00
Michele Caini
4846d211e0 updated TODO list 2018-08-06 14:07:34 +02:00
Michele Caini
a586ad1237 updated build system 2018-08-05 15:17:48 +02:00
Michele Caini
b701c9c464 review 2018-08-05 15:10:56 +02:00
Michele Caini
d0f20ed2bf updated tests 2018-08-05 13:54:21 +02:00
Michele Caini
0f64a2f3b0 updated .travis.yml (see #110) 2018-08-03 08:17:03 +02:00
Michele Caini
bd83fba6cd updated version 2018-08-02 17:17:09 +02:00
Michele Caini
15b9255a25 fix #120 2018-08-02 16:55:03 +02:00
Michele Caini
6794d21487 one shot bubble sort alg 2018-07-31 09:11:18 +02:00
Michele Caini
118c4432ec typo 2018-07-24 09:25:01 +02:00
Michele Caini
ccda429bf1 Update README.md 2018-07-23 12:10:48 +02:00
Michele Caini
590937d2a0 added Registry::entity 2018-07-20 22:17:18 +02:00
Michele Caini
241827dd80 added gitter badge 2018-07-20 22:03:24 +02:00
Michele Caini
77b4e0b4bd EnTT in action: Face Smash 2018-07-18 15:01:36 +02:00
Michele Caini
6a53cb32d8 updated TODO 2018-07-18 14:08:30 +02:00
Michele Caini
f12ff3b15e fix #116 2018-07-18 14:07:48 +02:00
Michele Caini
69ad8676b7 added USE_ASAN option 2018-07-18 14:07:43 +02:00
Michele Caini
d0fd756f6b logo + review doc 2018-07-12 15:30:52 +02:00
Michele Caini
7f8ab67e9e performance improvements 2018-07-09 16:48:35 +02:00
Michele Caini
0e68bb3d2c added entt::label 2018-07-07 13:40:20 +02:00
Michele Caini
d5b3933752 updated README (added a note on EnTT and shared libraries - see #111) 2018-07-05 16:14:42 +02:00
Michele Caini
0b7206a92d review: README 2018-07-04 13:42:37 +02:00
Michele Caini
e875f306fd added FIND_GTEST_PACKAGE to force using system-wide gtest version (#106) 2018-07-03 22:09:28 +02:00
Michele Caini
8ed5e5ee28 updated cmake min version 2018-07-03 16:57:46 +02:00
Indi Kernick
948b0d40f6 Update packages with Travis-CI (#110) 2018-07-03 12:57:36 +02:00
David Kalnischkies
48f4feb7a7 Fix README: identifiers are accessed via type<>() 2018-07-02 13:29:03 +02:00
Michele Caini
ddc0a32bbc fix: runtime view (#108) 2018-07-02 08:40:30 +02:00
Michele Caini
10a7c54364 runtime view 2018-06-28 14:31:04 +02:00
Michele Caini
a66fa9d844 review: capacity 2018-06-28 08:36:24 +02:00
Michele Caini
a16f2ac15c fixed build system 2018-06-27 22:53:21 +02:00
Michele Caini
b699797a40 naming convention 2018-06-25 14:02:31 +02:00
Michele Caini
a6e9520d06 updated TODO 2018-06-25 13:16:11 +02:00
Michele Caini
f8310b1296 added monostate (built-in config system) 2018-06-23 19:43:22 +02:00
Michele Caini
353bf99cd5 now working with vs2017 (again) 2018-06-22 14:46:43 +02:00
Michele Caini
a478e4acc9 fixed compilation errors and suppressed warnings with clang 2018-06-22 14:16:02 +02:00
Michele Caini
d810e0ba7d review ident 2018-06-22 09:59:03 +02:00
Michele Caini
1e51ffdb72 user defined string literal for hashed strings 2018-06-20 17:08:14 +02:00
Michele Caini
7da1d1fc64 minor changes 2018-06-19 15:36:21 +02:00
Michele Caini
957697c383 review: delegate (see #101 and #102) 2018-06-18 17:10:31 +02:00
Michele Caini
107eb72225 updated TODO 2018-06-18 13:25:34 +02:00
Michele Caini
810b77f9da use null entity everywhere 2018-06-18 08:34:27 +02:00
Michele Caini
5e3bc2049b clean up 2018-06-14 14:48:19 +02:00
Michele Caini
229500347d review: process scheduler 2018-06-14 14:46:39 +02:00
Michele Caini
1f461db0a1 minor changes 2018-06-13 23:33:47 +02:00
Malte
6c55aafee3 Export CMake Config file and "modernize" CMake scripts (#87)
* CMake modifications and Config file generation

* CMake minor style changes

* Get rid of commented lines, fix indentation

* Respect Windows conventions for CMake config directory, Fix error with Config Version file, Export CMake package

* Add CMake option for use of ENTT_COMPILE_OPTIONS, Add CMake option for using libc++, Remove  from EnTT target and move it to test targets

* Fix indentation

* Fix indentation (again)

* Fix Windows problems with compile option -Wall in not using it on Windows

* Improved generator expression
The problem with -Wall is not due to the platform but due to the compiler MSVC

* Set compatibility for ConfigVersion file to AnyNewerVersion, Add PATH_VARS CMAKE_INSTALL_INCLUDE_DIR of configure_package_config_file, Remove redundant options, correct target_include_directory for INSTALL_INTERFACE, set the Version in EnTTConfig file and check CMake version

* Add missing closing brace, Add a special config file for the build tree
2018-06-13 14:04:05 +02:00
Michele Caini
d57e55b719 improved sort 2018-06-12 22:32:11 +02:00
Michele Caini
bf772e5fe5 docs 2018-06-12 13:00:33 +02:00
Michele Caini
413f3356ce updated README 2018-06-12 08:46:05 +02:00
Michele Caini
f147326fe0 typo 2018-06-11 14:50:43 +02:00
Michele Caini
30c59644b6 minor changes 2018-06-11 13:57:37 +02:00
Michele Caini
3fa5acf2e6 updated TOC 2018-06-11 08:26:53 +02:00
Michele Caini
3e6ded8823 more on destroy 2018-06-11 08:24:48 +02:00
Michele Caini
612017aaa2 null entity 2018-06-11 08:23:07 +02:00
Michele Caini
ef57d7e7b6 removed wrong noexcept 2018-06-10 00:10:12 +02:00
Michele Caini
a8d0db5036 updated README 2018-06-09 23:51:57 +02:00
Michele Caini
ad6b5f8fc1 fixed tests 2018-06-09 23:51:48 +02:00
Michele Caini
e3cb6a0aec minor changes 2018-06-09 21:58:49 +02:00
Sztergbaum Roman
cbf18a7dc4 fix warning on windows C4267 (#97) 2018-06-09 21:49:33 +02:00
Michele Caini
01559410a9 updated TODO 2018-06-09 12:26:59 +02:00
Michele Caini
f2ab94fa7f added operator[] to sparse set and views 2018-06-09 00:36:22 +02:00
Michele Caini
d7394a8369 more on prototype 2018-06-08 23:03:05 +02:00
Michele Caini
9feef11d6f cleanup + tests 2018-06-08 22:58:12 +02:00
Michele Caini
dc4e5ddc3c review: iterators (sparse set/view) 2018-06-08 22:30:50 +02:00
Michele Caini
8600781bb6 fixed doc + minor changes 2018-06-08 19:04:19 +02:00
Michele Caini
bdc7bbdc9d DefaultPrototype depends on DefaultRegistry now 2018-06-06 14:39:56 +02:00
Michele Caini
73badef594 updated gtest (#92) 2018-06-06 13:42:09 +02:00
Michele Caini
9474e6c08c updated prototype (#91) 2018-06-05 08:38:27 +02:00
Michele Caini
653ded0e6f updated version 2018-06-04 08:52:10 +02:00
Michele Caini
e34bec7dee cloning an entity is no longer allowed 2018-06-04 08:49:13 +02:00
Michele Caini
610b560fb5 typo 2018-06-03 22:24:45 +02:00
Michele Caini
0a03ddb8a7 typo 2018-06-03 22:17:06 +02:00
Michele Caini
f31790631a fixed include 2018-06-03 22:10:33 +02:00
Michele Caini
e07128760e review: prototype (#89) 2018-06-03 19:06:12 +02:00
Michele Caini
dd02ae313d minor changes 2018-06-02 17:06:44 +02:00
Michele Caini
d14b0b6843 updated version 2018-05-30 22:46:47 +02:00
Michele Caini
f763c8a777 docs 2018-05-30 22:40:06 +02:00
Michele Caini
4231b040d8 documented an already existent feature (#86) 2018-05-30 22:19:04 +02:00
Michele Caini
67c587e673 updated README.md 2018-05-23 23:35:39 +02:00
Michele Caini
3978e24fd8 added CONTRIBUTING.md 2018-05-23 23:12:23 +02:00
Michele Caini
c45de0c032 tests + bug fixing 2018-05-23 12:28:10 +02:00
Michele Caini
ff935efea1 updated README 2018-05-23 12:27:53 +02:00
Michele Caini
808464f47d more on save/restore (#67) 2018-05-23 12:26:53 +02:00
Michele Caini
c986a6c4dd added clone functionality 2018-05-22 22:49:24 +02:00
Michele Caini
17c0479343 minor changes 2018-05-22 22:45:09 +02:00
Michele Caini
d93238912a workaround for an ICE of g++-5 (see #79) 2018-05-18 09:27:57 +02:00
Michele Caini
d6ef0956e6 performance improvements 2018-05-17 17:47:02 +02:00
Michele Caini
05db0aad29 fixed constness of iterators' member functions 2018-05-16 17:04:44 +02:00
Michele Caini
89bbdfa1fe benchmark: clean up 2018-05-16 16:59:45 +02:00
Michele Caini
871bda6198 fixed toc 2018-05-15 23:30:45 +02:00
Michele Caini
20732c9206 added const begin/end on views + tests 2018-05-15 22:51:47 +02:00
Michele Caini
2a34a3ebb6 fixed and added more tests for prototype 2018-05-15 22:31:27 +02:00
Michele Caini
3f04247a53 fixed errors with vs2017 + const begin/end on iterators for sparse set 2018-05-15 22:21:51 +02:00
Michele Caini
ca0a1f8f8b review: multi component standard view 2018-05-15 17:30:22 +02:00
Michele Caini
d8a9f0ca12 minor changes 2018-05-15 17:29:05 +02:00
Michele Caini
c2116b841e fixed typo 2018-05-15 14:16:24 +02:00
Michele Caini
700cf69f18 prefer a clean api instead of cumbersome ones 2018-05-11 23:19:11 +02:00
Michele Caini
ed5dee5218 fixed error with msvc - logic flipped upside down :-( 2018-05-11 22:51:50 +02:00
Michele Caini
b224dfdfac trying to work around msvc limitations 2018-05-11 22:25:03 +02:00
Michele Caini
e7da68547f fixed errors with clang 2018-05-11 22:24:10 +02:00
Michele Caini
9a785ceb2e added prototype class 2018-05-11 22:12:43 +02:00
Indi Kernick
fca1cd5a1c Implement entt::Prototype (#74)
first draft - to be reviewed
2018-05-08 13:37:21 +02:00
Michele Caini
24b862e32e more flexible 2018-05-06 13:58:50 +02:00
Michele Caini
2ec9043cf2 fixed comments 2018-05-04 17:18:53 +02:00
Michele Caini
1102d63469 fix #73 2018-05-02 22:50:55 +02:00
Michele Caini
b89f39d78c added has for tag + entity to test ownership 2018-05-02 17:07:20 +02:00
Michele Caini
7ba479c9c9 minor changes 2018-05-02 13:17:50 +02:00
Michele Caini
8ad6a2980c updated doc 2018-05-01 17:32:11 +02:00
Michele Caini
d3b6ed78d9 custom sort function (#72) 2018-04-30 17:14:36 +02:00
Michele Caini
8bd5605c2a now works also with VS2017 and GCC6 2018-04-27 23:19:08 +02:00
Michele Caini
08dc2fcf33 performance improvement 2018-04-27 15:27:16 +02:00
Michele Caini
caa8d16371 updated README 2018-04-24 14:01:18 +02:00
Michele Caini
bce92b3d85 updated README + AUTHORS 2018-04-22 23:23:15 +02:00
Michele Caini
67858bf300 faster accommodate 2018-04-22 13:32:38 +02:00
Michele Caini
7157e7e77d minor changes 2018-04-20 17:11:57 +02:00
Michele Caini
cf5074bdc5 added Registry::data/::raw 2018-04-20 16:23:39 +02:00
Michele Caini
fda44063ce removed Registry::persistent/Registry::raw 2018-04-20 15:32:30 +02:00
Michele Caini
f9becda02c more tests 2018-04-20 14:57:21 +02:00
Michele Caini
fb9fc952c6 more tests 2018-04-20 14:28:40 +02:00
Michele Caini
18451edfe9 review: views and sparse sets 2018-04-20 13:17:50 +02:00
Michele Caini
8c73cac72f added ENTT_NOEXCEPT macro to fully disable exception handling (ie required with UE4) 2018-04-18 16:36:14 +02:00
Michele Caini
c54cedf14b readme: updated benchmarks 2018-04-18 16:16:15 +02:00
Michele Caini
8ef4cdc9c3 improvements 2018-04-18 13:48:50 +02:00
Michele Caini
c0213e84f6 signals on tags 2018-04-16 17:14:22 +02:00
Michele Caini
29de6d89d4 improvements 2018-04-13 22:54:20 +02:00
Michele Caini
c9bf38ce36 fixed dispatcher + minor changes 2018-04-13 14:01:34 +02:00
Michele Caini
338eb75bab review: actor + tests 2018-04-12 08:18:10 +02:00
Michele Caini
31b1b453b0 updated doc and tests 2018-04-10 15:12:42 +02:00
Michele Caini
aaf0e145eb fixed - no more anonymous namespaces 2018-04-10 11:25:17 +02:00
Michele Caini
089b3e13fd no more anonymous namespaces 2018-04-10 09:00:15 +02:00
Michele Caini
e9da2ce12a (maybe) fixed 2018-04-10 08:45:54 +02:00
Michele Caini
92048ac17b removed signal + added dependency function(s) 2018-04-09 23:26:39 +02:00
Michele Caini
5e8561a578 more on tags and components 2018-04-09 08:14:35 +02:00
Michele Caini
d2f5e13074 typo 2018-04-08 23:25:32 +02:00
Michele Caini
820178f006 tag dispatching to disambiguate tags and components (#65) 2018-04-07 00:29:09 +02:00
Michele Caini
0a36a91e6d review + docs 2018-04-06 23:30:13 +02:00
Michele Caini
5013a92795 review snapshot/loaders 2018-04-06 22:08:23 +02:00
Michele Caini
d81ecfec32 removed extra create functions from the registry (#52) 2018-04-06 22:06:44 +02:00
Michele Caini
e99d7e2c3c signals on component creation/destruction (#62) 2018-04-06 22:04:58 +02:00
Michele Caini
d417984ff3 review: iterators + fixed bug on raw views 2018-03-30 14:54:14 +02:00
Michele Caini
d38b3e641b added a note about thread safety (see #64) 2018-03-29 23:38:50 +02:00
Michele Caini
28ce491dd5 review sigh + added set/move for tags 2018-03-29 22:30:23 +02:00
Michele Caini
c260d72125 fixed typo 2018-03-28 22:35:30 +02:00
Michele Caini
d1d1b3156d fixed doc for #27 2018-03-28 22:29:01 +02:00
Michele Caini
472064b751 ensure -> assure (#63) 2018-03-28 22:04:16 +02:00
Michele Caini
95ab9a0b70 updated TODO 2018-03-28 17:20:31 +02:00
Michele Caini
4b03f6a039 minor changes 2018-03-28 15:49:02 +02:00
Michele Caini
c3460727fa updated TODO 2018-03-28 10:24:01 +02:00
Michele Caini
2cc1850212 save/restore - see #27 2018-03-28 10:23:47 +02:00
Matteo Galeotti
2d7443acaf Add Dispatcher header to EnTT global include (#59)
Add dispatcher.hpp to entt.hpp
2018-03-22 14:05:55 +01:00
Michele Caini
13d0b0940c raw views 2018-03-15 22:09:43 +01:00
Michele Caini
c101797924 minor changes 2018-03-15 22:03:18 +01:00
Michele Caini
83b55f8e3f fixed benchmark 2018-03-14 14:11:29 +01:00
Michele Caini
b3b6362cd9 minor changes 2018-03-14 14:00:48 +01:00
Nicholas Farshidmehr
fc9af32d5f Fix spelling mistakes. (#57)
Fix spelling mistakes.
2018-03-14 13:47:23 +01:00
Michele Caini
4cd1025011 cleanup 2018-03-14 08:41:31 +01:00
Michele Caini
5233fe8abc updated TODO 2018-03-14 08:39:33 +01:00
Michele Caini
041e31ea78 removed spaces: not satisfied with the current implementation 2018-03-14 08:38:20 +01:00
Michele Caini
7a3e881099 review: benchmark 2018-03-14 08:28:56 +01:00
Michele Caini
631bf42f84 cleanup 2018-03-13 13:55:37 +01:00
Michele Caini
1f704a7019 updated TODO list 2018-03-12 11:28:00 +01:00
Michele Caini
d295c88474 spaces 2018-03-11 23:11:45 +01:00
Michele Caini
1dd9da4dff improved views (extended API + better performance) 2018-03-11 23:07:10 +01:00
Michele Caini
f2eb0c8427 added Registry::fast 2018-03-11 23:05:56 +01:00
Michele Caini
c8ba11faf8 more tests 2018-03-11 23:04:32 +01:00
Michele Caini
a2e243d992 improved multi component standard view 2018-03-07 22:38:21 +01:00
Michele Caini
c588fff5ca minor changes 2018-03-07 17:24:44 +01:00
Michele Caini
87f9599fea minor changes 2018-03-07 08:52:13 +01:00
Michele Caini
0459599b1d added estimated number of entities for multi component standard view 2018-03-07 08:31:03 +01:00
Michele Caini
9447b1a696 fixed 2018-03-06 22:34:46 +01:00
Michele Caini
0ccb7443c2 only the registry should create views 2018-03-06 22:34:38 +01:00
Michele Caini
02cf27091f coding style 2018-03-06 22:22:52 +01:00
Michele Caini
fdfbd04503 review 2018-03-06 13:27:29 +01:00
Michele Caini
866c18200a iterators from sparse sets and views have now operator+/operator+= 2018-03-04 16:45:24 +01:00
Michele Caini
c1cada49d4 review 2018-03-04 16:03:59 +01:00
Michele Caini
7bf550a75f id/version review 2018-03-04 15:32:08 +01:00
Michele Caini
9c540c03aa thread safe family class + minor changes 2018-03-04 15:21:10 +01:00
Michele Caini
b3df46db19 better test 2018-03-02 08:58:09 +01:00
Michele Caini
7ca615a1c1 sfinaed construct in sparse set to favor emplace_back with arguments when possible (#48) 2018-03-02 08:45:59 +01:00
Michele Caini
c83db557a6 more tests + minor changes 2018-03-01 19:24:12 +01:00
Michele Caini
d54594f11d orphans/orphan + minor changes 2018-03-01 19:23:42 +01:00
Michele Caini
434e38608f fixed typo 2018-03-01 19:22:26 +01:00
Michele Caini
871f090ca0 bug fixing 2018-03-01 16:18:23 +01:00
Michele Caini
d1d235e025 Fixed #46 (#47)
Use libc++ if possible
2018-02-28 19:02:12 +01:00
Michele Caini
e822a5fd53 more tests 2018-02-27 23:42:19 +01:00
Michele Caini
7b82a4ae50 updated TODO 2018-02-27 23:42:05 +01:00
Michele Caini
c532e9f2eb updated TODO list 2018-02-25 22:53:57 +01:00
Michele Caini
3fd034816e more tests on sparse set 2018-02-22 23:23:46 +01:00
Michele Caini
bb4b868c79 improvement: standard multi component view 2018-02-22 22:46:42 +01:00
Michele Caini
3b3da11a36 cleanup 2018-02-22 13:13:02 +01:00
Nick Lange
f2cbb5306b Fix MSVC 2017 warning C4458 (#43)
Fix MSVC 2017 warning C4458.
2018-02-22 13:08:36 +01:00
Michele Caini
94ede1b324 updated TODO 2018-02-21 22:24:23 +01:00
Michele Caini
0367248338 Documentation (#42)
Everything is finally documented.
2018-02-21 22:16:02 +01:00
Michele Caini
936db30e58 Fewer allocations, faster destroy (#41)
Overall improvement of the registry.
2018-02-20 10:10:13 +01:00
Michele Caini
4822f0dd11 fixed 2018-02-18 23:19:26 +01:00
Michele Caini
456d220829 updated TODO 2018-02-15 09:27:39 +01:00
Michele Caini
b459ba6ea7 TODO list 2018-02-15 08:46:15 +01:00
Michele Caini
a19ef9bd16 slightly improved 2018-02-12 19:24:07 +01:00
Paolo Monteverde
59cec88a28 fixing clang build (#38) 2018-02-08 14:56:07 +01:00
Michele Caini
3ebc75af80 updated version 2018-02-08 12:42:38 +01:00
Michele Caini
4dce474e03 revert: too risky a function 2018-02-08 12:27:48 +01:00
Michele Caini
31a18da578 fix #37 2018-02-08 12:23:48 +01:00
Michele Caini
8c499850fc fixed doc 2018-02-04 12:36:50 +01:00
Michele Caini
6b6998a247 duktape is now an external 2018-02-04 12:31:24 +01:00
Michele Caini
a6cb0fc856 added Registry::alive and Registry::orphans 2018-02-02 17:35:15 +01:00
Michele Caini
e36b93e87b fixed 2018-02-02 12:58:10 +01:00
Michele Caini
1e3723b8bb minor changes 2018-02-02 12:38:17 +01:00
Michele Caini
412372289e updated copyright 2018-01-26 17:28:40 +01:00
Michele Caini
96f7e66073 fixed 2018-01-14 00:53:55 +01:00
Michele Caini
6040f8f263 issue #31: multi component get 2018-01-14 00:32:23 +01:00
Michele Caini
9761b6e14a updated version 2017-12-29 18:29:38 +01:00
Michele Caini
cb49910ed2 allow attaching listeners at any time, allow removing current listener 2017-12-29 18:25:49 +01:00
Michele Caini
62bd742673 fixed doc 2017-12-27 17:59:57 +01:00
Michele Caini
42d0a3d734 v2.4.0 2017-12-27 17:57:04 +01:00
Michele Caini
f0f8681455 bug fixing 2017-12-27 17:55:26 +01:00
Michele Caini
c801afddcb added optional data to process::init 2017-12-23 00:30:00 +01:00
Michele Caini
20e0e1333e minor changes 2017-12-23 00:21:05 +01:00
Michele Caini
a6b373fec4 minor changes 2017-12-23 00:18:23 +01:00
Michele Caini
41c77720bb added optional data to scheduler/process 2017-12-22 23:59:07 +01:00
Michele Caini
92e6340120 cleanup 2017-12-22 23:58:49 +01:00
Michele Caini
1221f63cbd updated doc 2017-12-22 09:24:56 +01:00
Michele Caini
0f24418891 added ResourceCache::temp 2017-12-20 13:39:23 +01:00
Michele Caini
f477c0ab87 fixed reserve 2017-12-18 14:57:23 +01:00
Michele Caini
9358691901 added reserve 2017-12-18 14:08:38 +01:00
Michele Caini
cd343ba598 updated appveyor.yml (waiting for a new stable release of googletest) 2017-12-15 23:06:43 +01:00
Michele Caini
50069d3743 fixed docs 2017-12-14 23:15:47 +01:00
Michele Caini
1e03f27f23 v2.3.0 2017-12-14 22:56:40 +01:00
Michele Caini
36bb55a9ce doc: fixed 2017-12-13 16:20:36 +01:00
Michele Caini
451e4050db cleanup 2017-12-11 22:35:48 +01:00
Michele Caini
367fd3e87f minor changes 2017-12-11 16:04:25 +01:00
Michele Caini
a67a2e12fd minor changes 2017-12-11 15:03:43 +01:00
Michele Caini
292978daf0 #23: runtime components (doc) 2017-12-11 15:03:35 +01:00
Michele Caini
85a4a76a14 mod example with duktape 2017-12-10 17:43:48 +01:00
Michele Caini
9d0ab7ed70 added target entt_aob 2017-12-04 15:10:52 +01:00
Michele Caini
3d5b6a5e0b exposed family types 2017-12-04 14:59:08 +01:00
Michele Caini
ab20372093 minor changes 2017-12-04 14:06:10 +01:00
Michele Caini
ab887f30e4 typo 2017-11-21 08:33:48 +01:00
Michele Caini
6cb6a8c25f minor changes 2017-11-20 15:45:08 +01:00
Michele Caini
9d1d2aca0a updated build system 2017-11-18 17:31:11 +01:00
Michele Caini
75cb2cd1f7 improved sort functionalities 2017-11-18 15:54:04 +01:00
142 changed files with 45776 additions and 6049 deletions

12
.github/FUNDING.yml vendored Normal file
View File

@@ -0,0 +1,12 @@
# These are supported funding model platforms
github: skypjack
patreon: skypjack
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: https://www.paypal.me/skypjack

74
.github/workflows/build.yml vendored Normal file
View File

@@ -0,0 +1,74 @@
name: build
on: [push, pull_request]
jobs:
linux:
timeout-minutes: 5
strategy:
matrix:
os: [ubuntu-18.04]
compiler: [g++, clang++]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- name: Compile tests
working-directory: build
env:
CXX: ${{ matrix.compiler }}
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ..
make -j4
- name: Run tests
working-directory: build
env:
CTEST_OUTPUT_ON_FAILURE: 1
run: ctest --timeout 5 -C Debug -j4
windows:
timeout-minutes: 5
strategy:
matrix:
os: [windows-2019, windows-2016]
include:
- os: windows-2019
generator: Visual Studio 16 2019
- os: windows-2016
generator: Visual Studio 15 2017
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v1
- name: Compile tests
working-directory: build
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON -DCMAKE_CXX_FLAGS=/W1 -G"${{ matrix.generator }}" ..
cmake --build . -j 4
- name: Run tests
working-directory: build
env:
CTEST_OUTPUT_ON_FAILURE: 1
run: ctest --timeout 5 -C Debug -j4
macos:
timeout-minutes: 5
runs-on: macOS-10.14
steps:
- uses: actions/checkout@v1
- name: Compile tests
working-directory: build
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ..
make -j4
- name: Run tests
working-directory: build
env:
CTEST_OUTPUT_ON_FAILURE: 1
run: ctest --timeout 5 -C Debug -j4

33
.github/workflows/coverage.yml vendored Normal file
View File

@@ -0,0 +1,33 @@
name: coverage
on: [push, pull_request]
jobs:
codecov:
timeout-minutes: 30
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- name: Compile tests
working-directory: build
env:
CXXFLAGS: "-O0 --coverage -fno-inline -fno-inline-small-functions -fno-default-inline"
CXX: g++
run: |
cmake -DBUILD_TESTING=ON -DBUILD_LIB=ON ..
make -j4
- name: Run tests
working-directory: build
env:
CTEST_OUTPUT_ON_FAILURE: 1
run: ctest --timeout 5 -C Debug -j4
- name: Upload coverage to Codecov
working-directory: build
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
run: |
wget https://codecov.io/bash -O codecov
chmod +x codecov
./codecov -t $CODECOV_TOKEN -B $GITHUB_REF -s test/

33
.github/workflows/deploy.yml vendored Normal file
View File

@@ -0,0 +1,33 @@
name: deploy
on:
push:
tags:
- v*
jobs:
conan:
timeout-minutes: 5
runs-on: ubuntu-latest
steps:
- uses: docker://conanio/gcc8
- uses: actions/checkout@v1
- name: Setup Python
uses: actions/setup-python@master
with:
version: 3.6
- name: Install
run: |
chmod +x conan/ci/install.sh
./conan/ci/install.sh
- name: Deploy
env:
CONAN_LOGIN_USERNAME: ${{ secrets.CONAN_LOGIN_USERNAME }}
CONAN_PASSWORD: ${{ secrets.CONAN_PASSWORD }}
CONAN_UPLOAD: ${{ secrets.CONAN_UPLOAD }}
CONAN_GCC_VERSIONS: 8
run: |
chmod +x conan/ci/build.sh
./conan/ci/build.sh

10
.gitignore vendored
View File

@@ -1,2 +1,10 @@
# QtCreator
# Conan
conan/test_package/build
# IDEs
*.user
.idea
.vscode
# Bazel
/bazel-*

View File

@@ -1,73 +0,0 @@
language: cpp
dist: trusty
sudo: false
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:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-7']
env: COMPILER=g++-7
- os: linux
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
- os: osx
osx_image: xcode8.3
compiler: clang
env: COMPILER=clang++
- os: osx
osx_image: xcode9.1
compiler: clang
env: COMPILER=clang++
- os: linux
compiler: gcc
addons:
apt:
sources: ['ubuntu-toolchain-r-test']
packages: ['g++-7']
env:
- COMPILER=g++-7
- CXXFLAGS="-O0 --coverage -fno-inline -fno-inline-small-functions -fno-default-inline"
before_script:
- 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
notifications:
email:
on_success: never
on_failure: always
install:
- echo ${PATH}
- cmake --version
- export CXX=${COMPILER}
- echo ${CXX}
- ${CXX} --version
- ${CXX} -v
script:
- mkdir -p build && cd build
- cmake .. && make -j4
- CTEST_OUTPUT_ON_FAILURE=1 make test

40
AUTHORS
View File

@@ -1,7 +1,43 @@
# Author
Michele Caini aka skypjack
skypjack
# Contributors
Paolo Monteverde aka morbo84
BenediktConze
bjadamson
ceeac
ColinH
corystegel
Croydon
dbacchet
dBagrat
djarek
DonKult
drglove
eugeneko
gale83
ghost
grdowns
Green-Sky
Innokentiy-Alaytsev
Kerndog73
Lawrencemm
markand
mhammerc
Milerius
morbo84
m-waka
NixAJ
Paolo-Oliverio
pgruenbacher
prowolf
suVrik
szunhammer
The5-1
vblanco20-1
willtunnels
WizardIke
w1th0utnam3
xissburg
zaucy

9
BUILD.bazel Normal file
View File

@@ -0,0 +1,9 @@
load("//bazel:copts.bzl", "entt_copts")
cc_library(
name = "entt",
visibility = ["//visibility:public"],
strip_include_prefix = "src",
hdrs = glob(["src/**/*.h", "src/**/*.hpp"]),
copts = entt_copts,
)

View File

@@ -2,7 +2,7 @@
# EnTT
#
cmake_minimum_required(VERSION 3.2)
cmake_minimum_required(VERSION 3.7.2)
#
# Building in-tree is not allowed (we take care of your craziness).
@@ -16,7 +16,9 @@ endif()
# Project configuration
#
project(entt VERSION 2.2.0)
project(EnTT VERSION 3.2.1 LANGUAGES CXX)
include(GNUInstallDirs)
if(NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Debug)
@@ -29,58 +31,171 @@ set(PROJECT_AUTHOR_EMAIL "michele.caini@gmail.com")
message("*")
message("* ${PROJECT_NAME} v${PROJECT_VERSION} (${CMAKE_BUILD_TYPE})")
message("* Copyright (c) 2017 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
message("* Copyright (c) 2017-2019 ${PROJECT_AUTHOR} <${PROJECT_AUTHOR_EMAIL}>")
message("*")
option(USE_LIBCPP "Use libc++ by adding -stdlib=libc++ flag if availbale." ON)
option(USE_ASAN "Use address sanitizer by adding -fsanitize=address -fno-omit-frame-pointer flags" OFF)
option(USE_COMPILE_OPTIONS "Use compile options from EnTT." ON)
#
# Compiler stuff
#
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
if(NOT WIN32 AND USE_LIBCPP)
include(CheckCXXSourceCompiles)
include(CMakePushCheckState)
if(NOT MSVC)
set(CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} -Wl,--no-undefined")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pedantic -Wall")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -DRELEASE")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -O0 -g -DDEBUG")
cmake_push_check_state()
if (CMAKE_CXX_COMPILER_ID MATCHES "Clang")
# it seems that -O3 ruins the performance when using clang ...
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O2")
else()
# ... on the other side, GCC is incredibly comfortable with it.
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} -O3")
set(CMAKE_REQUIRED_FLAGS "${CMAKE_REQUIRED_FLAGS} -stdlib=libc++")
check_cxx_source_compiles("
#include<type_traits>
int main() { return std::is_same_v<int, char>; }
" HAS_LIBCPP)
if(NOT HAS_LIBCPP)
message(WARNING "The option USE_LIBCPP is set (by default) but libc++ is not available. The flag will not be added to the target.")
endif()
cmake_pop_check_state()
endif()
#
# CMake configuration
# Add EnTT target
#
set(PROJECT_CMAKE_IN ${entt_SOURCE_DIR}/cmake/in)
set(PROJECT_DEPS_DIR ${entt_SOURCE_DIR}/deps)
set(PROJECT_SRC_DIR ${entt_SOURCE_DIR}/src)
add_library(EnTT INTERFACE)
add_library(EnTT::EnTT ALIAS EnTT)
set(PROJECT_RUNTIME_OUTPUT_DIRECTORY bin)
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>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
target_compile_definitions(
EnTT
INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:DEBUG>
INTERFACE $<$<AND:$<CONFIG:Release>,$<NOT:$<CXX_COMPILER_ID:MSVC>>>:RELEASE>
)
if(USE_ASAN)
target_compile_options(EnTT INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<PLATFORM_ID:Windows>>>:-fsanitize=address -fno-omit-frame-pointer>)
target_link_libraries(EnTT INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<PLATFORM_ID:Windows>>>:-fsanitize=address -fno-omit-frame-pointer>)
endif()
if(USE_COMPILE_OPTIONS)
target_compile_options(
EnTT
INTERFACE $<$<AND:$<CONFIG:Debug>,$<NOT:$<PLATFORM_ID:Windows>>>:-O0 -g>
# it seems that -O3 ruins a bit the performance when using clang ...
INTERFACE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:Clang>,$<OR:$<PLATFORM_ID:Darwin>,$<PLATFORM_ID:Linux>>>:-O2>
# ... on the other side, GCC is incredibly comfortable with it.
INTERFACE $<$<AND:$<CONFIG:Release>,$<CXX_COMPILER_ID:GNU>>:-O3>
)
endif()
if(HAS_LIBCPP)
target_compile_options(EnTT BEFORE INTERFACE -stdlib=libc++)
endif()
target_compile_features(EnTT INTERFACE cxx_std_17)
#
# Install EnTT
#
if(${CMAKE_SYSTEM_NAME} STREQUAL "Windows")
set(CUSTOM_INSTALL_CONFIGDIR cmake)
else()
set(CUSTOM_INSTALL_CONFIGDIR ${CMAKE_INSTALL_LIBDIR}/cmake/entt)
endif()
install(DIRECTORY src/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
install(TARGETS EnTT EXPORT EnTTTargets)
export(EXPORT EnTTTargets FILE ${EnTT_BINARY_DIR}/EnTTTargets.cmake)
install(
EXPORT EnTTTargets
FILE EnTTTargets.cmake
DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
NAMESPACE EnTT::
)
#
# Build tree package config file
#
configure_file(cmake/in/EnTTBuildConfig.cmake.in EnTTConfig.cmake @ONLY)
include(CMakePackageConfigHelpers)
#
# Install tree package config file
#
configure_package_config_file(
cmake/in/EnTTConfig.cmake.in
${CUSTOM_INSTALL_CONFIGDIR}/EnTTConfig.cmake
INSTALL_DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
PATH_VARS CMAKE_INSTALL_INCLUDEDIR
)
write_basic_package_version_file(
${EnTT_BINARY_DIR}/EnTTConfigVersion.cmake
VERSION ${PROJECT_VERSION}
COMPATIBILITY AnyNewerVersion
)
install(
FILES
${EnTT_BINARY_DIR}/${CUSTOM_INSTALL_CONFIGDIR}/EnTTConfig.cmake
${EnTT_BINARY_DIR}/EnTTConfigVersion.cmake
DESTINATION ${CUSTOM_INSTALL_CONFIGDIR}
)
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)
find_package(Threads REQUIRED)
# gtest, gtest_main, gmock and gmock_main targets are available from now on
set(GOOGLETEST_DEPS_DIR ${PROJECT_DEPS_DIR}/googletest)
configure_file(${PROJECT_CMAKE_IN}/googletest.in ${GOOGLETEST_DEPS_DIR}/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
add_subdirectory(${GOOGLETEST_DEPS_DIR}/src ${GOOGLETEST_DEPS_DIR}/build)
option(FIND_GTEST_PACKAGE "Enable finding gtest package." OFF)
if(FIND_GTEST_PACKAGE)
find_package(GTest REQUIRED)
else()
# gtest, gtest_main, gmock and gmock_main targets are available from now on
set(GOOGLETEST_DEPS_DIR ${EnTT_SOURCE_DIR}/deps/googletest)
configure_file(${EnTT_SOURCE_DIR}/cmake/in/googletest.in ${GOOGLETEST_DEPS_DIR}/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${GOOGLETEST_DEPS_DIR})
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
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)
if(BUILD_MOD)
enable_language(C)
endif()
enable_testing()
add_subdirectory(test)
@@ -90,8 +205,30 @@ endif()
# Documentation
#
find_package(Doxygen 1.8)
option(BUILD_DOCS "Enable building with documentation." OFF)
if(DOXYGEN_FOUND)
add_subdirectory(docs)
if(BUILD_DOCS)
find_package(Doxygen 1.8)
if(DOXYGEN_FOUND)
add_subdirectory(docs)
endif()
endif()
#
# AOB
#
FILE(GLOB GH_WORKFLOWS .github/workflows/*.yml)
add_custom_target(
entt_aob
SOURCES
${GH_WORKFLOWS}
.github/FUNDING.yml
AUTHORS
CONTRIBUTING.md
LICENSE
README.md
TODO
)

43
CONTRIBUTING.md Normal file
View File

@@ -0,0 +1,43 @@
# Contributing
First of all, thank you very much for taking the time to contribute to the
`EnTT` framework.<br/>
How to do it mostly depends on the type of contribution:
* If you have a question, **please** ensure there isn't already an answer for
you by searching on GitHub under
[issues](https://github.com/skypjack/entt/issues). Do not forget to search
also through the closed ones. If you are unable to find a proper answer, feel
free to [open a new issue](https://github.com/skypjack/entt/issues/new).
Usually, questions are marked as such and closed in a few days.
* If you want to fix a typo in the inline documentation or in the README file,
if you want to add some new sections or if you want to help me with the
language by reviewing what I wrote so far (I'm not a native speaker after
all), **please** open a new
[pull request](https://github.com/skypjack/entt/pulls) with your changes.
* If you found a bug, **please** ensure there isn't already an answer for you by
searching on GitHub under [issues](https://github.com/skypjack/entt/issues).
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 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.
**Please**, add some tests to avoid regressions in future if possible, it
would be really appreciated. Note that the `EnTT` framework has a
[coverage at 100%](https://coveralls.io/github/skypjack/entt?branch=master)
(at least it was at 100% at the time I wrote this file) and this is the reason
for which you can be confident with using it in a production environment.
* If you want to propose a new feature and you know how to code it, **please**
do not issue directly a pull request. Before to do it,
[create a new issue](https://github.com/skypjack/entt/issues/new) to discuss
your proposal. Other users could be interested in your idea and the discussion
that will follow can refine it and therefore give us a better solution.
* If you want to request a new feature, I'm available for hiring. Take a look at
[my profile](https://github.com/skypjack) and feel free to write me.

View File

@@ -1,6 +1,6 @@
The MIT License (MIT)
Copyright (c) 2017 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

973
README.md

File diff suppressed because it is too large Load Diff

40
TODO Normal file
View File

@@ -0,0 +1,40 @@
* 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
* debugging tools (#60): the issue online already contains interesting tips on this, look at it
* 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)
- mt scheduler based on const awareness for types
* 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?
* types defined at runtime that refer to the same compile-time type (but to different pools) are possible, the library is almost there
* 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
* registry.each<T...>(first, last) by iterators, entities/components guaranteed
* built-in support for dual (or N-) buffering
* allow for custom stomp functions
* deprecate/replace snapshot
* hibitset, views and non-owning groups
* custom (decoupled) pools ==> double buffering, shared components, multi-model
* snapshot rework/deprecation
- create(hint: entity) -> force-create
- assign<T...>(first, last)
* use unordered_map for named pools and context variables:
- use direct access (pool-like) also for context variables
- allow for key/value variables where the key is an ENTT_ID_TYPE
- improves multi-stomp
* multi component registry::remove and some others?
- auto foo(It first, It last = entity_type{null})
* range based registry::remove and some others?
* add examples (and credits) from @alanjfs :)
* explore the possibility to wrap other backend with a XHandler component
* use [[nodiscard]] consistently for safety purposes
* static reflection, hint: template<> meta_type_t<Type>: meta_descriptor<name, func..., props..., etc...>
* null support for entt::component
* add ENTT_CUSTOM_NAMED_TYPE for custom names
* Make another attempt to overcome named types
* meta: members+class as fake functions, is it possible?
* named types: almost-stable index optimization for direct access to pools, no more linear searches

41
WORKSPACE Normal file
View File

@@ -0,0 +1,41 @@
workspace(name = "com_github_skypjack_entt")
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")
http_archive(
name = "com_google_googletest",
strip_prefix = "googletest-release-1.8.1",
url = "https://github.com/google/googletest/archive/release-1.8.1.tar.gz",
sha256 = "9bf1fe5182a604b4135edc1a425ae356c9ad15e9b23f9f12a02e80184c3a249c",
)
http_archive(
name = "org_duktape",
url = "https://duktape.org/duktape-2.4.0.tar.xz",
strip_prefix = "duktape-2.4.0",
sha256 = "86a89307d1633b5cedb2c6e56dc86e92679fc34b05be551722d8cc69ab0771fc",
build_file_content = """
cc_library(
name = "duktape",
visibility = ["//visibility:public"],
strip_include_prefix = "src",
hdrs = ["src/duktape.h", "src/duk_config.h"],
srcs = ["src/duktape.c", "src/duktape.h", "src/duk_config.h"],
)
""",
)
http_archive(
name = "bazelregistry_cereal",
strip_prefix = "cereal-8629f40d932d57c5337d4557327f6f22436211b7",
url = "https://github.com/bazelregistry/cereal/archive/8629f40d932d57c5337d4557327f6f22436211b7.zip",
sha256 = "c983a7a2e16b153c3de022a0818d2f4836e510a3fc3bea9d3703de79f58a90a6",
)
# This is for bazelregistry_cereal
http_archive(
name = "bazelregistry_rapidjson",
strip_prefix = "rapidjson-6b980984dacf689be8f65be823203b967c69da04",
url = "https://github.com/bazelregistry/rapidjson/archive/6b980984dacf689be8f65be823203b967c69da04.zip",
sha256 = "82187ba8de53bab3b4fb9e56d0bae81a96fa27a115e5a6ce14c70f0ef9338965",
)

View File

@@ -1,22 +0,0 @@
# can use variables like {build} and {branch}
version: 1.0.{build}
image: Visual Studio 2017
environment:
BUILD_DIR: "%APPVEYOR_BUILD_FOLDER%\\build"
platform:
- Win32
configuration:
- Release
before_build:
- cd %BUILD_DIR%
- cmake .. -G"Visual Studio 15 2017"
build:
parallel: true
project: build/entt.sln
verbosity: minimal

1
bazel/BUILD.bazel Normal file
View File

@@ -0,0 +1 @@
exports_files(["copts.bzl"], visibility = ["//:__subpackages__"])

10
bazel/copts.bzl Normal file
View File

@@ -0,0 +1,10 @@
_msvc_copts = ["/std:c++17"]
_gcc_copts = ["-std=c++17"]
entt_copts = select({
# Windows
"@bazel_tools//src/conditions:windows": _msvc_copts,
"@bazel_tools//src/conditions:windows_msvc": _msvc_copts,
"@bazel_tools//src/conditions:windows_msys": _msvc_copts,
"//conditions:default": _gcc_copts,
})

View File

@@ -0,0 +1,6 @@
set(ENTT_VERSION "@PROJECT_VERSION@")
set(ENTT_INCLUDE_DIRS "@CMAKE_CURRENT_SOURCE_DIR@/src")
if(NOT CMAKE_VERSION VERSION_LESS "3.0")
include("${CMAKE_CURRENT_LIST_DIR}/EnTTTargets.cmake")
endif()

View File

@@ -0,0 +1,11 @@
set(ENTT_VERSION "@PROJECT_VERSION@")
@PACKAGE_INIT@
set_and_check(ENTT_INCLUDE_DIRS "@PACKAGE_CMAKE_INSTALL_INCLUDEDIR@")
if(NOT CMAKE_VERSION VERSION_LESS "3.0")
include("${CMAKE_CURRENT_LIST_DIR}/EnTTTargets.cmake")
endif()
check_required_components("@PROJECT_NAME@")

19
cmake/in/cereal.in Normal file
View File

@@ -0,0 +1,19 @@
project(cereal-download NONE)
cmake_minimum_required(VERSION 3.2)
include(ExternalProject)
ExternalProject_Add(
cereal
GIT_REPOSITORY https://github.com/USCiLab/cereal.git
GIT_TAG v1.2.2
DOWNLOAD_DIR ${CEREAL_DEPS_DIR}
TMP_DIR ${CEREAL_DEPS_DIR}/tmp
STAMP_DIR ${CEREAL_DEPS_DIR}/stamp
SOURCE_DIR ${CEREAL_DEPS_DIR}/src
BINARY_DIR ${CEREAL_DEPS_DIR}/build
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

19
cmake/in/duktape.in Normal file
View File

@@ -0,0 +1,19 @@
project(duktape-download NONE)
cmake_minimum_required(VERSION 3.2)
include(ExternalProject)
ExternalProject_Add(
duktape
GIT_REPOSITORY https://github.com/svaarala/duktape-releases.git
GIT_TAG v2.2.0
DOWNLOAD_DIR ${DUKTAPE_DEPS_DIR}
TMP_DIR ${DUKTAPE_DEPS_DIR}/tmp
STAMP_DIR ${DUKTAPE_DEPS_DIR}/stamp
SOURCE_DIR ${DUKTAPE_DEPS_DIR}/src
BINARY_DIR ${DUKTAPE_DEPS_DIR}/build
CONFIGURE_COMMAND ""
BUILD_COMMAND ""
INSTALL_COMMAND ""
TEST_COMMAND ""
)

View File

@@ -6,7 +6,7 @@ include(ExternalProject)
ExternalProject_Add(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0
GIT_TAG master
DOWNLOAD_DIR ${GOOGLETEST_DEPS_DIR}
TMP_DIR ${GOOGLETEST_DEPS_DIR}/tmp
STAMP_DIR ${GOOGLETEST_DEPS_DIR}/stamp

11
cmake/in/version.h.in Normal file
View 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

37
conan/build.py Normal file
View File

@@ -0,0 +1,37 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from cpt.packager import ConanMultiPackager
import os
if __name__ == "__main__":
username = os.getenv("GITHUB_ACTOR")
tag_version = os.getenv("GITHUB_REF")
tag_package = os.getenv("GITHUB_REPOSITORY")
login_username = os.getenv("CONAN_LOGIN_USERNAME")
package_version = tag_version.replace("refs/tags/v", "")
package_name = tag_package.replace("skypjack/", "")
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)
disable_shared = os.getenv("CONAN_DISABLE_SHARED_BUILD", "False")
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)
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
View File

@@ -0,0 +1,7 @@
#!/bin/bash
set -e
set -x
conan user
python conan/build.py

6
conan/ci/install.sh Normal file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
set -e
set -x
pip install -U conan_package_tools conan

View 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})

View 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)

View 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 &registry) {
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 &registry) {
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;
}

27
conanfile.py Normal file
View File

@@ -0,0 +1,27 @@
#!/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_info(self):
if not self.in_local_cache:
self.cpp_info.includedirs = ["src"]
def package_id(self):
self.info.header_only()

View File

@@ -2,26 +2,30 @@
# Doxygen configuration (documentation)
#
set(TARGET_DOCS docs)
set(DOXY_IN_FILE doxy.in)
set(DOXY_SOURCE_DIRECTORY ${PROJECT_SRC_DIR})
set(DOXY_SOURCE_DIRECTORY ${EnTT_SOURCE_DIR}/src)
set(DOXY_DOCS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR})
set(DOXY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
set(DOXY_CFG_FILE doxy.cfg)
configure_file(${DOXY_IN_FILE} ${DOXY_CFG_FILE} @ONLY)
configure_file(doxy.in doxy.cfg @ONLY)
add_custom_target(
${TARGET_DOCS}
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/${DOXY_CFG_FILE}
WORKING_DIRECTORY ${entt_SOURCE_DIR}
docs ALL
COMMAND ${DOXYGEN_EXECUTABLE} ${CMAKE_CURRENT_BINARY_DIR}/doxy.cfg
WORKING_DIRECTORY ${EnTT_SOURCE_DIR}
VERBATIM
SOURCES ${DOXY_IN_FILE}
SOURCES doxy.in
)
install(
DIRECTORY ${DOXY_OUTPUT_DIRECTORY}/html
DESTINATION share/${PROJECT_NAME}-${PROJECT_VERSION}/
)
FILE(GLOB MD_FILES md/*.md)
add_custom_target(
docs_aob
SOURCES
dox/extra.dox
${MD_FILES}
)

View File

@@ -1,395 +0,0 @@
Attribution 4.0 International
=======================================================================
Creative Commons Corporation ("Creative Commons") is not a law firm and
does not provide legal services or legal advice. Distribution of
Creative Commons public licenses does not create a lawyer-client or
other relationship. Creative Commons makes its licenses and related
information available on an "as-is" basis. Creative Commons gives no
warranties regarding its licenses, any material licensed under their
terms and conditions, or any related information. Creative Commons
disclaims all liability for damages resulting from their use to the
fullest extent possible.
Using Creative Commons Public Licenses
Creative Commons public licenses provide a standard set of terms and
conditions that creators and other rights holders may use to share
original works of authorship and other material subject to copyright
and certain other rights specified in the public license below. The
following considerations are for informational purposes only, are not
exhaustive, and do not form part of our licenses.
Considerations for licensors: Our public licenses are
intended for use by those authorized to give the public
permission to use material in ways otherwise restricted by
copyright and certain other rights. Our licenses are
irrevocable. Licensors should read and understand the terms
and conditions of the license they choose before applying it.
Licensors should also secure all rights necessary before
applying our licenses so that the public can reuse the
material as expected. Licensors should clearly mark any
material not subject to the license. This includes other CC-
licensed material, or material used under an exception or
limitation to copyright. More considerations for licensors:
wiki.creativecommons.org/Considerations_for_licensors
Considerations for the public: By using one of our public
licenses, a licensor grants the public permission to use the
licensed material under specified terms and conditions. If
the licensor's permission is not necessary for any reason--for
example, because of any applicable exception or limitation to
copyright--then that use is not regulated by the license. Our
licenses grant only permissions under copyright and certain
other rights that a licensor has authority to grant. Use of
the licensed material may still be restricted for other
reasons, including because others have copyright or other
rights in the material. A licensor may make special requests,
such as asking that all changes be marked or described.
Although not required by our licenses, you are encouraged to
respect those requests where reasonable. More_considerations
for the public:
wiki.creativecommons.org/Considerations_for_licensees
=======================================================================
Creative Commons Attribution 4.0 International Public License
By exercising the Licensed Rights (defined below), You accept and agree
to be bound by the terms and conditions of this Creative Commons
Attribution 4.0 International Public License ("Public License"). To the
extent this Public License may be interpreted as a contract, You are
granted the Licensed Rights in consideration of Your acceptance of
these terms and conditions, and the Licensor grants You such rights in
consideration of benefits the Licensor receives from making the
Licensed Material available under these terms and conditions.
Section 1 -- Definitions.
a. Adapted Material means material subject to Copyright and Similar
Rights that is derived from or based upon the Licensed Material
and in which the Licensed Material is translated, altered,
arranged, transformed, or otherwise modified in a manner requiring
permission under the Copyright and Similar Rights held by the
Licensor. For purposes of this Public License, where the Licensed
Material is a musical work, performance, or sound recording,
Adapted Material is always produced where the Licensed Material is
synched in timed relation with a moving image.
b. Adapter's License means the license You apply to Your Copyright
and Similar Rights in Your contributions to Adapted Material in
accordance with the terms and conditions of this Public License.
c. Copyright and Similar Rights means copyright and/or similar rights
closely related to copyright including, without limitation,
performance, broadcast, sound recording, and Sui Generis Database
Rights, without regard to how the rights are labeled or
categorized. For purposes of this Public License, the rights
specified in Section 2(b)(1)-(2) are not Copyright and Similar
Rights.
d. Effective Technological Measures means those measures that, in the
absence of proper authority, may not be circumvented under laws
fulfilling obligations under Article 11 of the WIPO Copyright
Treaty adopted on December 20, 1996, and/or similar international
agreements.
e. Exceptions and Limitations means fair use, fair dealing, and/or
any other exception or limitation to Copyright and Similar Rights
that applies to Your use of the Licensed Material.
f. Licensed Material means the artistic or literary work, database,
or other material to which the Licensor applied this Public
License.
g. Licensed Rights means the rights granted to You subject to the
terms and conditions of this Public License, which are limited to
all Copyright and Similar Rights that apply to Your use of the
Licensed Material and that the Licensor has authority to license.
h. Licensor means the individual(s) or entity(ies) granting rights
under this Public License.
i. Share means to provide material to the public by any means or
process that requires permission under the Licensed Rights, such
as reproduction, public display, public performance, distribution,
dissemination, communication, or importation, and to make material
available to the public including in ways that members of the
public may access the material from a place and at a time
individually chosen by them.
j. Sui Generis Database Rights means rights other than copyright
resulting from Directive 96/9/EC of the European Parliament and of
the Council of 11 March 1996 on the legal protection of databases,
as amended and/or succeeded, as well as other essentially
equivalent rights anywhere in the world.
k. You means the individual or entity exercising the Licensed Rights
under this Public License. Your has a corresponding meaning.
Section 2 -- Scope.
a. License grant.
1. Subject to the terms and conditions of this Public License,
the Licensor hereby grants You a worldwide, royalty-free,
non-sublicensable, non-exclusive, irrevocable license to
exercise the Licensed Rights in the Licensed Material to:
a. reproduce and Share the Licensed Material, in whole or
in part; and
b. produce, reproduce, and Share Adapted Material.
2. Exceptions and Limitations. For the avoidance of doubt, where
Exceptions and Limitations apply to Your use, this Public
License does not apply, and You do not need to comply with
its terms and conditions.
3. Term. The term of this Public License is specified in Section
6(a).
4. Media and formats; technical modifications allowed. The
Licensor authorizes You to exercise the Licensed Rights in
all media and formats whether now known or hereafter created,
and to make technical modifications necessary to do so. The
Licensor waives and/or agrees not to assert any right or
authority to forbid You from making technical modifications
necessary to exercise the Licensed Rights, including
technical modifications necessary to circumvent Effective
Technological Measures. For purposes of this Public License,
simply making modifications authorized by this Section 2(a)
(4) never produces Adapted Material.
5. Downstream recipients.
a. Offer from the Licensor -- Licensed Material. Every
recipient of the Licensed Material automatically
receives an offer from the Licensor to exercise the
Licensed Rights under the terms and conditions of this
Public License.
b. No downstream restrictions. You may not offer or impose
any additional or different terms or conditions on, or
apply any Effective Technological Measures to, the
Licensed Material if doing so restricts exercise of the
Licensed Rights by any recipient of the Licensed
Material.
6. No endorsement. Nothing in this Public License constitutes or
may be construed as permission to assert or imply that You
are, or that Your use of the Licensed Material is, connected
with, or sponsored, endorsed, or granted official status by,
the Licensor or others designated to receive attribution as
provided in Section 3(a)(1)(A)(i).
b. Other rights.
1. Moral rights, such as the right of integrity, are not
licensed under this Public License, nor are publicity,
privacy, and/or other similar personality rights; however, to
the extent possible, the Licensor waives and/or agrees not to
assert any such rights held by the Licensor to the limited
extent necessary to allow You to exercise the Licensed
Rights, but not otherwise.
2. Patent and trademark rights are not licensed under this
Public License.
3. To the extent possible, the Licensor waives any right to
collect royalties from You for the exercise of the Licensed
Rights, whether directly or through a collecting society
under any voluntary or waivable statutory or compulsory
licensing scheme. In all other cases the Licensor expressly
reserves any right to collect such royalties.
Section 3 -- License Conditions.
Your exercise of the Licensed Rights is expressly made subject to the
following conditions.
a. Attribution.
1. If You Share the Licensed Material (including in modified
form), You must:
a. retain the following if it is supplied by the Licensor
with the Licensed Material:
i. identification of the creator(s) of the Licensed
Material and any others designated to receive
attribution, in any reasonable manner requested by
the Licensor (including by pseudonym if
designated);
ii. a copyright notice;
iii. a notice that refers to this Public License;
iv. a notice that refers to the disclaimer of
warranties;
v. a URI or hyperlink to the Licensed Material to the
extent reasonably practicable;
b. indicate if You modified the Licensed Material and
retain an indication of any previous modifications; and
c. indicate the Licensed Material is licensed under this
Public License, and include the text of, or the URI or
hyperlink to, this Public License.
2. You may satisfy the conditions in Section 3(a)(1) in any
reasonable manner based on the medium, means, and context in
which You Share the Licensed Material. For example, it may be
reasonable to satisfy the conditions by providing a URI or
hyperlink to a resource that includes the required
information.
3. If requested by the Licensor, You must remove any of the
information required by Section 3(a)(1)(A) to the extent
reasonably practicable.
4. If You Share Adapted Material You produce, the Adapter's
License You apply must not prevent recipients of the Adapted
Material from complying with this Public License.
Section 4 -- Sui Generis Database Rights.
Where the Licensed Rights include Sui Generis Database Rights that
apply to Your use of the Licensed Material:
a. for the avoidance of doubt, Section 2(a)(1) grants You the right
to extract, reuse, reproduce, and Share all or a substantial
portion of the contents of the database;
b. if You include all or a substantial portion of the database
contents in a database in which You have Sui Generis Database
Rights, then the database in which You have Sui Generis Database
Rights (but not its individual contents) is Adapted Material; and
c. You must comply with the conditions in Section 3(a) if You Share
all or a substantial portion of the contents of the database.
For the avoidance of doubt, this Section 4 supplements and does not
replace Your obligations under this Public License where the Licensed
Rights include other Copyright and Similar Rights.
Section 5 -- Disclaimer of Warranties and Limitation of Liability.
a. UNLESS OTHERWISE SEPARATELY UNDERTAKEN BY THE LICENSOR, TO THE
EXTENT POSSIBLE, THE LICENSOR OFFERS THE LICENSED MATERIAL AS-IS
AND AS-AVAILABLE, AND MAKES NO REPRESENTATIONS OR WARRANTIES OF
ANY KIND CONCERNING THE LICENSED MATERIAL, WHETHER EXPRESS,
IMPLIED, STATUTORY, OR OTHER. THIS INCLUDES, WITHOUT LIMITATION,
WARRANTIES OF TITLE, MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE, NON-INFRINGEMENT, ABSENCE OF LATENT OR OTHER DEFECTS,
ACCURACY, OR THE PRESENCE OR ABSENCE OF ERRORS, WHETHER OR NOT
KNOWN OR DISCOVERABLE. WHERE DISCLAIMERS OF WARRANTIES ARE NOT
ALLOWED IN FULL OR IN PART, THIS DISCLAIMER MAY NOT APPLY TO YOU.
b. TO THE EXTENT POSSIBLE, IN NO EVENT WILL THE LICENSOR BE LIABLE
TO YOU ON ANY LEGAL THEORY (INCLUDING, WITHOUT LIMITATION,
NEGLIGENCE) OR OTHERWISE FOR ANY DIRECT, SPECIAL, INDIRECT,
INCIDENTAL, CONSEQUENTIAL, PUNITIVE, EXEMPLARY, OR OTHER LOSSES,
COSTS, EXPENSES, OR DAMAGES ARISING OUT OF THIS PUBLIC LICENSE OR
USE OF THE LICENSED MATERIAL, EVEN IF THE LICENSOR HAS BEEN
ADVISED OF THE POSSIBILITY OF SUCH LOSSES, COSTS, EXPENSES, OR
DAMAGES. WHERE A LIMITATION OF LIABILITY IS NOT ALLOWED IN FULL OR
IN PART, THIS LIMITATION MAY NOT APPLY TO YOU.
c. The disclaimer of warranties and limitation of liability provided
above shall be interpreted in a manner that, to the extent
possible, most closely approximates an absolute disclaimer and
waiver of all liability.
Section 6 -- Term and Termination.
a. This Public License applies for the term of the Copyright and
Similar Rights licensed here. However, if You fail to comply with
this Public License, then Your rights under this Public License
terminate automatically.
b. Where Your right to use the Licensed Material has terminated under
Section 6(a), it reinstates:
1. automatically as of the date the violation is cured, provided
it is cured within 30 days of Your discovery of the
violation; or
2. upon express reinstatement by the Licensor.
For the avoidance of doubt, this Section 6(b) does not affect any
right the Licensor may have to seek remedies for Your violations
of this Public License.
c. For the avoidance of doubt, the Licensor may also offer the
Licensed Material under separate terms or conditions or stop
distributing the Licensed Material at any time; however, doing so
will not terminate this Public License.
d. Sections 1, 5, 6, 7, and 8 survive termination of this Public
License.
Section 7 -- Other Terms and Conditions.
a. The Licensor shall not be bound by any additional or different
terms or conditions communicated by You unless expressly agreed.
b. Any arrangements, understandings, or agreements regarding the
Licensed Material not stated herein are separate from and
independent of the terms and conditions of this Public License.
Section 8 -- Interpretation.
a. For the avoidance of doubt, this Public License does not, and
shall not be interpreted to, reduce, limit, restrict, or impose
conditions on any use of the Licensed Material that could lawfully
be made without permission under this Public License.
b. To the extent possible, if any provision of this Public License is
deemed unenforceable, it shall be automatically reformed to the
minimum extent necessary to make it enforceable. If the provision
cannot be reformed, it shall be severed from this Public License
without affecting the enforceability of the remaining terms and
conditions.
c. No term or condition of this Public License will be waived and no
failure to comply consented to unless expressly agreed to by the
Licensor.
d. Nothing in this Public License constitutes or may be interpreted
as a limitation upon, or waiver of, any privileges and immunities
that apply to the Licensor or You, including from the legal
processes of any jurisdiction or authority.
=======================================================================
Creative Commons is not a party to its public
licenses. Notwithstanding, Creative Commons may elect to apply one of
its public licenses to material it publishes and in those instances
will be considered the “Licensor.” The text of the Creative Commons
public licenses is dedicated to the public domain under the CC0 Public
Domain Dedication. Except for the limited purpose of indicating that
material is shared under a Creative Commons public license or as
otherwise permitted by the Creative Commons policies published at
creativecommons.org/policies, Creative Commons does not authorize the
use of the trademark "Creative Commons" or any other trademark or logo
of Creative Commons without its prior written consent including,
without limitation, in connection with any unauthorized modifications
to any of its public licenses or any other arrangements,
understandings, or agreements concerning use of licensed material. For
the avoidance of doubt, this paragraph does not form part of the
public licenses.
Creative Commons may be contacted at creativecommons.org.

View File

@@ -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

184
docs/md/core.md Normal file
View File

@@ -0,0 +1,184 @@
# Crash Course: core functionalities
<!--
@cond TURN_OFF_DOXYGEN
-->
# Table of Contents
* [Introduction](#introduction)
* [Compile-time identifiers](#compile-time-identifiers)
* [Runtime identifiers](#runtime-identifiers)
* [Hashed strings](#hashed-strings)
* [Wide characters](wide-characters)
* [Conflicts](#conflicts)
* [Monostate](#monostate)
<!--
@endcond TURN_OFF_DOXYGEN
-->
# Introduction
`EnTT` comes with a bunch of core functionalities mostly used by the other parts
of the library itself.<br/>
Hardly users will include these features in their code, but it's worth
describing what `EnTT` offers so as not to reinvent the wheel in case of need.
# Compile-time identifiers
Sometimes it's useful to be able to give unique identifiers to types at
compile-time.<br/>
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:
```cpp
#include <ident.hpp>
// defines the identifiers for the given types
using id = entt::identifier<a_type, another_type>;
// ...
switch(a_type_identifier) {
case id::type<a_type>:
// ...
break;
case id::type<another_type>:
// ...
break;
default:
// ...
}
```
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
same for every run. In case they have been used in a production environment and
a type has to be removed, one can just use a placeholder to left the other
identifiers unchanged:
```cpp
template<typename> struct ignore_type {};
using id = entt::identifier<
a_type_still_valid,
ignore_type<a_type_no_longer_valid>,
another_type_still_valid
>;
```
A bit ugly to see, but it works at least.
# Runtime identifiers
Sometimes it's useful to be able to give unique identifiers to types at
runtime.<br/>
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
entity-component system:
```cpp
using component_family = entt::family<struct internal_registry_component_family>;
// ...
template<typename Component>
component_type component() const noexcept {
return component_family::type<Component>;
}
```
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.
Indeed it mostly depends on the flow of execution.
# Hashed strings
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
characters. Once created, all what one can do with it is getting back the
original string or converting it into a number.<br/>
The good part is that a hashed string can be used wherever a constant expression
is required and no _string-to-number_ conversion will take place at runtime if
used carefully.
Example of use:
```cpp
auto load(entt::hashed_string::hash_type resource) {
// uses the numeric representation of the resource to load and return it
}
auto resource = load(entt::hashed_string{"gui/background"});
```
There is also a _user defined literal_ dedicated to hashed strings to make them
more user-friendly:
```cpp
constexpr auto str = "text"_hs;
```
## Wide characters
The hashed string has a design that is close to that of an `std::basic_string`.
It means that `hashed_string` is nothing more than an alias for
`basic_hashed_string<char>`. For those who want to use the C++ type for wide
character representation, there exists also the alias `hashed_wstring` for
`basic_hashed_string<wchar_t>`.<br/>
In this case, the user defined literal to use to create hashed strings on the
fly is `_hws`:
```cpp
constexpr auto str = "text"_hws;
```
Note that the hash type of the `hashed_wstring` is the same of its counterpart.
## Conflicts
The hashed string class uses internally FNV-1a to compute the numeric
counterpart of a string. Because of the _pigeonhole principle_, conflicts are
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 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.
# Monostate
The monostate pattern is often presented as an alternative to a singleton based
configuration system. This is exactly its purpose in `EnTT`. Moreover, this
implementation is thread safe by design (hopefully).<br/>
Keys are represented by hashed strings, values are basic types like `int`s or
`bool`s. Values of different types can be associated to each key, even more than
one at a time. Because of this, users must pay attention to use the same type
both during an assignment and when they try to read back their data. Otherwise,
they will probably incur in unexpected results.
Example of use:
```cpp
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::hashed_string{"mykey"}>{};
```

1774
docs/md/entity.md Normal file

File diff suppressed because it is too large Load Diff

199
docs/md/faq.md Normal file
View File

@@ -0,0 +1,199 @@
# 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)
* [How can I represent hierarchies with my components?](#how-can-i-represent-hierarchies-with-my-components)
* [Custom entity identifiers: yay or nay?](#custom-entity-identifiers-yay-or-nay)
* [Warning C4307: integral constant overflow](#warning-C4307-integral-constant-overflow)
* [Warning C4003: the min, the max and the macro](#warning-C4003-the-min-the-max-and-the-macro)
* [The standard and the non-copyable types](#the-standard-and-the-non-copyable-types)
<!--
@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`.
## How can I represent hierarchies with my components?
This is one of the first questions that anyone makes when starting to work with
the entity-component-system architectural pattern.<br/>
There are several approaches to the problem and whats the best one depends
mainly on the real problem one is facing. In all cases, how to do it doesn't
strictly depend on the library in use, but the latter can certainly allow or
not different techniques depending on how the data are laid out.
I tried to describe some of the techniques that fit well with the model of
`EnTT`. [Here](https://skypjack.github.io/2019-06-25-ecs-baf-part-4/) is the
first post of a series that tries to explore the problem. More will probably
come in future.
Long story short, you can always define a tree where the nodes expose implicit
lists of children by means of the following type:
```cpp
struct relationship {
std::size_t children{};
entt::entity first{entt::null};
entt::entity prev{entt::null};
entt::entity next{entt::null};
entt::entity parent{entt::null};
// ... other data members ...
};
```
The sort functionalities of `EnTT`, the groups and all the other features of the
library can help then to get the best in terms of data locality and therefore
performance from this component.
## Custom entity identifiers: yay or nay?
Custom entity identifiers are definitely a good idea in two cases at least:
* If `std::uint32_t` isn't large enough as an underlying type.
* If you want to avoid conflicts when using multiple registries.
These identifiers are nothing more than enum classes with some salt.<br/>
To simplify the creation of new identifiers, `EnTT` provides the macro
`ENTT_OPAQUE_TYPE` that accepts two arguments:
* The name you want to give to the new identifier (watch out for namespaces).
* The underlying type to use (either `std::uint16_t`, `std::uint32_t`
or `std::uint64_t`).
In fact, this is the definition of `entt::entity`:
```cpp
ENTT_OPAQUE_TYPE(entity, std::uint32_t)
```
The use of this macro is highly recommended, so as not to run into problems if
the requirements for the identifiers should change in the future.
## Warning C4307: integral constant overflow
According to [this](https://github.com/skypjack/entt/issues/121) issue, using a
hashed string under VS could generate a warning.<br/>
First of all, I want to reassure you: it's expected and harmless. However, it
can be annoying.
To suppress it and if you don't want to suppress all the other warnings as well,
here is a workaround in the form of a macro:
```cpp
#if defined(_MSC_VER)
#define HS(str)\
__pragma(warning(push))\
__pragma(warning(disable:4307))\
entt::hashed_string{str}\
__pragma(warning(pop))
#else
#define HS(str) entt::hashed_string{str}
#endif
```
With an example of use included:
```cpp
constexpr auto identifier = HS("my/resource/identifier");
```
Thanks to [huwpascoe](https://github.com/huwpascoe) for the courtesy.
## Warning C4003: the min, the max and the macro
On Windows, a header file defines two macros `min` and `max` which may result in
conflicts with their counterparts in the standard library and therefore in
errors during compilation.
It's a pretty big problem but fortunately it's not a problem of `EnTT` and there
is a fairly simple solution to it.<br/>
It consists in defining the `NOMINMAX` macro before to include any other header
so as to get rid of the extra definitions:
```cpp
#define NOMINMAX
```
Please refer to [this](https://github.com/skypjack/entt/issues/96) issue for
more details.
## The standard and the non-copyable types
`EnTT` uses internally the trait `std::is_copy_constructible_v` to check if a
component is actually copyable. This trait doesn't check if an object can
actually be copied but only verifies if there is a copy constructor
available.<br/>
This can lead to surprising results due to some idiosyncrasies of the standard
mainly related to the need to guarantee backward compatibility.
For example, `std::vector` defines a copy constructor no matter if its value
type is copyable or not. As a result, `std::is_copy_constructible_v` is true
for the following specialization:
```cpp
struct type {
std::vector<std::unique_ptr<action>> vec;
};
```
When trying to assign an instance of this type to an entity in the ECS part,
this may trigger a compilation error because we cannot really make a copy of
it.<br/>
As a workaround, users can mark the type explicitly as non-copyable:
```cpp
struct type {
type(const type &) = delete;
type & operator=(const type &) = delete;
std::vector<std::unique_ptr<action>> vec;
};
```
Unfortunately, this will also disable aggregate initialization.

196
docs/md/lib.md Normal file
View File

@@ -0,0 +1,196 @@
# 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)
* [Runtime reflection system](#runtime-reflection-system)
* [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.
# Runtime reflection system
The runtime reflection system deserves a special mention when it comes to using
it across boundaries.<br/>
As in all other cases, it's necessary to give a name to the types. However, this
time this isn't enough to get the job done.
The runtime reflection system is linked to a static context to which the visible
components are linked. A component is visible when it's given a name, so the
named components are implicitly visible.<br/>
Different contexts don't relate to each other. Therefore, to allow the use of
named types across boundaries, a context must also be shared.
Sharing a context is straightforward. First of all, the local one must be
acquired:
```
entt::meta_ctx ctx{};
```
Then, it must be pushed across boundaries and the receiving space must set it as
its global context, thus releasing the local one that remains available but is
no longer referred to by the runtime reflection system:
```
entt::meta_ctx::bind(ctx);
```
From now on, both spaces will refer to the same context and on it will be
associated the new visible meta types, no matter _where_ they are created.
A context can also be reset and then associated again locally as:
```
entt::meta_ctx::bind{entt::meta_ctx{});
```
This is allowed because local and global contexts are separated. Therefore, it's
always possible to make the local context the current one again.
# 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.

115
docs/md/links.md Normal file
View File

@@ -0,0 +1,115 @@
# 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.
* [Land of the Rair](https://github.com/LandOfTheRair/core2): the new backend
of [a retro-style MUD](https://rair.land/) for the new age.
* [Openblack](https://github.com/openblack/openblack): open source
reimplementation of the game _Black & White_ (2001).
* [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`.
* [Wacman](https://github.com/carlfindahl/wacman): a pacman clone with OpenGL.
* [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`.
* [Arcade puzzle game with EnTT](https://github.com/MasonRG/ArcadePuzzleGame):
arcade puzzle game made in C++ using the `SDL2` and `EnTT` libraries.
* [Snake with EnTT](https://github.com/MasonRG/SnakeGame): simple snake game
made in C++ with the `SDL2` and `EnTT` libraries.
* [Mirrors lasers and robots](https://github.com/guillaume-haerinck/imac-tower-defense):
a small tower defense game based on mirror orientation.
* Engines and the like:
* [Fling Engine](https://github.com/flingengine/FlingEngine): A Vulkan game
engine with a focus on data oriented design.
* [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.
* [NovusCore](https://github.com/novuscore/NovusCore): A modern take on World
of Warcraft emulation.
* [ImGui/EnTT editor](https://github.com/Green-Sky/imgui_entt_entity_editor):
A drop-in, single-file entity editor for `EnTT` that uses `ImGui` as
graphical backend (with
[demo code](https://github.com/Green-Sky/imgui_entt_entity_editor_demo)).
* 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.
* [Apparently](https://www.linkedin.com/jobs/view/architekt-c%2B%2B-at-tieto-1219512333/)
[Tieto](https://www.tieto.com/): they published a job post where `EnTT` was
listed on their software stack.
* [Godot meets EnTT](https://github.com/portaloffreedom/godot_entt_example/):
A simple example on how to use `EnTT` within
[`Godot`](https://godotengine.org/).
* [Godot and GameNetworkingSockets meet EnTT](https://github.com/portaloffreedom/godot_entt_net_example):
A simple example on how to use `EnTT` and
[`GameNetworkingSockets`](https://github.com/ValveSoftware/GameNetworkingSockets)
within [`Godot`](https://godotengine.org/).
* [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.

75
docs/md/locator.md Normal file
View File

@@ -0,0 +1,75 @@
# Crash Course: service locator
<!--
@cond TURN_OFF_DOXYGEN
-->
# Table of Contents
* [Introduction](#introduction)
* [Service locator](#service-locator)
<!--
@endcond TURN_OFF_DOXYGEN
-->
# Introduction
Usually service locators are tightly bound to the services they expose and it's
hard to define a general purpose solution. This template based implementation
tries to fill the gap and to get rid of the burden of defining a different
specific locator for each application.<br/>
This class is tiny, partially unsafe and thus risky to use. Moreover it doesn't
fit probably most of the scenarios in which a service locator is required. Look
at it as a small tool that can sometimes be useful if users know how to handle
it.
# Service locator
The API is straightforward. The basic idea is that services are implemented by
means of interfaces and rely on polymorphism.<br/>
The locator is instantiated with the base type of the service if any and a
concrete implementation is provided along with all the parameters required to
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::service_locator<my_service>::set(params...);
// sets up an opaque service
entt::service_locator<audio_interface>::set<audio_implementation>(params...);
// resets (destroys) the service
entt::service_locator<audio_interface>::reset();
```
The locator can also be queried to know if an active service is currently set
and to retrieve it if necessary (either as a pointer or as a reference):
```cpp
// no service currently set
auto empty = entt::service_locator<audio_interface>::empty();
// gets a (possibly empty) shared pointer to the service ...
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
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::service_locator<camera_interface>;
using audio = entt::service_locator<audio_interface>;
// ...
};
// ...
void init() {
locator::camera::set<camera_null>();
locator::audio::set<audio_implementation>(params...);
// ...
}
```

585
docs/md/meta.md Normal file
View File

@@ -0,0 +1,585 @@
# Crash Course: runtime reflection system
<!--
@cond TURN_OFF_DOXYGEN
-->
# Table of Contents
* [Introduction](#introduction)
* [Names and identifiers](#names-and-identifiers)
* [Reflection in a nutshell](#reflection-in-a-nutshell)
* [Any as in any type](#any-as-in-any-type)
* [Enjoy the runtime](#enjoy-the-runtime)
* [Policies: the more, the less](#policies-the-more-the-less)
* [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.
# Names and identifiers
The meta system doesn't force the user to use the tools provided by the library
when it comes to working with names and identifiers. It does this by offering an
API that works with opaque identifiers that may or may not be generated by means
of a hashed string.<br/>
This means that users can assign any type of identifier to the meta objects, as
long as they are numeric. It doesn't matter if they are generated at runtime, at
compile-time or with custom functions.
However, the examples in the following sections are all based on the
`hashed_string` class as provided by this library. Therefore, where an
identifier is required, it's likely that a user defined literal is used as
follows:
```cpp
auto factory = entt::meta<my_type>().type("reflected_type"_hs);
```
For what it's worth, this is likely completely equivalent to:
```cpp
auto factory = entt::meta<my_type>().type(42);
```
Obviously, human-readable identifiers are more convenient to use and highly
recommended.
# 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 create a meta node, the library provides the `meta` function that accepts a
type to reflect as a template parameter:
```cpp
auto factory = entt::meta<my_type>();
```
This isn't enough to _export_ the given type and make it visible though.<br/>
The returned value is a factory object to use to continue building the meta
type. In order to make the type _visible_, users can assign it an identifier:
```cpp
auto factory = entt::meta<my_type>().type("reflected_type"_hs);
```
When working with named types, it isn't even necessary to specify the
identifier. In fact, it isn't allowed and it will trigger a compilation
error.<br/>
Identifiers are important because users can retrieve meta types at runtime by
searching for them by _name_ other than by type. On the other hand, 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_. In this case, it's sufficient not
to invoke `type` and the type will not be searchable _by name_.
A factory is such that all its member functions returns the factory itself or
a decorated version of it. This object can be used to 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::meta<my_type>().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::meta<my_type>().dtor<&destroy>();
```
A function should neither delete nor explicitly invoke the destructor of a
given instance.
* _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::meta<my_type>()
.data<&my_type::static_variable>("static"_hs)
.data<&my_type::data_member>("member"_hs)
.data<&global_variable>("global"_hs);
```
This function requires as an argument the identifier 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::meta<my_type>()
.func<&my_type::static_function>("static"_hs)
.func<&my_type::member_function>("member"_hs)
.func<&free_function>("free"_hs);
```
This function requires as an argument the identifier 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::meta<derived_type>().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::meta<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.
The class `meta_any` offers 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 through the reflection system, allocations are at zero.
Creating instances of `meta_any`, whether empty or from existing objects, is
trivial:
```cpp
// a container for an int
entt::meta_any any{0};
// an empty container
entt::meta_any empty{};
```
The `meta_any` class takes also the burden of destroying the contained object
when required.<br/>
Furthermore, an instance of `meta_any` is not tied to a specific type.
Therefore, the wrapper will be reconfigured by assigning it an object of a
different type than the one contained, so as to be able to handle the new
instance.
A particularly interesting feature of this class is that it can also be used as
an opaque container for unmanaged objects:
```cpp
int value;
entt::meta_any any{std::ref(value)};
```
In other words, whenever `meta_any` intercepts a `reference_wrapper`, it acts as
a reference to the original instance rather than making a copy of it. The
contained object is never destroyed and users must ensure that its lifetime
exceeds that of the container.
The `meta_any` class has also a `type` member function that returns the meta
type of the contained value, if any. The member functions `try_cast`, `cast` and
`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.
## 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"_hs);
```
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 kind of
objects offer an API to know their _runtime identifiers_, 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"_hs);
```
The returned type is `meta_data` and may be invalid if there is no meta data
object associated with the given identifier.<br/>
A meta data object offers an API to query the underlying type (for example, 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"_hs);
```
The returned type is `meta_func` and may be invalid if there is no meta
function object associated with the given identifier.<br/>
A meta function object offers an API to query the underlying type (for
example, 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 a `meta_any` object.
* _Meta bases_. They are accessed through the _name_ of the base types:
```cpp
auto base = entt::resolve<derived_type>().base("base"_hs);
```
The returned type is `meta_base` and may be invalid if there is no meta base
object associated with the given identifier.<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
if(auto func = entt::resolve<my_type>().func("member"_hs); 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.<br/>
Be aware that the result of a call to `destroy` may not be what is expected. The
purpose is to give users the ability to free up resources that require special
treatment and **not** to actually destroy instances.
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 inline documentation to get the best out of this powerful tool.
## Policies: the more, the less
Policies are a kind of compile-time directives that can be used when recording
reflection information.<br/>
Their purpose is to require slightly different behavior than the default in some
specific cases. For example, when reading a given data member, its value is
returned wrapped in a `meta_any` object which, by default, makes a copy of it.
For large objects or if the caller wants to access the original instance, this
behavior isn't desirable. Policies are there to offer a solution to this and
other problems.
There are a few alternatives available at the moment:
* The _as-is_ policy, associated with the type `entt::as_is_t`.<br/>
This is the default policy. In general, it should never be used explicitly,
since it's implicitly selected if no other policy is specified.<br/>
In this case, the return values of the functions as well as the properties
exposed as data members are always returned by copy in a dedicated wrapper and
therefore associated with their original meta types.
* The _as-void_ policy, associated with the type `entt::as_void_t`.<br/>
Its purpose is to discard the return value of a meta object, whatever it is,
thus making it appear as if its type were `void`.<br/>
If the use with functions is obvious, it must be said that it's also possible
to use this policy with constructors and data members. In the first case, the
constructor will be invoked but the returned wrapper will actually be empty.
In the second case, instead, the property will not be accessible for reading.
As an example of use:
```cpp
entt::meta<my_type>().func<&my_type::member_function, entt::as_void_t>("member"_hs);
```
* The _as-alias_ policy, associated with the type `entt::as_alias_t`.<br/>
It allows to build wrappers that act as aliases for the objects used to
initialize them. Modifying the object contained in the wrapper for which the
_aliasing_ was requested will make it possible to directly modify the instance
used to initialize the wrapper itself.<br/>
This policy works with constructors (for example, when objects are taken from
an external container rather than created on demand), data members and
functions in general (as long as their return types are lvalue references).
As an example of use:
```cpp
entt::meta<my_type>().data<&my_type::data_member, entt::as_alias_t>("member"_hs);
```
Some uses are rather trivial, but it's useful to note that there are some less
obvious corner cases that can in turn be solved with the use of policies.
## 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::meta<my_enum>()
.data<my_enum::a_value>("a_value"_hs)
.data<my_enum::another_value>("another_value"_hs);
entt::meta<int>().data<2048>("max_int"_hs);
```
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"_hs).get({}).cast<my_enum>();
auto max = entt::resolve<int>().data("max_int"_hs).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 (for example, when it comes to creating an editor) it might be useful
to attach properties to the meta objects created. Fortunately, this is possible
for most of them.<br/>
For the meta objects that support properties, the member functions of the
factory used for registering them will return a decorated version of the factory
itself. The latter can be used to attach properties to the last created meta
object.<br/>
Apparently, it's more difficult to say than to do:
```cpp
entt::meta<my_type>().type("reflected_type"_hs).prop("tooltip"_hs, "message");
```
Properties are always in the key/value form. There are no restrictions on the
type of the key or value, as long as they are copy constructible objects.<br/>
Multiple formats are supported when it comes to defining a property:
* Properties as key/value pairs:
```cpp
entt::meta<my_type>().type("reflected_type"_hs).prop("tooltip"_hs, "message");
```
* Properties as `std::pair`s:
```cpp
entt::meta<my_type>().type("reflected_type"_hs).prop(std::make_pair("tooltip"_hs, "message"));
```
* Key only properties:
```cpp
entt::meta<my_type>().type("reflected_type"_hs).prop(my_enum::key_only);
```
* Properties as `std::tuple`s:
```cpp
entt::meta<my_type>().type("reflected_type"_hs)
.prop(std::make_tuple(std::make_pair("tooltip"_hs, "message"), my_enum::key_only));
```
A tuple contains one or more properties. All of them are treated individually.
* Annotations:
```cpp
entt::meta<my_type>().type("reflected_type"_hs).prop(&property_generator);
```
An annotation is an invocable object that returns one or more properties. All
of them are treated individually.
It's possible to invoke the `prop` function several times if needed, one for
each property to associate with the last meta object created:
```cpp
entt::meta<my_type>()
.type("reflected_type"_hs)
.prop(entt::hashed_string{"Name"}, "Reflected Type")
.data<&my_type::data_member>("member"_hs)
.prop(std::make_pair("tooltip"_hs, "Member"))
.prop(my_enum::a_value, 42);
```
Alternatively, the `props` function is available to associate several properties
at a time. However, in this case properties in the key/value form aren't
allowed, since they would be interpreted as two different properties rather than
a single one.
The meta objects for which properties are supported are currently the meta
types, meta constructors, meta data and meta functions. It's not possible to
attach properties to other types of meta objects and the factory returned as a
result of their construction won't allow such an operation.
These types offer a couple of member functions named `prop` to iterate all
properties at once or to search a specific property by key:
```cpp
// iterate all 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.<br/>
Roughly speaking, unregistering a type means disconnecting all associated meta
objects from it and making its identifier no longer visible. The underlying node
will remain available though, as if it were implicitly generated:
```cpp
entt::meta<my_type>().reset();
```
The type can be re-registered later with a completely different name and form.

208
docs/md/process.md Normal file
View File

@@ -0,0 +1,208 @@
# Crash Course: cooperative scheduler
<!--
@cond TURN_OFF_DOXYGEN
-->
# Table of Contents
* [Introduction](#introduction)
* [The process](#the-process)
* [Adaptor](#adaptor)
* [The scheduler](#the-scheduler)
<!--
@endcond TURN_OFF_DOXYGEN
-->
# Introduction
Sometimes processes are a useful tool to work around the strict definition of a
system and introduce logic in a different way, usually without resorting to the
introduction of other components.
`EnTT` offers a minimal support to this paradigm by introducing a few classes
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
to the CRTP idiom. Moreover, derived classes must specify what's the intended
type for elapsed times.
A process should expose publicly the following member functions whether
required (note that it isn't required to define a function unless the derived
class wants to _override_ the default behavior):
* `void update(Delta, void *);`
It's invoked once per tick until a process is explicitly aborted or it
terminates either with or without errors. Even though it's not mandatory to
declare this member function, as a rule of thumb each process should at
least define it to work properly. The `void *` parameter is an opaque pointer
to user data (if any) forwarded directly to the process during an update.
* `void init();`
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();`
It's invoked in case of success, immediately after an update and during the
same tick.
* `void failed();`
It's invoked in case of errors, immediately after an update and during the
same tick.
* `void aborted();`
It's invoked only if a process is explicitly aborted. There is no guarantee
that it executes in the same tick, this depends solely on whether the
process is aborted immediately or not.
Derived classes can also change the internal state of a process by invoking
`succeed` and `fail`, as well as `pause` and `unpause` the process itself. All
these are protected member functions made available to be able to manage the
life cycle of a process from a derived class.
Here is a minimal example for the sake of curiosity:
```cpp
struct my_process: entt::process<my_process, std::uint32_t> {
using delta_type = std::uint32_t;
void update(delta_type delta, void *) {
remaining -= std::min(remaining, delta);
// ...
if(!remaining) {
succeed();
}
}
private:
delta_type remaining{1000u};
};
```
## Adaptor
Lambdas and functors can't be used directly with a scheduler for they are not
properly defined processes with managed life cycles.<br/>
This class helps in filling the gap and turning lambdas and functors into
full featured processes usable by a scheduler.
The function call operator has a signature similar to the one of the `update`
function of a process but for the fact that it receives two extra arguments to
call whenever a process is terminated with success or with an error:
```cpp
void(Delta delta, void *data, auto succeed, auto fail);
```
Parameters have the following meaning:
* `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.
Both `succeed` and `fail` accept no parameters at all.
Note that usually users shouldn't worry about creating adaptors at all. A
scheduler creates them internally each and every time a lambda or a functor is
used as a process.
# The scheduler
A cooperative scheduler runs different processes and helps managing their life
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 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
entt::scheduler<std::uint32_t> scheduler;
```
It has member functions to query its internal data structures, like `empty` or
`size`, as well as a `clear` utility to reset it to a clean state:
```cpp
// checks if there are processes still running
const auto empty = scheduler.empty();
// gets the number of processes still running
entt::scheduler<std::uint32_t>::size_type size = scheduler.size();
// resets the scheduler to its initial state and discards all the processes
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
indicate its type and submit all the parameters required to construct it to
the `attach` member function:
```cpp
scheduler.attach<my_process>("foobar");
```
* Otherwise, in case of a lambda or a functor, it's enough to provide an
instance of the class to the `attach` member function:
```cpp
scheduler.attach([](auto...){ /* ... */ });
```
In both cases, the return value is an opaque object that offers a `then` member
function to use to create chains of processes to run sequentially.<br/>
As a minimal example of use:
```cpp
// schedules a task in the form of a lambda function
scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
// ...
})
// appends a child in the form of another lambda function
.then([](auto delta, void *, auto succeed, auto fail) {
// ...
})
// appends a child in the form of a process class
.then<my_process>();
```
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
scheduler.update(delta);
// updates all the processes and provides them with custom data
scheduler.update(delta, &data);
```
In addition to these functions, the scheduler offers an `abort` member function
that can be used to discard all the running processes at once:
```cpp
// aborts all the processes abruptly ...
scheduler.abort(true);
// ... or gracefully during the next tick
scheduler.abort();
```

237
docs/md/resource.md Normal file
View File

@@ -0,0 +1,237 @@
# Crash Course: resource management
<!--
@cond TURN_OFF_DOXYGEN
-->
# Table of Contents
* [Introduction](#introduction)
* [The resource, the loader and the cache](#the-resource-the-loader-and-the-cache)
<!--
@endcond TURN_OFF_DOXYGEN
-->
# Introduction
Resource management is usually one of the most critical part of a software like
a game. Solutions are often tuned to the particular application. There exist
several approaches and all of them are perfectly fine as long as they fit the
requirements of the piece of software in which they are used.<br/>
Examples are loading everything on start, loading on request, predictive
loading, and so on.
`EnTT` doesn't pretend to offer a _one-fits-all_ solution for the different
cases. Instead, it offers a minimal and perhaps trivial cache that can be useful
most of the time during prototyping and sometimes even in a production
environment.<br/>
For those interested in the subject, the plan is to improve it considerably over
time in terms of performance, memory usage and functionalities. Hoping to make
it, of course, one step at a time.
# The resource, the loader and the cache
There are three main actors in the model: the resource, the loader and the
cache.
The _resource_ is whatever users want it to be. An image, a video, an audio,
whatever. There are no limits.<br/>
As a minimal example:
```cpp
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 my_loader final: entt::loader<my_loader, my_resource> {
// ...
};
```
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 my_loader: entt::loader<my_loader, my_resource> {
std::shared_ptr<my_resource> load(int value) const {
// ...
return std::shared_ptr<my_resource>(new my_resource{ value });
}
};
```
In general, resource loaders should not have a state or retain data of any type.
They should let the cache manage their resources instead.<br/>
As a side note, base class and CRTP idiom aren't strictly required with the
current implementation. One could argue that a cache can easily work with
loaders of any type. However, future changes won't be breaking ones by forcing
the use of a base class today and that's why the model is already in its place.
Finally, a cache is a specialization of a class template tailored to a specific
resource:
```cpp
using my_cache = entt::cache<my_resource>;
// ...
my_cache cache{};
```
The idea is to create different caches for different types of resources and to
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.
A cache offers a set of basic functionalities to query its internal state and to
_organize_ it:
```cpp
// gets the number of resources managed by a cache
const auto size = cache.size();
// checks if a cache contains at least a valid resource
const auto empty = cache.empty();
// clears a cache and discards its content
cache.clear();
```
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::cache<resource>::id_type
```
Where `id_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::cache<resource>::id_type{"my/resource/identifier"_hs};
// this is equivalent to the following
constexpr auto hs = entt::hashed_string{"my/resource/identifier"};
```
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
function. It accepts the loader to use as a template parameter, the resource
identifier and the parameters used to construct the resource as arguments:
```cpp
// uses the identifier declared above
cache.load<my_loader>(identifier, 0);
// uses a const char * directly as an identifier
cache.load<my_loader>("another/identifier"_hs, 42);
```
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(auto handle = cache.load<my_loader>("another/identifier"_hs, 42); handle) {
// ...
}
```
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"_hs);
```
There exists also a member function to use to force a reload of an already
existing resource if needed:
```cpp
auto handle = cache.reload<my_loader>("another/identifier"_hs, 42);
```
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<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, `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 later on:
```cpp
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 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:
```cpp
const auto &resource = handle.get();
```
* Using the proper cast operator:
```cpp
const auto &resource = handle;
```
* Through the dereference operator:
```cpp
const auto &resource = *handle;
```
The resource can also be accessed directly using the arrow operator if required:
```cpp
auto value = handle->value;
```
To test if a handle is still valid, the cast operator to `bool` allows users to
use it in a guard:
```cpp
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 that of `load`, a (possibly invalid) handle for
the resource is returned also in this case:
```cpp
if(auto handle = cache.temp<my_loader>(42); handle) {
// ...
}
```
Do not forget to test the handle for validity. Otherwise, getting a reference to
the resource it points may result in undefined behavior.

488
docs/md/signal.md Normal file
View File

@@ -0,0 +1,488 @@
# Crash Course: events, signals and everything in between
<!--
@cond TURN_OFF_DOXYGEN
-->
# Table of Contents
* [Introduction](#introduction)
* [Delegate](#delegate)
* [Signals](#signals)
* [Event dispatcher](#event-dispatcher)
* [Event emitter](#event-emitter)
<!--
@endcond TURN_OFF_DOXYGEN
-->
# Introduction
Signals are usually a core part of games and software architectures in
general.<br/>
Roughly speaking, they help to decouple the various parts of a system while
allowing them to communicate with each other somehow.
The so called _modern C++_ comes with a tool that can be useful in these terms,
the `std::function`. As an example, it can be used to create delegates.<br/>
However, there is no guarantee that an `std::function` does not perform
allocations under the hood and this could be problematic sometimes. Furthermore,
it solves a problem but may not adapt well to other requirements that may arise
from time to time.
In case that the flexibility and power of an `std::function` isn't required or
if the price to pay for them is too high,` EnTT` offers a complete set of
lightweight classes to solve the same and many other 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. That said, it's most
likely even a better fit than an `std::function` in a lot of cases, so expect to
use it quite a lot anyway.
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 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.
Free 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 reference to `c` and `42`. However, the
function type of the delegate is still `void(int)`. This is also the signature
of its function call operator.
Another interesting aspect of the delegate class is that it accepts also
functions with a list of parameters that is shorter than that of the function
type used to specialize the delegate itself.<br/>
The following code is therefore perfectly valid:
```cpp
void g() { /* ... */ }
delegate.connect<&g>();
delegate(42);
```
Where the function type of the delegate is `void(int)` as above. It goes without
saying that the extra arguments are silently discarded internally.<br/>v
This is a nice-to-have feature in a lot of cases, as an example when the
`delegate` class is used as a building block of a signal-slot system.
To create and initialize a delegate at once, there are a few 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
already shown in the examples above:
```cpp
auto ret = delegate(42);
```
In all cases, the listeners don't 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.
# Signals
Signal handlers work with references to classes, function pointers and pointers
to members. Listeners can be any kind of objects and users are in charge of
connecting and disconnecting them from a signal to avoid crashes due to
different lifetimes. On the other side, performance shouldn't be affected that
much by the presence of such a signal handler.<br/>
Signals make use of delegates internally and therefore they undergo the same
rules and offer similar functionalities. It may be a good idea to consult the
documentation of the `delegate` class for further information.
A signal handler can be used as a private data member without exposing any
_publish_ functionality to the clients of a class. The basic idea is to impose a
clear separation between the signal itself and the `sink` class, that is a tool
to be used to connect and disconnect listeners on the fly.
The API of a signal handler is straightforward. If a collector is supplied to
the signal when something is published, all the values returned by the listeners
can be literally _collected_ and used later by the caller. Otherwise, the class
works just like a plain signal that emits events from time to time.<br/>
To create instances of signal handlers it is sufficient to provide the type of
function to which they refer:
```cpp
entt::sigh<void(int, char)> signal;
```
Signals offer all the basic functionalities required to know how many listeners
they contain (`size`) or if they contain at least a listener (`empty`), as well
as a function to use to swap handlers (`swap`).
Besides them, there are member functions to use both to connect and disconnect
listeners in all their forms by means of a sink:
```cpp
void foo(int, char) { /* ... */ }
struct listener {
void bar(const int &, char) { /* ... */ }
};
// ...
entt::sink sink{signal};
listener instance;
sink.connect<&foo>();
sink.connect<&listener::bar>(instance);
// ...
// disconnects a free function
sink.disconnect<&foo>();
// disconnect a member function of an instance
sink.disconnect<&listener::bar>(instance);
// disconnect all the member functions of an instance, if any
sink.disconnect(instance);
// discards all the listeners at once
sink.disconnect();
```
As shown above, the listeners don't 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 return type, everything works just
fine.<br/>
It's also possible to connect a listener before other listeners already
contained by the signal. The `before` function returns a `sink` object correctly
initialized for the purpose that can be used to connect one or more listeners in
order and in the desired position:
```cpp
sink.before<&foo>().connect<&listener::bar>(instance);
```
In all cases, the `connect` member function returns by default a `connection`
object to be used as an alternative to break a connection by means of its
`release` member function. A `scoped_connection` can also be created from a
connection. In this case, the link is broken automatically as soon as the object
goes out of scope.
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:
```cpp
signal.publish(42, 'c');
```
To collect data, the `collect` member function should be used instead. Below is
a minimal example to show how to use it:
```cpp
int f() { return 0; }
int g() { return 1; }
// ...
entt::sigh<int()> signal;
entt::sink sink{signal};
sink.connect<&f>();
sink.connect<&g>();
std::vector<int> vec{};
signal.collect([&vec](int value) { vec.push_back(value); });
assert(vec[0] == 0);
assert(vec[1] == 1);
```
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 can
optionally return a boolean value that is true to stop collecting data, false
otherwise. This way one can avoid calling all the listeners in case it isn't
necessary.<br/>
Functors can also be used in place of a lambda. Since the collector is copied
when invoking the `collect` member function, `std::ref` is the way to go in this
case:
```cpp
struct my_collector {
std::vector<int> vec{};
bool operator()(int v) noexcept {
vec.push_back(v);
return true;
}
};
// ...
my_collector collector;
signal.collect(std::ref(collector));
```
# Event dispatcher
The event dispatcher class is designed so as to be used in a loop. It allows
users both to trigger immediate events or to queue events to be published all
together once per tick.<br/>
This class shares part of its API with the one of the signal handler, but it
doesn't require that all the types of events are specified when declared:
```cpp
// define a general purpose 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 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 an_event { int value; };
struct another_event {};
struct listener {
void receive(const an_event &) { /* ... */ }
void method(const another_event &) { /* ... */ }
};
// ...
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
remove one listener at a time or all of them at once:
```cpp
dispatcher.sink<an_event>().disconnect<&listener::receive>(listener);
dispatcher.sink<another_event>().disconnect(listener);
```
The `trigger` member function serves the purpose of sending an immediate event
to all the listeners registered so far. It offers a convenient approach that
relieves users from having to create the event itself. Instead, it's enough to
specify the type of event and provide all the parameters required to construct
it.<br/>
As an example:
```cpp
dispatcher.trigger<an_event>(42);
dispatcher.trigger<another_event>();
```
Listeners are invoked immediately, order of execution isn't guaranteed. This
method can be used to push around urgent messages like an _is terminating_
notification on a mobile app.
On the other hand, the `enqueue` member function queues messages together and
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<an_event>(42);
dispatcher.enqueue<another_event>();
```
Events are stored aside until the `update` member function is invoked, then all
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<my_event>();
// emits all the events queued so far at once
dispatcher.update();
```
This way users can embed the dispatcher in a loop and literally dispatch events
once per tick to their systems.
# Event emitter
A general purpose event emitter thought mainly for those cases where it comes to
working with asynchronous stuff.<br/>
Originally designed to fit the requirements of
[`uvw`](https://github.com/skypjack/uvw) (a wrapper for `libuv` written in
modern C++), it was adapted later to be included in this library.
To create a custom emitter type, derived classes must inherit directly from the
base class as:
```cpp
struct my_emitter: emitter<my_emitter> {
// ...
}
```
The full list of accepted types of events isn't required. Handlers are created
internally on the fly and thus each type of event is accepted by default.
Whenever an event is published, an emitter provides the listeners with a
reference to itself along with a const reference to the event. Therefore
listeners have an handy way to work with it without incurring in the need of
capturing a reference to the emitter itself.<br/>
In addition, an opaque object is returned each time a connection is established
between an emitter and a listener, allowing the caller to disconnect them at a
later time.<br/>
The opaque object used to handle connections is both movable and copyable. On
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
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 &, my_emitter &)
```
Where `Event` is the type of event they want to listen.<br/>
There are two ways to attach a listener to an event emitter that differ
slightly from each other:
* To register a long-lived listener, use the `on` member function. It is meant
to register a listener designed to be invoked more than once for the given
event type.<br/>
As an example:
```cpp
auto conn = emitter.on<my_event>([](const my_event &event, my_emitter &emitter) {
// ...
});
```
The connection object can be freely discarded. Otherwise, it can be used later
to disconnect the listener if required.
* To register a short-lived listener, use the `once` member function. It is
meant to register a listener designed to be invoked only once for the given
event type. The listener is automatically disconnected after the first
invocation.<br/>
As an example:
```cpp
auto conn = emitter.once<my_event>([](const my_event &event, my_emitter &emitter) {
// ...
});
```
The connection object can be freely discarded. Otherwise, it can be used later
to disconnect the listener if required.
In both cases, the connection object can be used with the `erase` member
function:
```cpp
emitter.erase(conn);
```
There are also two member functions to use either to disconnect all the
listeners for a given type of event or to clear the emitter:
```cpp
// removes all the listener for the specific event
emitter.clear<my_event>();
// removes all the listeners registered so far
emitter.clear();
```
To send an event to all the listeners that are interested in it, the `publish`
member function offers a convenient approach that relieves users from having to
create the event:
```cpp
struct my_event { int i; };
// ...
emitter.publish<my_event>(42);
```
Finally, the `empty` member function tests if there exists at least either a
listener registered with the event emitter or to a given type of event:
```cpp
bool empty;
// checks if there is any listener registered for the specific event
empty = emitter.empty<my_event>();
// checks it there are listeners registered with the event emitter
empty = emitter.empty();
```
In general, the event emitter is a handy tool when the derived classes _wrap_
asynchronous operations, because it introduces a _nice-to-have_ model based on
events and listeners that kindly hides the complexity behind the scenes. However
it is not limited to such uses.

299
scripts/amalgamate.py Normal file
View 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
View File

@@ -0,0 +1,8 @@
{
"project": "entt",
"target": "single_include/entt/entt.hpp",
"sources": [
"src/entt/entt.hpp"
],
"include_paths": ["src"]
}

60
scripts/update_homebrew.sh Executable file
View File

@@ -0,0 +1,60 @@
#!/bin/sh
# only argument should be the version to upgrade to
if [ $# != 1 ]
then
echo "Expected a version tag like v2.7.1"
exit 1
fi
VERSION="$1"
URL="https://github.com/skypjack/entt/archive/$VERSION.tar.gz"
FORMULA="entt.rb"
echo "Updating homebrew package to $VERSION"
echo "Cloning..."
git clone https://github.com/skypjack/homebrew-entt.git
if [ $? != 0 ]
then
exit 1
fi
cd homebrew-entt
# download the repo at the version
# exit with error messages if curl fails
echo "Curling..."
curl "$URL" --location --fail --silent --show-error --output archive.tar.gz
if [ $? != 0 ]
then
exit 1
fi
# compute sha256 hash
echo "Hashing..."
HASH="$(openssl sha256 archive.tar.gz | cut -d " " -f 2)"
# delete the archive
rm archive.tar.gz
echo "Sedding..."
# change the url in the formula file
# the slashes in the URL must be escaped
ESCAPED_URL="$(echo "$URL" | sed -e 's/[\/&]/\\&/g')"
sed -i -e '/url/s/".*"/"'$ESCAPED_URL'"/' $FORMULA
# change the hash in the formula file
sed -i -e '/sha256/s/".*"/"'$HASH'"/' $FORMULA
# delete temporary file created by sed
rm -rf "$FORMULA-e"
# update remote repo
echo "Gitting..."
git add entt.rb
git commit -m "Update to $VERSION"
git push origin master
# out of homebrew-entt dir
cd ..

17800
single_include/entt/entt.hpp Normal file

File diff suppressed because it is too large Load Diff

56
src/entt/config/config.h Normal file
View File

@@ -0,0 +1,56 @@
#ifndef ENTT_CONFIG_CONFIG_H
#define ENTT_CONFIG_CONFIG_H
#ifndef ENTT_NOEXCEPT
#define ENTT_NOEXCEPT noexcept
#endif // ENTT_NOEXCEPT
#ifndef ENTT_HS_SUFFIX
#define ENTT_HS_SUFFIX _hs
#endif // ENTT_HS_SUFFIX
#ifndef ENTT_HWS_SUFFIX
#define ENTT_HWS_SUFFIX _hws
#endif // ENTT_HWS_SUFFIX
#ifndef ENTT_NO_ATOMIC
#include <atomic>
#define ENTT_MAYBE_ATOMIC(Type) std::atomic<Type>
#else // ENTT_NO_ATOMIC
#define ENTT_MAYBE_ATOMIC(Type) Type
#endif // ENTT_NO_ATOMIC
#ifndef ENTT_DISABLE_ETO
#include <type_traits>
#define ENTT_ENABLE_ETO(Type) std::is_empty_v<Type>
#else // ENTT_DISABLE_ETO
// sfinae-friendly definition
#define ENTT_ENABLE_ETO(Type) (false && std::is_empty_v<Type>)
#endif // ENTT_DISABLE_ETO
#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 // ENTT_PAGE_SIZE
#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
View File

@@ -0,0 +1,11 @@
#ifndef ENTT_CONFIG_VERSION_H
#define ENTT_CONFIG_VERSION_H
#define ENTT_VERSION "3.2.1"
#define ENTT_VERSION_MAJOR 3
#define ENTT_VERSION_MINOR 2
#define ENTT_VERSION_PATCH 1
#endif // ENTT_CONFIG_VERSION_H

143
src/entt/core/algorithm.hpp Normal file
View File

@@ -0,0 +1,143 @@
#ifndef ENTT_CORE_ALGORITHM_HPP
#define ENTT_CORE_ALGORITHM_HPP
#include <utility>
#include <iterator>
#include <algorithm>
#include <functional>
#include "utility.hpp"
namespace entt {
/**
* @brief Function object to wrap `std::sort` in a class type.
*
* Unfortunately, `std::sort` cannot be passed as template argument to a class
* template or a function template.<br/>
* This class fills the gap by wrapping some flavors of `std::sort` in a
* function object.
*/
struct std_sort {
/**
* @brief Sorts the elements in a range.
*
* Sorts the elements in a range using the given binary comparison function.
*
* @tparam It Type of random access iterator.
* @tparam Compare Type of comparison function object.
* @tparam Args Types of arguments to forward to the sort function.
* @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.
* @param args Arguments to forward to the sort function, if any.
*/
template<typename It, typename Compare = std::less<>, typename... Args>
void operator()(It first, It last, Compare compare = Compare{}, Args &&... args) const {
std::sort(std::forward<Args>(args)..., std::move(first), std::move(last), std::move(compare));
}
};
/*! @brief Function object for performing insertion sort. */
struct insertion_sort {
/**
* @brief Sorts the elements in a range.
*
* Sorts the elements in a range using the given binary comparison 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) {
for(auto it = first+1; it < last; ++it) {
auto value = std::move(*it);
auto pre = it;
for(; pre > first && compare(value, *(pre-1)); --pre) {
*pre = std::move(*(pre-1));
}
*pre = std::move(value);
}
}
}
};
/**
* @brief Function object for performing LSD radix sort.
* @tparam Bit Number of bits processed per pass.
* @tparam N Maximum number of bits to sort.
*/
template<std::size_t Bit, std::size_t N>
struct radix_sort {
static_assert((N % Bit) == 0);
/**
* @brief Sorts the elements in a range.
*
* Sorts the elements in a range using the given _getter_ to access the
* actual data to be sorted.
*
* This implementation is inspired by the online book
* [Physically Based Rendering](http://www.pbr-book.org/3ed-2018/Primitives_and_Intersection_Acceleration/Bounding_Volume_Hierarchies.html#RadixSort).
*
* @tparam It Type of random access iterator.
* @tparam Getter Type of _getter_ 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 getter A valid _getter_ function object.
*/
template<typename It, typename Getter = identity>
void operator()(It first, It last, Getter getter = Getter{}) const {
if(first < last) {
static constexpr auto mask = (1 << Bit) - 1;
static constexpr auto buckets = 1 << Bit;
static constexpr auto passes = N / Bit;
using value_type = typename std::iterator_traits<It>::value_type;
std::vector<value_type> aux(std::distance(first, last));
auto part = [getter = std::move(getter)](auto from, auto to, auto out, auto start) {
std::size_t index[buckets]{};
std::size_t count[buckets]{};
std::for_each(from, to, [&getter, &count, start](const value_type &item) {
++count[(getter(item) >> start) & mask];
});
std::for_each(std::next(std::begin(index)), std::end(index), [index = std::begin(index), count = std::begin(count)](auto &item) mutable {
item = *(index++) + *(count++);
});
std::for_each(from, to, [&getter, &out, &index, start](value_type &item) {
out[index[(getter(item) >> start) & mask]++] = std::move(item);
});
};
for(std::size_t pass = 0; pass < (passes & ~1); pass += 2) {
part(first, last, aux.begin(), pass * Bit);
part(aux.begin(), aux.end(), first, (pass + 1) * Bit);
}
if constexpr(passes & 1) {
part(first, last, aux.begin(), (passes - 1) * Bit);
std::move(aux.begin(), aux.end(), first);
}
}
}
};
}
#endif // ENTT_CORE_ALGORITHM_HPP

View File

@@ -2,8 +2,8 @@
#define ENTT_CORE_FAMILY_HPP
#include<type_traits>
#include<cstddef>
#include <type_traits>
#include "../config/config.h"
namespace entt {
@@ -17,30 +17,17 @@ namespace entt {
* identifiers.
*/
template<typename...>
class Family {
static std::size_t identifier() noexcept {
static std::size_t value = 0;
return value++;
}
template<typename...>
static std::size_t family() noexcept {
static const std::size_t value = identifier();
return value;
}
class family {
inline static ENTT_MAYBE_ATOMIC(ENTT_ID_TYPE) 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>
static family_type type() noexcept {
return family<std::decay_t<Type>...>();
}
// at the time I'm writing, clang crashes during compilation if auto is used instead of family_type
inline static const family_type type = identifier++;
};

View File

@@ -2,107 +2,251 @@
#define ENTT_CORE_HASHED_STRING_HPP
#include <cstdint>
#include <cstddef>
#include "../config/config.h"
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
* counterparts at runtime.<br/>
* Because of that, a hashed string can also be used in constant expressions if
* required.
*
* @tparam Char Character type.
*/
class HashedString final {
struct ConstCharWrapper final {
template<typename Char>
class basic_hashed_string {
using traits_type = internal::fnv1a_traits<ENTT_ID_TYPE>;
struct const_wrapper {
// non-explicit constructor on purpose
constexpr ConstCharWrapper(const char *str) noexcept: str{str} {}
const char *str;
constexpr const_wrapper(const Char *curr) ENTT_NOEXCEPT: str{curr} {}
const Char *str;
};
static constexpr std::uint64_t offset = 14695981039346656037u;
static constexpr std::uint64_t prime = 1099511628211u;
// FowlerNollVo hash function v. 1a - the good
static constexpr std::uint64_t helper(std::uint64_t partial, const char *str) noexcept {
return str[0] == 0 ? partial : helper((partial^str[0])*prime, str+1);
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 Character type. */
using value_type = Char;
/*! @brief Unsigned integer type. */
using hash_type = std::uint64_t;
using hash_type = ENTT_ID_TYPE;
/**
* @brief Constructs a hashed string from an array of const chars.
* @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}
* HashedString sh{"my.png"};
* const auto value = basic_hashed_string<char>::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>
constexpr HashedString(const char (&str)[N]) noexcept
: hash{helper(offset, str)}, str{str}
template<std::size_t N>
static constexpr hash_type to_value(const value_type (&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.
*/
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.
*/
static hash_type to_value(const value_type *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 basic_hashed_string() ENTT_NOEXCEPT
: str{nullptr}, hash{}
{}
/**
* @brief Constructs a hashed string from an array of const characters.
*
* 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}
* basic_hashed_string<char> hs{"my.png"};
* @endcode
*
* @tparam N Number of characters of the identifier.
* @param curr Human-readable identifer.
*/
template<std::size_t N>
constexpr basic_hashed_string(const value_type (&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 *`.
*
* string directly from a `const value_type *`.
* @param wrapper Helps achieving the purpose by relying on overloading.
*/
explicit constexpr HashedString(ConstCharWrapper wrapper) noexcept
: hash{helper(offset, wrapper.str)}, str{wrapper.str}
explicit constexpr basic_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 operator const char *() const noexcept { return str; }
constexpr const value_type * data() const ENTT_NOEXCEPT {
return str;
}
/**
* @brief Returns the numeric representation of a hashed string.
* @return The numeric representation of the instance.
*/
constexpr operator hash_type() const noexcept { return hash; }
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 value_type *() const ENTT_NOEXCEPT { return str; }
/*! @copydoc value */
constexpr operator hash_type() const ENTT_NOEXCEPT { return hash; }
/**
* @brief Compares two hashed strings.
* @param other Hashed string with which to compare.
* @return True if the two hashed strings are identical, false otherwise.
*/
constexpr bool operator==(const HashedString &other) const noexcept {
constexpr bool operator==(const basic_hashed_string &other) const ENTT_NOEXCEPT {
return hash == other.hash;
}
private:
const hash_type hash;
const char *str;
const value_type *str;
hash_type hash;
};
/**
* @brief Deduction guide.
*
* It allows to deduce the character type of the hashed string directly from a
* human-readable identifer provided to the constructor.
*
* @tparam Char Character type.
* @tparam N Number of characters of the identifier.
* @param str Human-readable identifer.
*/
template<typename Char, std::size_t N>
basic_hashed_string(const Char (&str)[N]) ENTT_NOEXCEPT
-> basic_hashed_string<Char>;
/**
* @brief Compares two hashed strings.
* @tparam Char Character type.
* @param lhs A valid hashed string.
* @param rhs A valid hashed string.
* @return True if the two hashed strings are identical, false otherwise.
*/
constexpr bool operator!=(const HashedString &lhs, const HashedString &rhs) noexcept {
template<typename Char>
constexpr bool operator!=(const basic_hashed_string<Char> &lhs, const basic_hashed_string<Char> &rhs) ENTT_NOEXCEPT {
return !(lhs == rhs);
}
/*! @brief Aliases for common character types. */
using hashed_string = basic_hashed_string<char>;
/*! @brief Aliases for common character types. */
using hashed_wstring = basic_hashed_string<wchar_t>;
}
/**
* @brief User defined literal for hashed strings.
* @param str The literal without its suffix.
* @return A properly initialized hashed string.
*/
constexpr entt::hashed_string operator"" ENTT_HS_SUFFIX(const char *str, std::size_t) ENTT_NOEXCEPT {
return entt::hashed_string{str};
}
/**
* @brief User defined literal for hashed wstrings.
* @param str The literal without its suffix.
* @return A properly initialized hashed wstring.
*/
constexpr entt::hashed_wstring operator"" ENTT_HWS_SUFFIX(const wchar_t *str, std::size_t) ENTT_NOEXCEPT {
return entt::hashed_wstring{str};
}

View File

@@ -2,72 +2,33 @@
#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 {
namespace {
template<typename... Types>
struct Identifier final: Identifier<Types>... {
using identifier_type = std::size_t;
template<std::size_t... Indexes>
constexpr Identifier(std::index_sequence<Indexes...>)
: Identifier<Types>{std::index_sequence<Indexes>{}}...
{}
template<typename Type>
constexpr std::size_t get() const {
return Identifier<std::decay_t<Type>>::get();
}
};
template<typename Type>
struct Identifier<Type> {
using identifier_type = std::size_t;
template<std::size_t Index>
constexpr Identifier(std::index_sequence<Index>)
: index{Index}
{}
constexpr std::size_t get() const {
return index;
}
private:
const std::size_t index;
};
}
/**
* @brief Types identifers.
* @brief Types identifiers.
*
* Variable template used to generate identifiers at compile-time for the given
* types. Use the `constexpr` `get` member function to know what's the
* identifier associated to the specific type.
* types. Use the `get` member function to know what's the identifier associated
* to the specific type.
*
* @note
* Identifiers are constant expression and can be used in any context where such
* an expression is required. As an example:
* @code{.cpp}
* constexpr auto identifiers = entt::ident<AType, AnotherType>;
* using id = entt::identifier<a_type, another_type>;
*
* switch(aTypeIdentifier) {
* case identifers.get<AType>():
* switch(a_type_identifier) {
* case id::type<a_type>:
* // ...
* break;
* case identifers.get<AnotherType>():
* case id::type<another_type>:
* // ...
* break;
* default:
@@ -75,19 +36,26 @@ private:
* }
* @endcode
*
* @note
* In case of single type list, `get` isn't a member function template:
* @code{.cpp}
* func(std::integral_constant<
* entt::ident<AType>::identifier_type,
* entt::ident<AType>::get()
* >{});
* @endcode
*
* @tparam Types List of types for which to generate identifiers.
*/
template<typename... Types>
constexpr auto ident = Identifier<std::decay_t<Types>...>{std::make_index_sequence<sizeof...(Types)>{}};
class identifier {
using tuple_type = std::tuple<std::decay_t<Types>...>;
template<typename Type, std::size_t... Indexes>
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 = ENTT_ID_TYPE;
/*! @brief Statically generated unique identifier for the given type. */
template<typename Type>
static constexpr identifier_type type = get<std::decay_t<Type>>(std::index_sequence_for<Types...>{});
};
}

View File

@@ -0,0 +1,62 @@
#ifndef ENTT_CORE_MONOSTATE_HPP
#define ENTT_CORE_MONOSTATE_HPP
#include <cassert>
#include "../config/config.h"
namespace entt {
/**
* @brief Minimal implementation of the monostate pattern.
*
* A minimal, yet complete configuration system built on top of the monostate
* pattern. Thread safe by design, it works only with basic types like `int`s or
* `bool`s.<br/>
* Multiple types and therefore more than one value can be associated with a
* single key. Because of this, users must pay attention to use the same type
* both during an assignment and when they try to read back their data.
* Otherwise, they can incur in unexpected results.
*/
template<ENTT_ID_TYPE>
struct monostate {
/**
* @brief Assigns a value of a specific type to a given key.
* @tparam Type Type of the value to assign.
* @param val User data to assign to the given key.
*/
template<typename Type>
void operator=(Type val) const ENTT_NOEXCEPT {
value<Type> = val;
}
/**
* @brief Gets a value of a specific type for a given key.
* @tparam Type Type of the value to get.
* @return Stored value, if any.
*/
template<typename Type>
operator Type() const ENTT_NOEXCEPT {
return value<Type>;
}
private:
template<typename Type>
inline static ENTT_MAYBE_ATOMIC(Type) value{};
};
/**
* @brief Helper variable template.
* @tparam Value Value used to differentiate between different variables.
*/
template<ENTT_ID_TYPE Value>
inline monostate<Value> monostate_v = {};
}
#endif // ENTT_CORE_MONOSTATE_HPP

View File

@@ -0,0 +1,333 @@
#ifndef ENTT_CORE_TYPE_TRAITS_HPP
#define ENTT_CORE_TYPE_TRAITS_HPP
#include <cstddef>
#include <type_traits>
#include "../config/config.h"
#include "../core/hashed_string.hpp"
namespace entt {
/**
* @brief Utility class to disambiguate overloaded functions.
* @tparam N Number of choices available.
*/
template<std::size_t N>
struct choice_t
// Unfortunately, doxygen cannot parse such a construct.
/*! @cond TURN_OFF_DOXYGEN */
: choice_t<N-1>
/*! @endcond TURN_OFF_DOXYGEN */
{};
/*! @copybrief choice_t */
template<>
struct choice_t<0> {};
/**
* @brief Variable template for the choice trick.
* @tparam N Number of choices available.
*/
template<std::size_t N>
constexpr choice_t<N> choice{};
/*! @brief A class to use to push around lists of types, nothing more. */
template<typename...>
struct type_list {};
/*! @brief Primary template isn't defined on purpose. */
template<typename>
struct type_list_size;
/**
* @brief Compile-time number of elements in a type list.
* @tparam Type Types provided by the type list.
*/
template<typename... Type>
struct type_list_size<type_list<Type...>>
: std::integral_constant<std::size_t, sizeof...(Type)>
{};
/**
* @brief Helper variable template.
* @tparam List Type list.
*/
template<class List>
constexpr auto type_list_size_v = type_list_size<List>::value;
/*! @brief Primary template isn't defined on purpose. */
template<typename...>
struct type_list_cat;
/*! @brief Concatenates multiple type lists. */
template<>
struct type_list_cat<> {
/*! @brief A type list composed by the types of all the type lists. */
using type = type_list<>;
};
/**
* @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 Provides the member constant `value` to true if a given type is
* equality comparable, false otherwise.
* @tparam Type Potentially equality comparable type.
*/
template<typename Type, typename = std::void_t<>>
struct is_equality_comparable: std::false_type {};
/*! @copydoc is_equality_comparable */
template<typename Type>
struct is_equality_comparable<Type, std::void_t<decltype(std::declval<Type>() == std::declval<Type>())>>: std::true_type {};
/**
* @brief Helper variable template.
* @tparam Type Potentially equality comparable type.
*/
template<class Type>
constexpr auto is_equality_comparable_v = is_equality_comparable<Type>::value;
/*! @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 Helper variable template.
* @tparam Type Potentially named type.
*/
template<class Type>
constexpr auto named_type_traits_v = named_type_traits<Type>::value;
/**
* @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, typename = std::void_t<>>
struct is_named_type: std::false_type {};
/*! @copydoc is_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.
* @tparam Type Potentially named type.
*/
template<class Type>
constexpr auto is_named_type_v = is_named_type<Type>::value;
/**
* @brief Defines an enum class to use for opaque identifiers and a dedicate
* `to_integer` function to convert the identifiers to their underlying type.
* @param clazz The name to use for the enum class.
* @param type The underlying type for the enum class.
*/
#define ENTT_OPAQUE_TYPE(clazz, type)\
enum class clazz: type {};\
constexpr auto to_integer(const clazz id) ENTT_NOEXCEPT {\
return std::underlying_type_t<clazz>(id);\
}\
static_assert(true)
}
/**
* @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.
*
* The current definition contains a workaround for Clang 6 because it fails to
* deduce correctly the type to use to specialize the class template.<br/>
* With a compiler that fully supports C++17 and works fine with deduction
* guides, the following should be fine instead:
*
* @code{.cpp}
* std::integral_constant<ENTT_ID_TYPE, entt::basic_hashed_string{#type}>
* @endcode
*
* In order to support even sligthly older compilers, I prefer to stick to the
* implementation below.
*
* @param type Type to assign a name to.
*/
#define ENTT_NAMED_TYPE(type)\
template<>\
struct entt::named_type_traits<type>\
: std::integral_constant<ENTT_ID_TYPE, entt::basic_hashed_string<std::remove_cv_t<std::remove_pointer_t<std::decay_t<decltype(#type)>>>>{#type}>\
{\
static_assert(std::is_same_v<std::remove_cv_t<type>, type>);\
static_assert(std::is_object_v<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

104
src/entt/core/utility.hpp Normal file
View File

@@ -0,0 +1,104 @@
#ifndef ENTT_CORE_UTILITY_HPP
#define ENTT_CORE_UTILITY_HPP
#include "../config/config.h"
namespace entt {
/*! @brief Identity function object (waiting for C++20). */
struct identity {
/**
* @brief Returns its argument unchanged.
* @tparam Type Type of the argument.
* @param value The actual argument.
* @return The submitted value as-is.
*/
template<class Type>
constexpr Type && operator()(Type &&value) const ENTT_NOEXCEPT {
return std::forward<Type>(value);
}
};
/**
* @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) ENTT_NOEXCEPT { 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) ENTT_NOEXCEPT { return func; }
/**
* @brief Helper type for visitors.
* @tparam Func Types of function objects.
*/
template<class... Func>
struct overloaded: Func... {
using Func::operator()...;
};
/**
* @brief Deduction guide.
* @tparam Func Types of function objects.
*/
template<class... Type>
overloaded(Type...) -> overloaded<Type...>;
/**
* @brief Basic implementation of a y-combinator.
* @tparam Func Type of a potentially recursive function.
*/
template<class Func>
struct y_combinator {
/**
* @brief Constructs a y-combinator from a given function.
* @param recursive A potentially recursive function.
*/
y_combinator(Func recursive):
func{std::move(recursive)}
{}
/**
* @brief Invokes a y-combinator and therefore its underlying function.
* @tparam Args Types of arguments to use to invoke the underlying function.
* @param args Parameters to use to invoke the underlying function.
* @return Return value of the underlying function, if any.
*/
template <class... Args>
decltype(auto) operator()(Args &&... args) const {
return func(*this, std::forward<Args>(args)...);
}
/*! @copydoc operator()() */
template <class... Args>
decltype(auto) operator()(Args &&... args) {
return func(*this, std::forward<Args>(args)...);
}
private:
Func func;
};
}
#endif // ENTT_CORE_UTILITY_HPP

View File

@@ -2,51 +2,99 @@
#define ENTT_ENTITY_ACTOR_HPP
#include <cassert>
#include <utility>
#include <type_traits>
#include "../config/config.h"
#include "registry.hpp"
#include "entity.hpp"
#include "fwd.hpp"
namespace entt {
/**
* @brief Dedicated to those who aren't confident with entity-component systems.
* @brief Dedicated to those who aren't confident with the
* entity-component-system architecture.
*
* Tiny wrapper around a registry, for all those users that aren't confident
* with entity-component systems and prefer to iterate objects directly.
* with entity-component-system architecture and prefer to iterate objects
* directly.
*
* @tparam Entity A valid entity type (see entt_traits for more details).
* @tparam Delta Type to use to provide elapsed time.
*/
template<typename Entity, typename Delta>
struct Actor {
template<typename Entity>
struct basic_actor {
/*! @brief Type of registry used internally. */
using registry_type = Registry<Entity>;
/*! @brief Type used to provide elapsed time. */
using delta_type = Delta;
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.
*/
Actor(Registry<Entity> &reg)
: reg{reg}, entity{reg.create()}
basic_actor() ENTT_NOEXCEPT
: entt{entt::null}, reg{nullptr}
{}
/*! @brief Default destructor. */
virtual ~Actor() {
reg.destroy(entity);
/**
* @brief Constructs an actor from a given registry.
* @param ref An instance of the registry class.
*/
explicit basic_actor(registry_type &ref)
: entt{ref.create()}, reg{&ref}
{}
/**
* @brief Constructs an actor from a given entity.
* @param entity A valid entity identifier.
* @param ref An instance of the registry class.
*/
explicit basic_actor(entity_type entity, registry_type &ref)
: entt{entity}, reg{&ref}
{
ENTT_ASSERT(ref.valid(entity));
}
/*! @brief Default copy constructor. */
Actor(const Actor &) = default;
/*! @brief Default move constructor. */
Actor(Actor &&) = default;
/*! @brief Default destructor. */
virtual ~basic_actor() {
if(*this) {
reg->destroy(entt);
}
}
/*! @brief Default copy assignment operator. @return This actor. */
Actor & operator=(const Actor &) = default;
/*! @brief Default move assignment operator. @return This actor. */
Actor & operator=(Actor &&) = default;
/**
* @brief Move constructor.
*
* After actor move construction, instances that have been moved from are
* placed in a valid but unspecified state. It's highly discouraged to
* continue using them.
*
* @param other The instance to move from.
*/
basic_actor(basic_actor &&other)
: entt{other.entt}, reg{other.reg}
{
other.entt = null;
}
/**
* @brief Move assignment operator.
*
* After actor move assignment, instances that have been moved from are
* placed in a valid but unspecified state. It's highly discouraged to
* continue using them.
*
* @param other The instance to move from.
* @return This actor.
*/
basic_actor & operator=(basic_actor &&other) {
if(this != &other) {
auto tmp{std::move(other)};
std::swap(reg, tmp.reg);
std::swap(entt, tmp.entt);
}
return *this;
}
/**
* @brief Assigns the given component to an actor.
@@ -63,8 +111,8 @@ struct Actor {
* @return A reference to the newly created component.
*/
template<typename Component, typename... Args>
Component & set(Args&&... args) {
return reg.template accomodate<Component>(entity, std::forward<Args>(args)...);
decltype(auto) assign(Args &&... args) {
return reg->template assign_or_replace<Component>(entt, std::forward<Args>(args)...);
}
/**
@@ -72,80 +120,87 @@ struct Actor {
* @tparam Component Type of the component to remove.
*/
template<typename Component>
void unset() {
reg.template remove<Component>(entity);
void remove() {
reg->template remove<Component>(entt);
}
/**
* @brief Checks if an actor has the given component.
* @tparam Component Type of the component for which to perform the check.
* @return True if the actor has the component, false otherwise.
* @brief Checks if an actor has the given components.
* @tparam Component Components for which to perform the check.
* @return True if the actor has all the components, false otherwise.
*/
template<typename Component>
bool has() const noexcept {
return reg.template has<Component>(entity);
template<typename... Component>
bool has() const ENTT_NOEXCEPT {
return (reg->template has<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 entity.
* @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 Component>
const Component & get() const noexcept {
return reg.template get<Component>(entity);
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 component for an actor.
* @tparam Component Type of the component to get.
* @return A reference to the instance of the component owned by the entity.
* @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 Component>
Component & get() noexcept {
return const_cast<Component &>(const_cast<const Actor *>(this)->get<Component>());
template<typename... Component>
auto try_get() const ENTT_NOEXCEPT {
return std::as_const(*reg).template try_get<Component...>(entt);
}
/*! @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
* @return A reference to the underlying registry.
*/
const registry_type & registry() const noexcept {
return reg;
const registry_type & backend() const ENTT_NOEXCEPT {
return *reg;
}
/*! @copydoc backend */
registry_type & backend() ENTT_NOEXCEPT {
return const_cast<registry_type &>(std::as_const(*this).backend());
}
/**
* @brief Returns a reference to the underlying registry.
* @return A reference to the underlying registry
* @brief Returns the entity associated with an actor.
* @return The entity associated with the actor.
*/
registry_type & registry() noexcept {
return const_cast<registry_type &>(const_cast<const Actor *>(this)->registry());
entity_type entity() const ENTT_NOEXCEPT {
return entt;
}
/**
* @brief Updates an actor, whatever it means to update it.
* @param delta Elapsed time.
* @brief Checks if an actor refers to a valid entity or not.
* @return True if the actor refers to a valid entity, false otherwise.
*/
virtual void update(delta_type delta) = 0;
explicit operator bool() const ENTT_NOEXCEPT {
return reg && reg->valid(entt);
}
private:
registry_type &reg;
Entity entity;
entity_type entt;
registry_type *reg;
};
/**
* @brief Default actor class.
*
* The default actor is the best choice for almost all the applications.<br/>
* Users should have a really good reason to choose something different.
*
* @tparam Delta Type to use to provide elapsed time.
*/
template<typename Delta>
using DefaultActor = Actor<std::uint32_t, Delta>;
}

174
src/entt/entity/entity.hpp Normal file
View File

@@ -0,0 +1,174 @@
#ifndef ENTT_ENTITY_ENTITY_HPP
#define ENTT_ENTITY_ENTITY_HPP
#include <cstdint>
#include <type_traits>
#include "../config/config.h"
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.
*/
namespace internal {
class null {
template<typename Entity>
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
public:
template<typename Entity>
constexpr operator Entity() const ENTT_NOEXCEPT {
return Entity{traits_type<Entity>::entity_mask};
}
constexpr bool operator==(null) const ENTT_NOEXCEPT {
return true;
}
constexpr bool operator!=(null) const ENTT_NOEXCEPT {
return false;
}
template<typename Entity>
constexpr bool operator==(const Entity entity) const ENTT_NOEXCEPT {
return (to_integer(entity) & traits_type<Entity>::entity_mask) == to_integer(static_cast<Entity>(*this));
}
template<typename Entity>
constexpr bool operator!=(const Entity entity) const ENTT_NOEXCEPT {
return !(entity == *this);
}
};
template<typename Entity>
constexpr bool operator==(const Entity entity, null other) ENTT_NOEXCEPT {
return other == entity;
}
template<typename Entity>
constexpr bool operator!=(const Entity entity, null other) ENTT_NOEXCEPT {
return other != entity;
}
}
/**
* Internal details not to be documented.
* @endcond TURN_OFF_DOXYGEN
*/
/**
* @brief Compile-time constant for null entities.
*
* There exist implicit conversions from this variable to entity identifiers of
* any allowed type. Similarly, there exist comparision operators between the
* null entity and any other entity identifier.
*/
constexpr auto null = internal::null{};
}
#endif // ENTT_ENTITY_ENTITY_HPP

92
src/entt/entity/fwd.hpp Normal file
View File

@@ -0,0 +1,92 @@
#ifndef ENTT_ENTITY_FWD_HPP
#define ENTT_ENTITY_FWD_HPP
#include "../config/config.h"
#include "../core/type_traits.hpp"
namespace entt {
/*! @class basic_registry */
template <typename>
class basic_registry;
/*! @class basic_view */
template<typename...>
class basic_view;
/*! @class basic_runtime_view */
template<typename>
class basic_runtime_view;
/*! @class basic_group */
template<typename...>
class basic_group;
/*! @class basic_observer */
template<typename>
class basic_observer;
/*! @struct basic_actor */
template <typename>
struct basic_actor;
/*! @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. */
ENTT_OPAQUE_TYPE(entity, ENTT_ID_TYPE);
/*! @brief Alias declaration for the most common use case. */
ENTT_OPAQUE_TYPE(component, ENTT_ID_TYPE);
/*! @brief Alias declaration for the most common use case. */
using registry = basic_registry<entity>;
/*! @brief Alias declaration for the most common use case. */
using observer = basic_observer<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 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 Types 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

859
src/entt/entity/group.hpp Normal file
View File

@@ -0,0 +1,859 @@
#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 "utility.hpp"
#include "fwd.hpp"
namespace entt {
/**
* @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).
* * The entity currently pointed is destroyed.
*
* 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 Exclude Types of components used to filter the group.
* @tparam Get Type of components observed by the group.
*/
template<typename Entity, typename... Exclude, typename... Get>
class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>> {
/*! @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<Type> *..., but vs complains about it and refuses to compile for unknown reasons (most likely a bug)
basic_group(sparse_set<Entity> *ref, storage<Entity, std::remove_const_t<Get>> *... gpool) ENTT_NOEXCEPT
: handler{ref},
pools{gpool...}
{}
template<typename Func, typename... Weak>
void traverse(Func func, type_list<Weak...>) const {
for(const auto entt: *handler) {
if constexpr(std::is_invocable_v<Func, decltype(get<Weak>({}))...>) {
func(std::get<pool_type<Weak> *>(pools)->get(entt)...);
} else {
func(entt, std::get<pool_type<Weak> *>(pools)->get(entt)...);
}
}
}
public:
/*! @brief Underlying entity identifier. */
using entity_type = Entity;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @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 group or the pools of the given components are
* empty.
* @tparam Component Types of components in which one is interested.
* @return True if the group or the pools of the given components are empty,
* false otherwise.
*/
template<typename... Component>
bool empty() const ENTT_NOEXCEPT {
if constexpr(sizeof...(Component) == 0) {
return handler->empty();
} else {
return (std::get<pool_type<Component> *>(pools)->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>({}))...>{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>
void each(Func func) const {
traverse(std::move(func), type_list<Get...>{});
}
/**
* @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 non-empty 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, Type &...);
* void(Type &...);
* @endcode
*
* @sa each
*
* @tparam Func Type of the function object to invoke.
* @param func A valid function object.
*/
template<typename Func>
void less(Func func) const {
using get_type_list = type_list_cat_t<std::conditional_t<ENTT_ENABLE_ETO(Get), type_list<>, type_list<Get>>...>;
traverse(std::move(func), get_type_list{});
}
/**
* @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(std::tuple<Component &...>, std::tuple<Component &...>);
* bool(const Component &..., const Component &...);
* bool(const Entity, const Entity);
* @endcode
*
* Where `Component` are 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.
*
* @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) {
if constexpr(sizeof...(Component) == 0) {
static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>);
handler->sort(handler->begin(), handler->end(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
} else if constexpr(sizeof...(Component) == 1) {
handler->sort(handler->begin(), handler->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
return compare((std::get<pool_type<Component> *>(pools)->get(lhs), ...), (std::get<pool_type<Component> *>(pools)->get(rhs), ...));
}, std::move(algo), std::forward<Args>(args)...);
} else {
handler->sort(handler->begin(), handler->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
return compare(std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(lhs)...}, std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(rhs)...});
}, std::move(algo), std::forward<Args>(args)...);
}
}
/**
* @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).
* * The entity currently pointed is destroyed.
*
* 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 Exclude Types of components used to filter the group.
* @tparam Get Types of components observed by the group.
* @tparam Owned Types of components owned by the group.
*/
template<typename Entity, typename... Exclude, typename... Get, typename... Owned>
class basic_group<Entity, exclude_t<Exclude...>, get_t<Get...>, Owned...> {
/*! @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());
// we could use pool_type<Type> *..., but vs complains about it and refuses to compile for unknown reasons (most likely a bug)
basic_group(const std::size_t *ref, const std::size_t *extent, storage<Entity, std::remove_const_t<Owned>> *... opool, storage<Entity, std::remove_const_t<Get>> *... gpool) ENTT_NOEXCEPT
: pools{opool..., gpool...},
length{extent},
super{ref}
{}
template<typename Func, typename... Strong, typename... Weak>
void traverse(Func func, type_list<Strong...>, type_list<Weak...>) const {
[[maybe_unused]] auto it = std::make_tuple((std::get<pool_type<Strong> *>(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<Strong>({}))..., decltype(get<Weak>({}))...>) {
if constexpr(sizeof...(Weak) == 0) {
func(*(std::get<component_iterator_type<Strong>>(it)++)...);
} else {
const auto entt = *(data++);
func(*(std::get<component_iterator_type<Strong>>(it)++)..., std::get<pool_type<Weak> *>(pools)->get(entt)...);
}
} else {
const auto entt = *(data++);
func(entt, *(std::get<component_iterator_type<Strong>>(it)++)..., std::get<pool_type<Weak> *>(pools)->get(entt)...);
}
}
}
public:
/*! @brief Underlying entity identifier. */
using entity_type = Entity;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @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 group or the pools of the given components are
* empty.
* @tparam Component Types of components in which one is interested.
* @return True if the group or the pools of the given components are empty,
* false otherwise.
*/
template<typename... Component>
bool empty() const ENTT_NOEXCEPT {
if constexpr(sizeof...(Component) == 0) {
return !*length;
} else {
return (std::get<pool_type<Component> *>(pools)->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.<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>({}))...>{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>
void each(Func func) const {
traverse(std::move(func), type_list<Owned...>{}, type_list<Get...>{});
}
/**
* @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 non-empty 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, Type &...);
* void(Type &...);
* @endcode
*
* @sa each
*
* @tparam Func Type of the function object to invoke.
* @param func A valid function object.
*/
template<typename Func>
void less(Func func) const {
using owned_type_list = type_list_cat_t<std::conditional_t<ENTT_ENABLE_ETO(Owned), type_list<>, type_list<Owned>>...>;
using get_type_list = type_list_cat_t<std::conditional_t<ENTT_ENABLE_ETO(Get), type_list<>, type_list<Get>>...>;
traverse(std::move(func), owned_type_list{}, get_type_list{});
}
/**
* @brief Checks whether the group can be sorted.
* @return True if the group can be sorted, false otherwise.
*/
bool sortable() const ENTT_NOEXCEPT {
constexpr auto size = sizeof...(Owned) + sizeof...(Get) + sizeof...(Exclude);
return *super == size;
}
/**
* @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(std::tuple<Component &...>, std::tuple<Component &...>);
* 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.
*
* @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) {
ENTT_ASSERT(sortable());
auto *cpool = std::get<0>(pools);
if constexpr(sizeof...(Component) == 0) {
static_assert(std::is_invocable_v<Compare, const entity_type, const entity_type>);
cpool->sort(cpool->end()-*length, cpool->end(), std::move(compare), std::move(algo), std::forward<Args>(args)...);
} else if constexpr(sizeof...(Component) == 1) {
cpool->sort(cpool->end()-*length, cpool->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
return compare((std::get<pool_type<Component> *>(pools)->get(lhs), ...), (std::get<pool_type<Component> *>(pools)->get(rhs), ...));
}, std::move(algo), std::forward<Args>(args)...);
} else {
cpool->sort(cpool->end()-*length, cpool->end(), [this, compare = std::move(compare)](const entity_type lhs, const entity_type rhs) {
return compare(std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(lhs)...}, std::tuple<decltype(get<Component>({}))...>{std::get<pool_type<Component> *>(pools)->get(rhs)...});
}, std::move(algo), std::forward<Args>(args)...);
}
[this](auto *head, auto *... other) {
for(auto next = *length; next; --next) {
const auto pos = next - 1;
[[maybe_unused]] const auto entt = head->data()[pos];
(other->swap(other->data()[pos], entt), ...);
}
}(std::get<pool_type<Owned> *>(pools)...);
}
private:
const std::tuple<pool_type<Owned> *..., pool_type<Get> *...> pools;
const size_type *length;
const size_type *super;
};
}
#endif // ENTT_ENTITY_GROUP_HPP

139
src/entt/entity/helper.hpp Normal file
View File

@@ -0,0 +1,139 @@
#ifndef ENTT_ENTITY_HELPER_HPP
#define ENTT_ENTITY_HELPER_HPP
#include <type_traits>
#include "../config/config.h"
#include "../signal/sigh.hpp"
#include "registry.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 Exclude Types of components used to filter the view.
* @tparam Component Type of components used to construct the view.
* @return A newly created view.
*/
template<typename Exclude, typename... Component>
operator entt::basic_view<Entity, Exclude, Component...>() const {
return reg.template view<Component...>(Exclude{});
}
private:
registry_type &reg;
};
/**
* @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.
* @tparam Exclude Types of components used to filter the group.
* @tparam Get Types of components observed by the group.
* @tparam Owned Types of components owned by the group.
* @return A newly created group.
*/
template<typename Exclude, typename Get, typename... Owned>
operator entt::basic_group<Entity, Exclude, Get, Owned...>() const {
return reg.template group<Owned...>(Get{}, Exclude{});
}
private:
registry_type &reg;
};
/**
* @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 Alias template to ease the assignment of tags to entities.
*
* If used in combination with hashed strings, it simplifies the assignment of
* 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::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<ENTT_ID_TYPE Value>
using tag = std::integral_constant<ENTT_ID_TYPE, Value>;
}
#endif // ENTT_ENTITY_HELPER_HPP

View File

@@ -0,0 +1,442 @@
#ifndef ENTT_ENTITY_OBSERVER_HPP
#define ENTT_ENTITY_OBSERVER_HPP
#include <limits>
#include <cstddef>
#include <cstdint>
#include <utility>
#include <algorithm>
#include <type_traits>
#include "../config/config.h"
#include "../core/type_traits.hpp"
#include "registry.hpp"
#include "storage.hpp"
#include "entity.hpp"
#include "fwd.hpp"
namespace entt {
/*! @brief Grouping matcher. */
template<typename...>
struct matcher {};
/**
* @brief Collector.
*
* Primary template isn't defined on purpose. All the specializations give a
* compile-time error, but for a few reasonable cases.
*/
template<typename...>
struct basic_collector;
/**
* @brief Collector.
*
* A collector contains a set of rules (literally, matchers) to use to track
* entities.<br/>
* Its main purpose is to generate a descriptor that allows an observer to know
* how to connect to a registry.
*/
template<>
struct basic_collector<> {
/**
* @brief Adds a grouping matcher to the collector.
* @tparam AllOf Types of components tracked by the matcher.
* @tparam NoneOf Types of components used to filter out entities.
* @return The updated collector.
*/
template<typename... AllOf, typename... NoneOf>
static constexpr auto group(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
return basic_collector<matcher<type_list<>, type_list<>, type_list<NoneOf...>, AllOf...>>{};
}
/**
* @brief Adds an observing matcher to the collector.
* @tparam AnyOf Type of component for which changes should be detected.
* @return The updated collector.
*/
template<typename AnyOf>
static constexpr auto replace() ENTT_NOEXCEPT {
return basic_collector<matcher<type_list<>, type_list<>, AnyOf>>{};
}
};
/**
* @brief Collector.
* @copydetails basic_collector<>
* @tparam Reject Untracked types used to filter out entities.
* @tparam Require Untracked types required by the matcher.
* @tparam Rule Specific details of the current matcher.
* @tparam Other Other matchers.
*/
template<typename... Reject, typename... Require, typename... Rule, typename... Other>
struct basic_collector<matcher<type_list<Reject...>, type_list<Require...>, Rule...>, Other...> {
/*! @brief Current matcher. */
using current_type = matcher<type_list<Reject...>, type_list<Require...>, Rule...>;
/**
* @brief Adds a grouping matcher to the collector.
* @tparam AllOf Types of components tracked by the matcher.
* @tparam NoneOf Types of components used to filter out entities.
* @return The updated collector.
*/
template<typename... AllOf, typename... NoneOf>
static constexpr auto group(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
return basic_collector<matcher<type_list<>, type_list<>, type_list<NoneOf...>, AllOf...>, current_type, Other...>{};
}
/**
* @brief Adds an observing matcher to the collector.
* @tparam AnyOf Type of component for which changes should be detected.
* @return The updated collector.
*/
template<typename AnyOf>
static constexpr auto replace() ENTT_NOEXCEPT {
return basic_collector<matcher<type_list<>, type_list<>, AnyOf>, current_type, Other...>{};
}
/**
* @brief Updates the filter of the last added matcher.
* @tparam AllOf Types of components required by the matcher.
* @tparam NoneOf Types of components used to filter out entities.
* @return The updated collector.
*/
template<typename... AllOf, typename... NoneOf>
static constexpr auto where(exclude_t<NoneOf...> = {}) ENTT_NOEXCEPT {
using extended_type = matcher<type_list<Reject..., NoneOf...>, type_list<Require..., AllOf...>, Rule...>;
return basic_collector<extended_type, Other...>{};
}
};
/*! @brief Variable template used to ease the definition of collectors. */
constexpr basic_collector<> collector{};
/**
* @brief Observer.
*
* An observer returns all the entities and only the entities that fit the
* requirements of at least one matcher. Moreover, it's guaranteed that the
* entity list is tightly packed in memory for fast iterations.<br/>
* In general, observers don't stay true to the order of any set of components.
*
* Observers work mainly with two types of matchers, provided through a
* collector:
*
* * Observing matcher: an observer will return at least all the living entities
* for which one or more of the given components have been explicitly
* replaced and not yet destroyed.
* * Grouping matcher: an observer will return at least all the living entities
* that would have entered the given group if it existed and that would have
* not yet left it.
*
* If an entity respects the requirements of multiple matchers, it will be
* returned once and only once by the observer in any case.
*
* Matchers support also filtering by means of a _where_ clause that accepts
* both a list of types and an exclusion list.<br/>
* Whenever a matcher finds that an entity matches its requirements, the
* condition of the filter is verified before to register the entity itself.
* Moreover, a registered entity isn't returned by the observer if the condition
* set by the filter is broken in the meantime.
*
* @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).
* * The entity currently pointed is destroyed.
*
* 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.
*
* @warning
* Lifetime of an observer doesn't necessarily have to overcome the one of the
* registry to which it is connected. However, the observer must be disconnected
* from the registry before being destroyed to avoid crashes due to dangling
* pointers.
*
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
template<typename Entity>
class basic_observer {
using payload_type = std::uint32_t;
template<typename>
struct matcher_handler;
template<typename... Reject, typename... Require, typename AnyOf>
struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, AnyOf>> {
template<std::size_t Index>
static void maybe_valid_if(basic_observer &obs, const Entity entt, const basic_registry<Entity> &reg) {
if(reg.template has<Require...>(entt) && !(reg.template has<Reject>(entt) || ...)) {
auto *comp = obs.view.try_get(entt);
(comp ? *comp : obs.view.construct(entt)) |= (1 << Index);
}
}
template<std::size_t Index>
static void discard_if(basic_observer &obs, const Entity entt) {
if(auto *value = obs.view.try_get(entt); value && !(*value &= (~(1 << Index)))) {
obs.view.destroy(entt);
}
}
template<std::size_t Index>
static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
(reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
(reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
reg.template on_replace<AnyOf>().template connect<&maybe_valid_if<Index>>(obs);
reg.template on_destroy<AnyOf>().template connect<&discard_if<Index>>(obs);
}
static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
(reg.template on_destroy<Require>().disconnect(obs), ...);
(reg.template on_construct<Reject>().disconnect(obs), ...);
reg.template on_replace<AnyOf>().disconnect(obs);
reg.template on_destroy<AnyOf>().disconnect(obs);
}
};
template<typename... Reject, typename... Require, typename... NoneOf, typename... AllOf>
struct matcher_handler<matcher<type_list<Reject...>, type_list<Require...>, type_list<NoneOf...>, AllOf...>> {
template<std::size_t Index>
static void maybe_valid_if(basic_observer &obs, const Entity entt, const basic_registry<Entity> &reg) {
if(reg.template has<AllOf...>(entt) && !(reg.template has<NoneOf>(entt) || ...)
&& reg.template has<Require...>(entt) && !(reg.template has<Reject>(entt) || ...))
{
auto *comp = obs.view.try_get(entt);
(comp ? *comp : obs.view.construct(entt)) |= (1 << Index);
}
}
template<std::size_t Index>
static void discard_if(basic_observer &obs, const Entity entt) {
if(auto *value = obs.view.try_get(entt); value && !(*value &= (~(1 << Index)))) {
obs.view.destroy(entt);
}
}
template<std::size_t Index>
static void connect(basic_observer &obs, basic_registry<Entity> &reg) {
(reg.template on_destroy<Require>().template connect<&discard_if<Index>>(obs), ...);
(reg.template on_construct<Reject>().template connect<&discard_if<Index>>(obs), ...);
(reg.template on_construct<AllOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
(reg.template on_destroy<NoneOf>().template connect<&maybe_valid_if<Index>>(obs), ...);
(reg.template on_destroy<AllOf>().template connect<&discard_if<Index>>(obs), ...);
(reg.template on_construct<NoneOf>().template connect<&discard_if<Index>>(obs), ...);
}
static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
(reg.template on_destroy<Require>().disconnect(obs), ...);
(reg.template on_construct<Reject>().disconnect(obs), ...);
(reg.template on_construct<AllOf>().disconnect(obs), ...);
(reg.template on_destroy<NoneOf>().disconnect(obs), ...);
(reg.template on_destroy<AllOf>().disconnect(obs), ...);
(reg.template on_construct<NoneOf>().disconnect(obs), ...);
}
};
template<typename... Matcher>
static void disconnect(basic_observer &obs, basic_registry<Entity> &reg) {
(matcher_handler<Matcher>::disconnect(obs, reg), ...);
}
template<typename... Matcher, std::size_t... Index>
void connect(basic_registry<Entity> &reg, std::index_sequence<Index...>) {
static_assert(sizeof...(Matcher) < std::numeric_limits<payload_type>::digits);
(matcher_handler<Matcher>::template connect<Index>(*this, reg), ...);
release = &basic_observer::disconnect<Matcher...>;
}
public:
/*! @brief Underlying entity identifier. */
using entity_type = Entity;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @brief Input iterator type. */
using iterator_type = typename sparse_set<Entity>::iterator_type;
/*! @brief Default constructor. */
basic_observer() ENTT_NOEXCEPT
: target{}, release{}, view{}
{}
/*! @brief Default copy constructor, deleted on purpose. */
basic_observer(const basic_observer &) = delete;
/*! @brief Default move constructor, deleted on purpose. */
basic_observer(basic_observer &&) = delete;
/**
* @brief Creates an observer and connects it to a given registry.
* @tparam Matcher Types of matchers to use to initialize the observer.
* @param reg A valid reference to a registry.
*/
template<typename... Matcher>
basic_observer(basic_registry<entity_type> &reg, basic_collector<Matcher...>) ENTT_NOEXCEPT
: target{&reg},
release{},
view{}
{
connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
}
/*! @brief Default destructor. */
~basic_observer() = default;
/**
* @brief Default copy assignment operator, deleted on purpose.
* @return This observer.
*/
basic_observer & operator=(const basic_observer &) = delete;
/**
* @brief Default move assignment operator, deleted on purpose.
* @return This observer.
*/
basic_observer & operator=(basic_observer &&) = delete;
/**
* @brief Connects an observer to a given registry.
* @tparam Matcher Types of matchers to use to initialize the observer.
* @param reg A valid reference to a registry.
*/
template<typename... Matcher>
void connect(basic_registry<entity_type> &reg, basic_collector<Matcher...>) {
disconnect();
connect<Matcher...>(reg, std::index_sequence_for<Matcher...>{});
target = &reg;
view.reset();
}
/*! @brief Disconnects an observer from the registry it keeps track of. */
void disconnect() {
if(release) {
release(*this, *target);
release = nullptr;
}
}
/**
* @brief Returns the number of elements in an observer.
* @return Number of elements.
*/
size_type size() const ENTT_NOEXCEPT {
return view.size();
}
/**
* @brief Checks whether an observer is empty.
* @return True if the observer is empty, false otherwise.
*/
bool empty() const ENTT_NOEXCEPT {
return view.empty();
}
/**
* @brief Direct access to the list of entities of the observer.
*
* 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 observer in the expected order.
*
* @return A pointer to the array of entities.
*/
const entity_type * data() const ENTT_NOEXCEPT {
return view.data();
}
/**
* @brief Returns an iterator to the first entity of the observer.
*
* The returned iterator points to the first entity of the observer. If the
* container is empty, the returned iterator will be equal to `end()`.
*
* @return An iterator to the first entity of the observer.
*/
iterator_type begin() const ENTT_NOEXCEPT {
return view.sparse_set<entity_type>::begin();
}
/**
* @brief Returns an iterator that is past the last entity of the observer.
*
* The returned iterator points to the entity following the last entity of
* the observer. Attempting to dereference the returned iterator results in
* undefined behavior.
*
* @return An iterator to the entity following the last entity of the
* observer.
*/
iterator_type end() const ENTT_NOEXCEPT {
return view.sparse_set<entity_type>::end();
}
/*! @brief Resets the underlying container. */
void clear() {
view.reset();
}
/**
* @brief Iterates entities and applies the given function object to them,
* then clears the observer.
*
* The function object is invoked for each entity.<br/>
* The signature of the function must be equivalent to the following form:
*
* @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 {
static_assert(std::is_invocable_v<Func, entity_type>);
std::for_each(begin(), end(), std::move(func));
}
/**
* @brief Iterates entities and applies the given function object to them,
* then clears the observer.
*
* The function object is invoked for each entity.<br/>
* The signature of the function must be equivalent to the following form:
*
* @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) {
std::as_const(*this).each(std::move(func));
clear();
}
private:
basic_registry<entity_type> *target;
void(* release)(basic_observer &, basic_registry<entity_type> &);
storage<entity_type, payload_type> view;
};
}
#endif // ENTT_ENTITY_OBSERVER_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,256 @@
#ifndef ENTT_ENTITY_RUNTIME_VIEW_HPP
#define ENTT_ENTITY_RUNTIME_VIEW_HPP
#include <iterator>
#include <cassert>
#include <vector>
#include <utility>
#include <algorithm>
#include <type_traits>
#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).
* * The entity currently pointed is destroyed.
*
* 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;
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) ENTT_NOEXCEPT
: begin{first},
end{last},
from{others},
to{length}
{
if(begin != end && !valid()) {
++(*this);
}
}
bool valid() const ENTT_NOEXCEPT {
return std::all_of(from, to, [entt = *begin](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;
}
bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this == other);
}
pointer operator->() const ENTT_NOEXCEPT {
return begin.operator->();
}
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;
};
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());
}
bool valid() const ENTT_NOEXCEPT {
return !pools.empty() && pools.front();
}
public:
/*! @brief Underlying entity identifier. */
using entity_type = Entity;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @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() };
}
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 };
}
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->find(entt) != view->end();
});
}
/**
* @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

View File

@@ -0,0 +1,605 @@
#ifndef ENTT_ENTITY_SNAPSHOT_HPP
#define ENTT_ENTITY_SNAPSHOT_HPP
#include <array>
#include <cstddef>
#include <utility>
#include <iterator>
#include <type_traits>
#include <unordered_map>
#include "../config/config.h"
#include "entity.hpp"
#include "fwd.hpp"
namespace entt {
/**
* @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 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 basic_snapshot {
/*! @brief A registry is allowed to create snapshots. */
friend class basic_registry<Entity>;
using follow_fn_type = Entity(const basic_registry<Entity> &, const Entity);
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
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>
void get(Archive &archive, std::size_t sz, It first, It last) const {
archive(typename traits_type::entity_type(sz));
while(first != last) {
const auto entt = *(first++);
if(reg->template has<Component>(entt)) {
if constexpr(std::is_empty_v<Component>) {
archive(entt);
} else {
archive(entt, reg->template get<Component>(entt));
}
}
}
}
template<typename... Component, typename Archive, typename It, std::size_t... Indexes>
void component(Archive &archive, It first, It last, std::index_sequence<Indexes...>) const {
std::array<std::size_t, sizeof...(Indexes)> size{};
auto begin = first;
while(begin != last) {
const auto entt = *(begin++);
((reg->template has<Component>(entt) ? ++size[Indexes] : size[Indexes]), ...);
}
(get<Component>(archive, size[Indexes], first, last), ...);
}
public:
/*! @brief Default move constructor. */
basic_snapshot(basic_snapshot &&) = default;
/*! @brief Default move assignment operator. @return This snapshot. */
basic_snapshot & operator=(basic_snapshot &&) = default;
/**
* @brief Puts aside all the entities that are still in use.
*
* Entities are serialized along with their versions. Destroyed entities are
* not taken in consideration by this function.
*
* @tparam Archive Type of output archive.
* @param archive A valid reference to an output archive.
* @return An object of this type to continue creating the snapshot.
*/
template<typename Archive>
const basic_snapshot & entities(Archive &archive) const {
archive(typename traits_type::entity_type(reg->alive()));
reg->each([&archive](const auto entt) { archive(entt); });
return *this;
}
/**
* @brief Puts aside destroyed entities.
*
* Entities are serialized along with their versions. Entities that are
* still in use are not taken in consideration by this function.
*
* @tparam Archive Type of output archive.
* @param archive A valid reference to an output archive.
* @return An object of this type to continue creating the snapshot.
*/
template<typename Archive>
const basic_snapshot & destroyed(Archive &archive) const {
auto size = reg->size() - reg->alive();
archive(typename traits_type::entity_type(size));
if(size) {
auto curr = seed;
archive(curr);
for(--size; size; --size) {
curr = follow(*reg, curr);
archive(curr);
}
}
return *this;
}
/**
* @brief Puts aside the given components.
*
* Each instance is serialized together with the entity to which it belongs.
* Entities are serialized along with their versions.
*
* @tparam Component Types of components to serialize.
* @tparam Archive Type of output archive.
* @param archive A valid reference to an output archive.
* @return An object of this type to continue creating the snapshot.
*/
template<typename... Component, typename Archive>
const basic_snapshot & component(Archive &archive) const {
(component<Component>(archive, reg->template data<Component>(), reg->template data<Component>() + reg->template size<Component>()), ...);
return *this;
}
/**
* @brief Puts aside the given components for the entities in a range.
*
* Each instance is serialized together with the entity to which it belongs.
* Entities are serialized along with their versions.
*
* @tparam Component Types of components to serialize.
* @tparam Archive Type of output archive.
* @tparam It Type of input iterator.
* @param archive A valid reference to an output archive.
* @param first An iterator to the first element of the range to serialize.
* @param last An iterator past the last element of the range to serialize.
* @return An object of this type to continue creating the snapshot.
*/
template<typename... Component, typename Archive, typename It>
const basic_snapshot & component(Archive &archive, It first, It last) const {
component<Component...>(archive, first, last, std::index_sequence_for<Component...>{});
return *this;
}
private:
const basic_registry<Entity> *reg;
const Entity seed;
follow_fn_type *follow;
};
/**
* @brief Utility class to restore a snapshot as a whole.
*
* A snapshot loader requires that the destination registry be empty and loads
* all the data at once while keeping intact the identifiers that the entities
* originally had.<br/>
* An example of use is the implementation of a save/restore utility.
*
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
template<typename Entity>
class basic_snapshot_loader {
/*! @brief A registry is allowed to create snapshot loaders. */
friend class basic_registry<Entity>;
using force_fn_type = void(basic_registry<Entity> &, const Entity, const bool);
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
basic_snapshot_loader(basic_registry<Entity> *source, force_fn_type *fn) ENTT_NOEXCEPT
: reg{source},
force{fn}
{
// to restore a snapshot as a whole requires a clean registry
ENTT_ASSERT(reg->empty());
}
template<typename Archive>
void assure(Archive &archive, bool discard) const {
typename traits_type::entity_type length{};
archive(length);
while(length--) {
Entity entt{};
archive(entt);
force(*reg, entt, discard);
}
}
template<typename Type, typename Archive, typename... Args>
void assign(Archive &archive, Args... args) const {
typename traits_type::entity_type length{};
archive(length);
while(length--) {
static constexpr auto discard = false;
Entity entt{};
if constexpr(std::is_empty_v<Type>) {
archive(entt);
force(*reg, entt, discard);
reg->template assign<Type>(args..., entt);
} else {
Type instance{};
archive(entt, instance);
force(*reg, entt, discard);
reg->template assign<Type>(args..., entt, std::as_const(instance));
}
}
}
public:
/*! @brief Default move constructor. */
basic_snapshot_loader(basic_snapshot_loader &&) = default;
/*! @brief Default move assignment operator. @return This loader. */
basic_snapshot_loader & operator=(basic_snapshot_loader &&) = default;
/**
* @brief Restores entities that were in use during serialization.
*
* This function restores the entities that were in use during serialization
* and gives them the versions they originally had.
*
* @tparam Archive Type of input archive.
* @param archive A valid reference to an input archive.
* @return A valid loader to continue restoring data.
*/
template<typename Archive>
const basic_snapshot_loader & entities(Archive &archive) const {
static constexpr auto discard = false;
assure(archive, discard);
return *this;
}
/**
* @brief Restores entities that were destroyed during serialization.
*
* This function restores the entities that were destroyed during
* serialization and gives them the versions they originally had.
*
* @tparam Archive Type of input archive.
* @param archive A valid reference to an input archive.
* @return A valid loader to continue restoring data.
*/
template<typename Archive>
const basic_snapshot_loader & destroyed(Archive &archive) const {
static constexpr auto discard = true;
assure(archive, discard);
return *this;
}
/**
* @brief Restores components and assigns them to the right entities.
*
* The template parameter list must be exactly the same used during
* serialization. In the event that the entity to which the component is
* assigned doesn't exist yet, the loader will take care to create it with
* the version it originally had.
*
* @tparam Component Types of components to restore.
* @tparam Archive Type of input archive.
* @param archive A valid reference to an input archive.
* @return A valid loader to continue restoring data.
*/
template<typename... Component, typename Archive>
const basic_snapshot_loader & component(Archive &archive) const {
(assign<Component>(archive), ...);
return *this;
}
/**
* @brief Destroys those entities that have no components.
*
* In case all the entities were serialized but only part of the components
* 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 basic_snapshot_loader & orphans() const {
reg->orphans([this](const auto entt) {
reg->destroy(entt);
});
return *this;
}
private:
basic_registry<Entity> *reg;
force_fn_type *force;
};
/**
* @brief Utility class for _continuous loading_.
*
* A _continuous loader_ is designed to load data from a source registry to a
* (possibly) non-empty destination. The loader can accomodate in a registry
* more than one snapshot in a sort of _continuous loading_ that updates the
* destination one step at a time.<br/>
* Identifiers that entities originally had are not transferred to the target.
* Instead, the loader maps remote identifiers to local ones while restoring a
* snapshot.<br/>
* An example of use is the implementation of a client-server applications with
* the requirement of transferring somehow parts of the representation side to
* side.
*
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
template<typename Entity>
class basic_continuous_loader {
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
void destroy(Entity entt) {
const auto it = remloc.find(entt);
if(it == remloc.cend()) {
const auto local = reg->create();
remloc.emplace(entt, std::make_pair(local, true));
reg->destroy(local);
}
}
void restore(Entity entt) {
const auto it = remloc.find(entt);
if(it == remloc.cend()) {
const auto local = reg->create();
remloc.emplace(entt, std::make_pair(local, true));
} else {
remloc[entt].first = reg->valid(remloc[entt].first) ? remloc[entt].first : reg->create();
// set the dirty flag
remloc[entt].second = true;
}
}
template<typename Container>
auto update(int, Container &container)
-> decltype(typename Container::mapped_type{}, void()) {
// map like container
Container other;
for(auto &&pair: container) {
using first_type = std::remove_const_t<typename std::decay_t<decltype(pair)>::first_type>;
using second_type = typename std::decay_t<decltype(pair)>::second_type;
if constexpr(std::is_same_v<first_type, Entity> && std::is_same_v<second_type, Entity>) {
other.emplace(map(pair.first), map(pair.second));
} else if constexpr(std::is_same_v<first_type, Entity>) {
other.emplace(map(pair.first), std::move(pair.second));
} else {
static_assert(std::is_same_v<second_type, Entity>);
other.emplace(std::move(pair.first), map(pair.second));
}
}
std::swap(container, other);
}
template<typename Container>
auto update(char, Container &container)
-> decltype(typename Container::value_type{}, void()) {
// vector like container
static_assert(std::is_same_v<typename Container::value_type, Entity>);
for(auto &&entt: container) {
entt = map(entt);
}
}
template<typename Other, typename Type, typename Member>
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...
update(0, instance.*member);
}
}
template<typename Archive>
void assure(Archive &archive, void(basic_continuous_loader:: *member)(Entity)) {
typename traits_type::entity_type length{};
archive(length);
while(length--) {
Entity entt{};
archive(entt);
(this->*member)(entt);
}
}
template<typename Component>
void reset() {
for(auto &&ref: remloc) {
const auto local = ref.second.first;
if(reg->valid(local)) {
reg->template reset<Component>(local);
}
}
}
template<typename Other, typename Archive, typename... Type, typename... Member>
void assign(Archive &archive, [[maybe_unused]] Member Type:: *... member) {
typename traits_type::entity_type length{};
archive(length);
while(length--) {
Entity entt{};
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));
}
}
}
public:
/*! @brief Underlying entity identifier. */
using entity_type = Entity;
/**
* @brief Constructs a loader that is bound to a given registry.
* @param source A valid reference to a registry.
*/
basic_continuous_loader(basic_registry<entity_type> &source) ENTT_NOEXCEPT
: reg{&source}
{}
/*! @brief Default move constructor. */
basic_continuous_loader(basic_continuous_loader &&) = default;
/*! @brief Default move assignment operator. @return This loader. */
basic_continuous_loader & operator=(basic_continuous_loader &&) = default;
/**
* @brief Restores entities that were in use during serialization.
*
* This function restores the entities that were in use during serialization
* and creates local counterparts for them if required.
*
* @tparam Archive Type of input archive.
* @param archive A valid reference to an input archive.
* @return A non-const reference to this loader.
*/
template<typename Archive>
basic_continuous_loader & entities(Archive &archive) {
assure(archive, &basic_continuous_loader::restore);
return *this;
}
/**
* @brief Restores entities that were destroyed during serialization.
*
* This function restores the entities that were destroyed during
* serialization and creates local counterparts for them if required.
*
* @tparam Archive Type of input archive.
* @param archive A valid reference to an input archive.
* @return A non-const reference to this loader.
*/
template<typename Archive>
basic_continuous_loader & destroyed(Archive &archive) {
assure(archive, &basic_continuous_loader::destroy);
return *this;
}
/**
* @brief Restores components and assigns them to the right entities.
*
* The template parameter list must be exactly the same used during
* serialization. In the event that the entity to which the component is
* assigned doesn't exist yet, the loader will take care to create a local
* counterpart for it.<br/>
* Members can be either data members of type entity_type or containers of
* entities. In both cases, the loader will visit them and update the
* entities by replacing each one with its local counterpart.
*
* @tparam Component Type of component to restore.
* @tparam Archive Type of input archive.
* @tparam Type Types of 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... Component, typename Archive, typename... Type, typename... Member>
basic_continuous_loader & component(Archive &archive, Member Type:: *... member) {
(reset<Component>(), ...);
(assign<Component>(archive, member...), ...);
return *this;
}
/**
* @brief Helps to purge entities that no longer have a conterpart.
*
* Users should invoke this member function after restoring each snapshot,
* unless they know exactly what they are doing.
*
* @return A non-const reference to this loader.
*/
basic_continuous_loader & shrink() {
auto it = remloc.begin();
while(it != remloc.cend()) {
const auto local = it->second.first;
bool &dirty = it->second.second;
if(dirty) {
dirty = false;
++it;
} else {
if(reg->valid(local)) {
reg->destroy(local);
}
it = remloc.erase(it);
}
}
return *this;
}
/**
* @brief Destroys those entities that have no components.
*
* In case all the entities were serialized but only part of the components
* 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.
*/
basic_continuous_loader & orphans() {
reg->orphans([this](const auto entt) {
reg->destroy(entt);
});
return *this;
}
/**
* @brief Tests if a loader knows about a given entity.
* @param entt An entity identifier.
* @return True if `entity` is managed by the loader, false otherwise.
*/
bool has(entity_type entt) const ENTT_NOEXCEPT {
return (remloc.find(entt) != remloc.cend());
}
/**
* @brief Returns the identifier to which an entity refers.
* @param entt An entity identifier.
* @return The local identifier if any, the null entity otherwise.
*/
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_type, std::pair<entity_type, bool>> remloc;
basic_registry<entity_type> *reg;
};
}
#endif // ENTT_ENTITY_SNAPSHOT_HPP

View File

@@ -3,26 +3,22 @@
#include <algorithm>
#include <iterator>
#include <utility>
#include <vector>
#include <memory>
#include <cstddef>
#include <cassert>
#include "traits.hpp"
#include <numeric>
#include <type_traits>
#include "../config/config.h"
#include "../core/algorithm.hpp"
#include "entity.hpp"
#include "fwd.hpp"
namespace entt {
/**
* @brief Sparse set.
*
* Primary template isn't defined on purpose. All the specializations give a
* compile-time error, but for a few reasonable cases.
*/
template<typename...>
class SparseSet;
/**
* @brief Basic sparse set implementation.
*
@@ -30,9 +26,9 @@ class SparseSet;
* Two arrays: an _external_ one and an _internal_ one; a _sparse_ one and a
* _packed_ one; one used for direct access through contiguous memory, the other
* one used to get the data through an extra level of indirection.<br/>
* This is largely used by the Registry to offer users the fastest access ever
* to the components. View and PersistentView are entirely designed around
* sparse sets.
* This is largely used by the registry to offer users the fastest access ever
* to the components. Views and groups in general are almost entirely designed
* around sparse sets.
*
* This type of data structure is widely documented in the literature and on the
* web. This is nothing more than a customized implementation suitable for the
@@ -51,69 +47,227 @@ class SparseSet;
* @tparam Entity A valid entity type (see entt_traits for more details).
*/
template<typename Entity>
class SparseSet<Entity> {
using traits_type = entt_traits<Entity>;
class sparse_set {
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
struct Iterator {
using value_type = Entity;
static_assert(ENTT_PAGE_SIZE && ((ENTT_PAGE_SIZE & (ENTT_PAGE_SIZE - 1)) == 0));
static constexpr auto entt_per_page = ENTT_PAGE_SIZE / sizeof(typename traits_type::entity_type);
Iterator(const std::vector<Entity> *direct, std::size_t pos)
: direct{direct}, pos{pos}
class iterator {
friend class sparse_set<Entity>;
using direct_type = const std::vector<Entity>;
using index_type = typename traits_type::difference_type;
iterator(direct_type *ref, const index_type idx) ENTT_NOEXCEPT
: direct{ref}, index{idx}
{}
Iterator & operator++() noexcept {
return --pos, *this;
public:
using difference_type = index_type;
using value_type = Entity;
using pointer = const value_type *;
using reference = const value_type &;
using iterator_category = std::random_access_iterator_tag;
iterator() ENTT_NOEXCEPT = default;
iterator & operator++() ENTT_NOEXCEPT {
return --index, *this;
}
Iterator operator++(int) noexcept {
Iterator orig = *this;
iterator operator++(int) ENTT_NOEXCEPT {
iterator orig = *this;
return ++(*this), orig;
}
bool operator==(const Iterator &other) const noexcept {
return other.pos == pos && other.direct == direct;
iterator & operator--() ENTT_NOEXCEPT {
return ++index, *this;
}
bool operator!=(const Iterator &other) const noexcept {
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{direct, index-value};
}
iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
return (*this += -value);
}
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 (*direct)[pos];
}
bool operator==(const iterator &other) const ENTT_NOEXCEPT {
return other.index == index;
}
bool operator!=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this == other);
}
value_type operator*() const noexcept {
return (*direct)[pos-1];
bool operator<(const iterator &other) const ENTT_NOEXCEPT {
return index > other.index;
}
bool operator>(const iterator &other) const ENTT_NOEXCEPT {
return index < other.index;
}
bool operator<=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this > other);
}
bool operator>=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this < other);
}
pointer operator->() const ENTT_NOEXCEPT {
const auto pos = size_type(index-1);
return &(*direct)[pos];
}
reference operator*() const ENTT_NOEXCEPT {
return *operator->();
}
private:
const std::vector<Entity> *direct;
std::size_t pos;
direct_type *direct;
index_type index;
};
static constexpr Entity in_use = 1 << traits_type::entity_shift;
void assure(const std::size_t page) {
if(!(page < reverse.size())) {
reverse.resize(page+1);
}
if(!reverse[page]) {
reverse[page] = std::make_unique<entity_type[]>(entt_per_page);
// null is safe in all cases for our purposes
std::fill_n(reverse[page].get(), entt_per_page, null);
}
}
auto map(const Entity entt) const ENTT_NOEXCEPT {
const auto identifier = to_integer(entt) & traits_type::entity_mask;
const auto page = size_type(identifier / entt_per_page);
const auto offset = size_type(identifier & (entt_per_page - 1));
return std::make_pair(page, offset);
}
public:
/*! @brief Underlying entity identifier. */
using entity_type = Entity;
/*! @brief Entity dependent position type. */
using pos_type = entity_type;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @brief Input iterator type. */
using iterator_type = Iterator;
/*! @brief Random access iterator type. */
using iterator_type = iterator;
/*! @brief Default constructor. */
SparseSet() noexcept = default;
sparse_set() = default;
/**
* @brief Copy constructor.
* @param other The instance to copy from.
*/
sparse_set(const sparse_set &other)
: reverse{},
direct{other.direct}
{
for(size_type pos{}, last = other.reverse.size(); pos < last; ++pos) {
if(other.reverse[pos]) {
assure(pos);
std::copy_n(other.reverse[pos].get(), entt_per_page, reverse[pos].get());
}
}
}
/*! @brief Default move constructor. */
sparse_set(sparse_set &&) = default;
/*! @brief Default destructor. */
virtual ~SparseSet() noexcept = default;
virtual ~sparse_set() ENTT_NOEXCEPT = default;
/*! @brief Copying a sparse set isn't allowed. */
SparseSet(const SparseSet &) = delete;
/*! @brief Default move constructor. */
SparseSet(SparseSet &&) = default;
/**
* @brief Copy assignment operator.
* @param other The instance to copy from.
* @return This sparse set.
*/
sparse_set & operator=(const sparse_set &other) {
if(&other != this) {
auto tmp{other};
*this = std::move(tmp);
}
return *this;
}
/*! @brief Copying a sparse set isn't allowed. @return This sparse set. */
SparseSet & operator=(const SparseSet &) = delete;
/*! @brief Default move assignment operator. @return This sparse set. */
SparseSet & operator=(SparseSet &&) = default;
sparse_set & operator=(sparse_set &&) = default;
/**
* @brief Increases the capacity of a sparse set.
*
* If the new capacity is greater than the current capacity, new storage is
* allocated, otherwise the method does nothing.
*
* @param cap Desired capacity.
*/
void reserve(const size_type cap) {
direct.reserve(cap);
}
/**
* @brief Returns the number of elements that a sparse set has currently
* allocated space for.
* @return Capacity of the sparse set.
*/
size_type capacity() const ENTT_NOEXCEPT {
return direct.capacity();
}
/*! @brief Requests the removal of unused capacity. */
void shrink_to_fit() {
// conservative approach
if(direct.empty()) {
reverse.clear();
}
reverse.shrink_to_fit();
direct.shrink_to_fit();
}
/**
* @brief Returns the extent of a sparse set.
*
* The extent of a sparse set is also the size of the internal sparse array.
* There is no guarantee that the internal packed array has the same size.
* Usually the size of the internal sparse array is equal or greater than
* the one of the internal packed array.
*
* @return Extent of the sparse set.
*/
size_type extent() const ENTT_NOEXCEPT {
return reverse.size() * entt_per_page;
}
/**
* @brief Returns the number of elements in a sparse set.
@@ -125,7 +279,7 @@ public:
*
* @return Number of elements.
*/
size_type size() const noexcept {
size_type size() const ENTT_NOEXCEPT {
return direct.size();
}
@@ -133,7 +287,7 @@ public:
* @brief Checks whether a sparse set is empty.
* @return True if the sparse set is empty, false otherwise.
*/
bool empty() const noexcept {
bool empty() const ENTT_NOEXCEPT {
return direct.empty();
}
@@ -144,7 +298,7 @@ public:
* always a valid range, even if the container is empty.
*
* @note
* There are no guarantees on the order, even though `sort` has been
* There are no guarantees on the order, even though `respect` has been
* previously invoked. Internal data structures arrange elements to maximize
* performance. Accessing them directly gives a performance boost but less
* guarantees. Use `begin` and `end` if you want to iterate the sparse set
@@ -152,52 +306,65 @@ public:
*
* @return A pointer to the internal packed array.
*/
const entity_type * data() const noexcept {
const entity_type * data() const ENTT_NOEXCEPT {
return direct.data();
}
/**
* @brief Returns an iterator to the beginning.
*
* The returned iterator points to the first element of the internal packed
* The returned iterator points to the first entity of the internal packed
* array. If the sparse set is empty, the returned iterator will be equal to
* `end()`.
*
* @note
* Input iterators stay true to the order imposed by a call to `sort`.
* Random access iterators stay true to the order imposed by a call to
* `respect`.
*
* @return An iterator to the first element of the internal packed array.
* @return An iterator to the first entity of the internal packed array.
*/
iterator_type begin() const noexcept {
return Iterator{&direct, direct.size()};
iterator_type begin() const ENTT_NOEXCEPT {
const typename traits_type::difference_type pos = direct.size();
return iterator_type{&direct, pos};
}
/**
* @brief Returns an iterator to the end.
*
* The returned iterator points to the element following the last element in
* The returned iterator points to the element following the last entity in
* the internal packed array. Attempting to dereference the returned
* iterator results in undefined behavior.
*
* @note
* Input iterators stay true to the order imposed by a call to `sort`.
* Random access iterators stay true to the order imposed by a call to
* `respect`.
*
* @return An iterator to the element following the last element of the
* @return An iterator to the element following the last entity of the
* internal packed array.
*/
iterator_type end() const noexcept {
return Iterator{&direct, 0};
iterator_type end() const ENTT_NOEXCEPT {
return iterator_type{&direct, {}};
}
/**
* @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 {
return has(entt) ? --(end() - index(entt)) : end();
}
/**
* @brief Checks if a sparse set contains an entity.
* @param entity A valid entity identifier.
* @param entt A valid entity identifier.
* @return True if the sparse set contains the entity, false otherwise.
*/
bool has(entity_type entity) const noexcept {
const auto entt = entity & traits_type::entity_mask;
// the in-use control bit permits to avoid accessing the direct vector
return (entt < reverse.size()) && (reverse[entt] & in_use);
bool has(const entity_type entt) const ENTT_NOEXCEPT {
auto [page, offset] = map(entt);
// testing against null permits to avoid accessing the direct vector
return (page < reverse.size() && reverse[page] && reverse[page][offset] != null);
}
/**
@@ -209,14 +376,13 @@ public:
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entity.
*
* @param entity A valid entity identifier.
* @param entt A valid entity identifier.
* @return The position of the entity in the sparse set.
*/
pos_type get(entity_type entity) const noexcept {
assert(has(entity));
const auto entt = entity & traits_type::entity_mask;
// we must get rid of the in-use bit for it's not part of the position
return reverse[entt] & ~in_use;
size_type index(const entity_type entt) const ENTT_NOEXCEPT {
ENTT_ASSERT(has(entt));
auto [page, offset] = map(entt);
return size_type(reverse[page][offset]);
}
/**
@@ -228,20 +394,39 @@ public:
* An assertion will abort the execution at runtime in debug mode if the
* sparse set already contains the given entity.
*
* @param entity A valid entity identifier.
* @param entt A valid entity identifier.
*/
void construct(entity_type entity) {
assert(!has(entity));
const auto entt = entity & traits_type::entity_mask;
void construct(const entity_type entt) {
ENTT_ASSERT(!has(entt));
auto [page, offset] = map(entt);
assure(page);
reverse[page][offset] = entity_type(direct.size());
direct.push_back(entt);
}
if(!(entt < reverse.size())) {
reverse.resize(entt+1, pos_type{});
}
/**
* @brief Assigns one or more entities to a sparse set.
*
* @warning
* Attempting to assign an entity that already belongs to the sparse set
* results in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set already contains the given entity.
*
* @tparam 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.
*/
template<typename It>
void batch(It first, It last) {
std::for_each(first, last, [this, next = direct.size()](const auto entt) mutable {
ENTT_ASSERT(!has(entt));
auto [page, offset] = map(entt);
assure(page);
reverse[page][offset] = entity_type(next++);
});
// we exploit the fact that pos_type is equal to entity_type and pos has
// traits_type::version_mask bits unused we can use to mark it as in-use
reverse[entt] = pos_type(direct.size()) | in_use;
direct.emplace_back(entity);
direct.insert(direct.end(), first, last);
}
/**
@@ -253,24 +438,20 @@ public:
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entity.
*
* @param entity A valid entity identifier.
* @param entt A valid entity identifier.
*/
virtual void destroy(entity_type entity) {
assert(has(entity));
const auto entt = entity & traits_type::entity_mask;
const auto back = direct.back() & traits_type::entity_mask;
const auto pos = reverse[entt] & ~in_use;
// the order matters: if back and entt are the same (for the sparse set
// has size 1), switching the two lines below doesn't work as expected
reverse[back] = pos | in_use;
reverse[entt] = pos;
// swapping isn't required here, we are getting rid of the last element
direct[pos] = direct.back();
void destroy(const entity_type entt) {
ENTT_ASSERT(has(entt));
auto [from_page, from_offset] = map(entt);
auto [to_page, to_offset] = map(direct.back());
direct[size_type(reverse[from_page][from_offset])] = entity_type(direct.back());
reverse[to_page][to_offset] = reverse[from_page][from_offset];
reverse[from_page][from_offset] = null;
direct.pop_back();
}
/**
* @brief Swaps the position of two entities in the internal packed array.
* @brief Swaps two entities in the internal packed array.
*
* For what it's worth, this function affects both the internal sparse array
* and the internal packed array. Users should not care of that anyway.
@@ -284,301 +465,179 @@ public:
* @param lhs A valid entity identifier.
* @param rhs A valid entity identifier.
*/
virtual void swap(entity_type lhs, entity_type rhs) {
assert(has(lhs));
assert(has(rhs));
auto &le = reverse[lhs & traits_type::entity_mask];
auto &re = reverse[rhs & traits_type::entity_mask];
// we must get rid of the in-use bit for it's not part of the position
std::swap(direct[le & ~in_use], direct[re & ~in_use]);
std::swap(le, re);
virtual void swap(const entity_type lhs, const entity_type rhs) ENTT_NOEXCEPT {
auto [src_page, src_offset] = map(lhs);
auto [dst_page, dst_offset] = map(rhs);
auto &from = reverse[src_page][src_offset];
auto &to = reverse[dst_page][dst_offset];
std::swap(direct[size_type(from)], direct[size_type(to)]);
std::swap(from, to);
}
/**
* @brief Sort entities according to the given comparison function.
* @brief Sort elements according to the given comparison function.
*
* Sort the elements so that iterating the sparse set with a couple of
* iterators returns them in the expected order. See `begin` and `end` for
* more details.
* Sort the elements so that iterating the range with a couple of iterators
* returns them in the expected order. See `begin` and `end` for more
* details.
*
* The comparison function object must return `true` if the first element
* is _less_ than the second one, `false` otherwise. The signature of the
* comparison function should be equivalent to the following:
*
* @code{.cpp}
* bool(const Entity, const Entity);
* @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.
*
* @note
* Attempting to iterate elements using the raw pointer returned by `data`
* gives no guarantees on the order, even though `sort` has been invoked.
* Attempting to iterate elements using a raw pointer returned by a call to
* `data` gives no guarantees on the order, even though `sort` has been
* invoked.
*
* @tparam Compare Type of the comparison function.
* @param compare A comparison function whose signature shall be equivalent
* to: `bool(Entity, Entity)`.
* @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 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.
* @param algo A valid sort function object.
* @param args Arguments to forward to the sort function object, if any.
*/
template<typename Compare>
void sort(Compare compare) {
std::vector<pos_type> copy{direct.cbegin(), direct.cend()};
std::sort(copy.begin(), copy.end(), [compare = std::move(compare)](auto... args) {
return !compare(args...);
});
template<typename Compare, typename Sort = std_sort, typename... Args>
void sort(iterator_type first, iterator_type last, Compare compare, Sort algo = Sort{}, Args &&... args) {
ENTT_ASSERT(!(last < first));
ENTT_ASSERT(!(last > end()));
for(pos_type i = 0; i < copy.size(); ++i) {
if(direct[i] != copy[i]) {
swap(direct[i], copy[i]);
const auto length = std::distance(first, last);
const auto skip = std::distance(last, end());
const auto to = direct.rend() - skip;
const auto from = to - length;
algo(from, to, std::move(compare), std::forward<Args>(args)...);
for(size_type pos = skip, end = skip+length; pos < end; ++pos) {
auto [page, offset] = map(direct[pos]);
reverse[page][offset] = entity_type(pos);
}
}
/**
* @brief Sort elements according to the given comparison function.
*
* @sa sort
*
* This function is a slightly slower version of `sort` that invokes the
* caller to indicate which entities are swapped.<br/>
* It's recommended when the caller wants to sort its own data structures to
* align them with the order induced in the sparse set.
*
* The signature of the callback should be equivalent to the following:
*
* @code{.cpp}
* bool(const Entity, const Entity);
* @endcode
*
* @tparam Apply Type of function object to invoke to notify the caller.
* @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 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 apply A valid function object to use as a callback.
* @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 Apply, typename Compare, typename Sort = std_sort, typename... Args>
void arrange(iterator_type first, iterator_type last, Apply apply, Compare compare, Sort algo = Sort{}, Args &&... args) {
ENTT_ASSERT(!(last < first));
ENTT_ASSERT(!(last > end()));
const auto length = std::distance(first, last);
const auto skip = std::distance(last, end());
const auto to = direct.rend() - skip;
const auto from = to - length;
algo(from, to, std::move(compare), std::forward<Args>(args)...);
for(size_type pos = skip, end = skip+length; pos < end; ++pos) {
auto curr = pos;
auto next = index(direct[curr]);
while(curr != next) {
apply(direct[curr], direct[next]);
auto [page, offset] = map(direct[curr]);
reverse[page][offset] = entity_type(curr);
curr = next;
next = index(direct[curr]);
}
}
}
/**
* @brief Sort entities according to their order in a sparse set.
* @brief Sort entities according to their order in another sparse set.
*
* Entities that are part of both the sparse sets are ordered internally
* according to the order they have in `other`. All the other entities goes
* to the end of the list and there are no guarantess on their order.<br/>
* to the end of the list and there are no guarantees on their order.<br/>
* In other terms, this function can be used to impose the same order on two
* sets by using one of them as a master and the other one as a slave.
*
* Iterating the sparse set with a couple of iterators returns elements in
* the expected order after a call to `sort`. See `begin` and `end` for more
* details.
* the expected order after a call to `respect`. See `begin` and `end` for
* more details.
*
* @note
* Attempting to iterate elements using the raw pointer returned by `data`
* gives no guarantees on the order, even though `sort` has been invoked.
* Attempting to iterate elements using a raw pointer returned by a call to
* `data` gives no guarantees on the order, even though `respect` has been
* invoked.
*
* @param other The sparse sets that imposes the order of the entities.
*/
void respect(const SparseSet<Entity> &other) {
struct Bool { bool value{false}; };
std::vector<Bool> check(std::max(other.reverse.size(), reverse.size()));
void respect(const sparse_set &other) ENTT_NOEXCEPT {
const auto to = other.end();
auto from = other.begin();
for(auto entity: other.direct) {
check[entity & traits_type::entity_mask].value = true;
}
size_type pos = direct.size() - 1;
sort([this, &other, &check](auto lhs, auto rhs) {
const auto le = lhs & traits_type::entity_mask;
const auto re = rhs & traits_type::entity_mask;
while(pos && from != to) {
if(has(*from)) {
if(*from != direct[pos]) {
swap(direct[pos], *from);
}
const bool bLhs = check[le].value;
const bool bRhs = check[re].value;
bool compare = false;
if(bLhs && bRhs) {
compare = other.get(rhs) < other.get(lhs);
} else if(!bLhs && !bRhs) {
compare = re < le;
} else {
compare = bLhs;
--pos;
}
return compare;
});
++from;
}
}
/**
* @brief Resets a sparse set.
*/
virtual void reset() {
void reset() {
reverse.clear();
direct.clear();
}
private:
std::vector<pos_type> reverse;
std::vector<std::unique_ptr<entity_type[]>> reverse;
std::vector<entity_type> direct;
};
/**
* @brief Extended sparse set implementation.
*
* This specialization of a sparse set associates an object to an entity. The
* main purpose of this class is to use 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.
*
* @sa SparseSet<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>
class SparseSet<Entity, Type>: public SparseSet<Entity> {
using underlying_type = SparseSet<Entity>;
public:
/*! @brief Type of the objects associated to the entities. */
using object_type = Type;
/*! @brief Underlying entity identifier. */
using entity_type = typename underlying_type::entity_type;
/*! @brief Entity dependent position type. */
using pos_type = typename underlying_type::pos_type;
/*! @brief Unsigned integer type. */
using size_type = typename underlying_type::size_type;
/*! @brief Input iterator type. */
using iterator_type = typename underlying_type::iterator_type;
/*! @brief Default constructor. */
SparseSet() noexcept = default;
/*! @brief Copying a sparse set isn't allowed. */
SparseSet(const SparseSet &) = delete;
/*! @brief Default move constructor. */
SparseSet(SparseSet &&) = default;
/*! @brief Copying a sparse set isn't allowed. @return This sparse set. */
SparseSet & operator=(const SparseSet &) = delete;
/*! @brief Default move assignment operator. @return This sparse set. */
SparseSet & operator=(SparseSet &&) = default;
/**
* @brief 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 `sort` has been
* previously invoked. Internal data structures arrange elements to maximize
* performance. Accessing them directly gives a performance boost but less
* guarantees. Use `begin` and `end` if you want to iterate the sparse set
* in the expected order.
*
* @return A pointer to the array of objects.
*/
const object_type * raw() const noexcept {
return instances.data();
}
/**
* @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 `sort` has been
* previously invoked. Internal data structures arrange elements to maximize
* performance. Accessing them directly gives a performance boost but less
* guarantees. Use `begin` and `end` if you want to iterate the sparse set
* in the expected order.
*
* @return A pointer to the array of objects.
*/
object_type * raw() noexcept {
return instances.data();
}
/**
* @brief Returns the object associated to an entity.
*
* @warning
* Attempting to use an entity that doesn't belong to the sparse set results
* in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entity.
*
* @param entity A valid entity identifier.
* @return The object associated to the entity.
*/
const object_type & get(entity_type entity) const noexcept {
return instances[underlying_type::get(entity)];
}
/**
* @brief Returns the object associated to an entity.
*
* @warning
* Attempting to use an entity that doesn't belong to the sparse set results
* in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entity.
*
* @param entity A valid entity identifier.
* @return The object associated to the entity.
*/
object_type & get(entity_type entity) noexcept {
return const_cast<object_type &>(const_cast<const SparseSet *>(this)->get(entity));
}
/**
* @brief Assigns an entity to a sparse set and constructs its object.
*
* @warning
* Attempting to use an entity that already belongs to the sparse set
* results in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set already contains the given entity.
*
* @tparam Args Types of arguments to use to construct the object.
* @param entity A valid entity identifier.
* @param args Parameters to use to construct an object for the entity.
* @return The object associated to the entity.
*/
template<typename... Args>
object_type & construct(entity_type entity, Args&&... args) {
underlying_type::construct(entity);
// emplace_back doesn't work well with PODs because of its placement new
instances.push_back({ std::forward<Args>(args)... });
return instances.back();
}
/**
* @brief Removes an entity from a sparse set and destroies its object.
*
* @warning
* Attempting to use an entity that doesn't belong to the sparse set results
* in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entity.
*
* @param entity A valid entity identifier.
*/
void destroy(entity_type entity) override {
// swapping isn't required here, we are getting rid of the last element
instances[underlying_type::get(entity)] = std::move(instances.back());
instances.pop_back();
underlying_type::destroy(entity);
}
/**
* @brief Swaps two entities and their objects.
*
* @note
* This function doesn't swap objects between entities. It exchanges entity
* and object positions in the sparse set. It's used mainly for sorting.
*
* @warning
* Attempting to use entities that don't belong to the sparse set results
* in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entities.
*
* @param lhs A valid entity identifier.
* @param rhs A valid entity identifier.
*/
void swap(entity_type lhs, entity_type rhs) override {
std::swap(instances[underlying_type::get(lhs)], instances[underlying_type::get(rhs)]);
underlying_type::swap(lhs, rhs);
}
/**
* @brief Resets a sparse set.
*/
void reset() override {
underlying_type::reset();
instances.clear();
}
private:
std::vector<object_type> instances;
};
}

703
src/entt/entity/storage.hpp Normal file
View File

@@ -0,0 +1,703 @@
#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 random or input access
* 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<std::underlying_type_t<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};
}
iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
return (*this += -value);
}
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;
}
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;
}
bool operator<=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this > other);
}
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];
}
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 = Entity;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @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) {
underlying_type::reserve(cap);
instances.reserve(cap);
}
/*! @brief Requests the removal of unused capacity. */
void shrink_to_fit() {
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
* Random access 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 */
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
* Random access 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 */
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::index(entt)];
}
/*! @copydoc get */
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::index(entt)) : nullptr;
}
/*! @copydoc try_get */
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 default constructs
* or copy constructs their objects.
*
* The object type must be at least move and default insertable if no
* arguments are provided, move and copy insertable otherwise.
*
* @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.
* @tparam Args Types of arguments to use to construct the object.
* @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.
* @param args Parameters to use to construct an object for the entities.
* @return An iterator to the list of instances just created and sorted the
* same of the entities.
*/
template<typename It, typename... Args>
iterator_type batch(It first, It last, Args &&... args) {
if constexpr(sizeof...(Args) == 0) {
instances.resize(instances.size() + std::distance(first, last));
} else {
instances.resize(instances.size() + std::distance(first, last), Type{std::forward<Args>(args)...});
}
// entity goes after component in case constructor throws
underlying_type::batch(first, last);
return begin();
}
/**
* @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) {
auto other = std::move(instances.back());
instances[underlying_type::index(entt)] = std::move(other);
instances.pop_back();
underlying_type::destroy(entt);
}
/**
* @brief Swaps entities and objects in the internal packed arrays.
*
* @warning
* Attempting to swap entities that don't belong to the sparse set results
* in undefined behavior.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* sparse set doesn't contain the given entities.
*
* @param lhs A valid entity identifier.
* @param rhs A valid entity identifier.
*/
void swap(const entity_type lhs, const entity_type rhs) ENTT_NOEXCEPT override {
std::swap(instances[underlying_type::index(lhs)], instances[underlying_type::index(rhs)]);
underlying_type::swap(lhs, rhs);
}
/**
* @brief Sort elements according to the given comparison function.
*
* Sort the elements so that iterating the range 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.
*
* @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.
*
* @warning
* Empty types are never instantiated. Therefore, only comparison function
* objects that require to return entities rather than components are
* accepted.
*
* @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 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.
* @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(iterator_type first, iterator_type last, Compare compare, Sort algo = Sort{}, Args &&... args) {
ENTT_ASSERT(!(last < first));
ENTT_ASSERT(!(last > end()));
const auto from = underlying_type::begin() + std::distance(begin(), first);
const auto to = from + std::distance(first, last);
const auto apply = [this](const auto lhs, const auto rhs) {
std::swap(instances[underlying_type::index(lhs)], instances[underlying_type::index(rhs)]);
};
if constexpr(std::is_invocable_v<Compare, const object_type &, const object_type &>) {
underlying_type::arrange(from, to, std::move(apply), [this, compare = std::move(compare)](const auto lhs, const auto rhs) {
return compare(std::as_const(instances[underlying_type::index(lhs)]), std::as_const(instances[underlying_type::index(rhs)]));
}, std::move(algo), std::forward<Args>(args)...);
} else {
underlying_type::arrange(from, to, std::move(apply), std::move(compare), std::move(algo), std::forward<Args>(args)...);
}
}
/*! @brief Resets a storage. */
void reset() {
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<ENTT_ENABLE_ETO(Type)>>: public sparse_set<Entity> {
using traits_type = entt_traits<std::underlying_type_t<Entity>>;
using underlying_type = sparse_set<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};
}
iterator & operator-=(const difference_type value) ENTT_NOEXCEPT {
return (*this += -value);
}
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;
}
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;
}
bool operator<=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this > other);
}
bool operator>=(const iterator &other) const ENTT_NOEXCEPT {
return !(*this < other);
}
pointer operator->() const ENTT_NOEXCEPT {
return nullptr;
}
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 = Entity;
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/*! @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 */
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 */
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 {};
}
/**
* @brief Assigns one or more entities to a storage.
*
* 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 An iterator to the list of instances just created and sorted the
* same of the entities.
*/
template<typename It>
iterator_type batch(It first, It last, const object_type & = {}) {
underlying_type::batch(first, last);
return begin();
}
/*! @copydoc storage::sort */
template<typename Compare, typename Sort = std_sort, typename... Args>
void sort(iterator_type first, iterator_type last, Compare compare, Sort algo = Sort{}, Args &&... args) {
ENTT_ASSERT(!(last < first));
ENTT_ASSERT(!(last > end()));
const auto from = underlying_type::begin() + std::distance(begin(), first);
const auto to = from + std::distance(first, last);
underlying_type::sort(from, to, std::move(compare), std::move(algo), std::forward<Args>(args)...);
}
};
/*! @copydoc basic_storage */
template<typename Entity, typename Type>
struct storage: basic_storage<Entity, Type> {};
}
#endif // ENTT_ENTITY_STORAGE_HPP

View File

@@ -1,96 +0,0 @@
#ifndef ENTT_ENTITY_ENTT_HPP
#define ENTT_ENTITY_ENTT_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 Mask to use to get the entity number out of an identifier. */
static constexpr auto entity_mask = 0xFFF;
/*! @brief Mask to use to get the version out of an identifier. */
static constexpr auto version_mask = 0xF;
/*! @brief Extent of the entity number within an identifier. */
static constexpr auto entity_shift = 12;
};
/**
* @brief Entity traits for a 32 bits entity identifier.
*
* A 32 bits entity identifier guarantees:
*
* * 24 bits for the entity number (suitable for almost all the games).
* * 8 bit for the version (resets in [0-255]).
*/
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 Mask to use to get the entity number out of an identifier. */
static constexpr auto entity_mask = 0xFFFFFF;
/*! @brief Mask to use to get the version out of an identifier. */
static constexpr auto version_mask = 0xFF;
/*! @brief Extent of the entity number within an identifier. */
static constexpr auto entity_shift = 24;
};
/**
* @brief Entity traits for a 64 bits entity identifier.
*
* A 64 bits entity identifier guarantees:
*
* * 40 bits for the entity number (an indecently large number).
* * 24 bit for the version (an indecently large number).
*/
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 Mask to use to get the entity number out of an identifier. */
static constexpr auto entity_mask = 0xFFFFFFFFFF;
/*! @brief Mask to use to get the version out of an identifier. */
static constexpr auto version_mask = 0xFFFFFF;
/*! @brief Extent of the entity number within an identifier. */
static constexpr auto entity_shift = 40;
};
}
#endif // ENTT_ENTITY_ENTT_HPP

View File

@@ -0,0 +1,46 @@
#ifndef ENTT_ENTITY_UTILITY_HPP
#define ENTT_ENTITY_UTILITY_HPP
#include "../core/type_traits.hpp"
namespace entt {
/**
* @brief Alias for exclusion lists.
* @tparam Type List of types.
*/
template<typename... Type>
struct exclude_t: type_list<Type...> {};
/**
* @brief Variable template for exclusion lists.
* @tparam Type List of types.
*/
template<typename... Type>
constexpr exclude_t<Type...> exclude{};
/**
* @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{};
}
#endif // ENTT_ENTITY_UTILITY_HPP

File diff suppressed because it is too large Load Diff

View File

@@ -1,19 +1,32 @@
#include "core/algorithm.hpp"
#include "core/family.hpp"
#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/entity.hpp"
#include "entity/group.hpp"
#include "entity/helper.hpp"
#include "entity/observer.hpp"
#include "entity/registry.hpp"
#include "entity/runtime_view.hpp"
#include "entity/snapshot.hpp"
#include "entity/sparse_set.hpp"
#include "entity/traits.hpp"
#include "entity/storage.hpp"
#include "entity/utility.hpp"
#include "entity/view.hpp"
#include "locator/locator.hpp"
#include "meta/factory.hpp"
#include "meta/meta.hpp"
#include "meta/policy.hpp"
#include "process/process.hpp"
#include "process/scheduler.hpp"
#include "resource/cache.hpp"
#include "resource/handle.hpp"
#include "resource/loader.hpp"
#include "signal/bus.hpp"
#include "signal/delegate.hpp"
#include "signal/dispatcher.hpp"
#include "signal/emitter.hpp"
#include "signal/sigh.hpp"
#include "signal/signal.hpp"

3
src/entt/fwd.hpp Normal file
View File

@@ -0,0 +1,3 @@
#include "entity/fwd.hpp"
#include "resource/fwd.hpp"
#include "signal/fwd.hpp"

View File

@@ -4,7 +4,7 @@
#include <memory>
#include <utility>
#include <cassert>
#include "../config/config.h"
namespace entt {
@@ -14,7 +14,7 @@ namespace entt {
* @brief Service locator, nothing more.
*
* A service locator can be used to do what it promises: locate services.<br/>
* Usually service locators are tighly bound to the services they expose and
* Usually service locators are tightly bound to the services they expose and
* thus it's hard to define a general purpose class to do that. This template
* based implementation tries to fill the gap and to get rid of the burden of
* defining a different specific locator for each application.
@@ -22,20 +22,20 @@ 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.
* @return True if the service is set, false otherwise.
*/
inline static bool empty() noexcept {
static bool empty() ENTT_NOEXCEPT {
return !static_cast<bool>(service);
}
@@ -49,7 +49,7 @@ struct ServiceLocator final {
*
* @return A reference to the service implementation currently set, if any.
*/
inline static std::weak_ptr<Service> get() noexcept {
static std::weak_ptr<Service> get() ENTT_NOEXCEPT {
return service;
}
@@ -67,7 +67,7 @@ struct ServiceLocator final {
*
* @return A reference to the service implementation currently set, if any.
*/
inline static Service & ref() noexcept {
static Service & ref() ENTT_NOEXCEPT {
return *service;
}
@@ -78,7 +78,7 @@ struct ServiceLocator final {
* @param args Parameters to use to construct the service.
*/
template<typename Impl = Service, typename... Args>
inline static void set(Args&&... args) {
static void set(Args &&... args) {
service = std::make_shared<Impl>(std::forward<Args>(args)...);
}
@@ -86,8 +86,8 @@ struct ServiceLocator final {
* @brief Sets or replaces a service.
* @param ptr Service to use to replace the current one.
*/
inline static void set(std::shared_ptr<Service> ptr) {
assert(static_cast<bool>(ptr));
static void set(std::shared_ptr<Service> ptr) {
ENTT_ASSERT(static_cast<bool>(ptr));
service = std::move(ptr);
}
@@ -96,19 +96,15 @@ struct ServiceLocator final {
*
* The service is no longer valid after a reset.
*/
inline static void reset() {
static void reset() {
service.reset();
}
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{};
}

858
src/entt/meta/factory.hpp Normal file
View File

@@ -0,0 +1,858 @@
#ifndef ENTT_META_FACTORY_HPP
#define ENTT_META_FACTORY_HPP
#include <tuple>
#include <array>
#include <cstddef>
#include <utility>
#include <functional>
#include <type_traits>
#include "../config/config.h"
#include "../core/type_traits.hpp"
#include "policy.hpp"
#include "meta.hpp"
namespace entt {
/**
* @cond TURN_OFF_DOXYGEN
* Internal details not to be documented.
*/
namespace internal {
template<typename>
struct meta_function_helper;
template<typename Ret, typename... Args>
struct meta_function_helper<Ret(Args...)> {
using return_type = std::remove_cv_t<std::remove_reference_t<Ret>>;
using args_type = std::tuple<std::remove_cv_t<std::remove_reference_t<Args>>...>;
static constexpr std::index_sequence_for<Args...> index_sequence{};
static constexpr auto is_const = false;
static auto arg(typename internal::meta_func_node::size_type index) ENTT_NOEXCEPT {
return std::array<meta_type_node *, sizeof...(Args)>{{meta_info<Args>::resolve()...}}[index];
}
};
template<typename Ret, typename... Args>
struct meta_function_helper<Ret(Args...) const>: meta_function_helper<Ret(Args...)> {
static constexpr auto is_const = true;
};
template<typename Ret, typename... Args, typename Class>
constexpr meta_function_helper<Ret(Args...)>
to_meta_function_helper(Ret(Class:: *)(Args...));
template<typename Ret, typename... Args, typename Class>
constexpr meta_function_helper<Ret(Args...) const>
to_meta_function_helper(Ret(Class:: *)(Args...) const);
template<typename Ret, typename... Args>
constexpr meta_function_helper<Ret(Args...)>
to_meta_function_helper(Ret(*)(Args...));
constexpr void to_meta_function_helper(...);
template<typename Candidate>
using meta_function_helper_t = decltype(to_meta_function_helper(std::declval<Candidate>()));
template<typename Type, typename... Args, std::size_t... Indexes>
meta_any construct(meta_any * const args, std::index_sequence<Indexes...>) {
[[maybe_unused]] auto direct = std::make_tuple((args+Indexes)->try_cast<Args>()...);
meta_any any{};
if(((std::get<Indexes>(direct) || (args+Indexes)->convert<Args>()) && ...)) {
any = Type{(std::get<Indexes>(direct) ? *std::get<Indexes>(direct) : (args+Indexes)->cast<Args>())...};
}
return any;
}
template<bool Const, typename Type, auto Data>
bool setter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index, [[maybe_unused]] meta_any value) {
bool accepted = false;
if constexpr(!Const) {
if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Data)>> || std::is_member_function_pointer_v<decltype(Data)>) {
using helper_type = meta_function_helper_t<decltype(Data)>;
using data_type = std::tuple_element_t<!std::is_member_function_pointer_v<decltype(Data)>, typename helper_type::args_type>;
static_assert(std::is_invocable_v<decltype(Data), Type &, data_type>);
auto * const clazz = meta_any{handle}.try_cast<Type>();
auto * const direct = value.try_cast<data_type>();
if(clazz && (direct || value.convert<data_type>())) {
std::invoke(Data, *clazz, direct ? *direct : value.cast<data_type>());
accepted = true;
}
} else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
static_assert(std::is_invocable_v<decltype(Data), Type *>);
auto * const clazz = meta_any{handle}.try_cast<Type>();
if constexpr(std::is_array_v<data_type>) {
using underlying_type = std::remove_extent_t<data_type>;
auto * const direct = value.try_cast<underlying_type>();
auto * const idx = index.try_cast<std::size_t>();
if(clazz && idx && (direct || value.convert<underlying_type>())) {
std::invoke(Data, clazz)[*idx] = direct ? *direct : value.cast<underlying_type>();
accepted = true;
}
} else {
auto * const direct = value.try_cast<data_type>();
if(clazz && (direct || value.convert<data_type>())) {
std::invoke(Data, clazz) = (direct ? *direct : value.cast<data_type>());
accepted = true;
}
}
} else {
static_assert(std::is_pointer_v<decltype(Data)>);
using data_type = std::remove_cv_t<std::remove_reference_t<decltype(*Data)>>;
if constexpr(std::is_array_v<data_type>) {
using underlying_type = std::remove_extent_t<data_type>;
auto * const direct = value.try_cast<underlying_type>();
auto * const idx = index.try_cast<std::size_t>();
if(idx && (direct || value.convert<underlying_type>())) {
(*Data)[*idx] = (direct ? *direct : value.cast<underlying_type>());
accepted = true;
}
} else {
auto * const direct = value.try_cast<data_type>();
if(direct || value.convert<data_type>()) {
*Data = (direct ? *direct : value.cast<data_type>());
accepted = true;
}
}
}
}
return accepted;
}
template<typename Type, auto Data, typename Policy>
meta_any getter([[maybe_unused]] meta_handle handle, [[maybe_unused]] meta_any index) {
auto dispatch = [](auto &&value) {
if constexpr(std::is_same_v<Policy, as_void_t>) {
return meta_any{std::in_place_type<void>};
} else if constexpr(std::is_same_v<Policy, as_alias_t>) {
return meta_any{std::ref(std::forward<decltype(value)>(value))};
} else {
static_assert(std::is_same_v<Policy, as_is_t>);
return meta_any{std::forward<decltype(value)>(value)};
}
};
if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Data)>> || std::is_member_function_pointer_v<decltype(Data)>) {
static_assert(std::is_invocable_v<decltype(Data), Type &>);
auto * const clazz = meta_any{handle}.try_cast<Type>();
return clazz ? dispatch(std::invoke(Data, *clazz)) : meta_any{};
} else if constexpr(std::is_member_object_pointer_v<decltype(Data)>) {
using data_type = std::remove_cv_t<std::remove_reference_t<decltype(std::declval<Type>().*Data)>>;
static_assert(std::is_invocable_v<decltype(Data), Type *>);
auto * const clazz = meta_any{handle}.try_cast<Type>();
if constexpr(std::is_array_v<data_type>) {
auto * const idx = index.try_cast<std::size_t>();
return (clazz && idx) ? dispatch(std::invoke(Data, clazz)[*idx]) : meta_any{};
} else {
return clazz ? dispatch(std::invoke(Data, clazz)) : meta_any{};
}
} else {
static_assert(std::is_pointer_v<std::decay_t<decltype(Data)>>);
if constexpr(std::is_array_v<std::remove_pointer_t<decltype(Data)>>) {
auto * const idx = index.try_cast<std::size_t>();
return idx ? dispatch((*Data)[*idx]) : meta_any{};
} else {
return dispatch(*Data);
}
}
}
template<typename Type, auto Candidate, typename Policy, std::size_t... Indexes>
meta_any invoke([[maybe_unused]] meta_handle handle, meta_any *args, std::index_sequence<Indexes...>) {
using helper_type = meta_function_helper_t<decltype(Candidate)>;
auto dispatch = [](auto *... params) {
if constexpr(std::is_void_v<typename helper_type::return_type> || std::is_same_v<Policy, as_void_t>) {
std::invoke(Candidate, *params...);
return meta_any{std::in_place_type<void>};
} else if constexpr(std::is_same_v<Policy, as_alias_t>) {
return meta_any{std::ref(std::invoke(Candidate, *params...))};
} else {
static_assert(std::is_same_v<Policy, as_is_t>);
return meta_any{std::invoke(Candidate, *params...)};
}
};
[[maybe_unused]] const auto direct = std::make_tuple([](meta_any *any, auto *instance) {
using arg_type = std::remove_reference_t<decltype(*instance)>;
if(!instance && any->convert<arg_type>()) {
instance = any->try_cast<arg_type>();
}
return instance;
}(args+Indexes, (args+Indexes)->try_cast<std::tuple_element_t<Indexes, typename helper_type::args_type>>())...);
if constexpr(std::is_function_v<std::remove_pointer_t<decltype(Candidate)>>) {
return (std::get<Indexes>(direct) && ...) ? dispatch(std::get<Indexes>(direct)...) : meta_any{};
} else {
auto * const clazz = meta_any{handle}.try_cast<Type>();
return (clazz && (std::get<Indexes>(direct) && ...)) ? dispatch(clazz, std::get<Indexes>(direct)...) : meta_any{};
}
}
}
/**
* Internal details not to be documented.
* @endcond TURN_OFF_DOXYGEN
*/
template<typename, typename...>
class extended_meta_factory;
/**
* @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 {
template<typename Node>
bool duplicate(const Node *candidate, const Node *node) ENTT_NOEXCEPT {
return node && (node == candidate || duplicate(candidate, node->next));
}
template<typename Node>
bool duplicate(const ENTT_ID_TYPE identifier, const Node *node) ENTT_NOEXCEPT {
return node && (node->identifier == identifier || duplicate(identifier, node->next));
}
auto record(const ENTT_ID_TYPE identifier) ENTT_NOEXCEPT {
auto * const node = internal::meta_info<Type>::resolve();
ENTT_ASSERT(!duplicate(identifier, *internal::meta_info<>::global));
ENTT_ASSERT(!duplicate(node, *internal::meta_info<>::global));
node->identifier = identifier;
node->next = *internal::meta_info<>::global;
*internal::meta_info<>::global = node;
return extended_meta_factory<Type>{&node->prop};
}
public:
/**
* @brief Extends a meta type by assigning it an identifier.
*
* This function is intended only for unnamed types.
*
* @param identifier Unique identifier.
* @return An extended meta factory for the parent type.
*/
auto type(const ENTT_ID_TYPE identifier) ENTT_NOEXCEPT {
static_assert(!is_named_type_v<Type>);
return record(identifier);
}
/**
* @brief Extends a meta type by assigning it an identifier.
*
* This function is intended only for named types
*
* @return An extended meta factory for the parent type.
*/
auto type() ENTT_NOEXCEPT {
static_assert(is_named_type_v<Type>);
return record(named_type_traits_t<Type>::value);
}
/**
* @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>
auto 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{
type,
nullptr,
&internal::meta_info<Base>::resolve,
[](void *instance) ENTT_NOEXCEPT -> void * {
return static_cast<Base *>(static_cast<Type *>(instance));
}
};
ENTT_ASSERT(!duplicate(&node, type->base));
node.next = type->base;
type->base = &node;
return meta_factory<Type>{};
}
/**
* @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>
auto 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{
type,
nullptr,
&internal::meta_info<To>::resolve,
[](const void *instance) -> meta_any {
return static_cast<To>(*static_cast<const Type *>(instance));
}
};
ENTT_ASSERT(!duplicate(&node, type->conv));
node.next = type->conv;
type->conv = &node;
return meta_factory<Type>{};
}
/**
* @brief Assigns a meta conversion function to a meta type.
*
* Conversion functions can be either free functions or member
* functions.<br/>
* In case of free functions, they must accept a const reference to an
* instance of the parent type as an argument. In case of member functions,
* they should have no arguments at all.
*
* @tparam Candidate The actual function to use for the conversion.
* @return A meta factory for the parent type.
*/
template<auto Candidate>
auto conv() ENTT_NOEXCEPT {
using conv_type = std::invoke_result_t<decltype(Candidate), Type &>;
auto * const type = internal::meta_info<Type>::resolve();
static internal::meta_conv_node node{
type,
nullptr,
&internal::meta_info<conv_type>::resolve,
[](const void *instance) -> meta_any {
return std::invoke(Candidate, *static_cast<const Type *>(instance));
}
};
ENTT_ASSERT(!duplicate(&node, type->conv));
node.next = type->conv;
type->conv = &node;
return meta_factory<Type>{};
}
/**
* @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 Policy Optional policy (no policy set by default).
* @return An extended meta factory for the parent type.
*/
template<auto Func, typename Policy = as_is_t>
auto ctor() ENTT_NOEXCEPT {
using helper_type = internal::meta_function_helper_t<decltype(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{
type,
nullptr,
nullptr,
helper_type::index_sequence.size(),
&helper_type::arg,
[](meta_any * const any) {
return internal::invoke<Type, Func, Policy>({}, any, helper_type::index_sequence);
}
};
ENTT_ASSERT(!duplicate(&node, type->ctor));
node.next = type->ctor;
type->ctor = &node;
return extended_meta_factory<Type, std::integral_constant<decltype(Func), Func>>{&node.prop};
}
/**
* @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.
* @return An extended meta factory for the parent type.
*/
template<typename... Args>
auto ctor() ENTT_NOEXCEPT {
using helper_type = internal::meta_function_helper_t<Type(*)(Args...)>;
auto * const type = internal::meta_info<Type>::resolve();
static internal::meta_ctor_node node{
type,
nullptr,
nullptr,
helper_type::index_sequence.size(),
&helper_type::arg,
[](meta_any * const any) {
return internal::construct<Type, std::remove_cv_t<std::remove_reference_t<Args>>...>(any, helper_type::index_sequence);
}
};
ENTT_ASSERT(!duplicate(&node, type->ctor));
node.next = type->ctor;
type->ctor = &node;
return extended_meta_factory<Type, Type(Args...)>{&node.prop};
}
/**
* @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
*
* The purpose is to give users the ability to free up resources that
* require special treatment before an object is actually destroyed.
*
* @tparam Func The actual function to use as a destructor.
* @return A meta factory for the parent type.
*/
template<auto Func>
auto 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{
type,
[](meta_handle handle) {
const auto valid = (handle.type() == internal::meta_info<Type>::resolve());
if(valid) {
std::invoke(Func, *meta_any{handle}.try_cast<Type>());
}
return valid;
}
};
ENTT_ASSERT(!type->dtor);
type->dtor = &node;
return meta_factory<Type>{};
}
/**
* @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 Policy Optional policy (no policy set by default).
* @param identifier Unique identifier.
* @return An extended meta factory for the parent type.
*/
template<auto Data, typename Policy = as_is_t>
auto data(const ENTT_ID_TYPE identifier) 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_assert(std::is_same_v<Policy, as_is_t>);
static internal::meta_data_node node{
{},
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; }
};
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{
{},
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, Policy>
};
curr = &node;
} else {
static_assert(std::is_pointer_v<std::decay_t<decltype(Data)>>);
using data_type = std::remove_pointer_t<std::decay_t<decltype(Data)>>;
static internal::meta_data_node node{
{},
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, Policy>
};
curr = &node;
}
ENTT_ASSERT(!duplicate(identifier, type->data));
ENTT_ASSERT(!duplicate(curr, type->data));
curr->identifier = identifier;
curr->next = type->data;
type->data = curr;
return extended_meta_factory<Type, std::integral_constant<decltype(Data), Data>>{&curr->prop};
}
/**
* @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 reference 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 Policy Optional policy (no policy set by default).
* @param identifier Unique identifier.
* @return An extended meta factory for the parent type.
*/
template<auto Setter, auto Getter, typename Policy = as_is_t>
auto data(const ENTT_ID_TYPE identifier) ENTT_NOEXCEPT {
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{
{},
type,
nullptr,
nullptr,
false,
false,
&internal::meta_info<underlying_type>::resolve,
&internal::setter<false, Type, Setter>,
&internal::getter<Type, Getter, Policy>
};
ENTT_ASSERT(!duplicate(identifier, type->data));
ENTT_ASSERT(!duplicate(&node, type->data));
node.identifier = identifier;
node.next = type->data;
type->data = &node;
return extended_meta_factory<Type, std::integral_constant<decltype(Setter), Setter>, std::integral_constant<decltype(Getter), Getter>>{&node.prop};
}
/**
* @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 Candidate The actual function to attach to the meta type.
* @tparam Policy Optional policy (no policy set by default).
* @param identifier Unique identifier.
* @return An extended meta factory for the parent type.
*/
template<auto Candidate, typename Policy = as_is_t>
auto func(const ENTT_ID_TYPE identifier) ENTT_NOEXCEPT {
using helper_type = internal::meta_function_helper_t<decltype(Candidate)>;
auto * const type = internal::meta_info<Type>::resolve();
static internal::meta_func_node node{
{},
type,
nullptr,
nullptr,
helper_type::index_sequence.size(),
helper_type::is_const,
!std::is_member_function_pointer_v<decltype(Candidate)>,
&internal::meta_info<std::conditional_t<std::is_same_v<Policy, as_void_t>, void, typename helper_type::return_type>>::resolve,
&helper_type::arg,
[](meta_handle handle, meta_any *any) {
return internal::invoke<Type, Candidate, Policy>(handle, any, helper_type::index_sequence);
}
};
ENTT_ASSERT(!duplicate(identifier, type->func));
ENTT_ASSERT(!duplicate(&node, type->func));
node.identifier = identifier;
node.next = type->func;
type->func = &node;
return extended_meta_factory<Type, std::integral_constant<decltype(Candidate), Candidate>>{&node.prop};
}
/**
* @brief Resets a meta type and all its parts.
*
* This function resets a meta 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 reset but the link between the two types is removed.
*/
void reset() ENTT_NOEXCEPT {
internal::meta_info<Type>::reset();
}
};
/**
* @brief Extended meta factory to be used for reflection purposes.
* @tparam Type Reflected type for which the factory was created.
* @tparam Spec Property specialization pack used to disambiguate overloads.
*/
template<typename Type, typename... Spec>
class extended_meta_factory: public meta_factory<Type> {
bool duplicate(const meta_any &key, const internal::meta_prop_node *node) ENTT_NOEXCEPT {
return node && (node->key() == key || duplicate(key, node->next));
}
template<std::size_t Step = 0, std::size_t... Index, typename... Property, typename... Other>
void unpack(std::index_sequence<Index...>, std::tuple<Property...> property, Other &&... other) {
unroll<Step>(choice<3>, std::move(std::get<Index>(property))..., std::forward<Other>(other)...);
}
template<std::size_t Step = 0, typename... Property, typename... Other>
void unroll(choice_t<3>, std::tuple<Property...> property, Other &&... other) {
unpack<Step>(std::index_sequence_for<Property...>{}, std::move(property), std::forward<Other>(other)...);
}
template<std::size_t Step = 0, typename... Property, typename... Other>
void unroll(choice_t<2>, std::pair<Property...> property, Other &&... other) {
assign<Step>(std::move(property.first), std::move(property.second));
unroll<Step+1>(choice<3>, std::forward<Other>(other)...);
}
template<std::size_t Step = 0, typename Property, typename... Other>
std::enable_if_t<!std::is_invocable_v<Property>>
unroll(choice_t<1>, Property &&property, Other &&... other) {
assign<Step>(std::forward<Property>(property));
unroll<Step+1>(choice<3>, std::forward<Other>(other)...);
}
template<std::size_t Step = 0, typename Func, typename... Other>
void unroll(choice_t<0>, Func &&invocable, Other &&... other) {
unroll<Step>(choice<3>, std::forward<Func>(invocable)(), std::forward<Other>(other)...);
}
template<std::size_t>
void unroll(choice_t<0>) {}
template<std::size_t = 0, typename Key, typename... Value>
void assign(Key &&key, Value &&... value) {
static auto property{std::make_tuple(std::forward<Key>(key), std::forward<Value>(value)...)};
static internal::meta_prop_node node{
nullptr,
[]() -> meta_any {
return std::get<0>(property);
},
[]() -> meta_any {
if constexpr(sizeof...(Value) == 0) {
return {};
} else {
return std::get<1>(property);
}
}
};
ENTT_ASSERT(!duplicate(node.key(), *curr));
node.next = *curr;
*curr = &node;
}
public:
/**
* @brief Constructs an extended factory from a given node.
* @param target The underlying node to which to assign the properties.
*/
extended_meta_factory(entt::internal::meta_prop_node **target)
: curr{target}
{}
/**
* @brief Assigns a property to the last meta object created.
*
* Both the key and the value (if any) must be at least copy constructible.
*
* @tparam PropertyOrKey Type of the property or property key.
* @tparam Value Optional type of the property value.
* @param property_or_key Property or property key.
* @param value Optional property value.
* @return A meta factory for the parent type.
*/
template<typename PropertyOrKey, typename... Value>
auto prop(PropertyOrKey &&property_or_key, Value &&... value) && {
if constexpr(sizeof...(Value) == 0) {
unroll(choice<3>, std::forward<PropertyOrKey>(property_or_key));
} else {
assign(std::forward<PropertyOrKey>(property_or_key), std::forward<Value>(value)...);
}
return extended_meta_factory<Type, Spec..., PropertyOrKey, Value...>{curr};
}
/**
* @brief Assigns properties to the last meta object created.
*
* Both the keys and the values (if any) must be at least copy
* constructible.
*
* @tparam Property Types of the properties.
* @param property Properties to assign to the last meta object created.
* @return A meta factory for the parent type.
*/
template <typename... Property>
auto props(Property... property) && {
unroll(choice<3>, std::forward<Property>(property)...);
return extended_meta_factory<Type, Spec..., Property...>{curr};
}
private:
entt::internal::meta_prop_node **curr{nullptr};
};
/**
* @brief Utility 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 meta objects through a
* dedicated factory.
*
* @tparam Type Type to reflect.
* @return A meta factory for the given type.
*/
template<typename Type>
inline meta_factory<Type> meta() ENTT_NOEXCEPT {
return meta_factory<Type>{};
}
/**
* @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();
}
/**
* @brief Returns the meta type associated with a given identifier.
* @param identifier Unique identifier.
* @return The meta type associated with the given identifier, if any.
*/
inline meta_type resolve(const ENTT_ID_TYPE identifier) ENTT_NOEXCEPT {
return internal::find_if([identifier](auto *node) {
return node->identifier == identifier;
}, *internal::meta_info<>::global);
}
/**
* @brief Iterates all the reflected types.
* @tparam Op Type of the function object to invoke.
* @param op A valid function object.
*/
template<typename Op>
inline std::enable_if_t<std::is_invocable_v<Op, meta_type>, void>
resolve(Op op) ENTT_NOEXCEPT {
internal::iterate<meta_type>(std::move(op), *internal::meta_info<>::global);
}
}
#endif // ENTT_META_FACTORY_HPP

1854
src/entt/meta/meta.hpp Normal file

File diff suppressed because it is too large Load Diff

27
src/entt/meta/policy.hpp Normal file
View File

@@ -0,0 +1,27 @@
#ifndef ENTT_META_POLICY_HPP
#define ENTT_META_POLICY_HPP
namespace entt {
/*! @brief Empty class type used to request the _as alias_ policy. */
struct as_alias_t {};
/*! @brief Disambiguation tag. */
constexpr as_alias_t as_alias;
/*! @brief Empty class type used to request the _as-is_ policy. */
struct as_is_t {};
/*! @brief Empty class type used to request the _as void_ policy. */
struct as_void_t {};
}
#endif // ENTT_META_POLICY_HPP

View File

@@ -2,36 +2,14 @@
#define ENTT_PROCESS_PROCESS_HPP
#include <type_traits>
#include <functional>
#include <utility>
#include <type_traits>
#include "../config/config.h"
namespace entt {
namespace {
struct BaseProcess {
enum class State: unsigned int {
UNINITIALIZED = 0,
RUNNING,
PAUSED,
SUCCEEDED,
FAILED,
ABORTED,
FINISHED
};
template<State state>
using tag = std::integral_constant<State, state>;
};
}
/**
* @brief Base class for processes.
*
@@ -41,33 +19,43 @@ struct BaseProcess {
* required:
*
* * @code{.cpp}
* void update(Delta);
* void update(Delta, void *);
* @endcode
*
* It's invoked once per tick until a process is explicitly aborted or it
* terminates either with or without errors. Even though it's not mandatory to
* declare this member function, as a rule of thumb each process should at
* least define it to work properly.
* least define it to work properly. The `void *` parameter is an opaque
* pointer to user data (if any) forwarded directly to the process during an
* update.
*
* * @code{.cpp}
* void init();
* @endcode
* It's invoked at the first tick, immediately before 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();
* @endcode
*
* It's invoked in case of success, immediately after an update and during the
* same tick.
*
* * @code{.cpp}
* void failed();
* @endcode
*
* It's invoked in case of errors, immediately after an update and during the
* same tick.
*
* * @code{.cpp}
* void aborted();
* @endcode
*
* It's invoked only if a process is explicitly aborted. There is no guarantee
* that it executes in the same tick, this depends solely on whether the
* process is aborted immediately or not.
@@ -76,45 +64,58 @@ struct BaseProcess {
* `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: private BaseProcess {
class process {
enum class state: unsigned int {
UNINITIALIZED = 0,
RUNNING,
PAUSED,
SUCCEEDED,
FAILED,
ABORTED,
FINISHED
};
template<state value>
using state_value_t = std::integral_constant<state, value>;
template<typename Target = Derived>
auto tick(int, tag<State::UNINITIALIZED>)
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)
-> decltype(std::declval<Target>().update(delta)) {
static_cast<Target *>(this)->update(delta);
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&&...) {}
template<state value, typename... Args>
void tick(char, state_value_t<value>, Args &&...) const ENTT_NOEXCEPT {}
protected:
/**
@@ -123,9 +124,9 @@ protected:
* The function is idempotent and it does nothing if the process isn't
* alive.
*/
void succeed() noexcept {
void succeed() ENTT_NOEXCEPT {
if(alive()) {
current = State::SUCCEEDED;
current = state::SUCCEEDED;
}
}
@@ -135,9 +136,9 @@ protected:
* The function is idempotent and it does nothing if the process isn't
* alive.
*/
void fail() noexcept {
void fail() ENTT_NOEXCEPT {
if(alive()) {
current = State::FAILED;
current = state::FAILED;
}
}
@@ -147,9 +148,9 @@ protected:
* The function is idempotent and it does nothing if the process isn't
* running.
*/
void pause() noexcept {
if(current == State::RUNNING) {
current = State::PAUSED;
void pause() ENTT_NOEXCEPT {
if(current == state::RUNNING) {
current = state::PAUSED;
}
}
@@ -159,9 +160,9 @@ protected:
* The function is idempotent and it does nothing if the process isn't
* paused.
*/
void unpause() noexcept {
if(current == State::PAUSED) {
current = State::RUNNING;
void unpause() ENTT_NOEXCEPT {
if(current == state::PAUSED) {
current = state::RUNNING;
}
}
@@ -170,8 +171,8 @@ public:
using delta_type = Delta;
/*! @brief Default destructor. */
~Process() noexcept {
static_assert(std::is_base_of<Process, Derived>::value, "!");
virtual ~process() ENTT_NOEXCEPT {
static_assert(std::is_base_of_v<process, Derived>);
}
/**
@@ -182,12 +183,12 @@ public:
*
* @param immediately Requests an immediate operation.
*/
void abort(bool immediately = false) noexcept {
void abort(const bool immediately = false) ENTT_NOEXCEPT {
if(alive()) {
current = State::ABORTED;
current = state::ABORTED;
if(immediately) {
tick(0);
tick({});
}
}
}
@@ -196,46 +197,48 @@ public:
* @brief Returns true if a process is either running or paused.
* @return True if the process is still alive, false otherwise.
*/
bool alive() const noexcept {
return current == State::RUNNING || current == State::PAUSED;
bool alive() const ENTT_NOEXCEPT {
return current == state::RUNNING || current == state::PAUSED;
}
/**
* @brief Returns true if a process is already terminated.
* @return True if the process is terminated, false otherwise.
*/
bool dead() const noexcept {
return current == State::FINISHED;
bool dead() const ENTT_NOEXCEPT {
return current == state::FINISHED;
}
/**
* @brief Returns true if a process is currently paused.
* @return True if the process is paused, false otherwise.
*/
bool paused() const noexcept {
return current == State::PAUSED;
bool paused() const ENTT_NOEXCEPT {
return current == state::PAUSED;
}
/**
* @brief Returns true if a process terminated with errors.
* @return True if the process terminated with errors, false otherwise.
*/
bool rejected() const noexcept {
bool rejected() const ENTT_NOEXCEPT {
return stopped;
}
/**
* @brief Updates a process and its internal state if required.
* @param delta Elapsed time.
* @param data Optional data.
*/
void tick(Delta delta) {
void tick(const Delta delta, void *data = nullptr) {
switch (current) {
case State::UNINITIALIZED:
tick(0, tag<State::UNINITIALIZED>{});
current = State::RUNNING;
// no break on purpose, tasks are executed immediately
case State::RUNNING:
tick(0, tag<State::RUNNING>{}, delta);
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;
@@ -243,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:
@@ -264,7 +267,7 @@ public:
}
private:
State current{State::UNINITIALIZED};
state current{state::UNINITIALIZED};
bool stopped{false};
};
@@ -281,12 +284,13 @@ private:
* 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.
*
@@ -301,30 +305,31 @@ 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)...}
{}
/**
* @brief Updates a process and its internal state if required.
* @param delta Elapsed time.
* @param data Optional data.
*/
void update(Delta delta) {
Func::operator()(delta, [this](){ this->succeed(); }, [this](){ this->fail(); });
void update(const Delta delta, void *data) {
Func::operator()(delta, data, [this]() { this->succeed(); }, [this]() { this->fail(); });
}
};

View File

@@ -5,9 +5,9 @@
#include <vector>
#include <memory>
#include <utility>
#include <iterator>
#include <algorithm>
#include <type_traits>
#include "../config/config.h"
#include "process.hpp"
@@ -28,69 +28,69 @@ namespace entt {
* Example of use (pseudocode):
*
* @code{.cpp}
* scheduler.attach([](auto delta, auto succeed, auto fail) {
* 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 {
template<typename T>
struct tag { using type = T; };
struct ProcessHandler final {
class scheduler {
struct process_handler {
using instance_type = std::unique_ptr<void, void(*)(void *)>;
using update_type = bool(*)(ProcessHandler &, Delta);
using abort_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_type update;
abort_type abort;
update_fn_type *update;
abort_fn_type *abort;
next_type next;
};
template<typename Lambda>
struct Then final: Lambda {
Then(Lambda &&lambda, ProcessHandler *handler)
: Lambda{std::forward<Lambda>(lambda)}, 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 = Lambda::operator()(handler, tag<Proc>{}, 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, Delta delta) {
static bool update(process_handler &handler, const Delta delta, void *data) {
auto *process = static_cast<Proc *>(handler.instance.get());
process->tick(delta);
process->tick(delta, data);
auto dead = process->dead();
if(dead) {
if(handler.next && !process->rejected()) {
handler = std::move(*handler.next);
dead = handler.update(handler, delta);
// forces the process to exit the uninitialized state
dead = handler.update(handler, {}, nullptr);
} else {
handler.instance.reset();
}
@@ -100,7 +100,7 @@ class Scheduler final {
}
template<typename Proc>
static void abort(ProcessHandler &handler, bool immediately) {
static void abort(process_handler &handler, const bool immediately) {
static_cast<Proc *>(handler.instance.get())->abort(immediately);
}
@@ -109,44 +109,24 @@ class Scheduler final {
delete static_cast<Proc *>(proc);
}
auto then(ProcessHandler *handler) {
auto lambda = [this](ProcessHandler *handler, auto next, auto... args) {
using Proc = typename decltype(next)::type;
if(handler) {
auto proc = typename ProcessHandler::instance_type{ new Proc{std::forward<decltype(args)>(args)...}, &Scheduler::deleter<Proc> };
handler->next.reset(new ProcessHandler{std::move(proc), &Scheduler::update<Proc>, &Scheduler::abort<Proc>, nullptr});
handler = handler->next.get();
}
return handler;
};
return Then<decltype(lambda)>{std::move(lambda), handler};
}
public:
/*! @brief Unsigned integer type. */
using size_type = typename std::vector<ProcessHandler>::size_type;
using size_type = std::size_t;
/*! @brief Default constructor. */
Scheduler() 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 assignament operator. @return This scheduler. */
Scheduler & operator=(Scheduler &&) = default;
/*! @brief Default move assignment operator. @return This scheduler. */
scheduler & operator=(scheduler &&) = default;
/**
* @brief Number of processes currently scheduled.
* @return Number of processes currently scheduled.
*/
size_type size() const noexcept {
size_type size() const ENTT_NOEXCEPT {
return handlers.size();
}
@@ -154,7 +134,7 @@ public:
* @brief Returns true if at least a process is currently scheduled.
* @return True if there are scheduled processes, false otherwise.
*/
bool empty() const noexcept {
bool empty() const ENTT_NOEXCEPT {
return handlers.empty();
}
@@ -179,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, auto succeed, auto fail) {
* .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.
@@ -194,14 +174,13 @@ public:
* @return An opaque object to use to concatenate processes.
*/
template<typename Proc, typename... Args>
auto attach(Args&&... args) {
static_assert(std::is_base_of<Process<Proc, Delta>, Proc>::value, "!");
auto proc = typename ProcessHandler::instance_type{ new Proc{std::forward<Args>(args)...}, &Scheduler::deleter<Proc> };
ProcessHandler handler{std::move(proc), &Scheduler::update<Proc>, &Scheduler::abort<Proc>, nullptr};
handlers.push_back(std::move(handler));
return then(&handlers.back());
auto attach(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>};
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))};
}
/**
@@ -213,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.
*
@@ -237,18 +217,18 @@ public:
*
* @code{.cpp}
* // schedules a task in the form of a lambda function
* scheduler.attach([](auto delta, auto succeed, auto fail) {
* scheduler.attach([](auto delta, void *, auto succeed, auto fail) {
* // code
* })
* // appends a child in the form of another lambda function
* .then([](auto delta, auto succeed, auto fail) {
* .then([](auto delta, void *, auto succeed, auto fail) {
* // 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.
@@ -256,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));
}
@@ -269,18 +249,19 @@ public:
* with its child.
*
* @param delta Elapsed time.
* @param data Optional data.
*/
void update(Delta delta) {
void update(const Delta delta, void *data = nullptr) {
bool clean = false;
for(auto i = handlers.size(); i > 0; --i) {
auto &handler = handlers[i-1];
const bool dead = handler.update(handler, delta);
for(auto pos = handlers.size(); pos; --pos) {
auto &handler = handlers[pos-1];
const bool dead = handler.update(handler, delta, data);
clean = clean || dead;
}
if(clean) {
handlers.erase(std::remove_if(handlers.begin(), handlers.end(), [delta](auto &handler) {
handlers.erase(std::remove_if(handlers.begin(), handlers.end(), [](auto &handler) {
return !handler.instance;
}), handlers.end());
}
@@ -296,7 +277,7 @@ public:
*
* @param immediately Requests an immediate operation.
*/
void abort(bool immediately = false) {
void abort(const bool immediately = false) {
decltype(handlers) exec;
exec.swap(handlers);
@@ -309,7 +290,7 @@ public:
}
private:
std::vector<ProcessHandler> handlers{};
std::vector<process_handler> handlers{};
};

View File

@@ -6,9 +6,10 @@
#include <utility>
#include <type_traits>
#include <unordered_map>
#include "../core/hashed_string.hpp"
#include "../config/config.h"
#include "handle.hpp"
#include "loader.hpp"
#include "fwd.hpp"
namespace entt {
@@ -25,33 +26,28 @@ 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>>;
public:
struct cache {
/*! @brief Unsigned integer type. */
using size_type = typename container_type::size_type;
using size_type = std::size_t;
/*! @brief Type of resources managed by a cache. */
using resource_type = HashedString;
using resource_type = Resource;
/*! @brief Unique identifier type for resources. */
using id_type = ENTT_ID_TYPE;
/*! @brief Default constructor. */
ResourceCache() = default;
cache() = default;
/*! @brief Copying a cache isn't allowed. */
ResourceCache(const ResourceCache &) noexcept = delete;
/*! @brief Default move constructor. */
ResourceCache(ResourceCache &&) noexcept = default;
cache(cache &&) = default;
/*! @brief Copying a cache isn't allowed. @return This cache. */
ResourceCache & operator=(const ResourceCache &) noexcept = delete;
/*! @brief Default move assignment operator. @return This cache. */
ResourceCache & operator=(ResourceCache &&) noexcept = default;
cache & operator=(cache &&) = default;
/**
* @brief Number of resources managed by a cache.
* @return Number of resources currently stored.
*/
size_type size() const noexcept {
size_type size() const ENTT_NOEXCEPT {
return resources.size();
}
@@ -59,7 +55,7 @@ public:
* @brief Returns true if a cache contains no resources, false otherwise.
* @return True if the cache contains no resources, false otherwise.
*/
bool empty() const noexcept {
bool empty() const ENTT_NOEXCEPT {
return resources.empty();
}
@@ -69,12 +65,12 @@ public:
* Handles are not invalidated and the memory used by a resource isn't
* freed as long as at least a handle keeps the resource itself alive.
*/
void clear() noexcept {
void clear() ENTT_NOEXCEPT {
resources.clear();
}
/**
* @brief Loads the resource that corresponds to the given identifier.
* @brief Loads the resource that corresponds to a given identifier.
*
* In case an identifier isn't already present in the cache, it loads its
* resource and stores it aside for future uses. Arguments are forwarded
@@ -85,24 +81,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(resource_type id, Args&&... args) {
static_assert(std::is_base_of<ResourceLoader<Loader, Resource>, Loader>::value, "!");
entt::handle<Resource> load(const id_type id, Args &&... args) {
static_assert(std::is_base_of_v<loader<Loader, Resource>, Loader>);
entt::handle<Resource> resource{};
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 instance = Loader{}.get(std::forward<Args>(args)...); instance) {
resources[id] = instance;
resource = std::move(instance);
}
} else {
resource = it->second;
}
return loaded;
return resource;
}
/**
@@ -118,62 +121,116 @@ 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>
void reload(resource_type id, Args&&... args) {
return (discard(id), load(id, std::forward<Args>(args)...));
entt::handle<Resource> reload(const id_type id, Args &&... args) {
return (discard(id), load<Loader>(id, std::forward<Args>(args)...));
}
/**
* @brief Creates a handle for the given resource identifier.
* @brief Creates a temporary handle for a resource.
*
* Arguments are forwarded directly to the loader in order to construct
* properly the requested resource. The handle isn't stored aside and the
* cache isn't in charge of the lifetime of the resource itself.
*
* @tparam Loader Type of loader to use to load the resource.
* @tparam Args Types of arguments to use to load the resource.
* @param args Arguments to use to load the resource.
* @return A handle for the given resource.
*/
template<typename Loader, typename... Args>
entt::handle<Resource> temp(Args &&... args) const {
return { Loader{}.get(std::forward<Args>(args)...) };
}
/**
* @brief Creates a handle for a given resource identifier.
*
* A resource handle can be in a either valid or invalid state. In other
* terms, a resource handle is properly initialized with a resource if the
* cache contains the resource itself. Otherwise the returned handle is
* uninitialized and accessing it results in undefined behavior.
*
* @sa ResourceHandle
* @sa handle
*
* @param id Unique resource identifier.
* @return A handle for the given resource.
*/
ResourceHandle<Resource> handle(resource_type id) const {
entt::handle<Resource> handle(const id_type id) const {
auto it = resources.find(id);
return { it == resources.end() ? nullptr : it->second };
}
/**
* @brief Checks if a cache contains the given identifier.
* @brief Checks if a cache contains a given identifier.
* @param id Unique resource identifier.
* @return True if the cache contains the resource, false otherwise.
*/
bool contains(resource_type id) const noexcept {
return !(resources.find(id) == resources.cend());
bool contains(const id_type id) const ENTT_NOEXCEPT {
return (resources.find(id) != resources.cend());
}
/**
* @brief Discards the resource that corresponds to the given identifier.
* @brief Discards the resource that corresponds to a given identifier.
*
* Handles are not invalidated and the memory used by the resource isn't
* freed as long as at least a handle keeps the resource itself alive.
*
* @param id Unique resource identifier.
*/
void discard(resource_type id) noexcept {
auto it = resources.find(id);
if(it != resources.end()) {
void discard(const id_type id) ENTT_NOEXCEPT {
if(auto it = resources.find(id); it != resources.end()) {
resources.erase(it);
}
}
/**
* @brief Iterates all resources.
*
* The function object is invoked for each element. It is provided with
* either the resource identifier, the resource handle or both of them.<br/>
* The signature of the function must be equivalent to one of the following
* forms:
*
* @code{.cpp}
* void(const id_type);
* void(handle<Resource>);
* void(const id_type, handle<Resource>);
* @endcode
*
* @tparam Func Type of the function object to invoke.
* @param func A valid function object.
*/
template <typename Func>
void each(Func func) const {
auto begin = resources.begin();
auto end = resources.end();
while(begin != end) {
auto curr = begin++;
if constexpr(std::is_invocable_v<Func, id_type>) {
func(curr->first);
} else if constexpr(std::is_invocable_v<Func, entt::handle<Resource>>) {
func(entt::handle{ curr->second });
} else {
func(curr->first, entt::handle{ curr->second });
}
}
}
private:
container_type resources;
std::unordered_map<id_type, std::shared_ptr<Resource>> resources;
};

24
src/entt/resource/fwd.hpp Normal file
View File

@@ -0,0 +1,24 @@
#ifndef ENTT_RESOURCE_FWD_HPP
#define ENTT_RESOURCE_FWD_HPP
namespace entt {
/*! @struct cache */
template<typename>
struct cache;
/*! @class handle */
template<typename>
class handle;
/*! @class loader */
template<typename, typename>
class loader;
}
#endif // ENTT_RESOURCE_FWD_HPP

View File

@@ -4,16 +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.
*
@@ -27,24 +24,17 @@ class ResourceCache;
* @tparam Resource Type of resource managed by a handle.
*/
template<typename Resource>
class ResourceHandle final {
class handle {
/*! @brief Resource handles are friends of their caches. */
friend class ResourceCache<Resource>;
friend struct cache<Resource>;
ResourceHandle(std::shared_ptr<Resource> res) noexcept
handle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
: resource{std::move(res)}
{}
public:
/*! @brief Default copy constructor. */
ResourceHandle(const ResourceHandle &) noexcept = default;
/*! @brief Default move constructor. */
ResourceHandle(ResourceHandle &&) noexcept = default;
/*! @brief Default copy assignment operator. @return This handle. */
ResourceHandle & operator=(const ResourceHandle &) noexcept = default;
/*! @brief Default move assignment operator. @return This handle. */
ResourceHandle & operator=(ResourceHandle &&) noexcept = default;
/*! @brief Default constructor. */
handle() ENTT_NOEXCEPT = default;
/**
* @brief Gets a reference to the managed resource.
@@ -56,35 +46,30 @@ public:
*
* @return A reference to the managed resource.
*/
const Resource & get() const noexcept {
assert(static_cast<bool>(resource));
const Resource & get() const ENTT_NOEXCEPT {
ENTT_ASSERT(static_cast<bool>(resource));
return *resource;
}
/**
* @brief Casts a handle and gets a reference to the managed resource.
*
* @warning
* The behavior is undefined if the handle doesn't contain a resource.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* handle is empty.
*/
inline operator const Resource & () const noexcept { return get(); }
/*! @copydoc get */
Resource & get() ENTT_NOEXCEPT {
return const_cast<Resource &>(std::as_const(*this).get());
}
/*! @copydoc get */
operator const Resource & () const ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
operator Resource & () ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
const Resource & operator *() const ENTT_NOEXCEPT { return get(); }
/*! @copydoc get */
Resource & operator *() ENTT_NOEXCEPT { return get(); }
/**
* @brief Dereferences a handle to obtain the managed resource.
*
* @warning
* The behavior is undefined if the handle doesn't contain a resource.<br/>
* An assertion will abort the execution at runtime in debug mode if the
* handle is empty.
*
* @return A reference to the managed resource.
*/
inline const Resource & operator *() const noexcept { return get(); }
/**
* @brief Gets a pointer to the managed resource from a handle .
* @brief Gets a pointer to the managed resource.
*
* @warning
* The behavior is undefined if the handle doesn't contain a resource.<br/>
@@ -94,13 +79,19 @@ public:
* @return A pointer to the managed resource or `nullptr` if the handle
* contains no resource at all.
*/
inline const Resource * operator ->() const noexcept {
assert(static_cast<bool>(resource));
const Resource * operator->() const ENTT_NOEXCEPT {
ENTT_ASSERT(static_cast<bool>(resource));
return resource.get();
}
/*! @copydoc operator-> */
Resource * operator->() ENTT_NOEXCEPT {
return const_cast<Resource *>(std::as_const(*this).operator->());
}
/**
* @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); }

View File

@@ -3,31 +3,28 @@
#include <memory>
#include "fwd.hpp"
namespace entt {
template<typename Resource>
class ResourceCache;
/**
* @brief Base class for resource loaders.
*
* Resource loaders must inherit from this class and stay true to the CRTP
* idiom. Moreover, a resource loader must expose a public, const member
* function named `load` that accepts a variable number of arguments and return
* function named `load` that accepts a variable number of arguments and returns
* a shared pointer to the resource just created.<br/>
* As an example:
*
* @code{.cpp}
* struct MyResource {};
* struct my_resource {};
*
* struct MyLoader: entt::ResourceLoader<MyLoader, MyResource> {
* std::shared_ptr<MyResource> load(int) const {
* struct my_loader: entt::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,12 +42,18 @@ class ResourceCache;
* @tparam Resource Type of resource for which to use the loader.
*/
template<typename Loader, typename Resource>
class ResourceLoader {
class loader {
/*! @brief Resource loaders are friends of their caches. */
friend class ResourceCache<Resource>;
friend struct 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 {
std::shared_ptr<Resource> get(Args &&... args) const {
return static_cast<const Loader *>(this)->load(std::forward<Args>(args)...);
}
};

View File

@@ -1,305 +0,0 @@
#ifndef ENTT_SIGNAL_BUS_HPP
#define ENTT_SIGNAL_BUS_HPP
#include <cstddef>
#include <utility>
#include "signal.hpp"
#include "sigh.hpp"
namespace entt {
/**
* @brief Minimal event bus.
*
* Primary template isn't defined on purpose. The main reason for which it
* exists is to work around the doxygen's parsing capabilities. In fact, there
* is no need to declare it actually.
*/
template<template<typename...> class, typename...>
class Bus;
/**
* @brief Event bus specialization for multiple types.
*
* The event bus is designed to allow an easy registration of specific member
* functions to a bunch of signal handlers (either manager or unmanaged).
* Classes must publicly expose the required member functions to allow the bus
* to detect them for the purpose of registering and unregistering
* instances.<br/>
* In particular, for each event type `E`, a matching member function has the
* following signature: `void receive(const E &)`. Events will be properly
* redirected to all the listeners by calling the right member functions, if
* any.
*
* @tparam Sig Type of signal handler to use.
* @tparam Event The list of events managed by the bus.
*/
template<template<typename...> class Sig, typename Event, typename... Other>
class Bus<Sig, Event, Other...>
: private Bus<Sig, Event>, private Bus<Sig, Other>...
{
public:
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/**
* @brief Unregisters all the member functions of an instance.
*
* A bus is used to convey a certain set of events. This method detects
* and unregisters from the bus all the matching member functions of an
* instance.<br/>
* For each event type `E`, a matching member function has the following
* signature: `void receive(const E &)`.
*
* @tparam Instance Type of instance to unregister.
* @param instance A valid instance of the right type.
*/
template<typename Instance>
void unreg(Instance instance) {
using accumulator_type = int[];
accumulator_type accumulator = {
(Bus<Sig, Event>::unreg(instance), 0),
(Bus<Sig, Other>::unreg(instance), 0)...
};
return void(accumulator);
}
/**
* @brief Registers all the member functions of an instance.
*
* A bus is used to convey a certain set of events. This method detects
* and registers to the bus all the matching member functions of an
* instance.<br/>
* For each event type `E`, a matching member function has the following
* signature: `void receive(const E &)`.
*
* @tparam Instance Type of instance to register.
* @param instance A valid instance of the right type.
*/
template<typename Instance>
void reg(Instance instance) {
using accumulator_type = int[];
accumulator_type accumulator = {
(Bus<Sig, Event>::reg(instance), 0),
(Bus<Sig, Other>::reg(instance), 0)...
};
return void(accumulator);
}
/**
* @brief Number of listeners connected to the bus.
* @return Number of listeners currently connected.
*/
size_type size() const noexcept {
using accumulator_type = std::size_t[];
std::size_t sz = Bus<Sig, Event>::size();
accumulator_type accumulator = { sz, (sz += Bus<Sig, Other>::size())... };
return void(accumulator), sz;
}
/**
* @brief Returns false if at least a listener is connected to the bus.
* @return True if the bus has no listeners connected, false otherwise.
*/
bool empty() const noexcept {
using accumulator_type = bool[];
bool ret = Bus<Sig, Event>::empty();
accumulator_type accumulator = { ret, (ret = ret && Bus<Sig, Other>::empty())... };
return void(accumulator), ret;
}
/**
* @brief Connects a free function to the bus.
* @tparam Type Type of event to which to connect the function.
* @tparam Function A valid free function pointer.
*/
template<typename Type, void(*Function)(const Type &)>
void connect() {
Bus<Sig, Type>::template connect<Function>();
}
/**
* @brief Disconnects a free function from the bus.
* @tparam Type Type of event from which to disconnect the function.
* @tparam Function A valid free function pointer.
*/
template<typename Type, void(*Function)(const Type &)>
void disconnect() {
Bus<Sig, Type>::template disconnect<Function>();
}
/**
* @brief Publishes an event.
*
* All the listeners are notified. Order isn't guaranteed.
*
* @tparam Type Type of event to publish.
* @tparam Args Types of arguments to use to construct the event.
* @param args Arguments to use to construct the event.
*/
template<typename Type, typename... Args>
void publish(Args&&... args) {
Bus<Sig, Type>::publish(std::forward<Args>(args)...);
}
};
/**
* @brief Event bus specialization for a single type.
*
* The event bus is designed to allow an easy registration of a specific member
* function to a signal handler (either manager or unmanaged).
* Classes must publicly expose the required member function to allow the bus to
* detect it for the purpose of registering and unregistering instances.<br/>
* In particular, a matching member function has the following signature:
* `void receive(const Event &)`. Events of the given type will be properly
* redirected to all the listeners by calling the right member function, if any.
*
* @tparam Sig Type of signal handler to use.
* @tparam Event Type of event managed by the bus.
*/
template<template<typename...> class Sig, typename Event>
class Bus<Sig, Event> {
using signal_type = Sig<void(const Event &)>;
template<typename Class>
using instance_type = typename signal_type::template instance_type<Class>;
template<typename Class>
auto disconnect(int, instance_type<Class> instance)
-> decltype(std::declval<Class>().receive(std::declval<Event>()), void()) {
signal.template disconnect<Class, &Class::receive>(std::move(instance));
}
template<typename Class>
auto connect(int, instance_type<Class> instance)
-> decltype(std::declval<Class>().receive(std::declval<Event>()), void()) {
signal.template connect<Class, &Class::receive>(std::move(instance));
}
template<typename Class> void disconnect(char, instance_type<Class>) {}
template<typename Class> void connect(char, instance_type<Class>) {}
public:
/*! @brief Unsigned integer type. */
using size_type = typename signal_type::size_type;
/**
* @brief Unregisters member functions of instances.
*
* This method tries to detect and unregister from the bus matching member
* functions of instances.<br/>
* A matching member function has the following signature:
* `void receive(const Event &)`.
*
* @tparam Class Type of instance to unregister.
* @param instance A valid instance of the right type.
*/
template<typename Class>
void unreg(instance_type<Class> instance) {
disconnect(0, std::move(instance));
}
/**
* @brief Tries to register an instance.
*
* This method tries to detect and register to the bus matching member
* functions of instances.<br/>
* A matching member function has the following signature:
* `void receive(const Event &)`.
*
* @tparam Class Type of instance to register.
* @param instance A valid instance of the right type.
*/
template<typename Class>
void reg(instance_type<Class> instance) {
connect(0, std::move(instance));
}
/**
* @brief Number of listeners connected to the bus.
* @return Number of listeners currently connected.
*/
size_type size() const noexcept {
return signal.size();
}
/**
* @brief Returns false if at least a listener is connected to the bus.
* @return True if the bus has no listeners connected, false otherwise.
*/
bool empty() const noexcept {
return signal.empty();
}
/**
* @brief Connects a free function to the bus.
* @tparam Function A valid free function pointer.
*/
template<void(*Function)(const Event &)>
void connect() {
signal.template connect<Function>();
}
/**
* @brief Disconnects a free function from the bus.
* @tparam Function A valid free function pointer.
*/
template<void(*Function)(const Event &)>
void disconnect() {
signal.template disconnect<Function>();
}
/**
* @brief Publishes an event.
*
* All the listeners are notified. Order isn't guaranteed.
*
* @tparam Args Types of arguments to use to construct the event.
* @param args Arguments to use to construct the event.
*/
template<typename... Args>
void publish(Args&&... args) {
signal.publish({ std::forward<Args>(args)... });
}
private:
signal_type signal;
};
/**
* @brief Managed event bus.
*
* A managed event bus uses the Signal class template as an underlying type. The
* type of the instances is the one required by the signal handler:
* `std::shared_ptr<Class>` (a shared pointer).
*
* @tparam Event The list of events managed by the bus.
*/
template<typename... Event>
using ManagedBus = Bus<Signal, Event...>;
/**
* @brief Unmanaged event bus.
*
* An unmanaged event bus uses the SigH class template as an underlying type.
* The type of the instances is the one required by the signal handler:
* `Class *` (a naked pointer).<br/>
* When it comes to work with this kind of bus, users must guarantee that the
* lifetimes of the instances overcome the one of the bus itself.
*
* @tparam Event The list of events managed by the bus.
*/
template<typename... Event>
using UnmanagedBus = Bus<SigH, Event...>;
}
#endif // ENTT_SIGNAL_BUS_HPP

View File

@@ -2,12 +2,80 @@
#define ENTT_SIGNAL_DELEGATE_HPP
#include <tuple>
#include <cstring>
#include <utility>
#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, typename Payload, typename = std::enable_if_t<std::is_convertible_v<const Payload *, const Type *>>>
auto to_function_pointer(Ret(*)(Type &, Args...), const Payload *) -> Ret(*)(Args...);
template<typename Ret, typename... Args, typename Type, typename Payload, typename = std::enable_if_t<std::is_convertible_v<const Payload *, const Type *>>>
auto to_function_pointer(Ret(*)(Type *, Args...), const Payload *) -> Ret(*)(Args...);
template<typename Class, typename Ret, typename... Args>
auto to_function_pointer(Ret(Class:: *)(Args...), const Class *) -> Ret(*)(Args...);
template<typename Class, typename Ret, typename... Args>
auto to_function_pointer(Ret(Class:: *)(Args...) const, const Class *) -> Ret(*)(Args...);
template<typename Class, typename Type>
auto to_function_pointer(Type Class:: *, const Class *) -> Type(*)();
template<typename... Type>
using to_function_pointer_t = decltype(internal::to_function_pointer(std::declval<Type>()...));
template<typename Ret, typename... Args>
constexpr auto index_sequence_for(Ret(*)(Args...)) {
return std::index_sequence_for<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.
*
@@ -15,122 +83,274 @@ 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_type = Ret(*)(void *, Args...);
using stub_type = std::pair<void *, proto_type>;
class delegate<Ret(Args...)> {
using proto_fn_type = Ret(const void *, std::tuple<Args &&...>);
static Ret fallback(void *, Args...) noexcept { return {}; }
template<auto Function, std::size_t... Index>
void connect(std::index_sequence<Index...>) ENTT_NOEXCEPT {
static_assert(std::is_invocable_r_v<Ret, decltype(Function), std::tuple_element_t<Index, std::tuple<Args...>>...>);
data = nullptr;
template<Ret(*Function)(Args...)>
static Ret proto(void *, Args... args) {
return (Function)(args...);
fn = [](const void *, std::tuple<Args &&...> args) -> Ret {
// Ret(...) makes void(...) eat the return values to avoid errors
return Ret(std::invoke(Function, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(args))...));
};
}
template<typename Class, Ret(Class::*Member)(Args...)>
static Ret proto(void *instance, Args... args) {
return (static_cast<Class *>(instance)->*Member)(args...);
template<auto Candidate, typename Type, std::size_t... Index>
void connect(Type &value_or_instance, std::index_sequence<Index...>) ENTT_NOEXCEPT {
static_assert(std::is_invocable_r_v<Ret, decltype(Candidate), Type &, std::tuple_element_t<Index, std::tuple<Args...>>...>);
data = &value_or_instance;
fn = [](const void *payload, std::tuple<Args &&...> args) -> Ret {
Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
// Ret(...) makes void(...) eat the return values to avoid errors
return Ret(std::invoke(Candidate, *curr, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(args))...));
};
}
template<auto Candidate, typename Type, std::size_t... Index>
void connect(Type *value_or_instance, std::index_sequence<Index...>) ENTT_NOEXCEPT {
static_assert(std::is_invocable_r_v<Ret, decltype(Candidate), Type *, std::tuple_element_t<Index, std::tuple<Args...>>...>);
data = value_or_instance;
fn = [](const void *payload, std::tuple<Args &&...> args) -> Ret {
Type *curr = static_cast<Type *>(const_cast<std::conditional_t<std::is_const_v<Type>, const void *, void *>>(payload));
// Ret(...) makes void(...) eat the return values to avoid errors
return Ret(std::invoke(Candidate, curr, std::forward<std::tuple_element_t<Index, std::tuple<Args...>>>(std::get<Index>(args))...));
};
}
public:
/*! @brief Function type of the delegate. */
using function_type = Ret(Args...);
/*! @brief Default constructor. */
Delegate() noexcept
: stub{std::make_pair(nullptr, &fallback)}
delegate() ENTT_NOEXCEPT
: fn{nullptr}, data{nullptr}
{}
/**
* @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...)>
void connect() noexcept {
stub = std::make_pair(nullptr, &proto<Function>);
template<auto Function>
delegate(connect_arg_t<Function>) ENTT_NOEXCEPT
: delegate{}
{
connect<Function>();
}
/**
* @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`.
* @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 reference that fits the purpose.
*/
template<typename Class, Ret(Class::*Member)(Args...)>
void connect(Class *instance) noexcept {
stub = std::make_pair(instance, &proto<Class, Member>);
template<auto Candidate, typename Type>
delegate(connect_arg_t<Candidate>, Type &value_or_instance) ENTT_NOEXCEPT
: delegate{}
{
connect<Candidate>(value_or_instance);
}
/**
* @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 {
connect<Function>(internal::index_sequence_for(internal::to_function_pointer_t<decltype(Function)>{}));
}
/**
* @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 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 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 reference that fits the purpose.
*/
template<auto Candidate, typename Type>
void connect(Type &value_or_instance) ENTT_NOEXCEPT {
connect<Candidate>(value_or_instance, internal::index_sequence_for(internal::to_function_pointer_t<decltype(Candidate), Type *>{}));
}
/**
* @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 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 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>
void connect(Type *value_or_instance) ENTT_NOEXCEPT {
connect<Candidate>(value_or_instance, internal::index_sequence_for(internal::to_function_pointer_t<decltype(Candidate), Type *>{}));
}
/**
* @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() noexcept {
stub = std::make_pair(nullptr, &fallback);
void reset() ENTT_NOEXCEPT {
fn = nullptr;
data = nullptr;
}
/**
* @brief Returns the instance or the payload linked to a delegate, if any.
* @return An opaque pointer to the underlying data.
*/
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) {
return stub.second(stub.first, args...);
Ret operator()(Args... args) const {
ENTT_ASSERT(fn);
return fn(data, std::forward_as_tuple(std::forward<Args>(args)...));
}
/**
* @brief Checks if the contents of the two delegates are different.
*
* Two delegates are identical if they contain the same listener.
*
* @param other Delegate with which to compare.
* @return True if the two delegates are identical, false otherwise.
* @brief Checks whether a delegate actually stores a listener.
* @return False if the delegate is empty, true otherwise.
*/
bool operator==(const Delegate<Ret(Args...)> &other) const noexcept {
return stub.first == other.stub.first && stub.second == other.stub.second;
explicit operator bool() const ENTT_NOEXCEPT {
// no need to test also data
return fn;
}
/**
* @brief Compares the contents of two delegates.
* @param other Delegate with which to compare.
* @return False if the two contents differ, true otherwise.
*/
bool operator==(const delegate<Ret(Args...)> &other) const ENTT_NOEXCEPT {
return fn == other.fn && data == other.data;
}
private:
stub_type stub;
proto_fn_type *fn;
const void *data;
};
/**
* @brief Checks if the contents of the two delegates are different.
*
* Two delegates are identical if they contain the same listener.
*
* @brief Compares the contents of two delegates.
* @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 two contents differ, false otherwise.
*/
template<typename Ret, typename... Args>
bool operator!=(const Delegate<Ret(Args...)> &lhs, const Delegate<Ret(Args...)> &rhs) 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<internal::to_function_pointer_t<decltype(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<internal::to_function_pointer_t<decltype(Candidate), Type *>>>;
/**
* @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<internal::to_function_pointer_t<decltype(Candidate), Type *>>>;
}

View File

@@ -4,10 +4,13 @@
#include <vector>
#include <memory>
#include <cstddef>
#include <utility>
#include <cstdint>
#include <algorithm>
#include <type_traits>
#include "../config/config.h"
#include "../core/family.hpp"
#include "signal.hpp"
#include "../core/type_traits.hpp"
#include "sigh.hpp"
@@ -20,124 +23,135 @@ 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 signature:
* `void(const Event &)`. Member functions named `receive` are automatically
* detected and registered or unregistered by the dispatcher.
* type `Event`, listeners are such that they can be invoked with an argument of
* type `const Event &`, no matter what the return type is.
*
* @tparam Sig Type of the signal handler to use.
* The types of the instances are `Class &`. Users must guarantee that the
* lifetimes of the objects overcome the one of the dispatcher itself to avoid
* crashes.
*/
template<template<typename...> class Sig>
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 Sig<void(const Event &)>::template instance_type<Class>;
using instance_type = typename sigh<void(const Event &)>::template instance_type<Class>;
struct BaseSignalWrapper {
virtual ~BaseSignalWrapper() = default;
virtual void publish(std::size_t) = 0;
struct base_wrapper {
virtual ~base_wrapper() = default;
virtual void publish() = 0;
virtual void clear() = 0;
};
template<typename Event>
struct SignalWrapper final: BaseSignalWrapper {
void publish(std::size_t current) final override {
for(auto &&event: events[current]) {
signal.publish(event);
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 length = events.size();
for(std::size_t pos{}; pos < length; ++pos) {
signal.publish(events[pos]);
}
events[current].clear();
events.erase(events.cbegin(), events.cbegin()+length);
}
template<typename Class, void(Class::*Member)(const Event &)>
inline void connect(instance_type<Class, Event> instance) noexcept {
signal.template connect<Class, Member>(std::move(instance));
void clear() override {
events.clear();
}
template<typename Class, void(Class::*Member)(const Event &)>
inline void disconnect(instance_type<Class, Event> instance) noexcept {
signal.template disconnect<Class, Member>(std::move(instance));
sink_type sink() ENTT_NOEXCEPT {
return entt::sink{signal};
}
template<typename... Args>
inline void trigger(Args&&... args) {
void trigger(Args &&... args) {
signal.publish({ std::forward<Args>(args)... });
}
template<typename... Args>
inline void enqueue(std::size_t current, Args&&... args) {
events[current].push_back({ std::forward<Args>(args)... });
void enqueue(Args &&... args) {
events.emplace_back(std::forward<Args>(args)...);
}
private:
Sig<void(const Event &)> signal{};
std::vector<Event> events[2];
signal_type signal{};
std::vector<Event> events;
};
inline static std::size_t buffer(bool mode) {
return mode ? 0 : 1;
struct wrapper_data {
std::unique_ptr<base_wrapper> wrapper;
ENTT_ID_TYPE runtime_type;
};
template<typename Event>
static auto type() ENTT_NOEXCEPT {
if constexpr(is_named_type_v<Event>) {
return named_type_traits_v<Event>;
} else {
return event_family::type<std::decay_t<Event>>;
}
}
template<typename Event>
SignalWrapper<Event> & wrapper() {
auto type = event_family::type<Event>();
signal_wrapper<Event> & assure() {
const auto wtype = type<Event>();
wrapper_data *wdata = nullptr;
if(!(type < wrappers.size())) {
wrappers.resize(type + 1);
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 Default constructor. */
Dispatcher() noexcept
: wrappers{}, mode{false}
{}
/*! @brief Type of sink for the given event. */
template<typename Event>
using sink_type = typename signal_wrapper<Event>::sink_type;
/**
* @brief Registers a listener given in the form of a member function.
* @brief Returns a sink object for the given event.
*
* A matching member function has the following signature:
* `void receive(const Event &)`. Member functions named `receive` are
* automatically detected and registered if available.
* A sink is an opaque object used to connect listeners to events.
*
* @warning
* Connecting a listener during an update may lead to unexpected behavior.
* Register listeners before or after invoking the update if possible.
* The function type for a listener is:
* @code{.cpp}
* void(const Event &);
* @endcode
*
* @tparam Event Type of event to which to connect the function.
* @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 the right type.
* The order of invocation of the listeners isn't guaranteed.
*
* @sa sink
*
* @tparam Event Type of event of which to get the sink.
* @return A temporary sink object.
*/
template<typename Event, typename Class, void(Class::*Member)(const Event &) = &Class::receive>
void connect(instance_type<Class, Event> instance) noexcept {
wrapper<Event>().template connect<Class, Member>(std::move(instance));
}
/**
* @brief Unregisters a listener given in the form of a member function.
*
* A matching member function has the following signature:
* `void receive(const Event &)`. Member functions named `receive` are
* automatically detected and unregistered if available.
*
* @warning
* Disonnecting a listener during an update may lead to unexpected behavior.
* Unregister listeners before or after invoking the update if possible.
*
* @tparam Event Type of event from which to disconnect the function.
* @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 the right type.
*/
template<typename Event, typename Class, void(Class::*Member)(const Event &) = &Class::receive>
void disconnect(instance_type<Class, Event> instance) noexcept {
wrapper<Event>().template disconnect<Class, Member>(std::move(instance));
template<typename Event>
sink_type<Event> sink() ENTT_NOEXCEPT {
return assure<Event>().sink();
}
/**
@@ -151,8 +165,22 @@ public:
* @param args Arguments to use to construct the event.
*/
template<typename Event, typename... Args>
void trigger(Args&&... args) {
wrapper<Event>().trigger(std::forward<Args>(args)...);
void trigger(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>
void trigger(Event &&event) {
assure<std::decay_t<Event>>().trigger(std::forward<Event>(event));
}
/**
@@ -161,61 +189,84 @@ 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>
void enqueue(Args&&... args) {
wrapper<Event>().enqueue(buffer(mode), std::forward<Args>(args)...);
void enqueue(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>
void enqueue(Event &&event) {
assure<std::decay_t<Event>>().enqueue(std::forward<Event>(event));
}
/**
* @brief Discards all the events queued so far.
*
* If no types are provided, the dispatcher will clear all the existing
* pools.
*
* @tparam Event Type of events to discard.
*/
template<typename... Event>
void discard() {
if constexpr(sizeof...(Event) == 0) {
std::for_each(wrappers.begin(), wrappers.end(), [](auto &&wdata) {
if(wdata.wrapper) {
wdata.wrapper->clear();
}
});
} else {
(assure<std::decay_t<Event>>().clear(), ...);
}
}
/**
* @brief Delivers all the pending events of the given type.
*
* This method is blocking and it doesn't return until all the events are
* delivered to the registered listeners. It's responsibility of the users
* to reduce at a minimum the time spent in the bodies of the listeners.
*
* @tparam Event Type of events to send.
*/
template<typename Event>
void update() {
assure<Event>().publish();
}
/**
* @brief Delivers all the pending events.
*
* This method is blocking and it doesn't return until all the events are
* delivered to the registered listeners. It's responsability of the users
* delivered to the registered listeners. It's responsibility of the users
* to reduce at a minimum the time spent in the bodies of the listeners.
*/
void update() {
auto buf = buffer(mode);
mode = !mode;
for(auto &&wrapper: wrappers) {
if(wrapper) {
wrapper->publish(buf);
void update() const {
for(auto pos = wrappers.size(); pos; --pos) {
if(auto &wdata = wrappers[pos-1]; wdata.wrapper) {
wdata.wrapper->publish();
}
}
}
private:
std::vector<std::unique_ptr<BaseSignalWrapper>> wrappers;
bool mode;
std::vector<wrapper_data> wrappers;
};
/**
* @brief Managed dispatcher.
*
* A managed dispatcher uses the Signal class template as an underlying type.
* The type of the instances is the one required by the signal handler:
* `std::shared_ptr<Class>` (a shared pointer).
*/
using ManagedDispatcher = Dispatcher<Signal>;
/**
* @brief Unmanaged dispatcher.
*
* An unmanaged dispatcher uses the SigH class template as an underlying type.
* The type of the instances is the one required by the signal handler:
* `Class *` (a naked pointer).<br/>
* When it comes to work with this kind of dispatcher, users must guarantee that
* the lifetimes of the instances overcome the one of the dispatcher itself.
*/
using UnmanagedDispatcher = Dispatcher<SigH>;
}

View File

@@ -6,10 +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 {
@@ -21,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/>
@@ -37,59 +39,61 @@ namespace entt {
* @tparam Derived Actual type of emitter that extends the class template.
*/
template<typename Derived>
class Emitter {
struct BaseHandler {
virtual ~BaseHandler() = default;
virtual bool empty() const noexcept = 0;
virtual void clear() noexcept = 0;
class emitter {
using handler_family = family<struct internal_emitter_handler_family>;
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>;
using connection_type = typename container_type::iterator;
bool empty() const noexcept override {
auto pred = [](auto &&element){ return element.first; };
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() noexcept override {
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);
auto func = [](auto &&element) { element.first = true; };
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));
connection_type once(listener_type 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));
connection_type on(listener_type listener) {
return on_list.emplace(on_list.cend(), false, std::move(listener));
}
void erase(connection_type conn) noexcept {
void erase(connection_type conn) ENTT_NOEXCEPT {
conn->first = true;
if(!publishing) {
auto pred = [](auto &&element){ return element.first; };
onceL.remove_if(pred);
onL.remove_if(pred);
auto pred = [](auto &&element) { return element.first; };
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);
@@ -97,50 +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{};
};
static std::size_t next() noexcept {
static std::size_t counter = 0;
return counter++;
}
struct handler_data {
std::unique_ptr<base_handler> handler;
ENTT_ID_TYPE runtime_type;
};
template<typename>
static std::size_t type() noexcept {
static std::size_t value = next();
return value;
template<typename Event>
static auto type() ENTT_NOEXCEPT {
if constexpr(is_named_type_v<Event>) {
return named_type_traits_v<Event>;
} else {
return handler_family::type<std::decay_t<Event>>;
}
}
template<typename Event>
Handler<Event> & handler() noexcept {
std::size_t family = type<Event>();
event_handler<Event> * assure() const ENTT_NOEXCEPT {
const auto htype = type<Event>();
handler_data *hdata = nullptr;
if(!(family < handlers.size())) {
handlers.resize(family+1);
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 type of event. */
/** @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.
@@ -152,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() 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 assignament 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() noexcept = default;
emitter() ENTT_NOEXCEPT = default;
/*! @brief Default destructor. */
virtual ~Emitter() 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 assignament operator. @return This emitter. */
Emitter & operator=(Emitter &&) = default;
/*! @brief Default move assignment operator. @return This emitter. */
emitter & operator=(emitter &&) = default;
/**
* @brief Emits the given event.
@@ -215,8 +219,8 @@ public:
* @param args Parameters to use to initialize the event.
*/
template<typename Event, typename... Args>
void publish(Args&&... args) {
handler<Event>().publish({ std::forward<Args>(args)... }, *static_cast<Derived *>(this));
void publish(Args &&... args) {
assure<Event>()->publish({ std::forward<Args>(args)... }, *static_cast<Derived *>(this));
}
/**
@@ -236,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));
}
/**
@@ -261,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));
}
/**
@@ -279,32 +283,33 @@ public:
* @param conn A valid connection.
*/
template<typename Event>
void erase(Connection<Event> conn) noexcept {
handler<Event>().erase(std::move(conn));
void erase(connection<Event> conn) ENTT_NOEXCEPT {
assure<Event>()->erase(std::move(conn));
}
/**
* @brief Disconnects all the listeners for the given event type.
*
* All the connections previously returned for the given event are
* invalidated. Using them results in undefined behaviour.
* invalidated. Using them results in undefined behavior.
*
* @tparam Event Type of event to reset.
*/
template<typename Event>
void clear() noexcept {
handler<Event>().clear();
void clear() ENTT_NOEXCEPT {
assure<Event>()->clear();
}
/**
* @brief Disconnects all the listeners.
*
* All the connections previously returned are invalidated. Using them
* results in undefined behaviour.
* results in undefined behavior.
*/
void clear() noexcept {
std::for_each(handlers.begin(), handlers.end(),
[](auto &&handler){ if(handler) { handler->clear(); } });
void clear() ENTT_NOEXCEPT {
std::for_each(handlers.begin(), handlers.end(), [](auto &&hdata) {
return hdata.handler ? hdata.handler->clear() : void();
});
}
/**
@@ -313,25 +318,22 @@ public:
* @return True if there are no listeners registered, false otherwise.
*/
template<typename Event>
bool empty() const noexcept {
std::size_t family = type<Event>();
return (!(family < handlers.size()) ||
!handlers[family] ||
static_cast<Handler<Event> &>(*handlers[family]).empty());
bool empty() const ENTT_NOEXCEPT {
return assure<Event>()->empty();
}
/**
* @brief Checks if there are listeners registered with the event emitter.
* @return True if there are no listeners registered, false otherwise.
*/
bool empty() const noexcept {
return std::all_of(handlers.cbegin(), handlers.cend(),
[](auto &&handler){ return !handler || handler->empty(); });
bool empty() const ENTT_NOEXCEPT {
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{};
};

24
src/entt/signal/fwd.hpp Normal file
View File

@@ -0,0 +1,24 @@
#ifndef ENTT_SIGNAL_FWD_HPP
#define ENTT_SIGNAL_FWD_HPP
namespace entt {
/*! @class delegate */
template<typename>
class delegate;
/*! @class sink */
template<typename>
class sink;
/*! @class sigh */
template<typename>
class sigh;
}
#endif // ENTT_SIGNAL_FWD_HPP

View File

@@ -2,129 +2,69 @@
#define ENTT_SIGNAL_SIGH_HPP
#include <algorithm>
#include <utility>
#include <vector>
#include <utility>
#include <iterator>
#include <algorithm>
#include <functional>
#include <type_traits>
#include "../config/config.h"
#include "delegate.hpp"
#include "fwd.hpp"
namespace entt {
namespace {
template<typename, typename>
struct Invoker;
template<typename Ret, typename... Args, typename Collector>
struct Invoker<Ret(Args...), Collector> {
using proto_type = Ret(*)(void *, Args...);
using call_type = std::pair<void *, proto_type>;
virtual ~Invoker() = default;
template<typename SFINAE = Ret>
typename std::enable_if<std::is_void<SFINAE>::value, bool>::type
invoke(Collector &, proto_type proto, void *instance, Args... args) {
proto(instance, args...);
return true;
}
template<typename SFINAE = Ret>
typename std::enable_if<!std::is_void<SFINAE>::value, bool>::type
invoke(Collector &collector, proto_type proto, void *instance, Args... args) {
return collector(proto(instance, args...));
}
};
template<typename Ret>
struct NullCollector final {
using result_type = Ret;
bool operator()(result_type) const noexcept { return true; }
};
template<>
struct NullCollector<void> final {
using result_type = void;
bool operator()() const noexcept { return true; }
};
template<typename>
struct DefaultCollector;
template<typename Ret, typename... Args>
struct DefaultCollector<Ret(Args...)> final {
using collector_type = NullCollector<Ret>;
};
template<typename Function>
using DefaultCollectorType = typename DefaultCollector<Function>::collector_type;
}
/**
* @brief Unmanaged signal handler declaration.
* @brief Sink class.
*
* Primary template isn't defined on purpose. All the specializations give a
* compile-time error unless the template parameter is a function type.
*
* @tparam Function A valid function type.
* @tparam Collector Type of collector to use, if any.
*/
template<typename Function, typename Collector = DefaultCollectorType<Function>>
class SigH;
template<typename Function>
class sink;
/**
* @brief Unmanaged signal handler definition.
* @brief Unmanaged signal handler.
*
* Unmanaged signal handler. It works directly with naked pointers to classes
* and pointers to member functions as well as pointers to free functions. Users
* of this class are in charge of disconnecting instances before deleting them.
* Primary template isn't defined on purpose. All the specializations give a
* compile-time error unless the template parameter is a function type.
*
* @tparam Function A valid function type.
*/
template<typename Function>
class sigh;
/**
* @brief Unmanaged signal handler.
*
* It works directly with references to classes and pointers to member functions
* as well as pointers to free functions. Users of this class are in charge of
* disconnecting instances before deleting them.
*
* This class serves mainly two purposes:
*
* * Creating signals used later to notify a bunch of listeners.
* * Creating signals to use later to notify a bunch of listeners.
* * Collecting results from a set of functions like in a voting system.
*
* The default collector does nothing. To properly collect data, define and use
* a class that has a call operator the signature of which is `bool(Param)` and:
*
* * `Param` is a type to which `Ret` can be converted.
* * The return type is true if the handler must stop collecting data, false
* otherwise.
*
* @tparam Ret Return type of a function type.
* @tparam Args Types of arguments of a function type.
* @tparam Collector Type of collector to use, if any.
*/
template<typename Ret, typename... Args, typename Collector>
class SigH<Ret(Args...), Collector> final: private Invoker<Ret(Args...), Collector> {
using typename Invoker<Ret(Args...), Collector>::call_type;
template<Ret(*Function)(Args...)>
static Ret proto(void *, Args... args) {
return (Function)(args...);
}
template<typename Class, Ret(Class::*Member)(Args... args)>
static Ret proto(void *instance, Args... args) {
return (static_cast<Class *>(instance)->*Member)(args...);
}
template<typename Ret, typename... Args>
class sigh<Ret(Args...)> {
/*! @brief A sink is allowed to modify a signal. */
friend class sink<Ret(Args...)>;
public:
/*! @brief Unsigned integer type. */
using size_type = typename std::vector<call_type>::size_type;
/*! @brief Collector type. */
using collector_type = Collector;
using size_type = std::size_t;
/*! @brief Sink type. */
using sink_type = entt::sink<Ret(Args...)>;
/**
* @brief Instance type when it comes to connecting member functions.
@@ -137,7 +77,7 @@ public:
* @brief Number of listeners connected to the signal.
* @return Number of listeners currently connected.
*/
size_type size() const noexcept {
size_type size() const ENTT_NOEXCEPT {
return calls.size();
}
@@ -145,82 +85,10 @@ public:
* @brief Returns false if at least a listener is connected to the signal.
* @return True if the signal has no listeners connected, false otherwise.
*/
bool empty() const noexcept {
bool empty() const ENTT_NOEXCEPT {
return calls.empty();
}
/**
* @brief Disconnects all the listeners from a signal.
*/
void clear() noexcept {
calls.clear();
}
/**
* @brief Connects a free function to a signal.
*
* The signal handler performs checks to avoid multiple connections for free
* functions.
*
* @tparam Function A valid free function pointer.
*/
template<Ret(*Function)(Args...)>
void connect() {
disconnect<Function>();
calls.emplace_back(nullptr, &proto<Function>);
}
/**
* @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...)>
void connect(instance_type<Class> instance) {
disconnect<Class, Member>(instance);
calls.emplace_back(instance, &proto<Class, Member>);
}
/**
* @brief Disconnects a free function from a signal.
* @tparam Function A valid free function pointer.
*/
template<Ret(*Function)(Args...)>
void disconnect() {
call_type target{nullptr, &proto<Function>};
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(instance_type<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.
* @tparam Class Type of class to which the member function belongs.
* @param instance A valid instance of type pointer to `Class`.
*/
template<typename Class>
void disconnect(instance_type<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());
}
/**
* @brief Triggers a signal.
*
@@ -228,73 +96,520 @@ public:
*
* @param args Arguments to use to invoke listeners.
*/
void publish(Args... args) {
for(auto &&call: calls) {
call.second(call.first, args...);
}
void publish(Args... args) const {
std::for_each(calls.cbegin(), calls.cend(), [&args...](auto &&call) {
call(args...);
});
}
/**
* @brief Collects return values from the listeners.
*
* The collector must expose a call operator with the following properties:
*
* * The return type is either `void` or such that it's convertible to
* `bool`. In the second case, a true value will stop the iteration.
* * The list of parameters is empty if `Ret` is `void`, otherwise it
* contains a single element such that `Ret` is convertible to it.
*
* @tparam Func Type of collector to use, if any.
* @param func A valid function object.
* @param args Arguments to use to invoke listeners.
* @return An instance of the collector filled with collected data.
*/
collector_type collect(Args... args) {
collector_type collector;
template<typename Func>
void collect(Func func, Args... args) const {
for(auto &&call: calls) {
if(!this->invoke(collector, call.second, call.first, args...)) {
break;
if constexpr(std::is_void_v<Ret>) {
if constexpr(std::is_invocable_r_v<bool, Func>) {
call(args...);
if(func()) { break; }
} else {
call(args...);
func();
}
} else {
if constexpr(std::is_invocable_r_v<bool, Func, Ret>) {
if(func(call(args...))) { break; }
} else {
func(call(args...));
}
}
}
return collector;
}
/**
* @brief Swaps listeners between the two signals.
* @param lhs A valid signal object.
* @param rhs A valid signal object.
*/
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 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.
* @brief Connection class.
*
* Two signals are identical if they have the same size and the same
* listeners registered exactly in the same order.
* Opaque object the aim of which is to allow users to release an already
* estabilished connection without having to keep a reference to the signal or
* the sink that generated it.
*/
class connection {
/*! @brief A sink is allowed to create connection objects. */
template<typename>
friend class sink;
connection(delegate<void(void *)> fn, void *ref)
: disconnect{fn}, signal{ref}
{}
public:
/*! @brief Default constructor. */
connection() = default;
/*! @brief Default copy constructor. */
connection(const connection &) = default;
/**
* @brief Default move constructor.
* @param other The instance to move from.
*/
connection(connection &&other)
: connection{}
{
std::swap(disconnect, other.disconnect);
std::swap(signal, other.signal);
}
/*! @brief Default copy assignment operator. @return This connection. */
connection & operator=(const connection &) = default;
/**
* @brief Default move assignment operator.
* @param other The instance to move from.
* @return This connection.
*/
connection & operator=(connection &&other) {
if(this != &other) {
auto tmp{std::move(other)};
disconnect = tmp.disconnect;
signal = tmp.signal;
}
return *this;
}
/**
* @brief Checks whether a connection is properly initialized.
* @return True if the connection is properly initialized, false otherwise.
*/
explicit operator bool() const ENTT_NOEXCEPT {
return static_cast<bool>(disconnect);
}
/*! @brief Breaks the connection. */
void release() {
if(disconnect) {
disconnect(signal);
disconnect.reset();
}
}
private:
delegate<void(void *)> disconnect;
void *signal{};
};
/**
* @brief Scoped connection class.
*
* Opaque object the aim of which is to allow users to release an already
* estabilished connection without having to keep a reference to the signal or
* the sink that generated it.<br/>
* A scoped connection automatically breaks the link between the two objects
* when it goes out of scope.
*/
struct scoped_connection: private connection {
using connection::operator bool;
using connection::release;
/*! @brief Default constructor. */
scoped_connection() = default;
/**
* @brief Constructs a scoped connection from a basic connection.
* @param conn A valid connection object.
*/
scoped_connection(const connection &conn)
: connection{conn}
{}
/*! @brief Default copy constructor, deleted on purpose. */
scoped_connection(const scoped_connection &) = delete;
/*! @brief Default move constructor. */
scoped_connection(scoped_connection &&) = default;
/*! @brief Automatically breaks the link on destruction. */
~scoped_connection() {
connection::release();
}
/**
* @brief Default copy assignment operator, deleted on purpose.
* @return This scoped connection.
*/
scoped_connection & operator=(const scoped_connection &) = delete;
/**
* @brief Default move assignment operator.
* @return This scoped connection.
*/
scoped_connection & operator=(scoped_connection &&) = default;
/**
* @brief Copies a connection.
* @param other The connection object to copy.
* @return This scoped connection.
*/
scoped_connection & operator=(const connection &other) {
static_cast<connection &>(*this) = other;
return *this;
}
/**
* @brief Moves a connection.
* @param other The connection object to move.
* @return This scoped connection.
*/
scoped_connection & operator=(connection &&other) {
static_cast<connection &>(*this) = std::move(other);
return *this;
}
};
/**
* @brief Sink class.
*
* A sink is used to connect listeners to signals and to disconnect them.<br/>
* 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 the class.
*
* @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) noexcept {
return !(lhs == rhs);
}
class sink<Ret(Args...)> {
using signal_type = sigh<Ret(Args...)>;
using difference_type = typename std::iterator_traits<typename decltype(signal_type::calls)::iterator>::difference_type;
template<auto Candidate, typename Type>
static void release(Type value_or_instance, void *signal) {
sink{*static_cast<signal_type *>(signal)}.disconnect<Candidate>(value_or_instance);
}
template<auto Function>
static void release(void *signal) {
sink{*static_cast<signal_type *>(signal)}.disconnect<Function>();
}
public:
/**
* @brief Constructs a sink that is allowed to modify a given signal.
* @param ref A valid reference to a signal object.
*/
sink(sigh<Ret(Args...)> &ref) ENTT_NOEXCEPT
: offset{},
signal{&ref}
{}
/**
* @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 signal->calls.empty();
}
/**
* @brief Returns a sink that connects before a given function.
* @tparam Function A valid free function pointer.
* @return A properly initialized sink object.
*/
template<auto Function>
sink before() {
delegate<Ret(Args...)> call{};
call.template connect<Function>();
const auto &calls = signal->calls;
const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
sink other{*this};
other.offset = std::distance(it, calls.cend());
return other;
}
/**
* @brief Returns a sink that connects before a given member function or
* free function with payload.
* @tparam Candidate Member or free function to look for.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid reference that fits the purpose.
* @return A properly initialized sink object.
*/
template<auto Candidate, typename Type>
sink before(Type &value_or_instance) {
delegate<Ret(Args...)> call{};
call.template connect<Candidate>(value_or_instance);
const auto &calls = signal->calls;
const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
sink other{*this};
other.offset = std::distance(it, calls.cend());
return other;
}
/**
* @brief Returns a sink that connects before a given member function or
* free function with payload.
* @tparam Candidate Member or free function to look for.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid pointer that fits the purpose.
* @return A properly initialized sink object.
*/
template<auto Candidate, typename Type>
sink before(Type *value_or_instance) {
delegate<Ret(Args...)> call{};
call.template connect<Candidate>(value_or_instance);
const auto &calls = signal->calls;
const auto it = std::find(calls.cbegin(), calls.cend(), std::move(call));
sink other{*this};
other.offset = std::distance(it, calls.cend());
return other;
}
/**
* @brief Returns a sink that connects before a given instance or specific
* payload.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid object that fits the purpose.
* @return A properly initialized sink object.
*/
template<typename Type>
sink before(Type &value_or_instance) {
return before(&value_or_instance);
}
/**
* @brief Returns a sink that connects before a given instance or specific
* payload.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid pointer that fits the purpose.
* @return A properly initialized sink object.
*/
template<typename Type>
sink before(Type *value_or_instance) {
sink other{*this};
if(value_or_instance) {
const auto &calls = signal->calls;
const auto it = std::find_if(calls.cbegin(), calls.cend(), [value_or_instance](const auto &delegate) {
return delegate.instance() == value_or_instance;
});
other.offset = std::distance(it, calls.cend());
}
return other;
}
/**
* @brief Returns a sink that connects before anything else.
* @return A properly initialized sink object.
*/
sink before() {
sink other{*this};
other.offset = signal->calls.size();
return other;
}
/**
* @brief Connects a free function to a signal.
*
* The signal handler performs checks to avoid multiple connections for free
* functions.
*
* @tparam Function A valid free function pointer.
* @return A properly initialized connection object.
*/
template<auto Function>
connection connect() {
disconnect<Function>();
delegate<Ret(Args...)> call{};
call.template connect<Function>();
signal->calls.insert(signal->calls.end() - offset, std::move(call));
delegate<void(void *)> conn{};
conn.template connect<&release<Function>>();
return { std::move(conn), signal };
}
/**
* @brief Connects a member function or a free function with payload to a
* signal.
*
* 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 Candidate Member or free function to connect to the signal.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid reference that fits the purpose.
* @return A properly initialized connection object.
*/
template<auto Candidate, typename Type>
connection connect(Type &value_or_instance) {
disconnect<Candidate>(value_or_instance);
delegate<Ret(Args...)> call{};
call.template connect<Candidate>(value_or_instance);
signal->calls.insert(signal->calls.end() - offset, std::move(call));
delegate<void(void *)> conn{};
conn.template connect<&release<Candidate, Type &>>(value_or_instance);
return { std::move(conn), signal };
}
/**
* @brief Connects a member function or a free function with payload to a
* signal.
*
* 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 Candidate Member or free function to connect to the signal.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid pointer that fits the purpose.
* @return A properly initialized connection object.
*/
template<auto Candidate, typename Type>
connection connect(Type *value_or_instance) {
disconnect<Candidate>(value_or_instance);
delegate<Ret(Args...)> call{};
call.template connect<Candidate>(value_or_instance);
signal->calls.insert(signal->calls.end() - offset, std::move(call));
delegate<void(void *)> conn{};
conn.template connect<&release<Candidate, Type *>>(value_or_instance);
return { std::move(conn), signal };
}
/**
* @brief Disconnects a free function from a signal.
* @tparam Function A valid free function pointer.
*/
template<auto Function>
void disconnect() {
auto &calls = signal->calls;
delegate<Ret(Args...)> call{};
call.template connect<Function>();
calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
}
/**
* @brief Disconnects a member function or a free function with payload from
* a signal.
* @tparam Candidate Member or free function to disconnect from the signal.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid reference that fits the purpose.
*/
template<auto Candidate, typename Type>
void disconnect(Type &value_or_instance) {
auto &calls = signal->calls;
delegate<Ret(Args...)> call{};
call.template connect<Candidate>(value_or_instance);
calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
}
/**
* @brief Disconnects a member function or a free function with payload from
* a signal.
* @tparam Candidate Member or free function to disconnect from the signal.
* @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>
void disconnect(Type *value_or_instance) {
auto &calls = signal->calls;
delegate<Ret(Args...)> call{};
call.template connect<Candidate>(value_or_instance);
calls.erase(std::remove(calls.begin(), calls.end(), std::move(call)), calls.end());
}
/**
* @brief Disconnects member functions or free functions based on an
* instance or specific payload.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid object that fits the purpose.
*/
template<typename Type>
void disconnect(Type &value_or_instance) {
disconnect(&value_or_instance);
}
/**
* @brief Disconnects member functions or free functions based on an
* instance or specific payload.
* @tparam Type Type of class or type of payload.
* @param value_or_instance A valid pointer that fits the purpose.
*/
template<typename Type>
void disconnect(Type *value_or_instance) {
if(value_or_instance) {
auto &calls = signal->calls;
calls.erase(std::remove_if(calls.begin(), calls.end(), [value_or_instance](const auto &delegate) {
return delegate.instance() == value_or_instance;
}), calls.end());
}
}
/*! @brief Disconnects all the listeners from a signal. */
void disconnect() {
signal->calls.clear();
}
private:
difference_type offset;
signal_type *signal;
};
/**
* @brief Deduction guide.
*
* It allows to deduce the function type of a sink directly from the signal it
* refers to.
*
* @tparam Ret Return type of a function type.
* @tparam Args Types of arguments of a function type.
*/
template<typename Ret, typename... Args>
sink(sigh<Ret(Args...)> &) ENTT_NOEXCEPT -> sink<Ret(Args...)>;
}

View File

@@ -1,227 +0,0 @@
#ifndef ENTT_SIGNAL_SIGNAL_HPP
#define ENTT_SIGNAL_SIGNAL_HPP
#include <memory>
#include <vector>
#include <utility>
#include <cstdint>
#include <iterator>
#include <algorithm>
namespace entt {
/**
* @brief Managed signal handler declaration.
*
* Primary template isn't defined on purpose. All the specializations give a
* compile-time error unless the template parameter is a function type.
*/
template<typename>
class Signal;
/**
* @brief Managed signal handler definition.
*
* Managed signal handler. It works with weak pointers to classes and pointers
* to member functions as well as pointers to free functions. References are
* automatically removed when the instances to which they point are freed.
*
* This class can be used to create signals used later to notify a bunch of
* listeners.
*
* @tparam Args Types of arguments of a function type.
*/
template<typename... Args>
class Signal<void(Args...)> final {
using proto_type = bool(*)(std::weak_ptr<void> &, Args...);
using call_type = std::pair<std::weak_ptr<void>, proto_type>;
template<void(*Function)(Args...)>
static bool proto(std::weak_ptr<void> &, Args... args) {
Function(args...);
return true;
}
template<typename Class, void(Class::*Member)(Args...)>
static bool proto(std::weak_ptr<void> &wptr, Args... args) {
bool ret = false;
if(!wptr.expired()) {
auto ptr = std::static_pointer_cast<Class>(wptr.lock());
(ptr.get()->*Member)(args...);
ret = true;
}
return ret;
}
public:
/*! @brief Unsigned integer type. */
using size_type = std::size_t;
/**
* @brief Instance type when it comes to connecting member functions.
* @tparam Class Type of class to which the member function belongs.
*/
template<typename Class>
using instance_type = std::shared_ptr<Class>;
/**
* @brief Number of listeners connected to the signal.
* @return Number of listeners currently connected.
*/
size_type size() const noexcept {
return calls.size();
}
/**
* @brief Returns false if at least a listener is connected to the signal.
* @return True if the signal has no listeners connected, false otherwise.
*/
bool empty() const noexcept {
return calls.empty();
}
/**
* @brief Disconnects all the listeners from a signal.
*/
void clear() noexcept {
calls.clear();
}
/**
* @brief Connects a free function to a signal.
*
* The signal handler performs checks to avoid multiple connections for free
* functions.
*
* @tparam Function A valid free function pointer.
*/
template<void(*Function)(Args...)>
void connect() {
disconnect<Function>();
calls.emplace_back(std::weak_ptr<void>{}, &proto<Function>);
}
/**
* @brief Connects a member function for a given instance to a signal.
*
* 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, void(Class::*Member)(Args...)>
void connect(instance_type<Class> instance) {
disconnect<Class, Member>(instance);
calls.emplace_back(std::move(instance), &proto<Class, Member>);
}
/**
* @brief Disconnects a free function from a signal.
* @tparam Function A valid free function pointer.
*/
template<void(*Function)(Args...)>
void disconnect() {
calls.erase(std::remove_if(calls.begin(), calls.end(),
[](const call_type &call) { return call.second == &proto<Function> && !call.first.lock(); }
), 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, void(Class::*Member)(Args...)>
void disconnect(instance_type<Class> instance) {
calls.erase(std::remove_if(calls.begin(), calls.end(),
[instance{std::move(instance)}](const call_type &call) { return call.second == &proto<Class, Member> && call.first.lock() == instance; }
), calls.end());
}
/**
* @brief Removes all existing connections for the given instance.
* @tparam Class Type of class to which the member function belongs.
* @param instance A valid instance of type pointer to `Class`.
*/
template<typename Class>
void disconnect(instance_type<Class> instance) {
calls.erase(std::remove_if(calls.begin(), calls.end(),
[instance{std::move(instance)}](const call_type &call) { return call.first.lock() == instance; }
), calls.end());
}
/**
* @brief Triggers a signal.
*
* All the listeners are notified. Order isn't guaranteed.
*
* @param args Arguments to use to invoke listeners.
*/
void publish(Args... args) {
for(auto it = calls.rbegin(), end = calls.rend(); it != end; it++) {
if(!(it->second)(it->first, args...)) {
calls.erase(std::next(it).base());
}
}
}
/**
* @brief Swaps listeners between the two signals.
* @param lhs A valid signal object.
* @param rhs A valid signal object.
*/
friend void swap(Signal &lhs, Signal &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 Signal &other) const noexcept {
return std::equal(calls.cbegin(), calls.cend(), other.calls.cbegin(), other.calls.cend(), [](const auto &lhs, const auto &rhs) {
return (lhs.second == rhs.second) && (lhs.first.lock() == rhs.first.lock());
});
}
private:
std::vector<call_type> 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 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... Args>
bool operator!=(const Signal<void(Args...)> &lhs, const Signal<void(Args...)> &rhs) noexcept {
return !(lhs == rhs);
}
}
#endif // ENTT_SIGNAL_SIGNAL_HPP

0
test/BUILD.bazel Normal file
View File

View File

@@ -2,89 +2,128 @@
# Tests configuration
#
include_directories(${PROJECT_SRC_DIR})
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 -Wshadow>)
target_compile_options(${LIB_TARGET} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc /W2>)
endmacro()
add_library(odr OBJECT odr.cpp)
SETUP_LIBRARY_TARGET(odr)
macro(SETUP_AND_ADD_TEST TEST_NAME TEST_SOURCE)
add_executable(${TEST_NAME} $<TARGET_OBJECTS:odr> ${TEST_SOURCE})
set_target_properties(${TEST_NAME} PROPERTIES CXX_EXTENSIONS OFF)
target_link_libraries(${TEST_NAME} PRIVATE EnTT GTest::Main Threads::Threads)
target_compile_definitions(${TEST_NAME} PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_DEFINITIONS>)
target_compile_features(${TEST_NAME} PRIVATE $<TARGET_PROPERTY:EnTT,INTERFACE_COMPILE_FEATURES>)
target_compile_options(${TEST_NAME} PRIVATE $<$<NOT:$<CXX_COMPILER_ID:MSVC>>:-pedantic -Wall -Wshadow>)
target_compile_options(${TEST_NAME} PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/EHsc /W2>)
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
endmacro()
# Test benchmark
if(CMAKE_BUILD_TYPE MATCHES Release)
add_executable(
benchmark
$<TARGET_OBJECTS:odr>
entt/entity/benchmark.cpp
)
target_link_libraries(benchmark PRIVATE gtest_main Threads::Threads)
add_test(NAME benchmark COMMAND benchmark)
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)
set(DUKTAPE_DEPS_DIR ${EnTT_SOURCE_DIR}/deps/duktape)
configure_file(${EnTT_SOURCE_DIR}/cmake/in/duktape.in ${DUKTAPE_DEPS_DIR}/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${DUKTAPE_DEPS_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${DUKTAPE_DEPS_DIR})
set(DUKTAPE_SRC_DIR ${DUKTAPE_DEPS_DIR}/src/src)
set(MOD_TEST_SOURCE ${DUKTAPE_SRC_DIR}/duktape.c mod/mod.cpp)
SETUP_AND_ADD_TEST(mod "${MOD_TEST_SOURCE}")
target_include_directories(mod PRIVATE ${DUKTAPE_SRC_DIR})
endif()
# Test snapshot
if(BUILD_SNAPSHOT)
set(CEREAL_DEPS_DIR ${EnTT_SOURCE_DIR}/deps/cereal)
configure_file(${EnTT_SOURCE_DIR}/cmake/in/cereal.in ${CEREAL_DEPS_DIR}/CMakeLists.txt)
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . WORKING_DIRECTORY ${CEREAL_DEPS_DIR})
execute_process(COMMAND ${CMAKE_COMMAND} --build . WORKING_DIRECTORY ${CEREAL_DEPS_DIR})
set(CEREAL_SRC_DIR ${CEREAL_DEPS_DIR}/src/include)
SETUP_AND_ADD_TEST(cereal snapshot/snapshot.cpp)
target_include_directories(cereal PRIVATE ${CEREAL_SRC_DIR})
endif()
# Test core
add_executable(
core
$<TARGET_OBJECTS:odr>
entt/core/family.cpp
entt/core/hashed_string.cpp
entt/core/ident.cpp
)
target_link_libraries(core PRIVATE gtest_main Threads::Threads)
add_test(NAME core COMMAND core)
SETUP_AND_ADD_TEST(algorithm entt/core/algorithm.cpp)
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
add_executable(
entity
$<TARGET_OBJECTS:odr>
entt/entity/actor.cpp
entt/entity/registry.cpp
entt/entity/sparse_set.cpp
entt/entity/view.cpp
)
target_link_libraries(entity PRIVATE gtest_main Threads::Threads)
add_test(NAME entity COMMAND entity)
SETUP_AND_ADD_TEST(actor entt/entity/actor.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(observer entt/entity/observer.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
add_executable(
locator
$<TARGET_OBJECTS:odr>
entt/locator/locator.cpp
)
target_link_libraries(locator PRIVATE gtest_main Threads::Threads)
add_test(NAME locator COMMAND locator)
SETUP_AND_ADD_TEST(locator entt/locator/locator.cpp)
# Test meta
SETUP_AND_ADD_TEST(meta entt/meta/meta.cpp)
# Test process
add_executable(
process
$<TARGET_OBJECTS:odr>
entt/process/process.cpp
entt/process/scheduler.cpp
)
target_link_libraries(process PRIVATE gtest_main Threads::Threads)
add_test(NAME process COMMAND process)
SETUP_AND_ADD_TEST(process entt/process/process.cpp)
SETUP_AND_ADD_TEST(scheduler entt/process/scheduler.cpp)
# Test resource
add_executable(
resource
$<TARGET_OBJECTS:odr>
entt/resource/resource.cpp
)
target_link_libraries(resource PRIVATE gtest_main Threads::Threads)
add_test(NAME resource COMMAND resource)
SETUP_AND_ADD_TEST(resource entt/resource/resource.cpp)
# Test signal
add_executable(
signal
$<TARGET_OBJECTS:odr>
entt/signal/bus.cpp
entt/signal/delegate.cpp
entt/signal/dispatcher.cpp
entt/signal/emitter.cpp
entt/signal/sigh.cpp
entt/signal/signal.cpp
)
target_link_libraries(signal PRIVATE gtest_main Threads::Threads)
add_test(NAME signal COMMAND signal)
SETUP_AND_ADD_TEST(delegate entt/signal/delegate.cpp)
SETUP_AND_ADD_TEST(dispatcher entt/signal/dispatcher.cpp)
SETUP_AND_ADD_TEST(emitter entt/signal/emitter.cpp)
SETUP_AND_ADD_TEST(sigh entt/signal/sigh.cpp)

View File

@@ -0,0 +1,11 @@
load("//bazel:copts.bzl", "entt_copts")
cc_binary(
name = "benchmark",
srcs = ["benchmark.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)

1085
test/benchmark/benchmark.cpp Normal file

File diff suppressed because it is too large Load Diff

0
test/entt/BUILD.bazel Normal file
View File

View File

@@ -0,0 +1,71 @@
load("//bazel:copts.bzl", "entt_copts")
cc_test(
name = "algorithm",
srcs = ["algorithm.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "family",
srcs = ["family.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "hashed_string",
srcs = ["hashed_string.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "ident",
srcs = ["ident.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "monostate",
srcs = ["monostate.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "type_traits",
srcs = ["type_traits.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)
cc_test(
name = "utility",
srcs = ["utility.cpp"],
copts = entt_copts,
deps = [
"//:entt",
"@com_google_googletest//:gtest_main",
],
)

View File

@@ -0,0 +1,97 @@
#include <array>
#include <gtest/gtest.h>
#include <entt/core/algorithm.hpp>
struct boxed_int {
int value;
};
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::std_sort sort;
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, StdSortBoxedInt) {
// well, I'm pretty sure it works, it's std::sort!!
std::array<boxed_int, 6> arr{{{4}, {1}, {3}, {2}, {0}, {6}}};
entt::std_sort sort;
sort(arr.begin(), arr.end(), [](const auto &lhs, const auto &rhs) {
return lhs.value > rhs.value;
});
for(typename decltype(arr)::size_type i = 0; i < (arr.size() - 1); ++i) {
ASSERT_GT(arr[i].value, arr[i+1].value);
}
}
TEST(Algorithm, InsertionSort) {
std::array<int, 5> arr{{4, 1, 3, 2, 0}};
entt::insertion_sort sort;
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, InsertionSortBoxedInt) {
std::array<boxed_int, 6> arr{{{4}, {1}, {3}, {2}, {0}, {6}}};
entt::insertion_sort sort;
sort(arr.begin(), arr.end(), [](const auto &lhs, const auto &rhs) {
return lhs.value > rhs.value;
});
for(typename decltype(arr)::size_type i = 0; i < (arr.size() - 1); ++i) {
ASSERT_GT(arr[i].value, arr[i+1].value);
}
}
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());
}
TEST(Algorithm, RadixSort) {
std::array<uint32_t, 5> arr{{4, 1, 3, 2, 0}};
entt::radix_sort<8, 32> sort;
sort(arr.begin(), arr.end(), [](const auto &value) {
return value;
});
for(typename decltype(arr)::size_type i = 0; i < (arr.size() - 1); ++i) {
ASSERT_LT(arr[i], arr[i+1]);
}
}
TEST(Algorithm, RadixSortBoxedInt) {
std::array<boxed_int, 6> arr{{{4}, {1}, {3}, {2}, {0}, {6}}};
entt::radix_sort<2, 6> sort;
sort(arr.rbegin(), arr.rend(), [](const auto &instance) {
return instance.value;
});
for(typename decltype(arr)::size_type i = 0; i < (arr.size() - 1); ++i) {
ASSERT_GT(arr[i].value, arr[i+1].value);
}
}
TEST(Algorithm, RadixSortEmptyContainer) {
std::vector<int> vec{};
entt::radix_sort<8, 32> sort;
// this should crash with asan enabled if we break the constraint
sort(vec.begin(), vec.end());
}

View File

@@ -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_NE(a_family::type<int>, a_family::type<int &>);
ASSERT_NE(a_family::type<int>, a_family::type<int &&>);
ASSERT_NE(a_family::type<int>, a_family::type<const int &>);
}

View File

@@ -1,37 +1,128 @@
#include <string>
#include <string_view>
#include <type_traits>
#include <gtest/gtest.h>
#include <entt/core/hashed_string.hpp>
constexpr bool check(const char *str) {
using hash_type = entt::HashedString::hash_type;
return (static_cast<hash_type>(entt::HashedString{str}) == entt::HashedString{str}
&& static_cast<const char *>(entt::HashedString{str}) == str
&& entt::HashedString{str} == entt::HashedString{str}
&& !(entt::HashedString{str} != entt::HashedString{str}));
}
TEST(HashedString, Constexprness) {
// how would you test a constepxr otherwise?
static_assert(check("foobar"), "!");
ASSERT_TRUE(true);
}
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_EQ(static_cast<const char *>(fooHs), "foo");
ASSERT_EQ(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_TRUE(fooHs == fooHs);
ASSERT_FALSE(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(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::hashed_string::hash_type;
// how would you test a constexpr otherwise?
(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);
}
TEST(HashedWString, Functionalities) {
using hash_type = entt::hashed_wstring::hash_type;
const wchar_t *bar = L"bar";
auto foo_hws = entt::hashed_wstring{L"foo"};
auto bar_hws = entt::hashed_wstring{bar};
ASSERT_NE(static_cast<hash_type>(foo_hws), static_cast<hash_type>(bar_hws));
ASSERT_STREQ(static_cast<const wchar_t *>(foo_hws), L"foo");
ASSERT_STREQ(static_cast<const wchar_t *>(bar_hws), bar);
ASSERT_STREQ(foo_hws.data(), L"foo");
ASSERT_STREQ(bar_hws.data(), bar);
ASSERT_EQ(foo_hws, foo_hws);
ASSERT_NE(foo_hws, bar_hws);
entt::hashed_wstring hws{L"foobar"};
ASSERT_EQ(static_cast<hash_type>(hws), 0xbf9cf968);
ASSERT_EQ(hws.value(), 0xbf9cf968);
ASSERT_EQ(foo_hws, L"foo"_hws);
ASSERT_NE(bar_hws, L"foo"_hws);
}
TEST(HashedWString, Empty) {
using hash_type = entt::hashed_wstring::hash_type;
entt::hashed_wstring hws{};
ASSERT_EQ(static_cast<hash_type>(hws), hash_type{});
ASSERT_EQ(static_cast<const wchar_t *>(hws), nullptr);
}
TEST(HashedWString, Constexprness) {
using hash_type = entt::hashed_wstring::hash_type;
// how would you test a constexpr otherwise?
(void)std::integral_constant<hash_type, entt::hashed_wstring{L"quux"}>{};
(void)std::integral_constant<hash_type, L"quux"_hws>{};
ASSERT_TRUE(true);
}
TEST(HashedWString, ToValue) {
using hash_type = entt::hashed_wstring::hash_type;
const wchar_t *foobar = L"foobar";
ASSERT_EQ(entt::hashed_wstring::to_value(foobar), 0xbf9cf968);
// how would you test a constexpr otherwise?
(void)std::integral_constant<hash_type, entt::hashed_wstring::to_value(L"quux")>{};
}
TEST(HashedWString, StringView) {
std::wstring str{L"__foobar__"};
std::wstring_view view{str.data()+2, 6};
ASSERT_EQ(entt::hashed_wstring::to_value(view.data(), view.size()), 0xbf9cf968);
}
TEST(BasicHashedString, DeductionGuide) {
static_assert(std::is_same_v<decltype(entt::basic_hashed_string{"foo"}), entt::hashed_string>);
static_assert(std::is_same_v<decltype(entt::basic_hashed_string{L"foo"}), entt::hashed_wstring>);
}

View File

@@ -2,32 +2,31 @@
#include <gtest/gtest.h>
#include <entt/core/ident.hpp>
struct A {};
struct B {};
struct a_type {};
struct another_type {};
TEST(Identifier, Uniqueness) {
constexpr auto ID = entt::ident<A, B>;
constexpr A a;
constexpr B b;
using id = entt::identifier<a_type, another_type>;
constexpr a_type an_instance;
constexpr another_type another_instance;
ASSERT_NE(ID.get<A>(), ID.get<B>());
ASSERT_EQ(ID.get<A>(), ID.get<decltype(a)>());
ASSERT_NE(ID.get<A>(), ID.get<decltype(b)>());
ASSERT_EQ(ID.get<A>(), ID.get<A>());
ASSERT_EQ(ID.get<B>(), ID.get<B>());
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<B>()) {
case ID.get<A>():
switch(id::type<another_type>) {
case id::type<a_type>:
FAIL();
break;
case ID.get<B>():
case id::type<another_type>:
SUCCEED();
}
}
TEST(Identifier, SingleType) {
constexpr auto ID = entt::ident<A>;
std::integral_constant<decltype(ID)::identifier_type, ID.get()> ic;
using id = entt::identifier<a_type>;
std::integral_constant<id::identifier_type, id::type<a_type>> ic;
(void)ic;
}

Some files were not shown because too many files have changed in this diff Show More