| 1 | // Copyright 2018 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "Common/Debug/Watches.h" |
| 5 | |
| 6 | #include <algorithm> |
| 7 | #include <locale> |
| 8 | #include <sstream> |
| 9 | |
| 10 | #include <fmt/format.h> |
| 11 | |
| 12 | namespace Common::Debug |
| 13 | { |
| 14 | Watch::Watch(u32 address_, std::string name_, State is_enabled_) |
| 15 | : address(address_), name(std::move(name_)), is_enabled(is_enabled_) |
| 16 | { |
| 17 | } |
| 18 | |
| 19 | std::size_t Watches::SetWatch(u32 address, std::string name) |
| 20 | { |
| 21 | const std::size_t size = m_watches.size(); |
| 22 | for (std::size_t index = 0; index < size; index++) |
| 23 | { |
| 24 | if (m_watches.at(index).address == address) |
| 25 | { |
| 26 | UpdateWatch(index, address, name); |
| 27 | return index; |
| 28 | } |
| 29 | } |
| 30 | m_watches.emplace_back(address, std::move(name), Watch::State::Enabled); |
| 31 | return size; |
| 32 | } |
| 33 | |
| 34 | const Watch& Watches::GetWatch(std::size_t index) const |
| 35 | { |
| 36 | return m_watches.at(index); |
| 37 | } |
| 38 | |
| 39 | const std::vector<Watch>& Watches::GetWatches() const |
| 40 | { |
| 41 | return m_watches; |
| 42 | } |
| 43 | |
| 44 | void Watches::UnsetWatch(u32 address) |
| 45 | { |
| 46 | std::erase_if(m_watches, [address](const auto& watch) { return watch.address == address; }); |
| 47 | } |
| 48 | |
| 49 | void Watches::UpdateWatch(std::size_t index, u32 address, std::string name) |
| 50 | { |
| 51 | m_watches[index].address = address; |
| 52 | m_watches[index].name = std::move(name); |
| 53 | } |
| 54 | |
| 55 | void Watches::UpdateWatchAddress(std::size_t index, u32 address) |
| 56 | { |
| 57 | m_watches[index].address = address; |
| 58 | } |
| 59 | |
| 60 | void Watches::UpdateWatchName(std::size_t index, std::string name) |
| 61 | { |
| 62 | m_watches[index].name = std::move(name); |
| 63 | } |
| 64 | |
| 65 | void Watches::UpdateWatchLockedState(std::size_t index, bool locked) |
| 66 | { |
| 67 | m_watches[index].locked = locked; |
| 68 | } |
| 69 | |
| 70 | void Watches::EnableWatch(std::size_t index) |
| 71 | { |
| 72 | m_watches[index].is_enabled = Watch::State::Enabled; |
| 73 | } |
| 74 | |
| 75 | void Watches::DisableWatch(std::size_t index) |
| 76 | { |
| 77 | m_watches[index].is_enabled = Watch::State::Disabled; |
| 78 | } |
| 79 | |
| 80 | bool Watches::HasEnabledWatch(u32 address) const |
| 81 | { |
| 82 | return std::ranges::any_of(m_watches, [address](const auto& watch) { |
| 83 | return watch.address == address && watch.is_enabled == Watch::State::Enabled; |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | void Watches::RemoveWatch(std::size_t index) |
| 88 | { |
| 89 | m_watches.erase(m_watches.begin() + index); |
| 90 | } |
| 91 | |
| 92 | void Watches::LoadFromStrings(std::span<const std::string> watches) |
| 93 | { |
| 94 | for (const std::string& watch : watches) |
| 95 | { |
| 96 | std::istringstream ss(watch); |
| 97 | ss.imbue(std::locale::classic()); |
| 98 | u32 address; |
| 99 | std::string name; |
| 100 | ss >> std::hex >> address; |
| 101 | ss >> std::ws; |
| 102 | std::getline(ss, name); |
| 103 | SetWatch(address, name); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | std::vector<std::string> Watches::SaveToStrings() const |
| 108 | { |
| 109 | std::vector<std::string> watches; |
| 110 | watches.reserve(m_watches.size()); |
| 111 | for (const auto& watch : m_watches) |
| 112 | watches.emplace_back(fmt::format("{:x} {}", watch.address, watch.name)); |
| 113 | return watches; |
| 114 | } |
| 115 | |
| 116 | void Watches::Clear() |
| 117 | { |
| 118 | m_watches.clear(); |
| 119 | } |
| 120 | } // namespace Common::Debug |