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/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 
60 LIB_TABLE_ROW* new_clone( const LIB_TABLE_ROW& aRow );
61 
62 
67 class LIB_TABLE_ROW : boost::noncopyable
68 {
69 public:
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  {
84  properties.reset();
85  SetOptions( aOptions );
86  SetFullURI( aURI );
87  }
88 
89  bool operator==( const LIB_TABLE_ROW& r ) const;
90 
91  bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
92 
96  const wxString& GetNickName() const { return nickName; }
97 
101  void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
102 
106  bool GetIsEnabled() const { return enabled; }
107 
111  void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
112 
116  virtual const wxString GetType() const = 0;
117 
122  virtual void SetType( const wxString& aType ) = 0;
123 
130  const wxString GetFullURI( bool aSubstituted = false ) const;
131 
135  void SetFullURI( const wxString& aFullURI );
136 
141  const wxString& GetOptions() const { return options; }
142 
146  void SetOptions( const wxString& aOptions );
147 
151  const wxString& GetDescr() const { return description; }
152 
156  void SetDescr( const wxString& aDescr ) { description = aDescr; }
157 
162  const PROPERTIES* GetProperties() const { return properties.get(); }
163 
172  void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
173 
174  static void Parse( std::unique_ptr< LIB_TABLE_ROW >& aRow, LIB_TABLE_LEXER* in );
175 
177  {
178  return do_clone();
179  }
180 
181 protected:
182  LIB_TABLE_ROW( const LIB_TABLE_ROW& aRow ) :
183  nickName( aRow.nickName ),
184  uri_user( aRow.uri_user ),
186  uri_expanded( aRow.uri_expanded ),
187 #endif
188  options( aRow.options ),
189  description( aRow.description ),
190  enabled( aRow.enabled )
191  {
192  if( aRow.properties )
193  properties.reset( new PROPERTIES( *aRow.properties.get() ) );
194  else
195  properties.reset();
196  }
197 
198  void operator=( const LIB_TABLE_ROW& aRow );
199 
200 private:
201  virtual LIB_TABLE_ROW* do_clone() const = 0;
202 
203  void setProperties( PROPERTIES* aProperties );
204 
205  wxString nickName;
206  wxString uri_user;
207 
208 #if !FP_LATE_ENVVAR
209  wxString uri_expanded;
210 #endif
211 
212  wxString options;
213  wxString description;
214 
215  bool enabled = true;
216 
217  std::unique_ptr< PROPERTIES > properties;
218 };
219 
220 
266 class LIB_TABLE : public PROJECT::_ELEM
267 {
268  friend class DIALOG_FP_LIB_TABLE;
269  friend class LIB_TABLE_GRID;
270 
271 public:
272 
285  virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
286 
297  virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
298 
307  LIB_TABLE( LIB_TABLE* aFallBackTable = NULL );
308 
309  virtual ~LIB_TABLE();
310 
312  void Clear()
313  {
314  rows.clear();
315  nickIndex.clear();
316  }
317 
318  bool operator==( const LIB_TABLE& r ) const
319  {
320  if( rows.size() == r.rows.size() )
321  {
322  unsigned i;
323 
324  for( i = 0; i < rows.size() && rows[i] == r.rows[i]; ++i )
325  ;
326 
327  if( i == rows.size() )
328  return true;
329  }
330 
331  return false;
332  }
333 
334  bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
335 
336  int GetCount() { return rows.size(); }
337 
338  LIB_TABLE_ROW* At( int aIndex ) { return &rows[aIndex]; }
339 
348  bool IsEmpty( bool aIncludeFallback = true );
349 
354  const wxString GetDescription( const wxString& aNickname );
355 
362  bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
363 
368  std::vector<wxString> GetLogicalLibs();
369 
373  wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
374 
387  bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
388 
393  const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
394 
403  void Load( const wxString& aFileName );
404 
410  void Save( const wxString& aFileName ) const;
411 
424  static PROPERTIES* ParseOptions( const std::string& aOptionsList );
425 
436  static UTF8 FormatOptions( const PROPERTIES* aProperties );
437 
445  static const wxString ExpandSubstitutions( const wxString& aString );
446 
447 protected:
448 
453  LIB_TABLE_ROW* findRow( const wxString& aNickname ) const;
454 
455  LIB_TABLE_ROW* findRow( const wxString& aNickname );
456 
457  void reindex()
458  {
459  nickIndex.clear();
460 
461  for( LIB_TABLE_ROWS_ITER it = rows.begin(); it != rows.end(); ++it )
462  nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - rows.begin() ) );
463  }
464 
465  void ensureIndex()
466  {
467  // The dialog lib table editor may not maintain the nickIndex.
468  // Lazy indexing may be required. To handle lazy indexing, we must enforce
469  // that "nickIndex" is either empty or accurate, but never inaccurate.
470  if( !nickIndex.size() )
471  reindex();
472  }
473 
475 
477  typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
478  typedef INDEX::iterator INDEX_ITER;
479  typedef INDEX::const_iterator INDEX_CITER;
480  typedef INDEX::value_type INDEX_VALUE;
481 
483  INDEX nickIndex;
484 
486 };
487 
488 #endif // _LIB_TABLE_BASE_H_
int GetCount()
Class UTF8 is an 8 bit string that is assuredly encoded in UTF8, and supplies special conversion supp...
Definition: utf8.h:73
void ensureIndex()
LIB_TABLE_ROW(const LIB_TABLE_ROW &aRow)
if(dlg.ShowModal()==wxID_OK)
Definition: selcolor.cpp:75
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 Format(OUTPUTFORMATTER *out, int nestLevel) const
Serialize this object as utf8 text to an OUTPUTFORMATTER, and tries to make it look good using multip...
void Clear()
Delete all rows.
const PROPERTIES * GetProperties() const
Return the constant PROPERTIES for this library (LIB_TABLE_ROW).
void reindex()
LIB_TABLE_ROW * clone() const
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)
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 DIALOG_FP_LIB_TABLE shows and edits the PCB library tables.
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
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
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
LIB_TABLE * fallBack
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...
INDEX::value_type INDEX_VALUE
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
wxString description
static struct PcbQueue * Save
Definition: queue.cpp:56
void SetEnabled(bool aEnabled=true)
Change the enabled status of this library.
virtual ~LIB_TABLE_ROW()
bool operator!=(const LIB_TABLE_ROW &r) const
void operator=(const LIB_TABLE_ROW &aRow)
bool operator==(const LIB_TABLE &r) const
bool operator==(const LIB_TABLE_ROW &r) const
const wxString & GetDescr() const
Return the description of the library referenced by this row.
#define FP_LATE_ENVVAR
late=1/early=0 environment variable expansion
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
const wxString & GetNickName() const
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
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)
Change the logical name of this library, useful for an editor.
size_t i
Definition: json11.cpp:597
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
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:66
KICAD_PLUGIN_EXPORT SCENEGRAPH * Load(char const *aFileName)
reads a model file and creates a generic display structure
void SetOptions(const wxString &aOptions)
Change the library options strings.
wxString nickName
bool GetIsEnabled() const
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)
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.
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.