RecentEntries: Expose container as base class.

Trying to refactor until there's nothing but the base class left.
This commit is contained in:
Jared Boone 2016-09-03 18:12:07 -07:00
parent b596d0697c
commit c8f7863c83

View File

@ -34,7 +34,7 @@
#include <algorithm> #include <algorithm>
template<class Entry> template<class Entry>
class RecentEntries { class RecentEntries : public std::list<Entry> {
public: public:
using EntryType = Entry; using EntryType = Entry;
using Key = typename EntryType::Key; using Key = typename EntryType::Key;
@ -44,48 +44,31 @@ public:
EntryType& on_packet(const Key key) { EntryType& on_packet(const Key key) {
auto matching_recent = find(key); auto matching_recent = find(key);
if( matching_recent != std::end(entries) ) { if( matching_recent != std::end(*this) ) {
// Found within. Move to front of list, increment counter. // Found within. Move to front of list, increment counter.
entries.push_front(*matching_recent); this->push_front(*matching_recent);
entries.erase(matching_recent); this->erase(matching_recent);
} else { } else {
entries.emplace_front(key); this->emplace_front(key);
truncate_entries(); truncate_entries();
} }
return entries.front(); return this->front();
}
const_reference front() const {
return entries.front();
} }
const_iterator find(const Key key) const { const_iterator find(const Key key) const {
return std::find_if( return std::find_if(
std::begin(entries), std::end(entries), std::begin(*this), std::end(*this),
[key](const EntryType& e) { return e.key() == key; } [key](const EntryType& e) { return e.key() == key; }
); );
} }
const_iterator begin() const {
return entries.begin();
}
const_iterator end() const {
return entries.end();
}
bool empty() const {
return entries.empty();
}
private: private:
ContainerType entries;
const size_t entries_max = 64; const size_t entries_max = 64;
void truncate_entries() { void truncate_entries() {
while(entries.size() > entries_max) { while(this->size() > entries_max) {
entries.pop_back(); this->pop_back();
} }
} }
}; };