From dcb23f8b4b889061e96d43949d570581469cab84 Mon Sep 17 00:00:00 2001 From: hachem Date: Fri, 4 Sep 2026 07:04:49 +0200 Subject: refactor: slight cleanup --- src/ui/animated_image.cpp | 26 +++++++++---------- src/ui/macro_expander.cpp | 26 ++++++++++++------- src/ui/profiler.cpp | 6 ++--- src/ui/profiler.h | 2 +- src/ui/source_view.cpp | 52 +++++++++++++++---------------------- src/ui/source_view.h | 2 +- src/ui/ui.cpp | 66 +++++++++++++++++++++++------------------------ src/ui/ui.h | 12 ++++----- 8 files changed, 94 insertions(+), 98 deletions(-) (limited to 'src/ui') diff --git a/src/ui/animated_image.cpp b/src/ui/animated_image.cpp index d7c6536..94ad8bc 100644 --- a/src/ui/animated_image.cpp +++ b/src/ui/animated_image.cpp @@ -130,25 +130,25 @@ namespace Hsdbg } const auto length = static_cast(bytes.size()); - int width = 0; - int height = 0; + int pixel_width = 0; + int pixel_height = 0; int comp = 0; int frames = 0; int* delays = nullptr; if (stbi_uc* data = stbi_load_gif_from_memory(bytes.data(), length, &delays, - &width, &height, &frames, &comp, 4)) + &pixel_width, &pixel_height, &frames, &comp, 4)) { - m_width = width; - m_height = height; + m_width = pixel_width; + m_height = pixel_height; for (int index = 0; index < frames; ++index) { - stbi_uc* frame = data + static_cast(index) * width * height * 4; + stbi_uc* frame = data + static_cast(index) * pixel_width * pixel_height * 4; - bleed_edges(frame, width, height); - m_frames.push_back(upload(frame, width, height)); + bleed_edges(frame, pixel_width, pixel_height); + m_frames.push_back(upload(frame, pixel_width, pixel_height)); const float seconds = delays != nullptr ? static_cast(delays[index]) / 1000.0f @@ -161,12 +161,12 @@ namespace Hsdbg std::free(delays); } else if (stbi_uc* still = stbi_load_from_memory(bytes.data(), length, - &width, &height, &comp, 4)) + &pixel_width, &pixel_height, &comp, 4)) { - m_width = width; - m_height = height; - bleed_edges(still, width, height); - m_frames.push_back(upload(still, width, height)); + m_width = pixel_width; + m_height = pixel_height; + bleed_edges(still, pixel_width, pixel_height); + m_frames.push_back(upload(still, pixel_width, pixel_height)); m_delays.push_back(MIN_FRAME_SECONDS); stbi_image_free(still); } diff --git a/src/ui/macro_expander.cpp b/src/ui/macro_expander.cpp index f4588c3..a2ca62c 100644 --- a/src/ui/macro_expander.cpp +++ b/src/ui/macro_expander.cpp @@ -3,6 +3,7 @@ #include #include #include +#include namespace Hsdbg { @@ -111,15 +112,17 @@ namespace Hsdbg "^=", "++", "--", ".*", }; - for (const std::string_view op : multis) + const auto multi = std::ranges::find_if(multis, [&](std::string_view op) { - if (text.compare(at, op.size(), op) == 0) - { - at += op.size(); - token.kind = PpKind::Punct; - token.text = std::string(op); - return token; - } + return text.compare(at, op.size(), op) == 0; + }); + + if (multi != std::end(multis)) + { + at += multi->size(); + token.kind = PpKind::Punct; + token.text = std::string(*multi); + return token; } token.kind = PpKind::Punct; @@ -508,8 +511,11 @@ namespace Hsdbg ArgList expanded_args; expanded_args.reserve(args.size()); - for (const std::vector& arg : args) - expanded_args.push_back(expand_full(table, arg)); + std::ranges::transform(args, std::back_inserter(expanded_args), + [&](const std::vector& arg) + { + return expand_full(table, arg); + }); std::vector repl = substitute(*def, args, expanded_args); diff --git a/src/ui/profiler.cpp b/src/ui/profiler.cpp index df22a20..b917704 100644 --- a/src/ui/profiler.cpp +++ b/src/ui/profiler.cpp @@ -1,5 +1,7 @@ #include "ui/profiler.h" +#include + namespace Hsdbg { auto TimeSeries::push(float value) -> void @@ -32,9 +34,7 @@ namespace Hsdbg if (m_count == 0) return 0.0f; - float sum = 0.0f; - for (int i = 0; i < m_count; ++i) - sum += m_values[i]; + const float sum = std::accumulate(m_values.begin(), m_values.begin() + m_count, 0.0f); return sum / static_cast(m_count); } diff --git a/src/ui/profiler.h b/src/ui/profiler.h index 0ceb740..db16f8e 100644 --- a/src/ui/profiler.h +++ b/src/ui/profiler.h @@ -50,7 +50,7 @@ namespace Hsdbg auto target_memory_mb() const -> const TimeSeries& { return m_target_memory_mb; } auto paused() const -> bool { return m_paused; } - auto set_paused(bool paused) -> void { m_paused = paused; } + auto set_paused(bool value) -> void { m_paused = value; } private: TimeSeries m_frame_ms; diff --git a/src/ui/source_view.cpp b/src/ui/source_view.cpp index 7638f17..0a6a230 100644 --- a/src/ui/source_view.cpp +++ b/src/ui/source_view.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -91,13 +92,10 @@ namespace Hsdbg auto only_space_before(const std::string& line, uint32_t upto) -> bool { - for (uint32_t index = 0; index < upto; ++index) + return std::ranges::all_of(line.begin(), line.begin() + upto, [](char character) { - if (std::isspace(static_cast(line[index])) == 0) - return false; - } - - return true; + return std::isspace(static_cast(character)) != 0; + }); } // splits every line into contiguous coloured spans. the whole file is @@ -372,11 +370,11 @@ namespace Hsdbg } } - auto SourceView::open(const std::filesystem::path& path) -> Result + auto SourceView::open(const std::filesystem::path& file_path) -> Result { - std::ifstream file(path); + std::ifstream file(file_path); if (!file.is_open()) - return fail("could not open '{}'", path.string()); + return fail("could not open '{}'", file_path.string()); std::vector lines; std::string line; @@ -389,9 +387,9 @@ namespace Hsdbg lines.push_back(std::move(line)); } - const std::optional language = language_of(path); + const std::optional language = language_of(file_path); - m_path = path; + m_path = file_path; m_lines = std::move(lines); m_highlight = language.has_value(); @@ -400,11 +398,11 @@ namespace Hsdbg m_macros.clear(); if (language == Language::Cpp) - m_macros.build(m_lines, path.parent_path()); + m_macros.build(m_lines, file_path.parent_path()); m_spans = language ? highlight_lines(m_lines, *language, m_macros) : std::vector>{}; - m_path_input = path.string(); + m_path_input = file_path.string(); m_error.clear(); m_highlighted_line = 0; @@ -519,16 +517,12 @@ namespace Hsdbg if (ImGui::InvisibleButton("##gutter", ImVec2(GUTTER_WIDTH, text_height))) { - const Breakpoint* existing = nullptr; - - for (const Breakpoint& breakpoint : debugger.breakpoints()) + const std::span breakpoints = debugger.breakpoints(); + const auto found = std::ranges::find_if(breakpoints, [&](const Breakpoint& candidate) { - if (breakpoint.file == m_path && breakpoint.line == line_number) - { - existing = &breakpoint; - break; - } - } + return candidate.file == m_path && candidate.line == line_number; + }); + const Breakpoint* existing = found != breakpoints.end() ? &*found : nullptr; if (existing != nullptr) debugger.remove_breakpoint(existing->id); @@ -540,16 +534,12 @@ namespace Hsdbg ImGui::PopID(); - const Breakpoint* breakpoint = nullptr; - - for (const Breakpoint& candidate : debugger.breakpoints()) + const std::span breakpoints = debugger.breakpoints(); + const auto found = std::ranges::find_if(breakpoints, [&](const Breakpoint& candidate) { - if (candidate.file == m_path && candidate.line == line_number) - { - breakpoint = &candidate; - break; - } - } + return candidate.file == m_path && candidate.line == line_number; + }); + const Breakpoint* breakpoint = found != breakpoints.end() ? &*found : nullptr; const ImVec2 marker_center(row_start.x + GUTTER_WIDTH * 0.5f, row_start.y + text_height * 0.5f); diff --git a/src/ui/source_view.h b/src/ui/source_view.h index c8a9a21..0c10efa 100644 --- a/src/ui/source_view.h +++ b/src/ui/source_view.h @@ -39,7 +39,7 @@ namespace Hsdbg class SourceView { public: - auto open(const std::filesystem::path& path) -> Result; + auto open(const std::filesystem::path& file_path) -> Result; auto close() -> void; auto draw(Debugger& debugger) -> void; diff --git a/src/ui/ui.cpp b/src/ui/ui.cpp index f39b95f..14fd1d8 100644 --- a/src/ui/ui.cpp +++ b/src/ui/ui.cpp @@ -73,21 +73,21 @@ namespace Hsdbg // the dim connectors drawn between a folder and its children const ImU32 SOURCE_TREE_LINE_COLOR = IM_COL32(110, 110, 122, 160); - auto breakpoint_at(Debugger& debugger, const Instruction& instruction) -> const Breakpoint* + auto breakpoint_at(const Debugger& debugger, const Instruction& instruction) -> const Breakpoint* { - for (const Breakpoint& candidate : debugger.breakpoints()) + const std::span breakpoints = debugger.breakpoints(); + const auto found = std::ranges::find_if(breakpoints, [&](const Breakpoint& candidate) { if (instruction.file_address != 0 && candidate.file_address == instruction.file_address) { - return &candidate; + return true; } - if (candidate.address != 0 && candidate.address == instruction.address) - return &candidate; - } + return candidate.address != 0 && candidate.address == instruction.address; + }); - return nullptr; + return found != breakpoints.end() ? &*found : nullptr; } auto toggle_instruction_breakpoint(Debugger& debugger, const Instruction& instruction) -> void @@ -128,11 +128,10 @@ namespace Hsdbg auto find_or_add_child(std::vector& nodes, std::string_view name) -> SourceNode& { - for (SourceNode& node : nodes) - { - if (node.name == name) - return node; - } + const auto existing = std::ranges::find(nodes, name, &SourceNode::name); + + if (existing != nodes.end()) + return *existing; nodes.push_back({ std::string(name), {}, {} }); return nodes.back(); @@ -242,19 +241,20 @@ namespace Hsdbg auto preferred_source(std::span files) -> std::filesystem::path { - for (const std::filesystem::path& file : files) + const auto named_main = std::ranges::find_if(files, [](const std::filesystem::path& file) { - if (file.stem() == "main" && std::filesystem::exists(file)) - return file; - } + return file.stem() == "main" && std::filesystem::exists(file); + }); - for (const std::filesystem::path& file : files) + if (named_main != files.end()) + return *named_main; + + const auto existing = std::ranges::find_if(files, [](const std::filesystem::path& file) { - if (std::filesystem::exists(file)) - return file; - } + return std::filesystem::exists(file); + }); - return {}; + return existing != files.end() ? *existing : std::filesystem::path{}; } auto draw_instruction_table(Debugger& debugger, @@ -913,7 +913,7 @@ namespace Hsdbg ImGui::PopStyleVar(2); } - auto Ui::draw_status_bar(Debugger& debugger) -> void + auto Ui::draw_status_bar(const Debugger& debugger) -> void { ImGui::Separator(); @@ -1376,7 +1376,7 @@ namespace Hsdbg ImGui::End(); } - auto Ui::draw_source_tree_panel(Debugger& debugger) -> void + auto Ui::draw_source_tree_panel(const Debugger& debugger) -> void { if (!m_visible.source_tree) return; @@ -1485,7 +1485,7 @@ namespace Hsdbg ImGui::End(); } - auto Ui::draw_locals_panel(Debugger& debugger) -> void + auto Ui::draw_locals_panel(const Debugger& debugger) -> void { if (!m_visible.locals) return; @@ -1520,7 +1520,7 @@ namespace Hsdbg ImGui::End(); } - auto Ui::draw_registers_panel(Debugger& debugger) -> void + auto Ui::draw_registers_panel(const Debugger& debugger) -> void { if (!m_visible.registers) return; @@ -1944,8 +1944,8 @@ namespace Hsdbg ImGui::InvisibleButton("##timeline_canvas", ImVec2(canvas_w, canvas_h)); const bool canvas_hovered = ImGui::IsItemHovered(); - ImDrawList* draw = ImGui::GetWindowDrawList(); - draw->PushClipRect(origin, ImVec2(origin.x + canvas_w, origin.y + canvas_h), true); + ImDrawList* draw_list = ImGui::GetWindowDrawList(); + draw_list->PushClipRect(origin, ImVec2(origin.x + canvas_w, origin.y + canvas_h), true); const float scale = canvas_w / static_cast(range); // pixels per second const float baseline = origin.y + canvas_h; // row 0 rests on the bottom @@ -1961,15 +1961,15 @@ namespace Hsdbg const ImU32 fill = ImColor::HSV(span.trace_id * 0.13f, 0.55f, 0.78f); - draw->AddRectFilled(ImVec2(x0, y0), ImVec2(x0 + width, y1), fill, 2.0f); - draw->AddRect(ImVec2(x0, y0), ImVec2(x0 + width, y1), IM_COL32(0, 0, 0, 90), 2.0f); + draw_list->AddRectFilled(ImVec2(x0, y0), ImVec2(x0 + width, y1), fill, 2.0f); + draw_list->AddRect(ImVec2(x0, y0), ImVec2(x0 + width, y1), IM_COL32(0, 0, 0, 90), 2.0f); if (width > 24.0f) { - draw->PushClipRect(ImVec2(x0 + 2.0f, y0), ImVec2(x0 + width - 2.0f, y1), true); - draw->AddText(ImVec2(x0 + 4.0f, y0 + 2.0f), IM_COL32(20, 20, 20, 255), + draw_list->PushClipRect(ImVec2(x0 + 2.0f, y0), ImVec2(x0 + width - 2.0f, y1), true); + draw_list->AddText(ImVec2(x0 + 4.0f, y0 + 2.0f), IM_COL32(20, 20, 20, 255), name_of(span.trace_id)); - draw->PopClipRect(); + draw_list->PopClipRect(); } const bool over = canvas_hovered && mouse.x >= x0 && mouse.x <= x0 + width && @@ -1988,7 +1988,7 @@ namespace Hsdbg } } - draw->PopClipRect(); + draw_list->PopClipRect(); } } diff --git a/src/ui/ui.h b/src/ui/ui.h index 70cf691..3365e51 100644 --- a/src/ui/ui.h +++ b/src/ui/ui.h @@ -30,7 +30,7 @@ namespace Hsdbg auto operator=(const Ui&) -> Ui& = delete; auto operator=(Ui&&) -> Ui& = delete; - auto begin_frame() -> void; + static auto begin_frame() -> void; auto draw(Debugger& debugger) -> void; auto end_frame() -> void; @@ -57,11 +57,11 @@ namespace Hsdbg }; auto apply_style() -> void; - auto build_default_layout(uint32_t dockspace_id) -> void; + static auto build_default_layout(uint32_t dockspace_id) -> void; auto draw_menu_bar(Debugger& debugger) -> void; auto draw_toolbar(Debugger& debugger) -> void; - auto draw_status_bar(Debugger& debugger) -> void; + auto draw_status_bar(const Debugger& debugger) -> void; auto draw_load_target_popup(Debugger& debugger) -> void; auto draw_preferences_window() -> void; auto apply_preferences() -> void; @@ -70,9 +70,9 @@ namespace Hsdbg auto draw_breakpoints_panel(Debugger& debugger) -> void; auto draw_call_stack_panel(Debugger& debugger) -> void; auto draw_threads_panel(Debugger& debugger) -> void; - auto draw_source_tree_panel(Debugger& debugger) -> void; - auto draw_locals_panel(Debugger& debugger) -> void; - auto draw_registers_panel(Debugger& debugger) -> void; + auto draw_source_tree_panel(const Debugger& debugger) -> void; + auto draw_locals_panel(const Debugger& debugger) -> void; + auto draw_registers_panel(const Debugger& debugger) -> void; auto draw_symbols_panel(Debugger& debugger) -> void; auto draw_disassembly_panel(Debugger& debugger) -> void; auto draw_console_panel(Debugger& debugger) -> void; -- cgit v1.3