refactor: Use shared_ptr for Bar/Opts

This commit is contained in:
Michael Carlberg 2016-06-29 12:42:54 +02:00
parent f406f1eb9f
commit 80e6936cdc
7 changed files with 24 additions and 20 deletions

View file

@ -89,7 +89,7 @@ class Bar
public:
Bar();
std::unique_ptr<Options> opts;
std::shared_ptr<Options> opts;
std::shared_ptr<Registry> registry;
void load(std::shared_ptr<Registry> registry);
@ -98,6 +98,5 @@ class Bar
std::string get_exec_line();
};
std::shared_ptr<Bar> &get_bar();
const Options& bar_opts();
std::shared_ptr<Bar> get_bar();
std::shared_ptr<Options> bar_opts();

View file

@ -1,5 +1,6 @@
#pragma once
#include "exception.hpp"
#include "modules/base.hpp"
DefineBaseException(RegistryError);

View file

@ -3,6 +3,7 @@
#include <string>
#include <memory>
#include "bar.hpp"
#include "drawtypes/animation.hpp"
#include "drawtypes/bar.hpp"
#include "drawtypes/icon.hpp"
@ -41,8 +42,12 @@ class Builder
void align_center();
void align_right();
std::shared_ptr<Options> opts;
public:
explicit Builder(bool lazy_closing = true) : lazy_closing(lazy_closing){}
explicit Builder(bool lazy_closing = true)
: lazy_closing(lazy_closing)
, opts(bar_opts()) {}
void set_lazy_closing(bool mode) { this->lazy_closing = mode; }

View file

@ -1,3 +1,3 @@
#pragma once
#define GIT_TAG "1.4.1-3-g6329b56-dev"
#define GIT_TAG "1.4.1-5-gf406f1e-dev"

View file

@ -28,21 +28,21 @@
#endif
std::shared_ptr<Bar> bar;
std::shared_ptr<Bar> &get_bar()
std::shared_ptr<Bar> get_bar()
{
if (bar == nullptr)
bar = std::make_shared<Bar>();
return bar;
}
const Options& bar_opts() {
return *bar->opts.get();
std::shared_ptr<Options> bar_opts() {
return bar->opts;
}
/**
* Bar constructor
*/
Bar::Bar() : config_path(config::get_bar_path()), opts(std::make_unique<Options>())
Bar::Bar() : config_path(config::get_bar_path()), opts(std::make_shared<Options>())
{
struct Options defaults;

View file

@ -26,7 +26,7 @@ bool DateModule::update()
auto date_format = this->detailed ? this->date_detailed : this->date;
auto time = std::time(nullptr);
char new_str[256] = {0,};
std::strftime(new_str, sizeof(this->date_str), date_format.c_str(), std::localtime(&time));
std::strftime(new_str, sizeof(new_str), date_format.c_str(), std::localtime(&time));
if (std::strncmp(new_str, this->date_str, sizeof(new_str)) == 0)
return false;
@ -40,7 +40,7 @@ std::string DateModule::get_output()
{
if (!this->date_detailed.empty())
this->builder->cmd(Cmd::LEFT_CLICK, EVENT_TOGGLE);
this->builder->node(this->Module::get_output());
this->builder->node(this->TimerModule::get_output());
return this->builder->flush();
}

View file

@ -6,7 +6,6 @@
#include "config.hpp"
#include "exception.hpp"
#include "bar.hpp"
#include "services/builder.hpp"
#include "utils/string.hpp"
#include "utils/math.hpp"
@ -94,12 +93,12 @@ void Builder::append_module_output(Alignment alignment, std::string module_outpu
int margin;
if (margin_left && (margin= bar_opts().module_margin_left) > 0)
if (margin_left && (margin= this->opts->module_margin_left) > 0)
this->output += std::string(margin, ' ');
this->append(module_output);
if (margin_right && (margin = bar_opts().module_margin_right) > 0)
if (margin_right && (margin = this->opts->module_margin_right) > 0)
this->output += std::string(margin, ' ');
}
@ -272,7 +271,7 @@ void Builder::offset(int pixels)
void Builder::space(int width)
{
if (width == DEFAULT_SPACING) width = bar_opts().spacing;
if (width == DEFAULT_SPACING) width = this->opts->spacing;
if (width <= 0) return;
std::string str(width, ' ');
this->append(str);
@ -280,7 +279,7 @@ void Builder::space(int width)
void Builder::remove_trailing_space(int width)
{
if (width == DEFAULT_SPACING) width = bar_opts().spacing;
if (width == DEFAULT_SPACING) width = this->opts->spacing;
if (width <= 0) return;
std::string::size_type spacing = width;
std::string str(spacing, ' ');
@ -324,7 +323,7 @@ void Builder::background(std::string color_)
if (color.length() == 2 || (color.find("#") == 0 && color.length() == 3)) {
color = "#"+ color.substr(color.length()-2);
auto bg = bar_opts().background;
auto bg = this->opts->background;
color += bg.substr(bg.length()-(bg.length() < 6 ? 3 : 6));
} else if (color.length() >= 7 && color == "#"+ std::string(color.length()-1, color[1])) {
color = color.substr(0, 4);
@ -353,7 +352,7 @@ void Builder::color(std::string color_)
auto color(color_);
if (color.length() == 2 || (color.find("#") == 0 && color.length() == 3)) {
color = "#"+ color.substr(color.length()-2);
auto bg = bar_opts().foreground;
auto bg = this->opts->foreground;
color += bg.substr(bg.length()-(bg.length() < 6 ? 3 : 6));
} else if (color.length() >= 7 && color == "#"+ std::string(color.length()-1, color[1])) {
color = color.substr(0, 4);
@ -371,7 +370,7 @@ void Builder::color(std::string color_)
void Builder::color_alpha(std::string alpha_)
{
auto alpha(alpha_);
std::string val = bar_opts().foreground;
std::string val = this->opts->foreground;
if (val.size() < 6 && val.size() > 2) {
val.append(val.substr(val.size() - 3));
} else if (val.length() > 6) {