refactor(config): Defer xrm initialization

This commit is contained in:
Michael Carlberg 2017-01-01 21:12:38 +01:00
parent ce7faa9fe7
commit 6341c7ab25
4 changed files with 12 additions and 17 deletions

View File

@ -22,7 +22,7 @@ class config {
using make_type = const config&;
static make_type make(string path = "", string bar = "");
explicit config(const logger& logger, const xresource_manager& xrm, string&& path = "", string&& bar = "");
explicit config(const logger& logger, string&& path = "", string&& bar = "");
string filepath() const;
string section() const;
@ -263,13 +263,14 @@ class config {
*/
template <typename T>
T dereference_xrdb(string var, const T& fallback) const {
const auto& xrm = xresource_manager::make();
size_t pos;
if ((pos = var.find(":")) != string::npos) {
return convert<T>(m_xrm.get_string(var.substr(0, pos), var.substr(pos + 1)));
return convert<T>(xrm->get_string(var.substr(0, pos), var.substr(pos + 1)));
}
string str{m_xrm.get_string(var, "")};
string str{xrm->get_string(var, "")};
return str.empty() ? fallback : convert<T>(move(str));
}
@ -292,7 +293,6 @@ class config {
static constexpr const char* KEY_INHERIT{"inherit"};
const logger& m_log;
const xresource_manager& m_xrm;
string m_file;
string m_barname;
sectionmap_t m_sections{};

View File

@ -9,7 +9,7 @@ POLYBAR_NS
class xresource_manager {
public:
using make_type = const xresource_manager&;
using make_type = unique_ptr<xresource_manager>;
static make_type make();
explicit xresource_manager(Display*);
@ -26,7 +26,6 @@ class xresource_manager {
string load_value(const string& key, const string& res_type, size_t n) const;
private:
Display* m_display{nullptr};
XrmDatabase m_db;
char* m_manager{nullptr};
};

View File

@ -19,15 +19,15 @@ namespace chrono = std::chrono;
* Create instance
*/
config::make_type config::make(string path, string bar) {
return static_cast<config::make_type>(*factory_util::singleton<std::remove_reference_t<config::make_type>>(
logger::make(), xresource_manager::make(), move(path), move(bar)));
return static_cast<config::make_type>(
*factory_util::singleton<std::remove_reference_t<config::make_type>>(logger::make(), move(path), move(bar)));
}
/**
* Construct config object
*/
config::config(const logger& logger, const xresource_manager& xrm, string&& path, string&& bar)
: m_log(logger), m_xrm(xrm), m_file(forward<string>(path)), m_barname(forward<string>(bar)) {
config::config(const logger& logger, string&& path, string&& bar)
: m_log(logger), m_file(forward<string>(path)), m_barname(forward<string>(bar)) {
if (!file_util::exists(m_file)) {
throw application_error("Could not find config file: " + m_file);
}

View File

@ -12,17 +12,16 @@ POLYBAR_NS
* Create instance
*/
xresource_manager::make_type xresource_manager::make() {
return static_cast<xresource_manager::make_type>(
*factory_util::singleton<std::remove_reference_t<xresource_manager::make_type>>(connection::make()));
return factory_util::unique<xresource_manager>(static_cast<Display*>(connection::make()));
}
/**
* Construct manager instance
*/
xresource_manager::xresource_manager(Display* dsp) : m_display(forward<decltype(dsp)>(dsp)) {
xresource_manager::xresource_manager(Display* dsp) {
XrmInitialize();
if ((m_manager = XResourceManagerString(m_display)) != nullptr) {
if ((m_manager = XResourceManagerString(dsp)) != nullptr) {
m_db = XrmGetStringDatabase(m_manager);
}
}
@ -31,9 +30,6 @@ xresource_manager::xresource_manager(Display* dsp) : m_display(forward<decltype(
* Deconstruct instance
*/
xresource_manager::~xresource_manager() {
if (m_manager != nullptr) {
XFree(m_manager);
}
if (m_db != nullptr) {
XrmDestroyDatabase(m_db);
}