KiCad PCB EDA Suite
Loading...
Searching...
No Matches
api_task_scheduler.h
Go to the documentation of this file.
1/*
2 * This program source code file is part of KiCad, a free EDA CAD application.
3 *
4 * Copyright The KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * This program is free software: you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation, either version 3 of the License, or (at your
9 * option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <https://www.gnu.org/licenses/>.
18 */
19
20#ifndef KICAD_API_TASK_SCHEDULER_H
21#define KICAD_API_TASK_SCHEDULER_H
22
23#include <deque>
24#include <map>
25#include <memory>
26#include <mutex>
27#include <optional>
28#include <string>
29
30#include <wx/event.h>
31
32#include <kicommon.h>
33
35{
36class Any;
37}
38
40{
41class TaskStatusResponse;
42}
43
45{
46public:
47 virtual ~API_TASK() = default;
48
50 virtual void Run() = 0;
51
53 virtual void FillStatus( kiapi::common::tasks::TaskStatusResponse& aStatus ) const = 0;
54};
55
59class KICOMMON_API API_TASK_SCHEDULER : public wxEvtHandler
60{
61public:
62 static constexpr size_t MAX_FINISHED_TASKS = 32;
63
65
66 ~API_TASK_SCHEDULER() override;
67
70
77 std::optional<std::string> StartTask( const std::shared_ptr<API_TASK>& aTask, const wxString& aDescription );
78
83 std::optional<kiapi::common::tasks::TaskStatusResponse> GetStatus( const std::string& aToken ) const;
84
89 std::shared_ptr<API_TASK> GetFinishedTask( const std::string& aToken ) const;
90
92 bool AnyTaskRunning() const;
93
98 void Pump();
99
100private:
102 {
103 std::string token;
104 wxString description;
105 std::shared_ptr<API_TASK> task;
106 bool finished = false;
107 bool failed = false;
108 std::string failureMessage;
109 };
110
112
113 void onTaskRunEvent( wxCommandEvent& aEvent );
114 void runTask( const std::string& aToken );
115
116 mutable std::mutex m_mutex;
117 std::map<std::string, std::shared_ptr<TASK_ENTRY>> m_tasks;
118 std::deque<std::string> m_finishedTokens;
119};
120
121#endif //KICAD_API_TASK_SCHEDULER_H
std::shared_ptr< API_TASK > GetFinishedTask(const std::string &aToken) const
Look up a task that has finished, for result retrieval.
std::optional< kiapi::common::tasks::TaskStatusResponse > GetStatus(const std::string &aToken) const
Get a snapshot of the task's current status.
API_TASK_SCHEDULER & operator=(const API_TASK_SCHEDULER &)=delete
std::deque< std::string > m_finishedTokens
void onTaskRunEvent(wxCommandEvent &aEvent)
void Pump()
Run the current task to completion, synchronously.
void runTask(const std::string &aToken)
static API_TASK_SCHEDULER & Instance()
API_TASK_SCHEDULER(const API_TASK_SCHEDULER &)=delete
static constexpr size_t MAX_FINISHED_TASKS
std::map< std::string, std::shared_ptr< TASK_ENTRY > > m_tasks
std::optional< std::string > StartTask(const std::shared_ptr< API_TASK > &aTask, const wxString &aDescription)
Register a new task and begin running it.
virtual ~API_TASK()=default
virtual void Run()=0
The task runner; called on the main thread.
virtual void FillStatus(kiapi::common::tasks::TaskStatusResponse &aStatus) const =0
Called after Run() returns; should fill in the result of the run.
#define KICOMMON_API
Definition kicommon.h:27
bool failed
wxString description
std::string failureMessage
bool finished
std::string token
std::shared_ptr< API_TASK > task