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