dispatcher: use fast path if available, standard path otherwise

This commit is contained in:
Michele Caini
2020-04-05 22:07:12 +02:00
parent 819cd623f9
commit a6fddd6a2c

View File

@@ -81,17 +81,23 @@ class dispatcher {
template<typename Event>
pool_handler<Event> & assure() {
static_assert(std::is_same_v<Event, std::decay_t<Event>>);
const auto index = type_index<Event>::value();
if(!(index < pools.size())) {
pools.resize(index+1);
if constexpr(has_type_index_v<Event>) {
const auto index = type_index<Event>::value();
if(!(index < pools.size())) {
pools.resize(index+1);
}
if(!pools[index]) {
pools[index].reset(new pool_handler<Event>{});
}
return static_cast<pool_handler<Event> &>(*pools[index]);
} else {
auto it = std::find_if(pools.begin(), pools.end(), [id = type_info<Event>::id()](const auto &cpool) { return id == cpool->type_id(); });
return static_cast<pool_handler<Event> &>(it == pools.cend() ? *pools.emplace_back(new pool_handler<Event>{}) : **it);
}
if(!pools[index]) {
pools[index].reset(new pool_handler<Event>{});
}
return static_cast<pool_handler<Event> &>(*pools[index]);
}
public: