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