KiCad PCB EDA Suite
Loading...
Searching...
No Matches
git_transfer_busy.cpp
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
22
23#include <utility>
24#include <vector>
25
26
27void GIT_TRANSFER_BUSY::HOLD::Begin( const std::shared_ptr<KIGIT_COMMON>& aRepo, const wxString& aStatus,
28 std::function<void()> aCancel )
29{
30 // One counter for every repository, so a hold on another repository never matches a live one here
31 static uint64_t s_nextId = 0;
32
33 Release();
34 m_repo = aRepo;
35 m_id = ++s_nextId;
36
37 GIT_TRANSFER_BUSY& transfers = aRepo->Transfers();
38 transfers.m_live.emplace( m_id, LIVE{ aStatus, std::move( aCancel ) } );
40}
41
42
44{
45 std::shared_ptr<KIGIT_COMMON> repo = std::exchange( m_repo, {} ).lock();
46 uint64_t id = std::exchange( m_id, 0 );
47
48 if( repo && repo->Transfers().m_live.erase( id ) )
49 repo->Transfers().Notify( &GIT_TRANSFER_WATCHER::OnGitTransfersChanged );
50}
51
52
53bool GIT_TRANSFER_BUSY::IsBusy( const HOLD& aExcept ) const
54{
55 return m_live.size() > m_live.count( aExcept.m_id );
56}
57
58
59wxString GIT_TRANSFER_BUSY::Status( const HOLD& aExcept ) const
60{
61 for( auto it = m_live.rbegin(); it != m_live.rend(); ++it )
62 {
63 if( it->first != aExcept.m_id )
64 return it->second.status;
65 }
66
67 return wxString();
68}
69
70
71bool GIT_TRANSFER_BUSY::CanCancel( const HOLD& aExcept ) const
72{
73 for( const auto& [id, live] : m_live )
74 {
75 if( id != aExcept.m_id && live.cancel )
76 return true;
77 }
78
79 return false;
80}
81
82
84{
85 std::vector<std::function<void()>> cancels;
86
87 for( auto& [id, live] : m_live )
88 {
89 if( live.cancel )
90 cancels.push_back( std::exchange( live.cancel, nullptr ) );
91 }
92
93 // Collect first, since a cancel may end its transfer and erase it from m_live
94 for( std::function<void()>& cancel : cancels )
95 cancel();
96}
One transfer's hold on its repository, released when reset, begun again or destroyed.
void Begin(const std::shared_ptr< KIGIT_COMMON > &aRepo, const wxString &aStatus, std::function< void()> aCancel)
Release any earlier hold, then hold aRepo busy for one transfer.
std::weak_ptr< KIGIT_COMMON > m_repo
wxString Status(const HOLD &aExcept) const
The status of the latest live transfer other than aExcept's, or empty when none is.
void CancelAll()
Call each live transfer's cancel once. They stay live until their owners end them.
bool CanCancel(const HOLD &aExcept) const
True while a live transfer other than aExcept's has a cancel that has not been called.
std::map< uint64_t, LIVE > m_live
Keyed by an id that only grows, so the last entry is the latest transfer.
bool IsBusy(const HOLD &aExcept) const
True while a transfer other than aExcept's is live.
virtual void OnGitTransfersChanged()=0
void Notify(void(ObserverInterface::*Ptr)(Args1...), Args2 &&... aArgs)
Notify event to all subscribed observers.
Definition observable.h:178