KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_transfer.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 along
17 * with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20
21#ifndef GIT_TRANSFER_H
22#define GIT_TRANSFER_H
23
24#include <functional>
25#include <cstdint>
26#include <memory>
27#include <optional>
28#include <string>
29
32
33namespace KIGIT
34{
35
43void CallOnUi( std::function<void()> aFn );
44
47void CallIfAlive( const std::weak_ptr<bool>& aWatch, std::function<void()> aFn );
48
56bool StartTransfer( const std::string& aLabel, std::function<void()> aWork );
57
58} // namespace KIGIT
59
60
79{
80public:
81 KIGIT_STATUS_REPORTER( const std::weak_ptr<bool>& aAlive, std::function<void( const wxString& )> aShow );
82
83protected:
84 bool updateUI() override;
85
86private:
87 std::weak_ptr<bool> m_alive;
88 std::function<void( const wxString& )> m_show;
89 wxString m_lastShown;
90};
91
92
94{
95public:
96 KIGIT_TRANSFER() = default;
98
99 KIGIT_TRANSFER( const KIGIT_TRANSFER& ) = delete;
101
103 bool IsBusy() const { return m_busy; }
104
106 std::weak_ptr<bool> Alive() const { return m_alive; }
107
109 void Cancel();
110
120 bool Start( const std::string& aLabel, const std::function<void( const KIGIT_COMMON::CANCEL_TOKEN& )>& aWork,
121 const std::function<void()>& aDone );
122
123private:
125 std::shared_ptr<bool> m_alive = std::make_shared<bool>( true );
126
130
133 std::shared_ptr<std::function<void()>> m_completion;
134
135 bool m_busy = false;
136};
137
138
147{
148public:
149 KIGIT_QUERY() = default;
150 ~KIGIT_QUERY();
151
152 KIGIT_QUERY( const KIGIT_QUERY& ) = delete;
153 KIGIT_QUERY& operator=( const KIGIT_QUERY& ) = delete;
154
164 void Run( std::shared_ptr<KIGIT_COMMON> aSession, std::function<void( KIGIT_COMMON& )> aWork,
165 std::function<void()> aDone );
166
168 void Cancel();
169
171 bool IsBusy() const { return m_transfer.IsBusy(); }
172
173private:
174 struct REQUEST
175 {
176 std::shared_ptr<KIGIT_COMMON> session;
177 std::function<void( KIGIT_COMMON& )> work;
178 std::function<void()> done;
179 uint64_t generation;
180 };
181
182 void start( REQUEST aRequest );
183
185 std::optional<REQUEST> m_pending;
186 uint64_t m_generation = 0;
187};
188
189#endif // GIT_TRANSFER_H
The project's git session.
std::shared_ptr< std::atomic< bool > > CANCEL_TOKEN
One token per operation.
bool IsBusy() const
Remains true until the running worker returns, including after Cancel().
uint64_t m_generation
KIGIT_QUERY()=default
KIGIT_TRANSFER m_transfer
KIGIT_QUERY(const KIGIT_QUERY &)=delete
void Cancel()
Suppress the running completion and discard pending work. Does not wait for a read.
KIGIT_QUERY & operator=(const KIGIT_QUERY &)=delete
std::optional< REQUEST > m_pending
void start(REQUEST aRequest)
void Run(std::shared_ptr< KIGIT_COMMON > aSession, std::function< void(KIGIT_COMMON &)> aWork, std::function< void()> aDone)
Run aWork under the session's action lock on a worker, then aDone on the UI thread.
std::function< void(const wxString &)> m_show
std::weak_ptr< bool > m_alive
KIGIT_STATUS_REPORTER(const std::weak_ptr< bool > &aAlive, std::function< void(const wxString &)> aShow)
bool updateUI() override
KIGIT_TRANSFER()=default
std::weak_ptr< bool > Alive() const
Dropped when this object goes. Hand it to a reporter so its updates stop with it.
KIGIT_TRANSFER(const KIGIT_TRANSFER &)=delete
KIGIT_COMMON::CANCEL_TOKEN m_cancel
The token of the transfer started last.
KIGIT_TRANSFER & operator=(const KIGIT_TRANSFER &)=delete
std::shared_ptr< std::function< void()> > m_completion
Created and cleared on the UI thread.
std::shared_ptr< bool > m_alive
Dropped when this object goes, so a transfer that outlives it completes into nothing.
bool IsBusy() const
True while a transfer started here is still running.
void Cancel()
Ask the running transfer to stop. It finishes on its own time.
bool Start(const std::string &aLabel, const std::function< void(const KIGIT_COMMON::CANCEL_TOKEN &)> &aWork, const std::function< void()> &aDone)
Run aWork on a background thread, then aDone on the UI thread.
bool StartTransfer(const std::string &aLabel, std::function< void()> aWork)
Run aWork on a thread the orphan registry owns.
void CallIfAlive(const std::weak_ptr< bool > &aWatch, std::function< void()> aFn)
As CallOnUi, for work that touches the window that started the transfer.
void CallOnUi(std::function< void()> aFn)
Run aFn on the UI thread.
std::function< void()> done
std::function< void(KIGIT_COMMON &)> work
std::shared_ptr< KIGIT_COMMON > session