diff --git a/include/vapor/ptr_cache.hpp b/include/vapor/ptr_cache.hpp index 760087ff2..dfb1cb746 100644 --- a/include/vapor/ptr_cache.hpp +++ b/include/vapor/ptr_cache.hpp @@ -35,24 +35,21 @@ namespace VAPoR { // Note : 1) Key must support == operator and = operator. // 2) BigObj must be able to be destructed by delete. // -template -class ptr_cache { +template class ptr_cache { public: - // // Constructors and Destructor // ptr_cache() = default; - ptr_cache(const ptr_cache&) = delete; - ptr_cache(const ptr_cache&&) = delete; - ptr_cache& operator=(const ptr_cache&) = delete; - ptr_cache& operator=(const ptr_cache&&) = delete; + ptr_cache(const ptr_cache &) = delete; + ptr_cache(const ptr_cache &&) = delete; + ptr_cache &operator=(const ptr_cache &) = delete; + ptr_cache &operator=(const ptr_cache &&) = delete; ~ptr_cache() { - for (auto& p : _element_vector) { - if (p.second) - delete p.second; - } + for (auto &p : _element_vector) { + if (p.second) delete p.second; + } } auto size() const -> size_t { return _element_vector.size(); } @@ -64,19 +61,23 @@ class ptr_cache { // auto query(const Key &key) -> const BigObj * { - // Only need to apply the mutex if `Query` is true. - if (Query) const std::lock_guard lock_gd(_element_vector_mutex); - - auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; }); - if (it == _element_vector.end()) { // This key does not exist - return nullptr; - } else { // This key does exist - if (!Query) { - return it->second; - } else { + if (Query) { + // Use a guard lock if queries can change ordering. + const std::lock_guard lock_gd(_element_vector_mutex); + auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; }); + if (it == _element_vector.end()) // This key does not exist + return nullptr; + else { // This key does exist std::rotate(_element_vector.begin(), it, it + 1); return _element_vector.front().second; } + } else { + // Just query, no change whatsoever. + auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; }); + if (it == _element_vector.end()) // This key does not exist + return nullptr; + else // This key does exist + return it->second; } } @@ -86,21 +87,21 @@ class ptr_cache { auto it = std::find_if(_element_vector.begin(), _element_vector.end(), [&key](element_type &e) { return e.first == key; }); - if (it == _element_vector.end()) { // This key does not exist - --it; // `it` points to the last element now. + if (it == _element_vector.end()) { // This key does not exist + --it; // `it` points to the last element now. it->first = key; } - if (it->second) // Destroy the old object held here. - delete it->second; + if (it->second) // Destroy the old object held here. + delete it->second; it->second = ptr; std::rotate(_element_vector.begin(), it, it + 1); } private: - using element_type = std::pair; + using element_type = std::pair; - std::mutex _element_vector_mutex; - std::array _element_vector; + std::mutex _element_vector_mutex; + std::array _element_vector; }; } // namespace VAPoR