The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
manager_impl.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 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 <unordered_map>
21 
22 namespace game_events
23 {
24 // event_handlers is essentially the implementation details of the manager
26 {
27 private:
28  using handler_vec_t = std::vector<handler_ptr>;
29  using map_t = std::unordered_map<std::string, handler_list>;
30  using id_map_t = std::unordered_map<std::string, weak_handler_ptr>;
31 
32  /**
33  * Active event handlers. Will not have elements removed unless the event_handlers is clear()ed.
34  * This is the only container that actually 'owns' any events in the form of shared_ptrs. The other
35  * three storage methods own weak_ptrs.
36  */
38 
39  /** Active event handlers with fixed event names, organized by event name. */
41 
42  /** Active event handlers with variables in their event names. */
44 
45  /** Allows quick locating of handlers by id. */
47 
48  void log_handlers();
49 
50  /** Utility to standardize the event names used in by_name_. */
52 
53 public:
55  : active_()
56  , by_name_()
57  , dynamic_()
58  , id_map_()
59  {
60  }
61 
62  /** Access to the handlers with varying event names. */
64  {
65  return dynamic_;
66  }
67 
68  /** Read-only access to the active event handlers. Essentially gives all events. */
69  const handler_vec_t& get_active() const
70  {
71  return active_;
72  }
73 
74  /** Access to the handlers with fixed event names, by event name. */
75  handler_list& get(const std::string& name);
76 
77  /** Adds an event handler. */
78  void add_event_handler(const config& cfg, bool is_menu_item = false);
79 
80  /** Removes an event handler, identified by its ID. */
81  void remove_event_handler(const std::string& id);
82 
83  /**
84  * Removes all expired event handlers and any weak_ptrs to them.
85  *
86  * @param event_name The event name from whose by-name queue to clean
87  * up handlers.
88  */
89  void clean_up_expired_handlers(const std::string& event_name);
90 
91  /** Gets an event handler, identified by its ID. */
93 
94  /** The number of active event handlers. */
95  size_t size() const
96  {
97  return active_.size();
98  }
99 };
100 
101 } // end namespace game_events
handler_list & get_dynamic()
Access to the handlers with varying event names.
std::vector< char_t > string
handler_vec_t active_
Active event handlers.
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.
std::vector< handler_ptr > handler_vec_t
id_map_t id_map_
Allows quick locating of handlers by id.
Definitions for the interface to Wesnoth Markup Language (WML).
const handler_vec_t & get_active() const
Read-only access to the 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.
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.
size_t size() const
The number of active event handlers.
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
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
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:93