diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-01 18:53:51 +0000 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2022-01-01 19:01:39 +0000 |
commit | f91391d6a199b8699cec0426a73fd06055be3052 (patch) | |
tree | 40a3e2f493807a6bdc00891c79e98c3d4e5b072f | |
parent | Don't forward declare Cache (diff) | |
download | ilt-f91391d6a199b8699cec0426a73fd06055be3052.tar.bz2 ilt-f91391d6a199b8699cec0426a73fd06055be3052.tar.xz ilt-f91391d6a199b8699cec0426a73fd06055be3052.zip |
Cache allows multiple key parts
-rw-r--r-- | game/network/network.cpp | 1 | ||||
-rw-r--r-- | game/terrain.cpp | 1 | ||||
-rw-r--r-- | gfx/models/texture.cpp | 2 | ||||
-rw-r--r-- | gfx/models/texture.h | 2 | ||||
-rw-r--r-- | 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 <array> #include <cache.h> +#include <filesystem> #include <game/network/link.h> #include <gfx/models/texture.h> #include <initializer_list> 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 <cache.h> #include <cstddef> +#include <filesystem> #include <gfx/gl/shader.h> #include <gfx/image.h> #include <gfx/models/mesh.h> 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 <resource.h>
#include <stb/stb_image.h>
-Cache<Texture> Texture::cachedTexture;
+Cache<Texture, std::filesystem::path> 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<Texture> cachedTexture;
+ static Cache<Texture, std::filesystem::path> 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 <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 |