App: refactor to use unique_ptr
Also address other reviewer comments.
This commit is contained in:
committed by
Chris Hennes
parent
c1805413e0
commit
fa98beb221
@@ -186,7 +186,7 @@ Base::ConsoleObserverStd *Application::_pConsoleObserverStd = nullptr;
|
||||
Base::ConsoleObserverFile *Application::_pConsoleObserverFile = nullptr;
|
||||
|
||||
AppExport std::map<std::string, std::string> Application::mConfig;
|
||||
std::shared_ptr<ApplicationDirectories> Application::_appDirs;
|
||||
std::unique_ptr<ApplicationDirectories> Application::_appDirs;
|
||||
|
||||
|
||||
//**************************************************************************
|
||||
@@ -1142,7 +1142,7 @@ bool Application::isDevelopmentVersion()
|
||||
return suffix == "dev";
|
||||
}
|
||||
|
||||
std::shared_ptr<ApplicationDirectories> Application::directories() {
|
||||
const std::unique_ptr<ApplicationDirectories>& Application::directories() {
|
||||
return _appDirs;
|
||||
}
|
||||
|
||||
@@ -1153,10 +1153,7 @@ std::string Application::getTempPath()
|
||||
|
||||
std::string Application::getTempFileName(const char* FileName)
|
||||
{
|
||||
if (FileName) {
|
||||
return Base::FileInfo::pathToString(_appDirs->getTempFileName(FileName));
|
||||
}
|
||||
return Base::FileInfo::pathToString(_appDirs->getTempFileName(std::string()));
|
||||
return Base::FileInfo::pathToString(_appDirs->getTempFileName(FileName ? FileName : std::string()));
|
||||
}
|
||||
|
||||
std::string Application::getUserCachePath()
|
||||
@@ -2573,7 +2570,7 @@ void Application::initConfig(int argc, char ** argv)
|
||||
}
|
||||
|
||||
// extract home paths
|
||||
_appDirs = std::make_shared<ApplicationDirectories>(mConfig);
|
||||
_appDirs = std::make_unique<ApplicationDirectories>(mConfig);
|
||||
|
||||
if (vm.contains("safe-mode")) {
|
||||
SafeMode::StartSafeMode();
|
||||
|
||||
@@ -426,7 +426,7 @@ public:
|
||||
static bool isDevelopmentVersion();
|
||||
|
||||
/// Access to the various directories for the program a replacement for the get*Path methods below
|
||||
static std::shared_ptr<ApplicationDirectories> directories();
|
||||
static const std::unique_ptr<ApplicationDirectories>& directories();
|
||||
|
||||
/*!
|
||||
Returns the temporary directory. By default, this is set to the
|
||||
@@ -632,7 +632,7 @@ private:
|
||||
/// startup configuration container
|
||||
static std::map<std::string,std::string> mConfig;
|
||||
/// Management of and access to applications directories
|
||||
static std::shared_ptr<ApplicationDirectories> _appDirs;
|
||||
static std::unique_ptr<ApplicationDirectories> _appDirs;
|
||||
static int _argc;
|
||||
static char ** _argv;
|
||||
//@}
|
||||
|
||||
@@ -56,13 +56,7 @@ fs::path qstringToPath(const QString& path)
|
||||
|
||||
ApplicationDirectories::ApplicationDirectories(std::map<std::string,std::string> &config)
|
||||
{
|
||||
try {
|
||||
int major = std::stoi(config.at("BuildVersionMajor"));
|
||||
int minor = std::stoi(config.at("BuildVersionMinor"));
|
||||
_currentVersion = std::make_tuple(major, minor);
|
||||
} catch (const std::exception& e) {
|
||||
throw Base::RuntimeError("Failed to parse version from config: " + std::string(e.what()));
|
||||
}
|
||||
_currentVersion = extractVersionFromConfigMap(config);
|
||||
configurePaths(config);
|
||||
configureResourceDirectory(config);
|
||||
configureLibraryDirectory(config);
|
||||
@@ -724,5 +718,16 @@ fs::path ApplicationDirectories::findHomePath(const char* sCall)
|
||||
}
|
||||
|
||||
#else
|
||||
# error "std::string ApplicationDirectories::FindHomePath(const char*) not implemented"
|
||||
# error "std::string ApplicationDirectories::findHomePath(const char*) not implemented"
|
||||
#endif
|
||||
|
||||
std::tuple<int, int> ApplicationDirectories::extractVersionFromConfigMap(const std::map<std::string,std::string> &config)
|
||||
{
|
||||
try {
|
||||
int major = std::stoi(config.at("BuildVersionMajor"));
|
||||
int minor = std::stoi(config.at("BuildVersionMinor"));
|
||||
return std::make_tuple(major, minor);
|
||||
} catch (const std::exception& e) {
|
||||
throw Base::RuntimeError("Failed to parse version from config: " + std::string(e.what()));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,7 +193,6 @@ namespace App {
|
||||
void configureHelpDirectory(const std::map<std::string,std::string>& mConfig);
|
||||
|
||||
/*!
|
||||
* \brief getCustomPaths
|
||||
* Returns a tuple of path names where to store config, data, and temp. files.
|
||||
* The method therefore reads the environment variables:
|
||||
* \list
|
||||
@@ -205,7 +204,6 @@ namespace App {
|
||||
static std::tuple<std::filesystem::path, std::filesystem::path, std::filesystem::path> getCustomPaths();
|
||||
|
||||
/*!
|
||||
* \brief getStandardPaths
|
||||
* Returns a tuple of XDG-compliant standard paths names where to store config, data and cached files.
|
||||
* The method therefore reads the environment variables:
|
||||
* \list
|
||||
@@ -216,6 +214,12 @@ namespace App {
|
||||
*/
|
||||
std::tuple<std::filesystem::path, std::filesystem::path, std::filesystem::path, std::filesystem::path> getStandardPaths();
|
||||
|
||||
/// Find the BuildVersionMajor, BuildVersionMinor pair in the config map, convert them to an int tuple, and
|
||||
/// return it. If the pair is not found, or cannot be converted to integers, a RuntimeError is raised.
|
||||
/// \param config The config map to search.
|
||||
/// \return The version tuple.
|
||||
static std::tuple<int, int> extractVersionFromConfigMap(const std::map<std::string,std::string> &config);
|
||||
|
||||
private:
|
||||
std::tuple<int, int> _currentVersion;
|
||||
std::filesystem::path _home;
|
||||
|
||||
Reference in New Issue
Block a user