| 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_WRAP_HPP |
| 23 |
#define LRU_WRAP_HPP |
| 24 |
|
| 25 |
#include <cstddef> |
| 26 |
#include <tuple> |
| 27 |
#include <type_traits> |
| 28 |
#include <utility> |
| 29 |
|
| 30 |
#include <lru/cache.hpp> |
| 31 |
#include <lru/internal/hash.hpp> |
| 32 |
#include <lru/internal/utility.hpp> |
| 33 |
|
| 34 |
namespace LRU { |
| 35 |
|
| 36 |
/// Wraps a function with a "shallow" LRU cache. |
| 37 |
/// |
| 38 |
/// Given a function, this function will return a new function, where |
| 39 |
/// "top-level" calls are cached. With "top-level" or "shallow", we mean |
| 40 |
/// that recursive calls to the same function are not cached, since those |
| 41 |
/// will call the original function symbol, not the wrapped one. |
| 42 |
/// |
| 43 |
/// \tparam CacheType The cache template class to use. |
| 44 |
/// \param original_function The function to wrap. |
| 45 |
/// \param args Any arguments to forward to the cache. |
| 46 |
/// \returns A new function with a shallow LRU cache. |
| 47 |
template <typename Function, |
| 48 |
template <typename...> class CacheType = Cache, |
| 49 |
typename... Args> |
| 50 |
auto wrap(Function original_function, Args&&... args) { |
| 51 |
return [ |
| 52 |
original_function, |
| 53 |
cache_args = std::forward_as_tuple(std::forward<Args>(args)...) |
| 54 |
](auto&&... arguments) mutable { |
| 55 |
using Arguments = std::tuple<std::decay_t<decltype(arguments)>...>; |
| 56 |
using ReturnType = decltype( |
| 57 |
original_function(std::forward<decltype(arguments)>(arguments)...)); |
| 58 |
|
| 59 |
static_assert(!std::is_void<ReturnType>::value, |
| 60 |
"Return type of wrapped function must not be void"); |
| 61 |
|
| 62 |
static auto cache = |
| 63 |
Internal::construct_from_tuple<CacheType<Arguments, ReturnType>>( |
| 64 |
cache_args); |
| 65 |
|
| 66 |
auto key = std::make_tuple(arguments...); |
| 67 |
auto iterator = cache.find(key); |
| 68 |
|
| 69 |
if (iterator != cache.end()) { |
| 70 |
return iterator->second; |
| 71 |
} |
| 72 |
|
| 73 |
auto value = |
| 74 |
original_function(std::forward<decltype(arguments)>(arguments)...); |
| 75 |
cache.emplace(key, value); |
| 76 |
|
| 77 |
return value; |
| 78 |
}; |
| 79 |
} |
| 80 |
|
| 81 |
/// Wraps a function with a "shallow" LRU timed cache. |
| 82 |
/// |
| 83 |
/// Given a function, this function will return a new function, where |
| 84 |
/// "top-level" calls are cached. With "top-level" or "shallow", we mean |
| 85 |
/// that recursive calls to the same function are not cached, since those |
| 86 |
/// will call the original function symbol, not the wrapped one. |
| 87 |
/// |
| 88 |
/// \param original_function The function to wrap. |
| 89 |
/// \param args Any arguments to forward to the cache. |
| 90 |
/// \returns A new function with a shallow LRU cache. |
| 91 |
template <typename Function, typename Duration, typename... Args> |
| 92 |
auto timed_wrap(Function original_function, Duration duration, Args&&... args) { |
| 93 |
return wrap<Function, TimedCache>( |
| 94 |
original_function, duration, std::forward<Args>(args)...); |
| 95 |
} |
| 96 |
|
| 97 |
} // namespace LRU |
| 98 |
|
| 99 |
#endif // LRU_WRAP_HPP |