The Battle for Wesnoth  1.17.0-dev
manager_impl.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2021
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 namespace game_events
25 {
26 // event_handlers is essentially the implementation details of the manager
28 {
29 private:
30  using handler_queue_t = std::deque<handler_ptr>;
31  using map_t = std::unordered_map<std::string, handler_list>;
32  using id_map_t = std::unordered_map<std::string, weak_handler_ptr>;
33 
34  /**
35  * Active event handlers. Will not have elements removed unless the event_handlers is clear()ed.
36  * This is the only container that actually 'owns' any events in the form of shared_ptrs. The other
37  * three storage methods own weak_ptrs.
38  */
40 
41  /** Active event handlers with fixed event names, organized by event name. */
43 
44  /** Active event handlers with variables in their event names. */
46 
47  /** Allows quick locating of handlers by id. */
49 
50  void log_handlers();
51 
52 public:
53  /** Utility to standardize the event names used in by_name_. */
54  static std::string standardize_name(const std::string& name);
55 
57  : active_()
58  , by_name_()
59  , dynamic_()
60  , id_map_()
61  {
62  }
63 
64  /** Access to the handlers with varying event names. */
66  {
67  return dynamic_;
68  }
69 
70  /** Read-only access to the active event handlers. Essentially gives all events. */
71  const handler_queue_t& get_active() const
72  {
73  return active_;
74  }
75 
77  {
78  return active_;
79  }
80 
81  /** Access to the handlers with fixed event names, by event name. */
82  handler_list& get(const std::string& name);
83 
84  /** Adds an event handler. */
85  void add_event_handler(const config& cfg, bool is_menu_item = false);
86 
87  /** Removes an event handler, identified by its ID. */
88  void remove_event_handler(const std::string& id);
89 
90  /**
91  * Removes all expired event handlers and any weak_ptrs to them.
92  *
93  * @param event_name The event name from whose by-name queue to clean
94  * up handlers.
95  */
96  void clean_up_expired_handlers(const std::string& event_name);
97 
98  /** Gets an event handler, identified by its ID. */
99  const handler_ptr get_event_handler_by_id(const std::string& id);
100 
101  /** The number of active event handlers. */
102  std::size_t size() const
103  {
104  return active_.size();
105  }
106 };
107 
108 } // end namespace game_events
handler_list & get_dynamic()
Access to the handlers with varying event names.
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
std::unordered_map< std::string, handler_list > map_t
void clean_up_expired_handlers(const std::string &event_name)
Removes all expired event handlers and any weak_ptrs to them.
id_map_t id_map_
Allows quick locating of handlers by id.
Definitions for the interface to Wesnoth Markup Language (WML).
const handler_queue_t & get_active() const
Read-only access to the active event handlers.
std::deque< handler_ptr > handler_queue_t
handler_queue_t & get_active()
std::size_t size() const
The number of active event handlers.
void remove_event_handler(const std::string &id)
Removes an event handler, identified by its ID.
handler_list dynamic_
Active event handlers with variables in their event names.
handler_queue_t active_
Active event handlers.
Domain specific events.
Definition: action_wml.cpp:87
std::unordered_map< std::string, weak_handler_ptr > id_map_t
void add_event_handler(const config &cfg, bool is_menu_item=false)
Adds an event handler.
map_t by_name_
Active event handlers with fixed event names, organized by event name.
std::list< weak_handler_ptr > handler_list
Definition: fwd.hpp:27
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:25
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler, identified by its ID.
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:60