]> git.saurik.com Git - apple/icu.git/blob - icuSources/common/icunotif.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / common / icunotif.cpp
1 /**
2 *******************************************************************************
3 * Copyright (C) 2001-2003, 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 #if DEBUG
14 #include <stdio.h>
15 #endif
16
17 U_NAMESPACE_BEGIN
18
19 EventListener::~EventListener() {}
20 UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener)
21
22 ICUNotifier::ICUNotifier(void)
23 : notifyLock(0), listeners(NULL)
24 {
25 umtx_init(&notifyLock);
26 }
27
28 ICUNotifier::~ICUNotifier(void) {
29 {
30 Mutex lmx(&notifyLock);
31 delete listeners;
32 listeners = NULL;
33 }
34 umtx_destroy(&notifyLock);
35 }
36
37
38 void
39 ICUNotifier::addListener(const EventListener* l, UErrorCode& status)
40 {
41 if (U_SUCCESS(status)) {
42 if (l == NULL) {
43 status = U_ILLEGAL_ARGUMENT_ERROR;
44 }
45
46 if (acceptsListener(*l)) {
47 Mutex lmx(&notifyLock);
48 if (listeners == NULL) {
49 listeners = new UVector(5, status);
50 } else {
51 for (int i = 0, e = listeners->size(); i < e; ++i) {
52 const EventListener* el = (const EventListener*)(listeners->elementAt(i));
53 if (l == el) {
54 return;
55 }
56 }
57 }
58
59 listeners->addElement((void*)l, status); // cast away const
60 } else {
61 #if DEBUG
62 fprintf(stderr, "Listener invalid for this notifier.");
63 exit(1);
64 #endif
65 }
66 }
67 }
68
69 void
70 ICUNotifier::removeListener(const EventListener *l, UErrorCode& status)
71 {
72 if (U_SUCCESS(status)) {
73 if (l == NULL) {
74 status = U_ILLEGAL_ARGUMENT_ERROR;
75 return;
76 }
77
78 {
79 Mutex lmx(&notifyLock);
80 if (listeners != NULL) {
81 // identity equality check
82 for (int i = 0, e = listeners->size(); i < e; ++i) {
83 const EventListener* el = (const EventListener*)listeners->elementAt(i);
84 if (l == el) {
85 listeners->removeElementAt(i);
86 if (listeners->size() == 0) {
87 delete listeners;
88 listeners = NULL;
89 }
90 return;
91 }
92 }
93 }
94 }
95 }
96 }
97
98 void
99 ICUNotifier::notifyChanged(void)
100 {
101 if (listeners != NULL) {
102 Mutex lmx(&notifyLock);
103 if (listeners != NULL) {
104 for (int i = 0, e = listeners->size(); i < e; ++i) {
105 EventListener* el = (EventListener*)listeners->elementAt(i);
106 notifyListener(*el);
107 }
108 }
109 }
110 }
111
112 U_NAMESPACE_END
113
114 /* UCONFIG_NO_SERVICE */
115 #endif
116