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