/src/icu/source/common/servnotf.cpp
Line  | Count  | Source (jump to first uncovered line)  | 
1  |  | // © 2016 and later: Unicode, Inc. and others.  | 
2  |  | // License & terms of use: http://www.unicode.org/copyright.html  | 
3  |  | /**  | 
4  |  |  *******************************************************************************  | 
5  |  |  * Copyright (C) 2001-2012, International Business Machines Corporation and    *  | 
6  |  |  * others. All Rights Reserved.                                                *  | 
7  |  |  *******************************************************************************  | 
8  |  |  */  | 
9  |  |  | 
10  |  | #include "unicode/utypes.h"  | 
11  |  |  | 
12  |  | #if !UCONFIG_NO_SERVICE  | 
13  |  |  | 
14  |  | #include "servnotf.h"  | 
15  |  | #ifdef NOTIFIER_DEBUG  | 
16  |  | #include <stdio.h>  | 
17  |  | #endif  | 
18  |  |  | 
19  |  | U_NAMESPACE_BEGIN  | 
20  |  |  | 
21  | 0  | EventListener::~EventListener() {} | 
22  |  | UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener)  | 
23  |  |  | 
24  |  | static UMutex notifyLock;  | 
25  |  |  | 
26  |  | ICUNotifier::ICUNotifier(void)   | 
27  |  | : listeners(NULL)   | 
28  | 0  | { | 
29  | 0  | }  | 
30  |  |  | 
31  | 0  | ICUNotifier::~ICUNotifier(void) { | 
32  | 0  |     { | 
33  | 0  |         Mutex lmx(¬ifyLock);  | 
34  | 0  |         delete listeners;  | 
35  | 0  |         listeners = NULL;  | 
36  | 0  |     }  | 
37  | 0  | }  | 
38  |  |  | 
39  |  |  | 
40  |  | void   | 
41  |  | ICUNotifier::addListener(const EventListener* l, UErrorCode& status)   | 
42  | 0  | { | 
43  | 0  |     if (U_SUCCESS(status)) { | 
44  | 0  |         if (l == NULL) { | 
45  | 0  |             status = U_ILLEGAL_ARGUMENT_ERROR;  | 
46  | 0  |             return;  | 
47  | 0  |         }  | 
48  |  |  | 
49  | 0  |         if (acceptsListener(*l)) { | 
50  | 0  |             Mutex lmx(¬ifyLock);  | 
51  | 0  |             if (listeners == NULL) { | 
52  | 0  |                 listeners = new UVector(5, status);  | 
53  | 0  |             } else { | 
54  | 0  |                 for (int i = 0, e = listeners->size(); i < e; ++i) { | 
55  | 0  |                     const EventListener* el = (const EventListener*)(listeners->elementAt(i));  | 
56  | 0  |                     if (l == el) { | 
57  | 0  |                         return;  | 
58  | 0  |                     }  | 
59  | 0  |                 }  | 
60  | 0  |             }  | 
61  |  |  | 
62  | 0  |             listeners->addElementX((void*)l, status); // cast away const  | 
63  | 0  |         }  | 
64  |  | #ifdef NOTIFIER_DEBUG  | 
65  |  |         else { | 
66  |  |             fprintf(stderr, "Listener invalid for this notifier.");  | 
67  |  |             exit(1);  | 
68  |  |         }  | 
69  |  | #endif  | 
70  | 0  |     }  | 
71  | 0  | }  | 
72  |  |  | 
73  |  | void   | 
74  |  | ICUNotifier::removeListener(const EventListener *l, UErrorCode& status)   | 
75  | 0  | { | 
76  | 0  |     if (U_SUCCESS(status)) { | 
77  | 0  |         if (l == NULL) { | 
78  | 0  |             status = U_ILLEGAL_ARGUMENT_ERROR;  | 
79  | 0  |             return;  | 
80  | 0  |         }  | 
81  |  |  | 
82  | 0  |         { | 
83  | 0  |             Mutex lmx(¬ifyLock);  | 
84  | 0  |             if (listeners != NULL) { | 
85  |  |                 // identity equality check  | 
86  | 0  |                 for (int i = 0, e = listeners->size(); i < e; ++i) { | 
87  | 0  |                     const EventListener* el = (const EventListener*)listeners->elementAt(i);  | 
88  | 0  |                     if (l == el) { | 
89  | 0  |                         listeners->removeElementAt(i);  | 
90  | 0  |                         if (listeners->size() == 0) { | 
91  | 0  |                             delete listeners;  | 
92  | 0  |                             listeners = NULL;  | 
93  | 0  |                         }  | 
94  | 0  |                         return;  | 
95  | 0  |                     }  | 
96  | 0  |                 }  | 
97  | 0  |             }  | 
98  | 0  |         }  | 
99  | 0  |     }  | 
100  | 0  | }  | 
101  |  |  | 
102  |  | void   | 
103  |  | ICUNotifier::notifyChanged(void)   | 
104  | 0  | { | 
105  | 0  |     if (listeners != NULL) { | 
106  | 0  |         Mutex lmx(¬ifyLock);  | 
107  | 0  |         if (listeners != NULL) { | 
108  | 0  |             for (int i = 0, e = listeners->size(); i < e; ++i) { | 
109  | 0  |                 EventListener* el = (EventListener*)listeners->elementAt(i);  | 
110  | 0  |                 notifyListener(*el);  | 
111  | 0  |             }  | 
112  | 0  |         }  | 
113  | 0  |     }  | 
114  | 0  | }  | 
115  |  |  | 
116  |  | U_NAMESPACE_END  | 
117  |  |  | 
118  |  | /* UCONFIG_NO_SERVICE */  | 
119  |  | #endif  | 
120  |  |  |