-
Notifications
You must be signed in to change notification settings - Fork 22
/
callback_list.hpp
94 lines (88 loc) · 2.05 KB
/
callback_list.hpp
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
/* (c) William Edwards, 2011
Using the Simplified BSD License. See LICENSE file for details */
#ifndef CALLBACK_LIST_HPP
#define CALLBACK_LIST_HPP
#include "error.hpp"
#include <vector>
#include <algorithm>
#include <assert.h>
#include <cstdio>
template<typename C> class CallbackList {
// a re-entrant safe list of callbacks
public:
CallbackList(): state(UNLOCKED) {}
void add(C* c) {
const size_t count = callbacks.size();
for(size_t i=0; i<count; i++)
if(callbacks[i] == c) {
assert(!"adding duplicate callback");
return;
}
callbacks.push_back(c);
}
void remove(C* c) {
const size_t count = callbacks.size();
for(size_t i=0; i<count; i++) {
if(callbacks[i] == c) {
if(UNLOCKED==state) {
callbacks[i] = callbacks[count-1];
callbacks.resize(count-1);
} else {
callbacks[i] = NULL;
state = COMPRESS;
}
return;
}
}
}
size_t count() const { return callbacks.size(); }
template<typename Func> void notify(Func& f) {
if(size_t count = callbacks.size()) {
state = LOCKED;
if(1 == count) {
invoke(f,callbacks[0]);
} else {
for(size_t i=0; i<count; i++)
invoke(f,callbacks[i]);
}
if(COMPRESS == state) {
count = callbacks.size();
Callbacks fresh;
for(size_t i=0; i<count; i++)
if(callbacks[i])
fresh.push_back(callbacks[i]);
callbacks.clear();
count = fresh.size();
for(size_t i=0; i<count; i++)
callbacks.push_back(fresh[i]);
}
state = UNLOCKED;
}
}
private:
template<typename Func> void invoke(Func& f,C* c) {
if(!c || c->is_closed())
return;
try {
f(c);
return;
} catch(std::exception& e) {
c->dump_context(stderr);
fprintf(stderr,"%s: unexpected exception in callback\n",e.what());
} catch(...) {
c->dump_context(stderr);
fprintf(stderr,"unexpected exception in callback\n");
}
c->close();
remove(c);
}
private:
typedef std::vector<C*> Callbacks;
Callbacks callbacks;
enum {
UNLOCKED,
LOCKED,
COMPRESS
} state;
};
#endif //CALLBACK_LIST_HPP