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 
41 #include <atomic>
42 #include <functional>
43 #include <memory>
44 
45 
46 #define USE_FPI_LAZY 0 // 1:yes lazy, 0:no early
47 
48 
49 class FP_LIB_TABLE;
50 class FOOTPRINT_LIST;
53 class wxTopLevelWindow;
54 class KIWAY;
55 
56 
57 /*
58  * Helper class to handle the list of footprints available in libraries. It stores
59  * footprint names, doc and keywords.
60  *
61  * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
62  * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
63  */
65 {
66  friend bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 );
67 
68 public:
69  virtual ~FOOTPRINT_INFO()
70  {
71  }
72 
73  // These two accessors do not have to call ensure_loaded(), because constructor
74  // fills in these fields:
75 
76  const wxString& GetFootprintName() const
77  {
78  return m_fpname;
79  }
80 
81  const wxString& GetNickname() const
82  {
83  return m_nickname;
84  }
85 
86  const wxString& GetDoc()
87  {
88  ensure_loaded();
89  return m_doc;
90  }
91 
92  const wxString& GetKeywords()
93  {
94  ensure_loaded();
95  return m_keywords;
96  }
97 
98  unsigned GetPadCount()
99  {
100  ensure_loaded();
101  return m_pad_count;
102  }
103 
104  unsigned GetUniquePadCount()
105  {
106  ensure_loaded();
107  return m_unique_pad_count;
108  }
109 
111  {
112  ensure_loaded();
113  return m_num;
114  }
115 
124  bool InLibrary( const wxString& aLibrary ) const;
125 
126 protected:
128  {
129  if( !m_loaded )
130  load();
131  }
132 
134  virtual void load() = 0;
135 
137 
138  bool m_loaded;
139 
140  wxString m_nickname;
141  wxString m_fpname;
142  int m_num;
145  wxString m_doc;
146  wxString m_keywords;
147 };
148 
149 
151 inline bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 )
152 {
153  int retv = StrNumCmp( item1.m_nickname, item2.m_nickname, INT_MAX, true );
154 
155  if( retv != 0 )
156  return retv < 0;
157 
158  return StrNumCmp( item1.m_fpname, item2.m_fpname, INT_MAX, true ) < 0;
159 }
160 
161 
170 {
172 
173 protected:
175 
176  typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
178 
179  FPILIST m_list;
180  ERRLIST m_errors;
181 
183 
184 
185 public:
186  FOOTPRINT_LIST() : m_lib_table( 0 )
187  {
188  }
189 
190  virtual ~FOOTPRINT_LIST()
191  {
192  }
193 
197  unsigned GetCount() const
198  {
199  return m_list.size();
200  }
201 
203  const FPILIST& GetList() const
204  {
205  return m_list;
206  }
207 
213  FOOTPRINT_INFO* GetModuleInfo( const wxString& aFootprintName );
214 
220  FOOTPRINT_INFO& GetItem( unsigned aIdx )
221  {
222  return *m_list[aIdx];
223  }
224 
229  void AddItem( FOOTPRINT_INFO* aItem );
230 
231  unsigned GetErrorCount() const
232  {
233  return m_errors.size();
234  }
235 
236  std::unique_ptr<IO_ERROR> PopError()
237  {
238  std::unique_ptr<IO_ERROR> error;
239 
240  m_errors.pop( error );
241  return error;
242  }
243 
254  virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = NULL ) = 0;
255 
256  void DisplayErrors( wxTopLevelWindow* aCaller = NULL );
257 
259  {
260  return m_lib_table;
261  }
262 
269  static std::unique_ptr<FOOTPRINT_LIST> GetInstance( KIWAY& aKiway );
270 
271 protected:
276  virtual void StartWorkers( FP_LIB_TABLE* aTable, wxString const* aNickname,
277  FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) = 0;
278 
282  virtual bool JoinWorkers() = 0;
283 
284 
288  virtual size_t CountFinished() = 0;
289 };
290 
291 
298 {
299  friend class FOOTPRINT_LIST;
300  friend class FOOTPRINT_LIST_IMPL;
301 
303  std::function<void()> m_completion_cb;
304  std::string m_last_table;
305 
306  bool m_started;
308 
309 public:
314 
319  void SetList( FOOTPRINT_LIST* aList );
320 
328  void Start( FP_LIB_TABLE* aTable, wxString const* aNickname = nullptr,
329  unsigned aNThreads = DEFAULT_THREADS );
330 
345  bool Join();
346 
358  int GetProgress() const;
359 
366  void SetCompletionCallback( std::function<void()> aCallback );
367 
373  bool IsSameTable( FP_LIB_TABLE* aOther );
374 
379  static constexpr unsigned DEFAULT_THREADS = 6;
380 };
381 
382 
383 #endif // FOOTPRINT_INFO_H_
std::vector< std::unique_ptr< FOOTPRINT_INFO > > FPILIST
unsigned GetPadCount()
#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
ERRLIST m_errors
some can be PARSE_ERRORs also
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.
boost::interprocess::interprocess_mutex MUTEX
Establish KiCad MUTEX choices here in this file: typedef MUTEX and typedef MUTLOCK.
Definition: ki_mutex.h:42
int m_unique_pad_count
Number of unique pads.
int m_num
Order number in the display list.
FOOTPRINT_LIST * m_list
FOOTPRINT_LIST * m_owner
provides access to FP_LIB_TABLE
wxString m_doc
Footprint description.
unsigned GetCount() const
const wxString & GetDoc()
FOOTPRINT_INFO & GetItem(unsigned aIdx)
Get info for a module by index.
bool m_started
True if Start() has been called - does not reset.
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:257
const wxString & GetKeywords()
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()
int m_pad_count
Number of pads.
Holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or PARSE_ERRORs that were thro...
std::function< void()> m_completion_cb
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
const wxString & GetNickname() const
int StrNumCmp(const wxString &aString1, const wxString &aString2, int aLength, bool aIgnoreCase)
Function StrLenNumCmp is a routine compatible with qsort() to sort by alphabetical order...
Definition: string.cpp:238
wxString m_nickname
library as known in FP_LIB_TABLE
FP_LIB_TABLE * GetTable() const