The Battle for Wesnoth  1.19.5+dev
manager_impl.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2024
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 
25 namespace game_events
26 {
27 class event_handlers;
28 /**
29  * Represents a handler that is about to be added to the events manager but is still waiting for some data.
30  * The handler will automatically be added when this class is destroyed, unless it has become invalid somehow.
31  */
33 {
38  // It's move-constructible, but there's no way to make it move-assignable since it contains a reference...
40 public:
42  : list_(list)
43  , handler_(handler)
44  {}
45  /** Check if this handler is valid. */
46  bool valid() const {return handler_.get();}
47  /** Access the event handler. */
48  event_handler* operator->() {return handler_.get();}
52 };
53 
54 // event_handlers is essentially the implementation details of the manager
56 {
57 private:
58  using handler_queue_t = std::deque<handler_ptr>;
59  using map_t = std::unordered_map<std::string, handler_list>;
60  using id_map_t = std::unordered_map<std::string, weak_handler_ptr>;
61 
62  /**
63  * Active event handlers. Will not have elements removed unless the event_handlers is clear()ed.
64  * This is the only container that actually 'owns' any events in the form of shared_ptrs. The other
65  * three storage methods own weak_ptrs.
66  */
68 
69  /** Active event handlers with fixed event names, organized by event name. */
71 
72  /** Active event handlers with variables in their event names. */
74 
75  /** Allows quick locating of handlers by id. */
77 
78  void log_handlers();
79 
81  void finish_adding_event_handler(handler_ptr new_handler);
82 
83 public:
84  /** Utility to standardize the event names used in by_name_. */
85  static std::string standardize_name(const std::string& name);
86 
87  /** Compare function to sort event handlers by priority. */
88  static bool cmp(const handler_ptr lhs, const handler_ptr rhs);
89 
91  : active_()
92  , by_name_()
93  , dynamic_()
94  , id_map_()
95  {
96  }
97 
98  /** Access to the handlers with varying event names. */
100  {
101  return dynamic_;
102  }
103 
104  /** Read-only access to the active event handlers. Essentially gives all events. */
106  {
107  return active_;
108  }
109 
111  {
112  return active_;
113  }
114 
115  /** Access to the handlers with fixed event names, by event name. */
116  handler_list& get(const std::string& name);
117 
118  /** Adds an event handler. */
119  pending_event_handler add_event_handler(const std::string& name, const std::string& id, bool repeat, double priority = 0., bool is_menu_item = false);
120 
121  /** Removes an event handler, identified by its ID. */
122  void remove_event_handler(const std::string& id);
123 
124  /**
125  * Removes all expired event handlers and any weak_ptrs to them.
126  *
127  * @param event_name The event name from whose by-name queue to clean
128  * up handlers.
129  */
130  void clean_up_expired_handlers(const std::string& event_name);
131 
132  /** Gets an event handler, identified by its ID. */
133  const handler_ptr get_event_handler_by_id(const std::string& id);
134 
135  /** The number of active event handlers. */
136  std::size_t size() const
137  {
138  return active_.size();
139  }
140 };
141 
142 } // 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.
Definitions for the interface to Wesnoth Markup Language (WML).
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