The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
handlers.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 /**
16  * @file
17  * Define the handlers for the game's events mechanism.
18  *
19  * Events might be units moving or fighting, or when victory or defeat occurs.
20  * A scenario's configuration file will define actions to take when certain events occur.
21  * This module is responsible for tracking these definitions.
22  */
23 
24 #pragma once
25 
26 #include "config.hpp"
27 #include "utils/smart_list.hpp"
28 
29 #include <memory>
30 #include <set>
31 #include <string>
32 
33 class game_data;
34 class game_lua_kernel;
35 
36 namespace game_events
37 {
38 struct queued_event;
39 class event_handler; // Defined a few lines down.
40 class manager;
41 
42 /// Shared pointer to handler objects.
43 typedef std::shared_ptr<event_handler> handler_ptr;
44 
45 /// Storage of event handlers.
46 typedef std::vector<handler_ptr> handler_vec;
47 
49 {
50 public:
51  event_handler(const config& cfg, bool is_menu_item, handler_vec::size_type index, manager&);
52 
53  /** The index of *this should only be of interest when controlling iterations. */
54  handler_vec::size_type index() const
55  {
56  return index_;
57  }
58 
59  bool matches_name(const std::string& name, const game_data* data) const;
60 
61  bool is_menu_item() const
62  {
63  return is_menu_item_;
64  }
65 
66  /** Disables *this, removing it from the game. */
67  void disable();
68  void handle_event(const queued_event& event_info, handler_ptr& handler_p, game_lua_kernel&);
69 
70  const config& get_config() const
71  {
72  return cfg_;
73  }
74 
75 private:
78  handler_vec::size_type index_;
81 };
82 
83 /**
84  * This is a wrapper for a list of weak pointers to handlers. It allows forward
85  * iterations of the list, with each element returned as a shared pointer.
86  * (Weak pointers that fail to lock are silently removed from the list.) These
87  * iterations can be used recursively, even when the innermost iteration might
88  * erase arbitrary elements from the list.
89  *
90  * The interface is not the standard list interface because that would be
91  * inconvenient. The functionality implemented is that required by Wesnoth.
92  */
94 {
95  /// The weak pointers that are used internally.
96  typedef std::weak_ptr<event_handler> internal_ptr;
97 
98  /// The underlying list.
100 
101 public:
102  /**
103  * Handler list iterators are rather limited. They can be constructed
104  * from a reference iterator (not default constructed), incremented,
105  * and dereferenced. Consecutive dereferences are not guaranteed to
106  * return the same element (if the list mutates between them, the next
107  * element might be returned). An increment guarantees that the next
108  * dereference will differ from the previous (unless at the end of the
109  * list). The end of the list is indicated by dereferencing to a null
110  * pointer.
111  */
112  class iterator
113  {
114  /// The current element.
116 
117  public:
118  /// Initialized constructor (to be called by handler_list).
119  explicit iterator(const list_t::iterator& base_iter)
120  : iter_(base_iter)
121  {
122  }
123 
124  /// Increment.
126  {
127  ++iter_;
128  return *this;
129  }
130  /// Dereference.
131  handler_ptr operator*();
132  };
133  friend class iterator;
135 
136 public:
137  /**
138  * Default constructor.
139  * Note: This explicit definition is required (by the more pedantic
140  * compilers) in order to declare a default-constructed, static,
141  * and const variable in event_handlers::get(), in handlers.cpp.
142  */
144  : data_()
145  {
146  }
147 
148  const_iterator begin() const
149  {
150  return iterator(const_cast<list_t&>(data_).begin());
151  }
152 
153  // The above const_cast is so the iterator can remove obsolete entries.
154  const_iterator end() const
155  {
156  return iterator(const_cast<list_t&>(data_).end());
157  }
158 
159  // push_front() is probably unneeded, but I'll leave the code here, just in case.
160  // (These lists must be maintained in index order, which means pushing to the back.)
161  void push_front(const handler_ptr& p)
162  {
164  }
165 
166  void push_back(const handler_ptr& p)
167  {
169  }
170 
171  void clear()
172  {
173  data_.clear();
174  }
175 
176 private:
177  /// No implementation of operator=() since smart_list does not support it.
179 
180  /// The actual list.
181  list_t data_;
182 };
183 }
void push_back(const handler_ptr &p)
Definition: handlers.hpp:166
handler_ptr operator*()
Dereference.
Definition: handlers.cpp:54
std::vector< char_t > string
bool is_menu_item() const
Definition: handlers.hpp:61
void push_front(const handler_ptr &p)
Definition: handlers.hpp:161
list_t data_
The actual list.
Definition: handlers.hpp:181
std::weak_ptr< event_handler > internal_ptr
The weak pointers that are used internally.
Definition: handlers.hpp:96
utils::smart_list< internal_ptr > list_t
The underlying list.
Definition: handlers.hpp:99
Handler list iterators are rather limited.
Definition: handlers.hpp:112
Definitions for the interface to Wesnoth Markup Language (WML).
iterator & operator++()
Increment.
Definition: handlers.hpp:125
bool matches_name(const std::string &name, const game_data *data) const
Definition: handlers.cpp:135
void handle_event(const queued_event &event_info, handler_ptr &handler_p, game_lua_kernel &)
Handles the queued event, according to our WML instructions.
Definition: handlers.cpp:111
This is a wrapper for a list of weak pointers to handlers.
Definition: handlers.hpp:93
std::vector< handler_ptr > handler_vec
Storage of event handlers.
Definition: handlers.hpp:46
const config & get_config() const
Definition: handlers.hpp:70
const_iterator begin() const
Definition: handlers.hpp:148
handler_vec::size_type index_
Definition: handlers.hpp:78
handler_list()
Default constructor.
Definition: handlers.hpp:143
Domain specific events.
Definition: action_wml.cpp:88
void disable()
Disables *this, removing it from the game.
Definition: handlers.cpp:90
list_t::iterator iter_
The current element.
Definition: handlers.hpp:115
mock_party p
const_iterator end() const
Definition: handlers.hpp:154
void push_back(const value_type &d)
Definition: smart_list.hpp:264
handler_list & operator=(const handler_list &)
No implementation of operator=() since smart_list does not support it.
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
iterator(const list_t::iterator &base_iter)
Initialized constructor (to be called by handler_list).
Definition: handlers.hpp:119
handler_vec::size_type index() const
The index of *this should only be of interest when controlling iterations.
Definition: handlers.hpp:54
static const char * name(const std::vector< SDL_Joystick * > &joysticks, const size_t index)
Definition: joystick.cpp:48
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:93
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
event_handler(const config &cfg, bool is_menu_item, handler_vec::size_type index, manager &)
Definition: handlers.cpp:73
void push_front(const value_type &d)
Definition: smart_list.hpp:263