Saga
Saga Game Engine
Loading...
Searching...
No Matches
eventmap.h
Go to the documentation of this file.
1#pragma once
2
3#include <atomic>
4#include <unordered_map>
5#include <functional>
6#include <iostream>
7#include <memory>
8#include "../_Core/logger.h"
9
10// maps event (practically ints) to functions
11namespace Saga {
15 class ICallback {
16 public:
17 virtual ~ICallback() = default;
18 };
19
25 template <typename ...DataType>
26 class Callback : public ICallback {
27 public:
28 Callback(std::shared_ptr<std::function<void(DataType...)>> func) : func(func) {}
29
35 void evoke(DataType... args) { (*func)(args...); }
36
37 private:
38 std::shared_ptr<std::function<void(DataType...)>> func;
39 };
40
45 class EventMap {
46 public:
47 enum Id: uint64_t {};
48 private:
49 using Map = std::unordered_map<int, std::unordered_map<Id, std::shared_ptr<ICallback>>>;
50 public:
51 typedef typename Map::iterator iterator;
52 typedef typename Map::const_iterator const_iterator;
53 typedef typename Map::value_type value_type;
54
55 const_iterator begin() const { return map.begin(); }
56 const_iterator end() const { return map.end(); }
57 iterator begin() { return map.begin(); }
58 iterator end() { return map.end(); }
59
60 template <typename Event>
61 iterator find(Event event) { return map.find((int) event); }
62
63 template <typename Event>
64 const_iterator find(Event event) const { return map.find((int) event); }
65
75 template <typename Event, typename ...DataType>
76 Id addListener(Event event, std::shared_ptr<std::function<void(DataType...)>> callbackFunc) {
77 const Id id = Id(++id_value());
78
79 map[(int) event][id] = std::make_shared<Callback<DataType...>>(callbackFunc);
80 return id;
81 }
82
90 template <typename Event>
91 void removeListener(Event event, Id id) {
92 if (map.count((int) event)) {
93 int removeCnt = map[(int) event].erase(id);
94 if (!removeCnt) SWARN("Trying to remove listener %d from event %d, but the event does not have this listener.", id, event);
95 } else
96 SWARN("Trying to remove a listener from event %d, but the event does not exist.", event);
97 }
98
107 template <typename Event, typename ...DataType>
108 void invoke(Event event, DataType... args) {
109 if (map.count((int) event))
110 for (auto& pp : map[(int) event])
111 std::dynamic_pointer_cast<Callback<DataType...>>(pp.second)->evoke(args...);
112 }
113
114 private:
115 Map map;
116 static auto id_value()
117 -> uint64_t & {
118 static uint64_t the_id;
119 return the_id;
120 }
121 };
122}
A wrapper around a shared_ptr to a function.
Definition: eventmap.h:26
Callback(std::shared_ptr< std::function< void(DataType...)> > func)
Definition: eventmap.h:28
void evoke(DataType... args)
Call the function this stores.
Definition: eventmap.h:35
A mapping between events (which are ints under the hood) and callbacks.
Definition: eventmap.h:45
Map::iterator iterator
Definition: eventmap.h:51
iterator begin()
Definition: eventmap.h:57
iterator find(Event event)
Definition: eventmap.h:61
Map::const_iterator const_iterator
Definition: eventmap.h:52
const_iterator find(Event event) const
Definition: eventmap.h:64
Map::value_type value_type
Definition: eventmap.h:53
void removeListener(Event event, Id id)
Remove a listener from an event, by their id.
Definition: eventmap.h:91
Id
Definition: eventmap.h:47
const_iterator end() const
Definition: eventmap.h:56
void invoke(Event event, DataType... args)
Invoke an event. This calls all listeners attached to that event, with the specified parameters.
Definition: eventmap.h:108
Id addListener(Event event, std::shared_ptr< std::function< void(DataType...)> > callbackFunc)
Add a listener to a specific event.
Definition: eventmap.h:76
iterator end()
Definition: eventmap.h:58
const_iterator begin() const
Definition: eventmap.h:55
Generic callback used for events.
Definition: eventmap.h:15
virtual ~ICallback()=default
#define SWARN(message,...)
Log warn-level messages.
Definition: logger.h:77
Definition: app.cpp:8