The Battle for Wesnoth  1.13.4+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 - 2016 by David White <[email protected]>
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 #ifndef GAME_EVENTS_MANAGER_HPP
16 #define GAME_EVENTS_MANAGER_HPP
17 
18 #include "game_events/handlers.hpp"
20 
21 #include <boost/noncopyable.hpp>
22 #include <boost/scoped_ptr.hpp>
23 #include <boost/shared_ptr.hpp>
24 #include <set>
25 #include <string>
26 
27 class game_lua_kernel;
28 class filter_context;
29 class game_display;
30 class game_data;
31 class unit_map;
32 
33 namespace game_events {
34 
35  class t_pump;
36 
37  class t_event_handlers;
38 
39  /// The game event manager loads the scenario configuration object,
40  /// and ensures that events are handled according to the
41  /// scenario configuration for its lifetime.
42  ///
43  /// Thus, a manager object should be created when a scenario is played,
44  /// and destroyed at the end of the scenario.
45  /// If a second manager object is created before destroying the previous
46  /// one, the game will crash with an assertion failure.
47  class manager : boost::noncopyable {
48  public:
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  class iteration
61  {
62  public:
63  /// Event-specific constructor.
64  explicit iteration(const std::string & event_name, manager &);
65 
66  // Increment:
68  // Dereference:
70 
71  private: // functions
72  /// Gets the index from a pointer, capped at end_.
73  handler_vec::size_type ptr_index(const handler_ptr & ptr) const
74  { return !bool(ptr) ? end_ : std::min(ptr->index(), end_); }
75 
76  private: // data
77  /// The fixed-name event handlers for this iteration.
79  /// The varying-name event handlers for this iteration.
81  /// The event name for this iteration.
83  /// The end of this iteration. We intentionally exclude handlers
84  /// added after *this is constructed.
85  const handler_vec::size_type end_;
86 
87  /// Set to true upon dereferencing.
89  /// true if the most recent dereference was taken from main_list_.
91  /// The current (or next) element from main_list_.
93  /// The current (or next) element from var_list_.
95 
97  };
98 
99  boost::scoped_ptr<t_event_handlers> event_handlers_;
100  std::set<std::string> unit_wml_ids_;
101 
102  boost::scoped_ptr<game_events::t_pump> pump_;
104 
105  public:
106  explicit manager();
107  void read_scenario(const config& scenario_cfg);
108  ~manager();
109 
110  /// Create an event handler.
111  void add_event_handler(const config & handler, bool is_menu_item=false);
112  /// Removes an event handler.
113  void remove_event_handler(const std::string & id);
114 
115  void add_events(const config::const_child_itors &cfgs,
116  const std::string& type = std::string());
117  void write_events(config& cfg);
118 
120  };
121 }
122 
123 #endif
void remove_event_handler(const std::string &id)
Removes an event handler.
Definition: manager.cpp:56
boost::scoped_ptr< game_events::t_pump > pump_
Definition: manager.hpp:102
static thandler * handler
Definition: handler.cpp:60
handler_list::iterator main_it_
The current (or next) element from main_list_.
Definition: manager.hpp:92
void write_events(config &cfg)
Definition: manager.cpp:181
GLuint GLuint GLsizei GLenum type
Definition: glew.h:1221
const handler_list & var_list_
The varying-name event handlers for this iteration.
Definition: manager.hpp:80
Handler list iterators are rather limited.
Definition: handlers.hpp:104
std::pair< const_child_iterator, const_child_iterator > const_child_itors
Definition: config.hpp:214
handler_list::iterator var_it_
The current (or next) element from var_list_.
Definition: manager.hpp:94
game_events::wmi_container wml_menu_items_
Definition: manager.hpp:103
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:88
std::set< std::string > unit_wml_ids_
Definition: manager.hpp:100
Domain specific events.
Definition: action_wml.cpp:93
Declarations for a container for wml_menu_item.
const std::string event_name_
The event name for this iteration.
Definition: manager.hpp:82
void add_event_handler(const config &handler, bool is_menu_item=false)
Create an event handler.
Definition: manager.cpp:50
A container of wml_menu_item.
const handler_list & main_list_
The fixed-name event handlers for this iteration.
Definition: manager.hpp:78
bool main_is_current_
true if the most recent dereference was taken from main_list_.
Definition: manager.hpp:90
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:47
This class is similar to an input iterator through event handlers, except each instance knows its own...
Definition: manager.hpp:60
game_events::t_pump & pump()
Definition: manager.cpp:194
iteration(const std::string &event_name, manager &)
Event-specific constructor.
Definition: manager.cpp:96
bool current_is_known_
Set to true upon dereferencing.
Definition: manager.hpp:88
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:140
Container associating units to locations.
Definition: map.hpp:90
boost::scoped_ptr< t_event_handlers > event_handlers_
Definition: manager.hpp:99
iteration & operator++()
Increment Incrementing guarantees that the next dereference will differ from the previous derference ...
Definition: manager.cpp:116
const handler_vec::size_type end_
The end of this iteration.
Definition: manager.hpp:85
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
void add_events(const config::const_child_itors &cfgs, const std::string &type=std::string())
Definition: manager.cpp:165
GLsizei const GLcharARB ** string
Definition: glew.h:4503
handler_vec::size_type ptr_index(const handler_ptr &ptr) const
Gets the index from a pointer, capped at end_.
Definition: manager.hpp:73