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 LIB_TABLE_GRID;
47 class IO_ERROR;
48 
49 
50 typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
51 typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
52 typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
53 
54 
62 LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow );
63 
64 
71 class LIB_TABLE_ROW : boost::noncopyable
72 {
73 public:
75  {
76  }
77 
78  virtual ~LIB_TABLE_ROW()
79  {
80  }
81 
82  LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
83  const wxString& aDescr = wxEmptyString ) :
84  nickName( aNick ),
85  description( aDescr )
86  {
87  properties.reset();
88  SetOptions( aOptions );
89  SetFullURI( aURI );
90  }
91 
92  bool operator==( const LIB_TABLE_ROW& r ) const;
93 
94  bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
95 
101  const wxString& GetNickName() const { return nickName; }
102 
108  void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
109 
115  virtual const wxString GetType() const = 0;
116 
123  virtual void SetType( const wxString& aType ) = 0;
124 
133  const wxString GetFullURI( bool aSubstituted = false ) const;
134 
140  void SetFullURI( const wxString& aFullURI );
141 
148  const wxString& GetOptions() const { return options; }
149 
153  void SetOptions( const wxString& aOptions );
154 
160  const wxString& GetDescr() const { return description; }
161 
167  void SetDescr( const wxString& aDescr ) { description = aDescr; }
168 
175  const PROPERTIES* GetProperties() const { return properties.get(); }
176 
187  void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
188 
189  static void Parse( std::unique_ptr< LIB_TABLE_ROW >& aRow, LIB_TABLE_LEXER* in );
190 
192  {
193  return do_clone();
194  }
195 
196 protected:
197  LIB_TABLE_ROW( const LIB_TABLE_ROW& aRow ) :
198  nickName( aRow.nickName ),
199  uri_user( aRow.uri_user ),
201  uri_expanded( aRow.uri_expanded ),
202 #endif
203  options( aRow.options ),
204  description( aRow.description )
205  {
206  if( aRow.properties )
207  properties.reset( new PROPERTIES( *aRow.properties.get() ) );
208  else
209  properties.reset();
210  }
211 
212  void operator=( const LIB_TABLE_ROW& aRow );
213 
214 private:
215  virtual LIB_TABLE_ROW* do_clone() const = 0;
216 
217  void setProperties( PROPERTIES* aProperties );
218 
219  wxString nickName;
220  wxString uri_user;
221 
222 #if !FP_LATE_ENVVAR
223  wxString uri_expanded;
224 #endif
225 
226  wxString options;
227  wxString description;
228 
229  std::unique_ptr< PROPERTIES > properties;
230 };
231 
232 
279 class LIB_TABLE : public PROJECT::_ELEM
280 {
281  friend class DIALOG_FP_LIB_TABLE;
282  friend class LIB_TABLE_GRID;
283 
284 public:
285 
300  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
301 
314  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
315 
325  LIB_TABLE( LIB_TABLE* aFallBackTable = NULL );
326 
327  virtual ~LIB_TABLE();
328 
330  void Clear()
331  {
332  rows.clear();
333  nickIndex.clear();
334  }
335 
336  bool operator==( const LIB_TABLE& r ) const
337  {
338  if( rows.size() == r.rows.size() )
339  {
340  unsigned i;
341 
342  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
343  ;
344 
345  if( i == rows.size() )
346  return true;
347  }
348 
349  return false;
350  }
351 
352  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
353 
354  int GetCount() { return rows.size(); }
355 
356  LIB_TABLE_ROW* At( int aIndex ) { return &rows[aIndex]; }
357 
366  bool IsEmpty( bool aIncludeFallback = true );
367 
374  const wxString GetDescription( const wxString& aNickname );
375 
382  std::vector<wxString> GetLogicalLibs();
383 
398  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
399 
406  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
407 
418  void Load( const wxString& aFileName ) throw( IO_ERROR );
419 
427  void Save( const wxString& aFileName ) const
428  throw( IO_ERROR, boost::interprocess::lock_exception );
429 
440  size_t GetEnvVars( wxArrayString& aEnvVars ) const;
441 
455  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
456 
468  static UTF8 FormatOptions( const PROPERTIES* aProperties );
469 
478  static const wxString ExpandSubstitutions( const wxString& aString );
479 
480 protected:
481 
487  LIB_TABLE_ROW* findRow( const wxString& aNickname ) const;
488 
489  void reindex()
490  {
491  nickIndex.clear();
492 
493  for( LIB_TABLE_ROWS_ITER it = rows.begin(); it != rows.end(); ++it )
494  nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - rows.begin() ) );
495  }
496 
497  void ensureIndex()
498  {
499  // The dialog lib table editor may not maintain the nickIndex.
500  // Lazy indexing may be required. To handle lazy indexing, we must enforce
501  // that "nickIndex" is either empty or accurate, but never inaccurate.
502  if( !nickIndex.size() )
503  reindex();
504  }
505 
507 
509  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
510  typedef INDEX::iterator INDEX_ITER;
511  typedef INDEX::const_iterator INDEX_CITER;
512  typedef INDEX::value_type INDEX_VALUE;
513 
515  INDEX nickIndex;
516 
518 };
519 
520 #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.
size_t GetEnvVars(wxArrayString &aEnvVars) const
Search the paths all of the LIB_TABLE_ROWS of the LIB_TABLE and add all of the environment variable s...
void Clear()
Delete all rows.
const PROPERTIES * GetProperties() const
Function GetProperties.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Function InsertRow.
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
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
virtual ~LIB_TABLE()
wxString description
static PROPERTIES * ParseOptions(const std::string &aOptionsList)
Function ParseOptions.
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.
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER
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:62
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
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
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.