transaction.hpp (3666B)
1 /* ReaPack: Package manager for REAPER 2 * Copyright (C) 2015-2025 Christian Fillion 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU Lesser General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 #ifndef REAPACK_TRANSACTION_HPP 19 #define REAPACK_TRANSACTION_HPP 20 21 #include "event.hpp" 22 #include "receipt.hpp" 23 #include "registry.hpp" 24 #include "task.hpp" 25 #include "thread.hpp" 26 27 #include <functional> 28 #include <memory> 29 #include <optional> 30 #include <set> 31 #include <unordered_set> 32 33 class ArchiveReader; 34 class InstallTask; 35 class Path; 36 class Remote; 37 class SynchronizeTask; 38 class UninstallTask; 39 40 typedef std::shared_ptr<Task> TaskPtr; 41 42 struct HostTicket { bool add; Registry::Entry entry; Registry::File file; }; 43 44 class Transaction { 45 public: 46 typedef std::function<void()> CleanupHandler; 47 typedef std::function<bool(std::vector<Registry::Entry> &)> ObsoleteHandler; 48 49 Transaction(); 50 51 void setCleanupHandler(const CleanupHandler &cb) { m_cleanupHandler = cb; } 52 void setObsoleteHandler(const ObsoleteHandler &cb) { m_promptObsolete = cb; } 53 54 void fetchIndexes(const std::vector<Remote> &, bool stale = false); 55 std::vector<IndexPtr> getIndexes(const std::vector<Remote> &) const; 56 void synchronize(const Remote &, 57 const std::optional<bool> &forceAutoInstall = std::nullopt); 58 void install(const Version *, int flags = 0, const ArchiveReaderPtr & = nullptr); 59 void install(const Version *, const Registry::Entry &oldEntry, 60 int flags = 0, const ArchiveReaderPtr & = nullptr); 61 void setFlags(const Registry::Entry &, int flags); 62 void uninstall(const Remote &); 63 void uninstall(const Registry::Entry &); 64 void exportArchive(const std::string &path); 65 bool runTasks(); 66 67 bool isCancelled() const { return m_isCancelled; } 68 69 Receipt *receipt() { return &m_receipt; } 70 Registry *registry() { return &m_registry; } 71 ThreadPool *threadPool() { return &m_threadPool; } 72 73 Event<void()> onFinish; 74 75 protected: 76 friend SynchronizeTask; 77 friend InstallTask; 78 friend UninstallTask; 79 80 IndexPtr loadIndex(const Remote &); 81 void addObsolete(const Registry::Entry &e) { m_obsolete.insert(e); } 82 void registerAll(bool add, const Registry::Entry &); 83 void registerFile(const HostTicket &); 84 85 private: 86 class CompareTask { 87 public: 88 bool operator()(const TaskPtr &l, const TaskPtr &r) const 89 { 90 return *l < *r; 91 } 92 }; 93 94 typedef std::priority_queue<TaskPtr, 95 std::vector<TaskPtr>, CompareTask> TaskQueue; 96 97 void registerQueued(); 98 void registerScript(const HostTicket &, bool isLast); 99 void inhibit(const Remote &); 100 void promptObsolete(); 101 void runQueue(TaskQueue &queue); 102 bool commitTasks(); 103 void finish(); 104 105 bool m_isCancelled; 106 Registry m_registry; 107 Receipt m_receipt; 108 109 std::unordered_set<std::string> m_syncedRemotes; 110 std::map<std::string, IndexPtr> m_indexes; 111 std::unordered_set<Registry::Entry> m_obsolete; 112 113 ThreadPool m_threadPool; 114 TaskQueue m_nextQueue; 115 std::queue<TaskQueue> m_taskQueues; 116 std::queue<TaskPtr> m_runningTasks; 117 std::queue<HostTicket> m_regQueue; 118 119 CleanupHandler m_cleanupHandler; 120 ObsoleteHandler m_promptObsolete; 121 }; 122 123 #endif