diff --git a/common/settings/settings_manager.cpp b/common/settings/settings_manager.cpp index ede063ba36..e8c41c61ef 100644 --- a/common/settings/settings_manager.cpp +++ b/common/settings/settings_manager.cpp @@ -69,8 +69,7 @@ SETTINGS_MANAGER::SETTINGS_MANAGER( bool aHeadless ) : m_ok = true; // create the common settings shared by all applications. Not loaded immediately - m_common_settings = static_cast( RegisterSettings( new COMMON_SETTINGS, - false ) ); + m_common_settings = RegisterSettings( new COMMON_SETTINGS, false ); loadAllColorSettings(); } @@ -83,7 +82,7 @@ SETTINGS_MANAGER::~SETTINGS_MANAGER() } -JSON_SETTINGS* SETTINGS_MANAGER::RegisterSettings( JSON_SETTINGS* aSettings, bool aLoadNow ) +JSON_SETTINGS* SETTINGS_MANAGER::registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow ) { std::unique_ptr ptr( aSettings ); @@ -213,18 +212,17 @@ COLOR_SETTINGS* SETTINGS_MANAGER::loadColorSettingsByName( const wxString& aName return nullptr; } - auto cs = static_cast( RegisterSettings( new COLOR_SETTINGS( aName ) ) ); + COLOR_SETTINGS* settings = RegisterSettings( new COLOR_SETTINGS( aName ) ); - if( cs->GetFilename() != aName.ToStdString() ) + if( settings->GetFilename() != aName.ToStdString() ) { - // wxLogTrace is actually a macro so these braces are needed to keep Coverity from - // worrying about dangline else clauses.... - wxLogTrace( traceSettings, "Warning: stored filename is actually %s, ", cs->GetFilename() ); + wxLogTrace( traceSettings, "Warning: stored filename is actually %s, ", + settings->GetFilename() ); } - m_color_settings[aName] = cs; + m_color_settings[aName] = settings; - return cs; + return settings; } @@ -262,8 +260,8 @@ COLOR_SETTINGS* SETTINGS_MANAGER::registerColorSettings( const wxString& aName ) { if( !m_color_settings.count( aName ) ) { - auto cs = static_cast( RegisterSettings( new COLOR_SETTINGS( aName ) ) ); - m_color_settings[aName] = cs; + COLOR_SETTINGS* colorSettings = RegisterSettings( new COLOR_SETTINGS( aName ) ); + m_color_settings[aName] = colorSettings; } return m_color_settings.at( aName ); @@ -296,10 +294,7 @@ void SETTINGS_MANAGER::loadAllColorSettings() { // Create the built-in color settings for( COLOR_SETTINGS* settings : COLOR_SETTINGS::CreateBuiltinColorSettings() ) - { - m_color_settings[settings->GetFilename()] = - static_cast( RegisterSettings( settings, false ) ); - } + m_color_settings[settings->GetFilename()] = RegisterSettings( settings, false ); // Search for and load any other settings COLOR_SETTINGS_LOADER loader( [&]( const wxString& aFilename ) @@ -809,7 +804,7 @@ bool SETTINGS_MANAGER::LoadProject( const wxString& aFullPath, bool aSetActive ) PROJECT_LOCAL_SETTINGS* settings = new PROJECT_LOCAL_SETTINGS( m_projects[fullPath], fn ); if( aSetActive ) - settings = static_cast( RegisterSettings( settings ) ); + settings = RegisterSettings( settings ); m_projects[fullPath]->setLocalSettings( settings ); @@ -974,8 +969,7 @@ bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject ) wxFileName fullFn( aProject.GetProjectFullName() ); wxString fn( fullFn.GetName() ); - PROJECT_FILE* file = static_cast( RegisterSettings( new PROJECT_FILE( fn ), - false ) ); + PROJECT_FILE* file = RegisterSettings( new PROJECT_FILE( fn ), false ); m_project_files[aProject.GetProjectFullName()] = file; diff --git a/include/settings/settings_manager.h b/include/settings/settings_manager.h index 5c91546c7d..eb0780a290 100644 --- a/include/settings/settings_manager.h +++ b/include/settings/settings_manager.h @@ -59,7 +59,11 @@ public: * @param aSettings is a settings object to register * @return a handle to the owned pointer */ - JSON_SETTINGS* RegisterSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true ); + template + T* RegisterSettings( T* aSettings, bool aLoadNow = true ) + { + return static_cast( registerSettings( aSettings, aLoadNow ) ); + } void Load(); @@ -81,18 +85,18 @@ public: * If the settings have not been loaded, creates a new object owned by the * settings manager and returns a pointer to it. * - * @tparam AppSettings is a type derived from APP_SETTINGS_BASE + * @tparam T is a type derived from APP_SETTINGS_BASE * @param aLoadNow is true to load the registered file from disk immediately * @return a pointer to a loaded settings object */ - template - AppSettings* GetAppSettings( bool aLoadNow = true ) + template + T* GetAppSettings( bool aLoadNow = true ) { - AppSettings* ret = nullptr; - size_t typeHash = typeid( AppSettings ).hash_code(); + T* ret = nullptr; + size_t typeHash = typeid( T ).hash_code(); if( m_app_settings_cache.count( typeHash ) ) - ret = dynamic_cast( m_app_settings_cache.at( typeHash ) ); + ret = dynamic_cast( m_app_settings_cache.at( typeHash ) ); if( ret ) return ret; @@ -100,18 +104,18 @@ public: auto it = std::find_if( m_settings.begin(), m_settings.end(), []( const std::unique_ptr& aSettings ) { - return dynamic_cast( aSettings.get() ); + return dynamic_cast( aSettings.get() ); } ); if( it != m_settings.end() ) { - ret = dynamic_cast( it->get() ); + ret = dynamic_cast( it->get() ); } else { try { - ret = static_cast( RegisterSettings( new AppSettings, aLoadNow ) ); + ret = static_cast( RegisterSettings( new T, aLoadNow ) ); } catch( ... ) { @@ -342,6 +346,8 @@ public: private: + JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true ); + /** * Determines the base path for user settings files. *