config: type index based lookup as opt-in alternative (close #490)

This commit is contained in:
Michele Caini
2020-06-03 15:32:30 +02:00
parent 290e942f3e
commit 98df1d6004
7 changed files with 32 additions and 16 deletions

View File

@@ -7,6 +7,7 @@
* [Introduction](#introduction)
* [Definitions](#definitions)
* [ENTT_STANDALONE](#entt_standalone)
* [ENTT_NOEXCEPT](#entt_noexcept)
* [ENTT_HS_SUFFIX and ENTT_HWS_SUFFIX](#entt_hs_suffix_and_entt_hws_suffix)
* [ENTT_USE_ATOMIC](#entt_use_atomic)
@@ -37,6 +38,14 @@ Each parameter can result in internal library definitions. It's not recommended
to try to also modify these definitions, since there is no guarantee that they
will remain stable over time unlike the options below.
## ENTT_STANDALONE
`EnTT` is designed in such a way that it works (almost) everywhere out of the
box. However, this is the result of many refinements over time and a compromise
regarding some optimizations.<br/>
It's worth noting that users can get a small performance boost by passing this
definition to the compiler when the library is used in a standalone application.
## ENTT_NOEXCEPT
The purpose of this parameter is to suppress the use of `noexcept` by this
@@ -96,5 +105,5 @@ dedicated storage for them.
After many adventures, `EnTT` finally works fine across boundaries.<br/>
To do this, the library mixes some non-standard language features with others
that are perfectly compliant.<br/>
This definition will make the library use only standard techniques, that is,
functionalities that are fully compliant with the standard C++.
This definition will prevent the library from using non-standard techniques,
that is, functionalities that aren't fully compliant with the standard C++.

View File

@@ -198,8 +198,9 @@ library or that will never be.
## Type info
This class template isn't a drop-in replacement for `std::type_info` but can
provide similar information which are not implementation defined at least.
Therefore, they can sometimes be even more reliable than those obtained otherwise.
provide similar information which are not implementation defined at least.<br/>
Therefore, they can sometimes be even more reliable than those obtained
otherwise.
Currently, the only information available is the numeric identifier associated
with a given type:
@@ -212,11 +213,11 @@ In general, the `id` function is also `constexpr` but this isn't guaranteed for
all compilers and platforms (although it's valid with the most well-known and
popular ones).<br/>
This function **can** use non-standard features of the language for its own
purposes. This allows it to provide compile-time identifiers that remain stable
across different runs. However, it's possible to force the use of standard
features only by defining the macro `ENTT_STANDARD_CPP`. In this case, there is
no guarantee that the identifiers are stable across executions though. Moreover,
identifiers are generated at runtime and are no longer a compile-time thing.
purposes. This makes it possible to provide compile-time identifiers that remain
stable across different runs. In all cases, users can prevent the library from
using these features by means of the `ENTT_STANDARD_CPP` definition. In this
case, there is no guarantee that identifiers remain stable across executions.
Moreover, they are generated at runtime and are no longer a compile-time thing.
An external type system can also be used if needed. In fact, `type_info` can be
specialized by type and is also _sfinae-friendly_ in order to allow more refined
@@ -303,10 +304,8 @@ struct entt::type_index<Type, std::void_d<decltype(Type::index())>> {
```
Note that indexes **must** still be generated sequentially in this case.<br/>
The tool is widely used within `EnTT`. It also plays a very important role in
making `EnTT` work nicely across boundaries in many cases. Generating indices
not sequentially would break an assumption and would likely lead to undesired
behaviors.
The tool is widely used within `EnTT`. Generating indices not sequentially would
break an assumption and would likely lead to undesired behaviors.
## Type traits

View File

@@ -66,4 +66,11 @@
#endif
#ifndef ENTT_STANDALONE
# define ENTT_FAST_PATH(...) false
#else
# define ENTT_FAST_PATH(Cond) Cond
#endif
#endif

View File

@@ -183,7 +183,7 @@ class basic_registry {
const pool_handler<Component> & assure() const {
const sparse_set<entity_type> *cpool;
if constexpr(has_type_index_v<Component>) {
if constexpr(ENTT_FAST_PATH(has_type_index_v<Component>)) {
const auto index = type_index<Component>::value();
if(!(index < pools.size())) {

View File

@@ -86,7 +86,7 @@ class dispatcher {
pool_handler<Event> & assure() {
static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
if constexpr(has_type_index_v<Event>) {
if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
const auto index = type_index<Event>::value();
if(!(index < pools.size())) {

View File

@@ -128,7 +128,7 @@ class emitter {
const pool_handler<Event> & assure() const {
static_assert(std::is_same_v<Event, std::decay_t<Event>>, "Invalid event type");
if constexpr(has_type_index_v<Event>) {
if constexpr(ENTT_FAST_PATH(has_type_index_v<Event>)) {
const auto index = type_index<Event>::value();
if(!(index < pools.size())) {

View File

@@ -65,6 +65,7 @@ function(SETUP_BASIC_TEST TEST_NAME TEST_SOURCES)
target_link_libraries(${TEST_NAME} PRIVATE GTest::Main Threads::Threads)
SETUP_TARGET(${TEST_NAME})
add_test(NAME ${TEST_NAME} COMMAND ${TEST_NAME})
target_compile_definitions(${TEST_NAME} PRIVATE ENTT_STANDALONE)
endfunction()
function(SETUP_LIB_TEST TEST_NAME)