refactor(config): Load on instantiation

This commit is contained in:
Michael Carlberg 2016-12-09 11:32:41 +01:00
parent 4fca0c89b4
commit 540bcb9fd0
3 changed files with 24 additions and 11 deletions

View File

@ -27,9 +27,9 @@ class config {
using sectionmap_t = std::unordered_map<string, valuemap_t>; using sectionmap_t = std::unordered_map<string, valuemap_t>;
using make_type = const config&; using make_type = const config&;
static make_type make(); static make_type make(string path = "", string bar = "");
explicit config(const logger& logger, const xresource_manager& xrm) : m_logger(logger), m_xrm(xrm) {} explicit config(const logger& logger, const xresource_manager& xrm, string&& path = "", string&& bar = "");
void load(string file, string barname); void load(string file, string barname);
string filepath() const; string filepath() const;

View File

@ -13,9 +13,19 @@ POLYBAR_NS
/** /**
* Create instance * Create instance
*/ */
config::make_type config::make() { config::make_type config::make(string path, string bar) {
auto instance = factory_util::singleton<const config>(logger::make(), xresource_manager::make()); return static_cast<const config&>(
return static_cast<const config&>(*instance); *factory_util::singleton<const config>(logger::make(), xresource_manager::make(), move(path), move(bar)));
}
/**
* Construct config object
*/
config::config(const logger& logger, const xresource_manager& xrm, string&& path, string&& bar)
: m_logger(logger), m_xrm(xrm) {
if (!path.empty() && !bar.empty()) {
load(forward<decltype(path)>(path), forward<decltype(bar)>(bar));
}
} }
/** /**

View File

@ -68,9 +68,10 @@ int main(int argc, char** argv) {
// Parse command line arguments // Parse command line arguments
//================================================== //==================================================
string scriptname{argv[0]}; string scriptname{argv[0]};
vector<string> args(argv + 1, argv + argc); vector<string> args{argv + 1, argv + argc};
cliparser::make_type cli{cliparser::make(move(scriptname), opts)};
unique_ptr<cliparser> cli{command_line::parser::make(scriptname, opts)};
cli->process_input(args); cli->process_input(args);
if (cli->has("quiet")) { if (cli->has("quiet")) {
@ -93,18 +94,20 @@ int main(int argc, char** argv) {
//================================================== //==================================================
// Load user configuration // Load user configuration
//================================================== //==================================================
config& conf{const_cast<config&>(config::make())}; string confpath;
if (cli->has("config")) { if (cli->has("config")) {
conf.load(cli->get("config"), args[0]); confpath = cli->get("config");
} else if (env_util::has("XDG_CONFIG_HOME")) { } else if (env_util::has("XDG_CONFIG_HOME")) {
conf.load(env_util::get("XDG_CONFIG_HOME") + "/polybar/config", args[0]); confpath = env_util::get("XDG_CONFIG_HOME") + "/polybar/config";
} else if (env_util::has("HOME")) { } else if (env_util::has("HOME")) {
conf.load(env_util::get("HOME") + "/.config/polybar/config", args[0]); confpath = env_util::get("HOME") + "/.config/polybar/config";
} else { } else {
throw application_error("Define configuration using --config=PATH"); throw application_error("Define configuration using --config=PATH");
} }
config::make_type conf{config::make(move(confpath), args[0])};
//================================================== //==================================================
// Dump requested data // Dump requested data
//================================================== //==================================================