2020-08-11 17:10:23 +08:00
|
|
|
// © 2016 and later: Unicode, Inc. and others.
|
|
|
|
// License & terms of use: http://www.unicode.org/copyright.html
|
|
|
|
/**
|
|
|
|
*******************************************************************************
|
|
|
|
* Copyright (C) 2001-2012, International Business Machines Corporation and *
|
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_SERVICE
|
|
|
|
|
|
|
|
#include "servnotf.h"
|
|
|
|
#ifdef NOTIFIER_DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
|
|
|
EventListener::~EventListener() {}
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener)
|
|
|
|
|
|
|
|
static UMutex notifyLock;
|
|
|
|
|
2023-05-23 08:05:01 +08:00
|
|
|
ICUNotifier::ICUNotifier()
|
|
|
|
: listeners(nullptr)
|
2020-08-11 17:10:23 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2023-05-23 08:05:01 +08:00
|
|
|
ICUNotifier::~ICUNotifier() {
|
2020-08-11 17:10:23 +08:00
|
|
|
{
|
|
|
|
Mutex lmx(¬ifyLock);
|
|
|
|
delete listeners;
|
2023-05-23 08:05:01 +08:00
|
|
|
listeners = nullptr;
|
2020-08-11 17:10:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUNotifier::addListener(const EventListener* l, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
2023-05-23 08:05:01 +08:00
|
|
|
if (l == nullptr) {
|
2020-08-11 17:10:23 +08:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (acceptsListener(*l)) {
|
|
|
|
Mutex lmx(¬ifyLock);
|
2023-05-23 08:05:01 +08:00
|
|
|
if (listeners == nullptr) {
|
2022-05-17 23:14:19 +08:00
|
|
|
LocalPointer<UVector> lpListeners(new UVector(5, status), status);
|
|
|
|
if (U_FAILURE(status)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
listeners = lpListeners.orphan();
|
2020-08-11 17:10:23 +08:00
|
|
|
} else {
|
|
|
|
for (int i = 0, e = listeners->size(); i < e; ++i) {
|
|
|
|
const EventListener* el = (const EventListener*)(listeners->elementAt(i));
|
|
|
|
if (l == el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-17 23:14:19 +08:00
|
|
|
listeners->addElement((void*)l, status); // cast away const
|
2020-08-11 17:10:23 +08:00
|
|
|
}
|
|
|
|
#ifdef NOTIFIER_DEBUG
|
|
|
|
else {
|
|
|
|
fprintf(stderr, "Listener invalid for this notifier.");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUNotifier::removeListener(const EventListener *l, UErrorCode& status)
|
|
|
|
{
|
|
|
|
if (U_SUCCESS(status)) {
|
2023-05-23 08:05:01 +08:00
|
|
|
if (l == nullptr) {
|
2020-08-11 17:10:23 +08:00
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Mutex lmx(¬ifyLock);
|
2023-05-23 08:05:01 +08:00
|
|
|
if (listeners != nullptr) {
|
2020-08-11 17:10:23 +08:00
|
|
|
// identity equality check
|
|
|
|
for (int i = 0, e = listeners->size(); i < e; ++i) {
|
|
|
|
const EventListener* el = (const EventListener*)listeners->elementAt(i);
|
|
|
|
if (l == el) {
|
|
|
|
listeners->removeElementAt(i);
|
|
|
|
if (listeners->size() == 0) {
|
|
|
|
delete listeners;
|
2023-05-23 08:05:01 +08:00
|
|
|
listeners = nullptr;
|
2020-08-11 17:10:23 +08:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2023-05-23 08:05:01 +08:00
|
|
|
ICUNotifier::notifyChanged()
|
2020-08-11 17:10:23 +08:00
|
|
|
{
|
2022-05-17 23:14:19 +08:00
|
|
|
Mutex lmx(¬ifyLock);
|
2023-05-23 08:05:01 +08:00
|
|
|
if (listeners != nullptr) {
|
2022-05-17 23:14:19 +08:00
|
|
|
for (int i = 0, e = listeners->size(); i < e; ++i) {
|
|
|
|
EventListener* el = (EventListener*)listeners->elementAt(i);
|
|
|
|
notifyListener(*el);
|
2020-08-11 17:10:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
U_NAMESPACE_END
|
|
|
|
|
|
|
|
/* UCONFIG_NO_SERVICE */
|
|
|
|
#endif
|
|
|
|
|