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 
35 #include <import_export.h>
36 #include <ki_exception.h>
37 #include <kicad_string.h>
38 #include <sync_queue.h>
39 #include <lib_tree_item.h>
40 
41 #include <atomic>
42 #include <functional>
43 #include <memory>
44 
45 
46 class FP_LIB_TABLE;
47 class FOOTPRINT_LIST;
50 class PROGRESS_REPORTER;
51 class wxTopLevelWindow;
52 class KIWAY;
53 
54 
55 /*
56  * Helper class to handle the list of footprints available in libraries. It stores
57  * footprint names, doc and keywords.
58  *
59  * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
60  * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
61  */
63 {
64  friend bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 );
65 
66 public:
67  virtual ~FOOTPRINT_INFO()
68  {
69  }
70 
71  // These two accessors do not have to call ensure_loaded(), because constructor
72  // fills in these fields:
73 
74  const wxString& GetFootprintName() const
75  {
76  return m_fpname;
77  }
78 
79  wxString GetLibNickname() const override
80  {
81  return m_nickname;
82  }
83 
84  const wxString& GetName() const override
85  {
86  return m_fpname;
87  }
88 
89  LIB_ID GetLibId() const override
90  {
91  return LIB_ID( m_nickname, m_fpname );
92  }
93 
94  const wxString& GetDescription() override
95  {
96  ensure_loaded();
97  return m_doc;
98  }
99 
100  const wxString& GetKeywords()
101  {
102  ensure_loaded();
103  return m_keywords;
104  }
105 
106  wxString GetSearchText() override
107  {
108  // Matches are scored by offset from front of string, so inclusion of this spacer
109  // discounts matches found after it.
110  static const wxString discount( wxT( " " ) );
111 
112  return GetKeywords() + discount + GetDescription();
113  }
114 
115  unsigned GetPadCount()
116  {
117  ensure_loaded();
118  return m_pad_count;
119  }
120 
121  unsigned GetUniquePadCount()
122  {
123  ensure_loaded();
124  return m_unique_pad_count;
125  }
126 
128  {
129  ensure_loaded();
130  return m_num;
131  }
132 
141  bool InLibrary( const wxString& aLibrary ) const;
142 
143 protected:
145  {
146  if( !m_loaded )
147  load();
148  }
149 
151  virtual void load() { };
152 
153  FOOTPRINT_LIST* m_owner;
154 
155  bool m_loaded;
156 
157  wxString m_nickname;
158  wxString m_fpname;
159  int m_num;
160  unsigned m_pad_count;
162  wxString m_doc;
163  wxString m_keywords;
164 };
165 
166 
168 inline bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 )
169 {
170  int retv = StrNumCmp( item1.m_nickname, item2.m_nickname, false );
171 
172  if( retv != 0 )
173  return retv < 0;
174 
175  // Technically footprint names are not case sensitive because the file name is used
176  // as the footprint name. On windows this would be problematic because windows does
177  // not support case sensitive file names by default. This should not cause any issues
178  // and allow for a future change to use the name defined in the footprint file.
179  return StrNumCmp( item1.m_fpname, item2.m_fpname, false ) < 0;
180 }
181 
182 
191 {
193 
194 protected:
196 
197  typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
199 
202 
203 public:
204  FOOTPRINT_LIST() : m_lib_table( 0 )
205  {
206  }
207 
208  virtual ~FOOTPRINT_LIST()
209  {
210  }
211 
212  virtual void WriteCacheToFile( wxTextFile* aFile ) { };
213  virtual void ReadCacheFromFile( wxTextFile* aFile ) { };
214 
218  unsigned GetCount() const
219  {
220  return m_list.size();
221  }
222 
224  const FPILIST& GetList() const
225  {
226  return m_list;
227  }
228 
232  FOOTPRINT_INFO* GetModuleInfo( const wxString& aFootprintId );
233 
237  FOOTPRINT_INFO* GetModuleInfo( const wxString& aLibNickname, const wxString& aFootprintName );
238 
244  FOOTPRINT_INFO& GetItem( unsigned aIdx )
245  {
246  return *m_list[aIdx];
247  }
248 
249  unsigned GetErrorCount() const
250  {
251  return m_errors.size();
252  }
253 
254  std::unique_ptr<IO_ERROR> PopError()
255  {
256  std::unique_ptr<IO_ERROR> error;
257 
258  m_errors.pop( error );
259  return error;
260  }
261 
274  virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
275  PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
276 
277  void DisplayErrors( wxTopLevelWindow* aCaller = NULL );
278 
280  {
281  return m_lib_table;
282  }
283 
290  static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
291 
292 protected:
297  virtual void StartWorkers( FP_LIB_TABLE* aTable, wxString const* aNickname,
298  FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) = 0;
299 
303  virtual bool JoinWorkers() = 0;
304 
308  virtual void StopWorkers() = 0;
309 };
310 
311 
318 {
319  friend class FOOTPRINT_LIST;
320  friend class FOOTPRINT_LIST_IMPL;
321 
323  std::string m_last_table;
324 
326 
327 public:
332 
334 
339  void SetList( FOOTPRINT_LIST* aList );
340 
348  void Start( FP_LIB_TABLE* aTable, wxString const* aNickname = nullptr,
349  unsigned aNThreads = DEFAULT_THREADS );
350 
365  bool Join();
366 
370  void Abort();
371 
376  static constexpr unsigned DEFAULT_THREADS = 6;
377 };
378 
379 
380 #endif // FOOTPRINT_INFO_H_
std::vector< std::unique_ptr< FOOTPRINT_INFO > > FPILIST
unsigned GetPadCount()
LIB_ID GetLibId() const override
const wxString & GetName() 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:354
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
This class can be used to populate a FOOTPRINT_LIST asynchronously.
const wxString & GetDescription() override
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.
wxString m_fpname
Module name.
SYNC_QUEUE< std::unique_ptr< IO_ERROR > > ERRLIST
Class KIWAY is a minimalistic software bus for communications between various DLLs/DSOs (DSOs) within...
Definition: kiway.h:274
const wxString & GetKeywords()
virtual const 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
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