The Battle for Wesnoth  1.15.0+dev
notifier.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2018 by Mark de Wever <koraq@xs4all.nl>
3  Part of the Battle for Wesnoth Project https://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 #pragma once
16 
17 #include "gui/core/notifiee.hpp"
18 
19 #include <cassert>
20 #include <map>
21 
22 namespace gui2
23 {
24 
25 /**
26  * Helper class to implement callbacks with lifetime management.
27  *
28  * This part manages the connecting and disconnecting of the callbacks.
29  *
30  * Subclasses should implement a way to call all callback.
31  */
32 template <class FUNCTOR>
33 class notifier
34 {
35 public:
36  typedef FUNCTOR functor_t;
37 
39  {
40  }
41 
43  {
44  for (auto & item : notifiees_)
45  {
46  assert(item.first);
47  assert((*item.first).notifier_ == this);
48 
49  (*item.first).notifier_ = nullptr;
50  }
51  }
52 
53  /**
54  * Connects a callback.
55  *
56  * @param target The notifiee controlling the lifetime of
57  * the callback.
58  * @param functor The callback to call.
59  */
60  void connect_notifiee(notifiee<functor_t>& target, functor_t functor)
61  {
62  notifiees_.emplace(&target, functor);
63 
64  assert(!target.notifier_);
65 
66  target.notifier_ = this;
67  }
68 
69  /**
70  * Disconnects a callback.
71  *
72  * @param target The notifiee controlling the lifetime of
73  * the callback. Uses since its address is an
74  * unique key.
75  */
77  {
78  typename std::map<notifiee<functor_t>*, functor_t>::iterator itor
79  = notifiees_.find(&target);
80 
81  if(itor != notifiees_.end()) {
82 
83  assert(target.notifier_ == this);
84 
85  target.notifier_ = nullptr;
86 
87  notifiees_.erase(itor);
88  }
89  }
90 
91  /***** ***** ***** setters / getters for members ***** ****** *****/
92 
93  const std::map<notifiee<functor_t>*, functor_t>& notifiees() const
94  {
95  return notifiees_;
96  }
97 
98 private:
99  /** List of registered callbacks. */
100  std::map<notifiee<functor_t>*, functor_t> notifiees_;
101 };
102 
103 } // namespace gui2
void disconnect_notifiee(notifiee< functor_t > &target)
Disconnects a callback.
Definition: notifier.hpp:76
Generic file dialog.
Definition: field-fwd.hpp:22
const std::map< notifiee< functor_t > *, functor_t > & notifiees() const
Definition: notifier.hpp:93
notifier< functor_t > * notifier_
Pointer the the notifier that&#39;s linked to us.
Definition: notifiee.hpp:50
void connect_notifiee(notifiee< functor_t > &target, functor_t functor)
Connects a callback.
Definition: notifier.hpp:60
std::string::const_iterator iterator
Definition: tokenizer.hpp:24
std::map< notifiee< functor_t > *, functor_t > notifiees_
List of registered callbacks.
Definition: notifier.hpp:100
std::pair< std::string, unsigned > item
Definition: help_impl.hpp:371
FUNCTOR functor_t
Definition: notifier.hpp:36