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 #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  friend class PANEL_FP_LIB_TABLE;
280  friend class LIB_TABLE_GRID;
281 
282 public:
283 
296  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
297 
308  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
309 
318  LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
319 
320  virtual ~LIB_TABLE();
321 
323  void Clear()
324  {
325  rows.clear();
326  nickIndex.clear();
327  }
328 
335  bool operator==( const LIB_TABLE& r ) const
336  {
337  if( rows.size() == r.rows.size() )
338  {
339  unsigned i;
340 
341  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
342  ;
343 
344  if( i == rows.size() )
345  return true;
346  }
347 
348  return false;
349  }
350 
351  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
352 
356  unsigned GetCount() const
357  {
358  return rows.size();
359  }
360 
366  LIB_TABLE_ROW& At( unsigned aIndex )
367  {
368  return rows[aIndex];
369  }
370 
374  const LIB_TABLE_ROW& At( unsigned aIndex ) const
375  {
376  return rows[aIndex];
377  }
378 
387  bool IsEmpty( bool aIncludeFallback = true );
388 
393  const wxString GetDescription( const wxString& aNickname );
394 
401  bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
402 
407  std::vector<wxString> GetLogicalLibs();
408 
412  wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
413 
426  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
427 
433  bool RemoveRow( LIB_TABLE_ROW* aRow )
434  {
435  for( auto iter = rows.begin(); iter != rows.end(); ++iter )
436  {
437  if( *iter == *aRow )
438  {
439  rows.erase( iter, iter + 1 );
440  return true;
441  }
442  }
443  return false;
444  }
445 
450  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
451 
460  void Load( const wxString& aFileName );
461 
467  void Save( const wxString& aFileName ) const;
468 
481  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
482 
493  static UTF8 FormatOptions( const PROPERTIES* aProperties );
494 
495 protected:
496 
501  LIB_TABLE_ROW* findRow( const wxString& aNickname ) const;
502 
503  LIB_TABLE_ROW* findRow( const wxString& aNickname );
504 
505  void reindex()
506  {
507  nickIndex.clear();
508 
509  for( LIB_TABLE_ROWS_ITER it = rows.begin(); it != rows.end(); ++it )
510  nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - rows.begin() ) );
511  }
512 
513  void ensureIndex()
514  {
515  // The dialog lib table editor may not maintain the nickIndex.
516  // Lazy indexing may be required. To handle lazy indexing, we must enforce
517  // that "nickIndex" is either empty or accurate, but never inaccurate.
518  if( !nickIndex.size() )
519  reindex();
520  }
521 
523 
525  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
526  typedef INDEX::iterator INDEX_ITER;
527  typedef INDEX::const_iterator INDEX_CITER;
528  typedef INDEX::value_type INDEX_VALUE;
529 
532 
534 };
535 
536 #endif // _LIB_TABLE_BASE_H_
UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to...
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.
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 ...
OUTPUTFORMATTER is an important interface (abstract class) used to output 8 bit text in a convenient ...
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.
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.
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)
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.
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:74
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 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.