commit 84306929aa2af31ad58b8e84c032f41729088367
parent b5664bfa4c620161144f961f26ea7adf96700449
Author: cfillion <cfillion@users.noreply.github.com>
Date: Tue, 4 Sep 2018 03:06:32 -0400
improve ReaPack member initialization code
Diffstat:
4 files changed, 35 insertions(+), 37 deletions(-)
diff --git a/src/config.cpp b/src/config.cpp
@@ -57,10 +57,16 @@ inline static string nKey(const char *key, const unsigned int i)
return key + to_string(i);
}
-Config::Config()
- : m_isFirstRun(false), m_version(0), m_remotesIniSize(0)
+Config::Config(const Path &path)
+ : m_path(path.join()), m_isFirstRun(false), m_version(0), m_remotesIniSize(0)
{
resetOptions();
+ read();
+}
+
+Config::~Config()
+{
+ write();
}
void Config::resetOptions()
@@ -130,10 +136,8 @@ void Config::migrate()
write();
}
-void Config::read(const Path &path)
+void Config::read()
{
- m_path = path.join();
-
install.autoInstall = getBool(INSTALL_GRP, AUTOINSTALL_KEY, install.autoInstall);
install.bleedingEdge = getBool(INSTALL_GRP, PRERELEASES_KEY, install.bleedingEdge);
install.promptObsolete = getBool(INSTALL_GRP, PROMPTOBSOLETE_KEY, install.promptObsolete);
diff --git a/src/config.hpp b/src/config.hpp
@@ -49,9 +49,11 @@ struct NetworkOpts {
class Config {
public:
- Config();
+ Config(const Path &);
+ Config(const Config &) = delete;
+ ~Config();
- void read(const Path &);
+ void read();
void write();
void resetOptions();
diff --git a/src/reapack.cpp b/src/reapack.cpp
@@ -79,23 +79,19 @@ Path ReaPack::resourcePath()
}
ReaPack::ReaPack(REAPER_PLUGIN_HINSTANCE instance)
- : m_tx(nullptr), m_progress(nullptr), m_browser(nullptr), m_manager(nullptr),
- m_about(nullptr), m_instance(instance), m_useRootPath(resourcePath())
+ : m_instance(instance), m_mainWindow(GetMainHwnd()),
+ m_useRootPath(resourcePath()), m_config(Path::CONFIG.prependRoot()),
+ m_tx{}, m_about{}, m_browser{}, m_manager{}, m_progress{}
{
assert(!s_instance);
s_instance = this;
- m_mainWindow = GetMainHwnd();
-
DownloadContext::GlobalInit();
RichEdit::Init();
createDirectories();
- m_config = new Config;
- m_config->read(Path::CONFIG.prependRoot());
-
- if(m_config->isFirstRun())
+ if(m_config.isFirstRun())
manageRemotes();
registerSelf();
@@ -108,10 +104,6 @@ ReaPack::ReaPack(REAPER_PLUGIN_HINSTANCE instance)
ReaPack::~ReaPack()
{
Dialog::DestroyAll();
-
- m_config->write();
- delete m_config;
-
DownloadContext::GlobalCleanup();
s_instance = nullptr;
@@ -119,7 +111,7 @@ ReaPack::~ReaPack()
void ReaPack::synchronizeAll()
{
- const vector<Remote> &remotes = m_config->remotes.getEnabled();
+ const vector<Remote> &remotes = m_config.remotes.getEnabled();
if(remotes.empty()) {
ShowMessageBox("No repository enabled, nothing to do!", "ReaPack", MB_OK);
@@ -139,8 +131,8 @@ void ReaPack::synchronizeAll()
void ReaPack::addSetRemote(const Remote &remote)
{
- if(remote.isEnabled() && remote.autoInstall(m_config->install.autoInstall)) {
- const Remote &previous = m_config->remotes.get(remote.name());
+ if(remote.isEnabled() && remote.autoInstall(m_config.install.autoInstall)) {
+ const Remote &previous = m_config.remotes.get(remote.name());
if(!previous || !previous.isEnabled() || previous.url() != remote.url()) {
if(Transaction *tx = setupTransaction())
@@ -148,7 +140,7 @@ void ReaPack::addSetRemote(const Remote &remote)
}
}
- m_config->remotes.add(remote);
+ m_config.remotes.add(remote);
}
void ReaPack::uninstall(const Remote &remote)
@@ -161,7 +153,7 @@ void ReaPack::uninstall(const Remote &remote)
m_tx->onFinish([=] {
if(!m_tx->isCancelled())
- m_config->remotes.remove(remote);
+ config()->remotes.remove(remote);
});
}
@@ -193,7 +185,7 @@ void ReaPack::manageRemotes()
Remote ReaPack::remote(const string &name) const
{
- return m_config->remotes.get(name);
+ return m_config.remotes.get(name);
}
void ReaPack::about(const Remote &repo, const bool focus)
@@ -296,7 +288,7 @@ Transaction *ReaPack::setupTransaction()
LockDialog progressLock(m_progress);
return Dialog::Show<ObsoleteQuery>(m_instance, m_mainWindow,
- &entries, &m_config->install.promptObsolete) == IDOK;
+ &entries, &config()->install.promptObsolete) == IDOK;
});
m_tx->setCleanupHandler(bind(&ReaPack::teardownTransaction, this));
@@ -324,7 +316,7 @@ void ReaPack::commitConfig(bool refresh)
m_tx->receipt()->setIndexChanged(); // force browser refresh
m_tx->onFinish(bind(&ReaPack::refreshManager, this));
}
- m_tx->onFinish(bind(&Config::write, m_config));
+ m_tx->onFinish(bind(&Config::write, &m_config));
m_tx->runTasks();
}
else {
@@ -332,7 +324,7 @@ void ReaPack::commitConfig(bool refresh)
refreshManager();
refreshBrowser();
}
- m_config->write();
+ m_config.write();
}
}
diff --git a/src/reapack.hpp b/src/reapack.hpp
@@ -20,6 +20,7 @@
#include "action.hpp"
#include "api.hpp"
+#include "config.hpp"
#include "path.hpp"
#include <list>
@@ -28,7 +29,6 @@
class About;
class Browser;
-class Config;
class Manager;
class Progress;
class Remote;
@@ -67,7 +67,7 @@ public:
Transaction *setupTransaction();
void commitConfig(bool refresh = true);
- Config *config() const { return m_config; }
+ Config *config() { return &m_config; }
private:
static ReaPack *s_instance;
@@ -76,19 +76,19 @@ private:
void registerSelf();
void teardownTransaction();
+ REAPER_PLUGIN_HINSTANCE m_instance;
+ HWND m_mainWindow;
+
+ UseRootPath m_useRootPath;
+ Config m_config;
ActionList m_actions;
std::list<APIDef> m_api;
- Config *m_config;
Transaction *m_tx;
- Progress *m_progress;
+ About *m_about;
Browser *m_browser;
Manager *m_manager;
- About *m_about;
-
- REAPER_PLUGIN_HINSTANCE m_instance;
- HWND m_mainWindow;
- UseRootPath m_useRootPath;
+ Progress *m_progress;
};
#endif