diff --git a/include/utils/cache.hpp b/include/utils/cache.hpp new file mode 100644 index 00000000..566b916a --- /dev/null +++ b/include/utils/cache.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include + +#include "common.hpp" +#include "utils/concurrency.hpp" + +POLYBAR_NS + +template +class cache { + public: + using map_type = std::unordered_map>; + using safe_map_type = mutex_wrapper; + + template + shared_ptr object(const KeyType& key, MakeArgs&&... make_args) { + std::lock_guard guard(m_cache); + auto ptr = m_cache[key].lock(); + if (!ptr) { + m_cache[key] = ptr = make_shared(forward(make_args)...); + } + return ptr; + } + + private: + safe_map_type m_cache; +}; + +namespace cache_util {} + +POLYBAR_NS_END