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/fwd.hpp"
19 #include "config.hpp"
20 
21 #include <functional>
22 #include <set>
23 #include <string>
24 
25 class filter_context;
26 class game_data;
27 
28 namespace game_events
29 {
30 class wml_event_pump;
31 class event_handlers;
32 
33 /**
34  * The game event manager loads the scenario configuration object,
35  * and ensures that events are handled according to the
36  * scenario configuration for its lifetime.
37  *
38  * Thus, a manager object should be created when a scenario is played,
39  * and destroyed at the end of the scenario.
40  * If a second manager object is created before destroying the previous
41  * one, the game will crash with an assertion failure.
42  */
43 class manager
44 {
45 private:
46  /**
47  * This class is similar to an input iterator through event handlers,
48  * except each instance knows its own end (determined when constructed).
49  * Subsequent dereferences are not guaranteed to return the same element,
50  * so it is important to assign a dereference to a variable if you want
51  * to use it more than once. On the other hand, a dereference will not
52  * return a null pointer until the end of the iteration is reached (and
53  * this is how to detect the end of the iteration).
54  *
55  * For simplicity, this class is neither assignable nor equality
56  * comparable nor default constructable, and there is no postincrement.
57  * Typedefs are also skipped.
58  */
59  class iteration
60  {
61  public:
62  /// Event-specific constructor.
63  explicit iteration(const std::string& event_name, manager&);
64 
65  // Increment:
67  // Dereference:
69 
70  private:
71  /// The fixed-name event handlers for this iteration.
73  /// The varying-name event handlers for this iteration.
75  /// The event name for this iteration.
77 
78  /// Set to true upon dereferencing.
80  /// true if the most recent dereference was taken from main_list_.
82  /// The current (or next) element from main_list_.
84  /// The current (or next) element from var_list_.
86 
88  };
89 
90  const std::unique_ptr<event_handlers> event_handlers_;
91  std::set<std::string> unit_wml_ids_;
92 
93  const std::unique_ptr<game_events::wml_event_pump> pump_;
95 
96 public:
97  manager(const manager&) = delete;
98  manager& operator=(const manager&) = delete;
99 
100  explicit manager();
101  void read_scenario(const config& scenario_cfg);
102  ~manager();
103 
104  /** Create an event handler. */
105  void add_event_handler(const config& handler, bool is_menu_item = false);
106 
107  /** Removes an event handler. */
108  void remove_event_handler(const std::string& id);
109 
110  /** Gets an event handler by ID */
112 
113  void add_events(const config::const_child_itors& cfgs, const std::string& type = std::string());
114 
115  void write_events(config& cfg) const;
116 
117  using event_func_t = std::function<void(game_events::manager&, handler_ptr&)>;
118  void execute_on_events(const std::string& event_id, event_func_t func);
119 
121 
123  {
124  return wml_menu_items_;
125  }
126 };
127 }
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:45
handler_list::iterator main_it_
The current (or next) element from main_list_.
Definition: manager.hpp:83
std::vector< char_t > string
void write_events(config &cfg) const
Definition: manager.cpp:199
Definitions for the interface to Wesnoth Markup Language (WML).
handler_list::iterator var_it_
The current (or next) element from var_list_.
Definition: manager.hpp:85
void read_scenario(const config &scenario_cfg)
Definition: manager.cpp:66
game_events::wmi_manager wml_menu_items_
Definition: manager.hpp:94
manager & operator=(const manager &)=delete
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:91
const handler_ptr get_event_handler_by_id(const std::string &id)
Gets an event handler by ID.
Definition: manager.cpp:51
handler_list & main_list_
The fixed-name event handlers for this iteration.
Definition: manager.hpp:72
Declarations for a container for wml_menu_item.
std::function< void(game_events::manager &, handler_ptr &)> event_func_t
Definition: manager.hpp:117
const std::unique_ptr< event_handlers > event_handlers_
Definition: manager.hpp:90
boost::iterator_range< const_child_iterator > const_child_itors
Definition: config.hpp:236
Domain specific events.
Definition: action_wml.cpp:88
const std::string event_name_
The event name for this iteration.
Definition: manager.hpp:76
game_events::wmi_manager & wml_menu_items()
Definition: manager.hpp:122
handler_list & var_list_
The varying-name event handlers for this iteration.
Definition: manager.hpp:74
const std::unique_ptr< game_events::wml_event_pump > pump_
Definition: manager.hpp:93
void add_event_handler(const config &handler, bool is_menu_item=false)
Create an event handler.
Definition: manager.cpp:39
bool main_is_current_
true if the most recent dereference was taken from main_list_.
Definition: manager.hpp:81
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:43
This class is similar to an input iterator through event handlers, except each instance knows its own...
Definition: manager.hpp:59
iteration(const std::string &event_name, manager &)
Event-specific constructor.
Definition: manager.cpp:94
bool current_is_known_
Set to true upon dereferencing.
Definition: manager.hpp:79
std::list< weak_handler_ptr > handler_list
Definition: fwd.hpp:26
handler_ptr operator*()
Dereference Will return a null pointer when the end of the iteration is reached.
Definition: manager.cpp:151
iteration & operator++()
Increment Incrementing guarantees that the next dereference will differ from the previous dereference...
Definition: manager.cpp:113
game_events::wml_event_pump & pump()
Definition: manager.cpp:226
std::shared_ptr< event_handler > handler_ptr
Definition: fwd.hpp:24
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:213
void add_events(const config::const_child_itors &cfgs, const std::string &type=std::string())
Definition: manager.cpp:179
std::string::const_iterator iterator
Definition: tokenizer.hpp:24