| 1 | // |
| 2 | // ExpirationDecorator.h |
| 3 | // |
| 4 | // Library: Foundation |
| 5 | // Package: Events |
| 6 | // Module: ExpirationDecorator |
| 7 | // |
| 8 | // Implementation of the ExpirationDecorator template. |
| 9 | // |
| 10 | // Copyright (c) 2006, Applied Informatics Software Engineering GmbH. |
| 11 | // and Contributors. |
| 12 | // |
| 13 | // SPDX-License-Identifier: BSL-1.0 |
| 14 | // |
| 15 | |
| 16 | |
| 17 | #ifndef Foundation_ExpirationDecorator_INCLUDED |
| 18 | #define Foundation_ExpirationDecorator_INCLUDED |
| 19 | |
| 20 | |
| 21 | #include "Poco/Timestamp.h" |
| 22 | #include "Poco/Timespan.h" |
| 23 | |
| 24 | |
| 25 | namespace Poco { |
| 26 | |
| 27 | |
| 28 | template <typename TArgs> |
| 29 | class ExpirationDecorator |
| 30 | /// ExpirationDecorator adds an expiration method to values so that they can be used |
| 31 | /// with the UniqueExpireCache. |
| 32 | { |
| 33 | public: |
| 34 | ExpirationDecorator(): |
| 35 | _value(), |
| 36 | _expiresAt() |
| 37 | { |
| 38 | } |
| 39 | |
| 40 | ExpirationDecorator(const TArgs& p, const Poco::Timespan::TimeDiff& diffInMs): |
| 41 | /// Creates an element that will expire in diff milliseconds |
| 42 | _value(p), |
| 43 | _expiresAt() |
| 44 | { |
| 45 | _expiresAt += (diffInMs*1000); |
| 46 | } |
| 47 | |
| 48 | ExpirationDecorator(const TArgs& p, const Poco::Timespan& timeSpan): |
| 49 | /// Creates an element that will expire after the given timeSpan |
| 50 | _value(p), |
| 51 | _expiresAt() |
| 52 | { |
| 53 | _expiresAt += timeSpan.totalMicroseconds(); |
| 54 | } |
| 55 | |
| 56 | ExpirationDecorator(const TArgs& p, const Poco::Timestamp& timeStamp): |
| 57 | /// Creates an element that will expire at the given time point |
| 58 | _value(p), |
| 59 | _expiresAt(timeStamp) |
| 60 | { |
| 61 | } |
| 62 | |
| 63 | |
| 64 | ~ExpirationDecorator() |
| 65 | { |
| 66 | } |
| 67 | |
| 68 | const Poco::Timestamp& getExpiration() const |
| 69 | { |
| 70 | return _expiresAt; |
| 71 | } |
| 72 | |
| 73 | const TArgs& value() const |
| 74 | { |
| 75 | return _value; |
| 76 | } |
| 77 | |
| 78 | TArgs& value() |
| 79 | { |
| 80 | return _value; |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | TArgs _value; |
| 85 | Timestamp _expiresAt; |
| 86 | }; |
| 87 | |
| 88 | |
| 89 | } // namespace Poco |
| 90 | |
| 91 | |
| 92 | #endif // Foundation_ExpirationDecorator_INCLUDED |
| 93 | |