2002-11-06 22:14:49 +00:00
|
|
|
/**
|
|
|
|
*******************************************************************************
|
2012-10-05 21:22:02 +00:00
|
|
|
* Copyright (C) 2001-2012, International Business Machines Corporation and *
|
2002-11-06 22:14:49 +00:00
|
|
|
* others. All Rights Reserved. *
|
|
|
|
*******************************************************************************
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "unicode/utypes.h"
|
|
|
|
|
|
|
|
#if !UCONFIG_NO_SERVICE
|
|
|
|
|
2004-12-21 06:55:20 +00:00
|
|
|
#include "servnotf.h"
|
2004-12-07 23:43:48 +00:00
|
|
|
#ifdef NOTIFIER_DEBUG
|
2002-11-06 22:14:49 +00:00
|
|
|
#include <stdio.h>
|
2003-08-27 01:01:42 +00:00
|
|
|
#endif
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
EventListener::~EventListener() {}
|
|
|
|
UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener)
|
|
|
|
|
2012-10-05 21:22:02 +00:00
|
|
|
static UMutex notifyLock = U_MUTEX_INITIALIZER;
|
2011-03-29 00:32:11 +00:00
|
|
|
|
2003-08-27 01:01:42 +00:00
|
|
|
ICUNotifier::ICUNotifier(void)
|
2011-03-29 00:32:11 +00:00
|
|
|
: listeners(NULL)
|
2003-08-27 01:01:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ICUNotifier::~ICUNotifier(void) {
|
|
|
|
{
|
|
|
|
Mutex lmx(¬ifyLock);
|
|
|
|
delete listeners;
|
|
|
|
listeners = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
ICUNotifier::addListener(const EventListener* l, UErrorCode& status)
|
|
|
|
{
|
2006-03-30 09:12:35 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (l == NULL) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
2002-11-06 22:14:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2006-03-30 09:12:35 +00:00
|
|
|
if (acceptsListener(*l)) {
|
|
|
|
Mutex lmx(¬ifyLock);
|
|
|
|
if (listeners == NULL) {
|
|
|
|
listeners = new UVector(5, status);
|
|
|
|
} else {
|
|
|
|
for (int i = 0, e = listeners->size(); i < e; ++i) {
|
|
|
|
const EventListener* el = (const EventListener*)(listeners->elementAt(i));
|
|
|
|
if (l == el) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
listeners->addElement((void*)l, status); // cast away const
|
|
|
|
}
|
2004-12-07 23:43:48 +00:00
|
|
|
#ifdef NOTIFIER_DEBUG
|
2006-03-30 09:12:35 +00:00
|
|
|
else {
|
|
|
|
fprintf(stderr, "Listener invalid for this notifier.");
|
|
|
|
exit(1);
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUNotifier::removeListener(const EventListener *l, UErrorCode& status)
|
|
|
|
{
|
2006-03-30 09:12:35 +00:00
|
|
|
if (U_SUCCESS(status)) {
|
|
|
|
if (l == NULL) {
|
|
|
|
status = U_ILLEGAL_ARGUMENT_ERROR;
|
|
|
|
return;
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
|
2006-03-30 09:12:35 +00:00
|
|
|
{
|
|
|
|
Mutex lmx(¬ifyLock);
|
|
|
|
if (listeners != NULL) {
|
|
|
|
// 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;
|
|
|
|
listeners = NULL;
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ICUNotifier::notifyChanged(void)
|
|
|
|
{
|
|
|
|
if (listeners != NULL) {
|
2006-03-30 09:12:35 +00:00
|
|
|
Mutex lmx(¬ifyLock);
|
|
|
|
if (listeners != NULL) {
|
|
|
|
for (int i = 0, e = listeners->size(); i < e; ++i) {
|
|
|
|
EventListener* el = (EventListener*)listeners->elementAt(i);
|
|
|
|
notifyListener(*el);
|
|
|
|
}
|
|
|
|
}
|
2002-11-06 22:14:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-12-08 19:30:01 +00:00
|
|
|
U_NAMESPACE_END
|
2002-11-06 22:14:49 +00:00
|
|
|
|
|
|
|
/* UCONFIG_NO_SERVICE */
|
|
|
|
#endif
|
|
|
|
|