NotificationCenter.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. //
  2. // NotificationCenter.h
  3. //
  4. // Library: Foundation
  5. // Package: Notifications
  6. // Module: NotificationCenter
  7. //
  8. // Definition of the NotificationCenter class.
  9. //
  10. // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH.
  11. // and Contributors.
  12. //
  13. // SPDX-License-Identifier: BSL-1.0
  14. //
  15. #ifndef Foundation_NotificationCenter_INCLUDED
  16. #define Foundation_NotificationCenter_INCLUDED
  17. #include "Poco/Foundation.h"
  18. #include "Poco/Notification.h"
  19. #include "Poco/Mutex.h"
  20. #include "Poco/SharedPtr.h"
  21. #include <vector>
  22. #include <cstddef>
  23. namespace Poco {
  24. class AbstractObserver;
  25. class Foundation_API NotificationCenter
  26. /// A NotificationCenter is essentially a notification dispatcher.
  27. /// It notifies all observers of notifications meeting specific criteria.
  28. /// This information is encapsulated in Notification objects.
  29. /// Client objects register themselves with the notification center as observers of
  30. /// specific notifications posted by other objects. When an event occurs, an object
  31. /// posts an appropriate notification to the notification center. The notification
  32. /// center invokes the registered method on each matching observer, passing the notification
  33. /// as argument.
  34. ///
  35. /// The order in which observers receive notifications is undefined.
  36. /// It is possible for the posting object and the observing object to be the same.
  37. /// The NotificationCenter delivers notifications to observers synchronously.
  38. /// In other words the postNotification() method does not return until all observers have
  39. /// received and processed the notification.
  40. /// If an observer throws an exception while handling a notification, the NotificationCenter
  41. /// stops dispatching the notification and postNotification() rethrows the exception.
  42. ///
  43. /// In a multithreaded scenario, notifications are always delivered in the thread in which the
  44. /// notification was posted, which may not be the same thread in which an observer registered itself.
  45. ///
  46. /// The NotificationCenter class is basically a C++ implementation of the NSNotificationCenter class
  47. /// found in Apple's Cocoa (or OpenStep).
  48. ///
  49. /// While handling a notification, an observer can unregister itself from the notification center,
  50. /// or it can register or unregister other observers. Observers added during a dispatch cycle
  51. /// will not receive the current notification.
  52. ///
  53. /// The method receiving the notification must be implemented as
  54. /// void handleNotification(MyNotification* pNf);
  55. /// The handler method gets co-ownership of the Notification object
  56. /// and must release it when done. This is best done with an AutoPtr:
  57. /// void MyClass::handleNotification(MyNotification* pNf)
  58. /// {
  59. /// AutoPtr<MyNotification> nf(pNf);
  60. /// ...
  61. /// }
  62. ///
  63. /// Alternatively, the NObserver class template can be used to register a callback
  64. /// method. In this case, the callback method receives the Notification in an
  65. /// AutoPtr and thus does not have to deal with object ownership issues:
  66. /// void MyClass::handleNotification(const AutoPtr<MyNotification>& pNf)
  67. /// {
  68. /// ...
  69. /// }
  70. {
  71. public:
  72. NotificationCenter();
  73. /// Creates the NotificationCenter.
  74. ~NotificationCenter();
  75. /// Destroys the NotificationCenter.
  76. void addObserver(const AbstractObserver& observer);
  77. /// Registers an observer with the NotificationCenter.
  78. /// Usage:
  79. /// Observer<MyClass, MyNotification> obs(*this, &MyClass::handleNotification);
  80. /// notificationCenter.addObserver(obs);
  81. ///
  82. /// Alternatively, the NObserver template class can be used instead of Observer.
  83. void removeObserver(const AbstractObserver& observer);
  84. /// Unregisters an observer with the NotificationCenter.
  85. bool hasObserver(const AbstractObserver& observer) const;
  86. /// Returns true if the observer is registered with this NotificationCenter.
  87. void postNotification(Notification::Ptr pNotification);
  88. /// Posts a notification to the NotificationCenter.
  89. /// The NotificationCenter then delivers the notification
  90. /// to all interested observers.
  91. /// If an observer throws an exception, dispatching terminates
  92. /// and the exception is rethrown to the caller.
  93. /// Ownership of the notification object is claimed and the
  94. /// notification is released before returning. Therefore,
  95. /// a call like
  96. /// notificationCenter.postNotification(new MyNotification);
  97. /// does not result in a memory leak.
  98. bool hasObservers() const;
  99. /// Returns true iff there is at least one registered observer.
  100. ///
  101. /// Can be used to improve performance if an expensive notification
  102. /// shall only be created and posted if there are any observers.
  103. std::size_t countObservers() const;
  104. /// Returns the number of registered observers.
  105. static NotificationCenter& defaultCenter();
  106. /// Returns a reference to the default
  107. /// NotificationCenter.
  108. private:
  109. typedef SharedPtr<AbstractObserver> AbstractObserverPtr;
  110. typedef std::vector<AbstractObserverPtr> ObserverList;
  111. ObserverList _observers;
  112. mutable Mutex _mutex;
  113. };
  114. } // namespace Poco
  115. #endif // Foundation_NotificationCenter_INCLUDED