KiCad PCB EDA Suite
Loading...
Searching...
No Matches
design_block_info_impl.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
20
22
23#include <design_block.h>
25#include <kiway.h>
26#include <progress_reporter.h>
27#include <string_utils.h>
28#include <thread_pool.h>
30
31#include <kiplatform/io.h>
32
33#include <wx/txtstrm.h>
34#include <wx/wfstream.h>
35
36
38{
40
41 wxASSERT( dbtable );
42
43 std::unique_ptr<const DESIGN_BLOCK> design_block( dbtable->GetEnumeratedDesignBlock( m_nickname, m_dbname ) );
44
45 if( design_block )
46 {
47 m_keywords = design_block->GetKeywords();
48 m_doc = design_block->GetLibDescription();
49 }
50
51 m_loaded = true;
52}
53
54
55bool DESIGN_BLOCK_LIST_IMPL::CatchErrors( const std::function<void()>& aFunc )
56{
57 try
58 {
59 aFunc();
60 }
61 catch( const IO_ERROR& ioe )
62 {
63 m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
64 return false;
65 }
66 catch( const std::exception& se )
67 {
68 // This is a round about way to do this, but who knows what THROW_IO_ERROR()
69 // may be tricked out to do someday, keep it in the game.
70 try
71 {
72 THROW_IO_ERROR( se.what() );
73 }
74 catch( const IO_ERROR& ioe )
75 {
76 m_errors.move_push( std::make_unique<IO_ERROR>( ioe ) );
77 }
78
79 return false;
80 }
81
82 return true;
83}
84
85
87 const wxString* aNickname,
88 PROGRESS_REPORTER* aProgressReporter )
89{
90 long long int generatedTimestamp = 0;
91
92 if( !CatchErrors(
93 [&]()
94 {
95 generatedTimestamp = aTable->GenerateTimestamp( aNickname );
96 } ) )
97 {
98 return false;
99 }
100
101 if( generatedTimestamp == m_list_timestamp )
102 return true;
103
104 // Disable KIID generation: not needed for library parts; sometimes very slow
105 KIID_NIL_SET_RESET reset_kiid;
106
107 m_progress_reporter = aProgressReporter;
108
109 m_cancelled = false;
110 m_lib_table = aTable;
111
112 // Clear data before reading files
113 m_errors.clear();
114 m_list.clear();
115 m_queue.clear();
116
117 if( aNickname )
118 {
119 m_queue.push( *aNickname );
120 }
121 else
122 {
123 for( const wxString& nickname : aTable->GetLogicalLibs() )
124 m_queue.push( nickname );
125 }
126
128 {
130 m_progress_reporter->Report( _( "Loading design_blocks..." ) );
131 }
132
134
137
138 if( m_cancelled )
139 m_list_timestamp = 0; // God knows what we got before we were canceled
140 else
141 m_list_timestamp = generatedTimestamp;
142
143 return m_errors.empty();
144}
145
146
148{
149 // Parse the design_blocks in parallel.
150
152
154 size_t num_elements = m_queue.size();
155 std::vector<std::future<size_t>> returns( num_elements );
156
157 auto db_thread =
158 [ this, &queue_parsed ]() -> size_t
159 {
160 wxString nickname;
161
162 if( m_cancelled || !m_queue.pop( nickname ) )
163 return 0;
164
165 wxArrayString dbnames;
166
168 [&]()
169 {
170 m_lib_table->DesignBlockEnumerate( dbnames, nickname, false );
171 } );
172
173 for( wxString dbname : dbnames )
174 {
176 [&]()
177 {
178 auto* dbinfo = new DESIGN_BLOCK_INFO_IMPL( this, nickname, dbname );
179 queue_parsed.move_push( std::unique_ptr<DESIGN_BLOCK_INFO>( dbinfo ) );
180 } );
181
182 if( m_cancelled )
183 return 0;
184 }
185
188
189 return 1;
190 };
191
192 for( size_t ii = 0; ii < num_elements; ++ii )
193 returns[ii] = tp.submit( db_thread );
194
195 for( const std::future<size_t>& ret : returns )
196 {
197 std::future_status status = ret.wait_for( std::chrono::milliseconds( 250 ) );
198
199 while( status != std::future_status::ready )
200 {
203
204 status = ret.wait_for( std::chrono::milliseconds( 250 ) );
205 }
206 }
207
208 std::unique_ptr<DESIGN_BLOCK_INFO> dbi;
209
210 while( queue_parsed.pop( dbi ) )
211 m_list.push_back( std::move( dbi ) );
212
213 std::sort( m_list.begin(), m_list.end(),
214 []( std::unique_ptr<DESIGN_BLOCK_INFO> const& lhs,
215 std::unique_ptr<DESIGN_BLOCK_INFO> const& rhs ) -> bool
216 {
217 return *lhs < *rhs;
218 } );
219}
220
221
223 m_list_timestamp( 0 ),
224 m_progress_reporter( nullptr ),
225 m_cancelled( false )
226{
227}
virtual void load() override
lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
DESIGN_BLOCK_LIST * m_owner
provides access to DESIGN_BLOCK_LIB_TABLE
wxString m_nickname
library as known in DESIGN_BLOCK_LIB_TABLE
wxString m_dbname
Module name.
wxString m_keywords
Design block keywords.
wxString m_doc
Design block description.
long long GenerateTimestamp(const wxString *aNickname)
Generate a hashed timestamp representing the last-mod-times of the library indicated by aNickname,...
const DESIGN_BLOCK * GetEnumeratedDesignBlock(const wxString &aNickname, const wxString &aDesignBlockName)
A version of DesignBlockLoad() for use after DesignBlockEnumerate() for more efficient cache manageme...
void DesignBlockEnumerate(wxArrayString &aDesignBlockNames, const wxString &aNickname, bool aBestEfforts)
Return a list of design block names contained within the library given by aNickname.
bool CatchErrors(const std::function< void()> &aFunc)
Call aFunc, pushing any IO_ERRORs and std::exceptions it throws onto m_errors.
PROGRESS_REPORTER * m_progress_reporter
bool ReadDesignBlockFiles(DESIGN_BLOCK_LIB_TABLE *aTable, const wxString *aNickname=nullptr, PROGRESS_REPORTER *aProgressReporter=nullptr) override
Read all the design blocks provided by the combination of aTable and aNickname.
SYNC_QUEUE< wxString > m_queue
SYNC_QUEUE< std::unique_ptr< IO_ERROR > > m_errors
some can be PARSE_ERRORs also
std::vector< std::unique_ptr< DESIGN_BLOCK_INFO > > m_list
DESIGN_BLOCK_LIB_TABLE * GetTable() const
DESIGN_BLOCK_LIB_TABLE * m_lib_table
no ownership
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:77
RAII class to safely set/reset nil KIIDs for use in footprint/symbol loading.
Definition: kiid.h:230
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
A progress reporter interface for use in multi-threaded environments.
virtual bool KeepRefreshing(bool aWait=false)=0
Update the UI (if any).
virtual void Report(const wxString &aMessage)=0
Display aMessage in the progress bar dialog.
virtual void AdvancePhase()=0
Use the next available virtual zone of the dialog progress bar.
virtual void AdvanceProgress()=0
Increment the progress bar length (inside the current virtual zone).
virtual void SetMaxProgress(int aMaxProgress)=0
Fix the value that gives the 100 percent progress bar length (inside the current virtual zone).
Synchronized, locking queue.
Definition: sync_queue.h:32
bool pop(T &aReceiver)
Pop a value if the queue into the provided variable.
Definition: sync_queue.h:63
bool empty() const
Return true if the queue is empty.
Definition: sync_queue.h:82
void clear()
Clear the queue.
Definition: sync_queue.h:100
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:91
void push(T const &aValue)
Push a value onto the queue.
Definition: sync_queue.h:41
void move_push(T &&aValue)
Move a value onto the queue.
Definition: sync_queue.h:50
#define _(s)
#define THROW_IO_ERROR(msg)
Definition: ki_exception.h:39
thread_pool & GetKiCadThreadPool()
Get a reference to the current thread pool.
Definition: thread_pool.cpp:30
static thread_pool * tp
Definition: thread_pool.cpp:28
BS::thread_pool thread_pool
Definition: thread_pool.h:31
Definition of file extensions used in Kicad.