KiCad PCB EDA Suite
sch_lib_plugin_cache.cpp
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) 2022 KiCad Developers, see AUTHORS.txt for contributors.
5 *
6 * @author Wayne Stambaugh <[email protected]>
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 along
19 * with this program. If not, see <http://www.gnu.org/licenses/>.
20 */
21
23
24#include <lib_symbol.h>
25#include <wx_filename.h>
26
27
28SCH_LIB_PLUGIN_CACHE::SCH_LIB_PLUGIN_CACHE( const wxString& aFullPathAndFileName ) :
29 m_modHash( 1 ),
30 m_fileName( aFullPathAndFileName ),
31 m_libFileName( aFullPathAndFileName ),
32 m_isWritable( true ),
33 m_isModified( false )
34{
36}
37
38
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}
47
48
49void SCH_LIB_PLUGIN_CACHE::Save( const std::optional<bool>& aOpt )
50{
51 wxCHECK( false, /* void */ );
52}
53
54
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}
63
64
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}
75
76
77bool SCH_LIB_PLUGIN_CACHE::IsFile( const wxString& aFullPathAndFileName ) const
78{
79 return m_fileName == aFullPathAndFileName;
80}
81
82
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}
92
93
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}
159
160
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}
176
177
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}
const char * name
Definition: DXF_plotter.cpp:56
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
Define a library symbol object.
Definition: lib_symbol.h:99
bool IsRoot() const override
For symbols derived from other symbols, IsRoot() indicates no derivation.
Definition: lib_symbol.h:187
LIB_ITEMS_CONTAINER & GetDrawItems()
Return a reference to the draw item list.
Definition: lib_symbol.h:508
wxString GetName() const override
Definition: lib_symbol.h:138
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_SYMBOL_SPTR SharedPtr() const
Definition: lib_symbol.h:110
bool IsFile(const wxString &aFullPathAndFileName) const
LIB_SYMBOL * removeSymbol(LIB_SYMBOL *aAlias)
virtual LIB_SYMBOL * GetSymbol(const wxString &aName)
wxFileName GetRealFile() const
SCH_LIB_PLUGIN_CACHE(const wxString &aLibraryPath)
virtual void AddSymbol(const LIB_SYMBOL *aSymbol)
virtual void Save(const std::optional< bool > &aOpt=std::nullopt)
Save the entire library to file m_libFileName;.
static void ResolvePossibleSymlinks(wxFileName &aFilename)
Definition: wx_filename.cpp:92
@ LIB_FIELD_T
Definition: typeinfo.h:208