root / Community_Core_Vision / libs / poco / include / Poco / Expire.h @ 11

View | Annotate | Download (3.2 KB)

1
//
2
// Expire.h
3
//
4
// $Id: //poco/1.3/Foundation/include/Poco/Expire.h#2 $
5
//
6
// Library: Foundation
7
// Package: Events
8
// Module:  Expire
9
//
10
// Implementation of the Expire template.
11
//
12
// Copyright (c) 2006, Applied Informatics Software Engineering GmbH.
13
// and Contributors.
14
//
15
// Permission is hereby granted, free of charge, to any person or organization
16
// obtaining a copy of the software and accompanying documentation covered by
17
// this license (the "Software") to use, reproduce, display, distribute,
18
// execute, and transmit the Software, and to prepare derivative works of the
19
// Software, and to permit third-parties to whom the Software is furnished to
20
// do so, all subject to the following:
21
// 
22
// The copyright notices in the Software and this entire statement, including
23
// the above license grant, this restriction and the following disclaimer,
24
// must be included in all copies of the Software, in whole or in part, and
25
// all derivative works of the Software, unless such copies or derivative
26
// works are solely in the form of machine-executable object code generated by
27
// a source language processor.
28
// 
29
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
31
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
32
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
33
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
34
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
35
// DEALINGS IN THE SOFTWARE.
36
//
37
38
39
#ifndef  Foundation_Expire_INCLUDED
40
#define  Foundation_Expire_INCLUDED
41
42
43
#include "Poco/Foundation.h"
44
#include "Poco/AbstractDelegate.h"
45
#include "Poco/Timestamp.h"
46
47
48
namespace Poco {
49
50
51
template <class TArgs>
52
class Expire: public AbstractDelegate<TArgs>
53
        /// Decorator for AbstractDelegate adding automatic 
54
        /// expiring of registrations to AbstractDelegates.
55
{
56
public:
57
        Expire(const AbstractDelegate<TArgs>& p, Timestamp::TimeDiff expireMillisecs):
58
                AbstractDelegate<TArgs>(p),
59
                _pDelegate(p.clone()), 
60
                _expire(expireMillisecs*1000)
61
        {
62
        }
63
64
        Expire(const Expire& expire):
65
                AbstractDelegate<TArgs>(expire),
66
                _pDelegate(expire._pDelegate->clone()),
67
                _expire(expire._expire),
68
                _creationTime(expire._creationTime)
69
        {
70
        }
71
72
        ~Expire()
73
        {
74
                destroy();
75
        }
76
        
77
        Expire& operator = (const Expire& expire)
78
        {
79
                if (&expire != this)
80
                {
81
                        delete this->_pDelegate;
82
                        this->_pDelegate    = expire._pDelegate->clone();
83
                        this->_expire       = expire._expire;
84
                        this->_creationTime = expire._creationTime;
85
                        this->_pTarget = expire._pTarget;
86
                }
87
                return *this;
88
        }
89
90
        bool notify(const void* sender, TArgs& arguments)
91
        {
92
                if (!expired())
93
                        return this->_pDelegate->notify(sender, arguments);
94
                else
95
                        return false;
96
        }
97
98
        AbstractDelegate<TArgs>* clone() const
99
        {
100
                return new Expire(*this);
101
        }
102
103
        void destroy()
104
        {
105
                delete this->_pDelegate;
106
                this->_pDelegate = 0;
107
        }
108
109
        const AbstractDelegate<TArgs>& getDelegate() const
110
        {
111
                return *this->_pDelegate;
112
        }
113
114
protected:
115
        bool expired() const
116
        {
117
                return _creationTime.isElapsed(_expire);
118
        }
119
120
        AbstractDelegate<TArgs>* _pDelegate;
121
        Timestamp::TimeDiff _expire;
122
        Timestamp _creationTime;
123
124
private:
125
        Expire();
126
};
127
128
129
} // namespace Poco
130
131
132
#endif