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/handlers.hpp"
18 
19 #include <memory>
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  typedef std::unordered_map<std::string, handler_list> map_t;
29  typedef std::unordered_map<std::string, std::weak_ptr<event_handler>> id_map_t;
30 
31  /**
32  * Active event handlers. Will not have elements removed unless the event_handlers is clear()ed.
33  * This is the only container that actually 'owns' any events in the form of shared_ptrs. The other
34  * three storage methods own weak_ptrs.
35  */
37 
38  /** Active event handlers with fixed event names, organized by event name. */
39  map_t by_name_;
40 
41  /** Active event handlers with variables in their event names. */
43 
44  /** Allows quick locating of handlers by id. */
45  id_map_t id_map_;
46 
47  void log_handlers();
48 
49  /** Utility to standardize the event names used in by_name_. */
51 
52 public:
53  // TODO: remove
54  typedef handler_vec::size_type size_type;
55 
57  : active_()
58  , by_name_()
59  , dynamic_()
60  , id_map_()
61  {
62  }
63 
64  /** Read-only access to the handlers with varying event names. */
65  const handler_list& get_dynamic() const
66  {
67  return dynamic_;
68  }
69 
70  /** Read-only access to the active event handlers. Essentially gives all events. */
71  const handler_vec& get_active() const
72  {
73  return active_;
74  }
75 
76  /** Read-only access to the handlers with fixed event names, by event name. */
77  const handler_list& get(const std::string& name) const;
78 
79  /** Adds an event handler. */
80  void add_event_handler(const config& cfg, manager& man, bool is_menu_item = false);
81 
82  /** Removes an event handler, identified by its ID. */
83  void remove_event_handler(const std::string& id);
84 
85  /** Gets an event handler, identified by its ID. */
87 
88  /** The number of active event handlers. */
89  size_type size() const
90  {
91  return active_.size();
92  }
93 
94  /** Access to active event handlers by index. */
96  {
97  return active_[index];
98  }
99 };
100 
101 } // end namespace game_events
const handler_list & get_dynamic() const
Read-only access to the handlers with varying event names.
std::vector< char_t > string
size_t index(const utf8::string &str, const size_t index)
Codepoint index corresponding to the nth character in a UTF-8 string.
Definition: unicode.cpp:71
static std::string standardize_name(const std::string &name)
Utility to standardize the event names used in by_name_.
size_type size() const
The number of active event handlers.
id_map_t id_map_
Allows quick locating of handlers by id.
This is a wrapper for a list of weak pointers to handlers.
Definition: handlers.hpp:93
void remove_event_handler(const std::string &id)
Removes an event handler, identified by its ID.
std::vector< handler_ptr > handler_vec
Storage of event handlers.
Definition: handlers.hpp:46
void add_event_handler(const config &cfg, manager &man, bool is_menu_item=false)
Adds an event handler.
handler_list dynamic_
Active event handlers with variables in their event names.
std::unordered_map< std::string, handler_list > map_t
std::unordered_map< std::string, std::weak_ptr< event_handler > > id_map_t
Domain specific events.
Definition: action_wml.cpp:88
handler_vec::size_type size_type
map_t by_name_
Active event handlers with fixed event names, organized by event name.
const handler_vec & get_active() const
Read-only access to the active event handlers.
std::shared_ptr< event_handler > handler_ptr
Shared pointer to handler objects.
Definition: handlers.hpp:40
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:45
handler_vec active_
Active event handlers.
Define the handlers for the game's events mechanism.
handler_ptr & operator[](size_type index)
Access to active event handlers by index.
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
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