generic_event.hpp

Go to the documentation of this file.
00001 /* $Id: generic_event.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2006 - 2012 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 as published by
00008    the Free Software Foundation; either version 2 of the License, or
00009    (at your option) any later version.
00010    This program is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY.
00012 
00013    See the COPYING file for more details.
00014 */
00015 
00016 #ifndef GENERIC_EVENT_HPP_INCLUDED
00017 #define GENERIC_EVENT_HPP_INCLUDED
00018 
00019 #include "global.hpp"
00020 
00021 #include <string>
00022 #include <vector>
00023 
00024 /*
00025 This is the basic framework for generic events. In contrast to events.cpp
00026 it does not concentrate on SDL events but events that wesnoth itself raises.
00027 It is also different to game_events.cpp in that it does not work with
00028 specific events but rather defines a generic framework.
00029 */
00030 
00031 namespace events{
00032 /*
00033 This is the observer that gets notified, if a generic event takes place
00034 Use this as base class for every class that is supposed to react on a
00035 generic event.
00036 */
00037 class observer{
00038 public:
00039     virtual void handle_generic_event(const std::string& event_name) = 0;
00040     virtual ~observer() {}
00041 };
00042 
00043 
00044 /*
00045 This is the class that notifies the observers and maintains a list of them.
00046 */
00047 class generic_event{
00048 public:
00049     generic_event(std::string name);
00050     virtual ~generic_event() {}
00051 
00052     virtual bool attach_handler(observer* obs);
00053     virtual bool detach_handler(observer* obs);
00054     virtual void notify_observers();
00055 private:
00056     //Name of the event to help event handlers distinguish between several events
00057     std::string name_;
00058 
00059     //List of all subscribers waiting to react on this event
00060     std::vector<observer*> observers_;
00061 
00062     //This flag makes sure, that an event is not raised while the vector of
00063     //observers is changed through attach_handler or detach_handler
00064     bool change_handler_;
00065 
00066     //This flag makes sure, that attaching/detaching event handlers does not
00067     //take place during notify of observers to prevent iterator corruption.
00068     bool notify_active_;
00069 };
00070 }
00071 
00072 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Fri May 25 2012 01:02:53 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs