-
Notifications
You must be signed in to change notification settings - Fork 0
/
eventhandler.h
executable file
·93 lines (57 loc) · 2.04 KB
/
eventhandler.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
//
// base_event.h
// delegation
//
// Created by 刘纬经 Liu on 5/7/12.
// Copyright (c) 2012 New York University. All rights reserved.
//
// Funtionality: it wraps up objects and provides a "event-broadcast" operation
// it works with the event class.
#ifndef WL_EVENTHANDLER_H
#define WL_EVENTHANDLER_H
namespace wl {
namespace details{ // hide complicated implementation
// class template: Base_EventHandler
// class type: abstract class
// description: provide interface: broadcast operation.
template <class SenderT, class ArgT, class RetT> class Base_EventHandler;
template <class SenderT, class ArgT, class RetT>
class Base_EventHandler<SenderT*, ArgT, RetT>
{
public:
virtual ~Base_EventHandler(){}
// Event broadcast operation
virtual RetT operator()(SenderT* sender, ArgT args) = 0;
};
// EventHandler
// Constraints on the first 2 argument to be pointer types;
template <class ObjT, class SenderT, class ArgT, class RetT> class EventHandler;
template <class ObjT, class SenderT, class ArgT, class RetT>
class EventHandler<ObjT*, SenderT*, ArgT, RetT> : public Base_EventHandler<SenderT*, ArgT, RetT>
{
typedef RetT (ObjT::*MemFunT) (SenderT*,ArgT);
ObjT* _obj; // refer, not own
MemFunT _fun;
public:
~EventHandler()
{
}
EventHandler(ObjT* obj, MemFunT memfun):
_obj(obj),
_fun(memfun)
{
}
virtual void operator()(SenderT* sender, ArgT args)
{
(_obj->*_fun)(sender, args);
}
};
}
template <class ObjT, class SenderT, class ArgT, class RetT>
details::EventHandler<ObjT*, SenderT*, ArgT, RetT>*
Subscriber(ObjT* obj, RetT (ObjT::*memfun)(SenderT*, ArgT))
{
return new details::EventHandler<ObjT*,SenderT*,ArgT,RetT>(obj,memfun);
}
}
#endif