gui/auxiliary/notifier.hpp

Go to the documentation of this file.
00001 /* $Id: notifier.hpp 52533 2012-01-07 02:35:17Z shadowmaster $ */
00002 /*
00003    Copyright (C) 2010 - 2012 by Mark de Wever <koraq@xs4all.nl>
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 GUI_WIDGETS_AUXILIARY_NOTIFIER_HPP_INCLUDED
00017 #define GUI_WIDGETS_AUXILIARY_NOTIFIER_HPP_INCLUDED
00018 
00019 #include "foreach.hpp"
00020 
00021 #include "gui/auxiliary/notifiee.hpp"
00022 
00023 #include <cassert>
00024 #include <map>
00025 
00026 namespace gui2 {
00027 
00028 /**
00029  * Helper class to implement callbacks with lifetime management.
00030  *
00031  * This part manages the connecting and disconnecting of the callbacks.
00032  *
00033  * Subclasses should implement a way to call all callback.
00034  */
00035 template<class FUNCTOR>
00036 class tnotifier
00037 {
00038 public:
00039 
00040     typedef FUNCTOR tfunctor;
00041 
00042     tnotifier()
00043         : notifiees_()
00044     {
00045     }
00046 
00047     ~tnotifier()
00048     {
00049         typedef std::pair<tnotifiee<tfunctor>* const, tfunctor> thack;
00050         foreach(thack& item, notifiees_) {
00051             assert(item.first);
00052             assert((*item.first).notifier_ == this);
00053 
00054             (*item.first).notifier_ = NULL;
00055         }
00056     }
00057 
00058     /**
00059      * Connects a callback.
00060      *
00061      * @param notifiee               The notifiee controlling the lifetime of
00062      *                               the callback.
00063      * @param functor                The callback to call.
00064      */
00065     void connect_notifiee(
00066               tnotifiee<tfunctor>& notifiee
00067             , tfunctor functor)
00068     {
00069         notifiees_.insert(std::make_pair(&notifiee, functor));
00070 
00071         assert(!notifiee.notifier_);
00072 
00073         notifiee.notifier_ = this;
00074     }
00075 
00076     /**
00077      * Disonnects a callback.
00078      *
00079      * @param notifiee               The notifiee controlling the lifetime of
00080      *                               the callback. Uses since its address is an
00081      *                               unique key.
00082      */
00083     void disconnect_notifiee(tnotifiee<tfunctor>& notifiee)
00084     {
00085         typename std::map<tnotifiee<tfunctor>*, tfunctor>::iterator
00086                 itor = notifiees_.find(&notifiee);
00087 
00088         if(itor != notifiees_.end()) {
00089 
00090             assert(notifiee.notifier_ == this);
00091 
00092             notifiee.notifier_ = NULL;
00093 
00094             notifiees_.erase(itor);
00095         }
00096     }
00097 
00098     /***** ***** ***** setters / getters for members ***** ****** *****/
00099 
00100     const std::map<tnotifiee<tfunctor>*, tfunctor>& notifiees() const
00101     {
00102         return notifiees_;
00103     }
00104 
00105 private:
00106 
00107     /** List of registered callbacks. */
00108     std::map<tnotifiee<tfunctor>*, tfunctor> notifiees_;
00109 
00110 };
00111 
00112 } //namespace gui2
00113 
00114 #endif
00115 
00116 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

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