Formatting.

This commit is contained in:
Jeff Young 2024-01-31 12:18:01 +00:00
parent 7f29ac39d8
commit 445ed380b8
4 changed files with 101 additions and 99 deletions

View File

@ -100,7 +100,7 @@ JSON_SETTINGS::JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation,
JSON_SETTINGS::~JSON_SETTINGS() JSON_SETTINGS::~JSON_SETTINGS()
{ {
for( auto param: m_params ) for( PARAM_BASE* param: m_params )
delete param; delete param;
m_params.clear(); m_params.clear();
@ -136,7 +136,7 @@ JSON_SETTINGS_INTERNALS* JSON_SETTINGS::Internals()
void JSON_SETTINGS::Load() void JSON_SETTINGS::Load()
{ {
for( auto param : m_params ) for( PARAM_BASE* param : m_params )
{ {
try try
{ {
@ -163,8 +163,11 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
LOCALE_IO locale; LOCALE_IO locale;
auto migrateFromLegacy = [&] ( wxFileName& aPath ) { auto migrateFromLegacy =
// Backup and restore during migration so that the original can be mutated if convenient [&] ( wxFileName& aPath )
{
// Backup and restore during migration so that the original can be mutated if
// convenient
bool backed_up = false; bool backed_up = false;
wxFileName temp; wxFileName temp;
@ -174,21 +177,25 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) ) if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) )
{ {
wxLogTrace( traceSettings, wxT( "%s: could not create temp file for migration" ), wxLogTrace( traceSettings,
wxT( "%s: could not create temp file for migration" ),
GetFullFilename() ); GetFullFilename() );
} }
else else
{
backed_up = true; backed_up = true;
} }
}
// Silence popups if legacy file is read-only // Silence popups if legacy file is read-only
wxLogNull doNotLog; wxLogNull doNotLog;
wxConfigBase::DontCreateOnDemand(); wxConfigBase::DontCreateOnDemand();
auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ), aPath.GetFullPath() ); auto cfg = std::make_unique<wxFileConfig>( wxT( "" ), wxT( "" ),
aPath.GetFullPath() );
// If migrate fails or is not implemented, fall back to built-in defaults that were // If migrate fails or is not implemented, fall back to built-in defaults that
// already loaded above // were already loaded above
if( !MigrateFromLegacy( cfg.get() ) ) if( !MigrateFromLegacy( cfg.get() ) )
{ {
success = false; success = false;
@ -211,7 +218,8 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
{ {
wxLogTrace( traceSettings, wxLogTrace( traceSettings,
wxT( "migrate; copy temp file %s to %s failed" ), wxT( "migrate; copy temp file %s to %s failed" ),
temp.GetFullPath(), aPath.GetFullPath() ); temp.GetFullPath(),
aPath.GetFullPath() );
} }
if( !wxRemoveFile( temp.GetFullPath() ) ) if( !wxRemoveFile( temp.GetFullPath() ) )
@ -339,7 +347,7 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory )
Load(); Load();
// And finally load any nested settings // And finally load any nested settings
for( auto settings : m_nested_settings ) for( NESTED_SETTINGS* settings : m_nested_settings )
settings->LoadFromFile(); settings->LoadFromFile();
wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(), wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(),
@ -367,7 +375,7 @@ bool JSON_SETTINGS::Store()
{ {
bool modified = false; bool modified = false;
for( auto param : m_params ) for( PARAM_BASE* param : m_params )
{ {
modified |= !param->MatchesFile( this ); modified |= !param->MatchesFile( this );
param->Store( this ); param->Store( this );
@ -379,7 +387,7 @@ bool JSON_SETTINGS::Store()
void JSON_SETTINGS::ResetToDefaults() void JSON_SETTINGS::ResetToDefaults()
{ {
for( auto param : m_params ) for( PARAM_BASE* param : m_params )
param->SetDefault(); param->SetDefault();
} }
@ -432,7 +440,7 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce )
bool modified = false; bool modified = false;
for( auto settings : m_nested_settings ) for( NESTED_SETTINGS* settings : m_nested_settings )
modified |= settings->SaveToFile(); modified |= settings->SaveToFile();
modified |= Store(); modified |= Store();
@ -832,7 +840,8 @@ void JSON_SETTINGS::ReleaseNestedSettings( NESTED_SETTINGS* aSettings )
return; return;
auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(), auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(),
[&aSettings]( const JSON_SETTINGS* aPtr ) { [&aSettings]( const JSON_SETTINGS* aPtr )
{
return aPtr == aSettings; return aPtr == aSettings;
} ); } );

View File

@ -311,13 +311,13 @@ void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing )
m_ptr->clear(); m_ptr->clear();
for( const auto& el : js->items() ) for( const auto& el : js->items() )
{
( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>(); ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get<wxString>();
} }
} }
}
else if( aResetIfMissing ) else if( aResetIfMissing )
{
*m_ptr = m_default; *m_ptr = m_default;
}
} }

View File

@ -116,7 +116,7 @@ public:
/** /**
* Calls Store() and then writes the contents of the JSON document to a file * Calls Store() and then writes the contents of the JSON document to a file
* @param aDirectory is the directory to save to, including trailing separator * @param aDirectory is the directory to save to, including trailing separator
c * @return true if the file was saved * @return true if the file was saved
*/ */
virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false ); virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false );

View File

@ -2,7 +2,7 @@
* This program source code file is part of KiCad, a free EDA CAD application. * This program source code file is part of KiCad, a free EDA CAD application.
* *
* Copyright (C) 2020 Jon Evans <jon@craftyjon.com> * Copyright (C) 2020 Jon Evans <jon@craftyjon.com>
* Copyright (C) 2020-2021 KiCad Developers, see AUTHORS.txt for contributors. * Copyright (C) 2020-2024 KiCad Developers, see AUTHORS.txt for contributors.
* *
* This program is free software: you can redistribute it and/or modify it * This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the * under the terms of the GNU General Public License as published by the
@ -18,8 +18,8 @@
* with this program. If not, see <http://www.gnu.org/licenses/>. * with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef _PARAMETERS_H #ifndef PARAMETERS_H
#define _PARAMETERS_H #define PARAMETERS_H
#include <set> #include <set>
#include <string> #include <string>
@ -69,13 +69,8 @@ public:
const std::string& GetJsonPath() const { return m_path; } const std::string& GetJsonPath() const { return m_path; }
protected: protected:
/** std::string m_path; ///< Address of the param in the json files
* the string used to store the param in json files bool m_readOnly; ///< Indicates param pointer should never be overwritten
*/
std::string m_path;
///! True if the parameter pointer should never be overwritten
bool m_readOnly;
}; };
@ -121,8 +116,10 @@ public:
*m_ptr = val; *m_ptr = val;
} }
else if( aResetIfMissing ) else if( aResetIfMissing )
{
*m_ptr = m_default; *m_ptr = m_default;
} }
}
void Store( JSON_SETTINGS* aSettings ) const override void Store( JSON_SETTINGS* aSettings ) const override
{ {
@ -240,8 +237,10 @@ public:
} }
else if( aResetIfMissing ) else if( aResetIfMissing )
{
*m_ptr = m_default; *m_ptr = m_default;
} }
}
void Store( JSON_SETTINGS* aSettings ) const override void Store( JSON_SETTINGS* aSettings ) const override
{ {
@ -317,9 +316,7 @@ public:
private: private:
ValueType m_default; ValueType m_default;
std::function<ValueType()> m_getter; std::function<ValueType()> m_getter;
std::function<void( ValueType )> m_setter; std::function<void( ValueType )> m_setter;
}; };
@ -442,7 +439,6 @@ public:
protected: protected:
std::vector<Type>* m_ptr; std::vector<Type>* m_ptr;
std::vector<Type> m_default; std::vector<Type> m_default;
}; };
@ -477,7 +473,6 @@ public:
protected: protected:
std::set<Type>* m_ptr; std::set<Type>* m_ptr;
std::set<Type> m_default; std::set<Type> m_default;
}; };
@ -568,7 +563,6 @@ public:
private: private:
std::map<std::string, Value>* m_ptr; std::map<std::string, Value>* m_ptr;
std::map<std::string, Value> m_default; std::map<std::string, Value> m_default;
}; };
@ -600,7 +594,6 @@ public:
private: private:
std::map<wxString, wxString>* m_ptr; std::map<wxString, wxString>* m_ptr;
std::map<wxString, wxString> m_default; std::map<wxString, wxString> m_default;
}; };