Mercurial > minori
changeset 150:ffa535b6d630
*: avoid usage of std::[pair,tuple]
https://arne-mertz.de/2017/03/smelly-pair-tuple/
it's better to use real structures and such where variables are easily known...
also apparently using [] on structs is actually valid? I had no idea.
author | Paper <mrpapersonic@gmail.com> |
---|---|
date | Tue, 14 Nov 2023 16:27:33 -0500 |
parents | e41505d24733 |
children | 54744a48a7d7 |
files | dep/animia/include/animia/fd.h dep/animia/include/animia/fd/bsd.h dep/animia/include/animia/fd/linux.h dep/animia/include/animia/fd/win32.h dep/animia/src/fd.cc dep/animia/src/fd/bsd.cc dep/animia/src/fd/linux.cc dep/animia/src/fd/win32.cc dep/animia/src/strategist.cc include/core/config.h src/core/config.cc src/gui/dialog/settings/recognition.cc |
diffstat | 12 files changed, 48 insertions(+), 22 deletions(-) [+] |
line wrap: on
line diff
--- a/dep/animia/include/animia/fd.h Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/include/animia/fd.h Tue Nov 14 16:27:33 2023 -0500 @@ -11,11 +11,16 @@ namespace animia::internal { +struct OpenFile { + pid_t pid = 0; + std::string path; +}; + class BaseFdTools { public: virtual bool GetAllPids(std::set<pid_t>& pids) { return false; } virtual bool GetProcessName(pid_t pid, std::string& result) { return false; } - virtual bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) { return false; } + virtual bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) { return false; } }; extern BaseFdTools& fd; // defined in fd.cc
--- a/dep/animia/include/animia/fd/bsd.h Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/include/animia/fd/bsd.h Tue Nov 14 16:27:33 2023 -0500 @@ -19,7 +19,7 @@ public: bool GetAllPids(std::set<pid_t>& pids) override; bool GetProcessName(pid_t pid, std::string& result) override; - bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) override; + bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) override; }; }
--- a/dep/animia/include/animia/fd/linux.h Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/include/animia/fd/linux.h Tue Nov 14 16:27:33 2023 -0500 @@ -20,7 +20,7 @@ public: bool GetAllPids(std::set<pid_t>& pids) override; bool GetProcessName(pid_t pid, std::string& result) override; - bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) override; + bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) override; }; }
--- a/dep/animia/include/animia/fd/win32.h Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/include/animia/fd/win32.h Tue Nov 14 16:27:33 2023 -0500 @@ -25,7 +25,7 @@ public: bool GetAllPids(std::set<pid_t>& pids) override; bool GetProcessName(pid_t pid, std::string& result) override; - bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) override; + bool EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) override; }; }
--- a/dep/animia/src/fd.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/src/fd.cc Tue Nov 14 16:27:33 2023 -0500 @@ -1,3 +1,4 @@ +#include "animia/os.h" #include "animia/fd.h" #ifdef ANIMIA_ON_WIN32
--- a/dep/animia/src/fd/bsd.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/src/fd/bsd.cc Tue Nov 14 16:27:33 2023 -0500 @@ -109,7 +109,11 @@ continue; */ - files.push_back({pid, vnodeInfo.pvip.vip_path}); + OpenFile file; + file.pid = pid; + file.path = vnodeInfo.pvip.vip_path; + + files.push_back(file); } } }
--- a/dep/animia/src/fd/linux.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/src/fd/linux.cc Tue Nov 14 16:27:33 2023 -0500 @@ -124,7 +124,7 @@ return true; } -bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) { +bool LinuxFdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) { for (const auto& pid : pids) { const std::string path = PROC_LOCATION "/" + std::to_string(pid) + "/fd"; @@ -137,7 +137,11 @@ if (!IsRegularFile(name)) continue; - files.push_back({pid, name}); + OpenFile file; + file.pid = pid; + file.path = name; + + files.push_back(file); } } return true;
--- a/dep/animia/src/fd/win32.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/src/fd/win32.cc Tue Nov 14 16:27:33 2023 -0500 @@ -224,7 +224,7 @@ bool Win32FdTools::GetProcessName(pid_t pid, std::string& result) { unsigned long ret_size = 0; // size given by GetModuleBaseNameW Handle handle(::OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid)); - if (!handle.get()) + if (handle.get() == INVALID_HANDLE_VALUE) return false; /* agh... */ @@ -243,12 +243,12 @@ } /* this could be changed to being a callback, but... I'm too lazy right now :) */ -bool Win32FdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<std::pair<pid_t, std::string>>& files) { +bool Win32FdTools::EnumerateOpenFiles(const std::set<pid_t>& pids, std::vector<OpenFile>& files) { std::unordered_map<pid_t, Handle> proc_handles; for (const pid_t& pid : pids) { const HANDLE handle = ::OpenProcess(PROCESS_DUP_HANDLE, false, pid); - if (handle) + if (handle != INVALID_HANDLE_VALUE) proc_handles[pid] = Handle(handle); } @@ -269,7 +269,7 @@ continue; Handle handle(DuplicateHandle(proc_handles[pid].get(), h.HandleValue)); - if (!handle.get()) + if (handle.get() == INVALID_HANDLE_VALUE) continue; if (GetFileType(handle.get()) != FILE_TYPE_DISK) @@ -279,8 +279,11 @@ if (!IsFilePathOk(path)) continue; - /* create an empty vector if it doesn't exist, probably unnecessary */ - files.push_back({pid, path}); + OpenFile file; + file.pid = pid; + file.path = path; + + files.push_back(file); } return true;
--- a/dep/animia/src/strategist.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/dep/animia/src/strategist.cc Tue Nov 14 16:27:33 2023 -0500 @@ -50,12 +50,12 @@ bool success = false; const std::set<pid_t> pids{result_.process.pid}; - std::vector<std::pair<pid_t, std::string>> files; + std::vector<OpenFile> files; fd.EnumerateOpenFiles(pids, files); - for (const auto& [pid, file] : files) { - success |= AddMedia({MediaInfoType::File, file}); + for (const auto& file : files) { + success |= AddMedia({MediaInfoType::File, file.path}); } return success;
--- a/include/core/config.h Tue Nov 14 13:40:11 2023 -0500 +++ b/include/core/config.h Tue Nov 14 16:27:33 2023 -0500 @@ -11,6 +11,11 @@ #include <vector> #include <utility> +struct MediaPlayer { + bool enabled = true; + animia::Player player; +}; + class Config { public: int Load(); @@ -44,9 +49,7 @@ struct { public: bool detect_media_players; - /* preferably web browsers would be in a separate vector, - but whatever :/ */ - std::vector<std::pair<bool, animia::Player>> players; + std::vector<MediaPlayer> players; } recognition; struct {
--- a/src/core/config.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/src/core/config.cc Tue Nov 14 16:27:33 2023 -0500 @@ -64,8 +64,14 @@ return false; recognition.players.reserve(players.size()); - for (const auto& player : players) - recognition.players.push_back({true, player}); + for (const auto& player : players) { + MediaPlayer mediaplayer; + + mediaplayer.enabled = true; + mediaplayer.player = player; + + recognition.players.push_back(mediaplayer); + } } for (auto& [enabled, player] : recognition.players) {
--- a/src/gui/dialog/settings/recognition.cc Tue Nov 14 13:40:11 2023 -0500 +++ b/src/gui/dialog/settings/recognition.cc Tue Nov 14 16:27:33 2023 -0500 @@ -49,7 +49,7 @@ if (!item) return; size_t i = item->data(Qt::UserRole).toUInt(); - players[i].first = item->checkState(); + players[i].enabled = item->checkState(); }); /* this is down here because the listwidget state depends on it */ connect(checkbox, &QCheckBox::stateChanged, this, [this, listwidget](int state) {