KiCad PCB EDA Suite
symbol_async_loader.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 (C) 2021 Jon Evans <[email protected]>
5  * Copyright (C) 2021 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software: you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation, either version 3 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #ifndef KICAD_SYMBOL_ASYNC_LOADER_H
22 #define KICAD_SYMBOL_ASYNC_LOADER_H
23 
24 #include <atomic>
25 #include <future>
26 #include <mutex>
27 #include <unordered_map>
28 #include <vector>
29 
30 #include <wx/string.h>
31 
32 class LIB_SYMBOL;
33 class PROGRESS_REPORTER;
34 class SYMBOL_LIB_TABLE;
35 
36 
38 {
39 public:
48  SYMBOL_ASYNC_LOADER( const std::vector<wxString>& aNicknames,
49  SYMBOL_LIB_TABLE* aTable, bool aOnlyPowerSymbols = false,
50  std::unordered_map<wxString, std::vector<LIB_SYMBOL*>>* aOutput = nullptr,
51  PROGRESS_REPORTER* aReporter = nullptr );
52 
54 
58  void Start();
59 
63  bool Join();
64 
68  void Abort();
69 
71  bool Done();
72 
74  const wxString& GetErrors() const { return m_errors; }
75 
77  typedef std::pair<wxString, std::vector<LIB_SYMBOL*>> LOADED_PAIR;
78 
79 private:
81  std::vector<LOADED_PAIR> worker();
82 
84  std::vector<wxString> m_nicknames;
85 
88 
91 
93  std::unordered_map<wxString, std::vector<LIB_SYMBOL*>>* m_output;
94 
97 
98  size_t m_threadCount;
99  std::atomic<size_t> m_nextLibrary;
100  std::atomic_bool m_canceled;
101  wxString m_errors;
102  std::mutex m_errorMutex;
103 
104  std::vector<std::future<std::vector<LOADED_PAIR>>> m_returns;
105 };
106 
107 #endif
bool m_onlyPowerSymbols
Handle to map that will be filled with the loaded parts per library.
void Start()
Spins up threads to load all the libraries in m_nicknames.
std::atomic< size_t > m_nextLibrary
bool Join()
Finalizes the threads and combines the output into the target output map.
A progress reporter interface for use in multi-threaded environments.
std::pair< wxString, std::vector< LIB_SYMBOL * > > LOADED_PAIR
void Abort()
Cancels a load in-progress.
Define a library symbol object.
Definition: lib_symbol.h:96
bool Done()
Returns a string containing any errors generated during the load.
const wxString & GetErrors() const
Represents a pair of <nickname, loaded parts list>
std::atomic_bool m_canceled
std::unordered_map< wxString, std::vector< LIB_SYMBOL * > > * m_output
Progress reporter (may be null)
SYMBOL_LIB_TABLE * m_table
True if we are loading only power symbols.
std::vector< wxString > m_nicknames
Handle to the symbol library table being loaded into.
std::vector< LOADED_PAIR > worker()
< Worker job that loads libraries and returns a list of pairs of <nickname, loaded parts>
std::vector< std::future< std::vector< LOADED_PAIR > > > m_returns
PROGRESS_REPORTER * m_reporter
SYMBOL_ASYNC_LOADER(const std::vector< wxString > &aNicknames, SYMBOL_LIB_TABLE *aTable, bool aOnlyPowerSymbols=false, std::unordered_map< wxString, std::vector< LIB_SYMBOL * >> *aOutput=nullptr, PROGRESS_REPORTER *aReporter=nullptr)
Constructs a loader for symbol libraries.