KiCad PCB EDA Suite
lib_table_base.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) 2010-2012 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
5  * Copyright (C) 2012-2017 Wayne Stambaugh <stambaughw@verizon.net>
6  * Copyright (C) 2012-2017 KiCad Developers, see change_log.txt for contributors.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, you may find one here:
20  * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
21  * or you may search the http://www.gnu.org website for the version 2 license,
22  * or you may write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24  */
25 
26 #ifndef _LIB_TABLE_BASE_H_
27 #define _LIB_TABLE_BASE_H_
28 
29 #include <map>
30 
31 #include <boost/interprocess/exceptions.hpp>
32 #include <boost/ptr_container/ptr_vector.hpp>
33 #include <boost/noncopyable.hpp>
34 
35 #include <project.h>
36 #include <properties.h>
37 #include <richio.h>
38 
39 
40 #define FP_LATE_ENVVAR 1
41 
42 class OUTPUTFORMATTER;
43 class LIB_TABLE_LEXER;
44 class LIB_ID;
45 class LIB_TABLE_ROW;
46 class IO_ERROR;
47 
48 
56 LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow );
57 
58 
65 class LIB_TABLE_ROW : boost::noncopyable
66 {
67 public:
69  {
70  }
71 
72  virtual ~LIB_TABLE_ROW()
73  {
74  }
75 
76  LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
77  const wxString& aDescr = wxEmptyString ) :
78  nickName( aNick ),
79  description( aDescr )
80  {
81  properties.reset();
82  SetOptions( aOptions );
83  SetFullURI( aURI );
84  }
85 
86  bool operator==( const LIB_TABLE_ROW& r ) const;
87 
88  bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
89 
95  const wxString& GetNickName() const { return nickName; }
96 
102  void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
103 
109  virtual const wxString GetType() const = 0;
110 
117  virtual void SetType( const wxString& aType ) = 0;
118 
127  const wxString GetFullURI( bool aSubstituted = false ) const;
128 
134  void SetFullURI( const wxString& aFullURI );
135 
142  const wxString& GetOptions() const { return options; }
143 
147  void SetOptions( const wxString& aOptions );
148 
154  const wxString& GetDescr() const { return description; }
155 
161  void SetDescr( const wxString& aDescr ) { description = aDescr; }
162 
169  const PROPERTIES* GetProperties() const { return properties.get(); }
170 
181  void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
182 
183  static void Parse( std::unique_ptr< LIB_TABLE_ROW >& aRow, LIB_TABLE_LEXER* in );
184 
186  {
187  return do_clone();
188  }
189 
190 protected:
191  LIB_TABLE_ROW( const LIB_TABLE_ROW& aRow ) :
192  nickName( aRow.nickName ),
193  uri_user( aRow.uri_user ),
195  uri_expanded( aRow.uri_expanded ),
196 #endif
197  options( aRow.options ),
198  description( aRow.description )
199  {
200  if( aRow.properties )
201  properties.reset( new PROPERTIES( *aRow.properties.get() ) );
202  else
203  properties.reset();
204  }
205 
206  void operator=( const LIB_TABLE_ROW& aRow );
207 
208 private:
209  virtual LIB_TABLE_ROW* do_clone() const = 0;
210 
211  void setProperties( PROPERTIES* aProperties );
212 
213  wxString nickName;
214  wxString uri_user;
215 
216 #if !FP_LATE_ENVVAR
217  wxString uri_expanded;
218 #endif
219 
220  wxString options;
221  wxString description;
222 
223  std::unique_ptr< PROPERTIES > properties;
224 };
225 
226 
273 class LIB_TABLE : public PROJECT::_ELEM
274 {
275  friend class DIALOG_FP_LIB_TABLE;
276  friend class FP_TBL_MODEL;
277 
278 public:
279 
294  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
295 
308  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
309 
319  LIB_TABLE( LIB_TABLE* aFallBackTable = NULL );
320 
321  virtual ~LIB_TABLE();
322 
324  void Clear()
325  {
326  rows.clear();
327  nickIndex.clear();
328  }
329 
330  bool operator==( const LIB_TABLE& r ) const
331  {
332  if( rows.size() == r.rows.size() )
333  {
334  unsigned i;
335 
336  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
337  ;
338 
339  if( i == rows.size() )
340  return true;
341  }
342 
343  return false;
344  }
345 
346  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
347 
348  int GetCount() { return rows.size(); }
349 
350  LIB_TABLE_ROW* At( int aIndex ) { return &rows[aIndex]; }
351 
360  bool IsEmpty( bool aIncludeFallback = true );
361 
368  const wxString GetDescription( const wxString& aNickname );
369 
376  std::vector<wxString> GetLogicalLibs();
377 
392  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
393 
400  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
401 
412  void Load( const wxString& aFileName ) throw( IO_ERROR );
413 
421  void Save( const wxString& aFileName ) const
422  throw( IO_ERROR, boost::interprocess::lock_exception );
423 
437  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
438 
450  static UTF8 FormatOptions( const PROPERTIES* aProperties );
451 
460  static const wxString ExpandSubstitutions( const wxString& aString );
461 
462 protected:
463 
469  LIB_TABLE_ROW* findRow( const wxString& aNickname ) const;
470 
471  void reindex()
472  {
473  nickIndex.clear();
474 
475  for( LIB_TABLE_ROWS_ITER it = rows.begin(); it != rows.end(); ++it )
476  nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - rows.begin() ) );
477  }
478 
479  void ensureIndex()
480  {
481  // The dialog lib table editor may not maintain the nickIndex.
482  // Lazy indexing may be required. To handle lazy indexing, we must enforce
483  // that "nickIndex" is either empty or accurate, but never inaccurate.
484  if( !nickIndex.size() )
485  reindex();
486  }
487 
488  typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
489  typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
490  typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
491 
492  LIB_TABLE_ROWS rows;
493 
495  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
496  typedef INDEX::iterator INDEX_ITER;
497  typedef INDEX::const_iterator INDEX_CITER;
498  typedef INDEX::value_type INDEX_VALUE;
499 
501  INDEX nickIndex;
502 
504 };
505 
506 #endif // _LIB_TABLE_BASE_H_
int GetCount()
Class UTF8 is an 8 bit std::string that is assuredly encoded in UTF8, and supplies special conversion...
Definition: utf8.h:53
void ensureIndex()
LIB_TABLE_ROW(const LIB_TABLE_ROW &aRow)
if(dlg.ShowModal()==wxID_OK)
Definition: selcolor.cpp:75
Class LIB_TABLE_ROW.
std::vector< PROPERTY > PROPERTIES
Definition: specctra.h:184
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Function Format.
void Clear()
Delete all rows.
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER
const PROPERTIES * GetProperties() const
Function GetProperties.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Function InsertRow.
Class FP_TBL_MODEL mixes in FP_LIB_TABLE into wxGridTableBase so the result can be used as a table wi...
void reindex()
LIB_TABLE_ROW * clone() const
static const wxString ExpandSubstitutions(const wxString &aString)
Function ExpandSubstitutions.
std::map< wxString, int > INDEX
this is a non-owning index into the LIB_TABLE_ROWS table
INDEX::const_iterator INDEX_CITER
void setProperties(PROPERTIES *aProperties)
virtual void SetType(const wxString &aType)=0
Function SetType.
Class DIALOG_FP_LIB_TABLE shows and edits the PCB library tables.
const wxString & GetOptions() const
Function GetOptions.
LIB_TABLE_ROW * At(int aIndex)
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
LIB_TABLE_ROWS rows
Class LIB_ID.
Definition: lib_id.h:56
virtual const wxString GetType() const =0
Function GetType.
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
std::unique_ptr< PROPERTIES > properties
LIB_TABLE * fallBack
const wxString GetFullURI(bool aSubstituted=false) const
Function GetFullURI.
static UTF8 FormatOptions(const PROPERTIES *aProperties)
Function FormatOptions.
INDEX::value_type INDEX_VALUE
virtual ~LIB_TABLE()
wxString description
static PROPERTIES * ParseOptions(const std::string &aOptionsList)
Function ParseOptions.
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
virtual void Parse(LIB_TABLE_LEXER *aLexer)=0
Function Parse.
const wxString GetDescription(const wxString &aNickname)
Function GetDescription.
virtual ~LIB_TABLE_ROW()
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
Function FindRowByURI.
bool operator!=(const LIB_TABLE_ROW &r) const
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Function Format.
void operator=(const LIB_TABLE_ROW &aRow)
bool operator==(const LIB_TABLE &r) const
bool operator==(const LIB_TABLE_ROW &r) const
void Save(const wxString &aFileName) const
Function Save.
LIB_TABLE_ROW * findRow(const wxString &aNickname) const
Function findRow returns a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBa...
void Load(const wxString &aFileName)
Function Load.
const wxString & GetDescr() const
Function GetDescr.
#define FP_LATE_ENVVAR
late=1/early=0 environment variable expansion
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Function new_clone.
const wxString & GetNickName() const
Function GetNickName.
void SetFullURI(const wxString &aFullURI)
Function SetFullURI.
INDEX::iterator INDEX_ITER
wxString options
virtual LIB_TABLE_ROW * do_clone() const =0
bool operator!=(const LIB_TABLE &r) const
void SetNickName(const wxString &aNickName)
Function SetNickName.
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
static void Parse(std::unique_ptr< LIB_TABLE_ROW > &aRow, LIB_TABLE_LEXER *in)
LIB_TABLE(LIB_TABLE *aFallBackTable=NULL)
Constructor LIB_TABLE builds a library table by pre-pending this table fragment in front of aFallBack...
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:61
Class LIB_TABLE_LEXER is an automatically generated class using the TokenList2DnsLexer.cmake technology, based on keywords provided by file: /home/kicad/workspace/kicad-doxygen/common/lib_table.keywords.
void SetOptions(const wxString &aOptions)
Function SetOptions.
wxString nickName
LIB_TABLE_ROW(const wxString &aNick, const wxString &aURI, const wxString &aOptions, const wxString &aDescr=wxEmptyString)
wxString uri_user
what user entered from UI or loaded from disk
void SetDescr(const wxString &aDescr)
Function SetDescr.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:47
INDEX nickIndex
this particular key is the nickName within each row.
bool IsEmpty(bool aIncludeFallback=true)
Function IsEmpty.
std::vector< wxString > GetLogicalLibs()
Function GetLogicalLibs.
Class LIB_TABLE.