Expire.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. //
  2. // Expire.h
  3. //
  4. // $Id: //poco/1.4/Foundation/include/Poco/Expire.h#3 $
  5. //
  6. // Library: Foundation
  7. // Package: Events
  8. // Module: Expire
  9. //
  10. // Implementation of the Expire template.
  11. //
  12. // Copyright (c) 2006-2011, Applied Informatics Software Engineering GmbH.
  13. // and Contributors.
  14. //
  15. // SPDX-License-Identifier: BSL-1.0
  16. //
  17. #ifndef Foundation_Expire_INCLUDED
  18. #define Foundation_Expire_INCLUDED
  19. #include "Poco/Foundation.h"
  20. #include "Poco/AbstractDelegate.h"
  21. #include "Poco/Timestamp.h"
  22. namespace Poco {
  23. template <class TArgs>
  24. class Expire: public AbstractDelegate<TArgs>
  25. /// Decorator for AbstractDelegate adding automatic
  26. /// expiration of registrations to AbstractDelegate's.
  27. {
  28. public:
  29. Expire(const AbstractDelegate<TArgs>& p, Timestamp::TimeDiff expireMillisecs):
  30. _pDelegate(p.clone()),
  31. _expire(expireMillisecs*1000)
  32. {
  33. }
  34. Expire(const Expire& expire):
  35. AbstractDelegate<TArgs>(expire),
  36. _pDelegate(expire._pDelegate->clone()),
  37. _expire(expire._expire),
  38. _creationTime(expire._creationTime)
  39. {
  40. }
  41. ~Expire()
  42. {
  43. delete _pDelegate;
  44. }
  45. Expire& operator = (const Expire& expire)
  46. {
  47. if (&expire != this)
  48. {
  49. delete this->_pDelegate;
  50. this->_pDelegate = expire._pDelegate->clone();
  51. this->_expire = expire._expire;
  52. this->_creationTime = expire._creationTime;
  53. this->_pTarget = expire._pTarget;
  54. }
  55. return *this;
  56. }
  57. bool notify(const void* sender, TArgs& arguments)
  58. {
  59. if (!expired())
  60. return this->_pDelegate->notify(sender, arguments);
  61. else
  62. return false;
  63. }
  64. bool equals(const AbstractDelegate<TArgs>& other) const
  65. {
  66. return other.equals(*_pDelegate);
  67. }
  68. AbstractDelegate<TArgs>* clone() const
  69. {
  70. return new Expire(*this);
  71. }
  72. void disable()
  73. {
  74. _pDelegate->disable();
  75. }
  76. const AbstractDelegate<TArgs>* unwrap() const
  77. {
  78. return this->_pDelegate;
  79. }
  80. protected:
  81. bool expired() const
  82. {
  83. return _creationTime.isElapsed(_expire);
  84. }
  85. AbstractDelegate<TArgs>* _pDelegate;
  86. Timestamp::TimeDiff _expire;
  87. Timestamp _creationTime;
  88. private:
  89. Expire();
  90. };
  91. template <>
  92. class Expire<void>: public AbstractDelegate<void>
  93. /// Decorator for AbstractDelegate adding automatic
  94. /// expiration of registrations to AbstractDelegate's.
  95. {
  96. public:
  97. Expire(const AbstractDelegate<void>& p, Timestamp::TimeDiff expireMillisecs):
  98. _pDelegate(p.clone()),
  99. _expire(expireMillisecs*1000)
  100. {
  101. }
  102. Expire(const Expire& expire):
  103. AbstractDelegate<void>(expire),
  104. _pDelegate(expire._pDelegate->clone()),
  105. _expire(expire._expire),
  106. _creationTime(expire._creationTime)
  107. {
  108. }
  109. ~Expire()
  110. {
  111. delete _pDelegate;
  112. }
  113. Expire& operator = (const Expire& expire)
  114. {
  115. if (&expire != this)
  116. {
  117. delete this->_pDelegate;
  118. this->_pDelegate = expire._pDelegate->clone();
  119. this->_expire = expire._expire;
  120. this->_creationTime = expire._creationTime;
  121. //this->_pTarget = expire._pTarget;
  122. }
  123. return *this;
  124. }
  125. bool notify(const void* sender)
  126. {
  127. if (!expired())
  128. return this->_pDelegate->notify(sender);
  129. else
  130. return false;
  131. }
  132. bool equals(const AbstractDelegate<void>& other) const
  133. {
  134. return other.equals(*_pDelegate);
  135. }
  136. AbstractDelegate<void>* clone() const
  137. {
  138. return new Expire(*this);
  139. }
  140. void disable()
  141. {
  142. _pDelegate->disable();
  143. }
  144. const AbstractDelegate<void>* unwrap() const
  145. {
  146. return this->_pDelegate;
  147. }
  148. protected:
  149. bool expired() const
  150. {
  151. return _creationTime.isElapsed(_expire);
  152. }
  153. AbstractDelegate<void>* _pDelegate;
  154. Timestamp::TimeDiff _expire;
  155. Timestamp _creationTime;
  156. private:
  157. Expire();
  158. };
  159. } // namespace Poco
  160. #endif // Foundation_Expire_INCLUDED