载入中...
搜索中...
未找到
Capability.cpp
浏览该文件的文档.
1#include "common/Capability.h"
2
3#include <algorithm>
4#include <string>
5#include <unordered_map>
6#include <vector>
7
8namespace eve::cap::detail {
9
10namespace {
11
12struct Listener {
13 void* impl = nullptr;
14 int priority = 0;
15};
16
17struct Slot {
18 void* service = nullptr;
19 std::vector<Listener> listeners; // kept sorted by priority
20};
21
27std::unordered_map<std::string, Slot>& slots() {
28 static std::unordered_map<std::string, Slot> table;
29 return table;
30}
31
32} // namespace
33
34void provideRaw(const char* name, void* impl) {
35 if (!name) return;
36 slots()[name].service = impl;
37}
38
39void* queryRaw(const char* name) {
40 if (!name) return nullptr;
41 auto& table = slots();
42 auto it = table.find(name);
43 return it == table.end() ? nullptr : it->second.service;
44}
45
46void revokeRaw(const char* name, void* impl) {
47 if (!name) return;
48 auto& table = slots();
49 auto it = table.find(name);
50 // Only clear when `impl` is still the current provider, so a stale revoke
51 // from a torn-down module cannot unregister its replacement.
52 if (it != table.end() && it->second.service == impl) it->second.service = nullptr;
53}
54
55void addListenerRaw(const char* name, void* impl, int priority) {
56 if (!name || !impl) return;
57 auto& list = slots()[name].listeners;
58 for (const auto& l : list)
59 if (l.impl == impl) return; // idempotent
60 // stable_sort keeps registration order within one priority.
61 list.push_back({impl, priority});
62 std::stable_sort(list.begin(), list.end(),
63 [](const Listener& a, const Listener& b) { return a.priority < b.priority; });
64}
65
66void removeListenerRaw(const char* name, void* impl) {
67 if (!name) return;
68 auto& table = slots();
69 auto it = table.find(name);
70 if (it == table.end()) return;
71 auto& list = it->second.listeners;
72 list.erase(std::remove_if(list.begin(), list.end(),
73 [impl](const Listener& l) { return l.impl == impl; }),
74 list.end());
75}
76
77size_t listenerCountRaw(const char* name) {
78 if (!name) return 0;
79 auto& table = slots();
80 auto it = table.find(name);
81 return it == table.end() ? 0 : it->second.listeners.size();
82}
83
84void* listenerAtRaw(const char* name, size_t index) {
85 if (!name) return nullptr;
86 auto& table = slots();
87 auto it = table.find(name);
88 if (it == table.end() || index >= it->second.listeners.size()) return nullptr;
89 return it->second.listeners[index].impl;
90}
91
92void clearAllRaw() { slots().clear(); }
93
94} // namespace eve::cap::detail
void * service
int priority
std::vector< Listener > listeners
void * impl
uint32_t a
uint32_t b
const char * name
Definition RockMesh.cpp:21
void addListenerRaw(const char *name, void *impl, int priority)
void provideRaw(const char *name, void *impl)
void * queryRaw(const char *name)
void * listenerAtRaw(const char *name, size_t index)
void revokeRaw(const char *name, void *impl)
size_t listenerCountRaw(const char *name)
void removeListenerRaw(const char *name, void *impl)
I * query()
Definition Capability.h:77