| 1 |
/// The MIT License (MIT) |
| 2 |
/// Copyright (c) 2016 Peter Goldsborough |
| 3 |
/// |
| 4 |
/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
/// of this software and associated documentation files (the "Software"), to |
| 6 |
/// deal in the Software without restriction, including without limitation the |
| 7 |
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
/// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
/// furnished to do so, subject to the following conditions: |
| 10 |
/// |
| 11 |
/// The above copyright notice and this permission notice shall be included in |
| 12 |
/// all copies or substantial portions of the Software. |
| 13 |
/// |
| 14 |
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
/// IN THE SOFTWARE. |
| 21 |
|
| 22 |
#ifndef LRU_INTERNAL_TIMED_INFORMATION_HPP |
| 23 |
#define LRU_INTERNAL_TIMED_INFORMATION_HPP |
| 24 |
|
| 25 |
#include <cstddef> |
| 26 |
#include <tuple> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
#include <lru/internal/definitions.hpp> |
| 30 |
#include <lru/internal/information.hpp> |
| 31 |
#include <lru/internal/utility.hpp> |
| 32 |
|
| 33 |
namespace LRU { |
| 34 |
namespace Internal { |
| 35 |
|
| 36 |
/// The information object for timed caches. |
| 37 |
/// |
| 38 |
/// TimedInformation differs from plain information only in that it stores the |
| 39 |
/// creation time, to know when a key has expired. |
| 40 |
/// |
| 41 |
/// \tparam Key The key type of the information. |
| 42 |
/// \tparam Value The value type of the information. |
| 43 |
template <typename Key, typename Value> |
| 44 |
struct TimedInformation : public Information<Key, Value> { |
| 45 |
using super = Information<Key, Value>; |
| 46 |
using typename super::QueueIterator; |
| 47 |
using Timestamp = Internal::Timestamp; |
| 48 |
|
| 49 |
/// Constructor. |
| 50 |
/// |
| 51 |
/// \param value_ The value for the information. |
| 52 |
/// \param insertion_time_ The insertion timestamp of the key. |
| 53 |
/// \param order_ The order iterator for the information. |
| 54 |
TimedInformation(const Value& value_, |
| 55 |
const Timestamp& insertion_time_, |
| 56 |
QueueIterator order_ = QueueIterator()) |
| 57 |
: super(value_, order_), insertion_time(insertion_time_) { |
| 58 |
} |
| 59 |
|
| 60 |
/// Constructor. |
| 61 |
/// |
| 62 |
/// Uses the current time as the insertion timestamp. |
| 63 |
/// |
| 64 |
/// \param value_ The value for the information. |
| 65 |
/// \param order_ The order iterator for the information. |
| 66 |
explicit TimedInformation(const Value& value_, |
| 67 |
QueueIterator order_ = QueueIterator()) |
| 68 |
: TimedInformation(value_, Internal::Clock::now(), order_) { |
| 69 |
} |
| 70 |
|
| 71 |
/// \copydoc Information::Information(QueueIterator,ValueArguments&&) |
| 72 |
template <typename... ValueArguments> |
| 73 |
TimedInformation(QueueIterator order_, ValueArguments&&... value_argument) |
| 74 |
: super(std::forward<ValueArguments>(value_argument)..., order_) |
| 75 |
, insertion_time(Internal::Clock::now()) { |
| 76 |
} |
| 77 |
|
| 78 |
/// \copydoc Information::Information(QueueIterator,const |
| 79 |
/// std::tuple<ValueArguments...>&) |
| 80 |
template <typename... ValueArguments> |
| 81 |
explicit TimedInformation( |
| 82 |
const std::tuple<ValueArguments...>& value_arguments, |
| 83 |
QueueIterator order_ = QueueIterator()) |
| 84 |
: super(value_arguments, order_), insertion_time(Internal::Clock::now()) { |
| 85 |
} |
| 86 |
|
| 87 |
/// Compares this timed information for equality with another one. |
| 88 |
/// |
| 89 |
/// Additionally to key and value equality, the timed information requires |
| 90 |
/// that the insertion timestamps be equal. |
| 91 |
/// |
| 92 |
/// \param other The other timed information. |
| 93 |
/// \returns True if this information equals the other one, else false. |
| 94 |
bool operator==(const TimedInformation& other) const noexcept { |
| 95 |
if (super::operator!=(other)) return false; |
| 96 |
return this->insertion_time == other.insertion_time; |
| 97 |
} |
| 98 |
|
| 99 |
/// Compares this timed information for inequality with another one. |
| 100 |
/// |
| 101 |
/// \param other The other timed information. |
| 102 |
/// \returns True if this information does not equal the other one, else |
| 103 |
/// false. |
| 104 |
/// \see operator==() |
| 105 |
bool operator!=(const TimedInformation& other) const noexcept { |
| 106 |
return !(*this == other); |
| 107 |
} |
| 108 |
|
| 109 |
/// The time at which the key of the information was insterted into a cache. |
| 110 |
const Timestamp insertion_time; |
| 111 |
}; |
| 112 |
|
| 113 |
} // namespace Internal |
| 114 |
} // namespace LRU |
| 115 |
|
| 116 |
#endif // LRU_INTERNAL_TIMED_INFORMATION_HPP |