-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpattern.listener.h
More file actions
122 lines (91 loc) · 2.85 KB
/
pattern.listener.h
File metadata and controls
122 lines (91 loc) · 2.85 KB
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
119
120
121
122
/* *****************************************
* File: pattern.listener.h
* Version: see function getClassVersion()
* Purpose: a template Listener pattern
* *****************************************
* Library: Cross Platform C++ Classes (cpcc)
* Copyright: 2013 StarMessage software.
* License: Free for opensource projects.
* Commercial license for closed source projects.
* Web: http://www.StarMessageSoftware.com
* https://github.com/starmessage/cpcc
* email: sales -at- starmessage.info
* *****************************************
*/
#pragma once
#ifndef CPCCLISTENERPATTERN_H
#define CPCCLISTENERPATTERN_H
#include <string>
#include <set>
#include <assert.h>
#include "cpccUnicodeSupport.h"
// ---------------- class cmiListener -----------------
template<typename Tobject>
class cpccListener
{
private:
int _nSubscriptions;
public: // data
cpcc_string name;
public: // ctors
cpccListener(const cpcc_char *aName= _T("Unnamed cpcclistener")):
name(aName),
_nSubscriptions(0)
{ };
~cpccListener()
{
assert((_nSubscriptions==0) && _T("#2352: destroying cpcclistener, but is still subscribed"));
};
public: // functions
void virtual beNotifiedAbout(Tobject &anObject) = 0;
int getNumSubsciptions(void) { return _nSubscriptions; } ;
void increaseSubscriptions(void)
{
assert((_nSubscriptions>=0) && _T("#5370: _nSubscriptions<0"));
_nSubscriptions++;
};
void decreaseSubscriptions(void)
{
assert((_nSubscriptions>0) && _T("#5371: _nSubscriptions<=0"));
_nSubscriptions--;
};
};
template <typename Tobject>
class cpccListenersList
{
private:
std::set<cpccListener<Tobject> *> _listenersList;
public: //constructors
cpccListenersList()
{
assert(countListeners()==0 && _T("#3415 cmiListenersList constructor: Not empty list"));
};
~cpccListenersList()
{
// at this point if the program is terminating
// you cannot have any calls to IO like std::cout << "Destructor cmiListenersList"
assert((countListeners()==0) && _T("#5274: destroying cmiListenersList, but listeners still exist"));
};
public: //functions
void notifyListeners(Tobject &anObject)
{ int n=countListeners();
typename std::set<cpccListener<Tobject> *>::iterator itr;
for (itr = _listenersList.begin(); itr != _listenersList.end(); itr++ )
{
(*itr)->beNotifiedAbout(anObject);
n--;
}
assert(n==0 && _T("#8246: notifyListeners iterator did not work properly"));
};
void addListener(cpccListener<Tobject> & o)
{ _listenersList.insert(&o);
o.increaseSubscriptions();
};
void removeListener(cpccListener<Tobject> & o)
{ _listenersList.erase(&o);
o.decreaseSubscriptions();
};
int countListeners(void)
{ return _listenersList.size(); };
};
#endif // CPCCLISTENERPATTERN_H