The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
generic_event.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2016 by Joerg Hinrichs <[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 GENERIC_EVENT_HPP_INCLUDED
16 #define GENERIC_EVENT_HPP_INCLUDED
17 
18 #include "global.hpp"
19 
20 #include <string>
21 #include <vector>
22 
23 /*
24 This is the basic framework for generic events. In contrast to events.cpp
25 it does not concentrate on SDL events but events that wesnoth itself raises.
26 It is also different to game_events.cpp in that it does not work with
27 specific events but rather defines a generic framework.
28 */
29 
30 namespace events{
31 /*
32 This is the observer that gets notified, if a generic event takes place
33 Use this as base class for every class that is supposed to react on a
34 generic event.
35 */
36 class observer{
37 public:
38  virtual void handle_generic_event(const std::string& event_name) = 0;
39  virtual ~observer() {}
40 };
41 
42 
43 /*
44 This is the class that notifies the observers and maintains a list of them.
45 */
47 public:
49  virtual ~generic_event() {}
50 
51  virtual bool attach_handler(observer* obs);
52  virtual bool detach_handler(observer* obs);
53  virtual void notify_observers();
54 private:
55  //Name of the event to help event handlers distinguish between several events
57 
58  //List of all subscribers waiting to react on this event
59  std::vector<observer*> observers_;
60 
61  //This flag makes sure, that an event is not raised while the vector of
62  //observers is changed through attach_handler or detach_handler
64 
65  //This flag makes sure, that attaching/detaching event handlers does not
66  //take place during notify of observers to prevent iterator corruption.
68 };
69 }
70 
71 #endif
virtual void notify_observers()
std::vector< observer * > observers_
virtual bool attach_handler(observer *obs)
generic_event(std::string name)
Handling of system events.
Definition: manager.hpp:42
GLuint const GLchar * name
Definition: glew.h:1782
virtual bool detach_handler(observer *obs)
virtual void handle_generic_event(const std::string &event_name)=0
GLsizei const GLcharARB ** string
Definition: glew.h:4503