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 Wayne Stambaugh <stambaughw@gmail.com>
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/ptr_container/ptr_vector.hpp>
32 #include <boost/noncopyable.hpp>
33 
34 #include <project.h>
35 #include <properties.h>
36 #include <richio.h>
37 
38 
39 #define FP_LATE_ENVVAR 1
40 
41 class OUTPUTFORMATTER;
42 class LIB_TABLE_LEXER;
43 class LIB_ID;
44 class LIB_TABLE_ROW;
45 class LIB_TABLE_GRID;
46 class IO_ERROR;
47 
48 
49 typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
50 typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
51 typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
52 
53 
59 LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow );
60 
61 
66 class LIB_TABLE_ROW : boost::noncopyable
67 {
68 public:
70  {
71  }
72 
73  virtual ~LIB_TABLE_ROW()
74  {
75  }
76 
77  LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
78  const wxString& aDescr = wxEmptyString ) :
79  nickName( aNick ),
80  description( aDescr ),
81  enabled( true )
82  {
83  properties.reset();
84  SetOptions( aOptions );
85  SetFullURI( aURI );
86  }
87 
88  bool operator==( const LIB_TABLE_ROW& r ) const;
89 
90  bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
91 
95  const wxString& GetNickName() const { return nickName; }
96 
100  void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
101 
105  bool GetIsEnabled() const { return enabled; }
106 
110  void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
111 
115  virtual const wxString GetType() const = 0;
116 
121  virtual void SetType( const wxString& aType ) = 0;
122 
129  const wxString GetFullURI( bool aSubstituted = false ) const;
130 
134  void SetFullURI( const wxString& aFullURI );
135 
140  const wxString& GetOptions() const { return options; }
141 
145  void SetOptions( const wxString& aOptions );
146 
150  const wxString& GetDescr() const { return description; }
151 
155  void SetDescr( const wxString& aDescr ) { description = aDescr; }
156 
161  const PROPERTIES* GetProperties() const { return properties.get(); }
162 
171  void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
172 
173  static void Parse( std::unique_ptr< LIB_TABLE_ROW >& aRow, LIB_TABLE_LEXER* in );
174 
176  {
177  return do_clone();
178  }
179 
180 protected:
181  LIB_TABLE_ROW( const LIB_TABLE_ROW& aRow ) :
182  nickName( aRow.nickName ),
183  uri_user( aRow.uri_user ),
184 #if !FP_LATE_ENVVAR
185  uri_expanded( aRow.uri_expanded ),
186 #endif
187  options( aRow.options ),
188  description( aRow.description ),
189  enabled( aRow.enabled )
190  {
191  if( aRow.properties )
192  properties.reset( new PROPERTIES( *aRow.properties.get() ) );
193  else
194  properties.reset();
195  }
196 
197  void operator=( const LIB_TABLE_ROW& aRow );
198 
199 private:
200  virtual LIB_TABLE_ROW* do_clone() const = 0;
201 
202  void setProperties( PROPERTIES* aProperties );
203 
204  wxString nickName;
205  wxString uri_user;
206 
207 #if !FP_LATE_ENVVAR
208  wxString uri_expanded;
209 #endif
210 
211  wxString options;
212  wxString description;
213 
214  bool enabled = true;
215 
216  std::unique_ptr< PROPERTIES > properties;
217 };
218 
219 
265 class LIB_TABLE : public PROJECT::_ELEM
266 {
267  friend class PANEL_FP_LIB_TABLE;
268  friend class LIB_TABLE_GRID;
269 
270 public:
271 
284  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
285 
296  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
297 
306  LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
307 
308  virtual ~LIB_TABLE();
309 
311  void Clear()
312  {
313  rows.clear();
314  nickIndex.clear();
315  }
316 
323  bool operator==( const LIB_TABLE& r ) const
324  {
325  if( rows.size() == r.rows.size() )
326  {
327  unsigned i;
328 
329  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
330  ;
331 
332  if( i == rows.size() )
333  return true;
334  }
335 
336  return false;
337  }
338 
339  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
340 
344  unsigned GetCount() const
345  {
346  return rows.size();
347  }
348 
354  LIB_TABLE_ROW& At( unsigned aIndex )
355  {
356  return rows[aIndex];
357  }
358 
362  const LIB_TABLE_ROW& At( unsigned aIndex ) const
363  {
364  return rows[aIndex];
365  }
366 
375  bool IsEmpty( bool aIncludeFallback = true );
376 
381  const wxString GetDescription( const wxString& aNickname );
382 
389  bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
390 
395  std::vector<wxString> GetLogicalLibs();
396 
400  wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
401 
414  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
415 
421  bool RemoveRow( LIB_TABLE_ROW* aRow )
422  {
423  for( auto iter = rows.begin(); iter != rows.end(); ++iter )
424  {
425  if( *iter == *aRow )
426  {
427  rows.erase( iter, iter + 1 );
428  return true;
429  }
430  }
431  return false;
432  }
433 
438  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
439 
448  void Load( const wxString& aFileName );
449 
455  void Save( const wxString& aFileName ) const;
456 
469  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
470 
481  static UTF8 FormatOptions( const PROPERTIES* aProperties );
482 
490  static const wxString ExpandSubstitutions( const wxString& aString );
491 
492 protected:
493 
498  LIB_TABLE_ROW* findRow( const wxString& aNickname ) const;
499 
500  LIB_TABLE_ROW* findRow( const wxString& aNickname );
501 
502  void reindex()
503  {
504  nickIndex.clear();
505 
506  for( LIB_TABLE_ROWS_ITER it = rows.begin(); it != rows.end(); ++it )
507  nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - rows.begin() ) );
508  }
509 
510  void ensureIndex()
511  {
512  // The dialog lib table editor may not maintain the nickIndex.
513  // Lazy indexing may be required. To handle lazy indexing, we must enforce
514  // that "nickIndex" is either empty or accurate, but never inaccurate.
515  if( !nickIndex.size() )
516  reindex();
517  }
518 
520 
522  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
523  typedef INDEX::iterator INDEX_ITER;
524  typedef INDEX::const_iterator INDEX_CITER;
525  typedef INDEX::value_type INDEX_VALUE;
526 
529 
531 };
532 
533 #endif // _LIB_TABLE_BASE_H_
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
void ensureIndex()
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
LIB_TABLE_ROW(const LIB_TABLE_ROW &aRow)
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
std::vector< PROPERTY > PROPERTIES
Definition: specctra.h:184
void Clear()
Delete all rows.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
void reindex()
static const wxString ExpandSubstitutions(const wxString &aString)
Replaces any environment variable references with their values and is here to fully embellish the TAB...
unsigned GetCount() const
Get the number of rows contained in the table.
std::map< wxString, int > INDEX
this is a non-owning index into the LIB_TABLE_ROWS table
bool enabled
Whether the LIB_TABLE_ROW is enabled.
INDEX::const_iterator INDEX_CITER
void setProperties(PROPERTIES *aProperties)
LIB_TABLE(LIB_TABLE *aFallBackTable=nullptr)
Build a library table by pre-pending this table fragment in front of aFallBackTable.
virtual void SetType(const wxString &aType)=0
Change the type of library represented by this row that must be implemented in the derived object to ...
Class OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a conve...
Definition: richio.h:327
bool operator==(const LIB_TABLE &r) const
Compares this table against another.
LIB_TABLE_ROWS rows
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:51
virtual const wxString GetType() const =0
Return the type of library represented by this row.
Class PROPERTIES is a name/value tuple with unique names and optional values.
Definition: properties.h:34
std::unique_ptr< PROPERTIES > properties
bool operator==(const LIB_TABLE_ROW &r) const
LIB_TABLE * fallBack
static UTF8 FormatOptions(const PROPERTIES *aProperties)
Returns a list of options from the aProperties parameter.
INDEX::value_type INDEX_VALUE
const wxString & GetDescr() const
Return the description of the library referenced by this row.
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
virtual ~LIB_TABLE()
wxString description
const wxString GetFullURI(bool aSubstituted=false) const
Return the full location specifying URI for the LIB, either in original UI form or in environment var...
void SetEnabled(bool aEnabled=true)
Change the enabled status of this library.
const wxString & GetNickName() const
static PROPERTIES * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a PROPERTIES object which is returned.
LIB_TABLE_ROW * clone() const
bool GetIsEnabled() const
virtual void Parse(LIB_TABLE_LEXER *aLexer)=0
Parse the #LIB_TABLE_LEXER s-expression library table format into the appropriate LIB_TABLE_ROW objec...
const wxString GetDescription(const wxString &aNickname)
virtual ~LIB_TABLE_ROW()
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
const LIB_TABLE_ROW & At(unsigned aIndex) const
Get the 'n'th LIB_TABLE_ROW object.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indention level of aIndentLevel.
void operator=(const LIB_TABLE_ROW &aRow)
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
#define FP_LATE_ENVVAR
late=1/early=0 environment variable expansion
bool RemoveRow(LIB_TABLE_ROW *aRow)
Removes a row from the table.
bool operator!=(const LIB_TABLE &r) const
void Format(OUTPUTFORMATTER *out, int nestLevel) const
Serialize this object as utf8 text to an OUTPUTFORMATTER, and tries to make it look good using multip...
bool operator!=(const LIB_TABLE_ROW &r) const
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER
LIB_TABLE_ROW & At(unsigned aIndex)
Get the 'n'th LIB_TABLE_ROW object.
INDEX::iterator INDEX_ITER
wxString options
virtual LIB_TABLE_ROW * do_clone() const =0
LIB_TABLE_ROW * findRow(const wxString &aNickname) const
Return a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBack table fragment,...
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
size_t i
Definition: json11.cpp:649
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
static void Parse(std::unique_ptr< LIB_TABLE_ROW > &aRow, LIB_TABLE_LEXER *in)
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:68
void SetOptions(const wxString &aOptions)
Change the library options strings.
wxString nickName
const PROPERTIES * GetProperties() const
Return the constant PROPERTIES for this library (LIB_TABLE_ROW).
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
Dialog to show and edit symbol library tables.
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)
Change the description of the library referenced by this row.
Struct IO_ERROR is a class used to hold an error message and may be used when throwing exceptions con...
Definition: ki_exception.h:76
INDEX nickIndex
this particular key is the nickName within each row.
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.