-
Notifications
You must be signed in to change notification settings - Fork 2
/
PListener.cxx
63 lines (56 loc) · 1.53 KB
/
PListener.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
//==============================================================================
// File: PListener.cxx
//
// Copyright (c) 2017, Phil Harvey, Queen's University
//==============================================================================
#include <stdio.h>
#include <stdlib.h>
#include "PListener.h"
#include "PSpeaker.h"
const int kListInc = 10;
PListener::PListener()
{
mIgnoring = 0;
mSpeakers = (PSpeaker **)0;
mNumSpeakers = 0;
mMaxSpeakers = 0;
}
PListener::~PListener()
{
while (mNumSpeakers) {
mSpeakers[0]->RemoveListener(this);
}
delete [] mSpeakers;
}
void PListener::AddSpeaker(PSpeaker *aSpeaker)
{
int i;
// don't add a speaker twice
for (i=0; i<mNumSpeakers; ++i) {
if (aSpeaker == mSpeakers[i]) return;
}
/* add speaker to this listener's list */
if (mNumSpeakers >= mMaxSpeakers) {
PSpeaker **tmp = new PSpeaker*[mMaxSpeakers + kListInc];
if (!tmp) {
fprintf(stderr,"Out of memory in PListener::AddSpeaker\n");
exit(1);
}
for (i=0; i<mNumSpeakers; ++i) tmp[i] = mSpeakers[i];
delete [] mSpeakers;
mSpeakers = tmp;
mMaxSpeakers += kListInc;
}
mSpeakers[mNumSpeakers++] = aSpeaker;
}
void PListener::RemoveSpeaker(PSpeaker *aSpeaker)
{
for (int i=0; i<mNumSpeakers; ++i) {
if (aSpeaker == mSpeakers[i]) {
for (int j=i+1; j<mNumSpeakers; ++j) {
mSpeakers[j-1] = mSpeakers[j];
}
--mNumSpeakers;
}
}
}