From d9c8d93180d90ccf50317d5d0e4f74958d0aa469 Mon Sep 17 00:00:00 2001 From: Pablo Curiel Date: Mon, 6 May 2024 00:41:41 +0200 Subject: [PATCH] TitleMetadataTask: use a struct instead of a vector The struct has been typedef'd as TitleApplicationMetadataInfo. It holds the pointer to the array returned by titleGetApplicationMetadataEntries(), as well as the number of elements available in the array. There's no point in populating a vector when we already have an array we can work with. --- include/tasks/title_metadata_task.hpp | 21 ++++++++------ include/views/root_view.hpp | 4 +-- include/views/titles_tab.hpp | 2 +- source/tasks/title_metadata_task.cpp | 40 ++++++++++----------------- source/views/titles_tab.cpp | 16 +++++------ 5 files changed, 38 insertions(+), 45 deletions(-) diff --git a/include/tasks/title_metadata_task.hpp b/include/tasks/title_metadata_task.hpp index 14f4cc2..310f3c9 100644 --- a/include/tasks/title_metadata_task.hpp +++ b/include/tasks/title_metadata_task.hpp @@ -31,23 +31,26 @@ namespace nxdt::tasks { - /* Used to hold pointers to application metadata entries. */ - typedef std::vector TitleApplicationMetadataVector; + /* Used to hold an application metadata array + its number of elements. */ + typedef struct { + TitleApplicationMetadata **app_metadata; + u32 app_metadata_count; + } TitleApplicationMetadataInfo; /* Custom event type. */ - typedef brls::Event UserTitleEvent; + typedef brls::Event UserTitleEvent; /* Title metadata task. */ - /* Its event provides a const reference to a TitleApplicationMetadataVector with metadata for user titles (system titles don't change at runtime). */ + /* Its event provides a const reference to a TitleApplicationMetadataInfo with metadata for user titles (system titles don't change at runtime). */ class TitleMetadataTask: public brls::RepeatingTask { private: UserTitleEvent user_title_event; - TitleApplicationMetadataVector system_metadata{}; - TitleApplicationMetadataVector user_metadata{}; + TitleApplicationMetadataInfo system_metadata_info{}; + TitleApplicationMetadataInfo user_metadata_info{}; - void PopulateApplicationMetadataVector(bool is_system); + void PopulateApplicationMetadataInfo(bool is_system); protected: void run(retro_time_t current_time) override; @@ -57,9 +60,9 @@ namespace nxdt::tasks ~TitleMetadataTask(); /* Intentionally left here to let views retrieve title metadata on-demand. */ - ALWAYS_INLINE const TitleApplicationMetadataVector& GetApplicationMetadata(bool is_system) + ALWAYS_INLINE const TitleApplicationMetadataInfo& GetApplicationMetadataInfo(bool is_system) { - return (is_system ? this->system_metadata : this->user_metadata); + return (is_system ? this->system_metadata_info : this->user_metadata_info); } ALWAYS_INLINE UserTitleEvent::Subscription RegisterListener(UserTitleEvent::Callback cb) diff --git a/include/views/root_view.hpp b/include/views/root_view.hpp index 03c1e0a..0e4b127 100644 --- a/include/views/root_view.hpp +++ b/include/views/root_view.hpp @@ -95,9 +95,9 @@ namespace nxdt::views return this->gc_status_task->GetGameCardStatus(); } - ALWAYS_INLINE const nxdt::tasks::TitleApplicationMetadataVector& GetApplicationMetadata(bool is_system) + ALWAYS_INLINE const nxdt::tasks::TitleApplicationMetadataInfo& GetApplicationMetadataInfo(bool is_system) { - return this->title_metadata_task->GetApplicationMetadata(is_system); + return this->title_metadata_task->GetApplicationMetadataInfo(is_system); } ALWAYS_INLINE const nxdt::tasks::UmsDeviceVector& GetUmsDevices(void) diff --git a/include/views/titles_tab.hpp b/include/views/titles_tab.hpp index 9dc83a9..fba01b4 100644 --- a/include/views/titles_tab.hpp +++ b/include/views/titles_tab.hpp @@ -76,7 +76,7 @@ namespace nxdt::views nxdt::tasks::UserTitleEvent::Subscription title_task_sub; bool is_system = false; - void PopulateList(const nxdt::tasks::TitleApplicationMetadataVector& app_metadata); + void PopulateList(const nxdt::tasks::TitleApplicationMetadataInfo& app_metadata_info); public: TitlesTab(RootView *root_view, bool is_system); diff --git a/source/tasks/title_metadata_task.cpp b/source/tasks/title_metadata_task.cpp index 86b9c27..a6df70d 100644 --- a/source/tasks/title_metadata_task.cpp +++ b/source/tasks/title_metadata_task.cpp @@ -30,10 +30,10 @@ namespace nxdt::tasks LOG_MSG_DEBUG("Title metadata task started."); /* Get system metadata entries. */ - this->PopulateApplicationMetadataVector(true); + this->PopulateApplicationMetadataInfo(true); /* Get user metadata entries. */ - this->PopulateApplicationMetadataVector(false); + this->PopulateApplicationMetadataInfo(false); /* Start task. */ brls::RepeatingTask::start(); @@ -41,9 +41,9 @@ namespace nxdt::tasks TitleMetadataTask::~TitleMetadataTask() { - /* Clear application metadata vectors. */ - this->system_metadata.clear(); - this->user_metadata.clear(); + /* Free application metadata arrays. */ + if (this->system_metadata_info.app_metadata) free(this->system_metadata_info.app_metadata); + if (this->user_metadata_info.app_metadata) free(this->user_metadata_info.app_metadata); LOG_MSG_DEBUG("Title metadata task stopped."); } @@ -54,37 +54,27 @@ namespace nxdt::tasks if (titleIsGameCardInfoUpdated()) { - /* Update user metadata vector. */ - this->PopulateApplicationMetadataVector(false); + /* Update user metadata array. */ + this->PopulateApplicationMetadataInfo(false); /* Fire task event. */ - this->user_title_event.fire(this->user_metadata); + this->user_title_event.fire(this->user_metadata_info); //brls::Application::notify("tasks/notifications/user_titles"_i18n); LOG_MSG_DEBUG("Title info updated."); } } - void TitleMetadataTask::PopulateApplicationMetadataVector(bool is_system) + void TitleMetadataTask::PopulateApplicationMetadataInfo(bool is_system) { - TitleApplicationMetadata **app_metadata = nullptr; - u32 app_metadata_count = 0; - - /* Get pointer to output vector. */ - TitleApplicationMetadataVector& vector = (is_system ? this->system_metadata : this->user_metadata); - vector.clear(); + /* Get reference to output struct. */ + TitleApplicationMetadataInfo& info = (is_system ? this->system_metadata_info : this->user_metadata_info); + if (info.app_metadata) free(info.app_metadata); + info.app_metadata_count = 0; /* Get application metadata entries. */ - app_metadata = titleGetApplicationMetadataEntries(is_system, &app_metadata_count); - if (app_metadata) - { - /* Fill output vector. */ - for(u32 i = 0; i < app_metadata_count; i++) vector.push_back(app_metadata[i]); + info.app_metadata = titleGetApplicationMetadataEntries(is_system, &(info.app_metadata_count)); - /* Free application metadata array. */ - free(app_metadata); - } - - LOG_MSG_DEBUG("Retrieved %u %s metadata %s.", app_metadata_count, is_system ? "system" : "user", app_metadata_count == 1 ? "entry" : "entries"); + LOG_MSG_DEBUG("Retrieved %u %s metadata %s.", info.app_metadata_count, is_system ? "system" : "user", info.app_metadata_count == 1 ? "entry" : "entries"); } } diff --git a/source/views/titles_tab.cpp b/source/views/titles_tab.cpp index 93d2cc3..d3bd046 100644 --- a/source/views/titles_tab.cpp +++ b/source/views/titles_tab.cpp @@ -82,14 +82,14 @@ namespace nxdt::views TitlesTab::TitlesTab(RootView *root_view, bool is_system) : LayeredErrorFrame("titles_tab/no_titles_available"_i18n), root_view(root_view), is_system(is_system) { /* Populate list. */ - this->PopulateList(this->root_view->GetApplicationMetadata(this->is_system)); + this->PopulateList(this->root_view->GetApplicationMetadataInfo(this->is_system)); /* Subscribe to the title event if this is the user titles tab. */ if (!this->is_system) { - this->title_task_sub = this->root_view->RegisterTitleMetadataTaskListener([this](const nxdt::tasks::TitleApplicationMetadataVector& app_metadata) { + this->title_task_sub = this->root_view->RegisterTitleMetadataTaskListener([this](const nxdt::tasks::TitleApplicationMetadataInfo& app_metadata_info) { /* Update list. */ - this->PopulateList(app_metadata); + this->PopulateList(app_metadata_info); }); } } @@ -100,34 +100,34 @@ namespace nxdt::views if (!this->is_system) this->root_view->UnregisterTitleMetadataTaskListener(this->title_task_sub); } - void TitlesTab::PopulateList(const nxdt::tasks::TitleApplicationMetadataVector& app_metadata) + void TitlesTab::PopulateList(const nxdt::tasks::TitleApplicationMetadataInfo& app_metadata_info) { /* Block user inputs. */ brls::Application::blockInputs(); /* Populate variables. */ - size_t app_metadata_count = app_metadata.size(); bool update_focused_view = this->IsListItemFocused(); int focus_stack_index = this->GetFocusStackViewIndex(); /* If needed, switch to the error frame *before* cleaning up our list. */ - if (!app_metadata_count) this->SwitchLayerView(true); + if (!app_metadata_info.app_metadata_count) this->SwitchLayerView(true); /* Clear list. */ this->list->clear(); this->list->invalidate(true); /* Return immediately if we have no application metadata. */ - if (!app_metadata_count) + if (!app_metadata_info.app_metadata_count) { brls::Application::unblockInputs(); return; } /* Populate list. */ - for(const TitleApplicationMetadata *cur_app_metadata : app_metadata) + for(u32 i = 0; i < app_metadata_info.app_metadata_count; i++) { /* Create list item. */ + const TitleApplicationMetadata *cur_app_metadata = app_metadata_info.app_metadata[i]; TitlesTabItem *item = new TitlesTabItem(cur_app_metadata, this->is_system); /* Register click event. */