-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Zephyr: Renderer: ensure that delete tasks cannot be scheduled past t…
…he geometry cache's lifetime
- Loading branch information
Showing
5 changed files
with
81 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
|
||
#pragma once | ||
|
||
#include <zephyr/integer.hpp> | ||
#include <zephyr/panic.hpp> | ||
#include <algorithm> | ||
#include <functional> | ||
#include <limits> | ||
#include <utility> | ||
#include <vector> | ||
|
||
namespace zephyr { | ||
|
||
template<typename... Args> | ||
class Event { | ||
public: | ||
using SubID = u64; | ||
using Handler = std::function<void(Args...)>; | ||
|
||
struct Subscription { | ||
Subscription(SubID id, Handler handler) : id{id}, handler{handler} {} | ||
SubID id; | ||
Handler handler; | ||
}; | ||
|
||
Event() = default; | ||
Event(const Event& other) = delete; | ||
|
||
Event& operator=(Event const& other) = delete; | ||
|
||
void Emit(Args&&... args) const { | ||
for(const Subscription& subscription : m_subscription_list) { | ||
subscription.handler(std::forward<Args>(args)...); | ||
} | ||
} | ||
|
||
SubID Subscribe(Handler handler) { | ||
return m_subscription_list.emplace_back(NextID(), std::move(handler)).id; | ||
} | ||
|
||
void Unsubscribe(SubID id) { | ||
const auto match = std::ranges::find_if(m_subscription_list, [id](const Subscription& subscription) { return subscription.id == id; }); | ||
if(match != m_subscription_list.end()) { | ||
m_subscription_list.erase(match); | ||
} | ||
} | ||
|
||
private: | ||
SubID NextID() { | ||
if(m_next_id == std::numeric_limits<decltype(m_next_id)>::max()) [[unlikely]] { | ||
ZEPHYR_PANIC("Reached the maximum number of event subscriptions. What?"); | ||
} | ||
return m_next_id++; | ||
} | ||
|
||
SubID m_next_id{0u}; | ||
std::vector<Subscription> m_subscription_list{}; | ||
}; | ||
|
||
typedef Event<> VoidEvent; | ||
|
||
} // namespace zephyr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters