KiCad PCB EDA Suite
footprint_info.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) 2011 Jean-Pierre Charras, <jp.charras@wanadoo.fr>
5  * Copyright (C) 1992-2019 KiCad Developers, see AUTHORS.txt for contributors.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, you may find one here:
19  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
20  * or you may search the http://www.gnu.org website for the version 2 license,
21  * or you may write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23  */
24 
25 /*
26  * @file footprint_info.h
27  */
28 
29 #ifndef FOOTPRINT_INFO_H_
30 #define FOOTPRINT_INFO_H_
31 
32 
33 #include <boost/ptr_container/ptr_vector.hpp>
34 #include <import_export.h>
35 #include <ki_exception.h>
36 #include <kicad_string.h>
37 #include <sync_queue.h>
38 #include <lib_tree_item.h>
39 #include <atomic>
40 #include <functional>
41 #include <memory>
42 
43 
44 class FP_LIB_TABLE;
45 class FOOTPRINT_LIST;
48 class PROGRESS_REPORTER;
49 class wxTopLevelWindow;
50 class KIWAY;
51 
52 
53 /*
54  * Helper class to handle the list of footprints available in libraries. It stores
55  * footprint names, doc and keywords.
56  *
57  * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
58  * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
59  */
61 {
62  friend bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 );
63 
64 public:
65  virtual ~FOOTPRINT_INFO()
66  {
67  }
68 
69  // These two accessors do not have to call ensure_loaded(), because constructor
70  // fills in these fields:
71 
72  const wxString& GetFootprintName() const
73  {
74  return m_fpname;
75  }
76 
77  wxString GetLibNickname() const override
78  {
79  return m_nickname;
80  }
81 
82  wxString GetName() const override
83  {
84  return m_fpname;
85  }
86 
87  LIB_ID GetLibId() const override
88  {
89  return LIB_ID( m_nickname, m_fpname );
90  }
91 
92  wxString GetDescription() override
93  {
94  ensure_loaded();
95  return m_doc;
96  }
97 
98  wxString GetKeywords()
99  {
100  ensure_loaded();
101  return m_keywords;
102  }
103 
104  wxString GetSearchText() override
105  {
106  // Matches are scored by offset from front of string, so inclusion of this spacer
107  // discounts matches found after it.
108  static const wxString discount( wxT( " " ) );
109 
110  return GetKeywords() + discount + GetDescription();
111  }
112 
113  unsigned GetPadCount()
114  {
115  ensure_loaded();
116  return m_pad_count;
117  }
118 
119  unsigned GetUniquePadCount()
120  {
121  ensure_loaded();
122  return m_unique_pad_count;
123  }
124 
126  {
127  ensure_loaded();
128  return m_num;
129  }
130 
139  bool InLibrary( const wxString& aLibrary ) const;
140 
141 protected:
143  {
144  if( !m_loaded )
145  load();
146  }
147 
149  virtual void load() { };
150 
151  FOOTPRINT_LIST* m_owner;
152 
153  bool m_loaded;
154 
155  wxString m_nickname;
156  wxString m_fpname;
157  int m_num;
158  unsigned m_pad_count;
160  wxString m_doc;
161  wxString m_keywords;
162 };
163 
164 
166 inline bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 )
167 {
168  int retv = StrNumCmp( item1.m_nickname, item2.m_nickname, false );
169 
170  if( retv != 0 )
171  return retv < 0;
172 
173  // Technically footprint names are not case sensitive because the file name is used
174  // as the footprint name. On windows this would be problematic because windows does
175  // not support case sensitive file names by default. This should not cause any issues
176  // and allow for a future change to use the name defined in the footprint file.
177  return StrNumCmp( item1.m_fpname, item2.m_fpname, false ) < 0;
178 }
179 
180 
189 {
191 
192 protected:
194 
195  typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
197 
200 
201 public:
202  FOOTPRINT_LIST() : m_lib_table( 0 )
203  {
204  }
205 
206  virtual ~FOOTPRINT_LIST()
207  {
208  }
209 
210  virtual void WriteCacheToFile( wxTextFile* aFile ) { };
211  virtual void ReadCacheFromFile( wxTextFile* aFile ) { };
212 
216  unsigned GetCount() const
217  {
218  return m_list.size();
219  }
220 
222  const FPILIST& GetList() const
223  {
224  return m_list;
225  }
226 
230  FOOTPRINT_INFO* GetModuleInfo( const wxString& aFootprintId );
231 
235  FOOTPRINT_INFO* GetModuleInfo( const wxString& aLibNickname, const wxString& aFootprintName );
236 
242  FOOTPRINT_INFO& GetItem( unsigned aIdx )
243  {
244  return *m_list[aIdx];
245  }
246 
247  unsigned GetErrorCount() const
248  {
249  return m_errors.size();
250  }
251 
252  std::unique_ptr<IO_ERROR> PopError()
253  {
254  std::unique_ptr<IO_ERROR> error;
255 
256  m_errors.pop( error );
257  return error;
258  }
259 
272  virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
273  PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
274 
275  void DisplayErrors( wxTopLevelWindow* aCaller = NULL );
276 
278  {
279  return m_lib_table;
280  }
281 
288  static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
289 
290 protected:
295  virtual void StartWorkers( FP_LIB_TABLE* aTable, wxString const* aNickname,
296  FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) = 0;
297 
301  virtual bool JoinWorkers() = 0;
302 
306  virtual void StopWorkers() = 0;
307 };
308 
309 
316 {
317  friend class FOOTPRINT_LIST;
318  friend class FOOTPRINT_LIST_IMPL;
319 
321  std::string m_last_table;
322 
324 
325 public:
330 
332 
337  void SetList( FOOTPRINT_LIST* aList );
338 
346  void Start( FP_LIB_TABLE* aTable, wxString const* aNickname = nullptr,
347  unsigned aNThreads = DEFAULT_THREADS );
348 
363  bool Join();
364 
368  void Abort();
369 
374  static constexpr unsigned DEFAULT_THREADS = 6;
375 };
376 
377 
378 #endif // FOOTPRINT_INFO_H_
std::vector< std::unique_ptr< FOOTPRINT_INFO > > FPILIST
unsigned GetPadCount()
LIB_ID GetLibId() const override
#define APIEXPORT
Macros which export functions from a DLL/DSO.
Definition: import_export.h:44
int StrNumCmp(const wxString &aString1, const wxString &aString2, bool aIgnoreCase)
Compare two strings with alphanumerical content.
Definition: string.cpp:388
bool pop(T &aReceiver)
Pop a value off the queue into the provided variable.
Definition: sync_queue.h:66
wxString GetLibNickname() const override
A progress reporter for use in multi-threaded environments.
A mix-in to provide polymorphism between items stored in libraries (symbols, aliases and footprints).
Definition: lib_tree_item.h:39
ERRLIST m_errors
some can be PARSE_ERRORs also
const FPILIST & GetList() const
Was forced to add this by modview_frame.cpp.
wxString GetSearchText() override
FP_LIB_TABLE * m_lib_table
no ownership
Synchronized, locking queue.
Definition: sync_queue.h:30
wxString GetDescription() override
This class can be used to populate a FOOTPRINT_LIST asynchronously.
unsigned m_pad_count
Number of pads.
int m_num
Order number in the display list.
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
FOOTPRINT_LIST * m_list
virtual void WriteCacheToFile(wxTextFile *aFile)
virtual void ReadCacheFromFile(wxTextFile *aFile)
wxString m_doc
Footprint description.
unsigned GetCount() const
FOOTPRINT_INFO & GetItem(unsigned aIdx)
Get info for a module by index.
#define NULL
wxString m_fpname
Module name.
wxString GetName() const override
SYNC_QUEUE< std::unique_ptr< IO_ERROR > > ERRLIST
KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within the s...
Definition: kiway.h:273
virtual wxString GetDescription()=0
virtual ~FOOTPRINT_LIST()
virtual ~FOOTPRINT_INFO()
wxString m_keywords
Footprint keywords.
std::unique_ptr< IO_ERROR > PopError()
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
unsigned GetErrorCount() const
unsigned GetUniquePadCount()
bool operator<(const FOOTPRINT_INFO &item1, const FOOTPRINT_INFO &item2)
FOOTPRINT object list sort function.
friend class FOOTPRINT_ASYNC_LOADER
wxString GetKeywords()
FP_LIB_TABLE * GetTable() const
wxString m_nickname
library as known in FP_LIB_TABLE
unsigned m_unique_pad_count
Number of unique pads.
const wxString & GetFootprintName() const