sl@0: /** sl@0: ******************************************************************************* sl@0: * Copyright (C) 2001-2004, International Business Machines Corporation and * sl@0: * others. All Rights Reserved. * sl@0: ******************************************************************************* sl@0: */ sl@0: #ifndef ICUNOTIF_H sl@0: #define ICUNOTIF_H sl@0: sl@0: #include "unicode/utypes.h" sl@0: sl@0: #if UCONFIG_NO_SERVICE sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: /* sl@0: * Allow the declaration of APIs with pointers to BreakIterator sl@0: * even when break iteration is removed from the build. sl@0: */ sl@0: class ICUNotifier; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: #else sl@0: sl@0: #include "unicode/uobject.h" sl@0: #include "unicode/unistr.h" sl@0: sl@0: #include "mutex.h" sl@0: #include "uvector.h" sl@0: sl@0: U_NAMESPACE_BEGIN sl@0: sl@0: class U_COMMON_API EventListener : public UObject { sl@0: public: sl@0: virtual ~EventListener(); sl@0: sl@0: public: sl@0: static UClassID U_EXPORT2 getStaticClassID(); sl@0: sl@0: virtual UClassID getDynamicClassID() const; sl@0: sl@0: public: sl@0: #ifdef SERVICE_DEBUG sl@0: virtual UnicodeString& debug(UnicodeString& result) const { sl@0: return debugClass(result); sl@0: } sl@0: sl@0: virtual UnicodeString& debugClass(UnicodeString& result) const { sl@0: return result.append("Key"); sl@0: } sl@0: #endif sl@0: }; sl@0: sl@0: /** sl@0: *
Abstract implementation of a notification facility. Clients add sl@0: * EventListeners with addListener and remove them with removeListener. sl@0: * Notifiers call notifyChanged when they wish to notify listeners. sl@0: * This queues the listener list on the notification thread, which sl@0: * eventually dequeues the list and calls notifyListener on each sl@0: * listener in the list.
sl@0: * sl@0: *Subclasses override acceptsListener and notifyListener sl@0: * to add type-safe notification. AcceptsListener should return sl@0: * true if the listener is of the appropriate type; ICUNotifier sl@0: * itself will ensure the listener is non-null and that the sl@0: * identical listener is not already registered with the Notifier. sl@0: * NotifyListener should cast the listener to the appropriate sl@0: * type and call the appropriate method on the listener. sl@0: */ sl@0: sl@0: class U_COMMON_API ICUNotifier : public UMemory { sl@0: private: UMTX notifyLock; sl@0: private: UVector* listeners; sl@0: sl@0: public: sl@0: ICUNotifier(void); sl@0: sl@0: virtual ~ICUNotifier(void); sl@0: sl@0: /** sl@0: * Add a listener to be notified when notifyChanged is called. sl@0: * The listener must not be null. AcceptsListener must return sl@0: * true for the listener. Attempts to concurrently sl@0: * register the identical listener more than once will be sl@0: * silently ignored. sl@0: */ sl@0: virtual void addListener(const EventListener* l, UErrorCode& status); sl@0: sl@0: /** sl@0: * Stop notifying this listener. The listener must sl@0: * not be null. Attemps to remove a listener that is sl@0: * not registered will be silently ignored. sl@0: */ sl@0: virtual void removeListener(const EventListener* l, UErrorCode& status); sl@0: sl@0: /** sl@0: * ICU doesn't spawn its own threads. All listeners are notified in sl@0: * the thread of the caller. Misbehaved listeners can therefore sl@0: * indefinitely block the calling thread. Callers should beware of sl@0: * deadlock situations. sl@0: */ sl@0: virtual void notifyChanged(void); sl@0: sl@0: protected: sl@0: /** sl@0: * Subclasses implement this to return TRUE if the listener is sl@0: * of the appropriate type. sl@0: */ sl@0: virtual UBool acceptsListener(const EventListener& l) const = 0; sl@0: sl@0: /** sl@0: * Subclasses implement this to notify the listener. sl@0: */ sl@0: virtual void notifyListener(EventListener& l) const = 0; sl@0: }; sl@0: sl@0: U_NAMESPACE_END sl@0: sl@0: /* UCONFIG_NO_SERVICE */ sl@0: #endif sl@0: sl@0: /* ICUNOTIF_H */ sl@0: #endif