事件模型是被廣泛使用的好東西,但是C++標准庫裡沒有現成的,其他實現又復雜或者不優雅,比如需要使用宏。現在VC11可以用在XP下了,那麼就痛快的拿起C++11提供的先進設施組合出一個輕便的實現吧。
為了達到簡潔的目的,需要放棄一些特性:
1、不支持判斷函數是否已經綁定過(因為std::function不提供比較方法,自己實現function的話代碼又變多了)
2、需要使用者接收返回的回調函數標識來移除事件綁定(原因同上)
3、事件沒有返回值,不支持回調函數優先級、條件回調等事件高級特性(比如返回所有處理結果中的最大最小值;只回調與指定參數匹配的事件處理函數)
4、事件參數理論上無限,實際上有限,一般支持0~10個參數(VC11還沒有支持變長模板參數,GCC有了。不過可以通過缺省模板參數和偏特化來模擬,所以理論上無限制)
5、不是線程安全的
注:3、5兩條可以通過引入策略模式來提供靈活支持,就像標准庫和Loki做的那樣,實現一個完整的事件機制。
最簡單的實現
1 #include <map>
2 #include <functional>
3
4 using namespace std;
5
6
7 template<class Param1, class Param2>
8 class Event
9 {
10 typedef void HandlerT(Param1, Param2);
11 int m_handlerId;
12
13 public:
14 Event() : m_handlerId(0) {}
15
16 template<class FuncT> int addHandler(FuncT func)
17 {
18 m_handlers.emplace(m_handlerId, forward<FuncT>(func));
19 return m_handlerId++;
20 }
21
22 void removeHandler(int handlerId)
23 {
24 m_handlers.erase(handlerId);
25 }
26
27 void operator ()(Param1 arg1, Param2 arg2)
28 {
29 for ( const auto& i : m_handlers )
30 i.second(arg1, arg2);
31 }
32
33 private:
34 map<int, function<HandlerT>> m_handlers;
35 };
addHandler把回調函數完美轉發給std::function,讓標准庫來搞定各種重載,然後返回一個標識符用來注銷綁定。試一下,工作的不錯:
1 void f1(int, int)
2 {
3 puts("f1()");
4 }
5
6 struct F2
7 {
8 void f(int, int)
9 {
10 puts("f2()");
11 }
12
13 void operator ()(int, int)
14 {
15 puts("f3()");
16 }
17 };
18
19 int _tmain(int argc, _TCHAR* argv[])
20 {
21 Event<int, int> e;
22
23 int id = e.addHandler(f1);
24
25 e.removeHandler(id);
26
27 using namespace std::placeholders;
28
29 F2 f2;
30
31 e.addHandler(bind(&F2::f, f2, _1, _2));
32 e.addHandler(bind(f2, _1, _2));
33
34 e.addHandler([](int, int) {
35 puts("f4()");
36 });
37
38 e(1, 2);
39
40 return 0;
41 }
雖然這裡有一個小小的缺點,對於仿函數,如果想使用它的指針或引用是不可以直接綁定的,需要這樣做:
1 e.addHandler(ref(f2)); 2 e.addHandler(ref(*pf2)); // pf2是指向f2的指針
但是使用仿函數對象指針的情形不多,也不差多敲幾個字符,何況在有Lambda表達式的情況下呢?
改進
1、有人不喜歡bind,用起來麻煩,放到addhandler裡面去:
1 template<class ObjT, class FuncT> int addHandler(ObjT obj, FuncT func)
2 {
3 using namespace std::placeholders;
4 m_handlers.emplace(m_handlerId, std::bind(func, std::forward<ObjT>(obj), _1, _2));
5 return m_handlerId++;
6 }
2、擴展參數個數。沒有變長模板參數,變通一下:
1 struct NullType {};
2
3 template<class P1 = Private::NullType, class P2 = Private::NullType>
4 class Event
5 {
6 public:
7 template<class ObjT, class FuncT> int addHandler(ObjT obj, FuncT func)
8 {
9 using namespace std::placeholders;
10 m_handlers.emplace(m_handlerId, std::bind(func, std::forward<ObjT>(obj), _1, _2));
11 return m_handlerId++;
12 }
13
14 void operator ()(P1 arg1, P2 arg2)
15 {
16 for ( const auto& i : m_handlers )
17 i.second(arg1, arg2);
18 }
19 };
20
21 template<>
22 class Event<Private::NullType, Private::NullType>
23 {
24 public:
25 template<class ObjT, class FuncT> int addHandler(ObjT obj, FuncT func)
26 {
27 using namespace std::placeholders;
28 m_handlers.emplace(m_handlerId, std::bind(func, std::forward<ObjT>(obj)));
29 return m_handlerId++;
30 }
31
32 void operator ()()
33 {
34 for ( const auto& i : m_handlers )
35 i.second();
36 }
37 };
38
39 template<class P1>
40 class Event<P1, Private::NullType>
41 {
42 public:
43 template<class ObjT, class FuncT> int addHandler(ObjT obj, FuncT func)
44 {
45 using namespace std::placeholders;
46 m_handlers.emplace(m_handlerId, std::bind(func, std::forward<ObjT>(obj), _1));
47 return m_handlerId++;
48 }
49
50 void operator ()(P1 arg1)
51 {
52 for ( const auto& i : m_handlers )
53 i.second(arg1);
54 }
55 };
現在支持0~2個參數了。注意到各個模板裡有公共代碼,提取出來放進基類,然後要做的就是打開文本生成器了
補充一下:VC裡std::function默認最多5個參數,最多支持10個,要在編譯開關裡設置一下宏_VARIADIC_MAX=10