1
0
mirror of https://github.com/cookiengineer/audacity synced 2025-08-16 08:34:10 +02:00

Hide two-phase construction of AudacityFileConfig in factory function

This commit is contained in:
Paul Licameli 2021-01-31 10:45:38 -05:00
parent 2c5c0f9dfd
commit c853450ce0
6 changed files with 67 additions and 11 deletions

@ -189,11 +189,10 @@ void PopulatePreferences()
{ {
const wxString fullPath{fn.GetFullPath()}; const wxString fullPath{fn.GetFullPath()};
AudacityFileConfig ini(wxEmptyString, auto pIni =
wxEmptyString, AudacityFileConfig::Create({}, {}, fullPath, {},
fullPath, wxCONFIG_USE_LOCAL_FILE);
wxEmptyString, auto &ini = *pIni;
wxCONFIG_USE_LOCAL_FILE);
wxString lang; wxString lang;
if (ini.Read(wxT("/FromInno/Language"), &lang)) if (ini.Read(wxT("/FromInno/Language"), &lang))
@ -1249,7 +1248,7 @@ bool AudacityApp::OnInit()
{ {
wxFileName configFileName(FileNames::DataDir(), wxT("audacity.cfg")); wxFileName configFileName(FileNames::DataDir(), wxT("audacity.cfg"));
auto appName = wxTheApp->GetAppName(); auto appName = wxTheApp->GetAppName();
InitPreferences( std::make_unique<AudacityFileConfig>( InitPreferences( AudacityFileConfig::Create(
appName, wxEmptyString, appName, wxEmptyString,
configFileName.GetFullPath(), configFileName.GetFullPath(),
wxEmptyString, wxCONFIG_USE_LOCAL_FILE) ); wxEmptyString, wxCONFIG_USE_LOCAL_FILE) );

@ -8,6 +8,35 @@ Paul Licameli split from Prefs.cpp
**********************************************************************/ **********************************************************************/
#include "Audacity.h"
#include "AudacityFileConfig.h" #include "AudacityFileConfig.h"
AudacityFileConfig::AudacityFileConfig(
const wxString& appName,
const wxString& vendorName,
const wxString& localFilename,
const wxString& globalFilename,
long style,
const wxMBConv& conv
)
: FileConfig{ appName, vendorName, localFilename, globalFilename, style, conv }
{}
AudacityFileConfig::~AudacityFileConfig() = default; AudacityFileConfig::~AudacityFileConfig() = default;
std::unique_ptr<AudacityFileConfig> AudacityFileConfig::Create(
const wxString& appName,
const wxString& vendorName,
const wxString& localFilename,
const wxString& globalFilename,
long style,
const wxMBConv& conv
)
{
// Private ctor means make_unique can't compile, so this verbosity:
auto result = std::unique_ptr<AudacityFileConfig>{
safenew AudacityFileConfig{
appName, vendorName, localFilename, globalFilename, style, conv } };
result->Init();
return result;
}

@ -12,14 +12,34 @@ Paul Licameli split from Prefs.h
#ifndef __AUDACITY_FILE_CONFIG__ #ifndef __AUDACITY_FILE_CONFIG__
#define __AUDACITY_FILE_CONFIG__ #define __AUDACITY_FILE_CONFIG__
#include <memory>
#include "widgets/FileConfig.h" // to inherit #include "widgets/FileConfig.h" // to inherit
/// \brief Our own specialisation of FileConfig. It is essentially a renaming. /// \brief Our own specialisation of FileConfig.
class AUDACITY_DLL_API AudacityFileConfig final : public FileConfig class AUDACITY_DLL_API AudacityFileConfig final : public FileConfig
{ {
public: public:
using FileConfig::FileConfig; //! Require a call to this factory, to guarantee proper two-phase initialization
static std::unique_ptr<AudacityFileConfig> Create(
const wxString& appName = {},
const wxString& vendorName = {},
const wxString& localFilename = {},
const wxString& globalFilename = {},
long style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE,
const wxMBConv& conv = wxConvAuto()
);
~AudacityFileConfig() override; ~AudacityFileConfig() override;
private:
//! Disallow direct constructor call, because a two-phase initialization is required
AudacityFileConfig(
const wxString& appName,
const wxString& vendorName,
const wxString& localFilename,
const wxString& globalFilename,
long style,
const wxMBConv& conv
);
}; };
#endif #endif

@ -1930,8 +1930,9 @@ bool PluginManager::DropFile(const wxString &fileName)
void PluginManager::Load() void PluginManager::Load()
{ {
// Create/Open the registry // Create/Open the registry
AudacityFileConfig registry( auto pRegistry = AudacityFileConfig::Create(
{}, {}, FileNames::PluginRegistry()); {}, {}, FileNames::PluginRegistry());
auto &registry = *pRegistry;
// If this group doesn't exist then we have something that's not a registry. // If this group doesn't exist then we have something that's not a registry.
// We should probably warn the user, but it's pretty unlikely that this will happen. // We should probably warn the user, but it's pretty unlikely that this will happen.
@ -2272,8 +2273,9 @@ void PluginManager::LoadGroup(FileConfig *pRegistry, PluginType type)
void PluginManager::Save() void PluginManager::Save()
{ {
// Create/Open the registry // Create/Open the registry
AudacityFileConfig registry( auto pRegistry = AudacityFileConfig::Create(
{}, {}, FileNames::PluginRegistry()); {}, {}, FileNames::PluginRegistry());
auto &registry = *pRegistry;
// Clear it out // Clear it out
registry.DeleteAll(); registry.DeleteAll();
@ -2780,7 +2782,8 @@ FileConfig *PluginManager::GetSettings()
{ {
if (!mSettings) if (!mSettings)
{ {
mSettings = std::make_unique<AudacityFileConfig>(wxEmptyString, wxEmptyString, FileNames::PluginSettings()); mSettings =
AudacityFileConfig::Create({}, {}, FileNames::PluginSettings());
// Check for a settings version that we can understand // Check for a settings version that we can understand
if (mSettings->HasEntry(SETVERKEY)) if (mSettings->HasEntry(SETVERKEY))

@ -45,6 +45,10 @@ FileConfig::FileConfig(const wxString& appName,
: wxFileConfig(appName, vendorName, localFilename, globalFilename, style, conv), : wxFileConfig(appName, vendorName, localFilename, globalFilename, style, conv),
mConfigPath(localFilename), mConfigPath(localFilename),
mDirty(false) mDirty(false)
{
}
void FileConfig::Init()
{ {
// Prevent wxFileConfig from attempting a Flush() during object deletion. This happens // Prevent wxFileConfig from attempting a Flush() during object deletion. This happens
// because we don't use the wxFileConfig::Flush() method and so the wxFileConfig dirty // because we don't use the wxFileConfig::Flush() method and so the wxFileConfig dirty

@ -25,6 +25,7 @@ public:
const wxString& globalFilename = wxEmptyString, const wxString& globalFilename = wxEmptyString,
long style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE, long style = wxCONFIG_USE_LOCAL_FILE | wxCONFIG_USE_GLOBAL_FILE,
const wxMBConv& conv = wxConvAuto()); const wxMBConv& conv = wxConvAuto());
void Init();
virtual ~FileConfig(); virtual ~FileConfig();
virtual bool Flush(bool bCurrentOnly = false) wxOVERRIDE; virtual bool Flush(bool bCurrentOnly = false) wxOVERRIDE;