From b7f16672ff51792a7f209d18fed464212d250606 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Mon, 26 Dec 2016 10:38:19 +0100 Subject: [PATCH] feat: Add object cache container --- include/utils/cache.hpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 include/utils/cache.hpp 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