This repository has been archived by the owner on Oct 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEventCallbackHandler.h
118 lines (82 loc) · 3.03 KB
/
EventCallbackHandler.h
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
/*
* EventCallbackHandler.h
* MWorksCore
*
* Created by David Cox on 12/9/09.
* Copyright 2009 Harvard University. All rights reserved.
*
*/
#ifndef EVENT_CALLBACK_HANDLER_H_
#define EVENT_CALLBACK_HANDLER_H_
#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <boost/enable_shared_from_this.hpp>
#include <string>
#include <map>
#ifdef USE_HASH_MAP_IN_CALLBACK_HANDLER
#include <ext/hash_map>
#endif
#include <map>
#include <boost/thread/mutex.hpp>
#include <boost/thread/recursive_mutex.hpp>
#include "Utilities.h"
#include "Event.h"
using namespace boost;
using namespace std;
using namespace __gnu_cxx;
namespace mw{
typedef boost::function<void(shared_ptr<Event>)> EventCallback;
// simple convenience class
class KeyedEventCallbackPair : public enable_shared_from_this<KeyedEventCallbackPair>{
protected:
shared_ptr<EventCallback> callback;
string key;
public:
KeyedEventCallbackPair();
KeyedEventCallbackPair(string _key, EventCallback _callback);
void operator=(const KeyedEventCallbackPair& cb_pair);
EventCallback getCallback();
string getKey();
void dummyCallback(shared_ptr<Event> evt);
};
#ifdef USE_HASH_MAP_IN_CALLBACK_HANDLER
typedef hash_multimap<int, KeyedEventCallbackPair> EventCallbackMap;
#else
typedef multimap<int, KeyedEventCallbackPair> EventCallbackMap;
#endif
typedef multimap<string, int> EventCallbackKeyCodeMap;
#define DEFAULT_CALLBACK_KEY "<default>"
#define ALWAYS_CALLBACK_KEY -1
#define ALWAYS_CALLBACK ALWAYS_CALLBACK_KEY
class EventCallbackHandler {
protected:
// A dictionary of callback functors by code
EventCallbackMap callbacks_by_code;
// A dictionary of event codes indexed by callback keys
EventCallbackKeyCodeMap codes_by_key;
// Thread safety measures
boost::mutex callbacks_lock;
boost::recursive_mutex recursive_callbacks_lock;
bool recursively_lock_callbacks;
void lock_callbacks();
void unlock_callbacks();
public:
EventCallbackHandler(bool locking){
recursively_lock_callbacks = locking;
}
virtual ~EventCallbackHandler(){
// grab these locks to ensure that anyone else who needs them
// is done
boost::recursive_mutex::scoped_lock recursive_lock(recursive_callbacks_lock);
boost::mutex::scoped_lock lock(callbacks_lock);
}
virtual void registerCallback(EventCallback cb, string callback_key = DEFAULT_CALLBACK_KEY);
virtual void registerCallback(int code, EventCallback cb, string callback_key = DEFAULT_CALLBACK_KEY);
//virtual void registerCallback(string tagname, EventCallback cb, string callback_key = DEFAULT_CALLBACK_KEY);
virtual void unregisterCallbacksNoLocking(const std::string &key);
virtual void unregisterCallbacks(const string &callback_key = DEFAULT_CALLBACK_KEY, bool locked = true);
// callback dispatch
virtual void handleCallbacks(shared_ptr<Event> evt);
};
}
#endif