/
manager_impl.hpp
106 lines (83 loc) · 2.8 KB
/
manager_impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
#include "game_events/fwd.hpp"
#include "config.hpp"
#include <unordered_map>
namespace game_events
{
// event_handlers is essentially the implementation details of the manager
class event_handlers
{
private:
using handler_vec_t = std::vector<handler_ptr>;
using map_t = std::unordered_map<std::string, handler_list>;
using id_map_t = std::unordered_map<std::string, weak_handler_ptr>;
/**
* Active event handlers. Will not have elements removed unless the event_handlers is clear()ed.
* This is the only container that actually 'owns' any events in the form of shared_ptrs. The other
* three storage methods own weak_ptrs.
*/
handler_vec_t active_;
/** Active event handlers with fixed event names, organized by event name. */
map_t by_name_;
/** Active event handlers with variables in their event names. */
handler_list dynamic_;
/** Allows quick locating of handlers by id. */
id_map_t id_map_;
void log_handlers();
public:
/** Utility to standardize the event names used in by_name_. */
static std::string standardize_name(const std::string& name);
event_handlers()
: active_()
, by_name_()
, dynamic_()
, id_map_()
{
}
/** Access to the handlers with varying event names. */
handler_list& get_dynamic()
{
return dynamic_;
}
/** Read-only access to the active event handlers. Essentially gives all events. */
const handler_vec_t& get_active() const
{
return active_;
}
handler_vec_t& get_active()
{
return active_;
}
/** Access to the handlers with fixed event names, by event name. */
handler_list& get(const std::string& name);
/** Adds an event handler. */
void add_event_handler(const config& cfg, bool is_menu_item = false);
/** Removes an event handler, identified by its ID. */
void remove_event_handler(const std::string& id);
/**
* Removes all expired event handlers and any weak_ptrs to them.
*
* @param event_name The event name from whose by-name queue to clean
* up handlers.
*/
void clean_up_expired_handlers(const std::string& event_name);
/** Gets an event handler, identified by its ID. */
const handler_ptr get_event_handler_by_id(const std::string& id);
/** The number of active event handlers. */
size_t size() const
{
return active_.size();
}
};
} // end namespace game_events