00001 /* $Id: generic_event.hpp 23842 2008-02-16 08:47:16Z mordante $ */ 00002 /* 00003 Copyright (C) 2006 - 2008 by Joerg Hinrichs <joerg.hinrichs@alice-dsl.de> 00004 Part of the Battle for Wesnoth Project http://www.wesnoth.org/ 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License version 2 00008 or at your option any later version. 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY. 00011 00012 See the COPYING file for more details. 00013 */ 00014 00015 #ifndef GENERIC_EVENT_HPP_INCLUDED 00016 #define GENERIC_EVENT_HPP_INCLUDED 00017 00018 #include "global.hpp" 00019 00020 #include <string> 00021 #include <vector> 00022 00023 /* 00024 This is the basic framework for generic events. In contrast to events.cpp 00025 it does not concentrate on SDL events but events that wesnoth itself raises. 00026 It is also different to game_events.cpp in that it does not work with 00027 specific events but rather defines a generic framework. 00028 */ 00029 00030 namespace events{ 00031 /* 00032 This is the observer that gets notified, if a generic event takes place 00033 Use this as base class for every class that is supposed to react on a 00034 generic event. 00035 */ 00036 class observer{ 00037 public: 00038 virtual void handle_generic_event(const std::string& event_name) = 0; 00039 virtual ~observer(); 00040 }; 00041 00042 /* 00043 This is the class that notifies the observers and maintains a list of them. 00044 */ 00045 class generic_event{ 00046 public: 00047 generic_event(std::string name); 00048 virtual ~generic_event(); 00049 virtual bool attach_handler(observer* obs); 00050 virtual bool detach_handler(observer* obs); 00051 virtual void notify_observers(); 00052 private: 00053 //Name of the event to help event handlers distinguish between several events 00054 std::string name_; 00055 00056 //List of all subscribers waiting to react on this event 00057 std::vector<observer*> observers_; 00058 00059 //This flag makes sure, that an event is not raised while the vector of 00060 //observers is changed through attach_handler or detach_handler 00061 bool change_handler_; 00062 00063 //This flag makes sure, that attaching/detaching event handlers does not 00064 //take place during notify of observers to prevent iterator corruption. 00065 bool notify_active_; 00066 }; 00067 } 00068 00069 #endif
Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth | Gna! | Forum | Wiki | CIA | devdocs |