Observer.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. //
  2. // Observer.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Observer.h#2 $
  5. //
  6. // Library: Foundation
  7. // Package: Notifications
  8. // Module: NotificationCenter
  9. //
  10. // Definition of the Observer class template.
  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_Observer_INCLUDED
  18. #define Foundation_Observer_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/AbstractObserver.h"
  21. #include "Poco/Mutex.h"
  22. namespace Poco {
  23. template <class C, class N>
  24. class Observer: public AbstractObserver
  25. /// This template class implements an adapter that sits between
  26. /// a NotificationCenter and an object receiving notifications
  27. /// from it. It is quite similar in concept to the
  28. /// RunnableAdapter, but provides some NotificationCenter
  29. /// specific additional methods.
  30. /// See the NotificationCenter class for information on how
  31. /// to use this template class.
  32. ///
  33. /// Instead of the Observer class template, you might want to
  34. /// use the NObserver class template, which uses an AutoPtr to
  35. /// pass the Notification to the callback function, thus freeing
  36. /// you from memory management issues.
  37. {
  38. public:
  39. typedef void (C::*Callback)(N*);
  40. Observer(C& object, Callback method):
  41. _pObject(&object),
  42. _method(method)
  43. {
  44. }
  45. Observer(const Observer& observer):
  46. AbstractObserver(observer),
  47. _pObject(observer._pObject),
  48. _method(observer._method)
  49. {
  50. }
  51. ~Observer()
  52. {
  53. }
  54. Observer& operator = (const Observer& observer)
  55. {
  56. if (&observer != this)
  57. {
  58. _pObject = observer._pObject;
  59. _method = observer._method;
  60. }
  61. return *this;
  62. }
  63. void notify(Notification* pNf) const
  64. {
  65. Poco::Mutex::ScopedLock lock(_mutex);
  66. if (_pObject)
  67. {
  68. N* pCastNf = dynamic_cast<N*>(pNf);
  69. if (pCastNf)
  70. {
  71. pCastNf->duplicate();
  72. (_pObject->*_method)(pCastNf);
  73. }
  74. }
  75. }
  76. bool equals(const AbstractObserver& abstractObserver) const
  77. {
  78. const Observer* pObs = dynamic_cast<const Observer*>(&abstractObserver);
  79. return pObs && pObs->_pObject == _pObject && pObs->_method == _method;
  80. }
  81. bool accepts(Notification* pNf) const
  82. {
  83. return dynamic_cast<N*>(pNf) != 0;
  84. }
  85. AbstractObserver* clone() const
  86. {
  87. return new Observer(*this);
  88. }
  89. void disable()
  90. {
  91. Poco::Mutex::ScopedLock lock(_mutex);
  92. _pObject = 0;
  93. }
  94. private:
  95. Observer();
  96. C* _pObject;
  97. Callback _method;
  98. mutable Poco::Mutex _mutex;
  99. };
  100. } // namespace Poco
  101. #endif // Foundation_Observer_INCLUDED