[80] | 1 | // |
---|
| 2 | // NotificationCenter.h |
---|
| 3 | // |
---|
| 4 | // $Id: //poco/1.3/Foundation/include/Poco/NotificationCenter.h#3 $ |
---|
| 5 | // |
---|
| 6 | // Library: Foundation |
---|
| 7 | // Package: Notifications |
---|
| 8 | // Module: NotificationCenter |
---|
| 9 | // |
---|
| 10 | // Definition of the NotificationCenter class. |
---|
| 11 | // |
---|
| 12 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
---|
| 13 | // and Contributors. |
---|
| 14 | // |
---|
| 15 | // Permission is hereby granted, free of charge, to any person or organization |
---|
| 16 | // obtaining a copy of the software and accompanying documentation covered by |
---|
| 17 | // this license (the "Software") to use, reproduce, display, distribute, |
---|
| 18 | // execute, and transmit the Software, and to prepare derivative works of the |
---|
| 19 | // Software, and to permit third-parties to whom the Software is furnished to |
---|
| 20 | // do so, all subject to the following: |
---|
| 21 | // |
---|
| 22 | // The copyright notices in the Software and this entire statement, including |
---|
| 23 | // the above license grant, this restriction and the following disclaimer, |
---|
| 24 | // must be included in all copies of the Software, in whole or in part, and |
---|
| 25 | // all derivative works of the Software, unless such copies or derivative |
---|
| 26 | // works are solely in the form of machine-executable object code generated by |
---|
| 27 | // a source language processor. |
---|
| 28 | // |
---|
| 29 | // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
---|
| 30 | // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
---|
| 31 | // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT |
---|
| 32 | // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE |
---|
| 33 | // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, |
---|
| 34 | // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
---|
| 35 | // DEALINGS IN THE SOFTWARE. |
---|
| 36 | // |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | #ifndef Foundation_NotificationCenter_INCLUDED |
---|
| 40 | #define Foundation_NotificationCenter_INCLUDED |
---|
| 41 | |
---|
| 42 | |
---|
| 43 | #include "Poco/Foundation.h" |
---|
| 44 | #include "Poco/Notification.h" |
---|
| 45 | #include "Poco/Mutex.h" |
---|
| 46 | #include "Poco/SharedPtr.h" |
---|
| 47 | #include <vector> |
---|
| 48 | #include <cstddef> |
---|
| 49 | |
---|
| 50 | |
---|
| 51 | namespace Poco { |
---|
| 52 | |
---|
| 53 | |
---|
| 54 | class AbstractObserver; |
---|
| 55 | |
---|
| 56 | |
---|
| 57 | class Foundation_API NotificationCenter |
---|
| 58 | /// A NotificationCenter is essentially a notification dispatcher. |
---|
| 59 | /// It notifies all observers of notifications meeting specific criteria. |
---|
| 60 | /// This information is encapsulated in Notification objects. |
---|
| 61 | /// Client objects register themselves with the notification center as observers of |
---|
| 62 | /// specific notifications posted by other objects. When an event occurs, an object |
---|
| 63 | /// posts an appropriate notification to the notification center. The notification |
---|
| 64 | /// center invokes the registered method on each matching observer, passing the notification |
---|
| 65 | /// as argument. |
---|
| 66 | /// |
---|
| 67 | /// The order in which observers receive notifications is undefined. |
---|
| 68 | /// It is possible for the posting object and the observing object to be the same. |
---|
| 69 | /// The NotificationCenter delivers notifications to observers synchronously. |
---|
| 70 | /// In other words the postNotification() method does not return until all observers have |
---|
| 71 | /// received and processed the notification. |
---|
| 72 | /// If an observer throws an exception while handling a notification, the NotificationCenter |
---|
| 73 | /// stops dispatching the notification and postNotification() rethrows the exception. |
---|
| 74 | /// |
---|
| 75 | /// In a multithreaded scenario, notifications are always delivered in the thread in which the |
---|
| 76 | /// notification was posted, which may not be the same thread in which an observer registered itself. |
---|
| 77 | /// |
---|
| 78 | /// The NotificationCenter class is basically a C++ implementation of the NSNotificationCenter class |
---|
| 79 | /// found in Apple's Cocoa (or OpenStep). |
---|
| 80 | /// |
---|
| 81 | /// While handling a notification, an observer can unregister itself from the notification center, |
---|
| 82 | /// or it can register or unregister other observers. Observers added during a dispatch cycle |
---|
| 83 | /// will not receive the current notification. |
---|
| 84 | /// |
---|
| 85 | /// The method receiving the notification must be implemented as |
---|
| 86 | /// void handleNotification(MyNotification* pNf); |
---|
| 87 | /// The handler method gets co-ownership of the Notification object |
---|
| 88 | /// and must release it when done. This is best done with an AutoPtr: |
---|
| 89 | /// void MyClass::handleNotification(MyNotification* pNf) |
---|
| 90 | /// { |
---|
| 91 | /// AutoPtr<MyNotification> nf(pNf); |
---|
| 92 | /// ... |
---|
| 93 | /// } |
---|
| 94 | /// |
---|
| 95 | /// Alternatively, the NObserver class template can be used to register a callback |
---|
| 96 | /// method. In this case, the callback method receives the Notification in an |
---|
| 97 | /// AutoPtr and thus does not have to deal with object ownership issues: |
---|
| 98 | /// void MyClass::handleNotification(const AutoPtr<MyNotification>& pNf) |
---|
| 99 | /// { |
---|
| 100 | /// ... |
---|
| 101 | /// } |
---|
| 102 | { |
---|
| 103 | public: |
---|
| 104 | NotificationCenter(); |
---|
| 105 | /// Creates the NotificationCenter. |
---|
| 106 | |
---|
| 107 | ~NotificationCenter(); |
---|
| 108 | /// Destroys the NotificationCenter. |
---|
| 109 | |
---|
| 110 | void addObserver(const AbstractObserver& observer); |
---|
| 111 | /// Registers an observer with the NotificationCenter. |
---|
| 112 | /// Usage: |
---|
| 113 | /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification); |
---|
| 114 | /// notificationCenter.addObserver(obs); |
---|
| 115 | /// |
---|
| 116 | /// Alternatively, the NObserver template class can be used instead of Observer. |
---|
| 117 | |
---|
| 118 | void removeObserver(const AbstractObserver& observer); |
---|
| 119 | /// Unregisters an observer with the NotificationCenter. |
---|
| 120 | |
---|
| 121 | void postNotification(Notification::Ptr pNotification); |
---|
| 122 | /// Posts a notification to the NotificationCenter. |
---|
| 123 | /// The NotificationCenter then delivers the notification |
---|
| 124 | /// to all interested observers. |
---|
| 125 | /// If an observer throws an exception, dispatching terminates |
---|
| 126 | /// and the exception is rethrown to the caller. |
---|
| 127 | /// Ownership of the notification object is claimed and the |
---|
| 128 | /// notification is released before returning. Therefore, |
---|
| 129 | /// a call like |
---|
| 130 | /// notificationCenter.postNotification(new MyNotification); |
---|
| 131 | /// does not result in a memory leak. |
---|
| 132 | |
---|
| 133 | bool hasObservers() const; |
---|
| 134 | /// Returns true iff there is at least one registered observer. |
---|
| 135 | /// |
---|
| 136 | /// Can be used to improve performance if an expensive notification |
---|
| 137 | /// shall only be created and posted if there are any observers. |
---|
| 138 | |
---|
| 139 | std::size_t countObservers() const; |
---|
| 140 | /// Returns the number of registered observers. |
---|
| 141 | |
---|
| 142 | static NotificationCenter& defaultCenter(); |
---|
| 143 | /// Returns a reference to the default |
---|
| 144 | /// NotificationCenter. |
---|
| 145 | |
---|
| 146 | private: |
---|
| 147 | typedef Poco::SharedPtr<AbstractObserver> AbstractObserverPtr; |
---|
| 148 | typedef std::vector<AbstractObserverPtr> ObserverList; |
---|
| 149 | |
---|
| 150 | ObserverList _observers; |
---|
| 151 | mutable Mutex _mutex; |
---|
| 152 | }; |
---|
| 153 | |
---|
| 154 | |
---|
| 155 | } // namespace Poco |
---|
| 156 | |
---|
| 157 | |
---|
| 158 | #endif // Foundation_NotificationCenter_INCLUDED |
---|