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 PROGRESS_REPORTER;
54 class wxTopLevelWindow;
55 class KIWAY;
56 
57 
58 /*
59  * Helper class to handle the list of footprints available in libraries. It stores
60  * footprint names, doc and keywords.
61  *
62  * This is a virtual class; its implementation lives in pcbnew/footprint_info_impl.cpp.
63  * To get instances of these classes, see FOOTPRINT_LIST::GetInstance().
64  */
66 {
67  friend bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 );
68 
69 public:
70  virtual ~FOOTPRINT_INFO()
71  {
72  }
73 
74  // These two accessors do not have to call ensure_loaded(), because constructor
75  // fills in these fields:
76 
77  const wxString& GetFootprintName() const
78  {
79  return m_fpname;
80  }
81 
82  const wxString& GetNickname() const
83  {
84  return m_nickname;
85  }
86 
87  const wxString& GetDoc()
88  {
89  ensure_loaded();
90  return m_doc;
91  }
92 
93  const wxString& GetKeywords()
94  {
95  ensure_loaded();
96  return m_keywords;
97  }
98 
99  unsigned GetPadCount()
100  {
101  ensure_loaded();
102  return m_pad_count;
103  }
104 
105  unsigned GetUniquePadCount()
106  {
107  ensure_loaded();
108  return m_unique_pad_count;
109  }
110 
112  {
113  ensure_loaded();
114  return m_num;
115  }
116 
125  bool InLibrary( const wxString& aLibrary ) const;
126 
127 protected:
129  {
130  if( !m_loaded )
131  load();
132  }
133 
135  virtual void load() = 0;
136 
138 
139  bool m_loaded;
140 
141  wxString m_nickname;
142  wxString m_fpname;
143  int m_num;
146  wxString m_doc;
147  wxString m_keywords;
148 };
149 
150 
152 inline bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 )
153 {
154  int retv = StrNumCmp( item1.m_nickname, item2.m_nickname, INT_MAX, true );
155 
156  if( retv != 0 )
157  return retv < 0;
158 
159  return StrNumCmp( item1.m_fpname, item2.m_fpname, INT_MAX, true ) < 0;
160 }
161 
162 
171 {
173 
174 protected:
176 
177  typedef std::vector<std::unique_ptr<FOOTPRINT_INFO>> FPILIST;
179 
180  FPILIST m_list;
181  ERRLIST m_errors;
182 
184 
185 
186 public:
187  FOOTPRINT_LIST() : m_lib_table( 0 )
188  {
189  }
190 
191  virtual ~FOOTPRINT_LIST()
192  {
193  }
194 
198  unsigned GetCount() const
199  {
200  return m_list.size();
201  }
202 
204  const FPILIST& GetList() const
205  {
206  return m_list;
207  }
208 
214  FOOTPRINT_INFO* GetModuleInfo( const wxString& aFootprintName );
215 
221  FOOTPRINT_INFO& GetItem( unsigned aIdx )
222  {
223  return *m_list[aIdx];
224  }
225 
230  void AddItem( FOOTPRINT_INFO* aItem );
231 
232  unsigned GetErrorCount() const
233  {
234  return m_errors.size();
235  }
236 
237  std::unique_ptr<IO_ERROR> PopError()
238  {
239  std::unique_ptr<IO_ERROR> error;
240 
241  m_errors.pop( error );
242  return error;
243  }
244 
257  virtual bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = nullptr,
258  PROGRESS_REPORTER* aProgressReporter = nullptr ) = 0;
259 
260  void DisplayErrors( wxTopLevelWindow* aCaller = NULL );
261 
263  {
264  return m_lib_table;
265  }
266 
273  static FOOTPRINT_LIST* GetInstance( KIWAY& aKiway );
274 
275 protected:
280  virtual void StartWorkers( FP_LIB_TABLE* aTable, wxString const* aNickname,
281  FOOTPRINT_ASYNC_LOADER* aLoader, unsigned aNThreads ) = 0;
282 
286  virtual bool JoinWorkers() = 0;
287 };
288 
289 
296 {
297  friend class FOOTPRINT_LIST;
298  friend class FOOTPRINT_LIST_IMPL;
299 
301  std::function<void()> m_completion_cb;
302  std::string m_last_table;
303 
304  bool m_started;
306 
307 public:
312 
317  void SetList( FOOTPRINT_LIST* aList );
318 
326  void Start( FP_LIB_TABLE* aTable, wxString const* aNickname = nullptr,
327  unsigned aNThreads = DEFAULT_THREADS );
328 
343  bool Join();
344 
351  void SetCompletionCallback( std::function<void()> aCallback );
352 
358  bool IsSameTable( FP_LIB_TABLE* aOther );
359 
364  static constexpr unsigned DEFAULT_THREADS = 6;
365 };
366 
367 
368 #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
A progress reporter for use in multi-threaded environments.
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:258
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 StrNumCmp 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