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