The Battle for Wesnoth  1.17.17+dev
manager_impl.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2023
3  by David White <dave@whitevine.net>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include "game_events/fwd.hpp"
19 #include "config.hpp"
20 
21 #include <deque>
22 #include <unordered_map>
23 
24 class game_lua_kernel;
25 
26 namespace game_events
27 {
28 class event_handlers;
29 /**
30  * Represents a handler that is about to be added to the events manager but is still waiting for some data.
31  * The handler will automatically be added when this class is destroyed, unless it has become invalid somehow.
32  */
34 {
39  // It's move-constructible, but there's no way to make it move-assignable since it contains a reference...
41 public:
43  : list_(list)
44  , handler_(handler)
45  {}
46  /** Check if this handler is valid. */
47  bool valid() const {return handler_.get();}
48  /** Access the event handler. */
49  event_handler* operator->() {return handler_.get();}
53 };
54 
55 // event_handlers is essentially the implementation details of the manager
57 {
58 private:
59  using handler_queue_t = std::deque<handler_ptr>;
60  using map_t = std::unordered_map<std::string, handler_list>;
61  using id_map_t = std::unordered_map<std::string, weak_handler_ptr>;
62 
63  /**
64  * Active event handlers. Will not have elements removed unless the event_handlers is clear()ed.
65  * This is the only container that actually 'owns' any events in the form of shared_ptrs. The other
66  * three storage methods own weak_ptrs.
67  */
69 
70  /** Active event handlers with fixed event names, organized by event name. */
72 
73  /** Active event handlers with variables in their event names. */
75 
76  /** Allows quick locating of handlers by id. */
78 
79  void log_handlers();
80 
82  void finish_adding_event_handler(handler_ptr new_handler);
83 
84 public:
85  /** Utility to standardize the event names used in by_name_. */
86  static std::string standardize_name(const std::string& name);
87 
89  : active_()
90  , by_name_()
91  , dynamic_()
92  , id_map_()
93  {
94  }
95 
96  /** Access to the handlers with varying event names. */
98  {
99  return dynamic_;
100  }
101 
102  /** Read-only access to the active event handlers. Essentially gives all events. */
104  {
105  return active_;
106  }
107 
109  {
110  return active_;
111  }
112 
113  /** Access to the handlers with fixed event names, by event name. */
114  handler_list& get(const std::string& name);
115 
116  /** Adds an event handler. */
117  pending_event_handler add_event_handler(const std::string& name, const std::string& id, bool repeat, bool is_menu_item = false);
118 
119  /** Removes an event handler, identified by its ID. */
120  void remove_event_handler(const std::string& id);
121 
122  /**
123  * Removes all expired event handlers and any weak_ptrs to them.
124  *
125  * @param event_name The event name from whose by-name queue to clean
126  * up handlers.
127  */
128  void clean_up_expired_handlers(const std::string& event_name);
129 
130  /** Gets an event handler, identified by its ID. */
131  const handler_ptr get_event_handler_by_id(const std::string& id);
132 
133  /** The number of active event handlers. */
134  std::size_t size() const
135  {
136  return active_.size();
137  }
138 };
139 
140 } // end namespace game_events
map_t by_name_
Active event handlers with fixed event names, organized by event name.
void clean_up_expired_handlers(const std::string &event_name)
Removes all expired event handlers and any weak_ptrs to them.
handler_list & get_dynamic()
Access to the handlers with varying event names.
handler_list & get(const std::string &name)
Access to the handlers with fixed event names, by event name.
const handler_queue_t & get_active() const
Read-only access to the active event handlers.
std::size_t size() const
The number of active event handlers.
handler_queue_t & get_active()
void remove_event_handler(const std::string &id)
Removes an event handler, identified by its ID.
std::deque< handler_ptr > handler_queue_t
pending_event_handler add_event_handler(const std::string &name, const std::string &id, bool repeat, bool is_menu_item=false)
Adds an event handler.
void finish_adding_event_handler(handler_ptr new_handler)
std::unordered_map< std::string, weak_handler_ptr > id_map_t
std::unordered_map< std::string, handler_list > map_t
handler_list dynamic_
Active event handlers with variables in their event names.
id_map_t id_map_
Allows quick locating of handlers by id.
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler, identified by its ID.
handler_queue_t active_
Active event handlers.
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
Represents a handler that is about to be added to the events manager but is still waiting for some da...
pending_event_handler & operator=(pending_event_handler &&)=delete
pending_event_handler & operator=(const pending_event_handler &)=delete
pending_event_handler(pending_event_handler &&)=default
pending_event_handler(const pending_event_handler &)=delete
pending_event_handler(event_handlers &list, handler_ptr handler)
bool valid() const
Check if this handler is valid.
event_handler * operator->()
Access the event handler.
Domain specific events.
std::list< weak_handler_ptr > handler_list
Definition: fwd.hpp:27
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25