Skip to content

Commit

Permalink
Using unique_ptr for dynamically loading listItems
Browse files Browse the repository at this point in the history
  • Loading branch information
robsdedude committed Apr 20, 2020
1 parent 3bf2ed2 commit 65510a9
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
20 changes: 6 additions & 14 deletions src/Views/ListView/listView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ListView::ListView(std::vector<Homebrew> homebrews)
log_printf(DBG_DEBUG, "posY: %d", posY);
log_printf(DBG_DEBUG, "homebrews size: %d", homebrews.size());
this->homebrews = homebrews;
listItems = std::vector<ListItem*>(homebrews.size(), nullptr);
listItems = std::vector<std::unique_ptr<ListItem>>(homebrews.size());
LoadListItems();
}

Expand All @@ -100,7 +100,7 @@ long ListView::_LoadPreviousListItems(long firstDisplayed, long firstToLoad, lon
{
if (!listItems[i])
{
listItems[i] = new ListItem(homebrews[i]);
listItems[i] = std::make_unique<ListItem>(homebrews[i]);
if (++loaded > maxLoad)
{
break;
Expand All @@ -117,7 +117,7 @@ long ListView::_LoadShownListItems(long firstDisplayed, long lastDisplayed, long
{
if (!listItems[i])
{
listItems[i] = new ListItem(homebrews[i]);
listItems[i] = std::make_unique<ListItem>(homebrews[i]);
if (++loaded > maxLoad)
{
break;
Expand All @@ -134,7 +134,7 @@ long ListView::_LoadNextListItems(long lastDisplayed, long lastToLoad, long maxL
{
if (!listItems[i])
{
listItems[i] = new ListItem(homebrews[i]);
listItems[i] = std::make_unique<ListItem>(homebrews[i]);
if (++loaded > maxLoad)
{
break;
Expand All @@ -153,19 +153,11 @@ void ListView::LoadListItems()
long loaded = 0;
for (long i = 0; i < firstToLoad; i++)
{
if (listItems[i])
{
delete listItems[i];
listItems[i] = nullptr;
}
listItems[i] = nullptr;
}
for (long i = lastToLoad; i < (long)listItems.size(); i++)
{
if (listItems[i])
{
delete listItems[i];
listItems[i] = nullptr;
}
listItems[i] = nullptr;
}

loaded += _LoadShownListItems(first, last, MAX_LOAD_LIST_ITEMS_PER_CYCLE);
Expand Down
2 changes: 1 addition & 1 deletion src/Views/ListView/listView.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ListView: public View {

protected:
std::vector<Homebrew> homebrews;
std::vector<ListItem*> listItems;
std::vector<std::unique_ptr<ListItem>> listItems;
void LoadListItems();

private:
Expand Down
7 changes: 2 additions & 5 deletions src/Views/ListView/searchView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,8 @@ int SearchView::Display()
std::vector<Homebrew> hbs;
hbs = db->Search(SearchQuery(_ime_search_view_result->userText));
homebrews = hbs;
for (ListItem* item: listItems) {
delete item;
}
listItems.clear();
listItems = std::vector<ListItem*>(homebrews.size(), nullptr);

std::fill(listItems.begin(), listItems.end(), nullptr);
LoadListItems();
lastQuery = _ime_search_view_result->userText;
}
Expand Down

0 comments on commit 65510a9

Please sign in to comment.