From 54d73a6d3b71034b347134014158bf4c1a3b8d4e Mon Sep 17 00:00:00 2001 From: ITotalJustice <47043333+ITotalJustice@users.noreply.github.com> Date: Mon, 4 Aug 2025 18:58:20 +0100 Subject: [PATCH] optimise: pass all large objects (std::function, std::string, structs) by const ref rather than value. really, these functions should be passed by && and using std::forward on assignment. however, this means writing a lot of extra code for every single class, as well as explicitly calling move in a lot of cases. In the case of std::string, passing by value and calling std::move is the correct approach, especially if the string is created as an rvalue, then it is only created once and moved into the dest. Whereas with a const ref, the string is created and then copied into the dst, basically creating 2 copies. The same thing happens std::function, and well any object. However, accepting everything by value sucks if you call a constructor from within a constructor, as now you need to create 2 impls that accept by value and the other by rvalue. All of this extra code to have a more efficent impl just isn't worth it when, going by the benchmarks, makes no measurable difference (i count anything within >= 1ms as measurable). --- sphaira/CMakeLists.txt | 1 + sphaira/include/app.hpp | 2 +- sphaira/include/download.hpp | 4 +-- sphaira/include/fs.hpp | 2 +- sphaira/include/ftpsrv_helper.hpp | 2 +- sphaira/include/haze_helper.hpp | 2 +- sphaira/include/threaded_file_transfer.hpp | 10 ++++---- sphaira/include/ui/list.hpp | 14 +++++----- sphaira/include/ui/menus/appstore.hpp | 12 +++++---- sphaira/include/ui/menus/menu_base.hpp | 14 +++++++--- sphaira/include/ui/menus/themezer.hpp | 4 +-- sphaira/include/ui/notification.hpp | 10 +++----- sphaira/include/ui/object.hpp | 2 +- sphaira/include/ui/option_box.hpp | 2 +- sphaira/include/ui/popup_list.hpp | 14 +++++----- sphaira/include/ui/progress_box.hpp | 2 +- sphaira/include/ui/sidebar.hpp | 30 +++++++++++----------- sphaira/include/ui/types.hpp | 12 ++++----- sphaira/include/ui/widget.hpp | 4 +-- sphaira/source/app.cpp | 2 +- sphaira/source/ftpsrv_helper.cpp | 2 +- sphaira/source/haze_helper.cpp | 2 +- sphaira/source/threaded_file_transfer.cpp | 20 +++++++-------- sphaira/source/ui/list.cpp | 12 ++++----- sphaira/source/ui/menus/appstore.cpp | 2 +- sphaira/source/ui/menus/file_picker.cpp | 2 +- sphaira/source/ui/menus/filebrowser.cpp | 2 +- sphaira/source/ui/menus/gc_menu.cpp | 2 +- sphaira/source/ui/menus/ghdl.cpp | 2 +- sphaira/source/ui/menus/menu_base.cpp | 12 --------- sphaira/source/ui/notification.cpp | 4 +-- sphaira/source/ui/option_box.cpp | 2 +- sphaira/source/ui/popup_list.cpp | 24 ++++++++--------- sphaira/source/ui/progress_box.cpp | 11 ++++---- sphaira/source/ui/sidebar.cpp | 14 +++++----- sphaira/source/ui/widget.cpp | 2 +- 36 files changed, 127 insertions(+), 133 deletions(-) diff --git a/sphaira/CMakeLists.txt b/sphaira/CMakeLists.txt index b8972a8..4905f9e 100644 --- a/sphaira/CMakeLists.txt +++ b/sphaira/CMakeLists.txt @@ -247,6 +247,7 @@ set(NANOVG_NO_HDR ON) set(NANOVG_NO_PIC ON) set(NANOVG_NO_PNM ON) +set(YYJSON_INSTALL OFF) set(YYJSON_DISABLE_READER OFF) set(YYJSON_DISABLE_WRITER OFF) set(YYJSON_DISABLE_UTILS ON) diff --git a/sphaira/include/app.hpp b/sphaira/include/app.hpp index 2d15778..43a32d3 100644 --- a/sphaira/include/app.hpp +++ b/sphaira/include/app.hpp @@ -71,7 +71,7 @@ public: static void PopToMenu(); // this is thread safe - static void Notify(std::string text, ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT); + static void Notify(const std::string& text, ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT); static void Notify(ui::NotifEntry entry); static void NotifyPop(ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT); static void NotifyClear(ui::NotifEntry::Side side = ui::NotifEntry::Side::RIGHT); diff --git a/sphaira/include/download.hpp b/sphaira/include/download.hpp index 02ed1a7..ef9e57f 100644 --- a/sphaira/include/download.hpp +++ b/sphaira/include/download.hpp @@ -52,7 +52,7 @@ struct Fields { struct Header { Header() = default; - Header(std::initializer_list> p) : m_map{p} {} + Header(std::initializer_list>&& p) : m_map{std::forward(p)} {} std::unordered_map m_map; auto Find(const std::string& key) const { @@ -91,7 +91,7 @@ struct UserPass { struct UploadInfo { UploadInfo() = default; UploadInfo(const std::string& name) : m_name{name} {} - UploadInfo(const std::string& name, s64 size, OnUploadCallback cb) : m_name{name}, m_size{size}, m_callback{cb} {} + UploadInfo(const std::string& name, s64 size, const OnUploadCallback& cb) : m_name{name}, m_size{size}, m_callback{cb} {} UploadInfo(const std::string& name, const std::vector& data) : m_name{name}, m_data{data} {} std::string m_name{}; std::vector m_data{}; diff --git a/sphaira/include/fs.hpp b/sphaira/include/fs.hpp index c14032c..4b22e03 100644 --- a/sphaira/include/fs.hpp +++ b/sphaira/include/fs.hpp @@ -487,7 +487,7 @@ struct FsNative : Fs { FsFileSystem m_fs{}; Result m_open_result{}; - bool m_own{true}; + const bool m_own{true}; }; #if 0 diff --git a/sphaira/include/ftpsrv_helper.hpp b/sphaira/include/ftpsrv_helper.hpp index 5482c23..24ef315 100644 --- a/sphaira/include/ftpsrv_helper.hpp +++ b/sphaira/include/ftpsrv_helper.hpp @@ -11,7 +11,7 @@ using OnInstallStart = std::function; using OnInstallWrite = std::function; using OnInstallClose = std::function; -void InitInstallMode(OnInstallStart on_start, OnInstallWrite on_write, OnInstallClose on_close); +void InitInstallMode(const OnInstallStart& on_start, const OnInstallWrite& on_write, const OnInstallClose& on_close); void DisableInstallMode(); unsigned GetPort(); diff --git a/sphaira/include/haze_helper.hpp b/sphaira/include/haze_helper.hpp index d958a4f..8f3f48c 100644 --- a/sphaira/include/haze_helper.hpp +++ b/sphaira/include/haze_helper.hpp @@ -11,7 +11,7 @@ using OnInstallStart = std::function; using OnInstallWrite = std::function; using OnInstallClose = std::function; -void InitInstallMode(OnInstallStart on_start, OnInstallWrite on_write, OnInstallClose on_close); +void InitInstallMode(const OnInstallStart& on_start, const OnInstallWrite& on_write, const OnInstallClose& on_close); void DisableInstallMode(); } // namespace sphaira::haze diff --git a/sphaira/include/threaded_file_transfer.hpp b/sphaira/include/threaded_file_transfer.hpp index 058ea26..c57101c 100644 --- a/sphaira/include/threaded_file_transfer.hpp +++ b/sphaira/include/threaded_file_transfer.hpp @@ -32,11 +32,11 @@ using StartCallback = std::function; using StartCallback2 = std::function; // reads data from rfunc into wfunc. -Result Transfer(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, WriteCallback wfunc, Mode mode = Mode::MultiThreaded); +Result Transfer(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const WriteCallback& wfunc, Mode mode = Mode::MultiThreaded); // reads data from rfunc, pull data from provided pull() callback. -Result TransferPull(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, StartCallback sfunc, Mode mode = Mode::MultiThreaded); -Result TransferPull(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, StartCallback2 sfunc, Mode mode = Mode::MultiThreaded); +Result TransferPull(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const StartCallback& sfunc, Mode mode = Mode::MultiThreaded); +Result TransferPull(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const StartCallback2& sfunc, Mode mode = Mode::MultiThreaded); // helper for extract zips. // this will multi-thread unzip if size >= 512KiB, otherwise it'll single pass. @@ -50,7 +50,7 @@ using UnzipAllFilter = std::function; + using Callback = std::function; using TouchCallback = std::function; List(s64 row, s64 page, const Vec4& pos, const Vec4& v, const Vec2& pad = {}); - void OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback); + void OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback); - void Draw(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const; + void Draw(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const; auto SetScrollBarPos(float x, float y, float h) { m_scrollbar.x = x; @@ -73,10 +73,10 @@ private: auto ClampX(float x, s64 count) const -> float; auto ClampY(float y, s64 count) const -> float; - void OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback); - void OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback); - void DrawHome(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const; - void DrawGrid(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const; + void OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback); + void OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback); + void DrawHome(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const; + void DrawGrid(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const; private: const s64 m_row; diff --git a/sphaira/include/ui/menus/appstore.hpp b/sphaira/include/ui/menus/appstore.hpp index 2a6c352..cc30a4b 100644 --- a/sphaira/include/ui/menus/appstore.hpp +++ b/sphaira/include/ui/menus/appstore.hpp @@ -86,14 +86,16 @@ struct EntryMenu final : MenuBase { private: struct Option { - Option(const std::string& dt, const std::string& ct, std::function f) + using Callback = std::function; + + Option(const std::string& dt, const std::string& ct, const Callback& f) : display_text{dt}, confirm_text{ct}, func{f} {} - Option(const std::string& dt, std::function f) + Option(const std::string& dt, const Callback& f) : display_text{dt}, func{f} {} - std::string display_text{}; - std::string confirm_text{}; - std::function func{}; + const std::string display_text; + const std::string confirm_text; + const Callback func{}; }; Entry& m_entry; diff --git a/sphaira/include/ui/menus/menu_base.hpp b/sphaira/include/ui/menus/menu_base.hpp index c211ba7..8d28f77 100644 --- a/sphaira/include/ui/menus/menu_base.hpp +++ b/sphaira/include/ui/menus/menu_base.hpp @@ -33,9 +33,17 @@ struct MenuBase : Widget { return true; } - void SetTitle(std::string title); - void SetTitleSubHeading(std::string sub_heading); - void SetSubHeading(std::string sub_heading); + void SetTitle(const std::string& title) { + m_title = title; + } + + void SetTitleSubHeading(const std::string& sub_heading) { + m_title_sub_heading = sub_heading; + } + + void SetSubHeading(const std::string& sub_heading) { + m_sub_heading = sub_heading; + } auto GetTitle() const { return m_title; diff --git a/sphaira/include/ui/menus/themezer.hpp b/sphaira/include/ui/menus/themezer.hpp index 533b16e..d86426f 100644 --- a/sphaira/include/ui/menus/themezer.hpp +++ b/sphaira/include/ui/menus/themezer.hpp @@ -104,7 +104,7 @@ struct Config { u32 limit{18}; bool nsfw{false}; - void SetQuery(std::string new_query) { + void SetQuery(const std::string& new_query) { query = new_query; } @@ -112,7 +112,7 @@ struct Config { query.clear(); } - void SetCreator(Creator new_creator) { + void SetCreator(const Creator& new_creator) { creator = new_creator.id; } diff --git a/sphaira/include/ui/notification.hpp b/sphaira/include/ui/notification.hpp index 66e3497..7c93922 100644 --- a/sphaira/include/ui/notification.hpp +++ b/sphaira/include/ui/notification.hpp @@ -11,8 +11,7 @@ public: enum class Side { LEFT, RIGHT }; public: - NotifEntry(std::string text, Side side); - ~NotifEntry() = default; + NotifEntry(const std::string& text, Side side); auto Draw(NVGcontext* vg, Theme* theme, float y) -> bool; auto GetSide() const noexcept { return m_side; } @@ -22,17 +21,14 @@ private: void Draw(NVGcontext* vg, Theme* theme) override; private: - std::string m_text{}; + std::string m_text; + Side m_side; std::size_t m_count{180}; // count down to zero - Side m_side{}; bool m_bounds_measured{}; }; class NotifMananger final : public Object { public: - NotifMananger() = default; - ~NotifMananger() = default; - void Draw(NVGcontext* vg, Theme* theme) override; void Push(const NotifEntry& entry); diff --git a/sphaira/include/ui/object.hpp b/sphaira/include/ui/object.hpp index fae55e5..968dfb8 100644 --- a/sphaira/include/ui/object.hpp +++ b/sphaira/include/ui/object.hpp @@ -54,7 +54,7 @@ public: m_pos = { x, y, w, h }; } - auto SetPos(Vec4 v) noexcept -> void { + auto SetPos(const Vec4& v) noexcept -> void { m_pos = v; } diff --git a/sphaira/include/ui/option_box.hpp b/sphaira/include/ui/option_box.hpp index 54038bb..f7afcba 100644 --- a/sphaira/include/ui/option_box.hpp +++ b/sphaira/include/ui/option_box.hpp @@ -9,7 +9,7 @@ class OptionBoxEntry final : public Widget { public: public: - OptionBoxEntry(const std::string& text, Vec4 pos); + OptionBoxEntry(const std::string& text, const Vec4& pos); auto Update(Controller* controller, TouchInfo* touch) -> void override {} auto Draw(NVGcontext* vg, Theme* theme) -> void override; diff --git a/sphaira/include/ui/popup_list.hpp b/sphaira/include/ui/popup_list.hpp index e8cf73d..8d987ac 100644 --- a/sphaira/include/ui/popup_list.hpp +++ b/sphaira/include/ui/popup_list.hpp @@ -13,11 +13,11 @@ public: using Callback = std::function)>; public: - explicit PopupList(std::string title, Items items, Callback cb, s64 index = 0); - PopupList(std::string title, Items items, Callback cb, std::string index); - PopupList(std::string title, Items items, std::string& index_str_ref, s64& index); - PopupList(std::string title, Items items, std::string& index_ref); - PopupList(std::string title, Items items, s64& index_ref); + explicit PopupList(const std::string& title, const Items& items, const Callback& cb, s64 index = 0); + PopupList(const std::string& title, const Items& items, const Callback& cb, const std::string& index); + PopupList(const std::string& title, const Items& items, std::string& index_str_ref, s64& index); + PopupList(const std::string& title, const Items& items, std::string& index_ref); + PopupList(const std::string& title, const Items& items, s64& index_ref); auto Update(Controller* controller, TouchInfo* touch) -> void override; auto Draw(NVGcontext* vg, Theme* theme) -> void override; @@ -33,8 +33,8 @@ private: static constexpr float m_text_xoffset{15.f}; static constexpr float m_line_width{1220.f}; - std::string m_title{}; - Items m_items{}; + const std::string m_title; + const Items m_items; Callback m_callback{}; s64 m_index{}; // index in list array s64 m_starting_index{}; diff --git a/sphaira/include/ui/progress_box.hpp b/sphaira/include/ui/progress_box.hpp index a985588..3ce3b5f 100644 --- a/sphaira/include/ui/progress_box.hpp +++ b/sphaira/include/ui/progress_box.hpp @@ -17,7 +17,7 @@ struct ProgressBox final : Widget { int image, const std::string& action, const std::string& title, - ProgressBoxCallback callback, ProgressBoxDoneCallback done = [](Result rc){}, + const ProgressBoxCallback& callback, const ProgressBoxDoneCallback& done = [](Result rc){}, int cpuid = 1, int prio = PRIO_PREEMPTIVE, int stack_size = 1024*128 ); ~ProgressBox(); diff --git a/sphaira/include/ui/sidebar.hpp b/sphaira/include/ui/sidebar.hpp index 82d24b5..e2e2c86 100644 --- a/sphaira/include/ui/sidebar.hpp +++ b/sphaira/include/ui/sidebar.hpp @@ -59,10 +59,10 @@ protected: } protected: - std::string m_title; + const std::string m_title; private: - std::string m_info{}; + const std::string m_info; std::string m_depends_info{}; DependsCallback m_depends_callback{}; DependsClickCallback m_depends_click{}; @@ -78,7 +78,7 @@ public: using Callback = std::function; public: - explicit SidebarEntryBool(const std::string& title, bool option, Callback cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); + explicit SidebarEntryBool(const std::string& title, bool option, const Callback& cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); explicit SidebarEntryBool(const std::string& title, bool& option, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); explicit SidebarEntryBool(const std::string& title, option::OptionBool& option, const Callback& cb, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); explicit SidebarEntryBool(const std::string& title, option::OptionBool& option, const std::string& info = "", const std::string& true_str = "On", const std::string& false_str = "Off"); @@ -96,13 +96,13 @@ public: using Callback = std::function; public: - explicit SidebarEntryCallback(const std::string& title, Callback cb, const std::string& info); - explicit SidebarEntryCallback(const std::string& title, Callback cb, bool pop_on_click = false, const std::string& info = ""); + explicit SidebarEntryCallback(const std::string& title, const Callback& cb, const std::string& info); + explicit SidebarEntryCallback(const std::string& title, const Callback& cb, bool pop_on_click = false, const std::string& info = ""); void Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) override; private: - Callback m_callback; - bool m_pop_on_click; + const Callback m_callback; + const bool m_pop_on_click; }; class SidebarEntryArray final : public SidebarEntryBase { @@ -112,16 +112,16 @@ public: using Callback = std::function; public: - explicit SidebarEntryArray(const std::string& title, const Items& items, Callback cb, s64 index = 0, const std::string& info = ""); - explicit SidebarEntryArray(const std::string& title, const Items& items, Callback cb, const std::string& index, const std::string& info = ""); + explicit SidebarEntryArray(const std::string& title, const Items& items, const Callback& cb, s64 index = 0, const std::string& info = ""); + explicit SidebarEntryArray(const std::string& title, const Items& items, const Callback& cb, const std::string& index, const std::string& info = ""); explicit SidebarEntryArray(const std::string& title, const Items& items, std::string& index, const std::string& info = ""); void Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, bool left) override; private: - Items m_items; - ListCallback m_list_callback; - Callback m_callback; + const Items m_items; + const Callback m_callback; s64 m_index; + ListCallback m_list_callback{}; }; // single text entry. @@ -204,9 +204,9 @@ private: void SetupButtons(); private: - std::string m_title; - std::string m_sub; - Side m_side; + const std::string m_title; + const std::string m_sub; + const Side m_side; Items m_items; s64 m_index{}; diff --git a/sphaira/include/ui/types.hpp b/sphaira/include/ui/types.hpp index 4275981..86d7115 100644 --- a/sphaira/include/ui/types.hpp +++ b/sphaira/include/ui/types.hpp @@ -56,8 +56,8 @@ struct Vec2 { struct Vec4 { constexpr Vec4() = default; constexpr Vec4(float _x, float _y, float _w, float _h) : x{_x}, y{_y}, w{_w}, h{_h} {} - constexpr Vec4(Vec2 vec0, Vec2 vec1) : x{vec0.x}, y{vec0.y}, w{vec1.x}, h{vec1.y} {} - constexpr Vec4(Vec4 vec0, Vec4 vec1) : x{vec0.x}, y{vec0.y}, w{vec1.w}, h{vec1.h} {} + constexpr Vec4(const Vec2& vec0, const Vec2& vec1) : x{vec0.x}, y{vec0.y}, w{vec1.x}, h{vec1.y} {} + constexpr Vec4(const Vec4& vec0, const Vec4& vec1) : x{vec0.x}, y{vec0.y}, w{vec1.w}, h{vec1.h} {} float& operator[](std::size_t idx) { switch (idx) { @@ -339,10 +339,10 @@ struct Action final { CallbackWithBool >; - Action(Callback cb) : Action{ActionType::DOWN, "", cb} {} - Action(std::string hint, Callback cb) : Action{ActionType::DOWN, hint, cb} {} - Action(u8 type, Callback cb) : Action{type, "", cb} {} - Action(u8 type, std::string hint, Callback cb) : m_type{type}, m_callback{cb}, m_hint{hint} {} + explicit Action(const Callback& cb) : Action{ActionType::DOWN, "", cb} {} + explicit Action(const std::string& hint, const Callback& cb) : Action{ActionType::DOWN, hint, cb} {} + explicit Action(u8 type, const Callback& cb) : Action{type, "", cb} {} + explicit Action(u8 type, const std::string& hint, const Callback& cb) : m_type{type}, m_callback{cb}, m_hint{hint} {} auto IsHidden() const noexcept { return m_hint.empty(); } diff --git a/sphaira/include/ui/widget.hpp b/sphaira/include/ui/widget.hpp index aa9fb71..7e3f3e7 100644 --- a/sphaira/include/ui/widget.hpp +++ b/sphaira/include/ui/widget.hpp @@ -48,8 +48,8 @@ struct Widget : public Object { } auto HasAction(Button button) const -> bool; - void SetAction(Button button, Action action); - void SetActions(std::same_as> auto ...args) { + void SetAction(Button button, const Action& action); + void SetActions(std::same_as> auto&& ...args) { const std::array list = {args...}; for (const auto& [button, action] : list) { SetAction(button, action); diff --git a/sphaira/source/app.cpp b/sphaira/source/app.cpp index ea3638f..6ed7213 100644 --- a/sphaira/source/app.cpp +++ b/sphaira/source/app.cpp @@ -539,7 +539,7 @@ auto App::PopToMenu() -> void { } } -void App::Notify(std::string text, ui::NotifEntry::Side side) { +void App::Notify(const std::string& text, ui::NotifEntry::Side side) { g_app->m_notif_manager.Push({text, side}); } diff --git a/sphaira/source/ftpsrv_helper.cpp b/sphaira/source/ftpsrv_helper.cpp index fab5029..f9e021b 100644 --- a/sphaira/source/ftpsrv_helper.cpp +++ b/sphaira/source/ftpsrv_helper.cpp @@ -406,7 +406,7 @@ void Exit() { } #if ENABLE_NETWORK_INSTALL -void InitInstallMode(OnInstallStart on_start, OnInstallWrite on_write, OnInstallClose on_close) { +void InitInstallMode(const OnInstallStart& on_start, const OnInstallWrite& on_write, const OnInstallClose& on_close) { SCOPED_MUTEX(&g_shared_data.mutex); g_shared_data.on_start = on_start; g_shared_data.on_write = on_write; diff --git a/sphaira/source/haze_helper.cpp b/sphaira/source/haze_helper.cpp index 1e8484b..9372068 100644 --- a/sphaira/source/haze_helper.cpp +++ b/sphaira/source/haze_helper.cpp @@ -610,7 +610,7 @@ void Exit() { } #if ENABLE_NETWORK_INSTALL -void InitInstallMode(OnInstallStart on_start, OnInstallWrite on_write, OnInstallClose on_close) { +void InitInstallMode(const OnInstallStart& on_start, const OnInstallWrite& on_write, const OnInstallClose& on_close) { SCOPED_MUTEX(&g_shared_data.mutex); g_shared_data.on_start = on_start; g_shared_data.on_write = on_write; diff --git a/sphaira/source/threaded_file_transfer.cpp b/sphaira/source/threaded_file_transfer.cpp index 0767c84..64c9af9 100644 --- a/sphaira/source/threaded_file_transfer.cpp +++ b/sphaira/source/threaded_file_transfer.cpp @@ -72,7 +72,7 @@ public: }; struct ThreadData { - ThreadData(ui::ProgressBox* _pbox, s64 size, ReadCallback _rfunc, WriteCallback _wfunc, u64 buffer_size); + ThreadData(ui::ProgressBox* _pbox, s64 size, const ReadCallback& _rfunc, const WriteCallback& _wfunc, u64 buffer_size); auto GetResults() volatile -> Result; void WakeAllThreads(); @@ -131,8 +131,8 @@ private: private: // these need to be copied ui::ProgressBox* const pbox; - const ReadCallback rfunc; - const WriteCallback wfunc; + const ReadCallback& rfunc; + const WriteCallback& wfunc; // these need to be created Mutex mutex{}; @@ -165,7 +165,7 @@ private: std::atomic_bool write_running{true}; }; -ThreadData::ThreadData(ui::ProgressBox* _pbox, s64 size, ReadCallback _rfunc, WriteCallback _wfunc, u64 buffer_size) +ThreadData::ThreadData(ui::ProgressBox* _pbox, s64 size, const ReadCallback& _rfunc, const WriteCallback& _wfunc, u64 buffer_size) : pbox{_pbox} , rfunc{_rfunc} , wfunc{_wfunc} @@ -355,7 +355,7 @@ auto GetAlternateCore(int id) { return id == 1 ? 2 : 1; } -Result TransferInternal(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, WriteCallback wfunc, StartCallback2 sfunc, Mode mode, u64 buffer_size = NORMAL_BUFFER_SIZE) { +Result TransferInternal(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const WriteCallback& wfunc, const StartCallback2& sfunc, Mode mode, u64 buffer_size = NORMAL_BUFFER_SIZE) { const auto is_file_based_emummc = App::IsFileBaseEmummc(); if (is_file_based_emummc) { @@ -480,18 +480,18 @@ Result TransferInternal(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, Wri } // namespace -Result Transfer(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, WriteCallback wfunc, Mode mode) { +Result Transfer(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const WriteCallback& wfunc, Mode mode) { return TransferInternal(pbox, size, rfunc, wfunc, nullptr, mode); } -Result TransferPull(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, StartCallback sfunc, Mode mode) { +Result TransferPull(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const StartCallback& sfunc, Mode mode) { return TransferInternal(pbox, size, rfunc, nullptr, [sfunc](StartThreadCallback start, PullCallback pull) -> Result { R_TRY(start()); return sfunc(pull); }, mode); } -Result TransferPull(ui::ProgressBox* pbox, s64 size, ReadCallback rfunc, StartCallback2 sfunc, Mode mode) { +Result TransferPull(ui::ProgressBox* pbox, s64 size, const ReadCallback& rfunc, const StartCallback2& sfunc, Mode mode) { return TransferInternal(pbox, size, rfunc, nullptr, sfunc, mode); } @@ -575,7 +575,7 @@ Result TransferZip(ui::ProgressBox* pbox, void* zfile, fs::Fs* fs, const fs::FsP ); } -Result TransferUnzipAll(ui::ProgressBox* pbox, void* zfile, fs::Fs* fs, const fs::FsPath& base_path, UnzipAllFilter filter, Mode mode) { +Result TransferUnzipAll(ui::ProgressBox* pbox, void* zfile, fs::Fs* fs, const fs::FsPath& base_path, const UnzipAllFilter& filter, Mode mode) { unz_global_info64 ginfo; if (UNZ_OK != unzGetGlobalInfo64(zfile, &ginfo)) { R_THROW(Result_UnzGetGlobalInfo64); @@ -632,7 +632,7 @@ Result TransferUnzipAll(ui::ProgressBox* pbox, void* zfile, fs::Fs* fs, const fs R_SUCCEED(); } -Result TransferUnzipAll(ui::ProgressBox* pbox, const fs::FsPath& zip_out, fs::Fs* fs, const fs::FsPath& base_path, UnzipAllFilter filter, Mode mode) { +Result TransferUnzipAll(ui::ProgressBox* pbox, const fs::FsPath& zip_out, fs::Fs* fs, const fs::FsPath& base_path, const UnzipAllFilter& filter, Mode mode) { zlib_filefunc64_def file_func; mz::FileFuncStdio(&file_func); diff --git a/sphaira/source/ui/list.cpp b/sphaira/source/ui/list.cpp index aa8683d..46e460d 100644 --- a/sphaira/source/ui/list.cpp +++ b/sphaira/source/ui/list.cpp @@ -35,7 +35,7 @@ auto List::ClampY(float y, s64 count) const -> float { return std::clamp(y, 0.F, y_max); } -void List::OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback) { +void List::OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback) { switch (m_layout) { case Layout::HOME: OnUpdateHome(controller, touch, index, count, callback); @@ -46,7 +46,7 @@ void List::OnUpdate(Controller* controller, TouchInfo* touch, s64 index, s64 cou } } -void List::Draw(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const { +void List::Draw(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const { switch (m_layout) { case Layout::HOME: DrawHome(vg, theme, count, callback); @@ -129,7 +129,7 @@ auto List::ScrollUp(s64& index, s64 step, s64 count) -> bool { return false; } -void List::OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback) { +void List::OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback) { if (controller->GotDown(Button::RIGHT)) { if (ScrollDown(index, m_row, count)) { callback(false, index); @@ -165,7 +165,7 @@ void List::OnUpdateHome(Controller* controller, TouchInfo* touch, s64 index, s64 } } -void List::OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 count, TouchCallback callback) { +void List::OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 count, const TouchCallback& callback) { const auto page_up_button = GetPageJump() ? (m_row == 1 ? Button::DPAD_LEFT : Button::L2) : (Button::NONE); const auto page_down_button = GetPageJump() ? (m_row == 1 ? Button::DPAD_RIGHT : Button::R2) : (Button::NONE); @@ -236,7 +236,7 @@ void List::OnUpdateGrid(Controller* controller, TouchInfo* touch, s64 index, s64 } } -void List::DrawHome(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const { +void List::DrawHome(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const { const auto yoff = ClampX(m_yoff + m_y_prog, count); auto v = m_v; v.x -= yoff; @@ -260,7 +260,7 @@ void List::DrawHome(NVGcontext* vg, Theme* theme, s64 count, Callback callback) nvgRestore(vg); } -void List::DrawGrid(NVGcontext* vg, Theme* theme, s64 count, Callback callback) const { +void List::DrawGrid(NVGcontext* vg, Theme* theme, s64 count, const Callback& callback) const { const auto yoff = ClampY(m_yoff + m_y_prog, count); const s64 start = yoff / GetMaxY() * m_row; gfx::drawScrollbar2(vg, theme, m_scrollbar.x, m_scrollbar.y, m_scrollbar.h, start, count, m_row, m_page); diff --git a/sphaira/source/ui/menus/appstore.cpp b/sphaira/source/ui/menus/appstore.cpp index 7f25807..036f70f 100644 --- a/sphaira/source/ui/menus/appstore.cpp +++ b/sphaira/source/ui/menus/appstore.cpp @@ -288,7 +288,7 @@ void DrawIcon(NVGcontext* vg, const LazyImage& l, const LazyImage& d, float x, f } } -void DrawIcon(NVGcontext* vg, const LazyImage& l, const LazyImage& d, Vec4 vec, bool rounded = true, float scale = 1.0) { +void DrawIcon(NVGcontext* vg, const LazyImage& l, const LazyImage& d, const Vec4& vec, bool rounded = true, float scale = 1.0) { DrawIcon(vg, l, d, vec.x, vec.y, vec.w, vec.h, rounded, scale); } diff --git a/sphaira/source/ui/menus/file_picker.cpp b/sphaira/source/ui/menus/file_picker.cpp index 9daabf4..097430f 100644 --- a/sphaira/source/ui/menus/file_picker.cpp +++ b/sphaira/source/ui/menus/file_picker.cpp @@ -444,7 +444,7 @@ void Menu::Draw(NVGcontext* vg, Theme* theme) { constexpr float text_xoffset{15.f}; bool got_dir_count = false; - m_list->Draw(vg, theme, m_entries_current.size(), [this, text_col, &got_dir_count](auto* vg, auto* theme, auto v, auto i) { + m_list->Draw(vg, theme, m_entries_current.size(), [this, text_col, &got_dir_count](auto* vg, auto* theme, auto& v, auto i) { const auto& [x, y, w, h] = v; auto& e = GetEntry(i); diff --git a/sphaira/source/ui/menus/filebrowser.cpp b/sphaira/source/ui/menus/filebrowser.cpp index 34a59a6..d286a0a 100644 --- a/sphaira/source/ui/menus/filebrowser.cpp +++ b/sphaira/source/ui/menus/filebrowser.cpp @@ -568,7 +568,7 @@ void FsView::Draw(NVGcontext* vg, Theme* theme) { constexpr float text_xoffset{15.f}; bool got_dir_count = false; - m_list->Draw(vg, theme, m_entries_current.size(), [this, text_col, &got_dir_count](auto* vg, auto* theme, auto v, auto i) { + m_list->Draw(vg, theme, m_entries_current.size(), [this, text_col, &got_dir_count](auto* vg, auto* theme, auto& v, auto i) { const auto& [x, y, w, h] = v; auto& e = GetEntry(i); diff --git a/sphaira/source/ui/menus/gc_menu.cpp b/sphaira/source/ui/menus/gc_menu.cpp index 1f7a745..e00d1d7 100644 --- a/sphaira/source/ui/menus/gc_menu.cpp +++ b/sphaira/source/ui/menus/gc_menu.cpp @@ -502,7 +502,7 @@ void Menu::Draw(NVGcontext* vg, Theme* theme) { nvgRestore(vg); } - m_list->Draw(vg, theme, std::size(g_option_list), [this](auto* vg, auto* theme, auto v, auto i) { + m_list->Draw(vg, theme, std::size(g_option_list), [this](auto* vg, auto* theme, auto& v, auto i) { const auto& [x, y, w, h] = v; const auto text_y = y + (h / 2.f); auto colour = ThemeEntryID_TEXT; diff --git a/sphaira/source/ui/menus/ghdl.cpp b/sphaira/source/ui/menus/ghdl.cpp index bdd6ec0..2d081b0 100644 --- a/sphaira/source/ui/menus/ghdl.cpp +++ b/sphaira/source/ui/menus/ghdl.cpp @@ -215,7 +215,7 @@ void Menu::Draw(NVGcontext* vg, Theme* theme) { constexpr float text_xoffset{15.f}; - m_list->Draw(vg, theme, m_entries.size(), [this, text_col](auto* vg, auto* theme, auto v, auto i) { + m_list->Draw(vg, theme, m_entries.size(), [this, text_col](auto* vg, auto* theme, auto& v, auto i) { const auto& [x, y, w, h] = v; auto& e = m_entries[i]; diff --git a/sphaira/source/ui/menus/menu_base.cpp b/sphaira/source/ui/menus/menu_base.cpp index 4cabb91..87e731f 100644 --- a/sphaira/source/ui/menus/menu_base.cpp +++ b/sphaira/source/ui/menus/menu_base.cpp @@ -110,16 +110,4 @@ void MenuBase::Draw(NVGcontext* vg, Theme* theme) { m_scroll_sub_heading.Draw(vg, true, 80, 685, text_w - 160, 18, NVG_ALIGN_LEFT, theme->GetColour(ThemeEntryID_TEXT), m_sub_heading.c_str()); } -void MenuBase::SetTitle(std::string title) { - m_title = title; -} - -void MenuBase::SetTitleSubHeading(std::string sub_heading) { - m_title_sub_heading = sub_heading; -} - -void MenuBase::SetSubHeading(std::string sub_heading) { - m_sub_heading = sub_heading; -} - } // namespace sphaira::ui::menu diff --git a/sphaira/source/ui/notification.cpp b/sphaira/source/ui/notification.cpp index 1b9de71..365db5c 100644 --- a/sphaira/source/ui/notification.cpp +++ b/sphaira/source/ui/notification.cpp @@ -11,8 +11,8 @@ constexpr u64 MAX_ENTRIES = 9; } // namespace -NotifEntry::NotifEntry(std::string text, Side side) -: m_text{std::move(text)} +NotifEntry::NotifEntry(const std::string& text, Side side) +: m_text{text} , m_side{side} { } diff --git a/sphaira/source/ui/option_box.cpp b/sphaira/source/ui/option_box.cpp index 38c47b0..da0facb 100644 --- a/sphaira/source/ui/option_box.cpp +++ b/sphaira/source/ui/option_box.cpp @@ -4,7 +4,7 @@ namespace sphaira::ui { -OptionBoxEntry::OptionBoxEntry(const std::string& text, Vec4 pos) +OptionBoxEntry::OptionBoxEntry(const std::string& text, const Vec4& pos) : m_text{text} { m_pos = pos; m_text_pos = Vec2{m_pos.x + (m_pos.w / 2.f), m_pos.y + (m_pos.h / 2.f)}; diff --git a/sphaira/source/ui/popup_list.cpp b/sphaira/source/ui/popup_list.cpp index 58bf754..c53cfa9 100644 --- a/sphaira/source/ui/popup_list.cpp +++ b/sphaira/source/ui/popup_list.cpp @@ -6,8 +6,8 @@ namespace sphaira::ui { -PopupList::PopupList(std::string title, Items items, std::string& index_str_ref, s64& index_ref) -: PopupList{std::move(title), std::move(items), Callback{}, index_ref} { +PopupList::PopupList(const std::string& title, const Items& items, std::string& index_str_ref, s64& index_ref) +: PopupList{title, items, Callback{}, index_ref} { m_callback = [&index_str_ref, &index_ref, this](auto op_idx) { if (op_idx) { @@ -17,8 +17,8 @@ PopupList::PopupList(std::string title, Items items, std::string& index_str_ref, }; } -PopupList::PopupList(std::string title, Items items, std::string& index_ref) -: PopupList{std::move(title), std::move(items), Callback{}} { +PopupList::PopupList(const std::string& title, const Items& items, std::string& index_ref) +: PopupList{title, items, Callback{}} { const auto it = std::find(m_items.cbegin(), m_items.cend(), index_ref); if (it != m_items.cend()) { @@ -35,8 +35,8 @@ PopupList::PopupList(std::string title, Items items, std::string& index_ref) }; } -PopupList::PopupList(std::string title, Items items, s64& index_ref) -: PopupList{std::move(title), std::move(items), Callback{}, index_ref} { +PopupList::PopupList(const std::string& title, const Items& items, s64& index_ref) +: PopupList{title, items, Callback{}, index_ref} { m_callback = [&index_ref, this](auto op_idx) { if (op_idx) { @@ -45,8 +45,8 @@ PopupList::PopupList(std::string title, Items items, s64& index_ref) }; } -PopupList::PopupList(std::string title, Items items, Callback cb, std::string index) -: PopupList{std::move(title), std::move(items), cb, 0} { +PopupList::PopupList(const std::string& title, const Items& items, const Callback& cb, const std::string& index) +: PopupList{title, items, cb, 0} { const auto it = std::find(m_items.cbegin(), m_items.cend(), index); if (it != m_items.cend()) { @@ -57,9 +57,9 @@ PopupList::PopupList(std::string title, Items items, Callback cb, std::string in } } -PopupList::PopupList(std::string title, Items items, Callback cb, s64 index) -: m_title{std::move(title)} -, m_items{std::move(items)} +PopupList::PopupList(const std::string& title, const Items& items, const Callback& cb, s64 index) +: m_title{title} +, m_items{items} , m_callback{cb} , m_index{index} { this->SetActions( @@ -111,7 +111,7 @@ auto PopupList::Draw(NVGcontext* vg, Theme* theme) -> void { gfx::drawRect(vg, 30.f, m_line_top, m_line_width, 1.f, theme->GetColour(ThemeEntryID_LINE)); gfx::drawRect(vg, 30.f, m_line_bottom, m_line_width, 1.f, theme->GetColour(ThemeEntryID_LINE)); - m_list->Draw(vg, theme, m_items.size(), [this](auto* vg, auto* theme, auto v, auto i) { + m_list->Draw(vg, theme, m_items.size(), [this](auto* vg, auto* theme, auto& v, auto i) { const auto& [x, y, w, h] = v; auto colour = ThemeEntryID_TEXT; const auto selected = m_index == i; diff --git a/sphaira/source/ui/progress_box.cpp b/sphaira/source/ui/progress_box.cpp index bb5882e..b8f321a 100644 --- a/sphaira/source/ui/progress_box.cpp +++ b/sphaira/source/ui/progress_box.cpp @@ -19,7 +19,11 @@ void threadFunc(void* arg) { } // namespace -ProgressBox::ProgressBox(int image, const std::string& action, const std::string& title, ProgressBoxCallback callback, ProgressBoxDoneCallback done, int cpuid, int prio, int stack_size) { +ProgressBox::ProgressBox(int image, const std::string& action, const std::string& title, const ProgressBoxCallback& callback, const ProgressBoxDoneCallback& done, int cpuid, int prio, int stack_size) +: m_done{done} +, m_action{action} +, m_title{title} +, m_image{image} { if (App::GetApp()->m_progress_boost_mode.Get()) { App::SetBoostMode(true); } @@ -38,11 +42,6 @@ ProgressBox::ProgressBox(int image, const std::string& action, const std::string m_pos.x = (SCREEN_WIDTH / 2.f) - (m_pos.w / 2.f); m_pos.y = (SCREEN_HEIGHT / 2.f) - (m_pos.h / 2.f); - m_done = done; - m_title = title; - m_action = action; - m_image = image; - // create cancel event. ueventCreate(&m_uevent, false); diff --git a/sphaira/source/ui/sidebar.cpp b/sphaira/source/ui/sidebar.cpp index 8b2f664..d014562 100644 --- a/sphaira/source/ui/sidebar.cpp +++ b/sphaira/source/ui/sidebar.cpp @@ -10,7 +10,7 @@ namespace sphaira::ui { namespace { -auto DistanceBetweenY(Vec4 va, Vec4 vb) -> Vec4 { +auto DistanceBetweenY(const Vec4& va, const Vec4& vb) -> Vec4 { return Vec4{ va.x, va.y, va.w, vb.y - va.y @@ -104,7 +104,7 @@ void SidebarEntryBase::DrawEntry(NVGcontext* vg, Theme* theme, const std::string m_scolling_value.Draw(vg, HasFocus(), xpos, ypos, max_off, 20.f, NVG_ALIGN_LEFT | NVG_ALIGN_MIDDLE, theme->GetColour(value_id), right); } -SidebarEntryBool::SidebarEntryBool(const std::string& title, bool option, Callback cb, const std::string& info, const std::string& true_str, const std::string& false_str) +SidebarEntryBool::SidebarEntryBool(const std::string& title, bool option, const Callback& cb, const std::string& info, const std::string& true_str, const std::string& false_str) : SidebarEntryBase{title, info} , m_option{option} , m_callback{cb} @@ -154,7 +154,7 @@ void SidebarEntryBool::Draw(NVGcontext* vg, Theme* theme, const Vec4& root_pos, SidebarEntryBase::DrawEntry(vg, theme, m_title, m_option ? m_true_str : m_false_str, m_option); } -SidebarEntryCallback::SidebarEntryCallback(const std::string& title, Callback cb, bool pop_on_click, const std::string& info) +SidebarEntryCallback::SidebarEntryCallback(const std::string& title, const Callback& cb, bool pop_on_click, const std::string& info) : SidebarEntryBase{title, info} , m_callback{cb} , m_pop_on_click{pop_on_click} { @@ -170,7 +170,7 @@ SidebarEntryCallback::SidebarEntryCallback(const std::string& title, Callback cb }); } -SidebarEntryCallback::SidebarEntryCallback(const std::string& title, Callback cb, const std::string& info) +SidebarEntryCallback::SidebarEntryCallback(const std::string& title, const Callback& cb, const std::string& info) : SidebarEntryCallback{title, cb, false, info} { } @@ -197,7 +197,7 @@ SidebarEntryArray::SidebarEntryArray(const std::string& title, const Items& item }; } -SidebarEntryArray::SidebarEntryArray(const std::string& title, const Items& items, Callback cb, const std::string& index, const std::string& info) +SidebarEntryArray::SidebarEntryArray(const std::string& title, const Items& items, const Callback& cb, const std::string& index, const std::string& info) : SidebarEntryArray{title, items, cb, 0, info} { const auto it = std::find(m_items.cbegin(), m_items.cend(), index); @@ -206,7 +206,7 @@ SidebarEntryArray::SidebarEntryArray(const std::string& title, const Items& item } } -SidebarEntryArray::SidebarEntryArray(const std::string& title, const Items& items, Callback cb, s64 index, const std::string& info) +SidebarEntryArray::SidebarEntryArray(const std::string& title, const Items& items, const Callback& cb, s64 index, const std::string& info) : SidebarEntryBase{title, info} , m_items{items} , m_callback{cb} @@ -355,7 +355,7 @@ auto Sidebar::Draw(NVGcontext* vg, Theme* theme) -> void { Widget::Draw(vg, theme); - m_list->Draw(vg, theme, m_items.size(), [this](auto* vg, auto* theme, auto v, auto i) { + m_list->Draw(vg, theme, m_items.size(), [this](auto* vg, auto* theme, auto& v, auto i) { const auto& [x, y, w, h] = v; if (i != m_items.size() - 1) { diff --git a/sphaira/source/ui/widget.cpp b/sphaira/source/ui/widget.cpp index 6afebe2..571fc5b 100644 --- a/sphaira/source/ui/widget.cpp +++ b/sphaira/source/ui/widget.cpp @@ -70,7 +70,7 @@ auto Widget::HasAction(Button button) const -> bool { return m_actions.contains(button); } -void Widget::SetAction(Button button, Action action) { +void Widget::SetAction(Button button, const Action& action) { m_actions.insert_or_assign(button, action); }