/
listeners_manager.cpp
180 lines (152 loc) · 5.49 KB
/
listeners_manager.cpp
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
173
174
175
176
177
178
179
180
/**
* =============================================================================
* Source Python
* Copyright (C) 2012-2015 Source Python Development Team. All rights reserved.
* =============================================================================
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License, version 3.0, as published by the
* Free Software Foundation.
*
* This program 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
* this program. If not, see <http://www.gnu.org/licenses/>.
*
* As a special exception, the Source Python Team gives you permission
* to link the code of this program (as well as its derivative works) to
* "Half-Life 2," the "Source Engine," and any Game MODs that run on software
* by the Valve Corporation. You must obey the GNU General Public License in
* all respects for all other code used. Additionally, the Source.Python
* Development Team grants this exception to all derivative works.
*/
//-----------------------------------------------------------------------------
// Includes.
//-----------------------------------------------------------------------------
#include "listeners_manager.h"
//-----------------------------------------------------------------------------
// Adds a callable to the end of the CListenerManager vector.
//-----------------------------------------------------------------------------
void CListenerManager::RegisterListener(PyObject* pCallable)
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));
// Is the callable already in the vector?
if( !IsRegistered(oCallable) )
{
if (!GetCount())
Initialize();
m_vecCallables.AddToTail(oCallable);
}
else {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback already registered.")
}
}
//-----------------------------------------------------------------------------
// Removes all instances of a callable from the CListenerManager vector.
//-----------------------------------------------------------------------------
void CListenerManager::UnregisterListener(PyObject* pCallable)
{
// Get the object instance of the callable
object oCallable = object(handle<>(borrowed(pCallable)));
int index = FindCallback(oCallable); //m_vecCallables.Find(oCallable);
if (index == -1) {
BOOST_RAISE_EXCEPTION(PyExc_ValueError, "Callback not registered.")
}
else {
m_vecCallables.Remove(index);
if (!GetCount())
Finalize();
}
}
//-----------------------------------------------------------------------------
// Notifies all registered callbacks.
//-----------------------------------------------------------------------------
void CListenerManager::Notify(tuple args, dict kwargs)
{
static object callback_caller = eval("lambda func, args, kwargs: func(*args, **kwargs)");
for(int i = 0; i < m_vecCallables.Count(); i++)
{
BEGIN_BOOST_PY()
callback_caller(m_vecCallables[i], args, kwargs);
END_BOOST_PY_NORET()
}
}
//-----------------------------------------------------------------------------
// Return the number of registered callbacks.
//-----------------------------------------------------------------------------
int CListenerManager::GetCount()
{
return m_vecCallables.Count();
}
//-----------------------------------------------------------------------------
// Called when the first callback is being registered.
//-----------------------------------------------------------------------------
void CListenerManager::Initialize()
{
override initialize = get_override("initialize");
if (!initialize.is_none())
initialize();
}
//-----------------------------------------------------------------------------
// Called when the last callback is being unregistered.
//-----------------------------------------------------------------------------
void CListenerManager::Finalize()
{
override finalize = get_override("finalize");
if (!finalize.is_none())
finalize();
}
//-----------------------------------------------------------------------------
// Return whether or not the given callback is registered.
//-----------------------------------------------------------------------------
bool CListenerManager::IsRegistered(object oCallback)
{
return FindCallback(oCallback) != -1;
//return m_vecCallables.HasElement(oCallback);
}
int CListenerManager::FindCallback(object oCallback)
{
for (int i=0; i < m_vecCallables.Count(); ++i) {
object oCurrent = m_vecCallables[i];
if (is_same_func(oCallback, oCurrent)) {
return i;
}
}
return -1;
}
object CListenerManager::__getitem__(unsigned int index)
{
if (index >= (unsigned int) m_vecCallables.Count())
BOOST_RAISE_EXCEPTION(PyExc_IndexError, "Index out of range.")
return m_vecCallables[index];
}
void CListenerManager::clear()
{
m_vecCallables.RemoveAll();
}
//-----------------------------------------------------------------------------
// Functions
//-----------------------------------------------------------------------------
bool is_same_func(object f1, object f2)
{
object self1, self2;
try {
self1 = f1.attr("__self__");
}
catch (...) {
PyErr_Clear();
return f1 == f2;
}
try {
self2 = f2.attr("__self__");
}
catch (...) {
PyErr_Clear();
return f1 == f2;
}
return self1.ptr() == self2.ptr() && f1.attr("__func__") == f2.attr("__func__");
}