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  {
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  enabled( true )
81  {
82  properties.reset();
83  SetOptions( aOptions );
84  SetFullURI( aURI );
85  }
86 
87  bool operator==( const LIB_TABLE_ROW& r ) const;
88 
89  bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
90 
94  const wxString& GetNickName() const { return nickName; }
95 
99  void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
100 
104  bool GetIsEnabled() const { return enabled; }
105 
109  void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
110 
114  virtual const wxString GetType() const = 0;
115 
120  virtual void SetType( const wxString& aType ) = 0;
121 
128  const wxString GetFullURI( bool aSubstituted = false ) const;
129 
133  void SetFullURI( const wxString& aFullURI );
134 
139  const wxString& GetOptions() const { return options; }
140 
144  void SetOptions( const wxString& aOptions );
145 
149  const wxString& GetDescr() const { return description; }
150 
154  void SetDescr( const wxString& aDescr ) { description = aDescr; }
155 
160  const PROPERTIES* GetProperties() const { return properties.get(); }
161 
170  void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
171 
173  {
174  return do_clone();
175  }
176 
177 protected:
178  LIB_TABLE_ROW( const LIB_TABLE_ROW& aRow ) :
179  nickName( aRow.nickName ),
180  uri_user( aRow.uri_user ),
181 #if !FP_LATE_ENVVAR
182  uri_expanded( aRow.uri_expanded ),
183 #endif
184  options( aRow.options ),
185  description( aRow.description ),
186  enabled( aRow.enabled )
187  {
188  if( aRow.properties )
189  properties = std::make_unique<PROPERTIES>( *aRow.properties.get() );
190  else
191  properties.reset();
192  }
193 
194  void operator=( const LIB_TABLE_ROW& aRow );
195 
196 private:
197  virtual LIB_TABLE_ROW* do_clone() const = 0;
198 
199  void setProperties( PROPERTIES* aProperties );
200 
201  wxString nickName;
202  wxString uri_user;
203 
204 #if !FP_LATE_ENVVAR
205  wxString uri_expanded;
206 #endif
207 
208  wxString options;
209  wxString description;
210 
211  bool enabled = true;
212 
213  std::unique_ptr< PROPERTIES > properties;
214 };
215 
216 
262 class LIB_TABLE : public PROJECT::_ELEM
263 {
264  friend class PANEL_FP_LIB_TABLE;
265  friend class LIB_TABLE_GRID;
266 
267 public:
268 
281  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
282 
293  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
294 
303  LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
304 
305  virtual ~LIB_TABLE();
306 
308  void Clear()
309  {
310  rows.clear();
311  nickIndex.clear();
312  }
313 
320  bool operator==( const LIB_TABLE& r ) const
321  {
322  if( rows.size() == r.rows.size() )
323  {
324  unsigned i;
325 
326  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
327  ;
328 
329  if( i == rows.size() )
330  return true;
331  }
332 
333  return false;
334  }
335 
336  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
337 
341  unsigned GetCount() const
342  {
343  return rows.size();
344  }
345 
351  LIB_TABLE_ROW& At( unsigned aIndex )
352  {
353  return rows[aIndex];
354  }
355 
359  const LIB_TABLE_ROW& At( unsigned aIndex ) const
360  {
361  return rows[aIndex];
362  }
363 
372  bool IsEmpty( bool aIncludeFallback = true );
373 
378  const wxString GetDescription( const wxString& aNickname );
379 
386  bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
387 
392  std::vector<wxString> GetLogicalLibs();
393 
397  wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
398 
411  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
412 
418  bool RemoveRow( LIB_TABLE_ROW* aRow )
419  {
420  for( auto iter = rows.begin(); iter != rows.end(); ++iter )
421  {
422  if( *iter == *aRow )
423  {
424  rows.erase( iter, iter + 1 );
425  return true;
426  }
427  }
428  return false;
429  }
430 
435  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
436 
445  void Load( const wxString& aFileName );
446 
452  void Save( const wxString& aFileName ) const;
453 
466  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
467 
478  static UTF8 FormatOptions( const PROPERTIES* aProperties );
479 
487  static const wxString ExpandSubstitutions( const wxString& aString );
488 
489 protected:
490 
495  LIB_TABLE_ROW* findRow( const wxString& aNickname ) const;
496 
497  LIB_TABLE_ROW* findRow( const wxString& aNickname );
498 
499  void reindex()
500  {
501  nickIndex.clear();
502 
503  for( LIB_TABLE_ROWS_ITER it = rows.begin(); it != rows.end(); ++it )
504  nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - rows.begin() ) );
505  }
506 
507  void ensureIndex()
508  {
509  // The dialog lib table editor may not maintain the nickIndex.
510  // Lazy indexing may be required. To handle lazy indexing, we must enforce
511  // that "nickIndex" is either empty or accurate, but never inaccurate.
512  if( !nickIndex.size() )
513  reindex();
514  }
515 
517 
519  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
520  typedef INDEX::iterator INDEX_ITER;
521  typedef INDEX::const_iterator INDEX_CITER;
522  typedef INDEX::value_type INDEX_VALUE;
523 
526 
528 };
529 
530 #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()
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 ...
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.
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.
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:69
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.