- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
template<typename T>
struct [[nodiscard]] InplacePointerGuard {
InplacePointerGuard(T *& ptr_) : ptr(ptr_) {}
~InplacePointerGuard()
{
if (ptr != nullptr) {
ptr->~T();
}
}
T *& ptr;
};
template<typename EventType, typename... Args>
bool publishEmplace(Args &&... args) const
{
static_assert((std::is_base_of_v<Event, EventType> && std::is_convertible_v<const EventType *, const Event *>),
"EventType must be a public subtype of Event");
auto typeIdx = Event::getStaticIndex<EventType>();
std::aligned_storage_t<sizeof(EventType), alignof(EventType)> eventStorage;
EventType *eventConstructed = nullptr;
auto guard = InplacePointerGuard(eventConstructed);
publishEmplaceImpl<EventType>(typeIdx, eventStorage, eventConstructed, std::forward<Args>(args)...);
return eventConstructed != nullptr;
}
template<typename EventType, typename... Args>
static EventType *constructEvent(std::aligned_storage_t<sizeof(EventType), alignof(EventType)> & eventStorage,
Args &&... args)
{
return std::launder(new(&eventStorage) EventType(std::forward<Args>(args)...));
}
template<typename EventType, typename... Args>
void publishEmplaceImpl(const std::type_index & typeIdx,
std::aligned_storage_t<sizeof(EventType), alignof(EventType)> & eventStorage,
EventType *& eventConstructed,
Args &&... args) const
{
if (auto it = callbacks.find(typeIdx); it != callbacks.end()) {
for (const auto & [subId, callback] : it->second) {
if (!eventConstructed) {
eventConstructed = constructEvent<EventType>(eventStorage, std::forward<Args>(args)...);
}
callback(*eventConstructed);
}
}
auto range = EventsRelation::RelationManager::getParentsRange(typeIdx);
for (auto it = range.first; it != range.second; ++it) {
if (!eventConstructed) {
publishEmplaceImpl<EventType>(it->second, eventStorage, eventConstructed, std::forward<Args>(args)...);
} else {
publishImpl(it->second, *eventConstructed);
}
}
}