34 lines
762 B
C++
34 lines
762 B
C++
#pragma once
|
|
#include <mutex>
|
|
#include <unordered_map>
|
|
#include <shared_mutex>
|
|
#include <vector>
|
|
#include <string_view>
|
|
#include "Types.hpp"
|
|
|
|
namespace vl::mixin {
|
|
|
|
class CallbackRegistry {
|
|
public:
|
|
static CallbackId add(ErasedCallback cb) {
|
|
static CallbackId next = 1;
|
|
std::unique_lock lk(mu_);
|
|
auto id = next++;
|
|
table_.emplace(id, std::move(cb));
|
|
return id;
|
|
}
|
|
|
|
static ErasedCallback* get(CallbackId id) {
|
|
std::shared_lock lk(mu_);
|
|
auto it = table_.find(id);
|
|
if (it == table_.end()) return nullptr;
|
|
return &it->second;
|
|
}
|
|
|
|
private:
|
|
static inline std::shared_mutex mu_;
|
|
static inline std::unordered_map<CallbackId, ErasedCallback> table_;
|
|
};
|
|
|
|
} // namespace vl::mixin
|