#ifndef CACHE_H #define CACHE_H #include "special_members.hpp" #include #include #include #include template class Cache { public: using Ptr = std::shared_ptr; using Key = std::tuple; Cache() = default; virtual ~Cache() = default; DEFAULT_MOVE(Cache); NO_COPY(Cache); [[nodiscard]] Ptr 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(keyparts...)).first->second; } [[nodiscard]] virtual Ptr construct(const KeyParts &... keyparts) const { return std::make_shared(keyparts...); } private: std::map> cached; }; // IWYU pragma: no_forward_declare Cache #endif