/* * WiVRn VR streaming * Copyright (C) 2025 Guillaume Meunier * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #pragma once #include "utils/thread_safe.h" #include #include #include namespace utils { template class cache { Loader loader_; thread_safe>> entries; public: template cache(Args &&... args) : loader_(std::forward(args)...) {} template requires std::invocable std::shared_ptr load(const Key & key, Args &&... args) { auto _ = entries.lock(); auto iter = _->find(key); if (iter != _->end()) return iter->second; return _->emplace(key, loader_(std::forward(args)...)).first->second; } template std::shared_ptr load_uncached(Args &&... args) { return loader_(std::forward(args)...); } void clear() { auto _ = entries.lock(); _->clear(); } void remove(const Key & key) { auto _ = entries.lock(); _->erase(key); } Loader & loader() { return loader_; } const Loader & loader() const { return loader_; } }; } // namespace utils