NotificationCenter.h 5.1 KB

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