diff --git a/src/autoload.h b/src/autoload.h index d874b4688..7c56588ba 100644 --- a/src/autoload.h +++ b/src/autoload.h @@ -27,10 +27,7 @@ file_access_attempt_t access_file(const wcstring &path, int mode); struct autoload_function_t { explicit autoload_function_t(bool placeholder) - : access(), - is_loaded(false), - is_placeholder(placeholder), - is_internalized(false) {} + : access(), is_loaded(false), is_placeholder(placeholder), is_internalized(false) {} /// The last access attempt recorded file_access_attempt_t access; @@ -82,7 +79,6 @@ class autoload_t : public lru_cache_t { virtual void command_removed(const wcstring &cmd) { UNUSED(cmd); } public: - // CRTP override void entry_was_evicted(wcstring key, autoload_function_t node); diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index f162c9d07..2746d184e 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -1128,9 +1128,7 @@ class test_lru_t : public lru_cache_t { std::vector> evicted; - void entry_was_evicted(wcstring key, int val) { - evicted.push_back({key, val}); - } + void entry_was_evicted(wcstring key, int val) { evicted.push_back({key, val}); } }; static void test_lru(void) { @@ -1144,7 +1142,7 @@ static void test_lru(void) { if (i < 4) expected_evicted.push_back({to_string(i), i}); // Adding the node the first time should work, and subsequent times should fail. do_test(cache.insert(to_string(i), i)); - do_test(!cache.insert(to_string(i), i+1)); + do_test(!cache.insert(to_string(i), i + 1)); } do_test(cache.evicted == expected_evicted); cache.evict_all_nodes(); diff --git a/src/history.cpp b/src/history.cpp index 5b80d0676..1f64d1fa9 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -155,8 +155,8 @@ class history_lru_item_t { class history_lru_cache_t : public lru_cache_t { typedef lru_cache_t super; - public: + public: using super::super; /// Function to add a history item. diff --git a/src/lru.h b/src/lru.h index 3b9b4d148..2f4dd9878 100644 --- a/src/lru.h +++ b/src/lru.h @@ -8,19 +8,19 @@ #include "common.h" -// Least-recently-used cache class +// Least-recently-used cache class. +// // This a map from wcstring to CONTENTS, that will evict entries when the count exceeds the maximum. -// It uses CRTP to inform clients when entries are evicted. This uses the classic LRU cache structure: -// a dictionary mapping keys to nodes, where the nodes also form a linked list. Our linked list is -// circular and has a sentinel node (the "mouth" - picture a snake swallowing its tail). This simplifies -// the logic: no pointer is ever NULL! It also works well with C++'s iterator since the sentinel node -// is a natural value for end(). Our nodes also have the unusual property of having a "back pointer": -// they store an iterator to the entry in the map containing the node. This allows us, given a node, to -// immediately locate the node and its key in the dictionary. This allows us to avoid duplicating the key -// in the node. +// It uses CRTP to inform clients when entries are evicted. This uses the classic LRU cache +// structure: a dictionary mapping keys to nodes, where the nodes also form a linked list. Our +// linked list is circular and has a sentinel node (the "mouth" - picture a snake swallowing its +// tail). This simplifies the logic: no pointer is ever NULL! It also works well with C++'s iterator +// since the sentinel node is a natural value for end(). Our nodes also have the unusual property of +// having a "back pointer": they store an iterator to the entry in the map containing the node. This +// allows us, given a node, to immediately locate the node and its key in the dictionary. This +// allows us to avoid duplicating the key in the node. template class lru_cache_t { - struct lru_node_t; typedef typename std::map::iterator node_iter_t; @@ -44,12 +44,9 @@ class lru_cache_t { // The value from the client CONTENTS value; - explicit lru_node_t(CONTENTS v) : - value(std::move(v)) - {} + explicit lru_node_t(CONTENTS v) : value(std::move(v)) {} }; - // Max node count. This may be (transiently) exceeded by add_node_without_eviction, which is // used from background threads. const size_t max_node_count; @@ -146,7 +143,7 @@ class lru_cache_t { // Adds a node under the given key. Returns true if the node was added, false if the node was // not because a node with that key is already in the set. bool insert(wcstring key, CONTENTS value) { - if (! this->insert_no_eviction(std::move(key), std::move(value))) { + if (!this->insert_no_eviction(std::move(key), std::move(value))) { return false; } @@ -161,7 +158,7 @@ class lru_cache_t { bool insert_no_eviction(wcstring key, CONTENTS value) { // Try inserting; return false if it was already in the set. auto iter_inserted = this->node_map.emplace(std::move(key), lru_node_t(std::move(value))); - if (! iter_inserted.second) { + if (!iter_inserted.second) { // already present return false; } @@ -190,6 +187,7 @@ class lru_cache_t { // Iterator for walking nodes, from least recently used to most. class iterator { lru_link_t *node; + public: typedef std::pair value_type;