From 445ed380b89bccdad3f50b9cfe779558fda3a46d Mon Sep 17 00:00:00 2001 From: Jeff Young Date: Wed, 31 Jan 2024 12:18:01 +0000 Subject: [PATCH] Formatting. --- common/settings/json_settings.cpp | 135 ++++++++++++++++-------------- common/settings/parameters.cpp | 4 +- include/settings/json_settings.h | 2 +- include/settings/parameters.h | 59 ++++++------- 4 files changed, 101 insertions(+), 99 deletions(-) diff --git a/common/settings/json_settings.cpp b/common/settings/json_settings.cpp index 6f1bf1a25c..f29b8d9150 100644 --- a/common/settings/json_settings.cpp +++ b/common/settings/json_settings.cpp @@ -100,7 +100,7 @@ JSON_SETTINGS::JSON_SETTINGS( const wxString& aFilename, SETTINGS_LOC aLocation, JSON_SETTINGS::~JSON_SETTINGS() { - for( auto param: m_params ) + for( PARAM_BASE* param: m_params ) delete param; m_params.clear(); @@ -136,7 +136,7 @@ JSON_SETTINGS_INTERNALS* JSON_SETTINGS::Internals() void JSON_SETTINGS::Load() { - for( auto param : m_params ) + for( PARAM_BASE* param : m_params ) { try { @@ -163,68 +163,76 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory ) LOCALE_IO locale; - auto migrateFromLegacy = [&] ( wxFileName& aPath ) { - // Backup and restore during migration so that the original can be mutated if convenient - bool backed_up = false; - wxFileName temp; - - if( aPath.IsDirWritable() ) - { - temp.AssignTempFileName( aPath.GetFullPath() ); - - if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) ) + auto migrateFromLegacy = + [&] ( wxFileName& aPath ) { - wxLogTrace( traceSettings, wxT( "%s: could not create temp file for migration" ), - GetFullFilename() ); - } - else - backed_up = true; - } + // Backup and restore during migration so that the original can be mutated if + // convenient + bool backed_up = false; + wxFileName temp; - // Silence popups if legacy file is read-only - wxLogNull doNotLog; + if( aPath.IsDirWritable() ) + { + temp.AssignTempFileName( aPath.GetFullPath() ); - wxConfigBase::DontCreateOnDemand(); - auto cfg = std::make_unique( wxT( "" ), wxT( "" ), aPath.GetFullPath() ); + if( !wxCopyFile( aPath.GetFullPath(), temp.GetFullPath() ) ) + { + wxLogTrace( traceSettings, + wxT( "%s: could not create temp file for migration" ), + GetFullFilename() ); + } + else + { + backed_up = true; + } + } - // If migrate fails or is not implemented, fall back to built-in defaults that were - // already loaded above - if( !MigrateFromLegacy( cfg.get() ) ) - { - success = false; - wxLogTrace( traceSettings, - wxT( "%s: migrated; not all settings were found in legacy file" ), - GetFullFilename() ); - } - else - { - success = true; - wxLogTrace( traceSettings, wxT( "%s: migrated from legacy format" ), - GetFullFilename() ); - } + // Silence popups if legacy file is read-only + wxLogNull doNotLog; - if( backed_up ) - { - cfg.reset(); + wxConfigBase::DontCreateOnDemand(); + auto cfg = std::make_unique( wxT( "" ), wxT( "" ), + aPath.GetFullPath() ); - if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) ) - { - wxLogTrace( traceSettings, - wxT( "migrate; copy temp file %s to %s failed" ), - temp.GetFullPath(), aPath.GetFullPath() ); - } + // If migrate fails or is not implemented, fall back to built-in defaults that + // were already loaded above + if( !MigrateFromLegacy( cfg.get() ) ) + { + success = false; + wxLogTrace( traceSettings, + wxT( "%s: migrated; not all settings were found in legacy file" ), + GetFullFilename() ); + } + else + { + success = true; + wxLogTrace( traceSettings, wxT( "%s: migrated from legacy format" ), + GetFullFilename() ); + } - if( !wxRemoveFile( temp.GetFullPath() ) ) - { - wxLogTrace( traceSettings, - wxT( "migrate; failed to remove temp file %s" ), - temp.GetFullPath() ); - } - } + if( backed_up ) + { + cfg.reset(); - // Either way, we want to clean up the old file afterwards - legacy_migrated = true; - }; + if( !wxCopyFile( temp.GetFullPath(), aPath.GetFullPath() ) ) + { + wxLogTrace( traceSettings, + wxT( "migrate; copy temp file %s to %s failed" ), + temp.GetFullPath(), + aPath.GetFullPath() ); + } + + if( !wxRemoveFile( temp.GetFullPath() ) ) + { + wxLogTrace( traceSettings, + wxT( "migrate; failed to remove temp file %s" ), + temp.GetFullPath() ); + } + } + + // Either way, we want to clean up the old file afterwards + legacy_migrated = true; + }; wxFileName path; @@ -298,7 +306,7 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory ) if( filever >= 0 && filever < m_schemaVersion ) { wxLogTrace( traceSettings, wxT( "%s: attempting migration from version " - "%d to %d" ), + "%d to %d" ), GetFullFilename(), filever, m_schemaVersion ); if( Migrate() ) @@ -339,7 +347,7 @@ bool JSON_SETTINGS::LoadFromFile( const wxString& aDirectory ) Load(); // And finally load any nested settings - for( auto settings : m_nested_settings ) + for( NESTED_SETTINGS* settings : m_nested_settings ) settings->LoadFromFile(); wxLogTrace( traceSettings, wxT( "Loaded <%s> with schema %d" ), GetFullFilename(), @@ -367,7 +375,7 @@ bool JSON_SETTINGS::Store() { bool modified = false; - for( auto param : m_params ) + for( PARAM_BASE* param : m_params ) { modified |= !param->MatchesFile( this ); param->Store( this ); @@ -379,7 +387,7 @@ bool JSON_SETTINGS::Store() void JSON_SETTINGS::ResetToDefaults() { - for( auto param : m_params ) + for( PARAM_BASE* param : m_params ) param->SetDefault(); } @@ -432,7 +440,7 @@ bool JSON_SETTINGS::SaveToFile( const wxString& aDirectory, bool aForce ) bool modified = false; - for( auto settings : m_nested_settings ) + for( NESTED_SETTINGS* settings : m_nested_settings ) modified |= settings->SaveToFile(); modified |= Store(); @@ -832,8 +840,9 @@ void JSON_SETTINGS::ReleaseNestedSettings( NESTED_SETTINGS* aSettings ) return; auto it = std::find_if( m_nested_settings.begin(), m_nested_settings.end(), - [&aSettings]( const JSON_SETTINGS* aPtr ) { - return aPtr == aSettings; + [&aSettings]( const JSON_SETTINGS* aPtr ) + { + return aPtr == aSettings; } ); if( it != m_nested_settings.end() ) diff --git a/common/settings/parameters.cpp b/common/settings/parameters.cpp index 8c21b45ab4..87ab25dd5f 100644 --- a/common/settings/parameters.cpp +++ b/common/settings/parameters.cpp @@ -311,13 +311,13 @@ void PARAM_WXSTRING_MAP::Load( JSON_SETTINGS* aSettings, bool aResetIfMissing ) m_ptr->clear(); for( const auto& el : js->items() ) - { ( *m_ptr )[wxString( el.key().c_str(), wxConvUTF8 )] = el.value().get(); - } } } else if( aResetIfMissing ) + { *m_ptr = m_default; + } } diff --git a/include/settings/json_settings.h b/include/settings/json_settings.h index 4d22ce3745..2434a52daa 100644 --- a/include/settings/json_settings.h +++ b/include/settings/json_settings.h @@ -116,7 +116,7 @@ public: /** * 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 -c * @return true if the file was saved + * @return true if the file was saved */ virtual bool SaveToFile( const wxString& aDirectory = "", bool aForce = false ); diff --git a/include/settings/parameters.h b/include/settings/parameters.h index b33cb97a63..785e6bfd11 100644 --- a/include/settings/parameters.h +++ b/include/settings/parameters.h @@ -2,7 +2,7 @@ * This program source code file is part of KiCad, a free EDA CAD application. * * Copyright (C) 2020 Jon Evans - * 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 * under the terms of the GNU General Public License as published by the @@ -18,8 +18,8 @@ * with this program. If not, see . */ -#ifndef _PARAMETERS_H -#define _PARAMETERS_H +#ifndef PARAMETERS_H +#define PARAMETERS_H #include #include @@ -69,13 +69,8 @@ public: const std::string& GetJsonPath() const { return m_path; } protected: - /** - * the string used to store the param in json files - */ - std::string m_path; - - ///! True if the parameter pointer should never be overwritten - bool m_readOnly; + std::string m_path; ///< Address of the param in the json files + bool m_readOnly; ///< Indicates param pointer should never be overwritten }; @@ -121,7 +116,9 @@ public: *m_ptr = val; } else if( aResetIfMissing ) + { *m_ptr = m_default; + } } void Store( JSON_SETTINGS* aSettings ) const override @@ -148,13 +145,13 @@ public: } private: - ValueType m_min; - ValueType m_max; - bool m_use_minmax; + ValueType m_min; + ValueType m_max; + bool m_use_minmax; protected: ValueType* m_ptr; - ValueType m_default; + ValueType m_default; }; /** @@ -240,7 +237,9 @@ public: } else if( aResetIfMissing ) + { *m_ptr = m_default; + } } void Store( JSON_SETTINGS* aSettings ) const override @@ -316,10 +315,8 @@ public: bool MatchesFile( JSON_SETTINGS* aSettings ) const override; private: - ValueType m_default; - - std::function m_getter; - + ValueType m_default; + std::function m_getter; std::function m_setter; }; @@ -404,11 +401,11 @@ public: private: ValueType* m_ptr; - ValueType m_default; - ValueType m_min; - ValueType m_max; - bool m_use_minmax; - double m_scale; + ValueType m_default; + ValueType m_min; + ValueType m_max; + bool m_use_minmax; + double m_scale; }; template @@ -442,8 +439,7 @@ public: protected: std::vector* m_ptr; - - std::vector m_default; + std::vector m_default; }; template @@ -477,8 +473,7 @@ public: protected: std::set* m_ptr; - - std::set m_default; + std::set m_default; }; /** @@ -568,8 +563,7 @@ public: private: std::map* m_ptr; - - std::map m_default; + std::map m_default; }; @@ -580,8 +574,8 @@ class PARAM_WXSTRING_MAP : public PARAM_BASE { public: PARAM_WXSTRING_MAP( const std::string& aJsonPath, std::map* aPtr, - std::initializer_list> aDefault, - bool aReadOnly = false ) : + std::initializer_list> aDefault, + bool aReadOnly = false ) : PARAM_BASE( aJsonPath, aReadOnly ), m_ptr( aPtr ), m_default( aDefault ) @@ -600,8 +594,7 @@ public: private: std::map* m_ptr; - - std::map m_default; + std::map m_default; }; #endif