The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
notifier.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2016 by Mark de Wever <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #ifndef GUI_WIDGETS_AUXILIARY_NOTIFIER_HPP_INCLUDED
16 #define GUI_WIDGETS_AUXILIARY_NOTIFIER_HPP_INCLUDED
17 
18 #include "gui/core/notifiee.hpp"
19 
20 #include <cassert>
21 #include <map>
22 
23 namespace gui2
24 {
25 
26 /**
27  * Helper class to implement callbacks with lifetime management.
28  *
29  * This part manages the connecting and disconnecting of the callbacks.
30  *
31  * Subclasses should implement a way to call all callback.
32  */
33 template <class FUNCTOR>
34 class tnotifier
35 {
36 public:
37  typedef FUNCTOR tfunctor;
38 
40  {
41  }
42 
44  {
45  for (auto & item : notifiees_)
46  {
47  assert(item.first);
48  assert((*item.first).notifier_ == this);
49 
50  (*item.first).notifier_ = nullptr;
51  }
52  }
53 
54  /**
55  * Connects a callback.
56  *
57  * @param notifiee The notifiee controlling the lifetime of
58  * the callback.
59  * @param functor The callback to call.
60  */
61  void connect_notifiee(tnotifiee<tfunctor>& notifiee, tfunctor functor)
62  {
63  notifiees_.insert(std::make_pair(&notifiee, functor));
64 
65  assert(!notifiee.notifier_);
66 
67  notifiee.notifier_ = this;
68  }
69 
70  /**
71  * Disconnects a callback.
72  *
73  * @param notifiee The notifiee controlling the lifetime of
74  * the callback. Uses since its address is an
75  * unique key.
76  */
78  {
79  typename std::map<tnotifiee<tfunctor>*, tfunctor>::iterator itor
80  = notifiees_.find(&notifiee);
81 
82  if(itor != notifiees_.end()) {
83 
84  assert(notifiee.notifier_ == this);
85 
86  notifiee.notifier_ = nullptr;
87 
88  notifiees_.erase(itor);
89  }
90  }
91 
92  /***** ***** ***** setters / getters for members ***** ****** *****/
93 
94  const std::map<tnotifiee<tfunctor>*, tfunctor>& notifiees() const
95  {
96  return notifiees_;
97  }
98 
99 private:
100  /** List of registered callbacks. */
101  std::map<tnotifiee<tfunctor>*, tfunctor> notifiees_;
102 };
103 
104 } // namespace gui2
105 
106 #endif
void disconnect_notifiee(tnotifiee< tfunctor > &notifiee)
Disconnects a callback.
Definition: notifier.hpp:77
itor second functor(surf, f[1])
A class inherited from ttext_box that displays its input as stars.
Definition: field-fwd.hpp:23
FUNCTOR tfunctor
Definition: notifier.hpp:37
void connect_notifiee(tnotifiee< tfunctor > &notifiee, tfunctor functor)
Connects a callback.
Definition: notifier.hpp:61
std::map< std::string, tfilter >::iterator itor
Definition: filter.cpp:199
const std::map< tnotifiee< tfunctor > *, tfunctor > & notifiees() const
Definition: notifier.hpp:94
std::string::const_iterator iterator
Definition: tokenizer.hpp:21
tnotifier< tfunctor > * notifier_
Pointer the the tnotifier that's linked to us.
Definition: notifiee.hpp:51
std::map< tnotifiee< tfunctor > *, tfunctor > notifiees_
List of registered callbacks.
Definition: notifier.hpp:101