michael@0: /** michael@0: ******************************************************************************* michael@0: * Copyright (C) 2001-2011, International Business Machines Corporation and * michael@0: * others. All Rights Reserved. * michael@0: ******************************************************************************* michael@0: */ michael@0: #ifndef ICUNOTIF_H michael@0: #define ICUNOTIF_H michael@0: michael@0: #include "unicode/utypes.h" michael@0: michael@0: #if UCONFIG_NO_SERVICE michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: /* michael@0: * Allow the declaration of APIs with pointers to BreakIterator michael@0: * even when break iteration is removed from the build. michael@0: */ michael@0: class ICUNotifier; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: #else michael@0: michael@0: #include "unicode/uobject.h" michael@0: #include "unicode/unistr.h" michael@0: michael@0: #include "mutex.h" michael@0: #include "uvector.h" michael@0: michael@0: U_NAMESPACE_BEGIN michael@0: michael@0: class U_COMMON_API EventListener : public UObject { michael@0: public: michael@0: virtual ~EventListener(); michael@0: michael@0: public: michael@0: static UClassID U_EXPORT2 getStaticClassID(); michael@0: michael@0: virtual UClassID getDynamicClassID() const; michael@0: michael@0: public: michael@0: #ifdef SERVICE_DEBUG michael@0: virtual UnicodeString& debug(UnicodeString& result) const { michael@0: return debugClass(result); michael@0: } michael@0: michael@0: virtual UnicodeString& debugClass(UnicodeString& result) const { michael@0: return result.append("Key"); michael@0: } michael@0: #endif michael@0: }; michael@0: michael@0: /** michael@0: *

Abstract implementation of a notification facility. Clients add michael@0: * EventListeners with addListener and remove them with removeListener. michael@0: * Notifiers call notifyChanged when they wish to notify listeners. michael@0: * This queues the listener list on the notification thread, which michael@0: * eventually dequeues the list and calls notifyListener on each michael@0: * listener in the list.

michael@0: * michael@0: *

Subclasses override acceptsListener and notifyListener michael@0: * to add type-safe notification. AcceptsListener should return michael@0: * true if the listener is of the appropriate type; ICUNotifier michael@0: * itself will ensure the listener is non-null and that the michael@0: * identical listener is not already registered with the Notifier. michael@0: * NotifyListener should cast the listener to the appropriate michael@0: * type and call the appropriate method on the listener. michael@0: */ michael@0: michael@0: class U_COMMON_API ICUNotifier : public UMemory { michael@0: private: UVector* listeners; michael@0: michael@0: public: michael@0: ICUNotifier(void); michael@0: michael@0: virtual ~ICUNotifier(void); michael@0: michael@0: /** michael@0: * Add a listener to be notified when notifyChanged is called. michael@0: * The listener must not be null. AcceptsListener must return michael@0: * true for the listener. Attempts to concurrently michael@0: * register the identical listener more than once will be michael@0: * silently ignored. michael@0: */ michael@0: virtual void addListener(const EventListener* l, UErrorCode& status); michael@0: michael@0: /** michael@0: * Stop notifying this listener. The listener must michael@0: * not be null. Attemps to remove a listener that is michael@0: * not registered will be silently ignored. michael@0: */ michael@0: virtual void removeListener(const EventListener* l, UErrorCode& status); michael@0: michael@0: /** michael@0: * ICU doesn't spawn its own threads. All listeners are notified in michael@0: * the thread of the caller. Misbehaved listeners can therefore michael@0: * indefinitely block the calling thread. Callers should beware of michael@0: * deadlock situations. michael@0: */ michael@0: virtual void notifyChanged(void); michael@0: michael@0: protected: michael@0: /** michael@0: * Subclasses implement this to return TRUE if the listener is michael@0: * of the appropriate type. michael@0: */ michael@0: virtual UBool acceptsListener(const EventListener& l) const = 0; michael@0: michael@0: /** michael@0: * Subclasses implement this to notify the listener. michael@0: */ michael@0: virtual void notifyListener(EventListener& l) const = 0; michael@0: }; michael@0: michael@0: U_NAMESPACE_END michael@0: michael@0: /* UCONFIG_NO_SERVICE */ michael@0: #endif michael@0: michael@0: /* ICUNOTIF_H */ michael@0: #endif