]> git.saurik.com Git - apple/icu.git/blame - icuSources/common/icunotif.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / common / icunotif.cpp
CommitLineData
b75a7d8f
A
1/**
2 *******************************************************************************
374ca955 3 * Copyright (C) 2001-2003, International Business Machines Corporation and *
b75a7d8f
A
4 * others. All Rights Reserved. *
5 *******************************************************************************
6 */
7
8#include "unicode/utypes.h"
9
10#if !UCONFIG_NO_SERVICE
11
12#include "icunotif.h"
374ca955 13#if DEBUG
b75a7d8f 14#include <stdio.h>
374ca955 15#endif
b75a7d8f
A
16
17U_NAMESPACE_BEGIN
18
374ca955
A
19EventListener::~EventListener() {}
20UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener)
21
22ICUNotifier::ICUNotifier(void)
23 : notifyLock(0), listeners(NULL)
24{
25 umtx_init(&notifyLock);
26}
27
28ICUNotifier::~ICUNotifier(void) {
29 {
30 Mutex lmx(&notifyLock);
31 delete listeners;
32 listeners = NULL;
33 }
34 umtx_destroy(&notifyLock);
35}
36
b75a7d8f
A
37
38void
39ICUNotifier::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
69void
70ICUNotifier::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
98void
99ICUNotifier::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
374ca955 112U_NAMESPACE_END
b75a7d8f
A
113
114/* UCONFIG_NO_SERVICE */
115#endif
116