task.hpp (3333B)
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_TASK_HPP 19 #define REAPACK_TASK_HPP 20 21 #include "config.hpp" 22 #include "path.hpp" 23 #include "registry.hpp" 24 #include "remote.hpp" 25 26 #include <memory> 27 #include <set> 28 #include <unordered_set> 29 #include <vector> 30 31 class ArchiveReader; 32 class Index; 33 class Source; 34 class ThreadTask; 35 class Transaction; 36 class Version; 37 struct InstallOpts; 38 39 typedef std::shared_ptr<ArchiveReader> ArchiveReaderPtr; 40 typedef std::shared_ptr<const Index> IndexPtr; 41 42 class Task { 43 public: 44 Task(Transaction *parent) : m_tx(parent) {} 45 virtual ~Task() = default; 46 47 virtual bool start() { return true; } 48 virtual void commit() = 0; 49 virtual void rollback() {} 50 51 bool operator<(const Task &o) { return priority() < o.priority(); } 52 53 protected: 54 virtual int priority() const { return 0; } 55 Transaction *tx() const { return m_tx; } 56 57 private: 58 Transaction *m_tx; 59 }; 60 61 class SynchronizeTask : public Task { 62 public: 63 // TODO: remove InstallOpts argument 64 SynchronizeTask(const Remote &remote, bool stale, bool fullSync, 65 const InstallOpts &, Transaction *); 66 67 protected: 68 bool start() override; 69 void commit() override; 70 71 private: 72 void synchronize(const Package *); 73 74 Remote m_remote; 75 Path m_indexPath; 76 InstallOpts m_opts; 77 bool m_stale; 78 bool m_fullSync; 79 }; 80 81 class InstallTask : public Task { 82 public: 83 InstallTask(const Version *ver, int flags, const Registry::Entry &, 84 const ArchiveReaderPtr &, Transaction *); 85 86 bool start() override; 87 void commit() override; 88 void rollback() override; 89 90 private: 91 void push(ThreadTask *, const TempPath &); 92 93 const Version *m_version; 94 int m_flags; 95 Registry::Entry m_oldEntry; 96 ArchiveReaderPtr m_reader; 97 98 bool m_fail; 99 IndexPtr m_index; // keep in memory 100 std::vector<Registry::File> m_oldFiles; 101 std::vector<TempPath> m_newFiles; 102 std::unordered_set<ThreadTask *> m_waiting; 103 }; 104 105 class UninstallTask : public Task { 106 public: 107 UninstallTask(const Registry::Entry &, Transaction *); 108 109 protected: 110 int priority() const override { return 1; } 111 bool start() override; 112 void commit() override; 113 114 private: 115 Registry::Entry m_entry; 116 std::vector<Registry::File> m_files; 117 std::set<Path> m_removedFiles; 118 }; 119 120 class FlagsTask : public Task { 121 public: 122 FlagsTask(const Registry::Entry &, int flags, Transaction *); 123 124 protected: 125 void commit() override; 126 127 private: 128 Registry::Entry m_entry; 129 int m_flags; 130 }; 131 132 class ExportTask : public Task { 133 public: 134 ExportTask(const std::string &path, Transaction *); 135 136 protected: 137 bool start() override; 138 void commit() override; 139 void rollback() override; 140 141 private: 142 TempPath m_path; 143 }; 144 145 #endif