]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/icunotif.cpp
ICU-3.13.tar.gz
[apple/icu.git] / icuSources / common / icunotif.cpp
1 /**
2 *******************************************************************************
3 * Copyright (C) 2001-2002, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 */
7
8 #include "unicode/utypes.h"
9
10 #if !UCONFIG_NO_SERVICE
11
12 #include "icunotif.h"
13 #include <stdio.h>
14
15 U_NAMESPACE_BEGIN
16
17 const char EventListener::fgClassID = '\0';
18
19 void
20 ICUNotifier::addListener(const EventListener* l, UErrorCode& status)
21 {
22 if (U_SUCCESS(status)) {
23 if (l == NULL) {
24 status = U_ILLEGAL_ARGUMENT_ERROR;
25 }
26
27 if (acceptsListener(*l)) {
28 Mutex lmx(&notifyLock);
29 if (listeners == NULL) {
30 listeners = new UVector(5, status);
31 } else {
32 for (int i = 0, e = listeners->size(); i < e; ++i) {
33 const EventListener* el = (const EventListener*)(listeners->elementAt(i));
34 if (l == el) {
35 return;
36 }
37 }
38 }
39
40 listeners->addElement((void*)l, status); // cast away const
41 } else {
42 #if DEBUG
43 fprintf(stderr, "Listener invalid for this notifier.");
44 exit(1);
45 #endif
46 }
47 }
48 }
49
50 void
51 ICUNotifier::removeListener(const EventListener *l, UErrorCode& status)
52 {
53 if (U_SUCCESS(status)) {
54 if (l == NULL) {
55 status = U_ILLEGAL_ARGUMENT_ERROR;
56 return;
57 }
58
59 {
60 Mutex lmx(&notifyLock);
61 if (listeners != NULL) {
62 // identity equality check
63 for (int i = 0, e = listeners->size(); i < e; ++i) {
64 const EventListener* el = (const EventListener*)listeners->elementAt(i);
65 if (l == el) {
66 listeners->removeElementAt(i);
67 if (listeners->size() == 0) {
68 delete listeners;
69 listeners = NULL;
70 }
71 return;
72 }
73 }
74 }
75 }
76 }
77 }
78
79 void
80 ICUNotifier::notifyChanged(void)
81 {
82 if (listeners != NULL) {
83 Mutex lmx(&notifyLock);
84 if (listeners != NULL) {
85 for (int i = 0, e = listeners->size(); i < e; ++i) {
86 EventListener* el = (EventListener*)listeners->elementAt(i);
87 notifyListener(*el);
88 }
89 }
90 }
91 }
92
93 U_NAMESPACE_END;
94
95 /* UCONFIG_NO_SERVICE */
96 #endif
97