Skip to content

Commit f9be359

Browse files
committed
Execution/EventSystem: Code cleaning
1 parent 7c69021 commit f9be359

1 file changed

Lines changed: 10 additions & 11 deletions

File tree

modules/Execution/EventSystem.mpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,21 @@ export namespace CppUtils::Execution
88
{
99
class EventSystem final
1010
{
11-
using GenericFunction = std::unique_ptr<void, void (*)(void*)>;
1211
using Key = std::pair<String::Hash, std::type_index>;
1312

1413
public:
1514
template<String::Hasher eventName = String::Hash{}>
16-
inline auto emit(auto event) -> void
15+
inline auto emit(const auto& event) -> void
1716
{
18-
using Event = decltype(event);
17+
using Event = std::remove_cvref_t<decltype(event)>;
1918
auto lockGuard = std::shared_lock{m_mutex};
2019

2120
auto key = std::make_pair(static_cast<String::Hash>(eventName), std::type_index{typeid(Event)});
2221
if (auto subscriberIt = m_subscribers.find(key); subscriberIt == std::cend(m_subscribers))
2322
return;
2423
else
2524
for (auto& function : subscriberIt->second)
26-
std::invoke(*static_cast<std::function<void(const Event&)>*>(function.get()), event);
25+
function(std::addressof(event));
2726
}
2827

2928
template<String::Hasher eventName = String::Hash{}>
@@ -34,16 +33,16 @@ export namespace CppUtils::Execution
3433
using ArgumentsTypes = FunctionInformations::ArgumentsTypes;
3534
constexpr auto nbArguments = std::tuple_size_v<ArgumentsTypes>;
3635
static_assert(nbArguments == 1, "EventSystem: subscribed callable must take exactly one argument");
37-
using Event = std::tuple_element_t<0, ArgumentsTypes>;
36+
using Event = std::remove_cvref_t<std::tuple_element_t<0, ArgumentsTypes>>;
3837

3938
auto lockGuard = std::unique_lock{m_mutex};
40-
4139
auto key = std::make_pair(static_cast<String::Hash>(eventName), std::type_index{typeid(Event)});
4240

43-
using Function = std::function<void(const Event&)>;
44-
auto functionPointer = std::make_unique<Function>(std::forward<decltype(function)>(function));
45-
auto genericPointer = GenericFunction{functionPointer.release(), [](void* pointer) { delete static_cast<Function*>(pointer); }};
46-
m_subscribers[key].emplace_back(std::move(genericPointer));
41+
m_subscribers[key].emplace_back(
42+
[function = std::forward<decltype(function)>(function)](const void* baseEvent) -> void {
43+
const Event& event = *static_cast<const Event*>(baseEvent);
44+
function(event);
45+
});
4746
}
4847

4948
private:
@@ -56,6 +55,6 @@ export namespace CppUtils::Execution
5655
};
5756

5857
std::shared_mutex m_mutex;
59-
std::unordered_map<Key, std::vector<GenericFunction>, PairHasher> m_subscribers;
58+
std::unordered_map<Key, std::vector<std::function<void(const void*)>>, PairHasher> m_subscribers;
6059
};
6160
}

0 commit comments

Comments
 (0)