From ebd9e5e13203f3d813d17efaaefbfe7aec527f3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fl=C3=B6ssie?= Date: Sat, 11 Mar 2017 17:14:11 +0100 Subject: [PATCH] Make `rtengine::Cache` build again with hashable keys While STL containers are not guaranteed to build with incomplete types, `rtengine::Cache` used to build with `std::map` as well as `std::unordered_map` when I first implemented it. With GCC 6.3 I recently realized, that this was no longer the case for hashable keys (i.e. `std::unordered_map` as `Store`). The recommended workaround is to use a `std::unique_ptr`. --- rtengine/cache.h | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/rtengine/cache.h b/rtengine/cache.h index f4c232673..ec284b2ae 100644 --- a/rtengine/cache.h +++ b/rtengine/cache.h @@ -19,11 +19,12 @@ #pragma once -#include -#include -#include -#include #include +#include +#include +#include +#include +#include #include "../rtgui/threadutils.h" @@ -87,9 +88,9 @@ public: lru_list.splice( lru_list.begin(), lru_list, - store_it->second.lru_list_it + store_it->second->lru_list_it ); - value = store_it->second.value; + value = store_it->second->value; } mutex.unlock(); @@ -139,7 +140,7 @@ public: mutex.lock(); if (hook) { for (const auto& entry : store) { - hook->onRemove(entry.first, entry.second.value); + hook->onRemove(entry.first, entry.second->value); } } lru_list.clear(); @@ -152,13 +153,13 @@ private: using Store = typename std::conditional< cache_helper::has_hash::value, - std::unordered_map, - std::map + std::unordered_map>, + std::map> >::type; using StoreIterator = typename Store::iterator; using StoreConstIterator = typename Store::const_iterator; - typedef std::list LruList; + using LruList = std::list; using LruListIterator = typename LruList::iterator; struct Value { @@ -176,7 +177,7 @@ private: { const StoreIterator store_it = lru_list.back(); if (hook) { - hook->onDiscard(store_it->first, store_it->second.value); + hook->onDiscard(store_it->first, store_it->second->value); } store.erase(store_it); lru_list.pop_back(); @@ -193,23 +194,25 @@ private: discard(); } lru_list.push_front(store.end()); - const Value v = { - value, - lru_list.begin() - }; - lru_list.front() = store.emplace(key, v).first; + std::unique_ptr v( + new Value{ + value, + lru_list.begin() + } + ); + lru_list.front() = store.emplace(key, std::move(v)).first; } } else { if (mode == Mode::UNCOND || mode == Mode::KNOWN) { if (hook) { - hook->onDisplace(key, store_it->second.value); + hook->onDisplace(key, store_it->second->value); } lru_list.splice( lru_list.begin(), lru_list, - store_it->second.lru_list_it + store_it->second->lru_list_it ); - store_it->second.value = value; + store_it->second->value = value; } } mutex.unlock(); @@ -220,9 +223,9 @@ private: void remove(const StoreIterator& store_it) { if (hook) { - hook->onRemove(store_it->first, store_it->second.value); + hook->onRemove(store_it->first, store_it->second->value); } - lru_list.erase(store_it->second.lru_list_it); + lru_list.erase(store_it->second->lru_list_it); store.erase(store_it); }