The Battle for Wesnoth  1.13.4+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 - 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 /**
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 #ifndef GAME_EVENTS_HANDLERS_H_INCLUDED
25 #define GAME_EVENTS_HANDLERS_H_INCLUDED
26 
27 #include "config.hpp"
28 #include "utils/smart_list.hpp"
29 
30 #include <boost/noncopyable.hpp>
31 #include <boost/scoped_ptr.hpp>
32 #include <boost/shared_ptr.hpp>
33 #include <boost/weak_ptr.hpp>
34 #include <set>
35 #include <string>
36 
37 class game_data;
38 class game_lua_kernel;
39 
40 namespace game_events
41 {
42  struct queued_event;
43  class event_handler; // Defined a few lines down.
44  class manager;
45 
46  /// Shared pointer to handler objects.
48  /// Storage of event handlers.
49  typedef std::vector<handler_ptr> handler_vec;
50 
51 
53  {
54  public:
55  event_handler(const config &cfg, bool is_menu_item,
56  handler_vec::size_type index, manager &);
57 
58  /// The index of *this should only be of interest when controlling iterations.
59  handler_vec::size_type index() const { return index_; }
60 
61  bool matches_name(const std::string& name, const game_data * data) const;
62 
63  bool is_menu_item() const { return is_menu_item_; }
64 
65  /// Disables *this, removing it from the game.
66  void disable();
67  void handle_event(const queued_event& event_info, handler_ptr& handler_p, game_lua_kernel &);
68 
69  const config &get_config() const { return cfg_; }
70 
71  private:
74  handler_vec::size_type index_;
77  };
78 
79 
80  /// This is a wrapper for a list of weak pointers to handlers. It allows forward
81  /// iterations of the list, with each element returned as a shared pointer.
82  /// (Weak pointers that fail to lock are silently removed from the list.) These
83  /// iterations can be used recursively, even when the innermost iteration might
84  /// erase arbitrary elements from the list.
85  ///
86  /// The interface is not the standard list interface because that would be
87  /// inconvenient. The functionality implemented is that required by Wesnoth.
89  {
90  /// The weak pointers that are used internally.
91  typedef boost::weak_ptr<event_handler> internal_ptr;
92  /// The underlying list.
94 
95  public: // types
96  /// Handler list iterators are rather limited. They can be constructed
97  /// from a reference iterator (not default constructed), incremented,
98  /// and dereferenced. Consecutive dereferences are not guaranteed to
99  /// return the same element (if the list mutates between them, the next
100  /// element might be returned). An increment guarantees that the next
101  /// dereference will differ from the previous (unless at the end of the
102  /// list). The end of the list is indicated by dereferencing to a null
103  /// pointer.
104  class iterator
105  {
106  /// The current element.
108 
109  public:
110  /// Initialized constructor (to be called by handler_list).
111  explicit iterator(const list_t::iterator & base_iter) :
112  iter_(base_iter)
113  {}
114 
115  /// Increment.
116  iterator & operator++() { ++iter_; return *this; }
117  /// Dereference.
118  handler_ptr operator*();
119  };
120  friend class iterator;
122 
123  public: // functions
124  /// Default constructor.
125  /// Note: This explicit definition is required (by the more pedantic
126  /// compilers) in order to declare a default-constructed, static,
127  /// and const variable in t_event_handlers::get(), in handlers.cpp.
129 
130  const_iterator begin() const { return iterator(const_cast<list_t &>(data_).begin()); }
131  // The above const_cast is so the iterator can remove obsolete entries.
132  const_iterator end() const { return iterator(const_cast<list_t &>(data_).end()); }
133 
134  // push_front() is probably unneeded, but I'll leave the code here, just in case.
135  // (These lists must be maintained in index order, which means pushing to the back.)
136  void push_front(const handler_ptr & p) { data_.push_front(internal_ptr(p)); }
137  void push_back(const handler_ptr & p) { data_.push_back(internal_ptr(p)); }
138 
139  void clear() { data_.clear(); }
140 
141  private:
142  /// No implementation of operator=() since smart_list does not support it.
144 
145  /// The actual list.
146  list_t data_;
147  };
148 }
149 
150 #endif // GAME_EVENTS_HANDLERS_H_INCLUDED
151 
void push_back(const handler_ptr &p)
Definition: handlers.hpp:137
handler_ptr operator*()
Dereference.
Definition: handlers.cpp:59
boost::shared_ptr< event_handler > handler_ptr
Shared pointer to handler objects.
Definition: handlers.hpp:44
bool is_menu_item() const
Definition: handlers.hpp:63
void push_front(const handler_ptr &p)
Definition: handlers.hpp:136
list_t data_
The actual list.
Definition: handlers.hpp:146
utils::smart_list< internal_ptr > list_t
The underlying list.
Definition: handlers.hpp:93
boost::weak_ptr< event_handler > internal_ptr
The weak pointers that are used internally.
Definition: handlers.hpp:91
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
Handler list iterators are rather limited.
Definition: handlers.hpp:104
Definitions for the interface to Wesnoth Markup Language (WML).
iterator & operator++()
Increment.
Definition: handlers.hpp:116
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:113
This is a wrapper for a list of weak pointers to handlers.
Definition: handlers.hpp:88
std::vector< handler_ptr > handler_vec
Storage of event handlers.
Definition: handlers.hpp:49
GLfloat GLfloat p
Definition: glew.h:12766
const config & get_config() const
Definition: handlers.hpp:69
const_iterator begin() const
Definition: handlers.hpp:130
handler_vec::size_type index_
Definition: handlers.hpp:74
handler_list()
Default constructor.
Definition: handlers.hpp:128
Domain specific events.
Definition: action_wml.cpp:93
void disable()
Disables *this, removing it from the game.
Definition: handlers.cpp:94
list_t::iterator iter_
The current element.
Definition: handlers.hpp:107
GLuint index
Definition: glew.h:1782
const_iterator end() const
Definition: handlers.hpp:132
void push_back(const value_type &d)
Definition: smart_list.hpp:265
handler_list & operator=(const handler_list &)
No implementation of operator=() since smart_list does not support it.
The game event manager loads the scenario configuration object, and ensures that events are handled a...
Definition: manager.hpp:47
iterator(const list_t::iterator &base_iter)
Initialized constructor (to be called by handler_list).
Definition: handlers.hpp:111
GLuint const GLchar * name
Definition: glew.h:1782
handler_vec::size_type index() const
The index of *this should only be of interest when controlling iterations.
Definition: handlers.hpp:59
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
A config object defines a single node in a WML file, with access to child nodes.
Definition: config.hpp:83
GLsizei const GLcharARB ** string
Definition: glew.h:4503
event_handler(const config &cfg, bool is_menu_item, handler_vec::size_type index, manager &)
Definition: handlers.cpp:78
void push_front(const value_type &d)
Definition: smart_list.hpp:264