1 |
/** |
2 |
* callback.C: C++ callback mechanism |
3 |
* Rights to this code are documented in doc/pod/license.pod. |
4 |
* |
5 |
* Copyright © 2007 Pippijn van Steenhoven |
6 |
*/ |
7 |
|
8 |
static char const rcsid[] = "$Id: callback.C,v 1.3 2007-07-21 13:23:21 pippijn Exp $"; |
9 |
|
10 |
#include <svsconfig.h> |
11 |
#include <common/callback.h> |
12 |
|
13 |
has_callbacks::callbacks has_callbacks::callback; |
14 |
|
15 |
has_callbacks::has_callbacks () |
16 |
{ |
17 |
callback.created (this); |
18 |
} |
19 |
|
20 |
has_callbacks::~has_callbacks () |
21 |
{ |
22 |
callback.destroyed (this); |
23 |
} |
24 |
|
25 |
namespace callback |
26 |
{ |
27 |
has_listeners::has_listeners () |
28 |
{ |
29 |
} |
30 |
|
31 |
has_listeners::~has_listeners () |
32 |
{ |
33 |
disconnect_all (); |
34 |
} |
35 |
|
36 |
has_listeners::sender_set & |
37 |
has_listeners::invokers () |
38 |
{ |
39 |
return this->m_senders; |
40 |
} |
41 |
|
42 |
const has_listeners::sender_set & |
43 |
has_listeners::invokers () const |
44 |
{ |
45 |
return this->m_senders; |
46 |
} |
47 |
|
48 |
|
49 |
has_listeners::has_listeners (const has_listeners &rhs) |
50 |
{ |
51 |
this->copy_invokers (rhs); |
52 |
} |
53 |
|
54 |
has_listeners & |
55 |
has_listeners::operator = (const has_listeners &rhs) |
56 |
{ |
57 |
this->disconnect_all (); |
58 |
this->copy_invokers (rhs); |
59 |
return *this; |
60 |
} |
61 |
|
62 |
void |
63 |
has_listeners::add_invoker (event_base * sender) |
64 |
{ |
65 |
this->invokers ().insert (sender); |
66 |
} |
67 |
|
68 |
void |
69 |
has_listeners::remove_invoker (event_base * sender) |
70 |
{ |
71 |
this->invokers ().erase (sender); |
72 |
} |
73 |
|
74 |
void |
75 |
has_listeners::copy_invokers (const has_listeners &rhs) |
76 |
{ |
77 |
if (&rhs == this) |
78 |
return; |
79 |
|
80 |
const_iterator it; |
81 |
const_iterator itEnd = rhs.invokers ().end (); |
82 |
|
83 |
for (it = rhs.invokers ().begin (); it != itEnd; ++it) |
84 |
{ |
85 |
(*it)->listener_duplicate (&rhs, this); |
86 |
this->invokers ().insert (*it); |
87 |
} |
88 |
} |
89 |
|
90 |
void |
91 |
has_listeners::disconnect_all () |
92 |
{ |
93 |
iterator it; |
94 |
iterator et = this->invokers ().end (); |
95 |
|
96 |
for (it = this->invokers ().begin (); et != it; ++it) |
97 |
(*it)->detach (this); |
98 |
|
99 |
this->invokers ().clear (); |
100 |
} |
101 |
} // namespace callback |