forked from KiCad/kicad-source-mirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfootprint_info.h
371 lines (307 loc) · 10.4 KB
/
footprint_info.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
* This program source code file is part of KiCad, a free EDA CAD application.
*
* Copyright (C) 2011 Jean-Pierre Charras, <[email protected]>
* Copyright (C) 1992-2021 KiCad Developers, see AUTHORS.txt for contributors.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, you may find one here:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
* or you may search the http://www.gnu.org website for the version 2 license,
* or you may write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
*/
/*
* @file footprint_info.h
*/
#ifndef FOOTPRINT_INFO_H_
#define FOOTPRINT_INFO_H_
#include <boost/ptr_container/ptr_vector.hpp>
#include <import_export.h>
#include <ki_exception.h>
#include <sync_queue.h>
#include <lib_tree_item.h>
#include <atomic>
#include <functional>
#include <memory>
class FP_LIB_TABLE;
class FOOTPRINT_LIST;
class FOOTPRINT_LIST_IMPL;
class FOOTPRINT_ASYNC_LOADER;
class PROGRESS_REPORTER;
class wxTopLevelWindow;
class KIWAY;
class wxTextFile;
/*
* Helper class to handle the list of footprints available in libraries. It stores
* footprint names, doc and keywords.
*
* This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
* To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
*/
class APIEXPORT FOOTPRINT_INFO : public LIB_TREE_ITEM
{
public:
virtual ~FOOTPRINT_INFO()
{
}
// These two accessors do not have to call ensure_loaded(), because constructor
// fills in these fields:
const wxString& GetFootprintName() const { return m_fpname; }
wxString GetLibNickname() const override { return m_nickname; }
wxString GetName() const override { return m_fpname; }
LIB_ID GetLibId() const override
{
return LIB_ID( m_nickname, m_fpname );
}
wxString GetDescription() override
{
ensure_loaded();
return m_doc;
}
wxString GetKeywords()
{
ensure_loaded();
return m_keywords;
}
wxString GetSearchText() override
{
// Matches are scored by offset from front of string, so inclusion of this spacer
// discounts matches found after it.
static const wxString discount( wxT( " " ) );
return GetKeywords() + discount + GetDescription();
}
unsigned GetPadCount()
{
ensure_loaded();
return m_pad_count;
}
unsigned GetUniquePadCount()
{
ensure_loaded();
return m_unique_pad_count;
}
int GetOrderNum()
{
ensure_loaded();
return m_num;
}
/**
* Test if the #FOOTPRINT_INFO object was loaded from \a aLibrary.
*
* @param aLibrary is the nickname of the library to test.
*
* @return true if the #FOOTPRINT_INFO object was loaded from \a aLibrary. Otherwise
* false.
*/
bool InLibrary( const wxString& aLibrary ) const;
/**
* Less than comparison operator, intended for sorting FOOTPRINT_INFO objects
*/
friend bool operator<( const FOOTPRINT_INFO& lhs, const FOOTPRINT_INFO& rhs );
protected:
void ensure_loaded()
{
if( !m_loaded )
load();
}
/// lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
virtual void load() { };
FOOTPRINT_LIST* m_owner; ///< provides access to FP_LIB_TABLE
bool m_loaded;
wxString m_nickname; ///< library as known in FP_LIB_TABLE
wxString m_fpname; ///< Module name.
int m_num; ///< Order number in the display list.
unsigned m_pad_count; ///< Number of pads
unsigned m_unique_pad_count; ///< Number of unique pads
wxString m_doc; ///< Footprint description.
wxString m_keywords; ///< Footprint keywords.
};
/**
* Holds a list of #FOOTPRINT_INFO objects, along with a list of IO_ERRORs or
* PARSE_ERRORs that were thrown acquiring the FOOTPRINT_INFOs.
*
* This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
* To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
*/
class APIEXPORT FOOTPRINT_LIST
{
public:
typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
typedef SYNC_QUEUE<std::unique_ptr<IO_ERROR>> ERRLIST;
FOOTPRINT_LIST() : m_lib_table( nullptr )
{
}
virtual ~FOOTPRINT_LIST()
{
}
virtual void WriteCacheToFile( const wxString& aFilePath ) {};
virtual void ReadCacheFromFile( const wxString& aFilePath ){};
/**
* @return the number of items stored in list
*/
unsigned GetCount() const
{
return m_list.size();
}
/// Was forced to add this by modview_frame.cpp
const FPILIST& GetList() const
{
return m_list;
}
/**
* @return Clears the footprint info cache
*/
void Clear()
{
m_list.clear();
}
/**
* Get info for a footprint by id.
*/
FOOTPRINT_INFO* GetFootprintInfo( const wxString& aFootprintName );
/**
* Get info for a footprint by libNickname/footprintName
*/
FOOTPRINT_INFO* GetFootprintInfo( const wxString& aLibNickname,
const wxString& aFootprintName );
/**
* Get info for a footprint by index.
*
* @param aIdx index of the given item.
* @return the aIdx item in list.
*/
FOOTPRINT_INFO& GetItem( unsigned aIdx ) const
{
return *m_list[aIdx];
}
unsigned GetErrorCount() const
{
return m_errors.size();
}
std::unique_ptr<IO_ERROR> PopError()
{
std::unique_ptr<IO_ERROR> error;
m_errors.pop( error );
return error;
}
/**
* Read all the footprints provided by the combination of aTable and aNickname.
*
* @param aTable defines all the libraries.
* @param aNickname is the library to read from, or if NULL means read all footprints
* from all known libraries in aTable.
* @param aProgressReporter is an optional progress reporter. ReadFootprintFiles() will
* use 2 phases within the reporter.
* @return true if it ran to completion, else false if it aborted after some number of
* errors. If true, it does not mean there were no errors, check GetErrorCount()
* for that, should be zero to indicate success.
*/
virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
void DisplayErrors( wxTopLevelWindow* aCaller = nullptr );
FP_LIB_TABLE* GetTable() const
{
return m_lib_table;
}
/**
* Factory function to return a #FOOTPRINT_LIST via Kiway.
*
* This is not guaranteed to succeed and will return null if the kiface is not available.
*
* @param aKiway active kiway instance.
*/
static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
protected:
/**
* Launch worker threads to load footprints. Part of the #FOOTPRINT_ASYNC_LOADER
* implementation.
*/
virtual void startWorkers( FP_LIB_TABLE* aTable, const wxString* aNickname,
FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) = 0;
/**
* Join worker threads. Part of the FOOTPRINT_ASYNC_LOADER implementation.
*/
virtual bool joinWorkers() = 0;
/**
* Stop worker threads. Part of the FOOTPRINT_ASYNC_LOADER implementation.
*/
virtual void stopWorkers() = 0;
private:
friend class FOOTPRINT_ASYNC_LOADER;
protected:
FP_LIB_TABLE* m_lib_table; ///< no ownership
FPILIST m_list;
ERRLIST m_errors; ///< some can be PARSE_ERRORs also
};
/**
* Object used to populate a #FOOTPRINT_LIST asynchronously.
*
* Construct one, calling #Start(), and then waiting until it reports completion. This is
* equivalent to calling #FOOTPRINT_LIST::ReadFootprintFiles().
*/
class APIEXPORT FOOTPRINT_ASYNC_LOADER
{
public:
/**
* Construct an asynchronous loader.
*/
FOOTPRINT_ASYNC_LOADER();
~FOOTPRINT_ASYNC_LOADER();
/**
* Assign a FOOTPRINT_LIST to the loader. This does not take ownership of
* the list.
*/
void SetList( FOOTPRINT_LIST* aList );
/**
* Launch the worker threads.
*
* @param aTable defines all the libraries.
* @param aNickname is the library to read from, or if NULL means read all footprints from
* all known libraries in \a aTable.
* @param aNThreads is the number of worker threads.
*/
void Start( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
unsigned aNThreads = DEFAULT_THREADS );
/**
* Wait until the worker threads are finished, and then perform any required
* single-threaded finishing on the list. This must be called before using
* the list, even if the completion callback was used!
*
* It is safe to call this method from a thread, but it is not safe to use
* the list from ANY thread until it completes. It is recommended to call
* this from the main thread because of this.
*
* It is safe to call this multiple times, but after the first it will
* always return true.
*
* @return true if no errors occurred
*/
bool Join();
/**
* Safely stop the current process.
*/
void Abort();
private:
/**
* Default number of worker threads. Determined empirically (by dickelbeck):
* More than 6 is not significantly faster, less than 6 is likely slower.
*/
static constexpr unsigned DEFAULT_THREADS = 6;
friend class FOOTPRINT_LIST;
friend class FOOTPRINT_LIST_IMPL;
FOOTPRINT_LIST* m_list;
std::string m_last_table;
int m_total_libs;
};
#endif // FOOTPRINT_INFO_H_