-
-
Notifications
You must be signed in to change notification settings - Fork 988
/
manager.hpp
142 lines (116 loc) · 4.7 KB
/
manager.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/*
Copyright (C) 2003 - 2015 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef GAME_EVENTS_MANAGER_HPP
#define GAME_EVENTS_MANAGER_HPP
#include "game_events/handlers.hpp"
#include "game_events/wmi_container.hpp"
#include <boost/function.hpp>
#include <boost/noncopyable.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <set>
#include <string>
class game_lua_kernel;
class filter_context;
class game_display;
class game_data;
class unit_map;
namespace game_events {
struct t_context {
game_lua_kernel * lua_kernel;
filter_context * filter_con;
game_display * screen;
game_data * gamedata;
unit_map * units;
boost::function<void()> on_gamestate_change; //whiteboard callback
boost::function<int()> current_side; //current_side function
t_context(game_lua_kernel * lua_kernel, filter_context * filter_con, game_display * screen, game_data * gamedata, unit_map * um, boost::function<void()>, boost::function<int()>);
};
class t_pump;
class t_event_handlers;
/// The game event manager loads the scenario configuration object,
/// and ensures that events are handled according to the
/// scenario configuration for its lifetime.
///
/// Thus, a manager object should be created when a scenario is played,
/// and destroyed at the end of the scenario.
/// If a second manager object is created before destroying the previous
/// one, the game will crash with an assertion failure.
class manager : boost::noncopyable {
public:
/// This class is similar to an input iterator through event handlers,
/// except each instance knows its own end (determined when constructed).
/// Subsequent dereferences are not guaranteed to return the same element,
/// so it is important to assign a dereference to a variable if you want
/// to use it more than once. On the other hand, a dereference will not
/// return a null pointer until the end of the iteration is reached (and
/// this is how to detect the end of the iteration).
///
/// For simplicity, this class is neither assignable nor equality
/// comparable nor default constructable, and there is no postincrement.
/// Typedefs are also skipped.
class iteration
{
public:
/// Event-specific constructor.
explicit iteration(const std::string & event_name, manager &);
// Increment:
iteration & operator++();
// Dereference:
handler_ptr operator*();
private: // functions
/// Gets the index from a pointer, capped at end_.
handler_vec::size_type ptr_index(const handler_ptr & ptr) const
{ return !bool(ptr) ? end_ : std::min(ptr->index(), end_); }
private: // data
/// The fixed-name event handlers for this iteration.
const handler_list & main_list_;
/// The varying-name event handlers for this iteration.
const handler_list & var_list_;
/// The event name for this iteration.
const std::string event_name_;
/// The end of this iteration. We intentionally exclude handlers
/// added after *this is constructed.
const handler_vec::size_type end_;
/// Set to true upon dereferencing.
bool current_is_known_;
/// true if the most recent dereference was taken from main_list_.
bool main_is_current_;
/// The current (or next) element from main_list_.
handler_list::iterator main_it_;
/// The current (or next) element from var_list_.
handler_list::iterator var_it_;
game_data * gamedata_;
};
boost::scoped_ptr<t_event_handlers> event_handlers_;
std::set<std::string> unit_wml_ids_;
boost::scoped_ptr<game_events::t_pump> pump_;
boost::shared_ptr<t_context> resources_;
game_events::wmi_container wml_menu_items_;
boost::shared_ptr<manager* const> me_;
public:
/// Note that references will be maintained,
/// and must remain valid for the life of the object.
explicit manager(const config& scenario_cfg, const boost::shared_ptr<t_context> &);
~manager();
/// Create an event handler.
void add_event_handler(const config & handler, bool is_menu_item=false);
/// Removes an event handler.
void remove_event_handler(const std::string & id);
void add_events(const config::const_child_itors &cfgs,
const std::string& type = std::string());
void write_events(config& cfg);
const boost::shared_ptr<manager * const> & get_shared();
game_events::t_pump & pump();
};
}
#endif