The Battle for Wesnoth  1.17.0-dev
notifier.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2010 - 2021
3  by Mark de Wever <koraq@xs4all.nl>
4  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY.
12 
13  See the COPYING file for more details.
14 */
15 
16 #pragma once
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 notifier
35 {
36 public:
37  typedef FUNCTOR functor_t;
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 target The notifiee controlling the lifetime of
58  * the callback.
59  * @param functor The callback to call.
60  */
61  void connect_notifiee(notifiee<functor_t>& target, functor_t functor)
62  {
63  notifiees_.emplace(&target, functor);
64 
65  assert(!target.notifier_);
66 
67  target.notifier_ = this;
68  }
69 
70  /**
71  * Disconnects a callback.
72  *
73  * @param target The notifiee controlling the lifetime of
74  * the callback. Uses since its address is an
75  * unique key.
76  */
78  {
79  auto itor = 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:77
Generic file dialog.
Definition: field-fwd.hpp:23
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:51
void connect_notifiee(notifiee< functor_t > &target, functor_t functor)
Connects a callback.
Definition: notifier.hpp:61
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:410
FUNCTOR functor_t
Definition: notifier.hpp:37