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 <[email protected]>
5 * Copyright (C) 2012 Wayne Stambaugh <[email protected]>
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 <mutex>
34#include <shared_mutex>
35#include <project.h>
36#include <string_utf8_map.h>
37#include <richio.h>
38
39class OUTPUTFORMATTER;
40class LIB_TABLE_LEXER;
41class LIB_ID;
42class LIB_TABLE_ROW;
43class LIB_TABLE_GRID;
44class LIB_TABLE;
45class IO_ERROR;
46
47
48typedef boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS;
49typedef LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER;
50typedef LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER;
51
52
59
60
65class LIB_TABLE_ROW : boost::noncopyable
66{
67public:
69 enabled( true ),
70 visible( true ),
71 m_loaded( false ),
72 m_parent( nullptr )
73 {
74 }
75
77 {
78 }
79
80 LIB_TABLE_ROW( const wxString& aNick, const wxString& aURI, const wxString& aOptions,
81 const wxString& aDescr = wxEmptyString, LIB_TABLE* aParent = nullptr ) :
82 nickName( aNick ),
83 description( aDescr ),
84 enabled( true ),
85 visible( true ),
86 m_loaded( false ),
87 m_parent( aParent )
88 {
89 properties.reset();
90 SetOptions( aOptions );
91 SetFullURI( aURI );
92 }
93
94 bool operator==( const LIB_TABLE_ROW& r ) const;
95
96 bool operator!=( const LIB_TABLE_ROW& r ) const { return !( *this == r ); }
97
101 const wxString& GetNickName() const { return nickName; }
102
106 void SetNickName( const wxString& aNickName ) { nickName = aNickName; }
107
111 bool GetIsLoaded() const { return m_loaded; }
112
116 void SetLoaded( bool aLoaded ) { m_loaded = aLoaded; };
117
121 bool GetIsEnabled() const { return enabled; }
122
126 void SetEnabled( bool aEnabled = true ) { enabled = aEnabled; }
127
128 bool GetIsVisible() const { return visible; }
129
130 void SetVisible( bool aVisible = true ) { visible = aVisible; }
131
135 virtual const wxString GetType() const = 0;
136
141 virtual void SetType( const wxString& aType ) = 0;
142
149 const wxString GetFullURI( bool aSubstituted = false ) const;
150
154 void SetFullURI( const wxString& aFullURI );
155
160 const wxString& GetOptions() const { return options; }
161
165 void SetOptions( const wxString& aOptions );
166
170 const wxString& GetDescr() const { return description; }
171
175 void SetDescr( const wxString& aDescr ) { description = aDescr; }
176
177 LIB_TABLE* GetParent() const { return m_parent; }
178
179 void SetParent( LIB_TABLE* aParent ) { m_parent = aParent; }
180
181 std::mutex& GetMutex() { return m_loadMutex; }
182
187 const STRING_UTF8_MAP* GetProperties() const { return properties.get(); }
188
197 void Format( OUTPUTFORMATTER* out, int nestLevel ) const;
198
200 {
201 return do_clone();
202 }
203
204protected:
206 nickName( aRow.nickName ),
207 uri_user( aRow.uri_user ),
208 options( aRow.options ),
209 description( aRow.description ),
210 enabled( aRow.enabled ),
211 visible( aRow.visible ),
212 m_loaded( aRow.m_loaded ),
213 m_parent( aRow.m_parent )
214 {
215 if( aRow.properties )
216 properties = std::make_unique<STRING_UTF8_MAP>( *aRow.properties.get() );
217 else
218 properties.reset();
219 }
220
221 void operator=( const LIB_TABLE_ROW& aRow );
222
223private:
224 virtual LIB_TABLE_ROW* do_clone() const = 0;
225
226 void setProperties( STRING_UTF8_MAP* aProperties );
227
228 wxString nickName;
229 wxString uri_user;
230 wxString options;
231 wxString description;
232
233 bool enabled = true;
234 bool visible = true;
235 bool m_loaded = false;
237
238 std::unique_ptr<STRING_UTF8_MAP> properties;
239
240 std::mutex m_loadMutex;
241};
242
243
290{
291public:
304 virtual void Parse( LIB_TABLE_LEXER* aLexer ) = 0;
305
316 virtual void Format( OUTPUTFORMATTER* aOutput, int aIndentLevel ) const = 0;
317
326 LIB_TABLE( LIB_TABLE* aFallBackTable = nullptr );
327
328 virtual ~LIB_TABLE();
329
331 void Clear()
332 {
333 std::lock_guard<std::shared_mutex> lock( m_nickIndexMutex );
334
335 m_rows.clear();
336 m_nickIndex.clear();
337 }
338
345 bool operator==( const LIB_TABLE& r ) const
346 {
347 if( m_rows.size() == r.m_rows.size() )
348 {
349 unsigned i;
350
351 for( i = 0; i < m_rows.size() && m_rows[i] == r.m_rows[i]; ++i )
352 ;
353
354 if( i == m_rows.size() )
355 return true;
356 }
357
358 return false;
359 }
360
361 bool operator!=( const LIB_TABLE& r ) const { return !( *this == r ); }
362
366 unsigned GetCount() const
367 {
368 return m_rows.size();
369 }
370
376 LIB_TABLE_ROW& At( unsigned aIndex )
377 {
378 return m_rows[aIndex];
379 }
380
384 const LIB_TABLE_ROW& At( unsigned aIndex ) const
385 {
386 return m_rows[aIndex];
387 }
388
397 bool IsEmpty( bool aIncludeFallback = true );
398
403 const wxString GetDescription( const wxString& aNickname );
404
411 bool HasLibrary( const wxString& aNickname, bool aCheckEnabled = false ) const;
412
419 bool HasLibraryWithPath( const wxString& aPath ) const;
420
425 std::vector<wxString> GetLogicalLibs();
426
430 wxString GetFullURI( const wxString& aLibNickname, bool aExpandEnvVars = true ) const;
431
444 bool InsertRow( LIB_TABLE_ROW* aRow, bool doReplace = false );
445
451 bool RemoveRow( const LIB_TABLE_ROW* aRow )
452 {
453 for( auto iter = m_rows.begin(); iter != m_rows.end(); ++iter )
454 {
455 if( *iter == *aRow )
456 {
457 m_rows.erase( iter, iter + 1 );
458 reindex( true );
459 return true;
460 }
461 }
462 return false;
463 }
464
469 const LIB_TABLE_ROW* FindRowByURI( const wxString& aURI );
470
479 void Load( const wxString& aFileName );
480
486 void Save( const wxString& aFileName ) const;
487
500 static STRING_UTF8_MAP* ParseOptions( const std::string& aOptionsList );
501
512 static UTF8 FormatOptions( const STRING_UTF8_MAP* aProperties );
513
519 int GetVersion() const
520 {
521 return m_version;
522 }
523
524protected:
533 LIB_TABLE_ROW* findRow( const wxString& aNickname, bool aCheckIfEnabled = false ) const;
534
541 bool migrate();
542
549 void reindex( bool aForce )
550 {
551 std::lock_guard<std::shared_mutex> lock( m_nickIndexMutex );
552
553 if( !aForce )
554 {
555 if( m_nickIndex.size() )
556 return;
557 }
558
559 m_nickIndex.clear();
560
561 for( LIB_TABLE_ROWS_ITER it = m_rows.begin(); it != m_rows.end(); ++it )
562 m_nickIndex.insert( INDEX_VALUE( it->GetNickName(), it - m_rows.begin() ) );
563 }
564
566 {
567 // The dialog lib table editor may not maintain the nickIndex.
568 // Lazy indexing may be required. To handle lazy indexing, we must enforce
569 // that "nickIndex" is either empty or accurate, but never inaccurate.
570 if( !m_nickIndex.size() )
571 reindex( false );
572 }
573
574private:
575 friend class PANEL_FP_LIB_TABLE;
576 friend class LIB_TABLE_GRID;
577
578protected:
580
582 typedef std::map<wxString,int> INDEX; // "int" is std::vector array index
583 typedef INDEX::iterator INDEX_ITER;
584 typedef INDEX::const_iterator INDEX_CITER;
585 typedef INDEX::value_type INDEX_VALUE;
586
589
591
593 mutable int m_version;
594
596 mutable std::shared_mutex m_nickIndexMutex;
597};
598
599#endif // _LIB_TABLE_BASE_H_
Hold an error message and may be used when throwing exceptions containing meaningful error messages.
Definition: ki_exception.h:76
A logical library item identifier and consists of various portions much like a URI.
Definition: lib_id.h:49
This abstract base class mixes any object derived from LIB_TABLE into wxGridTableBase so the result c...
Hold a record identifying a library accessed by the appropriate plug in object in the LIB_TABLE.
void SetFullURI(const wxString &aFullURI)
Change the full URI for the library.
bool visible
Whether the LIB_TABLE_ROW is visible in choosers.
const wxString & GetOptions() const
Return the options string, which may hold a password or anything else needed to instantiate the under...
std::mutex & GetMutex()
wxString nickName
const wxString & GetDescr() const
Return the description of the library referenced by this row.
virtual LIB_TABLE_ROW * do_clone() const =0
bool GetIsLoaded() const
std::unique_ptr< STRING_UTF8_MAP > properties
void SetVisible(bool aVisible=true)
void operator=(const LIB_TABLE_ROW &aRow)
wxString options
void SetNickName(const wxString &aNickName)
Change the logical name of this library, useful for an editor.
wxString uri_user
what user entered from UI or loaded from disk
LIB_TABLE * GetParent() const
bool operator!=(const LIB_TABLE_ROW &r) const
virtual const wxString GetType() const =0
Return the type of library represented by this row.
wxString description
void SetEnabled(bool aEnabled=true)
Change the enabled status of this library.
void SetDescr(const wxString &aDescr)
Change the description of the library referenced by this row.
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 enabled
Whether the LIB_TABLE_ROW is enabled.
void SetParent(LIB_TABLE *aParent)
bool m_loaded
Whether the LIB_TABLE_ROW is loaded.
void setProperties(STRING_UTF8_MAP *aProperties)
LIB_TABLE_ROW(const LIB_TABLE_ROW &aRow)
void SetLoaded(bool aLoaded)
Mark the row as being a loaded library.
virtual ~LIB_TABLE_ROW()
LIB_TABLE * m_parent
Pointer to the table this row lives in (maybe null)
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 ...
const wxString & GetNickName() const
std::mutex m_loadMutex
LIB_TABLE_ROW(const wxString &aNick, const wxString &aURI, const wxString &aOptions, const wxString &aDescr=wxEmptyString, LIB_TABLE *aParent=nullptr)
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...
LIB_TABLE_ROW * clone() const
bool operator==(const LIB_TABLE_ROW &r) const
bool GetIsEnabled() const
void SetOptions(const wxString &aOptions)
Change the library options strings.
const STRING_UTF8_MAP * GetProperties() const
Return the constant #PROPERTIES for this library (LIB_TABLE_ROW).
bool GetIsVisible() const
Manage LIB_TABLE_ROW records (rows), and can be searched based on library nickname.
const wxString GetDescription(const wxString &aNickname)
std::vector< wxString > GetLogicalLibs()
Return the logical library names, all of them that are pertinent to a look up done on this LIB_TABLE.
bool HasLibrary(const wxString &aNickname, bool aCheckEnabled=false) const
Test for the existence of aNickname in the library table.
int m_version
Versioning to handle importing old tables.
LIB_TABLE_ROW & At(unsigned aIndex)
Get the 'n'th LIB_TABLE_ROW object.
bool InsertRow(LIB_TABLE_ROW *aRow, bool doReplace=false)
Adds aRow if it does not already exist or if doReplace is true.
bool operator==(const LIB_TABLE &r) const
Compares this table against another.
LIB_TABLE_ROWS m_rows
virtual ~LIB_TABLE()
bool migrate()
Updates the env vars from older version of KiCad, provided they do not currently resolve to anything.
void Load(const wxString &aFileName)
Load the library table using the path defined by aFileName aFallBackTable.
bool HasLibraryWithPath(const wxString &aPath) const
Test for the existence of aPath in the library table.
virtual void Format(OUTPUTFORMATTER *aOutput, int aIndentLevel) const =0
Generate the table in s-expression format to aOutput with an indentation level of aIndentLevel.
bool RemoveRow(const LIB_TABLE_ROW *aRow)
Removes a row from the table.
void Clear()
Delete all rows.
INDEX m_nickIndex
this particular key is the nickName within each row.
INDEX::value_type INDEX_VALUE
LIB_TABLE * m_fallBack
wxString GetFullURI(const wxString &aLibNickname, bool aExpandEnvVars=true) const
Return the full URI of the library mapped to aLibNickname.
LIB_TABLE(LIB_TABLE *aFallBackTable=nullptr)
Build a library table by pre-pending this table fragment in front of aFallBackTable.
bool IsEmpty(bool aIncludeFallback=true)
Return true if the table is empty.
unsigned GetCount() const
Get the number of rows contained in the table.
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...
std::map< wxString, int > INDEX
this is a non-owning index into the LIB_TABLE_ROWS table
INDEX::const_iterator INDEX_CITER
const LIB_TABLE_ROW * FindRowByURI(const wxString &aURI)
void reindex(bool aForce)
Rebuilds the m_nickIndex.
std::shared_mutex m_nickIndexMutex
Mutex to protect access to the nickIndex variable.
bool operator!=(const LIB_TABLE &r) const
void Save(const wxString &aFileName) const
Write this library table to aFileName in s-expression form.
void ensureIndex()
static STRING_UTF8_MAP * ParseOptions(const std::string &aOptionsList)
Parses aOptionsList and places the result into a #PROPERTIES object which is returned.
const LIB_TABLE_ROW & At(unsigned aIndex) const
Get the 'n'th LIB_TABLE_ROW object.
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,...
INDEX::iterator INDEX_ITER
int GetVersion() const
Returns the version number (0 if unset)
static UTF8 FormatOptions(const STRING_UTF8_MAP *aProperties)
Returns a list of options from the aProperties parameter.
An interface used to output 8 bit text in a convenient way.
Definition: richio.h:310
Dialog to show and edit symbol library tables.
A PROJECT can hold stuff it knows nothing about, in the form of _ELEM derivatives.
Definition: project.h:75
A name/value tuple with unique names and optional values.
An 8 bit string that is assuredly encoded in UTF8, and supplies special conversion support to and fro...
Definition: utf8.h:71
boost::ptr_vector< LIB_TABLE_ROW > LIB_TABLE_ROWS
LIB_TABLE_ROWS::iterator LIB_TABLE_ROWS_ITER
LIB_TABLE_ROW * new_clone(const LIB_TABLE_ROW &aRow)
Allows boost pointer containers to make clones of the data stored in them.
LIB_TABLE_ROWS::const_iterator LIB_TABLE_ROWS_CITER