summaryrefslogtreecommitdiff
path: root/plugingui/notifier.h
blob: d68e28cb70b680aa3a2640f1a2c6f9bfe2576f1f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/***************************************************************************
 *            notifier.h
 *
 *  Thu Sep  3 15:48:39 CEST 2015
 *  Copyright 2015 Bent Bisballe Nyeng
 *  deva@aasimon.org
 ****************************************************************************/

/*
 *  This file is part of DrumGizmo.
 *
 *  DrumGizmo is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  DrumGizmo is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with DrumGizmo; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
 */
#pragma once

#include <iostream>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <type_traits>
#include <utility>

namespace GUI {

class Listener;
class NotifierBase {
public:
	virtual void disconnect(Listener* object) {}
};

class Listener {
public:
	virtual ~Listener()
	{
		for(auto signal = signals.begin(); signal != signals.end(); ++signal) {
			(*signal)->disconnect(this);
		}
	}

	void registerNotifier(NotifierBase* signal)
	{
		signals.insert(signal);
	}

	void unregisterNotifier(NotifierBase* signal)
	{
		signals.erase(signal);
	}

	std::set<NotifierBase*> signals;
};

template<typename T, typename... Args>
class Notifier : public NotifierBase {
public:
	Notifier() {}
	~Notifier()
	{
		for(auto slot = slots.begin(); slot != slots.end(); ++slot) {
			(*slot).first->unregisterNotifier(this);
		}
	}

	void connect(Listener* object, std::function<void(T, Args...)> slot)
	{
		slots[object] = slot;
		if(object) {
			object->registerNotifier(this);
		}
	}

	void disconnect(Listener* object)
	{
		slots.erase(object);
	}

	void notify(T t, Args...args)
	{
		for(auto slot = slots.begin(); slot != slots.end(); ++slot) {
			(*slot).second(t, args...);
		}
	}

	std::map<Listener*, std::function<void(T, Args...)>> slots;
};

} // GUI::

template<unsigned... Is> struct seq{};
template<unsigned I, unsigned... Is>
struct gen_seq : gen_seq<I-1, I-1, Is...>{};
template<unsigned... Is>
struct gen_seq<0, Is...> : seq<Is...>{};

template<unsigned I> struct placeholder{};

namespace std{
template<unsigned I>
struct is_placeholder< ::placeholder<I> > : integral_constant<int, I>{};
} // std::

namespace aux{
template<unsigned... Is, class F, class... Ts>
auto easy_bind(seq<Is...>, F&& f, Ts&&... vs)
	-> decltype(std::bind(std::forward<F>(f), std::forward<Ts>(vs)..., ::placeholder<1 + Is>()...))
{
    return std::bind(std::forward<F>(f), std::forward<Ts>(vs)..., ::placeholder<1 + Is>()...);
}
} // aux::

template<class R, class C, class... FArgs, class... Args>
auto mem_bind(R (C::*ptmf)(FArgs...), Args&&... vs)
	-> decltype(aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward<Args>(vs)...))
{
    // the +1s for 'this' argument
    static_assert(sizeof...(Args) <= sizeof...(FArgs) + 1, "too many arguments to mem_bind");
    return aux::easy_bind(gen_seq<(sizeof...(FArgs) + 1) - sizeof...(Args)>(), ptmf, std::forward<Args>(vs)...);
}

template<class T, class C, class... Args>
auto mem_bind(T C::*ptmd, Args&&... vs)
	-> decltype(aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward<Args>(vs)...))
{
    // just 'this' argument
    static_assert(sizeof...(Args) <= 1, "too many arguments to mem_bind");
    return aux::easy_bind(gen_seq<1 - sizeof...(Args)>(), ptmd, std::forward<Args>(vs)...);
}

//#define obj_connect_old(SRC, SIG, TAR, SLO) (SRC).SIG.connect(&(TAR), mem_bind(&decltype(TAR)::SLO, TAR))

#define fun_connect(SRC, SIG, SLO) \
	(SRC).SIG.connect(nullptr, SLO)

#define obj_connect(SRC, SIG, TAR, SLO) \
  (SRC)->SIG.connect(TAR, mem_bind(&SLO, std::ref(*TAR)))