1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/icu/source/common/servnotf.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,118 @@ 1.4 +/** 1.5 + ******************************************************************************* 1.6 + * Copyright (C) 2001-2012, International Business Machines Corporation and * 1.7 + * others. All Rights Reserved. * 1.8 + ******************************************************************************* 1.9 + */ 1.10 + 1.11 +#include "unicode/utypes.h" 1.12 + 1.13 +#if !UCONFIG_NO_SERVICE 1.14 + 1.15 +#include "servnotf.h" 1.16 +#ifdef NOTIFIER_DEBUG 1.17 +#include <stdio.h> 1.18 +#endif 1.19 + 1.20 +U_NAMESPACE_BEGIN 1.21 + 1.22 +EventListener::~EventListener() {} 1.23 +UOBJECT_DEFINE_RTTI_IMPLEMENTATION(EventListener) 1.24 + 1.25 +static UMutex notifyLock = U_MUTEX_INITIALIZER; 1.26 + 1.27 +ICUNotifier::ICUNotifier(void) 1.28 +: listeners(NULL) 1.29 +{ 1.30 +} 1.31 + 1.32 +ICUNotifier::~ICUNotifier(void) { 1.33 + { 1.34 + Mutex lmx(¬ifyLock); 1.35 + delete listeners; 1.36 + listeners = NULL; 1.37 + } 1.38 +} 1.39 + 1.40 + 1.41 +void 1.42 +ICUNotifier::addListener(const EventListener* l, UErrorCode& status) 1.43 +{ 1.44 + if (U_SUCCESS(status)) { 1.45 + if (l == NULL) { 1.46 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.47 + return; 1.48 + } 1.49 + 1.50 + if (acceptsListener(*l)) { 1.51 + Mutex lmx(¬ifyLock); 1.52 + if (listeners == NULL) { 1.53 + listeners = new UVector(5, status); 1.54 + } else { 1.55 + for (int i = 0, e = listeners->size(); i < e; ++i) { 1.56 + const EventListener* el = (const EventListener*)(listeners->elementAt(i)); 1.57 + if (l == el) { 1.58 + return; 1.59 + } 1.60 + } 1.61 + } 1.62 + 1.63 + listeners->addElement((void*)l, status); // cast away const 1.64 + } 1.65 +#ifdef NOTIFIER_DEBUG 1.66 + else { 1.67 + fprintf(stderr, "Listener invalid for this notifier."); 1.68 + exit(1); 1.69 + } 1.70 +#endif 1.71 + } 1.72 +} 1.73 + 1.74 +void 1.75 +ICUNotifier::removeListener(const EventListener *l, UErrorCode& status) 1.76 +{ 1.77 + if (U_SUCCESS(status)) { 1.78 + if (l == NULL) { 1.79 + status = U_ILLEGAL_ARGUMENT_ERROR; 1.80 + return; 1.81 + } 1.82 + 1.83 + { 1.84 + Mutex lmx(¬ifyLock); 1.85 + if (listeners != NULL) { 1.86 + // identity equality check 1.87 + for (int i = 0, e = listeners->size(); i < e; ++i) { 1.88 + const EventListener* el = (const EventListener*)listeners->elementAt(i); 1.89 + if (l == el) { 1.90 + listeners->removeElementAt(i); 1.91 + if (listeners->size() == 0) { 1.92 + delete listeners; 1.93 + listeners = NULL; 1.94 + } 1.95 + return; 1.96 + } 1.97 + } 1.98 + } 1.99 + } 1.100 + } 1.101 +} 1.102 + 1.103 +void 1.104 +ICUNotifier::notifyChanged(void) 1.105 +{ 1.106 + if (listeners != NULL) { 1.107 + Mutex lmx(¬ifyLock); 1.108 + if (listeners != NULL) { 1.109 + for (int i = 0, e = listeners->size(); i < e; ++i) { 1.110 + EventListener* el = (EventListener*)listeners->elementAt(i); 1.111 + notifyListener(*el); 1.112 + } 1.113 + } 1.114 + } 1.115 +} 1.116 + 1.117 +U_NAMESPACE_END 1.118 + 1.119 +/* UCONFIG_NO_SERVICE */ 1.120 +#endif 1.121 +