1*0e209d39SAndroid Build Coastguard Worker // © 2016 and later: Unicode, Inc. and others. 2*0e209d39SAndroid Build Coastguard Worker // License & terms of use: http://www.unicode.org/copyright.html 3*0e209d39SAndroid Build Coastguard Worker /** 4*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 5*0e209d39SAndroid Build Coastguard Worker * Copyright (C) 2001-2014, International Business Machines Corporation and * 6*0e209d39SAndroid Build Coastguard Worker * others. All Rights Reserved. * 7*0e209d39SAndroid Build Coastguard Worker ******************************************************************************* 8*0e209d39SAndroid Build Coastguard Worker */ 9*0e209d39SAndroid Build Coastguard Worker #ifndef ICUNOTIF_H 10*0e209d39SAndroid Build Coastguard Worker #define ICUNOTIF_H 11*0e209d39SAndroid Build Coastguard Worker 12*0e209d39SAndroid Build Coastguard Worker #include "unicode/utypes.h" 13*0e209d39SAndroid Build Coastguard Worker 14*0e209d39SAndroid Build Coastguard Worker #if UCONFIG_NO_SERVICE 15*0e209d39SAndroid Build Coastguard Worker 16*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 17*0e209d39SAndroid Build Coastguard Worker 18*0e209d39SAndroid Build Coastguard Worker /* 19*0e209d39SAndroid Build Coastguard Worker * Allow the declaration of APIs with pointers to BreakIterator 20*0e209d39SAndroid Build Coastguard Worker * even when break iteration is removed from the build. 21*0e209d39SAndroid Build Coastguard Worker */ 22*0e209d39SAndroid Build Coastguard Worker class ICUNotifier; 23*0e209d39SAndroid Build Coastguard Worker 24*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 25*0e209d39SAndroid Build Coastguard Worker 26*0e209d39SAndroid Build Coastguard Worker #else 27*0e209d39SAndroid Build Coastguard Worker 28*0e209d39SAndroid Build Coastguard Worker #include "unicode/uobject.h" 29*0e209d39SAndroid Build Coastguard Worker #include "unicode/unistr.h" 30*0e209d39SAndroid Build Coastguard Worker 31*0e209d39SAndroid Build Coastguard Worker #include "mutex.h" 32*0e209d39SAndroid Build Coastguard Worker #include "uvector.h" 33*0e209d39SAndroid Build Coastguard Worker 34*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_BEGIN 35*0e209d39SAndroid Build Coastguard Worker 36*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API EventListener : public UObject { 37*0e209d39SAndroid Build Coastguard Worker public: 38*0e209d39SAndroid Build Coastguard Worker virtual ~EventListener(); 39*0e209d39SAndroid Build Coastguard Worker 40*0e209d39SAndroid Build Coastguard Worker public: 41*0e209d39SAndroid Build Coastguard Worker static UClassID U_EXPORT2 getStaticClassID(); 42*0e209d39SAndroid Build Coastguard Worker 43*0e209d39SAndroid Build Coastguard Worker virtual UClassID getDynamicClassID() const override; 44*0e209d39SAndroid Build Coastguard Worker 45*0e209d39SAndroid Build Coastguard Worker public: 46*0e209d39SAndroid Build Coastguard Worker #ifdef SERVICE_DEBUG 47*0e209d39SAndroid Build Coastguard Worker virtual UnicodeString& debug(UnicodeString& result) const { 48*0e209d39SAndroid Build Coastguard Worker return debugClass(result); 49*0e209d39SAndroid Build Coastguard Worker } 50*0e209d39SAndroid Build Coastguard Worker 51*0e209d39SAndroid Build Coastguard Worker virtual UnicodeString& debugClass(UnicodeString& result) const { 52*0e209d39SAndroid Build Coastguard Worker return result.append((UnicodeString)"Key"); 53*0e209d39SAndroid Build Coastguard Worker } 54*0e209d39SAndroid Build Coastguard Worker #endif 55*0e209d39SAndroid Build Coastguard Worker }; 56*0e209d39SAndroid Build Coastguard Worker 57*0e209d39SAndroid Build Coastguard Worker /** 58*0e209d39SAndroid Build Coastguard Worker * <p>Abstract implementation of a notification facility. Clients add 59*0e209d39SAndroid Build Coastguard Worker * EventListeners with addListener and remove them with removeListener. 60*0e209d39SAndroid Build Coastguard Worker * Notifiers call notifyChanged when they wish to notify listeners. 61*0e209d39SAndroid Build Coastguard Worker * This queues the listener list on the notification thread, which 62*0e209d39SAndroid Build Coastguard Worker * eventually dequeues the list and calls notifyListener on each 63*0e209d39SAndroid Build Coastguard Worker * listener in the list.</p> 64*0e209d39SAndroid Build Coastguard Worker * 65*0e209d39SAndroid Build Coastguard Worker * <p>Subclasses override acceptsListener and notifyListener 66*0e209d39SAndroid Build Coastguard Worker * to add type-safe notification. AcceptsListener should return 67*0e209d39SAndroid Build Coastguard Worker * true if the listener is of the appropriate type; ICUNotifier 68*0e209d39SAndroid Build Coastguard Worker * itself will ensure the listener is non-null and that the 69*0e209d39SAndroid Build Coastguard Worker * identical listener is not already registered with the Notifier. 70*0e209d39SAndroid Build Coastguard Worker * NotifyListener should cast the listener to the appropriate 71*0e209d39SAndroid Build Coastguard Worker * type and call the appropriate method on the listener. 72*0e209d39SAndroid Build Coastguard Worker */ 73*0e209d39SAndroid Build Coastguard Worker 74*0e209d39SAndroid Build Coastguard Worker class U_COMMON_API ICUNotifier : public UMemory { 75*0e209d39SAndroid Build Coastguard Worker private: UVector* listeners; 76*0e209d39SAndroid Build Coastguard Worker 77*0e209d39SAndroid Build Coastguard Worker public: 78*0e209d39SAndroid Build Coastguard Worker ICUNotifier(); 79*0e209d39SAndroid Build Coastguard Worker 80*0e209d39SAndroid Build Coastguard Worker virtual ~ICUNotifier(); 81*0e209d39SAndroid Build Coastguard Worker 82*0e209d39SAndroid Build Coastguard Worker /** 83*0e209d39SAndroid Build Coastguard Worker * Add a listener to be notified when notifyChanged is called. 84*0e209d39SAndroid Build Coastguard Worker * The listener must not be null. AcceptsListener must return 85*0e209d39SAndroid Build Coastguard Worker * true for the listener. Attempts to concurrently 86*0e209d39SAndroid Build Coastguard Worker * register the identical listener more than once will be 87*0e209d39SAndroid Build Coastguard Worker * silently ignored. 88*0e209d39SAndroid Build Coastguard Worker */ 89*0e209d39SAndroid Build Coastguard Worker virtual void addListener(const EventListener* l, UErrorCode& status); 90*0e209d39SAndroid Build Coastguard Worker 91*0e209d39SAndroid Build Coastguard Worker /** 92*0e209d39SAndroid Build Coastguard Worker * Stop notifying this listener. The listener must 93*0e209d39SAndroid Build Coastguard Worker * not be null. Attempts to remove a listener that is 94*0e209d39SAndroid Build Coastguard Worker * not registered will be silently ignored. 95*0e209d39SAndroid Build Coastguard Worker */ 96*0e209d39SAndroid Build Coastguard Worker virtual void removeListener(const EventListener* l, UErrorCode& status); 97*0e209d39SAndroid Build Coastguard Worker 98*0e209d39SAndroid Build Coastguard Worker /** 99*0e209d39SAndroid Build Coastguard Worker * ICU doesn't spawn its own threads. All listeners are notified in 100*0e209d39SAndroid Build Coastguard Worker * the thread of the caller. Misbehaved listeners can therefore 101*0e209d39SAndroid Build Coastguard Worker * indefinitely block the calling thread. Callers should beware of 102*0e209d39SAndroid Build Coastguard Worker * deadlock situations. 103*0e209d39SAndroid Build Coastguard Worker */ 104*0e209d39SAndroid Build Coastguard Worker virtual void notifyChanged(); 105*0e209d39SAndroid Build Coastguard Worker 106*0e209d39SAndroid Build Coastguard Worker protected: 107*0e209d39SAndroid Build Coastguard Worker /** 108*0e209d39SAndroid Build Coastguard Worker * Subclasses implement this to return true if the listener is 109*0e209d39SAndroid Build Coastguard Worker * of the appropriate type. 110*0e209d39SAndroid Build Coastguard Worker */ 111*0e209d39SAndroid Build Coastguard Worker virtual UBool acceptsListener(const EventListener& l) const = 0; 112*0e209d39SAndroid Build Coastguard Worker 113*0e209d39SAndroid Build Coastguard Worker /** 114*0e209d39SAndroid Build Coastguard Worker * Subclasses implement this to notify the listener. 115*0e209d39SAndroid Build Coastguard Worker */ 116*0e209d39SAndroid Build Coastguard Worker virtual void notifyListener(EventListener& l) const = 0; 117*0e209d39SAndroid Build Coastguard Worker }; 118*0e209d39SAndroid Build Coastguard Worker 119*0e209d39SAndroid Build Coastguard Worker U_NAMESPACE_END 120*0e209d39SAndroid Build Coastguard Worker 121*0e209d39SAndroid Build Coastguard Worker /* UCONFIG_NO_SERVICE */ 122*0e209d39SAndroid Build Coastguard Worker #endif 123*0e209d39SAndroid Build Coastguard Worker 124*0e209d39SAndroid Build Coastguard Worker /* ICUNOTIF_H */ 125*0e209d39SAndroid Build Coastguard Worker #endif 126