1
0
Fork 0
mirror of https://github.com/polybar/polybar.git synced 2024-11-11 13:50:56 -05:00
polybar/include/utils/cache.hpp

36 lines
821 B
C++
Raw Normal View History

2016-12-26 04:38:19 -05:00
#pragma once
#include <unordered_map>
#include "common.hpp"
#include "utils/concurrency.hpp"
POLYBAR_NS
template <typename ValueType, typename KeyType>
class cache {
public:
using map_type = std::unordered_map<KeyType, std::weak_ptr<ValueType>>;
using safe_map_type = mutex_wrapper<map_type>;
2017-01-18 23:38:42 -05:00
bool check(const KeyType& key) {
std::lock_guard<safe_map_type> guard(m_cache);
return m_cache.find(key) == m_cache.end();
}
2016-12-26 04:38:19 -05:00
template <typename... MakeArgs>
shared_ptr<ValueType> object(const KeyType& key, MakeArgs&&... make_args) {
std::lock_guard<safe_map_type> guard(m_cache);
auto ptr = m_cache[key].lock();
if (!ptr) {
m_cache[key] = ptr = make_shared<ValueType>(forward<MakeArgs>(make_args)...);
}
return ptr;
}
private:
safe_map_type m_cache;
};
POLYBAR_NS_END