Generalize AISRecentEntries -> templated RecentEntries.

Also access Entry unique key via key().
This commit is contained in:
Jared Boone 2016-01-17 17:58:39 -08:00
parent 5a864d8d44
commit 3ee6fd3d87
2 changed files with 38 additions and 23 deletions

View file

@ -216,15 +216,15 @@ void AISRecentEntry::update(const ais::Packet& packet) {
}
}
const AISRecentEntry& AISRecentEntries::on_packet(const ais::Packet& packet) {
const auto source_id = packet.source_id();
auto matching_recent = find(source_id);
template<class Packet, class Entry>
const Entry& RecentEntries<Packet, Entry>::on_packet(const Key key, const Packet& packet) {
auto matching_recent = find(key);
if( matching_recent != std::end(entries) ) {
// Found within. Move to front of list, increment counter.
entries.push_front(*matching_recent);
entries.erase(matching_recent);
} else {
entries.emplace_front(source_id);
entries.emplace_front(key);
truncate_entries();
}
@ -234,21 +234,24 @@ const AISRecentEntry& AISRecentEntries::on_packet(const ais::Packet& packet) {
return entry;
}
AISRecentEntries::ContainerType::const_iterator AISRecentEntries::find(const ais::MMSI key) const {
template<class Packet, class Entry>
typename RecentEntries<Packet, Entry>::const_iterator RecentEntries<Packet, Entry>::find(const Key key) const {
return std::find_if(
std::begin(entries), std::end(entries),
[key](const AISRecentEntry& e) { return e.mmsi == key; }
[key](const Entry& e) { return e.key() == key; }
);
}
void AISRecentEntries::truncate_entries() {
template<class Packet, class Entry>
void RecentEntries<Packet, Entry>::truncate_entries() {
while(entries.size() > entries_max) {
entries.pop_back();
}
}
AISRecentEntries::RangeType AISRecentEntries::range_around(
ContainerType::const_iterator item, const size_t count
template<class Packet, class Entry>
typename RecentEntries<Packet, Entry>::RangeType RecentEntries<Packet, Entry>::range_around(
const_iterator item, const size_t count
) const {
auto start = item;
auto end = item;
@ -332,7 +335,7 @@ void AISRecentEntriesView::paint(Painter& painter) {
for(auto p = range.first; p != range.second; p++) {
const auto& entry = *p;
const auto is_selected_key = (selected_key == entry.mmsi);
const auto is_selected_key = (selected_key == entry.key());
ais_list_item_draw(entry, target_rect, painter, s, (has_focus() && is_selected_key));
target_rect.pos.y += target_rect.height();
}
@ -344,7 +347,7 @@ void AISRecentEntriesView::advance(const int32_t amount) {
if( recent.empty() ) {
selected_key = invalid_key;
} else {
selected_key = recent.front().mmsi;
selected_key = recent.front().key();
}
} else {
if( amount < 0 ) {
@ -358,7 +361,7 @@ void AISRecentEntriesView::advance(const int32_t amount) {
return;
}
}
selected_key = selected->mmsi;
selected_key = selected->key();
}
set_dirty();
@ -470,11 +473,11 @@ void AISAppView::set_parent_rect(const Rect new_parent_rect) {
void AISAppView::on_packet(const ais::Packet& packet) {
logger.on_packet(packet);
const auto updated_entry = recent.on_packet(packet);
const auto updated_entry = recent.on_packet(packet.source_id(), packet);
recent_entries_view.set_dirty();
// TODO: Crude hack, should be a more formal listener arrangement...
if( updated_entry.mmsi == recent_entry_detail_view.entry().mmsi ) {
if( updated_entry.key() == recent_entry_detail_view.entry().key() ) {
recent_entry_detail_view.set_entry(updated_entry);
}
}