Skip to content

Commit

Permalink
style: 🎨 Format event_handler.hpp
Browse files Browse the repository at this point in the history
  • Loading branch information
ion098 committed Jun 17, 2024
1 parent 1ca81b0 commit 0772340
Showing 1 changed file with 36 additions and 34 deletions.
70 changes: 36 additions & 34 deletions include/gamepad/event_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,46 @@
namespace Gamepad {

class MonotonicCounter {
template<typename... Args> friend class EventHandler;
static uint32_t next_value() {
static std::atomic<uint32_t> counter = 0;
return ++counter;
}
template <typename... Args> friend class EventHandler;

static uint32_t next_value() {
static std::atomic<uint32_t> counter = 0;
return ++counter;
}
};

template <typename... Args>
class EventHandler {
template <typename... Args> class EventHandler {
public:
using Listener = std::function<void(Args...)>;
uint32_t add_listener(Listener func) {
std::lock_guard lock(mutex);
uint32_t id = MonotonicCounter::next_value();
listeners.emplace(id, std::move(func));
return id;
}
bool remove_listener(uint32_t id) {
std::lock_guard lock(mutex);
if(listeners.find(id) == listeners.end()) {
TODO("change handling maybe?")
return false;
using Listener = std::function<void(Args...)>;

uint32_t add_listener(Listener func) {
std::lock_guard lock(mutex);
uint32_t id = MonotonicCounter::next_value();
listeners.emplace(id, std::move(func));
return id;
}

bool remove_listener(uint32_t id) {
std::lock_guard lock(mutex);
if (listeners.find(id) == listeners.end()) {
TODO("change handling maybe?")
return false;
}
listeners.erase(id);
return true;
}

bool is_empty() {
std::lock_guard lock(mutex);
return listeners.empty();
}
listeners.erase(id);
return true;
}
bool is_empty() {
std::lock_guard lock(mutex);
return listeners.empty();
}
void fire(Args... args) {
std::lock_guard lock(mutex);
for(auto listener : listeners) {
listener.second(args...);

void fire(Args... args) {
std::lock_guard lock(mutex);
for (auto listener : listeners) { listener.second(args...); }
}
}
private:
std::map<uint32_t, Listener> listeners;
pros::Mutex mutex;
std::map<uint32_t, Listener> listeners;
pros::Mutex mutex;
};
}
} // namespace Gamepad

0 comments on commit 0772340

Please sign in to comment.