Formatting, add remote calling to c library

This commit is contained in:
2026-02-18 09:18:26 -05:00
parent 0088a9070b
commit 376b0b5285
49 changed files with 1915 additions and 1593 deletions

View File

@@ -7,41 +7,44 @@
class Event;
class CallbackHandle {
public:
CallbackHandle(Event *evt, int id) : evt(evt), id(id) {}
~CallbackHandle();
public:
CallbackHandle(Event *evt, int id) : evt(evt), id(id) {
}
~CallbackHandle();
private:
Event *evt;
int id;
private:
Event *evt;
int id;
};
class Event {
public:
using Callback = std::function<void()>;
public:
using Callback = std::function<void()>;
CallbackHandle addListener(Callback cb) {
int id = nextId++;
callbacks[id] = std::move(cb);
return CallbackHandle(this, id);
}
CallbackHandle addListener(Callback cb) {
int id = nextId++;
callbacks[id] = std::move(cb);
return CallbackHandle(this, id);
}
void remove(int id) { callbacks.erase(id); }
void remove(int id) {
callbacks.erase(id);
}
void fire() {
for (auto &kv : callbacks)
kv.second();
}
void fire() {
for (auto &kv : callbacks)
kv.second();
}
private:
friend class CallbackHandle;
std::unordered_map<int, Callback> callbacks;
int nextId = 0;
private:
friend class CallbackHandle;
std::unordered_map<int, Callback> callbacks;
int nextId = 0;
};
CallbackHandle::~CallbackHandle() {
if (evt)
evt->remove(id);
if (evt)
evt->remove(id);
}
#endif // CONTROL_EVENT_H

View File

@@ -7,12 +7,12 @@
template <typename K, typename V, typename H>
std::vector<V> map_to_values(const std::unordered_map<K, V, H> &map) {
std::vector<V> out;
out.reserve(map.size());
for (auto const &[key, value] : map) {
out.push_back(value);
}
return out;
std::vector<V> out;
out.reserve(map.size());
for (auto const &[key, value] : map) {
out.push_back(value);
}
return out;
}
#endif // CONTROL_MAP_H

View File

@@ -6,9 +6,9 @@
// Custom hash function for std::pair
template <typename T> struct pair_hash {
std::size_t operator()(const std::pair<T, T> &p) const {
return std::hash<T>()(p.first) ^ (std::hash<T>()(p.second) << 1);
}
std::size_t operator()(const std::pair<T, T> &p) const {
return std::hash<T>()(p.first) ^ (std::hash<T>()(p.second) << 1);
}
};
#endif // CONTROL_PAIRHASH_H

View File

@@ -5,7 +5,7 @@
#include <variant> // NOLINT
template <class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
using Ts::operator()...;
};
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;