summaryrefslogtreecommitdiff
path: root/lib/cache.h
diff options
context:
space:
mode:
authorDan Goodliffe <dan@randomdan.homeip.net>2022-01-01 18:53:51 +0000
committerDan Goodliffe <dan@randomdan.homeip.net>2022-01-01 19:01:39 +0000
commitf91391d6a199b8699cec0426a73fd06055be3052 (patch)
tree40a3e2f493807a6bdc00891c79e98c3d4e5b072f /lib/cache.h
parentDon't forward declare Cache (diff)
downloadilt-f91391d6a199b8699cec0426a73fd06055be3052.tar.bz2
ilt-f91391d6a199b8699cec0426a73fd06055be3052.tar.xz
ilt-f91391d6a199b8699cec0426a73fd06055be3052.zip
Cache allows multiple key parts
Diffstat (limited to 'lib/cache.h')
-rw-r--r--lib/cache.h17
1 files changed, 10 insertions, 7 deletions
diff --git a/lib/cache.h b/lib/cache.h
index 2a651f0..cdc0a2e 100644
--- a/lib/cache.h
+++ b/lib/cache.h
@@ -2,13 +2,15 @@
#define CACHE_H
#include "special_members.hpp"
+#include <functional>
#include <map>
#include <memory>
-#include <string>
+#include <tuple>
-template<typename Obj> class Cache {
+template<typename Obj, typename... KeyParts> class Cache {
public:
using Ptr = std::shared_ptr<Obj>;
+ using Key = std::tuple<KeyParts...>;
Cache() = default;
virtual ~Cache() = default;
@@ -16,22 +18,23 @@ public:
NO_COPY(Cache);
[[nodiscard]] Ptr
- get(const std::string & key)
+ get(const KeyParts &... keyparts)
{
+ auto key = std::tie(keyparts...);
if (auto e = cached.find(key); e != cached.end()) {
return e->second;
}
- return cached.emplace(key, construct(key)).first->second;
+ return cached.emplace(key, construct(keyparts...)).first->second;
}
[[nodiscard]] virtual Ptr
- construct(const std::string & key) const
+ construct(const KeyParts &... keyparts) const
{
- return std::make_shared<Obj>(key);
+ return std::make_shared<Obj>(keyparts...);
}
private:
- std::map<std::string, Ptr> cached;
+ std::map<Key, Ptr, std::less<>> cached;
};
// IWYU pragma: no_forward_declare Cache