-
Notifications
You must be signed in to change notification settings - Fork 1
/
Notifier.h
99 lines (80 loc) · 1.9 KB
/
Notifier.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
//
// Notifier.h
// MessageLoop
//
// Created by Lymons on 14-3-20.
// Copyright (c) 2014年 Lymons. All rights reserved.
//
#ifndef __MessageLoop__Notifier__
#define __MessageLoop__Notifier__
#include <iostream>
#include <map>
#include "Handler.h"
#include "Mutex.h"
using namespace std;
template<class T>
class Notifier
{
public:
static T& Instance() {
// Unique point of access
if (0 == _instance) {
MLock l(_mutex);
_instance = new T();
}
return *_instance;
}
protected:
Notifier(){}
virtual ~Notifier(){}
void SetHandler(Handler* h, int iden);
void RemoveHandler(int iden);
private:
static void Destroy() {
// Destroy the only instance
if ( _instance != 0 ) {
MLock l(_mutex);
delete _instance;
_instance = 0;
}
}
static T * volatile _instance; // The one and only instance
map<int, Handler*> _handlers;
public:
void Notify(int id);
public:
virtual int CreateServer() = 0;
virtual int CreateClient(Handler *h) = 0;
virtual void DestoryServer(int id) = 0;
virtual void DestoryClient(int id) = 0;
virtual bool SendMessage(int id, void* m) = 0;
virtual void* RecvMessage(int id) = 0;
virtual void StartMonitor() = 0;
protected:
static MMutex _mutex;
};
template<class T>
MMutex Notifier<T>::_mutex;
template<class T>
T * volatile Notifier<T>::_instance = 0;
template<class T>
void Notifier<T>::SetHandler(Handler* h, int iden)
{
MLock l(_mutex);
_handlers[iden] = h;
}
template<class T>
void Notifier<T>::RemoveHandler(int iden)
{
MLock l(_mutex);
_handlers.erase(iden);
}
template<class T>
void Notifier<T>::Notify(int id)
{
Handler* handler = _handlers[id];
if (handler) {
handler->QueuePendingMessage();
}
}
#endif /* defined(__MessageLoop__Notifier__) */