forked from rtv/Stage
/
model_callbacks.cc
64 lines (48 loc) · 1.39 KB
/
model_callbacks.cc
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
#include "stage.hh"
using namespace Stg;
using namespace std;
void Model::AddCallback( callback_type_t type,
model_callback_t cb,
void* user )
{
//callbacks[address].insert( cb_t( cb, user ));
callbacks[type].insert( cb_t( cb, user ));
// debug info - record the global number of registered callbacks
if( type == CB_UPDATE )
{
assert( world->update_cb_count >= 0 );
world->update_cb_count++;
}
}
int Model::RemoveCallback( callback_type_t type,
model_callback_t callback )
{
set<cb_t>& callset = callbacks[type];
callset.erase( cb_t( callback, NULL) );
if( type == CB_UPDATE )
{
world->update_cb_count--;
assert( world->update_cb_count >= 0 );
}
// return the number of callbacks remaining for this address. Useful
// for detecting when there are none.
return callset.size();
}
int Model::CallCallbacks( callback_type_t type )
{
// maintain a list of callbacks that should be cancelled
vector<cb_t> doomed;
set<cb_t>& callset = callbacks[type];
FOR_EACH( it, callset )
{
const cb_t& cba = *it;
// callbacks return true if they should be cancelled
if( (cba.callback)( this, cba.arg ) )
doomed.push_back( cba );
}
FOR_EACH( it, doomed )
callset.erase( *it );
// return the number of callbacks remaining for this address. Useful
// for detecting when there are none.
return callset.size();
}