Store information about buffering & caching in properties

PROPERTIES object has been recreated every time it was needed, using
two fields in PART_LIB class. Now the buffering & caching settings are
stored directly in a PROPERTIES object.
This commit is contained in:
Maciej Suminski 2017-02-28 10:17:11 +01:00
parent fd4e2b042c
commit 8b8de76aa5
4 changed files with 56 additions and 89 deletions

View File

@ -64,11 +64,9 @@ PART_LIB::PART_LIB( int aType, const wxString& aFileName, SCH_IO_MGR::SCH_FILE_T
type = aType;
isModified = false;
timeStamp = 0;
isCache = false;
timeStamp = wxDateTime::Now();
versionMajor = 0; // Will be updated after reading the lib file
versionMinor = 0; // Will be updated after reading the lib file
m_buffering = false;
fileName = aFileName;
@ -76,6 +74,7 @@ PART_LIB::PART_LIB( int aType, const wxString& aFileName, SCH_IO_MGR::SCH_FILE_T
fileName = "unnamed.lib";
m_plugin.reset( SCH_IO_MGR::FindPlugin( m_pluginType ) );
m_properties = std::make_unique<PROPERTIES>();
}
@ -89,35 +88,24 @@ void PART_LIB::Save( bool aSaveDocFile )
wxCHECK_RET( m_plugin != NULL, wxString::Format( "no plugin defined for library `%s`.",
fileName.GetFullPath() ) );
std::unique_ptr< PROPERTIES > props;
PROPERTIES props;
if( !aSaveDocFile )
{
props.reset( new PROPERTIES );
(*props.get())[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
}
props[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
m_plugin->SaveLibrary( fileName.GetFullPath(), props.get() );
m_plugin->SaveLibrary( fileName.GetFullPath(), &props );
isModified = false;
}
void PART_LIB::Create( const wxString& aFileName )
{
std::unique_ptr< PROPERTIES > props;
if( isCache )
{
props.reset( new PROPERTIES );
(*props.get())[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
}
wxString tmpFileName = fileName.GetFullPath();
if( !aFileName.IsEmpty() )
tmpFileName = aFileName;
m_plugin->CreateSymbolLib( tmpFileName, props.get() );
m_plugin->CreateSymbolLib( tmpFileName, m_properties.get() );
}
@ -131,6 +119,33 @@ void PART_LIB::SetPluginType( SCH_IO_MGR::SCH_FILE_T aPluginType )
}
bool PART_LIB::IsCache() const
{
return m_properties->Exists( SCH_LEGACY_PLUGIN::PropNoDocFile );
}
void PART_LIB::SetCache()
{
(*m_properties)[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
}
bool PART_LIB::IsBuffering() const
{
return m_properties->Exists( SCH_LEGACY_PLUGIN::PropBuffering );
}
void PART_LIB::EnableBuffering( bool aEnable )
{
if( aEnable )
(*m_properties)[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
else
m_properties->Clear( SCH_LEGACY_PLUGIN::PropBuffering );
}
void PART_LIB::GetAliasNames( wxArrayString& aNames )
{
m_plugin->EnumerateSymbolLib( aNames, fileName.GetFullPath() );
@ -177,20 +192,7 @@ void PART_LIB::GetEntryTypePowerNames( wxArrayString& aNames )
LIB_ALIAS* PART_LIB::FindAlias( const wxString& aName )
{
std::unique_ptr< PROPERTIES > props;
if( isCache || m_buffering )
{
props.reset( new PROPERTIES );
if( isCache )
(*props.get())[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
if( m_buffering )
(*props.get())[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
}
return m_plugin->LoadSymbol( fileName.GetFullPath(), aName, props.get() );
return m_plugin->LoadSymbol( fileName.GetFullPath(), aName, m_properties.get() );
}
@ -232,25 +234,12 @@ bool PART_LIB::HasPowerParts()
void PART_LIB::AddPart( LIB_PART* aPart )
{
std::unique_ptr< PROPERTIES > props;
if( isCache || m_buffering )
{
props.reset( new PROPERTIES );
if( isCache )
(*props.get())[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
if( m_buffering )
(*props.get())[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
}
// add a clone, not the caller's copy, the plugin take ownership of the new symbol.
m_plugin->SaveSymbol( fileName.GetFullPath(), new LIB_PART( *aPart, this ), props.get() );
m_plugin->SaveSymbol( fileName.GetFullPath(), new LIB_PART( *aPart, this ), m_properties.get() );
// If we are not buffering, the library file is updated immediately when the plugin
// SaveSymbol() function is called.
if( m_buffering )
if( IsBuffering() )
isModified = true;
++m_mod_hash;
@ -261,24 +250,11 @@ LIB_ALIAS* PART_LIB::RemoveAlias( LIB_ALIAS* aEntry )
{
wxCHECK_MSG( aEntry != NULL, NULL, "NULL pointer cannot be removed from library." );
std::unique_ptr< PROPERTIES > props;
if( isCache || m_buffering )
{
props.reset( new PROPERTIES );
if( isCache )
(*props.get())[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
if( m_buffering )
(*props.get())[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
}
m_plugin->DeleteAlias( fileName.GetFullPath(), aEntry->GetName(), props.get() );
m_plugin->DeleteAlias( fileName.GetFullPath(), aEntry->GetName(), m_properties.get() );
// If we are not buffering, the library file is updated immediately when the plugin
// SaveSymbol() function is called.
if( m_buffering )
if( IsBuffering() )
isModified = true;
++m_mod_hash;
@ -291,28 +267,15 @@ LIB_PART* PART_LIB::ReplacePart( LIB_PART* aOldPart, LIB_PART* aNewPart )
wxASSERT( aOldPart != NULL );
wxASSERT( aNewPart != NULL );
std::unique_ptr< PROPERTIES > props;
if( isCache || m_buffering )
{
props.reset( new PROPERTIES );
if( isCache )
(*props.get())[ SCH_LEGACY_PLUGIN::PropNoDocFile ] = "";
if( m_buffering )
(*props.get())[ SCH_LEGACY_PLUGIN::PropBuffering ] = "";
}
m_plugin->DeleteSymbol( fileName.GetFullPath(), aOldPart->GetName(), props.get() );
m_plugin->DeleteSymbol( fileName.GetFullPath(), aOldPart->GetName(), m_properties.get() );
LIB_PART* my_part = new LIB_PART( *aNewPart, this );
m_plugin->SaveSymbol( fileName.GetFullPath(), my_part, props.get() );
m_plugin->SaveSymbol( fileName.GetFullPath(), my_part, m_properties.get() );
// If we are not buffering, the library file is updated immediately when the plugin
// SaveSymbol() function is called.
if( m_buffering )
if( IsBuffering() )
isModified = true;
++m_mod_hash;

View File

@ -328,15 +328,13 @@ class PART_LIB
wxDateTime timeStamp; ///< Library save time and date.
int versionMajor; ///< Library major version number.
int versionMinor; ///< Library minor version number.
bool isCache; /**< False for the "standard" libraries,
True for the library cache */
wxString header; ///< first line of loaded library.
bool isModified; ///< Library modification status.
int m_mod_hash; ///< incremented each time library is changed.
bool m_buffering; ///< Set to true to prevent file write on every change.
SCH_IO_MGR::SCH_FILE_T m_pluginType;
std::unique_ptr< SCH_PLUGIN > m_plugin;
std::unique_ptr< PROPERTIES > m_properties; ///< Library properties
public:
PART_LIB( int aType, const wxString& aFileName,
@ -379,11 +377,13 @@ public:
return isModified;
}
bool IsCache() const { return isCache; }
bool IsCache() const;
void SetCache( void ) { isCache = true; }
void SetCache();
void EnableBuffering( bool aEnable = true ) { m_buffering = aEnable; }
bool IsBuffering() const;
void EnableBuffering( bool aEnable = true );
void Save( bool aSaveDocFile = true );

View File

@ -3392,12 +3392,7 @@ bool SCH_LEGACY_PLUGIN::writeDocFile( const PROPERTIES* aProperties )
bool SCH_LEGACY_PLUGIN::isBuffering( const PROPERTIES* aProperties )
{
std::string propName( SCH_LEGACY_PLUGIN::PropBuffering );
if( aProperties && aProperties->find( propName ) != aProperties->end() )
return true;
return false;
return ( aProperties && aProperties->Exists( SCH_LEGACY_PLUGIN::PropBuffering ) );
}

View File

@ -36,6 +36,15 @@ class PROPERTIES : public std::map< std::string, UTF8 >
// alphabetical tuple of name and value hereby defined.
public:
bool Clear( const std::string& aProperty )
{
return erase( aProperty ) > 0;
}
bool Exists( const std::string& aProperty ) const
{
return count( aProperty ) > 0;
}
/**
* Function Value