Mercurial > minori
comparison dep/animia/src/strategist.cc @ 138:28842a8d0c6b
dep/animia: huge refactor (again...)
but this time, it actually compiles! and it WORKS! (on win32... not sure about
other platforms...)
configuring players is still not supported: at some point I'll prune something
up...
| author | Paper <mrpapersonic@gmail.com> |
|---|---|
| date | Sun, 12 Nov 2023 04:53:19 -0500 |
| parents | |
| children | 478f3b366199 |
comparison
equal
deleted
inserted
replaced
| 137:69db40272acd | 138:28842a8d0c6b |
|---|---|
| 1 #include "animia/strategies.h" | |
| 2 #include "animia/util.h" | |
| 3 #include "animia.h" | |
| 4 #include <iostream> | |
| 5 | |
| 6 #ifdef ANIMIA_ON_WIN32 | |
| 7 # include "animia/fd/win32.h" | |
| 8 #elif defined(ANIMIA_ON_LINUX) | |
| 9 # include "animia/fd/linux.h" | |
| 10 #elif defined(ANIMIA_ON_UNIX) | |
| 11 # include "animia/fd/bsd.h" | |
| 12 #endif | |
| 13 | |
| 14 namespace animia::internal { | |
| 15 | |
| 16 class Strategist { | |
| 17 public: | |
| 18 Strategist(Result& result) : result_(result) {} | |
| 19 | |
| 20 bool ApplyStrategies(); | |
| 21 | |
| 22 private: | |
| 23 bool AddMedia(const MediaInfo media_information); | |
| 24 | |
| 25 bool ApplyOpenFilesStrategy(); | |
| 26 | |
| 27 Result& result_; | |
| 28 }; | |
| 29 | |
| 30 bool Strategist::ApplyStrategies() { | |
| 31 bool success = false; | |
| 32 | |
| 33 for (const auto strategy : result_.player.strategies) { | |
| 34 switch (strategy) { | |
| 35 case Strategy::OpenFiles: | |
| 36 success |= ApplyOpenFilesStrategy(); | |
| 37 break; | |
| 38 } | |
| 39 } | |
| 40 | |
| 41 return success; | |
| 42 } | |
| 43 | |
| 44 bool ApplyStrategies(std::vector<Result>& results) { | |
| 45 bool success = false; | |
| 46 | |
| 47 for (auto& result : results) { | |
| 48 Strategist strategist(result); | |
| 49 success |= strategist.ApplyStrategies(); | |
| 50 } | |
| 51 | |
| 52 return success; | |
| 53 } | |
| 54 | |
| 55 //////////////////////////////////////////////////////////////////////////////// | |
| 56 | |
| 57 bool Strategist::ApplyOpenFilesStrategy() { | |
| 58 bool success = false; | |
| 59 | |
| 60 const std::set<pid_t> pids{result_.process.pid}; | |
| 61 std::vector<std::tuple<pid_t, std::string>> files; | |
| 62 | |
| 63 #ifdef ANIMIA_ON_WIN32 | |
| 64 win32::EnumerateOpenFiles(pids, files); | |
| 65 #elif defined(ANIMIA_ON_LINUX) | |
| 66 linux::EnumerateOpenFiles(pids, files); | |
| 67 #elif defined(ANIMIA_ON_UNIX) | |
| 68 unix::EnumerateOpenFiles(pids, files); | |
| 69 #endif | |
| 70 | |
| 71 for (const auto& [pid, file] : files) { | |
| 72 success |= AddMedia({MediaInfoType::File, file}); | |
| 73 } | |
| 74 | |
| 75 return success; | |
| 76 } | |
| 77 | |
| 78 //////////////////////////////////////////////////////////////////////////////// | |
| 79 | |
| 80 bool Strategist::AddMedia(const MediaInfo media_information) { | |
| 81 if (media_information.value.empty()) | |
| 82 return false; | |
| 83 | |
| 84 Media media; | |
| 85 media.information.push_back(media_information); | |
| 86 result_.media.push_back(std::move(media)); | |
| 87 | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 } |
