Reduce type-casting a bit.

This commit is contained in:
Jeff Young 2021-08-04 16:44:12 +01:00
parent 76bdb423d7
commit d6443d5a2c
2 changed files with 29 additions and 29 deletions

View File

@ -69,8 +69,7 @@ SETTINGS_MANAGER::SETTINGS_MANAGER( bool aHeadless ) :
m_ok = true; m_ok = true;
// create the common settings shared by all applications. Not loaded immediately // create the common settings shared by all applications. Not loaded immediately
m_common_settings = static_cast<COMMON_SETTINGS*>( RegisterSettings( new COMMON_SETTINGS, m_common_settings = RegisterSettings( new COMMON_SETTINGS, false );
false ) );
loadAllColorSettings(); 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<JSON_SETTINGS> ptr( aSettings ); std::unique_ptr<JSON_SETTINGS> ptr( aSettings );
@ -213,18 +212,17 @@ COLOR_SETTINGS* SETTINGS_MANAGER::loadColorSettingsByName( const wxString& aName
return nullptr; return nullptr;
} }
auto cs = static_cast<COLOR_SETTINGS*>( 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 wxLogTrace( traceSettings, "Warning: stored filename is actually %s, ",
// worrying about dangline else clauses.... settings->GetFilename() );
wxLogTrace( traceSettings, "Warning: stored filename is actually %s, ", cs->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 ) ) if( !m_color_settings.count( aName ) )
{ {
auto cs = static_cast<COLOR_SETTINGS*>( RegisterSettings( new COLOR_SETTINGS( aName ) ) ); COLOR_SETTINGS* colorSettings = RegisterSettings( new COLOR_SETTINGS( aName ) );
m_color_settings[aName] = cs; m_color_settings[aName] = colorSettings;
} }
return m_color_settings.at( aName ); return m_color_settings.at( aName );
@ -296,10 +294,7 @@ void SETTINGS_MANAGER::loadAllColorSettings()
{ {
// Create the built-in color settings // Create the built-in color settings
for( COLOR_SETTINGS* settings : COLOR_SETTINGS::CreateBuiltinColorSettings() ) for( COLOR_SETTINGS* settings : COLOR_SETTINGS::CreateBuiltinColorSettings() )
{ m_color_settings[settings->GetFilename()] = RegisterSettings( settings, false );
m_color_settings[settings->GetFilename()] =
static_cast<COLOR_SETTINGS*>( RegisterSettings( settings, false ) );
}
// Search for and load any other settings // Search for and load any other settings
COLOR_SETTINGS_LOADER loader( [&]( const wxString& aFilename ) 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 ); PROJECT_LOCAL_SETTINGS* settings = new PROJECT_LOCAL_SETTINGS( m_projects[fullPath], fn );
if( aSetActive ) if( aSetActive )
settings = static_cast<PROJECT_LOCAL_SETTINGS*>( RegisterSettings( settings ) ); settings = RegisterSettings( settings );
m_projects[fullPath]->setLocalSettings( settings ); m_projects[fullPath]->setLocalSettings( settings );
@ -974,8 +969,7 @@ bool SETTINGS_MANAGER::loadProjectFile( PROJECT& aProject )
wxFileName fullFn( aProject.GetProjectFullName() ); wxFileName fullFn( aProject.GetProjectFullName() );
wxString fn( fullFn.GetName() ); wxString fn( fullFn.GetName() );
PROJECT_FILE* file = static_cast<PROJECT_FILE*>( RegisterSettings( new PROJECT_FILE( fn ), PROJECT_FILE* file = RegisterSettings( new PROJECT_FILE( fn ), false );
false ) );
m_project_files[aProject.GetProjectFullName()] = file; m_project_files[aProject.GetProjectFullName()] = file;

View File

@ -59,7 +59,11 @@ public:
* @param aSettings is a settings object to register * @param aSettings is a settings object to register
* @return a handle to the owned pointer * @return a handle to the owned pointer
*/ */
JSON_SETTINGS* RegisterSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true ); template<typename T>
T* RegisterSettings( T* aSettings, bool aLoadNow = true )
{
return static_cast<T*>( registerSettings( aSettings, aLoadNow ) );
}
void Load(); void Load();
@ -81,18 +85,18 @@ public:
* If the settings have not been loaded, creates a new object owned by the * If the settings have not been loaded, creates a new object owned by the
* settings manager and returns a pointer to it. * 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 * @param aLoadNow is true to load the registered file from disk immediately
* @return a pointer to a loaded settings object * @return a pointer to a loaded settings object
*/ */
template<typename AppSettings> template<typename T>
AppSettings* GetAppSettings( bool aLoadNow = true ) T* GetAppSettings( bool aLoadNow = true )
{ {
AppSettings* ret = nullptr; T* ret = nullptr;
size_t typeHash = typeid( AppSettings ).hash_code(); size_t typeHash = typeid( T ).hash_code();
if( m_app_settings_cache.count( typeHash ) ) if( m_app_settings_cache.count( typeHash ) )
ret = dynamic_cast<AppSettings*>( m_app_settings_cache.at( typeHash ) ); ret = dynamic_cast<T*>( m_app_settings_cache.at( typeHash ) );
if( ret ) if( ret )
return ret; return ret;
@ -100,18 +104,18 @@ public:
auto it = std::find_if( m_settings.begin(), m_settings.end(), auto it = std::find_if( m_settings.begin(), m_settings.end(),
[]( const std::unique_ptr<JSON_SETTINGS>& aSettings ) []( const std::unique_ptr<JSON_SETTINGS>& aSettings )
{ {
return dynamic_cast<AppSettings*>( aSettings.get() ); return dynamic_cast<T*>( aSettings.get() );
} ); } );
if( it != m_settings.end() ) if( it != m_settings.end() )
{ {
ret = dynamic_cast<AppSettings*>( it->get() ); ret = dynamic_cast<T*>( it->get() );
} }
else else
{ {
try try
{ {
ret = static_cast<AppSettings*>( RegisterSettings( new AppSettings, aLoadNow ) ); ret = static_cast<T*>( RegisterSettings( new T, aLoadNow ) );
} }
catch( ... ) catch( ... )
{ {
@ -342,6 +346,8 @@ public:
private: private:
JSON_SETTINGS* registerSettings( JSON_SETTINGS* aSettings, bool aLoadNow = true );
/** /**
* Determines the base path for user settings files. * Determines the base path for user settings files.
* *