intl/icu/source/common/servnotf.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial