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-2017 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 <ki_mutex.h>
38 #include <kicad_string.h>
39 #include <sync_queue.h>
40 #include <lib_tree_item.h>
41 
42 #include <atomic>
43 #include <functional>
44 #include <memory>
45 
46 
47 class FP_LIB_TABLE;
48 class FOOTPRINT_LIST;
51 class PROGRESS_REPORTER;
52 class wxTopLevelWindow;
53 class KIWAY;
54 
55 
56 /*
57  * Helper class to handle the list of footprints available in libraries. It stores
58  * footprint names, doc and keywords.
59  *
60  * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
61  * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
62  */
64 {
65  friend bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 );
66 
67 public:
68  virtual ~FOOTPRINT_INFO()
69  {
70  }
71 
72  // These two accessors do not have to call ensure_loaded(), because constructor
73  // fills in these fields:
74 
75  const wxString& GetFootprintName() const
76  {
77  return m_fpname;
78  }
79 
80  wxString GetLibNickname() const override
81  {
82  return m_nickname;
83  }
84 
85  const wxString& GetName() const override
86  {
87  return m_fpname;
88  }
89 
90  LIB_ID GetLibId() const override
91  {
92  return LIB_ID( m_nickname, m_fpname );
93  }
94 
95  const wxString& GetDescription() override
96  {
97  ensure_loaded();
98  return m_doc;
99  }
100 
101  const wxString& GetKeywords()
102  {
103  ensure_loaded();
104  return m_keywords;
105  }
106 
107  wxString GetSearchText() override
108  {
109  // Matches are scored by offset from front of string, so inclusion of this spacer
110  // discounts matches found after it.
111  static const wxString discount( wxT( " " ) );
112 
113  return GetKeywords() + discount + GetDescription();
114  }
115 
116  unsigned GetPadCount()
117  {
118  ensure_loaded();
119  return m_pad_count;
120  }
121 
122  unsigned GetUniquePadCount()
123  {
124  ensure_loaded();
125  return m_unique_pad_count;
126  }
127 
129  {
130  ensure_loaded();
131  return m_num;
132  }
133 
142  bool InLibrary( const wxString& aLibrary ) const;
143 
144 protected:
146  {
147  if( !m_loaded )
148  load();
149  }
150 
152  virtual void load() { };
153 
155 
156  bool m_loaded;
157 
158  wxString m_nickname;
159  wxString m_fpname;
160  int m_num;
161  unsigned m_pad_count;
163  wxString m_doc;
164  wxString m_keywords;
165 };
166 
167 
169 inline bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 )
170 {
171  int retv = StrNumCmp( item1.m_nickname, item2.m_nickname, INT_MAX, true );
172 
173  if( retv != 0 )
174  return retv < 0;
175 
176  return StrNumCmp( item1.m_fpname, item2.m_fpname, INT_MAX, true ) < 0;
177 }
178 
179 
188 {
190 
191 protected:
193 
194  typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
196 
197  FPILIST m_list;
198  ERRLIST m_errors;
199 
200 public:
201  FOOTPRINT_LIST() : m_lib_table( 0 )
202  {
203  }
204 
205  virtual ~FOOTPRINT_LIST()
206  {
207  }
208 
209  virtual void WriteCacheToFile( wxTextFile* aFile ) { };
210  virtual void ReadCacheFromFile( wxTextFile* aFile ) { };
211 
215  unsigned GetCount() const
216  {
217  return m_list.size();
218  }
219 
221  const FPILIST& GetList() const
222  {
223  return m_list;
224  }
225 
229  FOOTPRINT_INFO* GetModuleInfo( const wxString& aFootprintId );
230 
234  FOOTPRINT_INFO* GetModuleInfo( const wxString& aLibNickname, const wxString& aFootprintName );
235 
241  FOOTPRINT_INFO& GetItem( unsigned aIdx )
242  {
243  return *m_list[aIdx];
244  }
245 
246  unsigned GetErrorCount() const
247  {
248  return m_errors.size();
249  }
250 
251  std::unique_ptr<IO_ERROR> PopError()
252  {
253  std::unique_ptr<IO_ERROR> error;
254 
255  m_errors.pop( error );
256  return error;
257  }
258 
271  virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
272  PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
273 
274  void DisplayErrors( wxTopLevelWindow* aCaller = NULL );
275 
277  {
278  return m_lib_table;
279  }
280 
287  static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
288 
289 protected:
294  virtual void StartWorkers( FP_LIB_TABLE* aTable, wxString const* aNickname,
295  FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) = 0;
296 
300  virtual bool JoinWorkers() = 0;
301 
305  virtual void StopWorkers() = 0;
306 };
307 
308 
315 {
316  friend class FOOTPRINT_LIST;
317  friend class FOOTPRINT_LIST_IMPL;
318 
320  std::string m_last_table;
321 
323 
324 public:
329 
331 
336  void SetList( FOOTPRINT_LIST* aList );
337 
345  void Start( FP_LIB_TABLE* aTable, wxString const* aNickname = nullptr,
346  unsigned aNThreads = DEFAULT_THREADS );
347 
362  bool Join();
363 
367  void Abort();
368 
373  static constexpr unsigned DEFAULT_THREADS = 6;
374 };
375 
376 
377 #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
const FPILIST & GetList() const
Was forced to add this by modview_frame.cpp.
const wxString & GetFootprintName() const
size_t size() const
Return the size of the queue.
Definition: sync_queue.h:94
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
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
FOOTPRINT_LIST * m_owner
provides access to FP_LIB_TABLE
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:258
const wxString & GetKeywords()
virtual const wxString & GetDescription()=0
virtual ~FOOTPRINT_LIST()
virtual ~FOOTPRINT_INFO()
static bool Join(wxPoint *aIntersectPoint, wxPoint a0, wxPoint a1, wxPoint b0, wxPoint b1)
Function Join finds the point where line segment (b1,b0) intersects with segment (a1,a0).
wxString m_keywords
Footprint keywords.
std::unique_ptr< IO_ERROR > PopError()
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
int StrNumCmp(const wxString &aString1, const wxString &aString2, int aLength, bool aIgnoreCase)
Function StrNumCmp is a routine compatible with qsort() to sort by alphabetical order.
Definition: string.cpp:315
wxString m_nickname
library as known in FP_LIB_TABLE
unsigned m_unique_pad_count
Number of unique pads.
FP_LIB_TABLE * GetTable() const