summaryrefslogtreecommitdiff
path: root/plugingui/test.cc
blob: 2f60d379c6e007ebee179916beb375b0f7b0ae0e (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#include <iostream>
#include <functional>
#include <vector>
#include <map>
#include <set>
#include <type_traits>
#include <utility>
 
/////////////////////////////////////////
//
// Listener and Notifier classes:
//
/////////////////////////////////////////
 
class Listener;
class NotifierBase {
public:
        virtual void disconnect(Listener* object) {}
};
 
class Listener {
public:
        virtual ~Listener()
        {
                for (auto signal: signals) {
                        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) {
                        slot.first->unregisterNotifier(this);
                }
        }
 
        void connect(Listener* object, std::function<void(T, Args...)> slot)
        {
                slots[object] = slot;
                object->registerNotifier(this);
        }
 
        void disconnect(Listener* object)
        {
                slots.erase(object);
        }
 
        void notify(T t, Args...args)
        {
                for (auto slot: slots) {
                        slot.second(t, args...);
                }
        }
 
        std::map<Listener*, std::function<void(T, Args...)>> slots;
};
 
/////////////////////////////////////////
//
// Code which I don't understand but does something clever and produces a compile warning:
//
/////////////////////////////////////////
 
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)...);
}
 
 
 
/////////////////////////////////////////
//
//  Example:
//
/////////////////////////////////////////
 
class MyObject : public Listener {
public:
        void onMouseMoved(int x, int y)
        {
                std::cout << __PRETTY_FUNCTION__ << std::endl;
                std::cout << "Mouse moved: " << x << "," << y << std::endl;
        }
};
 
class MyObject2 : public Listener {
public:
        void mouseMoved(int x, int y)
        {
                std::cout << __PRETTY_FUNCTION__ << std::endl;
                std::cout << "Mouse moved: " << x << "," << y << std::endl;
        }
};
 
class Button {
public:
        Notifier<int, int> mouseMove; // arguments: (int x, int y)
 
        void emulateMouseMove(int x, int y)
        {
                mouseMove.notify(x, y);
        }
};
 
 
#define CONN(O, M) &O, mem_bind(&decltype(O)::M, O)
#define _connect(SRC, SIG, TAR, SLO) SRC.SIG.connect(&TAR, mem_bind(&decltype(TAR)::SLO, TAR))
 
int main()
{
        Button btn;
 
        MyObject object;
        //btn.mouseMove.connect(&object, std::bind(&MyObject::onMouseMoved, std::ref(object), _1, _2 )); // 'Vanilla' interface
        //btn.mouseMove.connect(&object, std::bind(&decltype::onMouseMoved, std::ref(object), _1, _2 )); // Use decltype
        //btn.mouseMove.connect(&object, mem_bind(&decltype(object)::onMouseMoved, object)); // Use clever mem_bind construct
        btn.mouseMove.connect(CONN(object, onMouseMoved)); // Use convenience macro
 
        _connect(btn, mouseMove, object, onMouseMoved);
 
        MyObject2 object2;
        btn.mouseMove.connect(CONN(object2, mouseMoved));
 
        // Now trigger the notification
        btn.emulateMouseMove(10,10);
}