Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b4f3b6f7bd | ||
|
|
71b464f44a | ||
|
|
438070ed58 | ||
|
|
a06c891969 | ||
|
|
a935bd09aa | ||
|
|
fb8745ccf0 | ||
|
|
53a4c4be7f | ||
|
|
c0a110ea8a | ||
|
|
c426a8e331 | ||
|
|
526e4f69a4 | ||
|
|
f901fa50ff | ||
|
|
bea9eeac16 | ||
|
|
3055da5316 |
26
.travis.yml
26
.travis.yml
@@ -11,6 +11,13 @@ matrix:
|
||||
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:
|
||||
@@ -18,23 +25,34 @@ matrix:
|
||||
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++-6']
|
||||
packages: ['g++-7']
|
||||
env:
|
||||
- COMPILER=g++-6
|
||||
- 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-6 --gcov-options '\-lp' --root ${TRAVIS_BUILD_DIR} --build-root ${TRAVIS_BUILD_DIR}/build --extension cpp --extension hpp --exclude deps --include src
|
||||
- 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:
|
||||
@@ -51,5 +69,5 @@ install:
|
||||
|
||||
script:
|
||||
- mkdir -p build && cd build
|
||||
- cmake -DCMAKE_BUILD_TYPE=Release .. && make -j4
|
||||
- cmake .. && make -j4
|
||||
- CTEST_OUTPUT_ON_FAILURE=1 make test
|
||||
|
||||
@@ -16,7 +16,7 @@ endif()
|
||||
# Project configuration
|
||||
#
|
||||
|
||||
project(entt VERSION 2.0.0)
|
||||
project(entt VERSION 2.1.0)
|
||||
|
||||
if(NOT CMAKE_BUILD_TYPE)
|
||||
set(CMAKE_BUILD_TYPE Debug)
|
||||
|
||||
209
README.md
209
README.md
@@ -1,4 +1,4 @@
|
||||
# The EnTT Framework
|
||||
# EnTT Framework
|
||||
|
||||
[](https://travis-ci.org/skypjack/entt)
|
||||
[](https://ci.appveyor.com/project/skypjack/entt)
|
||||
@@ -9,22 +9,66 @@
|
||||
|
||||
`EnTT` is a header-only, tiny and easy to use framework written in modern
|
||||
C++.<br/>
|
||||
It's entirely designed around an architectural pattern pattern called _ECS_ that
|
||||
is used mostly in game development. For further details:
|
||||
It was originally designed entirely around an architectural pattern called _ECS_
|
||||
that is used mostly in game development. For further details:
|
||||
|
||||
* [Entity Systems Wiki](http://entity-systems.wikidot.com/)
|
||||
* [Evolve Your Hierarchy](http://cowboyprogramming.com/2007/01/05/evolve-your-heirachy/)
|
||||
* [ECS on Wikipedia](https://en.wikipedia.org/wiki/Entity%E2%80%93component%E2%80%93system)
|
||||
|
||||
Originally, `EnTT` was written as a faster alternative to other well known and
|
||||
open source entity-component systems.<br/>
|
||||
After a while the codebase has grown and more features have become part of the
|
||||
framework.
|
||||
A long time ago, the sole entity-component system was part of the project. After
|
||||
a while the codebase has grown and more and more classes have become part
|
||||
of the repository.<br/>
|
||||
That's why today it's called _the EnTT Framework_.
|
||||
|
||||
## The framework
|
||||
|
||||
`EnTT` was written initially as a faster alternative to other well known and
|
||||
open source entity-component systems. Nowadays the `EnTT` framework is moving
|
||||
its first steps. Much more will come in the future and hopefully I'm going to
|
||||
work on it for a long time.<br/>
|
||||
Requests for feature, PR, suggestions ad feedback are highly appreciated.
|
||||
|
||||
If you find you can help me and want to contribute to the `EnTT` framework with
|
||||
your experience or you do want to get part of the project for some other
|
||||
reason, feel free to contact me directly (you can find the mail in the
|
||||
[profile](https://github.com/skypjack)).<br/>
|
||||
I can't promise that each and every contribution will be accepted, but I can
|
||||
assure that I'll do my best to take them all seriously.
|
||||
|
||||
### State of the art
|
||||
|
||||
Here is a brief list of what it offers today:
|
||||
|
||||
* Statically generated integer identifiers for types (assigned either at
|
||||
compile-time or at runtime).
|
||||
* An incredibly fast entity-component system based on sparse sets, with its own
|
||||
views and a _pay for what you use_ policy to adjust performance and memory
|
||||
pressure according to the users' requirements.
|
||||
* Signal handlers of any type, delegates and an event bus.
|
||||
* A general purpose event emitter, that is a CRTP idiom based class template.
|
||||
* An event dispatcher for immediate and delayed events to integrate in loops.
|
||||
* The smallest and most basic implementation of a service locator ever seen.
|
||||
* ...
|
||||
* Any other business.
|
||||
|
||||
Consider it a work in progress. For more details and an updated list, please
|
||||
refer to the [online documentation](https://skypjack.github.io/entt/).
|
||||
|
||||
### A note about the README
|
||||
|
||||
The README file stays true to the original project and it describes only the
|
||||
entity-component system. However, the whole API is fully documented in-code and
|
||||
the [online documentation](https://skypjack.github.io/entt/) contains much
|
||||
more.<br/>
|
||||
Continue reading to know how the core part of the project works or follow the
|
||||
link above to take a look at the API reference for all other available classes.
|
||||
|
||||
## Code Example
|
||||
|
||||
```cpp
|
||||
#include <registry.hpp>
|
||||
#include <entt/entt.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
struct Position {
|
||||
float x;
|
||||
@@ -37,25 +81,43 @@ struct Velocity {
|
||||
};
|
||||
|
||||
void update(entt::DefaultRegistry ®istry) {
|
||||
auto view = ecs.view<Position, Velocity>();
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
// gets only the components that are going to be used ...
|
||||
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
|
||||
velocity.dx = 0.;
|
||||
velocity.dy = 0.;
|
||||
|
||||
// ...
|
||||
}
|
||||
}
|
||||
|
||||
void update(std::uint64_t dt, entt::DefaultRegistry ®istry) {
|
||||
registry.view<Position, Velocity>().each([dt](auto entity, auto &position, auto &velocity) {
|
||||
// gets all the components of the view at once ...
|
||||
|
||||
position.x += velocity.dx * dt;
|
||||
position.y += velocity.dy * dt;
|
||||
|
||||
// ...
|
||||
});
|
||||
}
|
||||
|
||||
int main() {
|
||||
entt::DefaultRegistry 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);
|
||||
auto entity = registry.create(Position{i * 1.f, i * 1.f});
|
||||
if(i % 2 == 0) { registry.assign<Velocity>(entity, i * .1f, i * .1f); }
|
||||
}
|
||||
|
||||
update(dt, registry);
|
||||
update(registry);
|
||||
|
||||
// ...
|
||||
}
|
||||
```
|
||||
@@ -71,8 +133,9 @@ realized it, I tried hard to keep intact the great performance of `EnTT` and to
|
||||
add all the features I wanted to see in *my* entity-component system at the same
|
||||
time.
|
||||
|
||||
Today `EnTT` is finally what I was looking for: still faster than its _rivals_,
|
||||
a really good API and an amazing set of features. And even more, of course.
|
||||
Today `EnTT` is finally what I was looking for: still faster than its
|
||||
_competitors_, a really good API and an amazing set of features. And even more,
|
||||
of course.
|
||||
|
||||
## Performance
|
||||
|
||||
@@ -96,9 +159,6 @@ Dell XPS 13 out of the mid 2014):
|
||||
| Iterating over 10M entities, unpacking ten components, standard view, half of the entities have all the components | **0.00899859s** | 0.200752s |
|
||||
| Iterating over 10M entities, unpacking ten components, standard view, one of the entities has all the components | 0.00700349s | **2.565e-06s** |
|
||||
| Iterating over 10M entities, unpacking ten components, persistent view | 0.0104708s | **6.23e-07s** |
|
||||
| Iterating over 50M entities, unpacking one component, standard view | 0.055194s | **2.87e-07s** |
|
||||
| Iterating over 50M entities, unpacking two components, standard view | **0.0533921s** | 0.243197s |
|
||||
| Iterating over 50M entities, unpacking two components, persistent view | 0.055194s | **4.47e-07s** |
|
||||
| Sort 150k entities, one component | - | **0.0080046s** |
|
||||
| Sort 150k entities, match two components | - | **0.00608322s** |
|
||||
|
||||
@@ -185,6 +245,26 @@ Benchmarks are compiled only in release mode currently.
|
||||
|
||||
# Crash Course
|
||||
|
||||
## Design choices
|
||||
|
||||
`EnTT` is entirely designed around the principle that users have to pay only for
|
||||
what they want.
|
||||
|
||||
When it comes to use an entity-componet system, the tradeoff is usually between
|
||||
performance and memory usage. The faster it is, the more memory it uses.
|
||||
However, slightly worse performance along non-critical paths are the right price
|
||||
to pay to reduce memory usage and I've always wondered why this kind of tools do
|
||||
not leave me the choice.<br/>
|
||||
`EnTT` follows a completely different approach. It squezees the best from the
|
||||
basic data structures and gives users the possibility to pay more for higher
|
||||
performance where needed.<br/>
|
||||
The disadvantage of this approach is that users need to know the systems they
|
||||
are working on and the tools they are using. Otherwise, the risk to ruin the
|
||||
performance along critical paths is high.
|
||||
|
||||
So far, this choice has proved to be a good one and I really hope it can be for
|
||||
many others besides me.
|
||||
|
||||
## Vademecum
|
||||
|
||||
The `Registry` to store, the `View`s to iterate. That's all.
|
||||
@@ -197,17 +277,18 @@ functionalities to query them out-of-the-box. The underlying type of an entity
|
||||
when defining a registry (actually the DefaultRegistry is nothing more than a
|
||||
Registry where the type of the entities is `std::uint32_t`).<br/>
|
||||
Components (the _C_ of an _ECS_) should be plain old data structures or more
|
||||
complex and moveable data structures with a proper constructor. They are list
|
||||
initialized by using the parameters provided to construct the component. No need
|
||||
to register components or their types neither with the registry nor with the
|
||||
entity-component system at all.<br/>
|
||||
complex and moveable data structures with a proper constructor. Actually, the
|
||||
sole requirement of a component type is that it must be both move constructible
|
||||
and move assignable. They are list initialized by using the parameters provided
|
||||
to construct the component itself. No need to register components or their types
|
||||
neither with the registry nor with the entity-component system at all.<br/>
|
||||
Systems (the _S_ of an _ECS_) are just plain functions, functors, lambdas or
|
||||
whatever the users want. They can accept a Registry, a View or a PersistentView
|
||||
and use them the way they prefer. No need to register systems or their types
|
||||
neither with the registry nor with the entity-component system at all.
|
||||
|
||||
The following sections will explain in short how to use the entity-component
|
||||
system, the core part of the `EnTT` framework.<br/>
|
||||
system, the core part of the whole framework.<br/>
|
||||
In fact, the framework is composed of many other classes in addition to those
|
||||
describe below. For more details, please refer to the
|
||||
[online documentation](https://skypjack.github.io/entt/).
|
||||
@@ -289,8 +370,8 @@ velocity.dy = 0.;
|
||||
|
||||
In case users want to assign a component to an entity, but it's unknown whether
|
||||
the entity already has it or not, `accomodate` does the work in a single call
|
||||
(of course, there is a performance penalty to pay for that mainly due to the
|
||||
fact that it must check if `entity` already has the given component or not):
|
||||
(there is a performance penalty to pay for that mainly due to the fact that it
|
||||
must check if `entity` already has the given component or not):
|
||||
|
||||
```cpp
|
||||
registry.accomodate<Position>(entity, 0., 0.);
|
||||
@@ -370,7 +451,8 @@ entity stored in the underlying data structures of the registry.
|
||||
|
||||
### Sorting: is it possible?
|
||||
|
||||
Of course, sorting entities and components is possible with `EnTT`.<br/>
|
||||
It goes without saying that sorting entities and components is possible with
|
||||
`EnTT`.<br/>
|
||||
In fact, there are two functions that respond to slightly different needs:
|
||||
|
||||
* Components can be sorted directly:
|
||||
@@ -440,8 +522,9 @@ To sum up and as a rule of thumb, use a standard view:
|
||||
|
||||
Use a persistent view in all the other cases.
|
||||
|
||||
To easily iterate entities, all the views offer _C++-ish_ `begin` and `end`
|
||||
member functions that allow users to use them in a typical range-for loop.<br/>
|
||||
To easily iterate entities, all the views offer the common `begin` and `end`
|
||||
member functions that allow users to use a view in a typical range-for
|
||||
loop.<br/>
|
||||
Continue reading for more details or refer to the
|
||||
[official documentation](https://skypjack.github.io/entt/).
|
||||
|
||||
@@ -477,7 +560,7 @@ There is no need to store views around for they are extremely cheap to
|
||||
construct, even though they can be copied without problems and reused
|
||||
freely. In fact, they return newly created and correctly initialized iterators
|
||||
whenever `begin` or `end` are invoked.<br/>
|
||||
To iterate a single component standard view, just use it in range-for:
|
||||
To iterate a single component standard view, either use it in range-for loop:
|
||||
|
||||
```cpp
|
||||
auto view = registry.view<Renderable>();
|
||||
@@ -489,8 +572,21 @@ for(auto entity: view) {
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: prefer the `get` member function of the view instead of the `get`
|
||||
member function template of the registry during iterations.
|
||||
Or rely on the `each` member function to iterate entities and get all their
|
||||
components at once:
|
||||
|
||||
```cpp
|
||||
registry.view<Renderable>().each([](auto entity, auto &renderable) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
Performance are more or less the same. The best approach depends mainly on
|
||||
whether all the components have to be accessed or not.
|
||||
|
||||
**Note**: prefer the `get` member function of a view instead of the `get` member
|
||||
function template of a registry during iterations, if possible. However, keep in
|
||||
mind that it works only with the components of the view itself.
|
||||
|
||||
#### Multi component standard view
|
||||
|
||||
@@ -508,7 +604,7 @@ There is no need to store views around for they are extremely cheap to
|
||||
construct, even though they can be copied without problems and reused
|
||||
freely. In fact, they return newly created and correctly initialized iterators
|
||||
whenever `begin` or `end` are invoked.<br/>
|
||||
To iterate a multi component standard view, just use it in range-for:
|
||||
To iterate a multi component standard view, either use it in range-for loop:
|
||||
|
||||
```cpp
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
@@ -521,8 +617,21 @@ for(auto entity: view) {
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: prefer the `get` member function template of the view instead of the
|
||||
`get` member function template of the registry during iterations.
|
||||
Or rely on the `each` member function to iterate entities and get all their
|
||||
components at once:
|
||||
|
||||
```cpp
|
||||
registry.view<Position, Velocity>().each([](auto entity, auto &position, auto &velocity) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
Performance are more or less the same. The best approach depends mainly on
|
||||
whether all the components have to be accessed or not.
|
||||
|
||||
**Note**: prefer the `get` member function of a view instead of the `get` member
|
||||
function template of a registry during iterations, if possible. However, keep in
|
||||
mind that it works only with the components of the view itself.
|
||||
|
||||
### Persistent View
|
||||
|
||||
@@ -563,7 +672,7 @@ of the components for which it has been constructed.<br/>
|
||||
Refer to the [official documentation](https://skypjack.github.io/entt/) for all
|
||||
the details.
|
||||
|
||||
To iterate a persistent view, just use it in range-for:
|
||||
To iterate a persistent view, either use it in range-for loop:
|
||||
|
||||
```cpp
|
||||
auto view = registry.persistent<Position, Velocity>();
|
||||
@@ -576,8 +685,21 @@ for(auto entity: view) {
|
||||
}
|
||||
```
|
||||
|
||||
**Note**: prefer the `get` member function template of the view instead of the
|
||||
`get` member function template of the registry during iterations.
|
||||
Or rely on the `each` member function to iterate entities and get all their
|
||||
components at once:
|
||||
|
||||
```cpp
|
||||
registry.persistent<Position, Velocity>().each([](auto entity, auto &position, auto &velocity) {
|
||||
// ...
|
||||
});
|
||||
```
|
||||
|
||||
Performance are more or less the same. The best approach depends mainly on
|
||||
whether all the components have to be accessed or not.
|
||||
|
||||
**Note**: prefer the `get` member function of a view instead of the `get` member
|
||||
function template of a registry during iterations, if possible. However, keep in
|
||||
mind that it works only with the components of the view itself.
|
||||
|
||||
## Side notes
|
||||
|
||||
@@ -625,21 +747,6 @@ for(auto entity: view) {
|
||||
renderable entities while updating a physic component concurrently on a
|
||||
separate thread if needed.
|
||||
|
||||
## What else?
|
||||
|
||||
The `EnTT` framework is moving its first steps. More and more will come in the
|
||||
future and hopefully I'm going to work on it for a long time.<br/>
|
||||
Here is a brief list of what it offers today:
|
||||
|
||||
* Statically generated integer identifiers for types.
|
||||
* An entity-component system based on sparse sets.
|
||||
* Signal handlers and event emitters of any type.
|
||||
* ...
|
||||
* Any other business.
|
||||
|
||||
Consider it a work in progress. For more details and an updated list, please
|
||||
refer to the [online documentation](https://skypjack.github.io/entt/).
|
||||
|
||||
# Contributors
|
||||
|
||||
If you want to contribute, please send patches as pull requests against the
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
|
||||
#include<type_traits>
|
||||
#include<cstddef>
|
||||
#include<utility>
|
||||
|
||||
|
||||
namespace entt {
|
||||
@@ -24,15 +23,23 @@ class Family {
|
||||
return value++;
|
||||
}
|
||||
|
||||
template<typename...>
|
||||
static std::size_t family() noexcept {
|
||||
static const std::size_t value = identifier();
|
||||
return value;
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Unsigned integer type. */
|
||||
using family_type = std::size_t;
|
||||
|
||||
/**
|
||||
* @brief Returns an unique identifier for the given type.
|
||||
* @return Statically generated unique identifier for the given type.
|
||||
*/
|
||||
template<typename...>
|
||||
static std::size_t type() noexcept {
|
||||
static const std::size_t value = identifier();
|
||||
return value;
|
||||
template<typename... Type>
|
||||
static family_type type() noexcept {
|
||||
return family<std::decay_t<Type>...>();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,21 +13,37 @@ namespace entt {
|
||||
namespace {
|
||||
|
||||
|
||||
template<typename Type>
|
||||
struct Wrapper {
|
||||
using type = Type;
|
||||
constexpr Wrapper(std::size_t index): index{index} {}
|
||||
const std::size_t index;
|
||||
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... Types>
|
||||
struct Identifier final: Wrapper<Types>... {
|
||||
template<std::size_t... Indexes>
|
||||
constexpr Identifier(std::index_sequence<Indexes...>): Wrapper<Types>{Indexes}... {}
|
||||
template<typename Type>
|
||||
struct Identifier<Type> {
|
||||
using identifier_type = std::size_t;
|
||||
|
||||
template<typename Type>
|
||||
constexpr std::size_t get() const { return Wrapper<std::decay_t<Type>>::index; }
|
||||
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;
|
||||
};
|
||||
|
||||
|
||||
@@ -59,7 +75,16 @@ struct Identifier final: Wrapper<Types>... {
|
||||
* }
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Types The list of types for which to generate identifiers.
|
||||
* @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)>{}};
|
||||
|
||||
@@ -17,7 +17,7 @@ namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief A repository class for entities and components.
|
||||
* @brief Fast and reliable entity-component system.
|
||||
*
|
||||
* The registry is the core class of the entity-component framework.<br/>
|
||||
* It stores entities and arranges pools of components on a per request basis.
|
||||
@@ -61,7 +61,7 @@ class Registry {
|
||||
}
|
||||
}
|
||||
|
||||
void append(SparseSet<Entity> &handler, test_fn_type fn) {
|
||||
inline void append(SparseSet<Entity> &handler, test_fn_type fn) {
|
||||
listeners.emplace_back(handler, fn);
|
||||
}
|
||||
|
||||
@@ -115,20 +115,20 @@ public:
|
||||
/*! @brief Default destructor. */
|
||||
~Registry() = default;
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. */
|
||||
/*! @brief Copying a registry isn't allowed. */
|
||||
Registry(const Registry &) = delete;
|
||||
/*! @brief Moving a sparse set isn't allowed. */
|
||||
/*! @brief Moving a registry isn't allowed. */
|
||||
Registry(Registry &&) = delete;
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. @return This sparse set. */
|
||||
/*! @brief Copying a registry isn't allowed. @return This registry. */
|
||||
Registry & operator=(const Registry &) = delete;
|
||||
/*! @brief Moving a sparse set isn't allowed. @return This sparse set. */
|
||||
/*! @brief Moving a registry isn't allowed. @return This registry. */
|
||||
Registry & operator=(Registry &&) = delete;
|
||||
|
||||
/**
|
||||
* @brief Returns the number of existing components of the given type.
|
||||
* @tparam Component The type of the component to which to return the size.
|
||||
* @return 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 noexcept {
|
||||
@@ -137,7 +137,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Returns the number of entities still in use.
|
||||
* @return The number of entities still in use.
|
||||
* @return Number of entities still in use.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return entities.size() - available.size();
|
||||
@@ -145,7 +145,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Returns the number of entities ever created.
|
||||
* @return The number of entities ever created.
|
||||
* @return Number of entities ever created.
|
||||
*/
|
||||
size_type capacity() const noexcept {
|
||||
return entities.size();
|
||||
@@ -153,7 +153,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Checks whether the pool for the given component is empty.
|
||||
* @tparam Component The type of the component in which one is interested.
|
||||
* @tparam Component Type of component in which one is interested.
|
||||
* @return True if the pool for the given component is empty, false
|
||||
* otherwise.
|
||||
*/
|
||||
@@ -183,7 +183,7 @@ public:
|
||||
/**
|
||||
* @brief Returns the version stored along with the given entity identifier.
|
||||
* @param entity An entity identifier, either valid or not.
|
||||
* @return The version stored along with the given entity identifier.
|
||||
* @return Version stored along with the given entity identifier.
|
||||
*/
|
||||
version_type version(entity_type entity) const noexcept {
|
||||
return version_type((entity >> traits_type::version_shift) & traits_type::version_mask);
|
||||
@@ -204,7 +204,7 @@ public:
|
||||
* registry doesn't own the given entity.
|
||||
*
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The actual version for the given entity identifier.
|
||||
* @return Actual version for the given entity identifier.
|
||||
*/
|
||||
version_type current(entity_type entity) const noexcept {
|
||||
const auto entt = entity & traits_type::entity_mask;
|
||||
@@ -212,6 +212,33 @@ public:
|
||||
return version_type((entities[entt] >> traits_type::version_shift) & traits_type::version_mask);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a new entity initialized with the given components.
|
||||
*
|
||||
* There are two kinds of entity identifiers:
|
||||
* * Newly created ones in case no entities have been previously destroyed.
|
||||
* * Recycled one with updated versions.
|
||||
*
|
||||
* Users should not care about the type of the returned entity identifier.
|
||||
* In case entity identifers are stored around, the `current` member
|
||||
* function can be used to know if they are still valid or the entity has
|
||||
* been destroyed and potentially recycled.
|
||||
*
|
||||
* The returned entity has fully initialized components assigned.
|
||||
*
|
||||
* @tparam Component A list of components to assign to the entity.
|
||||
* @param components Instances with which to initialize components.
|
||||
* @return A valid entity identifier.
|
||||
*/
|
||||
template<typename... Component>
|
||||
entity_type create(Component&&... components) noexcept {
|
||||
using accumulator_type = int[];
|
||||
const auto entity = create();
|
||||
accumulator_type accumulator = { 0, (ensure<Component>().construct(*this, entity, std::forward<Component>(components)), 0)... };
|
||||
(void)accumulator;
|
||||
return entity;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a new entity to which the given components are assigned.
|
||||
*
|
||||
@@ -224,6 +251,8 @@ public:
|
||||
* function can be used to know if they are still valid or the entity has
|
||||
* been destroyed and potentially recycled.
|
||||
*
|
||||
* The returned entity has default initialized components assigned.
|
||||
*
|
||||
* @tparam Component A list of components to assign to the entity.
|
||||
* @return A valid entity identifier.
|
||||
*/
|
||||
@@ -248,6 +277,8 @@ public:
|
||||
* function can be used to know if they are still valid or the entity has
|
||||
* been destroyed and potentially recycled.
|
||||
*
|
||||
* The returned entity has no components assigned.
|
||||
*
|
||||
* @return A valid entity identifier.
|
||||
*/
|
||||
entity_type create() noexcept {
|
||||
@@ -255,6 +286,7 @@ public:
|
||||
|
||||
if(available.empty()) {
|
||||
entity = entity_type(entities.size());
|
||||
assert(entity < traits_type::entity_mask);
|
||||
assert((entity >> traits_type::version_shift) == entity_type{});
|
||||
entities.push_back(entity);
|
||||
} else {
|
||||
@@ -285,8 +317,9 @@ public:
|
||||
|
||||
const auto entt = entity & traits_type::entity_mask;
|
||||
const auto version = 1 + ((entity >> traits_type::version_shift) & traits_type::version_mask);
|
||||
entities[entt] = entt | (version << traits_type::version_shift);
|
||||
available.push_back(entity);
|
||||
const auto next = entt | (version << traits_type::version_shift);
|
||||
entities[entt] = next;
|
||||
available.push_back(next);
|
||||
|
||||
for(auto &&cpool: pools) {
|
||||
if(cpool && cpool->has(entity)) {
|
||||
@@ -309,10 +342,10 @@ public:
|
||||
* invalid entity or if the entity already owns an instance of the given
|
||||
* component.
|
||||
*
|
||||
* @tparam Component The type of the component to create.
|
||||
* @tparam Args The types of the arguments used to construct the component.
|
||||
* @tparam Component Type of the component to create.
|
||||
* @tparam Args Types of arguments to use to construct the component.
|
||||
* @param entity A valid entity identifier.
|
||||
* @param args The parameters to use to initialize the component.
|
||||
* @param args Parameters to use to initialize the component.
|
||||
* @return A reference to the newly created component.
|
||||
*/
|
||||
template<typename Component, typename... Args>
|
||||
@@ -331,7 +364,7 @@ public:
|
||||
* invalid entity or if the entity doesn't own an instance of the given
|
||||
* component.
|
||||
*
|
||||
* @tparam Component The type of the component to remove.
|
||||
* @tparam Component Type of the component to remove.
|
||||
* @param entity A valid entity identifier.
|
||||
*/
|
||||
template<typename Component>
|
||||
@@ -348,7 +381,7 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode in case of
|
||||
* invalid entity.
|
||||
*
|
||||
* @tparam Component The components for which to perform the check.
|
||||
* @tparam Component Components for which to perform the check.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return True if the entity has all the components, false otherwise.
|
||||
*/
|
||||
@@ -373,7 +406,7 @@ public:
|
||||
* invalid entity or if the entity doesn't own an instance of the given
|
||||
* component.
|
||||
*
|
||||
* @tparam Component The type of the component to get.
|
||||
* @tparam Component Type of the component to get.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return A reference to the instance of the component owned by the entity.
|
||||
*/
|
||||
@@ -393,7 +426,7 @@ public:
|
||||
* invalid entity or if the entity doesn't own an instance of the given
|
||||
* component.
|
||||
*
|
||||
* @tparam Component The type of the component to get.
|
||||
* @tparam Component Type of the component to get.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return A reference to the instance of the component owned by the entity.
|
||||
*/
|
||||
@@ -416,10 +449,10 @@ public:
|
||||
* invalid entity or if the entity doesn't own an instance of the given
|
||||
* component.
|
||||
*
|
||||
* @tparam Component The type of the component to replace.
|
||||
* @tparam Args The types of the arguments used to construct the component.
|
||||
* @tparam Component Type of the component to replace.
|
||||
* @tparam Args Types of arguments to use to construct the component.
|
||||
* @param entity A valid entity identifier.
|
||||
* @param args The parameters to use to initialize the component.
|
||||
* @param args Parameters to use to initialize the component.
|
||||
* @return A reference to the newly created component.
|
||||
*/
|
||||
template<typename Component, typename... Args>
|
||||
@@ -449,10 +482,10 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode in case of
|
||||
* invalid entity.
|
||||
*
|
||||
* @tparam Component The type of the component to assign or replace.
|
||||
* @tparam Args The types of the arguments used to construct the component.
|
||||
* @tparam Component Type of the component to assign or replace.
|
||||
* @tparam Args Types of arguments to use to construct the component.
|
||||
* @param entity A valid entity identifier.
|
||||
* @param args The parameters to use to initialize the component.
|
||||
* @param args Parameters to use to initialize the component.
|
||||
* @return A reference to the newly created component.
|
||||
*/
|
||||
template<typename Component, typename... Args>
|
||||
@@ -486,8 +519,8 @@ public:
|
||||
*
|
||||
* Where `e1` and `e2` are valid entity identifiers.
|
||||
*
|
||||
* @tparam Component The type of the components to sort.
|
||||
* @tparam Compare The type of the comparison function object.
|
||||
* @tparam Component Type of the components to sort.
|
||||
* @tparam Compare Type of the comparison function object.
|
||||
* @param compare A valid comparison function object.
|
||||
*/
|
||||
template<typename Component, typename Compare>
|
||||
@@ -526,8 +559,8 @@ public:
|
||||
*
|
||||
* Any subsequent change to `B` won't affect the order in `A`.
|
||||
*
|
||||
* @tparam To The type of the components to sort.
|
||||
* @tparam From The type of the components to use to sort.
|
||||
* @tparam To Type of the components to sort.
|
||||
* @tparam From Type of the components to use to sort.
|
||||
*/
|
||||
template<typename To, typename From>
|
||||
void sort() {
|
||||
@@ -545,7 +578,7 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode in case of
|
||||
* invalid entity.
|
||||
*
|
||||
* @tparam Component The component to reset.
|
||||
* @tparam Component Type of the component to reset.
|
||||
* @param entity A valid entity identifier.
|
||||
*/
|
||||
template<typename Component>
|
||||
@@ -567,7 +600,7 @@ public:
|
||||
* For each entity that has an instance of the given component, the
|
||||
* component itself is removed and thus destroyed.
|
||||
*
|
||||
* @tparam Component The component whose pool must be reset.
|
||||
* @tparam Component type of the component whose pool must be reset.
|
||||
*/
|
||||
template<typename Component>
|
||||
void reset() {
|
||||
@@ -629,7 +662,7 @@ public:
|
||||
* @see View<Entity, Component>
|
||||
* @see PersistentView
|
||||
*
|
||||
* @tparam Component The type of the components used to construct the view.
|
||||
* @tparam Component Type of the components used to construct the view.
|
||||
* @return A newly created standard view.
|
||||
*/
|
||||
template<typename... Component>
|
||||
@@ -651,7 +684,7 @@ public:
|
||||
* can be prepared with this function. Just use the same set of components
|
||||
* that would have been used otherwise to contruct the view.
|
||||
*
|
||||
* @tparam Component The types of the components used to prepare the view.
|
||||
* @tparam Component Types of the components used to prepare the view.
|
||||
*/
|
||||
template<typename... Component>
|
||||
void prepare() {
|
||||
@@ -713,7 +746,7 @@ public:
|
||||
* @see View<Entity, Component>
|
||||
* @see PersistentView
|
||||
*
|
||||
* @tparam Component The types of the components used to construct the view.
|
||||
* @tparam Component Types of the components used to construct the view.
|
||||
* @return A newly created persistent view.
|
||||
*/
|
||||
template<typename... Component>
|
||||
|
||||
@@ -110,7 +110,7 @@ public:
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. @return This sparse set. */
|
||||
SparseSet & operator=(const SparseSet &) = delete;
|
||||
/*! @brief Default move operator. @return This sparse set. */
|
||||
/*! @brief Default move assignment operator. @return This sparse set. */
|
||||
SparseSet & operator=(SparseSet &&) = default;
|
||||
|
||||
/**
|
||||
@@ -121,7 +121,7 @@ public:
|
||||
* Usually the size of the internal sparse array is equal or greater than
|
||||
* the one of the internal packed array.
|
||||
*
|
||||
* @return The number of elements.
|
||||
* @return Number of elements.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return direct.size();
|
||||
@@ -297,7 +297,7 @@ public:
|
||||
* Attempting to iterate elements using the raw pointer returned by `data`
|
||||
* gives no guarantees on the order, even though `sort` has been invoked.
|
||||
*
|
||||
* @tparam Compare The type of the comparison function.
|
||||
* @tparam Compare Type of the comparison function.
|
||||
* @param compare A comparison function whose signature shall be equivalent
|
||||
* to: `bool(Entity, Entity)`.
|
||||
*/
|
||||
@@ -396,7 +396,7 @@ private:
|
||||
* @sa SparseSet<Entity>
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Type The type of the objects assigned to the entities.
|
||||
* @tparam Type Type of objects assigned to the entities.
|
||||
*/
|
||||
template<typename Entity, typename Type>
|
||||
class SparseSet<Entity, Type>: public SparseSet<Entity> {
|
||||
@@ -424,7 +424,7 @@ public:
|
||||
|
||||
/*! @brief Copying a sparse set isn't allowed. @return This sparse set. */
|
||||
SparseSet & operator=(const SparseSet &) = delete;
|
||||
/*! @brief Default move operator. @return This sparse set. */
|
||||
/*! @brief Default move assignment operator. @return This sparse set. */
|
||||
SparseSet & operator=(SparseSet &&) = default;
|
||||
|
||||
/**
|
||||
@@ -506,9 +506,9 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if the
|
||||
* sparse set already contains the given entity.
|
||||
*
|
||||
* @tparam Args The type of the params used to construct the object.
|
||||
* @tparam Args Types of arguments to use to construct the object.
|
||||
* @param entity A valid entity identifier.
|
||||
* @param args The params to use to construct an object for the entity.
|
||||
* @param args Parameters to use to construct an object for the entity.
|
||||
* @return The object associated to the entity.
|
||||
*/
|
||||
template<typename... Args>
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
|
||||
|
||||
#include <tuple>
|
||||
#include <utility>
|
||||
#include "sparse_set.hpp"
|
||||
|
||||
|
||||
@@ -46,7 +47,7 @@ namespace entt {
|
||||
* @sa View<Entity, Component>
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Component The types of the components iterated by the view.
|
||||
* @tparam Component Types of components iterated by the view.
|
||||
*/
|
||||
template<typename Entity, typename... Component>
|
||||
class PersistentView final {
|
||||
@@ -82,7 +83,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Returns the number of entities that have the given components.
|
||||
* @return The number of entities that have the given components.
|
||||
* @return Number of entities that have the given components.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return view.size();
|
||||
@@ -154,7 +155,7 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if
|
||||
* the view doesn't contain the given entity.
|
||||
*
|
||||
* @tparam Comp The type of the component to get.
|
||||
* @tparam Comp Type of the component to get.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The component assigned to the entity.
|
||||
*/
|
||||
@@ -176,7 +177,7 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if
|
||||
* the view doesn't contain the given entity.
|
||||
*
|
||||
* @tparam Comp The type of the component to get.
|
||||
* @tparam Comp Type of the component to get.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The component assigned to the entity.
|
||||
*/
|
||||
@@ -185,6 +186,50 @@ public:
|
||||
return const_cast<Comp &>(const_cast<const PersistentView *>(this)->get<Comp>(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterate the entities and applies them the given function object.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a set of references to all the components of the
|
||||
* view.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(entity_type, Component &...);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func &&func) {
|
||||
for(auto entity: *this) {
|
||||
std::forward<Func>(func)(entity, get<Component>(entity)...);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterate the entities and applies them the given function object.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a set of const references to all the components of the
|
||||
* view.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(entity_type, const Component &...);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func &&func) const {
|
||||
for(auto entity: *this) {
|
||||
std::forward<Func>(func)(entity, get<Component>(entity)...);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sort the shared pool of entities according to the given component.
|
||||
*
|
||||
@@ -199,7 +244,7 @@ public:
|
||||
* the pools of components can quickly ruin the order imposed to the pool of
|
||||
* entities shared between the persistent views.
|
||||
*
|
||||
* @tparam Comp The type of the component to use to impose the order.
|
||||
* @tparam Comp Type of the component to use to impose the order.
|
||||
*/
|
||||
template<typename Comp>
|
||||
void sort() {
|
||||
@@ -379,7 +424,7 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if
|
||||
* the view doesn't contain the given entity.
|
||||
*
|
||||
* @tparam Component The type of the component to get.
|
||||
* @tparam Component Type of the component to get.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The component assigned to the entity.
|
||||
*/
|
||||
@@ -401,7 +446,7 @@ public:
|
||||
* An assertion will abort the execution at runtime in debug mode if
|
||||
* the view doesn't contain the given entity.
|
||||
*
|
||||
* @tparam Component The type of the component to get.
|
||||
* @tparam Component Type of the component to get.
|
||||
* @param entity A valid entity identifier.
|
||||
* @return The component assigned to the entity.
|
||||
*/
|
||||
@@ -410,6 +455,50 @@ public:
|
||||
return const_cast<Component &>(const_cast<const View *>(this)->get<Component>(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterate the entities and applies them the given function object.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a set of references to all the components of the
|
||||
* view.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(entity_type, Component &...);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func &&func) {
|
||||
for(auto entity: *this) {
|
||||
std::forward<Func>(func)(entity, get<First>(entity), get<Other>(entity)...);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterate the entities and applies them the given function object.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a set of const references to all the components of the
|
||||
* view.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(entity_type, const Component &...);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func &&func) const {
|
||||
for(auto entity: *this) {
|
||||
std::forward<Func>(func)(entity, get<First>(entity), get<Other>(entity)...);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets the view and reinitializes it.
|
||||
*
|
||||
@@ -467,7 +556,7 @@ private:
|
||||
* @sa PersistentView
|
||||
*
|
||||
* @tparam Entity A valid entity type (see entt_traits for more details).
|
||||
* @tparam Component The type of the component iterated by the view.
|
||||
* @tparam Component Type of the component iterated by the view.
|
||||
*/
|
||||
template<typename Entity, typename Component>
|
||||
class View<Entity, Component> final {
|
||||
@@ -480,7 +569,7 @@ public:
|
||||
using entity_type = typename pool_type::entity_type;
|
||||
/*! @brief Unsigned integer type. */
|
||||
using size_type = typename pool_type::size_type;
|
||||
/*! The type of the component iterated by the view. */
|
||||
/*! Type of the component iterated by the view. */
|
||||
using raw_type = typename pool_type::type;
|
||||
|
||||
/**
|
||||
@@ -493,7 +582,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Returns the number of entities that have the given component.
|
||||
* @return The number of entities that have the given component.
|
||||
* @return Number of entities that have the given component.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return pool.size();
|
||||
@@ -622,6 +711,48 @@ public:
|
||||
return const_cast<Component &>(const_cast<const View *>(this)->get(entity));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterate the entities and applies them the given function object.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a reference to the component of the view.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(entity_type, Component &);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func &&func) {
|
||||
for(auto entity: *this) {
|
||||
std::forward<Func>(func)(entity, get(entity));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Iterate the entities and applies them the given function object.
|
||||
*
|
||||
* The function object is invoked for each entity. It is provided with the
|
||||
* entity itself and a const reference to the component of the view.<br/>
|
||||
* The signature of the function should be equivalent to the following:
|
||||
*
|
||||
* @code{.cpp}
|
||||
* void(entity_type, const Component &);
|
||||
* @endcode
|
||||
*
|
||||
* @tparam Func Type of the function object to invoke.
|
||||
* @param func A valid function object.
|
||||
*/
|
||||
template<typename Func>
|
||||
void each(Func &&func) const {
|
||||
for(auto entity: *this) {
|
||||
std::forward<Func>(func)(entity, get(entity));
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
pool_type &pool;
|
||||
};
|
||||
|
||||
@@ -4,4 +4,9 @@
|
||||
#include "entity/sparse_set.hpp"
|
||||
#include "entity/traits.hpp"
|
||||
#include "entity/view.hpp"
|
||||
#include "locator/locator.hpp"
|
||||
#include "signal/bus.hpp"
|
||||
#include "signal/delegate.hpp"
|
||||
#include "signal/emitter.hpp"
|
||||
#include "signal/sigh.hpp"
|
||||
#include "signal/signal.hpp"
|
||||
|
||||
115
src/entt/locator/locator.hpp
Normal file
115
src/entt/locator/locator.hpp
Normal file
@@ -0,0 +1,115 @@
|
||||
#ifndef ENTT_LOCATOR_LOCATOR_HPP
|
||||
#define ENTT_LOCATOR_LOCATOR_HPP
|
||||
|
||||
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <cassert>
|
||||
|
||||
|
||||
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
|
||||
* 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.
|
||||
*
|
||||
* @tparam Service Type of service managed by the locator.
|
||||
*/
|
||||
template<typename Service>
|
||||
struct ServiceLocator final {
|
||||
/*! @brief Type of service offered. */
|
||||
using service_type = Service;
|
||||
|
||||
/*! @brief Default constructor, deleted on purpose. */
|
||||
ServiceLocator() = delete;
|
||||
/*! @brief Default destructor, deleted on purpose. */
|
||||
~ServiceLocator() = delete;
|
||||
|
||||
/**
|
||||
* @brief Tests if a valid service implementation is set.
|
||||
* @return True if the service is set, false otherwise.
|
||||
*/
|
||||
inline static bool empty() noexcept {
|
||||
return !static_cast<bool>(service);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a weak pointer to a service implementation, if any.
|
||||
*
|
||||
* Clients of a service shouldn't retain references to it. The recommended
|
||||
* way is to retrieve the service implementation currently set each and
|
||||
* every time the need of using it arises. Otherwise users can incur in
|
||||
* unexpected behaviors.
|
||||
*
|
||||
* @return A reference to the service implementation currently set, if any.
|
||||
*/
|
||||
inline static std::weak_ptr<Service> get() noexcept {
|
||||
return service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns a weak reference to a service implementation, if any.
|
||||
*
|
||||
* Clients of a service shouldn't retain references to it. The recommended
|
||||
* way is to retrieve the service implementation currently set each and
|
||||
* every time the need of using it arises. Otherwise users can incur in
|
||||
* unexpected behaviors.
|
||||
*
|
||||
* @warning
|
||||
* In case no service implementation has been set, a call to this function
|
||||
* results in undefined behavior.
|
||||
*
|
||||
* @return A reference to the service implementation currently set, if any.
|
||||
*/
|
||||
inline static Service & ref() noexcept {
|
||||
return *service;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Sets or replaces a service.
|
||||
* @tparam Impl Type of the new service to use.
|
||||
* @tparam Args Types of arguments to use to construct the service.
|
||||
* @param args Parameters to use to construct the service.
|
||||
*/
|
||||
template<typename Impl = Service, typename... Args>
|
||||
inline static void set(Args&&... args) {
|
||||
service = std::make_shared<Impl>(std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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));
|
||||
service = std::move(ptr);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets a service.
|
||||
*
|
||||
* The service is no longer valid after a reset.
|
||||
*/
|
||||
inline static void reset() {
|
||||
service.reset();
|
||||
}
|
||||
|
||||
private:
|
||||
static std::shared_ptr<Service> service;
|
||||
};
|
||||
|
||||
|
||||
template<typename Service>
|
||||
std::shared_ptr<Service> ServiceLocator<Service>::service{};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_LOCATOR_LOCATOR_HPP
|
||||
335
src/entt/signal/bus.hpp
Normal file
335
src/entt/signal/bus.hpp
Normal file
@@ -0,0 +1,335 @@
|
||||
#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 Default constructor, explicit on purpose. */
|
||||
explicit Bus() noexcept = default;
|
||||
/*! @brief Default destructor. */
|
||||
~Bus() noexcept = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Bus(const Bus &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Bus(Bus &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This bus. */
|
||||
Bus & operator=(const Bus &) = default;
|
||||
/*! @brief Default move assignment operator. @return This bus. */
|
||||
Bus & operator=(Bus &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Unregisters all the member functions of an instance.
|
||||
*
|
||||
* 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 is 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 Default constructor, explicit on purpose. */
|
||||
explicit Bus() noexcept = default;
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Bus() noexcept = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Bus(const Bus &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Bus(Bus &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This bus. */
|
||||
Bus & operator=(const Bus &) = default;
|
||||
/*! @brief Default move assignment operator. @return This bus. */
|
||||
Bus & operator=(Bus &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Unregisters member functions of instances.
|
||||
*
|
||||
* 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 is 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
|
||||
137
src/entt/signal/delegate.hpp
Normal file
137
src/entt/signal/delegate.hpp
Normal file
@@ -0,0 +1,137 @@
|
||||
#ifndef ENTT_SIGNAL_DELEGATE_HPP
|
||||
#define ENTT_SIGNAL_DELEGATE_HPP
|
||||
|
||||
|
||||
#include <utility>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic delegate implementation.
|
||||
*
|
||||
* 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 Delegate;
|
||||
|
||||
|
||||
/**
|
||||
* @brief A delegate class to send around functions and member functions.
|
||||
*
|
||||
* Unmanaged delegate for function pointers and member functions. Users of this
|
||||
* class are in charge of disconnecting instances before deleting them.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @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>;
|
||||
|
||||
static Ret fallback(void *, Args...) noexcept { return {}; }
|
||||
|
||||
template<Ret(*Function)(Args...)>
|
||||
static Ret proto(void *, Args... args) {
|
||||
return (Function)(args...);
|
||||
}
|
||||
|
||||
template<typename Class, Ret(Class::*Member)(Args...)>
|
||||
static Ret proto(void *instance, Args... args) {
|
||||
return (static_cast<Class *>(instance)->*Member)(args...);
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Delegate() noexcept
|
||||
: stub{std::make_pair(nullptr, &fallback)}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Binds a free function to a delegate.
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<Ret(*Function)(Args...)>
|
||||
void connect() noexcept {
|
||||
stub = std::make_pair(nullptr, &proto<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`.
|
||||
*/
|
||||
template<typename Class, Ret(Class::*Member)(Args...)>
|
||||
void connect(Class *instance) noexcept {
|
||||
stub = std::make_pair(instance, &proto<Class, Member>);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Resets a delegate.
|
||||
*
|
||||
* After a reset, a delegate can be safely invoked with no effect.
|
||||
*/
|
||||
void reset() noexcept {
|
||||
stub = std::make_pair(nullptr, &fallback);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Triggers a delegate.
|
||||
* @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...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
*/
|
||||
bool operator==(const Delegate<Ret(Args...)> &other) const noexcept {
|
||||
return stub.first == other.stub.first && stub.second == other.stub.second;
|
||||
}
|
||||
|
||||
private:
|
||||
stub_type stub;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Checks if the contents of the two delegates are different.
|
||||
*
|
||||
* Two delegates are identical if they contain the same listener.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
template<typename Ret, typename... Args>
|
||||
bool operator!=(const Delegate<Ret(Args...)> &lhs, const Delegate<Ret(Args...)> &rhs) noexcept {
|
||||
return !(lhs == rhs);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_SIGNAL_DELEGATE_HPP
|
||||
235
src/entt/signal/dispatcher.hpp
Normal file
235
src/entt/signal/dispatcher.hpp
Normal file
@@ -0,0 +1,235 @@
|
||||
#ifndef ENTT_SIGNAL_DISPATCHER_HPP
|
||||
#define ENTT_SIGNAL_DISPATCHER_HPP
|
||||
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <cstdint>
|
||||
#include "../core/family.hpp"
|
||||
#include "signal.hpp"
|
||||
#include "sigh.hpp"
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief Basic dispatcher implementation.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* @tparam Sig Type of the signal handler to use.
|
||||
*/
|
||||
template<template<typename...> class Sig>
|
||||
class Dispatcher final {
|
||||
using event_family = Family<struct InternalDispatcherEventFamily>;
|
||||
|
||||
template<typename Class, typename Event>
|
||||
using instance_type = typename Sig<void(const Event &)>::template instance_type<Class>;
|
||||
|
||||
struct BaseSignalWrapper {
|
||||
virtual ~BaseSignalWrapper() = default;
|
||||
virtual void publish(std::size_t) = 0;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
struct SignalWrapper final: BaseSignalWrapper {
|
||||
void publish(std::size_t current) final override {
|
||||
for(auto &&event: events[current]) {
|
||||
signal.publish(event);
|
||||
}
|
||||
|
||||
events[current].clear();
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
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));
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline 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)... });
|
||||
}
|
||||
|
||||
private:
|
||||
Sig<void(const Event &)> signal{};
|
||||
std::vector<Event> events[2];
|
||||
};
|
||||
|
||||
inline static std::size_t buffer(bool mode) {
|
||||
return mode ? 0 : 1;
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
SignalWrapper<Event> & wrapper() {
|
||||
auto type = event_family::type<Event>();
|
||||
|
||||
if(!(type < wrappers.size())) {
|
||||
wrappers.resize(type + 1);
|
||||
}
|
||||
|
||||
if(!wrappers[type]) {
|
||||
wrappers[type] = std::make_unique<SignalWrapper<Event>>();
|
||||
}
|
||||
|
||||
return static_cast<SignalWrapper<Event> &>(*wrappers[type]);
|
||||
}
|
||||
|
||||
public:
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Dispatcher() noexcept
|
||||
: wrappers{}, mode{false}
|
||||
{}
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Dispatcher() = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Dispatcher(const Dispatcher &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Dispatcher(Dispatcher &&) = default;
|
||||
|
||||
/*! @brief Default copy assignment operator. @return This dispatcher. */
|
||||
Dispatcher & operator=(const Dispatcher &) = default;
|
||||
/*! @brief Default move assignment operator. @return This dispatcher. */
|
||||
Dispatcher & operator=(Dispatcher &&) = default;
|
||||
|
||||
/**
|
||||
* @brief Registers a listener given in the form of a member function.
|
||||
*
|
||||
* A matching member function has the following signature:
|
||||
* `void receive(const Event &)`. Member functions named `receive` are
|
||||
* automatically detected and registered if available.
|
||||
*
|
||||
* @warning
|
||||
* Connecting a listener during an update may lead to unexpected behavior.
|
||||
* Register listeners before or after invoking the update if possible.
|
||||
*
|
||||
* @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.
|
||||
*/
|
||||
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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @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.
|
||||
* @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 trigger(Args&&... args) {
|
||||
wrapper<Event>().trigger(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 trigger.
|
||||
* @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)...);
|
||||
}
|
||||
|
||||
/**
|
||||
* @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
|
||||
* 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<BaseSignalWrapper>> wrappers;
|
||||
bool mode;
|
||||
};
|
||||
|
||||
|
||||
/**
|
||||
* @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>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_SIGNAL_DISPATCHER_HPP
|
||||
344
src/entt/signal/emitter.hpp
Normal file
344
src/entt/signal/emitter.hpp
Normal file
@@ -0,0 +1,344 @@
|
||||
#ifndef ENTT_SIGNAL_EMITTER_HPP
|
||||
#define ENTT_SIGNAL_EMITTER_HPP
|
||||
|
||||
|
||||
#include <type_traits>
|
||||
#include <functional>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
|
||||
namespace entt {
|
||||
|
||||
|
||||
/**
|
||||
* @brief General purpose event emitter.
|
||||
*
|
||||
* 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> {
|
||||
* // ...
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* 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/>
|
||||
* Moreover, 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.
|
||||
*
|
||||
* @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;
|
||||
};
|
||||
|
||||
template<typename Event>
|
||||
struct Handler final: BaseHandler {
|
||||
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; };
|
||||
|
||||
return std::all_of(onceL.cbegin(), onceL.cend(), pred) &&
|
||||
std::all_of(onL.cbegin(), onL.cend(), pred);
|
||||
}
|
||||
|
||||
void clear() 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);
|
||||
} else {
|
||||
onceL.clear();
|
||||
onL.clear();
|
||||
}
|
||||
}
|
||||
|
||||
inline connection_type once(listener_type listener) {
|
||||
return onceL.emplace(onceL.cend(), false, std::move(listener));
|
||||
}
|
||||
|
||||
inline connection_type on(listener_type listener) {
|
||||
return onL.emplace(onL.cend(), false, std::move(listener));
|
||||
}
|
||||
|
||||
void erase(connection_type conn) noexcept {
|
||||
conn->first = true;
|
||||
|
||||
if(!publishing) {
|
||||
auto pred = [](auto &&element){ return element.first; };
|
||||
onceL.remove_if(pred);
|
||||
onL.remove_if(pred);
|
||||
}
|
||||
}
|
||||
|
||||
void publish(const Event &event, Derived &ref) {
|
||||
container_type currentL;
|
||||
onceL.swap(currentL);
|
||||
|
||||
auto func = [&event, &ref](auto &&element) {
|
||||
return element.first ? void() : element.second(event, ref);
|
||||
};
|
||||
|
||||
publishing = true;
|
||||
|
||||
std::for_each(onL.rbegin(), onL.rend(), func);
|
||||
std::for_each(currentL.rbegin(), currentL.rend(), func);
|
||||
|
||||
publishing = false;
|
||||
|
||||
onL.remove_if([](auto &&element){ return element.first; });
|
||||
}
|
||||
|
||||
private:
|
||||
bool publishing{false};
|
||||
container_type onceL{};
|
||||
container_type onL{};
|
||||
};
|
||||
|
||||
static std::size_t next() noexcept {
|
||||
static std::size_t counter = 0;
|
||||
return counter++;
|
||||
}
|
||||
|
||||
template<typename>
|
||||
static std::size_t type() noexcept {
|
||||
static std::size_t value = next();
|
||||
return value;
|
||||
}
|
||||
|
||||
template<typename Event>
|
||||
Handler<Event> & handler() noexcept {
|
||||
std::size_t family = type<Event>();
|
||||
|
||||
if(!(family < handlers.size())) {
|
||||
handlers.resize(family+1);
|
||||
}
|
||||
|
||||
if(!handlers[family]) {
|
||||
handlers[family] = std::make_unique<Handler<Event>>();
|
||||
}
|
||||
|
||||
return static_cast<Handler<Event> &>(*handlers[family]);
|
||||
}
|
||||
|
||||
public:
|
||||
/** @brief Type of listeners accepted for the given type of event. */
|
||||
template<typename Event>
|
||||
using Listener = typename Handler<Event>::listener_type;
|
||||
|
||||
/**
|
||||
* @brief Generic connection type for events.
|
||||
*
|
||||
* Type of the connection object returned by the event emitter whenever a
|
||||
* listener for the given type is registered.<br/>
|
||||
* It can be used to break connections still in use.
|
||||
*
|
||||
* @tparam Event Type of event for which the connection is created.
|
||||
*/
|
||||
template<typename Event>
|
||||
struct Connection final: private Handler<Event>::connection_type {
|
||||
/** @brief Event emitters are friend classes of connections. */
|
||||
friend class Emitter;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit Connection() = default;
|
||||
|
||||
/*! @brief Default copy constructor. */
|
||||
Connection(const Connection &) = default;
|
||||
/*! @brief Default move constructor. */
|
||||
Connection(Connection &&) = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Connection() = default;
|
||||
|
||||
/**
|
||||
* @brief 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)}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @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, explicit on purpose. */
|
||||
explicit Emitter() noexcept = default;
|
||||
|
||||
/*! @brief Copying an emitter isn't allowed. */
|
||||
Emitter(const Emitter &) = delete;
|
||||
/*! @brief Default move constructor. */
|
||||
Emitter(Emitter &&) = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
virtual ~Emitter() noexcept {
|
||||
static_assert(std::is_base_of<Emitter<Derived>, Derived>::value, "!");
|
||||
}
|
||||
|
||||
/*! @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 Emits the given event.
|
||||
*
|
||||
* All the listeners registered for the specific event type are invoked with
|
||||
* the given event. The event type must either have a proper constructor for
|
||||
* the arguments provided or be an aggregate type.
|
||||
*
|
||||
* @tparam Event Type of event to publish.
|
||||
* @tparam Args Types of arguments to use to construct the event.
|
||||
* @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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Registers a long-lived listener with the event emitter.
|
||||
*
|
||||
* This method can be used to register a listener designed to be invoked
|
||||
* more than once for the given event type.<br/>
|
||||
* The connection returned by the method can be freely discarded. It's meant
|
||||
* to be used later to disconnect the listener if required.
|
||||
*
|
||||
* The listener is as a callable object that can be moved and the type of
|
||||
* which is `void(const Event &, Derived &)`.
|
||||
*
|
||||
* @note
|
||||
* Whenever an event is emitted, the emitter provides the listener with a
|
||||
* reference to the derived class. Listeners don't have to capture those
|
||||
* instances for later uses.
|
||||
*
|
||||
* @tparam Event Type of event to which to connect the listener.
|
||||
* @param listener 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Registers a short-lived listener with the event emitter.
|
||||
*
|
||||
* This method can be used to register a listener designed to be invoked
|
||||
* only once for the given event type.<br/>
|
||||
* The connection returned by the method can be freely discarded. It's meant
|
||||
* to be used later to disconnect the listener if required.
|
||||
*
|
||||
* The listener is as a callable object that can be moved and the type of
|
||||
* which is `void(const Event &, Derived &)`.
|
||||
*
|
||||
* @note
|
||||
* Whenever an event is emitted, the emitter provides the listener with a
|
||||
* reference to the derived class. Listeners don't have to capture those
|
||||
* instances for later uses.
|
||||
*
|
||||
* @tparam Event Type of event to which to connect the listener.
|
||||
* @param listener 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));
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects a listener from the event emitter.
|
||||
*
|
||||
* Do not use twice the same connection to disconnect a listener, it results
|
||||
* in undefined behavior. Once used, discard the connection object.
|
||||
*
|
||||
* @tparam Event Type of event of the connection.
|
||||
* @param conn A valid connection.
|
||||
*/
|
||||
template<typename Event>
|
||||
void erase(Connection<Event> conn) noexcept {
|
||||
handler<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.
|
||||
*
|
||||
* @tparam Event Type of event to reset.
|
||||
*/
|
||||
template<typename Event>
|
||||
void clear() noexcept {
|
||||
handler<Event>().clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects all the listeners.
|
||||
*
|
||||
* All the connections previously returned are invalidated. Using them
|
||||
* results in undefined behaviour.
|
||||
*/
|
||||
void clear() noexcept {
|
||||
std::for_each(handlers.begin(), handlers.end(),
|
||||
[](auto &&handler){ if(handler) { handler->clear(); } });
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Checks if there are listeners registered for the specific event.
|
||||
* @tparam Event Type of event to test.
|
||||
* @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());
|
||||
}
|
||||
|
||||
/**
|
||||
* @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(); });
|
||||
}
|
||||
|
||||
private:
|
||||
std::vector<std::unique_ptr<BaseHandler>> handlers{};
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif // ENTT_SIGNAL_EMITTER_HPP
|
||||
@@ -71,24 +71,27 @@ using DefaultCollectorType = typename DefaultCollector<Function>::collector_type
|
||||
|
||||
|
||||
/**
|
||||
* @brief Signal handler.
|
||||
* @brief Unmanaged 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.
|
||||
*
|
||||
* @tparam Function A valid function type.
|
||||
* @tparam Collector Type of collector to use, if any.
|
||||
*/
|
||||
template<typename Function, typename = DefaultCollectorType<Function>>
|
||||
template<typename Function, typename Collector = DefaultCollectorType<Function>>
|
||||
class SigH;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Signal handler.
|
||||
* @brief Unmanaged signal handler definition.
|
||||
*
|
||||
* 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.
|
||||
*
|
||||
* This class serves mainly two purposes:
|
||||
* * Creating signals to be used later to notify a bunch of listeners.
|
||||
* * Creating signals used later to notify a bunch of listeners.
|
||||
* * Collecting results from a set of functions like in a voting system.
|
||||
*
|
||||
* The default collector does nothing. To properly collect data, define and use
|
||||
@@ -98,8 +101,8 @@ class SigH;
|
||||
* otherwise.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of the arguments of a function type.
|
||||
* @tparam Collector The type of the collector to use if any.
|
||||
* @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> {
|
||||
@@ -121,6 +124,13 @@ public:
|
||||
/*! @brief Collector type. */
|
||||
using collector_type = Collector;
|
||||
|
||||
/**
|
||||
* @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 = Class *;
|
||||
|
||||
/*! @brief Default constructor, explicit on purpose. */
|
||||
explicit SigH() noexcept = default;
|
||||
|
||||
@@ -129,7 +139,7 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Copy constructor, listeners are also connected to this signal.
|
||||
* @param other A signal to be used as source to initialize this instance.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
SigH(const SigH &other)
|
||||
: calls{other.calls}
|
||||
@@ -137,15 +147,18 @@ public:
|
||||
|
||||
/**
|
||||
* @brief Default move constructor.
|
||||
* @param other A signal to be used as source to initialize this instance.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
SigH(SigH &&other): SigH{} {
|
||||
swap(*this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Assignment operator, listeners are also connected to this signal.
|
||||
* @param other A signal to be used as source to initialize this instance.
|
||||
* @brief Copy assignment operator.
|
||||
*
|
||||
* Listeners are also connected to this signal.
|
||||
*
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
SigH & operator=(const SigH &other) {
|
||||
@@ -154,8 +167,8 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Default move operator.
|
||||
* @param other A signal to be used as source to initialize this instance.
|
||||
* @brief Move assignment operator.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
SigH & operator=(SigH &&other) {
|
||||
@@ -164,15 +177,15 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief The number of listeners connected to the signal.
|
||||
* @return The number of listeners currently connected.
|
||||
* @brief Number of listeners connected to the signal.
|
||||
* @return Number of listeners currently connected.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return calls.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns true is at least a listener is connected to the signal.
|
||||
* @brief Returns false is at least a listener is connected to the signal.
|
||||
* @return True if the signal has no listeners connected, false otherwise.
|
||||
*/
|
||||
bool empty() const noexcept {
|
||||
@@ -180,16 +193,15 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects all the listeners from the signal.
|
||||
* @brief Disconnects all the listeners from a signal.
|
||||
*/
|
||||
void clear() noexcept {
|
||||
calls.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects a free function to the signal.
|
||||
* @brief Connects a free function to a signal.
|
||||
*
|
||||
* @note
|
||||
* The signal handler performs checks to avoid multiple connections for free
|
||||
* functions.
|
||||
*
|
||||
@@ -202,28 +214,25 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Connects the member function for the given instance to the signal.
|
||||
* @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.
|
||||
* signal. On the other side, the signal handler performs checks to avoid
|
||||
* multiple connections for the same member function of a given instance.
|
||||
*
|
||||
* @warning
|
||||
* The signal handler performs checks to avoid multiple connections for the
|
||||
* same member function of a given instance.
|
||||
*
|
||||
* @tparam Class The type of the class to which the member function belongs.
|
||||
* @tparam Member The member function to connect to the 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 connect(Class *instance) {
|
||||
void connect(instance_type<Class> instance) {
|
||||
disconnect<Class, Member>(instance);
|
||||
calls.emplace_back(instance, &proto<Class, Member>);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects a free function from the signal.
|
||||
* @brief Disconnects a free function from a signal.
|
||||
* @tparam Function A valid free function pointer.
|
||||
*/
|
||||
template<Ret(*Function)(Args...)>
|
||||
@@ -233,30 +242,30 @@ public:
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Disconnects the given member function from the signal.
|
||||
* @tparam Class The type of the class to which the member function belongs.
|
||||
* @tparam Member The member function to connect to the signal.
|
||||
* @brief Disconnects the given member function from a signal.
|
||||
* @tparam Class Type of class to which the member function belongs.
|
||||
* @tparam Member Member function to connect to the signal.
|
||||
* @param instance A valid instance of type pointer to `Class`.
|
||||
*/
|
||||
template<typename Class, Ret(Class::*Member)(Args...)>
|
||||
void disconnect(Class *instance) {
|
||||
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 The type of the class to which the member function belongs.
|
||||
* @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(Class *instance) {
|
||||
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 the signal.
|
||||
* @brief Triggers a signal.
|
||||
*
|
||||
* All the listeners are notified. Order isn't guaranteed.
|
||||
*
|
||||
@@ -305,7 +314,7 @@ public:
|
||||
* @return True if the two signals are identical, false otherwise.
|
||||
*/
|
||||
bool operator==(const SigH &other) const noexcept {
|
||||
return (calls.size() == other.calls.size()) && std::equal(calls.cbegin(), calls.cend(), other.calls.cbegin());
|
||||
return std::equal(calls.cbegin(), calls.cend(), other.calls.cbegin(), other.calls.cend());
|
||||
}
|
||||
|
||||
private:
|
||||
@@ -320,7 +329,7 @@ private:
|
||||
* listeners registered exactly in the same order.
|
||||
*
|
||||
* @tparam Ret Return type of a function type.
|
||||
* @tparam Args Types of the arguments 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.
|
||||
@@ -331,17 +340,6 @@ bool operator!=(const SigH<Ret(Args...)> &lhs, const SigH<Ret(Args...)> &rhs) no
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief Event handler.
|
||||
*
|
||||
* Unmanaged event handler. Collecting data for this kind of signals doesn't
|
||||
* make sense at all. Its sole purpose is to provide the listeners with the
|
||||
* given event.
|
||||
*/
|
||||
template<typename Event>
|
||||
using EventH = SigH<void(const Event &)>;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
272
src/entt/signal/signal.hpp
Normal file
272
src/entt/signal/signal.hpp
Normal file
@@ -0,0 +1,272 @@
|
||||
#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 Default constructor, explicit on purpose. */
|
||||
explicit Signal() noexcept = default;
|
||||
|
||||
/*! @brief Default destructor. */
|
||||
~Signal() noexcept = default;
|
||||
|
||||
/**
|
||||
* @brief Copy constructor, listeners are also connected to this signal.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
Signal(const Signal &other)
|
||||
: calls{other.calls}
|
||||
{}
|
||||
|
||||
/**
|
||||
* @brief Default move constructor.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
*/
|
||||
Signal(Signal &&other): Signal{} {
|
||||
swap(*this, other);
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Copy assignment operator.
|
||||
*
|
||||
* Listeners are also connected to this signal.
|
||||
*
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
Signal & operator=(const Signal &other) {
|
||||
calls = other.calls;
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Move assignment operator.
|
||||
* @param other A signal to use as source to initialize this instance.
|
||||
* @return This signal.
|
||||
*/
|
||||
Signal & operator=(Signal &&other) {
|
||||
swap(*this, other);
|
||||
return *this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Number of listeners connected to the signal.
|
||||
* @return Number of listeners currently connected.
|
||||
*/
|
||||
size_type size() const noexcept {
|
||||
return calls.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Returns false is 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
|
||||
@@ -2,31 +2,66 @@
|
||||
# Tests configuration
|
||||
#
|
||||
|
||||
set(COMMON_LINK_LIBS gtest_main Threads::Threads)
|
||||
include_directories(${PROJECT_SRC_DIR})
|
||||
|
||||
# Test core
|
||||
|
||||
add_executable(core entt/core/ident.cpp entt/core/family.cpp odr.cpp)
|
||||
target_link_libraries(core PRIVATE ${COMMON_LINK_LIBS})
|
||||
add_test(NAME core COMMAND core)
|
||||
|
||||
# Test entt
|
||||
|
||||
add_executable(entity entt/entity/registry.cpp entt/entity/sparse_set.cpp entt/entity/view.cpp odr.cpp)
|
||||
target_link_libraries(entity PRIVATE ${COMMON_LINK_LIBS})
|
||||
add_test(NAME entity COMMAND entity)
|
||||
add_library(odr OBJECT odr.cpp)
|
||||
|
||||
# Test benchmark
|
||||
|
||||
IF(CMAKE_BUILD_TYPE MATCHES Release)
|
||||
add_executable(benchmark entt/entity/benchmark.cpp odr.cpp)
|
||||
target_link_libraries(benchmark PRIVATE ${COMMON_LINK_LIBS})
|
||||
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)
|
||||
ENDIF()
|
||||
endif()
|
||||
|
||||
# Test signal
|
||||
# Test core
|
||||
|
||||
add_executable(signal entt/signal/sigh.cpp odr.cpp)
|
||||
target_link_libraries(signal PRIVATE ${COMMON_LINK_LIBS})
|
||||
add_executable(
|
||||
core
|
||||
$<TARGET_OBJECTS:odr>
|
||||
entt/core/family.cpp
|
||||
entt/core/ident.cpp
|
||||
)
|
||||
target_link_libraries(core PRIVATE gtest_main Threads::Threads)
|
||||
add_test(NAME core COMMAND core)
|
||||
|
||||
# Test entity
|
||||
|
||||
add_executable(
|
||||
entity
|
||||
$<TARGET_OBJECTS:odr>
|
||||
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)
|
||||
|
||||
# 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)
|
||||
|
||||
# Test locator
|
||||
|
||||
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)
|
||||
|
||||
@@ -14,3 +14,9 @@ TEST(Family, Functionalities) {
|
||||
ASSERT_NE(myFamilyType, myOtherFamilyType);
|
||||
ASSERT_EQ(myFamilyType, yourFamilyType);
|
||||
}
|
||||
|
||||
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 &>());
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
#include <type_traits>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/core/ident.hpp>
|
||||
|
||||
@@ -24,3 +25,9 @@ TEST(Identifier, Uniqueness) {
|
||||
SUCCEED();
|
||||
}
|
||||
}
|
||||
|
||||
TEST(Identifier, SingleType) {
|
||||
constexpr auto ID = entt::ident<A>;
|
||||
std::integral_constant<decltype(ID)::identifier_type, ID.get()> ic;
|
||||
(void)ic;
|
||||
}
|
||||
|
||||
@@ -37,12 +37,11 @@ TEST(DefaultRegistry, Construct) {
|
||||
|
||||
Timer timer;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create();
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, Destroy) {
|
||||
@@ -51,13 +50,13 @@ TEST(DefaultRegistry, Destroy) {
|
||||
|
||||
std::cout << "Destroying 10000000 entities" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
entities.push_back(registry.create());
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
for (auto entity: entities) {
|
||||
for(auto entity: entities) {
|
||||
registry.destroy(entity);
|
||||
}
|
||||
|
||||
@@ -86,7 +85,6 @@ TEST(DefaultRegistry, IterateCreateDeleteSingleComponent) {
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateSingleComponent10M) {
|
||||
@@ -94,21 +92,13 @@ TEST(DefaultRegistry, IterateSingleComponent10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, one component" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get(entity);
|
||||
(void)position;
|
||||
}
|
||||
|
||||
registry.view<Position>().each([](auto, auto &) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents10M) {
|
||||
@@ -116,23 +106,13 @@ TEST(DefaultRegistry, IterateTwoComponents10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
|
||||
@@ -140,24 +120,14 @@ TEST(DefaultRegistry, IterateTwoComponents10MHalf) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, half of the entities have all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents10MOne) {
|
||||
@@ -165,24 +135,14 @@ TEST(DefaultRegistry, IterateTwoComponents10MOne) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, only one entity has all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent10M) {
|
||||
@@ -191,23 +151,13 @@ TEST(DefaultRegistry, IterateTwoComponentsPersistent10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, persistent view" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent10MHalf) {
|
||||
@@ -216,24 +166,14 @@ TEST(DefaultRegistry, IterateTwoComponentsPersistent10MHalf) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, persistent view, half of the entities have all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent10MOne) {
|
||||
@@ -242,95 +182,14 @@ TEST(DefaultRegistry, IterateTwoComponentsPersistent10MOne) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, two components, persistent view, only one entity has all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateSingleComponent50M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 50000000 entities, one component" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 50000000L; i++) {
|
||||
registry.create<Position>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get(entity);
|
||||
(void)position;
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponents50M) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
std::cout << "Iterating over 50000000 entities, two components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 50000000L; i++) {
|
||||
registry.create<Position, Velocity>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTwoComponentsPersistent50M) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<Position, Velocity>();
|
||||
|
||||
std::cout << "Iterating over 50000000 entities, two components, persistent view" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 50000000L; i++) {
|
||||
registry.create<Position, Velocity>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
}
|
||||
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateFiveComponents10M) {
|
||||
@@ -338,29 +197,13 @@ TEST(DefaultRegistry, IterateFiveComponents10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, five components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponents10M) {
|
||||
@@ -368,39 +211,13 @@ TEST(DefaultRegistry, IterateTenComponents10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
auto &comp4 = view.get<Comp<4>>(entity);
|
||||
auto &comp5 = view.get<Comp<5>>(entity);
|
||||
auto &comp6 = view.get<Comp<6>>(entity);
|
||||
auto &comp7 = view.get<Comp<7>>(entity);
|
||||
auto &comp8 = view.get<Comp<8>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
(void)comp4;
|
||||
(void)comp5;
|
||||
(void)comp6;
|
||||
(void)comp7;
|
||||
(void)comp8;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponents10MHalf) {
|
||||
@@ -408,40 +225,14 @@ TEST(DefaultRegistry, IterateTenComponents10MHalf) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, half of the entities have all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
auto &comp4 = view.get<Comp<4>>(entity);
|
||||
auto &comp5 = view.get<Comp<5>>(entity);
|
||||
auto &comp6 = view.get<Comp<6>>(entity);
|
||||
auto &comp7 = view.get<Comp<7>>(entity);
|
||||
auto &comp8 = view.get<Comp<8>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
(void)comp4;
|
||||
(void)comp5;
|
||||
(void)comp6;
|
||||
(void)comp7;
|
||||
(void)comp8;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponents10MOne) {
|
||||
@@ -449,40 +240,14 @@ TEST(DefaultRegistry, IterateTenComponents10MOne) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, only one entity has all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
auto &comp4 = view.get<Comp<4>>(entity);
|
||||
auto &comp5 = view.get<Comp<5>>(entity);
|
||||
auto &comp6 = view.get<Comp<6>>(entity);
|
||||
auto &comp7 = view.get<Comp<7>>(entity);
|
||||
auto &comp8 = view.get<Comp<8>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
(void)comp4;
|
||||
(void)comp5;
|
||||
(void)comp6;
|
||||
(void)comp7;
|
||||
(void)comp8;
|
||||
}
|
||||
|
||||
registry.view<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateFiveComponentsPersistent10M) {
|
||||
@@ -491,29 +256,13 @@ TEST(DefaultRegistry, IterateFiveComponentsPersistent10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, five components, persistent view" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponentsPersistent10M) {
|
||||
@@ -522,39 +271,13 @@ TEST(DefaultRegistry, IterateTenComponentsPersistent10M) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, persistent view" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
registry.create<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
auto &comp4 = view.get<Comp<4>>(entity);
|
||||
auto &comp5 = view.get<Comp<5>>(entity);
|
||||
auto &comp6 = view.get<Comp<6>>(entity);
|
||||
auto &comp7 = view.get<Comp<7>>(entity);
|
||||
auto &comp8 = view.get<Comp<8>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
(void)comp4;
|
||||
(void)comp5;
|
||||
(void)comp6;
|
||||
(void)comp7;
|
||||
(void)comp8;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponentsPersistent10MHalf) {
|
||||
@@ -563,40 +286,14 @@ TEST(DefaultRegistry, IterateTenComponentsPersistent10MHalf) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, persistent view, half of the entities have all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i % 2) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
auto &comp4 = view.get<Comp<4>>(entity);
|
||||
auto &comp5 = view.get<Comp<5>>(entity);
|
||||
auto &comp6 = view.get<Comp<6>>(entity);
|
||||
auto &comp7 = view.get<Comp<7>>(entity);
|
||||
auto &comp8 = view.get<Comp<8>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
(void)comp4;
|
||||
(void)comp5;
|
||||
(void)comp6;
|
||||
(void)comp7;
|
||||
(void)comp8;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, IterateTenComponentsPersistent10MOne) {
|
||||
@@ -605,40 +302,14 @@ TEST(DefaultRegistry, IterateTenComponentsPersistent10MOne) {
|
||||
|
||||
std::cout << "Iterating over 10000000 entities, ten components, persistent view, only one entity has all the components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 10000000L; i++) {
|
||||
for(uint64_t i = 0; i < 10000000L; i++) {
|
||||
auto entity = registry.create<Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
if(i == 5000000L) { registry.assign<Position>(entity); }
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
auto view = registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>();
|
||||
|
||||
for(auto entity: view) {
|
||||
auto &position = view.get<Position>(entity);
|
||||
auto &velocity = view.get<Velocity>(entity);
|
||||
auto &comp1 = view.get<Comp<1>>(entity);
|
||||
auto &comp2 = view.get<Comp<2>>(entity);
|
||||
auto &comp3 = view.get<Comp<3>>(entity);
|
||||
auto &comp4 = view.get<Comp<4>>(entity);
|
||||
auto &comp5 = view.get<Comp<5>>(entity);
|
||||
auto &comp6 = view.get<Comp<6>>(entity);
|
||||
auto &comp7 = view.get<Comp<7>>(entity);
|
||||
auto &comp8 = view.get<Comp<8>>(entity);
|
||||
(void)position;
|
||||
(void)velocity;
|
||||
(void)comp1;
|
||||
(void)comp2;
|
||||
(void)comp3;
|
||||
(void)comp4;
|
||||
(void)comp5;
|
||||
(void)comp6;
|
||||
(void)comp7;
|
||||
(void)comp8;
|
||||
}
|
||||
|
||||
registry.persistent<Position, Velocity, Comp<1>, Comp<2>, Comp<3>, Comp<4>, Comp<5>, Comp<6>, Comp<7>, Comp<8>>().each([](auto, auto &...) {});
|
||||
timer.elapsed();
|
||||
registry.reset();
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, SortSingle) {
|
||||
@@ -647,15 +318,14 @@ TEST(DefaultRegistry, SortSingle) {
|
||||
|
||||
std::cout << "Sort 150000 entities, one component" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 150000L; i++) {
|
||||
auto entity = registry.create();
|
||||
for(uint64_t i = 0; i < 150000L; i++) {
|
||||
auto entity = registry.create<Position>({ i, i });
|
||||
entities.push_back(entity);
|
||||
registry.assign<Position>(entity, i, i);
|
||||
}
|
||||
|
||||
Timer timer;
|
||||
|
||||
registry.sort<Position>([®istry](const auto &lhs, const auto &rhs) {
|
||||
registry.sort<Position>([](const auto &lhs, const auto &rhs) {
|
||||
return lhs.x < rhs.x && lhs.y < rhs.y;
|
||||
});
|
||||
|
||||
@@ -668,14 +338,12 @@ TEST(DefaultRegistry, SortMulti) {
|
||||
|
||||
std::cout << "Sort 150000 entities, two components" << std::endl;
|
||||
|
||||
for (uint64_t i = 0; i < 150000L; i++) {
|
||||
auto entity = registry.create();
|
||||
for(uint64_t i = 0; i < 150000L; i++) {
|
||||
auto entity = registry.create<Position, Velocity>({ i, i }, { i, i });
|
||||
entities.push_back(entity);
|
||||
registry.assign<Position>(entity, i, i);
|
||||
registry.assign<Velocity>(entity, i, i);
|
||||
}
|
||||
|
||||
registry.sort<Position>([®istry](const auto &lhs, const auto &rhs) {
|
||||
registry.sort<Position>([](const auto &lhs, const auto &rhs) {
|
||||
return lhs.x < rhs.x && lhs.y < rhs.y;
|
||||
});
|
||||
|
||||
|
||||
@@ -121,18 +121,27 @@ TEST(DefaultRegistry, Functionalities) {
|
||||
ASSERT_TRUE(registry.empty<int>());
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, CreateDestroyEntities) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto pre = registry.create<double>();
|
||||
registry.destroy(pre);
|
||||
auto post = registry.create<double>();
|
||||
|
||||
ASSERT_FALSE(registry.valid(pre));
|
||||
ASSERT_TRUE(registry.valid(post));
|
||||
ASSERT_NE(registry.version(pre), registry.version(post));
|
||||
ASSERT_EQ(registry.current(pre), registry.current(post));
|
||||
}
|
||||
|
||||
TEST(DefaultRegistry, SortSingle) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e1 = registry.create();
|
||||
auto e2 = registry.create();
|
||||
auto e3 = registry.create();
|
||||
int val = 0;
|
||||
|
||||
auto val = 0;
|
||||
|
||||
registry.assign<int>(e1, val++);
|
||||
registry.assign<int>(e2, val++);
|
||||
registry.assign<int>(e3, val++);
|
||||
registry.create(val++);
|
||||
registry.create(val++);
|
||||
registry.create(val++);
|
||||
|
||||
for(auto entity: registry.view<int>()) {
|
||||
ASSERT_EQ(registry.get<int>(entity), --val);
|
||||
@@ -148,20 +157,12 @@ TEST(DefaultRegistry, SortSingle) {
|
||||
TEST(DefaultRegistry, SortMulti) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e1 = registry.create();
|
||||
auto e2 = registry.create();
|
||||
auto e3 = registry.create();
|
||||
unsigned int uval = 0u;
|
||||
int ival = 0;
|
||||
|
||||
auto uval = 0u;
|
||||
auto ival = 0;
|
||||
|
||||
registry.assign<unsigned int>(e1, uval++);
|
||||
registry.assign<unsigned int>(e2, uval++);
|
||||
registry.assign<unsigned int>(e3, uval++);
|
||||
|
||||
registry.assign<int>(e1, ival++);
|
||||
registry.assign<int>(e2, ival++);
|
||||
registry.assign<int>(e3, ival++);
|
||||
registry.create(uval++, ival++);
|
||||
registry.create(uval++, ival++);
|
||||
registry.create(uval++, ival++);
|
||||
|
||||
for(auto entity: registry.view<unsigned int>()) {
|
||||
ASSERT_EQ(registry.get<unsigned int>(entity), --uval);
|
||||
|
||||
@@ -56,6 +56,25 @@ TEST(View, SingleComponentEmpty) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(View, SingleComponentEach) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
registry.create<int, char>();
|
||||
registry.create<int, char>();
|
||||
|
||||
auto view = registry.view<int>();
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto, int &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
|
||||
cview.each([&cnt](auto, const int &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(View, MultipleComponent) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
@@ -98,7 +117,26 @@ TEST(View, MultipleComponentEmpty) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PersistentView, MultipleComponentPrepare) {
|
||||
TEST(View, MultipleComponentEach) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
registry.create<int, char>();
|
||||
registry.create<int, char>();
|
||||
|
||||
auto view = registry.view<int, char>();
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto, int &, char &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
|
||||
cview.each([&cnt](auto, const int &, const char &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(PersistentView, Prepare) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, char>();
|
||||
|
||||
@@ -137,7 +175,7 @@ TEST(PersistentView, MultipleComponentPrepare) {
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(PersistentView, MultipleComponentNoPrepare) {
|
||||
TEST(PersistentView, NoPrepare) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
auto e1 = registry.create<char>();
|
||||
@@ -175,7 +213,7 @@ TEST(PersistentView, MultipleComponentNoPrepare) {
|
||||
ASSERT_EQ(view.begin(), view.end());
|
||||
}
|
||||
|
||||
TEST(PersistentView, MultipleComponentEmpty) {
|
||||
TEST(PersistentView, Empty) {
|
||||
entt::DefaultRegistry registry;
|
||||
|
||||
registry.create<double, int, float>();
|
||||
@@ -192,6 +230,26 @@ TEST(PersistentView, MultipleComponentEmpty) {
|
||||
}
|
||||
}
|
||||
|
||||
TEST(PersistentView, Each) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, char>();
|
||||
|
||||
registry.create<int, char>();
|
||||
registry.create<int, char>();
|
||||
|
||||
auto view = registry.persistent<int, char>();
|
||||
const auto &cview = static_cast<const decltype(view) &>(view);
|
||||
std::size_t cnt = 0;
|
||||
|
||||
view.each([&cnt](auto, int &, char &) { ++cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{2});
|
||||
|
||||
cview.each([&cnt](auto, const int &, const char &) { --cnt; });
|
||||
|
||||
ASSERT_EQ(cnt, std::size_t{0});
|
||||
}
|
||||
|
||||
TEST(PersistentView, Sort) {
|
||||
entt::DefaultRegistry registry;
|
||||
registry.prepare<int, unsigned int>();
|
||||
|
||||
49
test/entt/locator/locator.cpp
Normal file
49
test/entt/locator/locator.cpp
Normal file
@@ -0,0 +1,49 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/locator/locator.hpp>
|
||||
|
||||
struct A {};
|
||||
|
||||
struct B {
|
||||
virtual void f(bool) = 0;
|
||||
bool check{false};
|
||||
};
|
||||
|
||||
struct D: B {
|
||||
D(int): B{} {}
|
||||
void f(bool b) override { check = b; }
|
||||
};
|
||||
|
||||
TEST(ServiceLocator, Functionalities) {
|
||||
using entt::ServiceLocator;
|
||||
|
||||
ASSERT_TRUE(ServiceLocator<A>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<B>::empty());
|
||||
|
||||
ServiceLocator<A>::set();
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<A>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<B>::empty());
|
||||
|
||||
ServiceLocator<A>::reset();
|
||||
|
||||
ASSERT_TRUE(ServiceLocator<A>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<B>::empty());
|
||||
|
||||
ServiceLocator<A>::set(std::make_shared<A>());
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<A>::empty());
|
||||
ASSERT_TRUE(ServiceLocator<B>::empty());
|
||||
|
||||
ServiceLocator<B>::set<D>(42);
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<A>::empty());
|
||||
ASSERT_FALSE(ServiceLocator<B>::empty());
|
||||
|
||||
ServiceLocator<B>::get().lock()->f(!ServiceLocator<B>::get().lock()->check);
|
||||
|
||||
ASSERT_TRUE(ServiceLocator<B>::get().lock()->check);
|
||||
|
||||
ServiceLocator<B>::ref().f(!ServiceLocator<B>::get().lock()->check);
|
||||
|
||||
ASSERT_FALSE(ServiceLocator<B>::get().lock()->check);
|
||||
}
|
||||
141
test/entt/signal/bus.cpp
Normal file
141
test/entt/signal/bus.cpp
Normal file
@@ -0,0 +1,141 @@
|
||||
#include <memory>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/signal/bus.hpp>
|
||||
|
||||
struct EventA
|
||||
{
|
||||
EventA(int x, int y): value{x+y} {}
|
||||
int value;
|
||||
};
|
||||
|
||||
struct EventB {};
|
||||
struct EventC {};
|
||||
|
||||
struct MyListener
|
||||
{
|
||||
void receive(const EventA &) { A++; }
|
||||
static void listen(const EventB &) { B++; }
|
||||
void receive(const EventC &) { C++; }
|
||||
void reset() { A = 0; B = 0; C = 0; }
|
||||
int A{0};
|
||||
static int B;
|
||||
int C{0};
|
||||
};
|
||||
|
||||
int MyListener::B = 0;
|
||||
|
||||
template<typename Bus, typename Listener>
|
||||
void testRegUnregEmit(Listener listener) {
|
||||
Bus bus;
|
||||
|
||||
listener->reset();
|
||||
bus.template publish<EventA>(40, 2);
|
||||
bus.template publish<EventB>();
|
||||
bus.template publish<EventC>();
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))0);
|
||||
ASSERT_TRUE(bus.empty());
|
||||
ASSERT_EQ(listener->A, 0);
|
||||
ASSERT_EQ(listener->B, 0);
|
||||
ASSERT_EQ(listener->C, 0);
|
||||
|
||||
bus.reg(listener);
|
||||
bus.template connect<EventB, &MyListener::listen>();
|
||||
|
||||
listener->reset();
|
||||
bus.template publish<EventA>(40, 2);
|
||||
bus.template publish<EventB>();
|
||||
bus.template publish<EventC>();
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))3);
|
||||
ASSERT_FALSE(bus.empty());
|
||||
ASSERT_EQ(listener->A, 1);
|
||||
ASSERT_EQ(listener->B, 1);
|
||||
ASSERT_EQ(listener->C, 1);
|
||||
|
||||
bus.unreg(listener);
|
||||
|
||||
listener->reset();
|
||||
bus.template publish<EventA>(40, 2);
|
||||
bus.template publish<EventB>();
|
||||
bus.template publish<EventC>();
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))1);
|
||||
ASSERT_FALSE(bus.empty());
|
||||
ASSERT_EQ(listener->A, 0);
|
||||
ASSERT_EQ(listener->B, 1);
|
||||
ASSERT_EQ(listener->C, 0);
|
||||
|
||||
bus.template disconnect<EventB, MyListener::listen>();
|
||||
|
||||
listener->reset();
|
||||
bus.template publish<EventA>(40, 2);
|
||||
bus.template publish<EventB>();
|
||||
bus.template publish<EventC>();
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))0);
|
||||
ASSERT_TRUE(bus.empty());
|
||||
ASSERT_EQ(listener->A, 0);
|
||||
ASSERT_EQ(listener->B, 0);
|
||||
ASSERT_EQ(listener->C, 0);
|
||||
}
|
||||
|
||||
TEST(ManagedBus, RegUnregEmit) {
|
||||
using MyManagedBus = entt::ManagedBus<EventA, EventB, EventC>;
|
||||
testRegUnregEmit<MyManagedBus>(std::make_shared<MyListener>());
|
||||
}
|
||||
|
||||
TEST(ManagedBus, ExpiredListeners) {
|
||||
entt::ManagedBus<EventA, EventB, EventC> bus;
|
||||
auto listener = std::make_shared<MyListener>();
|
||||
|
||||
listener->reset();
|
||||
bus.reg(listener);
|
||||
bus.template publish<EventA>(40, 2);
|
||||
bus.template publish<EventB>();
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))2);
|
||||
ASSERT_FALSE(bus.empty());
|
||||
ASSERT_EQ(listener->A, 1);
|
||||
ASSERT_EQ(listener->B, 0);
|
||||
|
||||
listener->reset();
|
||||
listener = nullptr;
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))2);
|
||||
ASSERT_FALSE(bus.empty());
|
||||
|
||||
EXPECT_NO_THROW(bus.template publish<EventA>(40, 2));
|
||||
EXPECT_NO_THROW(bus.template publish<EventC>());
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))0);
|
||||
ASSERT_TRUE(bus.empty());
|
||||
}
|
||||
|
||||
TEST(UnmanagedBus, RegUnregEmit) {
|
||||
using MyUnmanagedBus = entt::UnmanagedBus<EventA, EventB, EventC>;
|
||||
auto ptr = std::make_unique<MyListener>();
|
||||
testRegUnregEmit<MyUnmanagedBus>(ptr.get());
|
||||
}
|
||||
|
||||
TEST(UnmanagedBus, ExpiredListeners) {
|
||||
entt::UnmanagedBus<EventA, EventB, EventC> bus;
|
||||
auto listener = std::make_unique<MyListener>();
|
||||
|
||||
listener->reset();
|
||||
bus.reg(listener.get());
|
||||
bus.template publish<EventA>(40, 2);
|
||||
bus.template publish<EventB>();
|
||||
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))2);
|
||||
ASSERT_FALSE(bus.empty());
|
||||
ASSERT_EQ(listener->A, 1);
|
||||
ASSERT_EQ(listener->B, 0);
|
||||
|
||||
listener->reset();
|
||||
listener = nullptr;
|
||||
|
||||
// dangling pointer inside ... well, unmanaged means unmanaged!! :-)
|
||||
ASSERT_EQ(bus.size(), (decltype(bus.size()))2);
|
||||
ASSERT_FALSE(bus.empty());
|
||||
}
|
||||
45
test/entt/signal/delegate.cpp
Normal file
45
test/entt/signal/delegate.cpp
Normal file
@@ -0,0 +1,45 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/signal/delegate.hpp>
|
||||
|
||||
int f(int i) {
|
||||
return i*i;
|
||||
}
|
||||
|
||||
struct S {
|
||||
int f(int i) {
|
||||
return i+i;
|
||||
}
|
||||
};
|
||||
|
||||
TEST(Delegate, Functionalities) {
|
||||
entt::Delegate<int(int)> ffdel;
|
||||
entt::Delegate<int(int)> mfdel;
|
||||
S test;
|
||||
|
||||
ASSERT_EQ(ffdel(42), int{});
|
||||
ASSERT_EQ(mfdel(42), int{});
|
||||
|
||||
ffdel.connect<&f>();
|
||||
mfdel.connect<S, &S::f>(&test);
|
||||
|
||||
ASSERT_EQ(ffdel(3), 9);
|
||||
ASSERT_EQ(mfdel(3), 6);
|
||||
|
||||
ffdel.reset();
|
||||
mfdel.reset();
|
||||
|
||||
ASSERT_EQ(ffdel(42), int{});
|
||||
ASSERT_EQ(mfdel(42), int{});
|
||||
}
|
||||
|
||||
TEST(Delegate, Comparison) {
|
||||
entt::Delegate<int(int)> delegate;
|
||||
entt::Delegate<int(int)> def;
|
||||
delegate.connect<&f>();
|
||||
|
||||
ASSERT_EQ(def, entt::Delegate<int(int)>{});
|
||||
ASSERT_NE(def, delegate);
|
||||
|
||||
ASSERT_TRUE(def == entt::Delegate<int(int)>{});
|
||||
ASSERT_TRUE (def != delegate);
|
||||
}
|
||||
47
test/entt/signal/dispatcher.cpp
Normal file
47
test/entt/signal/dispatcher.cpp
Normal file
@@ -0,0 +1,47 @@
|
||||
#include <memory>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/signal/dispatcher.hpp>
|
||||
|
||||
struct Event {};
|
||||
|
||||
struct Receiver {
|
||||
void receive(const Event &) { ++cnt; }
|
||||
void reset() { cnt = 0; }
|
||||
std::size_t cnt{0};
|
||||
};
|
||||
|
||||
template<typename Dispatcher, typename Rec>
|
||||
void testDispatcher(Rec receiver) {
|
||||
Dispatcher dispatcher;
|
||||
|
||||
dispatcher.template connect<Event>(receiver);
|
||||
dispatcher.template trigger<Event>();
|
||||
dispatcher.template enqueue<Event>();
|
||||
|
||||
ASSERT_EQ(receiver->cnt, static_cast<decltype(receiver->cnt)>(1));
|
||||
|
||||
dispatcher.update();
|
||||
dispatcher.update();
|
||||
dispatcher.template trigger<Event>();
|
||||
|
||||
ASSERT_EQ(receiver->cnt, static_cast<decltype(receiver->cnt)>(3));
|
||||
|
||||
receiver->reset();
|
||||
|
||||
dispatcher.template disconnect<Event>(receiver);
|
||||
dispatcher.template trigger<Event>();
|
||||
dispatcher.template enqueue<Event>();
|
||||
dispatcher.update();
|
||||
dispatcher.template trigger<Event>();
|
||||
|
||||
ASSERT_EQ(receiver->cnt, static_cast<decltype(receiver->cnt)>(0));
|
||||
}
|
||||
|
||||
TEST(ManagedDispatcher, Basics) {
|
||||
testDispatcher<entt::ManagedDispatcher>(std::make_shared<Receiver>());
|
||||
}
|
||||
|
||||
TEST(UnmanagedDispatcher, Basics) {
|
||||
auto ptr = std::make_unique<Receiver>();
|
||||
testDispatcher<entt::UnmanagedDispatcher>(ptr.get());
|
||||
}
|
||||
117
test/entt/signal/emitter.cpp
Normal file
117
test/entt/signal/emitter.cpp
Normal file
@@ -0,0 +1,117 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/signal/emitter.hpp>
|
||||
|
||||
struct TestEmitter: entt::Emitter<TestEmitter> {};
|
||||
|
||||
struct FooEvent { int i; char c; };
|
||||
struct BarEvent {};
|
||||
|
||||
TEST(Emitter, Clear) {
|
||||
TestEmitter emitter;
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
|
||||
emitter.on<FooEvent>([](const auto &, const auto &){});
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<FooEvent>());
|
||||
ASSERT_TRUE(emitter.empty<BarEvent>());
|
||||
|
||||
emitter.clear<BarEvent>();
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<FooEvent>());
|
||||
ASSERT_TRUE(emitter.empty<BarEvent>());
|
||||
|
||||
emitter.clear<FooEvent>();
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
ASSERT_TRUE(emitter.empty<FooEvent>());
|
||||
ASSERT_TRUE(emitter.empty<BarEvent>());
|
||||
|
||||
emitter.on<FooEvent>([](const auto &, const auto &){});
|
||||
emitter.on<BarEvent>([](const auto &, const auto &){});
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<FooEvent>());
|
||||
ASSERT_FALSE(emitter.empty<BarEvent>());
|
||||
|
||||
emitter.clear();
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
ASSERT_TRUE(emitter.empty<FooEvent>());
|
||||
ASSERT_TRUE(emitter.empty<BarEvent>());
|
||||
}
|
||||
|
||||
TEST(Emitter, ClearPublishing) {
|
||||
TestEmitter emitter;
|
||||
bool invoked = false;
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
|
||||
emitter.on<BarEvent>([&invoked](const auto &, auto &em){
|
||||
invoked = true;
|
||||
em.clear();
|
||||
});
|
||||
|
||||
emitter.publish<BarEvent>();
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
ASSERT_TRUE(invoked);
|
||||
}
|
||||
|
||||
TEST(Emitter, On) {
|
||||
TestEmitter emitter;
|
||||
|
||||
emitter.on<FooEvent>([](const auto &, const auto &){});
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<FooEvent>());
|
||||
|
||||
emitter.publish<FooEvent>(0, 'c');
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<FooEvent>());
|
||||
}
|
||||
|
||||
TEST(Emitter, Once) {
|
||||
TestEmitter emitter;
|
||||
|
||||
emitter.once<BarEvent>([](const auto &, const auto &){});
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<BarEvent>());
|
||||
|
||||
emitter.publish<BarEvent>();
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
ASSERT_TRUE(emitter.empty<BarEvent>());
|
||||
}
|
||||
|
||||
TEST(Emitter, OnceAndErase) {
|
||||
TestEmitter emitter;
|
||||
|
||||
auto conn = emitter.once<FooEvent>([](const auto &, const auto &){});
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<FooEvent>());
|
||||
|
||||
emitter.erase(conn);
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
ASSERT_TRUE(emitter.empty<FooEvent>());
|
||||
}
|
||||
|
||||
TEST(Emitter, OnAndErase) {
|
||||
TestEmitter emitter;
|
||||
|
||||
auto conn = emitter.on<BarEvent>([](const auto &, const auto &){});
|
||||
|
||||
ASSERT_FALSE(emitter.empty());
|
||||
ASSERT_FALSE(emitter.empty<BarEvent>());
|
||||
|
||||
emitter.erase(conn);
|
||||
|
||||
ASSERT_TRUE(emitter.empty());
|
||||
ASSERT_TRUE(emitter.empty<BarEvent>());
|
||||
}
|
||||
@@ -75,6 +75,17 @@ struct S {
|
||||
static void f(int &v) { v = 42; }
|
||||
};
|
||||
|
||||
TEST(SigH, Clear) {
|
||||
entt::SigH<void(int &)> sigh;
|
||||
sigh.connect<&S::f>();
|
||||
|
||||
ASSERT_FALSE(sigh.empty());
|
||||
|
||||
sigh.clear();
|
||||
|
||||
ASSERT_TRUE(sigh.empty());
|
||||
}
|
||||
|
||||
TEST(SigH, Functions) {
|
||||
entt::SigH<void(int &)> sigh;
|
||||
int v = 0;
|
||||
@@ -93,6 +104,8 @@ TEST(SigH, Functions) {
|
||||
ASSERT_TRUE(sigh.empty());
|
||||
ASSERT_EQ((entt::SigH<bool(int)>::size_type)0, sigh.size());
|
||||
ASSERT_EQ(0, v);
|
||||
|
||||
sigh.connect<&S::f>();
|
||||
}
|
||||
|
||||
TEST(SigH, Members) {
|
||||
|
||||
164
test/entt/signal/signal.cpp
Normal file
164
test/entt/signal/signal.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
#include <gtest/gtest.h>
|
||||
#include <entt/signal/signal.hpp>
|
||||
|
||||
struct S {
|
||||
static void f(const int &j) { i = j; }
|
||||
void g(const int &j) { i = j; }
|
||||
void h(const int &) {}
|
||||
static int i;
|
||||
};
|
||||
|
||||
int S::i = 0;
|
||||
|
||||
TEST(Signal, Lifetime) {
|
||||
using signal = entt::Signal<void(void)>;
|
||||
|
||||
ASSERT_NO_THROW(signal{});
|
||||
|
||||
signal src{}, other{};
|
||||
|
||||
ASSERT_NO_THROW(signal{src});
|
||||
ASSERT_NO_THROW(signal{std::move(other)});
|
||||
ASSERT_NO_THROW(src = other);
|
||||
ASSERT_NO_THROW(src = std::move(other));
|
||||
|
||||
ASSERT_NO_THROW(delete new signal{});
|
||||
}
|
||||
|
||||
TEST(Signal, Comparison) {
|
||||
struct S {
|
||||
void f() {}
|
||||
void g() {}
|
||||
};
|
||||
|
||||
entt::Signal<void()> sig1;
|
||||
entt::Signal<void()> sig2;
|
||||
|
||||
auto s1 = std::make_shared<S>();
|
||||
auto s2 = std::make_shared<S>();
|
||||
|
||||
sig1.connect<S, &S::f>(s1);
|
||||
sig2.connect<S, &S::f>(s2);
|
||||
|
||||
ASSERT_FALSE(sig1 == sig2);
|
||||
ASSERT_TRUE(sig1 != sig2);
|
||||
|
||||
sig1.disconnect<S, &S::f>(s1);
|
||||
sig2.disconnect<S, &S::f>(s2);
|
||||
|
||||
sig1.connect<S, &S::f>(s1);
|
||||
sig2.connect<S, &S::g>(s1);
|
||||
|
||||
ASSERT_FALSE(sig1 == sig2);
|
||||
ASSERT_TRUE(sig1 != sig2);
|
||||
|
||||
sig1.disconnect<S, &S::f>(s1);
|
||||
sig2.disconnect<S, &S::g>(s1);
|
||||
|
||||
ASSERT_TRUE(sig1 == sig2);
|
||||
ASSERT_FALSE(sig1 != sig2);
|
||||
|
||||
sig1.connect<S, &S::f>(s1);
|
||||
sig1.connect<S, &S::g>(s1);
|
||||
sig2.connect<S, &S::f>(s1);
|
||||
sig2.connect<S, &S::g>(s1);
|
||||
|
||||
ASSERT_TRUE(sig1 == sig2);
|
||||
|
||||
sig1.disconnect<S, &S::f>(s1);
|
||||
sig1.disconnect<S, &S::g>(s1);
|
||||
sig2.disconnect<S, &S::f>(s1);
|
||||
sig2.disconnect<S, &S::g>(s1);
|
||||
|
||||
sig1.connect<S, &S::f>(s1);
|
||||
sig1.connect<S, &S::g>(s1);
|
||||
sig2.connect<S, &S::g>(s1);
|
||||
sig2.connect<S, &S::f>(s1);
|
||||
|
||||
ASSERT_FALSE(sig1 == sig2);
|
||||
}
|
||||
|
||||
TEST(Signal, Clear) {
|
||||
entt::Signal<void(const int &)> signal;
|
||||
signal.connect<&S::f>();
|
||||
|
||||
ASSERT_FALSE(signal.empty());
|
||||
|
||||
signal.clear();
|
||||
|
||||
ASSERT_TRUE(signal.empty());
|
||||
}
|
||||
|
||||
TEST(Signal, Functions) {
|
||||
entt::Signal<void(const int &)> signal;
|
||||
auto val = S::i + 1;
|
||||
|
||||
signal.connect<&S::f>();
|
||||
signal.publish(val);
|
||||
|
||||
ASSERT_FALSE(signal.empty());
|
||||
ASSERT_EQ(entt::Signal<void(const int &)>::size_type{1}, signal.size());
|
||||
ASSERT_EQ(S::i, val);
|
||||
|
||||
signal.disconnect<&S::f>();
|
||||
signal.publish(val+1);
|
||||
|
||||
ASSERT_TRUE(signal.empty());
|
||||
ASSERT_EQ(entt::Signal<void(const int &)>::size_type{0}, signal.size());
|
||||
ASSERT_EQ(S::i, val);
|
||||
}
|
||||
|
||||
TEST(Signal, Members) {
|
||||
entt::Signal<void(const int &)> signal;
|
||||
auto ptr = std::make_shared<S>();
|
||||
auto val = S::i + 1;
|
||||
|
||||
signal.connect<S, &S::g>(ptr);
|
||||
signal.publish(val);
|
||||
|
||||
ASSERT_FALSE(signal.empty());
|
||||
ASSERT_EQ(entt::Signal<void(const int &)>::size_type{1}, signal.size());
|
||||
ASSERT_EQ(S::i, val);
|
||||
|
||||
signal.disconnect<S, &S::g>(ptr);
|
||||
signal.publish(val+1);
|
||||
|
||||
ASSERT_TRUE(signal.empty());
|
||||
ASSERT_EQ(entt::Signal<void(const int &)>::size_type{0}, signal.size());
|
||||
ASSERT_EQ(S::i, val);
|
||||
|
||||
++val;
|
||||
|
||||
signal.connect<S, &S::g>(ptr);
|
||||
signal.connect<S, &S::h>(ptr);
|
||||
signal.publish(val);
|
||||
|
||||
ASSERT_FALSE(signal.empty());
|
||||
ASSERT_EQ(entt::Signal<void(const int &)>::size_type{2}, signal.size());
|
||||
ASSERT_EQ(S::i, val);
|
||||
|
||||
signal.disconnect(ptr);
|
||||
signal.publish(val+1);
|
||||
|
||||
ASSERT_TRUE(signal.empty());
|
||||
ASSERT_EQ(entt::Signal<void(const int &)>::size_type{0}, signal.size());
|
||||
ASSERT_EQ(S::i, val);
|
||||
}
|
||||
|
||||
TEST(Signal, Cleanup) {
|
||||
entt::Signal<void(const int &)> signal;
|
||||
auto ptr = std::make_shared<S>();
|
||||
signal.connect<S, &S::g>(ptr);
|
||||
auto val = S::i;
|
||||
ptr = nullptr;
|
||||
|
||||
ASSERT_FALSE(signal.empty());
|
||||
ASSERT_EQ(S::i, val);
|
||||
|
||||
signal.publish(val);
|
||||
|
||||
ASSERT_TRUE(signal.empty());
|
||||
ASSERT_EQ(S::i, val);
|
||||
}
|
||||
Reference in New Issue
Block a user