KiCad PCB EDA Suite
SCH_LIB_PLUGIN_CACHE Class Referenceabstract

A base cache assistant implementation for the symbol library portion of the SCH_PLUGIN API. More...

#include <sch_lib_plugin_cache.h>

Inheritance diagram for SCH_LIB_PLUGIN_CACHE:
SCH_LEGACY_PLUGIN_CACHE SCH_SEXPR_PLUGIN_CACHE

Public Member Functions

 SCH_LIB_PLUGIN_CACHE (const wxString &aLibraryPath)
 
virtual ~SCH_LIB_PLUGIN_CACHE ()
 
void IncrementModifyHash ()
 
int GetModifyHash ()
 
virtual void Save (const std::optional< bool > &aOpt=std::nullopt)
 Save the entire library to file m_libFileName;. More...
 
virtual void Load ()=0
 
virtual void AddSymbol (const LIB_SYMBOL *aSymbol)
 
virtual void DeleteSymbol (const wxString &aName)=0
 
virtual LIB_SYMBOLGetSymbol (const wxString &aName)
 
wxFileName GetRealFile () const
 
wxDateTime GetLibModificationTime ()
 
bool IsFile (const wxString &aFullPathAndFileName) const
 
bool IsFileChanged () const
 
void SetModified (bool aModified=true)
 
wxString GetLogicalName () const
 
void SetFileName (const wxString &aFileName)
 
wxString GetFileName () const
 
const LIB_SYMBOL_MAPGetSymbolMap () const
 

Protected Member Functions

LIB_SYMBOLremoveSymbol (LIB_SYMBOL *aAlias)
 

Protected Attributes

int m_modHash
 
std::mutex m_modHashMutex
 
wxString m_fileName
 
wxFileName m_libFileName
 
wxDateTime m_fileModTime
 
LIB_SYMBOL_MAP m_symbols
 
bool m_isWritable
 
bool m_isModified
 
SCH_LIB_TYPE m_libType
 

Detailed Description

A base cache assistant implementation for the symbol library portion of the SCH_PLUGIN API.

Definition at line 40 of file sch_lib_plugin_cache.h.

Constructor & Destructor Documentation

◆ SCH_LIB_PLUGIN_CACHE()

SCH_LIB_PLUGIN_CACHE::SCH_LIB_PLUGIN_CACHE ( const wxString &  aLibraryPath)

Definition at line 28 of file sch_lib_plugin_cache.cpp.

28 :
29 m_modHash( 1 ),
30 m_fileName( aFullPathAndFileName ),
31 m_libFileName( aFullPathAndFileName ),
32 m_isWritable( true ),
33 m_isModified( false )
34{
36}

References LT_EESCHEMA, and m_libType.

◆ ~SCH_LIB_PLUGIN_CACHE()

SCH_LIB_PLUGIN_CACHE::~SCH_LIB_PLUGIN_CACHE ( )
virtual

Definition at line 39 of file sch_lib_plugin_cache.cpp.

40{
41 // When the cache is destroyed, all of the alias objects on the heap should be deleted.
42 for( auto& symbol : m_symbols )
43 delete symbol.second;
44
45 m_symbols.clear();
46}

References m_symbols.

Member Function Documentation

◆ AddSymbol()

void SCH_LIB_PLUGIN_CACHE::AddSymbol ( const LIB_SYMBOL aSymbol)
virtual

Definition at line 161 of file sch_lib_plugin_cache.cpp.

162{
163 // aSymbol is cloned in SYMBOL_LIB::AddSymbol(). The cache takes ownership of aSymbol.
164 wxString name = aSymbol->GetName();
165 LIB_SYMBOL_MAP::iterator it = m_symbols.find( name );
166
167 if( it != m_symbols.end() )
168 {
169 removeSymbol( it->second );
170 }
171
172 m_symbols[ name ] = const_cast< LIB_SYMBOL* >( aSymbol );
173 m_isModified = true;
175}
const char * name
Definition: DXF_plotter.cpp:56
Define a library symbol object.
Definition: lib_symbol.h:99
wxString GetName() const override
Definition: lib_symbol.h:138
LIB_SYMBOL * removeSymbol(LIB_SYMBOL *aAlias)

References LIB_SYMBOL::GetName(), IncrementModifyHash(), m_isModified, m_symbols, name, and removeSymbol().

Referenced by SCH_SEXPR_PLUGIN::SaveSymbol(), and SCH_LEGACY_PLUGIN::SaveSymbol().

◆ DeleteSymbol()

virtual void SCH_LIB_PLUGIN_CACHE::DeleteSymbol ( const wxString &  aName)
pure virtual

◆ GetFileName()

wxString SCH_LIB_PLUGIN_CACHE::GetFileName ( void  ) const
inline

Definition at line 88 of file sch_lib_plugin_cache.h.

88{ return m_libFileName.GetFullPath(); }

References m_libFileName.

Referenced by SCH_SEXPR_PLUGIN::SaveLibrary(), and SCH_LEGACY_PLUGIN::SaveLibrary().

◆ GetLibModificationTime()

wxDateTime SCH_LIB_PLUGIN_CACHE::GetLibModificationTime ( )

Definition at line 65 of file sch_lib_plugin_cache.cpp.

66{
67 wxFileName fn = GetRealFile();
68
69 // update the writable flag while we have a wxFileName, in a network this
70 // is possibly quite dynamic anyway.
71 m_isWritable = fn.IsFileWritable();
72
73 return fn.GetModificationTime();
74}
wxFileName GetRealFile() const

References GetRealFile(), and m_isWritable.

Referenced by SCH_SEXPR_PLUGIN_CACHE::Load(), and SCH_LEGACY_PLUGIN_CACHE::Load().

◆ GetLogicalName()

wxString SCH_LIB_PLUGIN_CACHE::GetLogicalName ( ) const
inline

Definition at line 84 of file sch_lib_plugin_cache.h.

84{ return m_libFileName.GetName(); }

References m_libFileName.

◆ GetModifyHash()

int SCH_LIB_PLUGIN_CACHE::GetModifyHash ( )
inline

Definition at line 52 of file sch_lib_plugin_cache.h.

53 {
54 std::lock_guard<std::mutex> mut( m_modHashMutex );
55 return m_modHash;
56 }

References m_modHash, and m_modHashMutex.

Referenced by SCH_SEXPR_PLUGIN::GetModifyHash(), and SCH_LEGACY_PLUGIN::GetModifyHash().

◆ GetRealFile()

wxFileName SCH_LIB_PLUGIN_CACHE::GetRealFile ( ) const

Definition at line 55 of file sch_lib_plugin_cache.cpp.

56{
57 wxFileName fn( m_libFileName );
58
59 // If m_libFileName is a symlink follow it to the real source file
61 return fn;
62}
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:92

References m_libFileName, and WX_FILENAME::ResolvePossibleSymlinks().

Referenced by GetLibModificationTime(), IsFileChanged(), SCH_LEGACY_PLUGIN_CACHE::Save(), and SCH_SEXPR_PLUGIN_CACHE::Save().

◆ GetSymbol()

LIB_SYMBOL * SCH_LIB_PLUGIN_CACHE::GetSymbol ( const wxString &  aName)
virtual

Definition at line 178 of file sch_lib_plugin_cache.cpp.

179{
180 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aName );
181
182 if( it != m_symbols.end() )
183 {
184 return it->second;
185 }
186
187 return nullptr;
188}

References m_symbols.

Referenced by EESCHEMA_JOBS_HANDLER::JobSymExportSvg().

◆ GetSymbolMap()

const LIB_SYMBOL_MAP & SCH_LIB_PLUGIN_CACHE::GetSymbolMap ( ) const
inline

Definition at line 90 of file sch_lib_plugin_cache.h.

90{ return m_symbols; }

References m_symbols.

Referenced by EESCHEMA_JOBS_HANDLER::JobSymExportSvg().

◆ IncrementModifyHash()

void SCH_LIB_PLUGIN_CACHE::IncrementModifyHash ( )
inline

Definition at line 46 of file sch_lib_plugin_cache.h.

47 {
48 std::lock_guard<std::mutex> mut( m_modHashMutex );
49 m_modHash++;
50 }

References m_modHash, and m_modHashMutex.

Referenced by AddSymbol(), SCH_LEGACY_PLUGIN_CACHE::DeleteSymbol(), SCH_LEGACY_PLUGIN_CACHE::Load(), and removeSymbol().

◆ IsFile()

bool SCH_LIB_PLUGIN_CACHE::IsFile ( const wxString &  aFullPathAndFileName) const

◆ IsFileChanged()

bool SCH_LIB_PLUGIN_CACHE::IsFileChanged ( ) const

Definition at line 83 of file sch_lib_plugin_cache.cpp.

84{
85 wxFileName fn = GetRealFile();
86
87 if( m_fileModTime.IsValid() && fn.IsOk() && fn.FileExists() )
88 return fn.GetModificationTime() != m_fileModTime;
89
90 return false;
91}

References GetRealFile(), and m_fileModTime.

Referenced by SCH_SEXPR_PLUGIN::cacheLib(), and SCH_LEGACY_PLUGIN::cacheLib().

◆ Load()

virtual void SCH_LIB_PLUGIN_CACHE::Load ( )
pure virtual

◆ removeSymbol()

LIB_SYMBOL * SCH_LIB_PLUGIN_CACHE::removeSymbol ( LIB_SYMBOL aAlias)
protected

Definition at line 94 of file sch_lib_plugin_cache.cpp.

95{
96 wxCHECK_MSG( aSymbol != nullptr, nullptr, "NULL pointer cannot be removed from library." );
97
98 LIB_SYMBOL* firstChild = nullptr;
99 LIB_SYMBOL_MAP::iterator it = m_symbols.find( aSymbol->GetName() );
100
101 if( it == m_symbols.end() )
102 return nullptr;
103
104 // If the entry pointer doesn't match the name it is mapped to in the library, we
105 // have done something terribly wrong.
106 wxCHECK_MSG( *it->second == aSymbol, nullptr,
107 "Pointer mismatch while attempting to remove alias entry <" + aSymbol->GetName() +
108 "> from library cache <" + m_libFileName.GetName() + ">." );
109
110 // If the symbol is a root symbol used by other symbols find the first alias that uses
111 // the root symbol and make it the new root.
112 if( aSymbol->IsRoot() )
113 {
114 for( const std::pair<const wxString, LIB_SYMBOL*>& entry : m_symbols )
115 {
116 if( entry.second->IsAlias()
117 && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
118 {
119 firstChild = entry.second;
120 break;
121 }
122 }
123
124 if( firstChild )
125 {
126 for( LIB_ITEM& drawItem : aSymbol->GetDrawItems() )
127 {
128 if( drawItem.Type() == LIB_FIELD_T )
129 {
130 LIB_FIELD& field = static_cast<LIB_FIELD&>( drawItem );
131
132 if( firstChild->FindField( field.GetCanonicalName() ) )
133 continue;
134 }
135
136 LIB_ITEM* newItem = (LIB_ITEM*) drawItem.Clone();
137 drawItem.SetParent( firstChild );
138 firstChild->AddDrawItem( newItem );
139 }
140
141 // Reparent the remaining aliases.
142 for( const std::pair<const wxString, LIB_SYMBOL*>& entry : m_symbols )
143 {
144 if( entry.second->IsAlias()
145 && entry.second->GetParent().lock() == aSymbol->SharedPtr() )
146 {
147 entry.second->SetParent( firstChild );
148 }
149 }
150 }
151 }
152
153 m_symbols.erase( it );
154 delete aSymbol;
155 m_isModified = true;
157 return firstChild;
158}
virtual void SetParent(EDA_ITEM *aParent)
Definition: eda_item.h:100
virtual EDA_ITEM * Clone() const
Create a duplicate of this item with linked list members set to NULL.
Definition: eda_item.cpp:82
Field object used in symbol libraries.
Definition: lib_field.h:61
wxString GetCanonicalName() const
Get a non-language-specific name for a field which can be used for storage, variable look-up,...
Definition: lib_field.cpp:479
The base class for drawable items used by schematic library symbols.
Definition: lib_item.h:61
void AddDrawItem(LIB_ITEM *aItem, bool aSort=true)
Add a new draw aItem to the draw object list and sort according to aSort.
Definition: lib_symbol.cpp:823
LIB_FIELD * FindField(const wxString &aFieldName)
Find a field within this symbol matching aFieldName and returns it or NULL if not found.
@ LIB_FIELD_T
Definition: typeinfo.h:208

References LIB_SYMBOL::AddDrawItem(), EDA_ITEM::Clone(), LIB_SYMBOL::FindField(), LIB_FIELD::GetCanonicalName(), LIB_SYMBOL::GetDrawItems(), LIB_SYMBOL::GetName(), IncrementModifyHash(), LIB_SYMBOL::IsRoot(), LIB_FIELD_T, m_isModified, m_libFileName, m_symbols, EDA_ITEM::SetParent(), and LIB_SYMBOL::SharedPtr().

Referenced by AddSymbol().

◆ Save()

void SCH_LIB_PLUGIN_CACHE::Save ( const std::optional< bool > &  aOpt = std::nullopt)
virtual

Save the entire library to file m_libFileName;.

Reimplemented in SCH_LEGACY_PLUGIN_CACHE, and SCH_SEXPR_PLUGIN_CACHE.

Definition at line 49 of file sch_lib_plugin_cache.cpp.

50{
51 wxCHECK( false, /* void */ );
52}

◆ SetFileName()

void SCH_LIB_PLUGIN_CACHE::SetFileName ( const wxString &  aFileName)
inline

◆ SetModified()

void SCH_LIB_PLUGIN_CACHE::SetModified ( bool  aModified = true)
inline

Member Data Documentation

◆ m_fileModTime

wxDateTime SCH_LIB_PLUGIN_CACHE::m_fileModTime
protected

◆ m_fileName

wxString SCH_LIB_PLUGIN_CACHE::m_fileName
protected

Definition at line 98 of file sch_lib_plugin_cache.h.

Referenced by IsFile().

◆ m_isModified

◆ m_isWritable

bool SCH_LIB_PLUGIN_CACHE::m_isWritable
protected

Definition at line 102 of file sch_lib_plugin_cache.h.

Referenced by GetLibModificationTime().

◆ m_libFileName

◆ m_libType

SCH_LIB_TYPE SCH_LIB_PLUGIN_CACHE::m_libType
protected

Definition at line 104 of file sch_lib_plugin_cache.h.

Referenced by SCH_LEGACY_PLUGIN_CACHE::Load(), and SCH_LIB_PLUGIN_CACHE().

◆ m_modHash

int SCH_LIB_PLUGIN_CACHE::m_modHash
protected

◆ m_modHashMutex

std::mutex SCH_LIB_PLUGIN_CACHE::m_modHashMutex
protected

Definition at line 96 of file sch_lib_plugin_cache.h.

Referenced by GetModifyHash(), and IncrementModifyHash().

◆ m_symbols


The documentation for this class was generated from the following files: