From f91391d6a199b8699cec0426a73fd06055be3052 Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Sat, 1 Jan 2022 18:53:51 +0000 Subject: Cache allows multiple key parts --- game/network/network.cpp | 1 + game/terrain.cpp | 1 + gfx/models/texture.cpp | 2 +- gfx/models/texture.h | 2 +- lib/cache.h | 17 ++++++++++------- 5 files changed, 14 insertions(+), 9 deletions(-) diff --git a/game/network/network.cpp b/game/network/network.cpp index 47e51e2..f59fe1e 100644 --- a/game/network/network.cpp +++ b/game/network/network.cpp @@ -2,6 +2,7 @@ #include "routeWalker.h" #include #include +#include #include #include #include diff --git a/game/terrain.cpp b/game/terrain.cpp index 4c902ef..3089f3a 100644 --- a/game/terrain.cpp +++ b/game/terrain.cpp @@ -2,6 +2,7 @@ #include "gfx/models/texture.h" #include #include +#include #include #include #include diff --git a/gfx/models/texture.cpp b/gfx/models/texture.cpp index 612f0a0..79376bc 100644 --- a/gfx/models/texture.cpp +++ b/gfx/models/texture.cpp @@ -6,7 +6,7 @@ #include #include -Cache Texture::cachedTexture; +Cache Texture::cachedTexture; Texture::Texture(const std::filesystem::path & fileName) { diff --git a/gfx/models/texture.h b/gfx/models/texture.h index 35c8c8b..e029a9b 100644 --- a/gfx/models/texture.h +++ b/gfx/models/texture.h @@ -11,7 +11,7 @@ class Texture { public: explicit Texture(const std::filesystem::path & fileName); - static Cache cachedTexture; + static Cache cachedTexture; void Bind() const; 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 #include #include -#include +#include -template class Cache { +template class Cache { public: using Ptr = std::shared_ptr; + using Key = std::tuple; 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(key); + return std::make_shared(keyparts...); } private: - std::map cached; + std::map> cached; }; // IWYU pragma: no_forward_declare Cache -- cgit v1.2.3