/
savushkin
/
CubeTaskManager
Обзор
Документация
Войти
/
savushkin
/
CubeTaskManager
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
cube_tm2/task.cpp
243 строки
8 KB
Alexander Savushkin
feat: cube_tm_2 - 2025-10-23 17:58:06
23 окт 2025, 17:58
23 окт 2025, 17:58
02548f3
Код
Авторство
О чём код?
#include "task.h" // Конструктор Task::Task(int id, const std::string &task_name, TaskType task_type, int priority_percent, std::function<void()> exec_func, unsigned char *icon, size_t icon_size, void *obj) : task_id(id), name(task_name), state(TaskState::READY), priority_percent(std::max(0, std::min(100, priority_percent))), type(task_type), execute_function(exec_func), icon_data(nullptr), icon_size(icon_size), task_object(obj), cpu_usage(0), memory_usage(0), skip_execution(false), execution_count(0) { // Копируем icon данные если они предоставлены if (icon && icon_size > 0) { this->icon_data = new unsigned char[icon_size]; std::copy(icon, icon + icon_size, this->icon_data); this->icon_size = icon_size; } // Если функция не предоставлена, создаем стандартную if (!execute_function) { execute_function = [this]() { this->defaultExecute(); }; } std::cout << "Создана задача: " << name << " (ID: " << task_id << ", Приоритет: " << priority_percent << "%)" << std::endl; } // Деструктор Task::~Task() { std::cout << "Уничтожается задача: " << name << " (ID: " << task_id << ")" << std::endl; // Освобождаем память icon данных if (icon_data) { delete[] icon_data; icon_data = nullptr; } } // Конструктор копирования Task::Task(const Task &other) : task_id(other.task_id), name(other.name), state(other.state), priority_percent(other.priority_percent), type(other.type), execute_function(other.execute_function), icon_data(nullptr), icon_size(other.icon_size), task_object(other.task_object), cpu_usage(other.cpu_usage), memory_usage(other.memory_usage), skip_execution(other.skip_execution), execution_count(other.execution_count) { if (other.icon_data && other.icon_size > 0) { icon_data = new unsigned char[other.icon_size]; std::copy(other.icon_data, other.icon_data + other.icon_size, icon_data); } } // Оператор присваивания Task &Task::operator=(const Task &other) { if (this != &other) { // Освобождаем старые данные if (icon_data) { delete[] icon_data; } // Копируем новые данные task_id = other.task_id; name = other.name; state = other.state; priority_percent = other.priority_percent; type = other.type; execute_function = other.execute_function; icon_size = other.icon_size; task_object = other.task_object; cpu_usage = other.cpu_usage; memory_usage = other.memory_usage; skip_execution = other.skip_execution; execution_count = other.execution_count; if (other.icon_data && other.icon_size > 0) { icon_data = new unsigned char[other.icon_size]; std::copy(other.icon_data, other.icon_data + other.icon_size, icon_data); } else { icon_data = nullptr; } } return *this; } // Получение приоритетной группы (0-9) int Task::getPriorityGroup() const { // 0% -> группа 0, 100% -> группа 9 return (priority_percent * 9) / 100; } // Стандартная логика выполнения void Task::defaultExecute() { if (skip_execution) { std::cout << "⏭️ Пропуск выполнения: " << name << " (приоритет: " << priority_percent << "%)" << std::endl; return; } state = TaskState::RUNNING; execution_count++; std::cout << "🚀 Выполняется задача: " << name << " (ID: " << task_id << ", Приоритет: " << priority_percent << "%" << ", Группа: " << getPriorityGroup() << ", Тип: " << static_cast<int>(type) << ")" << std::endl; // Имитация работы задачи simulateWork(); } // Запуск задачи void Task::execute() { if (state == TaskState::TERMINATED) { std::cout << "❌ Ошибка: задача " << name << " уже завершена" << std::endl; return; } if (skip_execution) { std::cout << "⏭️ Пропуск выполнения: " << name << " (приоритет: " << priority_percent << "%)" << std::endl; return; } if (execute_function) { execute_function(); } else { defaultExecute(); } } void Task::suspend() { if (state == TaskState::RUNNING) { state = TaskState::SUSPENDED; std::cout << "⏸️ Задача приостановлена: " << name << std::endl; } } void Task::resume() { if (state == TaskState::SUSPENDED) { state = TaskState::READY; std::cout << "▶️ Задача возобновлена: " << name << std::endl; } } void Task::terminate() { state = TaskState::TERMINATED; std::cout << "⏹️ Задача завершена: " << name << std::endl; } // Установка пользовательской функции выполнения void Task::setExecuteFunction(std::function<void()> func) { execute_function = func; } // Установка icon данных void Task::setIconData(const unsigned char *data, size_t size) { if (icon_data) { delete[] icon_data; } if (data && size > 0) { icon_data = new unsigned char[size]; std::copy(data, data + size, icon_data); icon_size = size; } else { icon_data = nullptr; icon_size = 0; } } // Получение информации о задаче void Task::printInfo() const { std::cout << "📋 Информация о задаче:" << std::endl; std::cout << " ID: " << task_id << std::endl; std::cout << " Имя: " << name << std::endl; std::cout << " Тип: " << static_cast<int>(type) << std::endl; std::cout << " Приоритет: " << priority_percent << "%" << std::endl; std::cout << " Приоритетная группа: " << getPriorityGroup() << std::endl; std::cout << " Состояние: " << static_cast<int>(state) << std::endl; std::cout << " Размер icon: " << icon_size << " байт" << std::endl; std::cout << " Использование CPU: " << cpu_usage << "%" << std::endl; std::cout << " Использование памяти: " << memory_usage << " KB" << std::endl; std::cout << " Пропуск выполнения: " << (skip_execution ? "Да" : "Нет") << std::endl; std::cout << " Количество выполнений: " << execution_count << std::endl; } void Task::simulateWork() { // Имитация работы задачи cpu_usage = 10 + (std::rand() % 80); // 10-90% memory_usage = 1 + (std::rand() % 100); // 1-100 KB // Имитация времени выполнения (зависит от приоритета) int work_steps = 1 + (priority_percent / 25); // 1-5 шагов в зависимости от приоритета for (int i = 0; i < work_steps; i++) { std::cout << " " << name << " работает... (" << (i + 1) << "/" << work_steps << ")" << std::endl; } state = TaskState::READY; std::cout << "✅ Задача " << name << " завершила выполнение" << std::endl; }