The Battle for Wesnoth  1.15.0+dev
generic_event.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2018 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include <string>
18 #include <vector>
19 
20 /*
21 This is the basic framework for generic events. In contrast to events.cpp
22 it does not concentrate on SDL events but events that wesnoth itself raises.
23 It is also different to game_events.cpp in that it does not work with
24 specific events but rather defines a generic framework.
25 */
26 
27 namespace events{
28 /*
29 This is the observer that gets notified, if a generic event takes place
30 Use this as base class for every class that is supposed to react on a
31 generic event.
32 */
33 class observer{
34 public:
35  virtual void handle_generic_event(const std::string& event_name) = 0;
36  virtual ~observer() {}
37 };
38 
39 
40 /*
41 This is the class that notifies the observers and maintains a list of them.
42 */
44 public:
45  generic_event(const std::string& name);
46  virtual ~generic_event() {}
47 
48  virtual bool attach_handler(observer* obs);
49  virtual bool detach_handler(observer* obs);
50  virtual void notify_observers();
51 private:
52  //Name of the event to help event handlers distinguish between several events
53  std::string name_;
54 
55  //List of all subscribers waiting to react on this event
56  std::vector<observer*> observers_;
57 
58  //This flag makes sure, that an event is not raised while the vector of
59  //observers is changed through attach_handler or detach_handler
61 
62  //This flag makes sure, that attaching/detaching event handlers does not
63  //take place during notify of observers to prevent iterator corruption.
65 };
66 }
static const char * name(const std::vector< SDL_Joystick *> &joysticks, const std::size_t index)
Definition: joystick.cpp:48
std::vector< observer * > observers_
Handling of system events.
Definition: manager.hpp:41
virtual void handle_generic_event(const std::string &event_name)=0