From 540bcb9fd03e161f50c3198e170ba264be9cbe54 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Fri, 9 Dec 2016 11:32:41 +0100 Subject: [PATCH] refactor(config): Load on instantiation --- include/components/config.hpp | 4 ++-- src/components/config.cpp | 16 +++++++++++++--- src/main.cpp | 15 +++++++++------ 3 files changed, 24 insertions(+), 11 deletions(-) diff --git a/include/components/config.hpp b/include/components/config.hpp index 10ca9a4d..0f81d0c9 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -27,9 +27,9 @@ class config { using sectionmap_t = std::unordered_map; 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); string filepath() const; diff --git a/src/components/config.cpp b/src/components/config.cpp index edff42a7..b412bcb2 100644 --- a/src/components/config.cpp +++ b/src/components/config.cpp @@ -13,9 +13,19 @@ POLYBAR_NS /** * Create instance */ -config::make_type config::make() { - auto instance = factory_util::singleton(logger::make(), xresource_manager::make()); - return static_cast(*instance); +config::make_type config::make(string path, string bar) { + return static_cast( + *factory_util::singleton(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(path), forward(bar)); + } } /** diff --git a/src/main.cpp b/src/main.cpp index 04133523..0a7af4b9 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -68,9 +68,10 @@ int main(int argc, char** argv) { // Parse command line arguments //================================================== string scriptname{argv[0]}; - vector args(argv + 1, argv + argc); + vector args{argv + 1, argv + argc}; + + cliparser::make_type cli{cliparser::make(move(scriptname), opts)}; - unique_ptr cli{command_line::parser::make(scriptname, opts)}; cli->process_input(args); if (cli->has("quiet")) { @@ -93,18 +94,20 @@ int main(int argc, char** argv) { //================================================== // Load user configuration //================================================== - config& conf{const_cast(config::make())}; + string confpath; if (cli->has("config")) { - conf.load(cli->get("config"), args[0]); + confpath = cli->get("config"); } 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")) { - conf.load(env_util::get("HOME") + "/.config/polybar/config", args[0]); + confpath = env_util::get("HOME") + "/.config/polybar/config"; } else { throw application_error("Define configuration using --config=PATH"); } + config::make_type conf{config::make(move(confpath), args[0])}; + //================================================== // Dump requested data //==================================================