The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
manager.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"
19 
20 #include <functional>
21 #include <set>
22 #include <string>
23 
24 class game_lua_kernel;
25 class filter_context;
26 class game_display;
27 class game_data;
28 class unit_map;
29 
30 namespace game_events
31 {
32 class wml_event_pump;
33 class event_handlers;
34 
35 /**
36  * The game event manager loads the scenario configuration object,
37  * and ensures that events are handled according to the
38  * scenario configuration for its lifetime.
39  *
40  * Thus, a manager object should be created when a scenario is played,
41  * and destroyed at the end of the scenario.
42  * If a second manager object is created before destroying the previous
43  * one, the game will crash with an assertion failure.
44  */
45 class manager
46 {
47 private:
48  /**
49  * This class is similar to an input iterator through event handlers,
50  * except each instance knows its own end (determined when constructed).
51  * Subsequent dereferences are not guaranteed to return the same element,
52  * so it is important to assign a dereference to a variable if you want
53  * to use it more than once. On the other hand, a dereference will not
54  * return a null pointer until the end of the iteration is reached (and
55  * this is how to detect the end of the iteration).
56  *
57  * For simplicity, this class is neither assignable nor equality
58  * comparable nor default constructable, and there is no postincrement.
59  * Typedefs are also skipped.
60  */
61  class iteration
62  {
63  public:
64  /// Event-specific constructor.
65  explicit iteration(const std::string& event_name, manager&);
66 
67  // Increment:
69  // Dereference:
71 
72  private:
73  /// Gets the index from a pointer, capped at end_.
74  handler_vec::size_type ptr_index(const handler_ptr& ptr) const
75  {
76  return !bool(ptr) ? end_ : std::min(ptr->index(), end_);
77  }
78 
79  private:
80  /// The fixed-name event handlers for this iteration.
82  /// The varying-name event handlers for this iteration.
84  /// The event name for this iteration.
86  /// The end of this iteration. We intentionally exclude handlers
87  /// added after *this is constructed.
88  const handler_vec::size_type end_;
89 
90  /// Set to true upon dereferencing.
92  /// true if the most recent dereference was taken from main_list_.
94  /// The current (or next) element from main_list_.
96  /// The current (or next) element from var_list_.
98 
100  };
101 
102  // Performs an assertion check to ensure these members are not null.
103  friend void event_handler::disable();
104 
105  const std::unique_ptr<event_handlers> event_handlers_;
106  std::set<std::string> unit_wml_ids_;
107 
108  const std::unique_ptr<game_events::wml_event_pump> pump_;
110 
111 public:
112  manager(const manager&) = delete;
113  manager& operator=(const manager&) = delete;
114 
115  explicit manager();
116  void read_scenario(const config& scenario_cfg);
117  ~manager();
118 
119  /** Create an event handler. */
120  void add_event_handler(const config& handler, bool is_menu_item = false);
121 
122  /** Removes an event handler. */
123  void remove_event_handler(const std::string& id);
124 
125  /** Gets an event handler by ID */
127 
128  void add_events(const config::const_child_itors& cfgs, const std::string& type = std::string());
129 
130  void write_events(config& cfg) const;
131 
132  using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
133  void execute_on_events(const std::string& event_id, event_func_t func);
134 
136 
138  {
139  return wml_menu_items_;
140  }
141 };
142 }
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:51
handler_list::iterator main_it_
The current (or next) element from main_list_.
Definition: manager.hpp:95
std::vector< char_t > string
const handler_list & var_list_
The varying-name event handlers for this iteration.
Definition: manager.hpp:83
void write_events(config &cfg) const
Definition: manager.cpp:193
Handler list iterators are rather limited.
Definition: handlers.hpp:112
handler_list::iterator var_it_
The current (or next) element from var_list_.
Definition: manager.hpp:97
void read_scenario(const config &scenario_cfg)
Definition: manager.cpp:72
This is a wrapper for a list of weak pointers to handlers.
Definition: handlers.hpp:93
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:109
manager & operator=(const manager &)=delete
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:106
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:57
Declarations for a container for wml_menu_item.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
Definition: manager.hpp:132
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:105
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:236
Domain specific events.
Definition: action_wml.cpp:88
void disable()
Disables *this, removing it from the game.
Definition: handlers.cpp:90
const std::string event_name_
The event name for this iteration.
Definition: manager.hpp:85
game_events::wmi_manager & wml_menu_items()
Definition: manager.hpp:137
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition: manager.hpp:108
void add_event_handler(const config &handler, bool is_menu_item=false)
Create an event handler.
Definition: manager.cpp:45
const handler_list & main_list_
The fixed-name event handlers for this iteration.
Definition: manager.hpp:81
bool main_is_current_
true if the most recent dereference was taken from main_list_.
Definition: manager.hpp:93
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
This class is similar to an input iterator through event handlers, except each instance knows its own...
Definition: manager.hpp:61
iteration(const std::string &event_name, manager &)
Event-specific constructor.
Definition: manager.cpp:100
bool current_is_known_
Set to true upon dereferencing.
Definition: manager.hpp:91
Define the handlers for the game's events mechanism.
handler_ptr operator*()
Dereference Will return a null pointer when the end of the iteration is reached.
Definition: manager.cpp:146
Container associating units to locations.
Definition: map.hpp:99
iteration & operator++()
Increment Incrementing guarantees that the next dereference will differ from the previous derference ...
Definition: manager.cpp:120
game_events::wml_event_pump & pump()
Definition: manager.cpp:217
const handler_vec::size_type end_
The end of this iteration.
Definition: manager.hpp:88
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
void execute_on_events(const std::string &event_id, event_func_t func)
Definition: manager.cpp:207
void add_events(const config::const_child_itors &cfgs, const std::string &type=std::string())
Definition: manager.cpp:173
handler_vec::size_type ptr_index(const handler_ptr &ptr) const
Gets the index from a pointer, capped at end_.
Definition: manager.hpp:74