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-2021 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 #include <boost/noncopyable.hpp>
31 #include <boost/ptr_container/ptr_vector.hpp>
32 #include <memory>
33 #include <project.h>
34 #include <properties.h>
35 #include <richio.h>
36 
37 
38 #define FP_LATE_ENVVAR 1
39 
40 class OUTPUTFORMATTER;
41 class LIB_TABLE_LEXER;
42 class LIB_ID;
43 class LIB_TABLE_ROW;
44 class LIB_TABLE_GRID;
45 class IO_ERROR;
46 
47 
48 typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
49 typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
50 typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
51 
52 
58 LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow );
59 
60 
65 class LIB_TABLE_ROW : boost::noncopyable
66 {
67 public:
69  enabled( true ),
70  m_loaded( false )
71  {
72  }
73 
74  virtual ~LIB_TABLE_ROW()
75  {
76  }
77 
78  LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
79  const wxString& aDescr = wxEmptyString ) :
80  nickName( aNick ),
81  description( aDescr ),
82  enabled( true ),
83  m_loaded( false )
84  {
85  properties.reset();
86  SetOptions( aOptions );
87  SetFullURI( aURI );
88  }
89 
90  bool operator==( const LIB_TABLE_ROW& r ) const;
91 
92  bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
93 
97  const wxString& GetNickName() const { return nickName; }
98 
102  void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
103 
107  bool GetIsLoaded() const { return m_loaded; }
108 
112  void SetLoaded( bool aLoaded ) { m_loaded = aLoaded; };
113 
117  bool GetIsEnabled() const { return enabled; }
118 
122  void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
123 
127  virtual const wxString GetType() const = 0;
128 
133  virtual void SetType( const wxString& aType ) = 0;
134 
141  const wxString GetFullURI( bool aSubstituted = false ) const;
142 
146  void SetFullURI( const wxString& aFullURI );
147 
152  const wxString& GetOptions() const { return options; }
153 
157  void SetOptions( const wxString& aOptions );
158 
162  const wxString& GetDescr() const { return description; }
163 
167  void SetDescr( const wxString& aDescr ) { description = aDescr; }
168 
173  const PROPERTIES* GetProperties() const { return properties.get(); }
174 
183  void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
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 ),
194 #if !FP_LATE_ENVVAR
195  uri_expanded( aRow.uri_expanded ),
196 #endif
197  options( aRow.options ),
198  description( aRow.description ),
199  enabled( aRow.enabled ),
200  m_loaded( aRow.m_loaded )
201  {
202  if( aRow.properties )
203  properties = std::make_unique<PROPERTIES>( *aRow.properties.get() );
204  else
205  properties.reset();
206  }
207 
208  void operator=( const LIB_TABLE_ROW& aRow );
209 
210 private:
211  virtual LIB_TABLE_ROW* do_clone() const = 0;
212 
213  void setProperties( PROPERTIES* aProperties );
214 
215  wxString nickName;
216  wxString uri_user;
217 
218 #if !FP_LATE_ENVVAR
219  wxString uri_expanded;
220 #endif
221 
222  wxString options;
223  wxString description;
224 
225  bool enabled = true;
226  bool m_loaded = false;
227 
228  std::unique_ptr< PROPERTIES > properties;
229 };
230 
231 
277 class LIB_TABLE : public PROJECT::_ELEM
278 {
279 public:
292  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
293 
304  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
305 
314  LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
315 
316  virtual ~LIB_TABLE();
317 
319  void Clear()
320  {
321  rows.clear();
322  nickIndex.clear();
323  }
324 
331  bool operator==( const LIB_TABLE& r ) const
332  {
333  if( rows.size() == r.rows.size() )
334  {
335  unsigned i;
336 
337  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
338  ;
339 
340  if( i == rows.size() )
341  return true;
342  }
343 
344  return false;
345  }
346 
347  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
348 
352  unsigned GetCount() const
353  {
354  return rows.size();
355  }
356 
362  LIB_TABLE_ROW& At( unsigned aIndex )
363  {
364  return rows[aIndex];
365  }
366 
370  const LIB_TABLE_ROW& At( unsigned aIndex ) const
371  {
372  return rows[aIndex];
373  }
374 
383  bool IsEmpty( bool aIncludeFallback = true );
384 
389  const wxString GetDescription( const wxString& aNickname );
390 
397  bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
398 
403  std::vector<wxString> GetLogicalLibs();
404 
408  wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
409 
422  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
423 
429  bool RemoveRow( LIB_TABLE_ROW* aRow )
430  {
431  for( auto iter = rows.begin(); iter != rows.end(); ++iter )
432  {
433  if( *iter == *aRow )
434  {
435  rows.erase( iter, iter + 1 );
436  return true;
437  }
438  }
439  return false;
440  }
441 
446  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
447 
456  void Load( const wxString& aFileName );
457 
463  void Save( const wxString& aFileName ) const;
464 
477  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
478 
489  static UTF8 FormatOptions( const PROPERTIES* aProperties );
490 
491 protected:
500  LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const;
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 
519 private:
520  friend class PANEL_FP_LIB_TABLE;
521  friend class LIB_TABLE_GRID;
522 
523 protected:
525 
527  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
528  typedef INDEX::iterator INDEX_ITER;
529  typedef INDEX::const_iterator INDEX_CITER;
530  typedef INDEX::value_type INDEX_VALUE;
531 
534 
536 };
537 
538 #endif // _LIB_TABLE_BASE_H_
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:70
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.
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()
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 ...
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:306
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.
A name/value tuple with unique names and optional values.
Definition: properties.h:33
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.
bool GetIsLoaded() const
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 SetLoaded(bool aLoaded)
Mark the row as being a loaded library.
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)
LIB_TABLE_ROW * findRow(const wxString &aNickname, bool aCheckIfEnabled=false) const
Return a LIB_TABLE_ROW if aNickname is found in this table or in any chained fallBack table fragment,...
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
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:73
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.
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:75
INDEX nickIndex
this particular key is the nickName within each row.
bool m_loaded
Whether the LIB_TABLE_ROW is loaded.
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.