-
Notifications
You must be signed in to change notification settings - Fork 2
/
PSpeaker.cxx
80 lines (68 loc) · 1.95 KB
/
PSpeaker.cxx
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
//==============================================================================
// File: PSpeaker.cxx
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <stdio.h>
#include <stdlib.h>
#include "PSpeaker.h"
#include "PListener.h"
const int kListInc = 10;
PSpeaker::PSpeaker()
{
mListeners = NULL;
mNumListeners = 0;
mMaxListeners = 0;
}
PSpeaker::~PSpeaker()
{
Speak(kMessageSpeakerDying, this);
while (mNumListeners) {
RemoveListener(*mListeners);
}
delete [] mListeners;
}
void PSpeaker::AddListener(PListener *aListener)
{
int i;
// don't add a listener twice
for (i=0; i<mNumListeners; ++i) {
if (aListener == mListeners[i]) return;
}
/* add listener to this speaker's list */
if (mNumListeners >= mMaxListeners) {
PListener **tmp = new PListener*[mMaxListeners + kListInc];
if (!tmp) {
fprintf(stderr,"Out of memory in PSpeaker::AddListener\n");
exit(1);
}
for (i=0; i<mNumListeners; ++i) tmp[i] = mListeners[i];
delete [] mListeners;
mListeners = tmp;
mMaxListeners += kListInc;
}
mListeners[mNumListeners++] = aListener;
/* add this speaker to listener's list */
aListener->AddSpeaker(this);
}
void PSpeaker::RemoveListener(PListener *aListener)
{
for (int i=0; i<mNumListeners; ++i) {
if (aListener == mListeners[i]) {
for (int j=i+1; j<mNumListeners; ++j) {
mListeners[j-1] = mListeners[j];
}
--mNumListeners;
}
}
/* remove this speaker from the listener's list */
aListener->RemoveSpeaker(this);
}
void PSpeaker::Speak(int message, void *dataPt)
{
for (int i=0; i<mNumListeners; ++i) {
if (!mListeners[i]->IsIgnoring()) {
mListeners[i]->Listen(message, dataPt);
}
}
}