generic_event.cpp

Go to the documentation of this file.
00001 /* $Id: generic_event.cpp 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 #include "generic_event.hpp"
00016 
00017 #include <algorithm>
00018 
00019 namespace events{
00020     observer::~observer() {
00021     }
00022     generic_event::generic_event(std::string name){
00023         name_ = name;
00024         change_handler_ = false;
00025         notify_active_ = false;
00026     }
00027     generic_event::~generic_event() {
00028     }
00029     bool generic_event::attach_handler(observer* obs){
00030         bool handler_attached = false;
00031 
00032         //make sure observers are not notified right now
00033         if (!notify_active_){
00034             change_handler_ = true;
00035             try{
00036                 std::vector<observer*>::const_iterator it = std::find(observers_.begin(), observers_.end(), obs);
00037                 if (it != observers_.end()){
00038                     handler_attached = false;
00039                 }
00040                 else{
00041                     observers_.push_back(obs);
00042                     handler_attached = true;
00043                 }
00044             }
00045             catch (std::exception&){
00046                 change_handler_ = false;
00047                 throw;
00048             }
00049             change_handler_ = false;
00050         }
00051 
00052         return handler_attached;
00053     }
00054 
00055     bool generic_event::detach_handler(observer* obs){
00056         bool handler_detached = false;
00057 
00058         //make sure observers are not notified right now
00059         if (!notify_active_){
00060             change_handler_ = true;
00061             try{
00062                 std::vector<observer*>::iterator it = std::find(observers_.begin(), observers_.end(), obs);
00063                 if (it == observers_.end()){
00064                     handler_detached = false;
00065                 }
00066                 else{
00067                     observers_.erase(it);
00068                     handler_detached = true;
00069                 }
00070             }
00071             catch (std::exception&){
00072                 change_handler_ = false;
00073                 throw;
00074             }
00075             change_handler_ = false;
00076         }
00077 
00078         return handler_detached;
00079     }
00080 
00081     void generic_event::notify_observers(){
00082         if (!change_handler_){
00083             notify_active_ = true;
00084             try{
00085                 for (std::vector<observer*>::const_iterator it = observers_.begin();
00086                     it != observers_.end(); it++){
00087                     (*it)->handle_generic_event(name_);
00088                 }
00089             }
00090             catch (std::exception&){
00091                 //reset the flag if event handlers throw exceptions and don't catch them
00092                 notify_active_ = false;
00093                 throw;
00094             }
00095             notify_active_ = false;
00096         }
00097     }
00098 
00099 } //namespace events

Generated by doxygen 1.5.5 on 23 May 2008 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs