Coverage Report

Created: 2024-04-24 06:23

/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(&notifyLock);
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(&notifyLock);
51
0
            if (listeners == NULL) {
52
0
                LocalPointer<UVector> lpListeners(new UVector(5, status), status);
53
0
                if (U_FAILURE(status)) {
54
0
                    return;
55
0
                }
56
0
                listeners = lpListeners.orphan();
57
0
            } else {
58
0
                for (int i = 0, e = listeners->size(); i < e; ++i) {
59
0
                    const EventListener* el = (const EventListener*)(listeners->elementAt(i));
60
0
                    if (l == el) {
61
0
                        return;
62
0
                    }
63
0
                }
64
0
            }
65
66
0
            listeners->addElement((void*)l, status); // cast away const
67
0
        }
68
#ifdef NOTIFIER_DEBUG
69
        else {
70
            fprintf(stderr, "Listener invalid for this notifier.");
71
            exit(1);
72
        }
73
#endif
74
0
    }
75
0
}
76
77
void 
78
ICUNotifier::removeListener(const EventListener *l, UErrorCode& status) 
79
0
{
80
0
    if (U_SUCCESS(status)) {
81
0
        if (l == NULL) {
82
0
            status = U_ILLEGAL_ARGUMENT_ERROR;
83
0
            return;
84
0
        }
85
86
0
        {
87
0
            Mutex lmx(&notifyLock);
88
0
            if (listeners != NULL) {
89
                // identity equality check
90
0
                for (int i = 0, e = listeners->size(); i < e; ++i) {
91
0
                    const EventListener* el = (const EventListener*)listeners->elementAt(i);
92
0
                    if (l == el) {
93
0
                        listeners->removeElementAt(i);
94
0
                        if (listeners->size() == 0) {
95
0
                            delete listeners;
96
0
                            listeners = NULL;
97
0
                        }
98
0
                        return;
99
0
                    }
100
0
                }
101
0
            }
102
0
        }
103
0
    }
104
0
}
105
106
void 
107
ICUNotifier::notifyChanged(void) 
108
0
{
109
0
    Mutex lmx(&notifyLock);
110
0
    if (listeners != NULL) {
111
0
        for (int i = 0, e = listeners->size(); i < e; ++i) {
112
0
            EventListener* el = (EventListener*)listeners->elementAt(i);
113
0
            notifyListener(*el);
114
0
        }
115
0
    }
116
0
}
117
118
U_NAMESPACE_END
119
120
/* UCONFIG_NO_SERVICE */
121
#endif
122