You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

248 lines
7.0 KiB

  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-2011 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. * @file footprint_info.h
  26. */
  27. #ifndef FOOTPRINT_INFO_H_
  28. #define FOOTPRINT_INFO_H_
  29. #include <boost/ptr_container/ptr_vector.hpp>
  30. #include <boost/foreach.hpp>
  31. #include <ki_mutex.h>
  32. #include <kicad_string.h>
  33. #define USE_FPI_LAZY 0 // 1:yes lazy, 0:no early
  34. class FP_LIB_TABLE;
  35. class FOOTPRINT_LIST;
  36. class wxTopLevelWindow;
  37. /*
  38. * Class FOOTPRINT_INFO
  39. * is a helper class to handle the list of footprints available in libraries. It stores
  40. * footprint names, doc and keywords
  41. */
  42. class FOOTPRINT_INFO
  43. {
  44. friend bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 );
  45. public:
  46. // These two accessors do not have to call ensure_loaded(), because constructor
  47. // fills in these fields:
  48. const wxString& GetFootprintName() const { return m_fpname; }
  49. const wxString& GetNickname() const { return m_nickname; }
  50. FOOTPRINT_INFO( FOOTPRINT_LIST* aOwner, const wxString& aNickname, const wxString& aFootprintName ) :
  51. m_owner( aOwner ),
  52. m_loaded( false ),
  53. m_nickname( aNickname ),
  54. m_fpname( aFootprintName ),
  55. m_num( 0 ),
  56. m_pad_count( 0 )
  57. {
  58. #if !USE_FPI_LAZY
  59. load();
  60. #endif
  61. }
  62. const wxString& GetDoc()
  63. {
  64. ensure_loaded();
  65. return m_doc;
  66. }
  67. const wxString& GetKeywords()
  68. {
  69. ensure_loaded();
  70. return m_keywords;
  71. }
  72. unsigned GetPadCount()
  73. {
  74. ensure_loaded();
  75. return m_pad_count;
  76. }
  77. int GetOrderNum()
  78. {
  79. ensure_loaded();
  80. return m_num;
  81. }
  82. /**
  83. * Function InLibrary
  84. * tests if the #FOOTPRINT_INFO object was loaded from \a aLibrary.
  85. *
  86. * @param aLibrary is the nickname of the library to test.
  87. *
  88. * @return true if the #FOOTPRINT_INFO object was loaded from \a aLibrary. Otherwise
  89. * false.
  90. */
  91. bool InLibrary( const wxString& aLibrary ) const;
  92. private:
  93. void ensure_loaded()
  94. {
  95. if( !m_loaded )
  96. load();
  97. }
  98. /// lazily load stuff not filled in by constructor. This may throw IO_ERRORS.
  99. void load();
  100. FOOTPRINT_LIST* m_owner; ///< provides access to FP_LIB_TABLE
  101. bool m_loaded;
  102. wxString m_nickname; ///< library as known in FP_LIB_TABLE
  103. wxString m_fpname; ///< Module name.
  104. int m_num; ///< Order number in the display list.
  105. int m_pad_count; ///< Number of pads
  106. wxString m_doc; ///< Footprint description.
  107. wxString m_keywords; ///< Footprint keywords.
  108. };
  109. /// FOOTPRINT object list sort function.
  110. inline bool operator<( const FOOTPRINT_INFO& item1, const FOOTPRINT_INFO& item2 )
  111. {
  112. int retv = StrNumCmp( item1.m_nickname, item2.m_nickname, INT_MAX, true );
  113. if( retv != 0 )
  114. return retv < 0;
  115. return StrNumCmp( item1.m_fpname, item2.m_fpname, INT_MAX, true ) < 0;
  116. }
  117. /**
  118. * Class FOOTPRINT_LIST
  119. * holds a list of FOOTPRINT_INFO objects, along with a list of IO_ERRORs or
  120. * PARSE_ERRORs that were thrown acquiring the FOOTPRINT_INFOs.
  121. */
  122. class FOOTPRINT_LIST
  123. {
  124. FP_LIB_TABLE* m_lib_table; ///< no ownership
  125. volatile int m_error_count; ///< thread safe to read.
  126. typedef boost::ptr_vector< FOOTPRINT_INFO > FPILIST;
  127. typedef boost::ptr_vector< IO_ERROR > ERRLIST;
  128. FPILIST m_list;
  129. ERRLIST m_errors; ///< some can be PARSE_ERRORs also
  130. MUTEX m_errors_lock;
  131. MUTEX m_list_lock;
  132. /**
  133. * Function loader_job
  134. * loads footprints from @a aNicknameList and calls AddItem() on to help fill
  135. * m_list.
  136. *
  137. * @param aNicknameList is a wxString[] holding libraries to load all footprints from.
  138. * @param aJobZ is the size of the job, i.e. the count of nicknames.
  139. */
  140. void loader_job( const wxString* aNicknameList, int aJobZ );
  141. void addItem( FOOTPRINT_INFO* aItem )
  142. {
  143. // m_list is not thread safe, and this function is called from
  144. // worker threads, lock m_list.
  145. MUTLOCK lock( m_list_lock );
  146. m_list.push_back( aItem );
  147. }
  148. public:
  149. FOOTPRINT_LIST() :
  150. m_lib_table( 0 ),
  151. m_error_count( 0 )
  152. {
  153. }
  154. /**
  155. * Function GetCount
  156. * @return the number of items stored in list
  157. */
  158. unsigned GetCount() const { return m_list.size(); }
  159. /// Was forced to add this by modview_frame.cpp
  160. const FPILIST& GetList() const { return m_list; }
  161. /**
  162. * Function GetModuleInfo
  163. * @param aFootprintName = the footprint name inside the FOOTPRINT_INFO of interest.
  164. * @return FOOTPRINT_INF* - the item stored in list if found
  165. */
  166. FOOTPRINT_INFO* GetModuleInfo( const wxString& aFootprintName );
  167. /**
  168. * Function GetItem
  169. * @param aIdx = index of the given item
  170. * @return the aIdx item in list
  171. */
  172. FOOTPRINT_INFO& GetItem( unsigned aIdx ) { return m_list[aIdx]; }
  173. /**
  174. * Function AddItem
  175. * add aItem in list
  176. * @param aItem = item to add
  177. */
  178. void AddItem( FOOTPRINT_INFO* aItem );
  179. unsigned GetErrorCount() const { return m_errors.size(); }
  180. const IO_ERROR* GetError( unsigned aIdx ) const { return &m_errors[aIdx]; }
  181. /**
  182. * Function ReadFootprintFiles
  183. * reads all the footprints provided by the combination of aTable and aNickname.
  184. *
  185. * @param aTable defines all the libraries.
  186. * @param aNickname is the library to read from, or if NULL means read all
  187. * footprints from all known libraries in aTable.
  188. * @return bool - true if it ran to completion, else false if it aborted after
  189. * some number of errors. If true, it does not mean there were no errors, check
  190. * GetErrorCount() for that, should be zero to indicate success.
  191. */
  192. bool ReadFootprintFiles( FP_LIB_TABLE* aTable, const wxString* aNickname = NULL );
  193. void DisplayErrors( wxTopLevelWindow* aCaller = NULL );
  194. FP_LIB_TABLE* GetTable() const { return m_lib_table; }
  195. };
  196. #endif // FOOTPRINT_INFO_H_