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