Callback.h
1//-*-C++-*-
2/***************************************************************************
3 *
4 * Copyright (C) 2004 by Willem van Straten
5 * Licensed under the Academic Free License version 2.1
6 *
7 ***************************************************************************/
8
9// psrchive/Util/units/Callback.h
10
11/* *************************************************************************
12
13 Callback - a namespace implementing a means of sending signals
14
15 Willem van Straten - February 2000 and May 2003
16
17 ************************************************************************* */
18
19#ifndef __Callback_Sender_h
20#define __Callback_Sender_h
21
22#include <vector>
23#include "Functor.h"
24
26template<class Type>
27class Callback : public Reference::Able {
28
29public:
30
32 bool verbose;
33
35 Callback () { verbose = false; }
36
38 virtual ~Callback () { }
39
41 void operator () (const Type& data) { send (data); }
42
44 void send (const Type& data)
45 {
46 clean ();
47 for (unsigned irecv=0; irecv < recipients.size(); irecv++) {
48 if (verbose) std::cerr << "Callback::send " << irecv
49 << "/" << recipients.size() << std::endl;
50 recipients[irecv] -> call (data);
51 }
52 }
53
55 template<class Class, typename Method>
56 void connect (Class* instance, Method method)
57 {
58 change (instance, method, true);
59 }
60
62 template<class Class, typename Method>
63 void disconnect (Class* instance, Method method)
64 {
65 change (instance, method, false);
66 }
67
69 template<typename Function>
70 void connect (Function function)
71 {
72 change (function, true);
73 }
74
76 template<typename Function>
77 void disconnect (Function function)
78 {
79 change (function, false);
80 }
81
82 template<class Class, typename Method>
83 void connect (Reference::To<Class>& ref, Method method)
84 {
85 connect (ref.get(), method);
86 }
87
88 template<class Class, typename Method>
89 void disconnect (Reference::To<Class>& ref, Method method)
90 {
91 disconnect (ref.get(), method);
92 }
93
94protected:
95
96 std::vector< Reference::To<typename Functor<void(Type)>::Base> > recipients;
97
98
99 template<class Class, typename Method>
100 void change (Class* instance, Method method, bool add)
101 {
102 if (!instance || !method)
103 return;
104
105 typedef typename Functor<void(Type)>::template
106 Method<Class, Method> ReceiverBase;
107
108 ReceiverBase* receiver = 0;
109
110 for (unsigned i=0; i < recipients.size(); i++) {
111
112 receiver = dynamic_cast<ReceiverBase*>(recipients[i].ptr());
113
114 if (receiver && receiver -> matches (instance, method)) {
115 if (!add)
116 recipients.erase(recipients.begin()+i);
117 return;
118 }
119
120 }
121
122 if (add) {
123 receiver = new ReceiverBase (instance, method);
124 recipients.push_back (receiver);
125 }
126
127 }
128
129 template<typename Function>
130 void change (Function function, bool add)
131 {
132 if (!function)
133 return;
134
135 typedef typename Functor<void(Type)>::template
136 Function<Function> ReceiverBase;
137
138 ReceiverBase* receiver = 0;
139
140 for (unsigned i=0; i < recipients.size(); i++) {
141
142 receiver = dynamic_cast<ReceiverBase*>(recipients[i].ptr());
143
144 if (receiver && receiver -> matches (function)) {
145 if (!add)
146 recipients.erase(recipients.begin()+i);
147 return;
148 }
149
150 }
151
152 if (add) {
153 receiver = new ReceiverBase (function);
154 recipients.push_back (receiver);
155 }
156
157 }
158
159 void clean ()
160 {
161 for (unsigned i=0; i < recipients.size(); )
162 if (!recipients[i]->is_valid())
163 recipients.erase(recipients.begin()+i);
164 else
165 i++;
166 }
167
168};
169
170#endif // #ifndef __Callback_Callback_h
bool verbose
Verbosity flag.
Definition Callback.h:32
void connect(Function function)
Add an function to be called during send.
Definition Callback.h:70
void operator()(const Type &data)
Operator interface to send method.
Definition Callback.h:41
Callback()
Default constructor.
Definition Callback.h:35
void connect(Class *instance, Method method)
Add an instance and its method to be called during send.
Definition Callback.h:56
void send(const Type &data)
Call all registered methods, passing data as the argument.
Definition Callback.h:44
virtual ~Callback()
Destructor.
Definition Callback.h:38
void disconnect(Function function)
Remove function.
Definition Callback.h:77
void disconnect(Class *instance, Method method)
Remove instance and its method.
Definition Callback.h:63
Manages Reference::To references to the instance.
Definition ReferenceAble.h:35
Template class manages Reference::Able objects.
Definition ReferenceTo.h:25
Type * get() const
Return the pointer.
Definition ReferenceTo.h:251

Generated using doxygen 1.14.0