The Battle for Wesnoth  1.17.23+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 
88  /** Compare function to sort event handlers by priority. */
89  static bool cmp(const handler_ptr lhs, const handler_ptr rhs);
90 
92  : active_()
93  , by_name_()
94  , dynamic_()
95  , id_map_()
96  {
97  }
98 
99  /** Access to the handlers with varying event names. */
101  {
102  return dynamic_;
103  }
104 
105  /** Read-only access to the active event handlers. Essentially gives all events. */
107  {
108  return active_;
109  }
110 
112  {
113  return active_;
114  }
115 
116  /** Access to the handlers with fixed event names, by event name. */
117  handler_list& get(const std::string& name);
118 
119  /** Adds an event handler. */
120  pending_event_handler add_event_handler(const std::string& name, const std::string& id, bool repeat, double priority = 0., bool is_menu_item = false);
121 
122  /** Removes an event handler, identified by its ID. */
123  void remove_event_handler(const std::string& id);
124 
125  /**
126  * Removes all expired event handlers and any weak_ptrs to them.
127  *
128  * @param event_name The event name from whose by-name queue to clean
129  * up handlers.
130  */
131  void clean_up_expired_handlers(const std::string& event_name);
132 
133  /** Gets an event handler, identified by its ID. */
134  const handler_ptr get_event_handler_by_id(const std::string& id);
135 
136  /** The number of active event handlers. */
137  std::size_t size() const
138  {
139  return active_.size();
140  }
141 };
142 
143 } // 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.
pending_event_handler add_event_handler(const std::string &name, const std::string &id, bool repeat, double priority=0., bool is_menu_item=false)
Adds an event handler.
std::deque< handler_ptr > handler_queue_t
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.
static bool cmp(const handler_ptr lhs, const handler_ptr rhs)
Compare function to sort event handlers by priority.
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