00001 /* 00002 * The Mana World Server 00003 * Copyright 2007 The Mana World Development Team 00004 * 00005 * This file is part of The Mana World. 00006 * 00007 * The Mana World is free software; you can redistribute it and/or modify it 00008 * under the terms of the GNU General Public License as published by the Free 00009 * Software Foundation; either version 2 of the License, or any later version. 00010 * 00011 * The Mana World is distributed in the hope that it will be useful, but 00012 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 00013 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 00014 * more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with The Mana World; if not, write to the Free Software Foundation, Inc., 00018 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00019 */ 00020 00021 #include <cassert> 00022 00023 #include "game-server/thing.hpp" 00024 00025 #include "game-server/eventlistener.hpp" 00026 00027 Thing::~Thing() 00028 { 00029 /* As another object will stop listening and call removeListener when it is 00030 deleted, the following assertion ensures that all the calls to 00031 removeListener have been performed will this object was still alive. It 00032 is not strictly necessary, as there are cases where no removal is 00033 performed (e.g. ~SpawnArea). But this is rather exceptional, so keep the 00034 assertion to catch all the other forgotten calls to removeListener. */ 00035 assert(mListeners.empty()); 00036 } 00037 00038 void Thing::addListener(const EventListener *l) 00039 { 00040 mListeners.insert(l); 00041 } 00042 00043 void Thing::removeListener(const EventListener *l) 00044 { 00045 mListeners.erase(l); 00046 } 00047 00048 void Thing::inserted() 00049 { 00050 for (Listeners::iterator i = mListeners.begin(), 00051 i_end = mListeners.end(); i != i_end;) 00052 { 00053 const EventListener &l = **i; 00054 ++i; // In case the listener removes itself from the list on the fly. 00055 if (l.dispatch->inserted) l.dispatch->inserted(&l, this); 00056 } 00057 } 00058 00059 void Thing::removed() 00060 { 00061 for (Listeners::iterator i = mListeners.begin(), 00062 i_end = mListeners.end(); i != i_end;) 00063 { 00064 const EventListener &l = **i; 00065 ++i; // In case the listener removes itself from the list on the fly. 00066 if (l.dispatch->removed) l.dispatch->removed(&l, this); 00067 } 00068 }