The Battle for Wesnoth  1.19.0-dev
generic_event.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2006 - 2024
3  by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
17 
18 #include <string>
19 #include <vector>
20 
21 /*
22 This is the basic framework for generic events. In contrast to events.cpp
23 it does not concentrate on SDL events but events that wesnoth itself raises.
24 It is also different to game_events.cpp in that it does not work with
25 specific events but rather defines a generic framework.
26 */
27 
28 namespace events{
29 /*
30 This is the observer that gets notified, if a generic event takes place
31 Use this as base class for every class that is supposed to react on a
32 generic event.
33 */
34 class observer{
35 public:
36  virtual void handle_generic_event(const std::string& event_name) = 0;
37  virtual ~observer() {}
38 };
39 
40 
41 /*
42 This is the class that notifies the observers and maintains a list of them.
43 */
45 public:
46  generic_event(const std::string& name);
47  virtual ~generic_event() {}
48 
49  virtual bool attach_handler(observer* obs);
50  virtual bool detach_handler(observer* obs);
51  virtual void notify_observers();
52 private:
53  //Name of the event to help event handlers distinguish between several events
54  std::string name_;
55 
56  //List of all subscribers waiting to react on this event
57  std::vector<observer*> observers_;
58 
59  //This flag makes sure, that an event is not raised while the vector of
60  //observers is changed through attach_handler or detach_handler
62 
63  //This flag makes sure, that attaching/detaching event handlers does not
64  //take place during notify of observers to prevent iterator corruption.
66 };
67 }
generic_event(const std::string &name)
std::vector< observer * > observers_
virtual bool attach_handler(observer *obs)
virtual bool detach_handler(observer *obs)
virtual void notify_observers()
virtual void handle_generic_event(const std::string &event_name)=0
Handling of system events.