Remove factory_util unique and shared

Equivalent to std::make_unique and std::make_shared
This commit is contained in:
patrick96 2021-09-21 21:15:49 +02:00 committed by Patrick Ziegler
parent 560065540b
commit 0d1db206c6
31 changed files with 83 additions and 99 deletions

View File

@ -77,7 +77,7 @@ class command<output_policy::IGNORED> {
string m_cmd;
pid_t m_forkpid{};
int m_forkstatus = - 1;
int m_forkstatus = -1;
};
template <>
@ -114,7 +114,7 @@ class command<output_policy::REDIRECTED> : private command<output_policy::IGNORE
namespace command_util {
template <output_policy OutputType, typename... Args>
unique_ptr<command<OutputType>> make_command(Args&&... args) {
return factory_util::unique<command<OutputType>>(logger::make(), forward<Args>(args)...);
return std::make_unique<command<OutputType>>(logger::make(), forward<Args>(args)...);
}
} // namespace command_util

View File

@ -12,34 +12,15 @@ namespace factory_util {
template <typename T>
void operator()(T*) const {}
};
struct fd_deleter {
void operator()(int* fd) const {
if (fd != nullptr && *fd > 0) {
close(*fd);
}
}
};
}
} // namespace detail
extern detail::null_deleter null_deleter;
extern detail::fd_deleter fd_deleter;
template <typename T, typename... Deps>
unique_ptr<T> unique(Deps&&... deps) {
return make_unique<T>(forward<Deps>(deps)...);
}
template <typename T, typename... Deps>
shared_ptr<T> shared(Deps&&... deps) {
return make_shared<T>(forward<Deps>(deps)...);
}
template <class T, class... Deps>
shared_ptr<T> singleton(Deps&&... deps) {
static shared_ptr<T> instance{make_shared<T>(forward<Deps>(deps)...)};
return instance;
}
}
} // namespace factory_util
POLYBAR_NS_END

View File

@ -114,7 +114,7 @@ namespace file_util {
template <typename... Args>
decltype(auto) make_file_descriptor(Args&&... args) {
return factory_util::unique<file_descriptor>(forward<Args>(args)...);
return std::make_unique<file_descriptor>(forward<Args>(args)...);
}
} // namespace file_util

View File

@ -23,7 +23,7 @@ class http_downloader {
namespace http_util {
template <typename... Args>
decltype(auto) make_downloader(Args&&... args) {
return factory_util::unique<http_downloader>(forward<Args>(args)...);
return std::make_unique<http_downloader>(forward<Args>(args)...);
}
} // namespace http_util

View File

@ -2,6 +2,7 @@
#include <poll.h>
#include <sys/inotify.h>
#include <cstdio>
#include "common.hpp"
@ -40,8 +41,8 @@ class inotify_watch {
namespace inotify_util {
template <typename... Args>
decltype(auto) make_watch(Args&&... args) {
return factory_util::unique<inotify_watch>(forward<Args>(args)...);
return std::make_unique<inotify_watch>(forward<Args>(args)...);
}
}
} // namespace inotify_util
POLYBAR_NS_END

View File

@ -3,7 +3,6 @@
// TODO: move to functional.hpp
#include "common.hpp"
#include "components/logger.hpp"
#include "utils/factory.hpp"
@ -37,8 +36,8 @@ namespace scope_util {
*/
template <typename Fn = function<void()>, typename... Args>
decltype(auto) make_exit_handler(Fn&& fn, Args&&... args) {
return factory_util::unique<on_exit<Args...>>(forward<Fn>(fn), forward<Args>(args)...);
return std::make_unique<on_exit<Args...>>(forward<Fn>(fn), forward<Args>(args)...);
}
}
} // namespace scope_util
POLYBAR_NS_END

View File

@ -41,8 +41,8 @@ namespace socket_util {
* \endcode
*/
inline unique_ptr<unix_connection> make_unix_connection(string&& path) {
return factory_util::unique<unix_connection>(forward<string>(path));
return std::make_unique<unix_connection>(forward<string>(path));
}
}
} // namespace socket_util
POLYBAR_NS_END

View File

@ -42,7 +42,7 @@ bar::make_type bar::make(bool only_initialize_values) {
auto action_ctxt = make_unique<tags::action_context>();
// clang-format off
return factory_util::unique<bar>(
return std::make_unique<bar>(
connection::make(),
signal_emitter::make(),
config::make(),

View File

@ -1,6 +1,7 @@
#include "components/command_line.hpp"
#include <algorithm>
#include "components/command_line.hpp"
#include "utils/factory.hpp"
POLYBAR_NS
@ -10,7 +11,7 @@ namespace command_line {
* Create instance
*/
parser::make_type parser::make(string&& scriptname, const options&& opts) {
return factory_util::unique<parser>("Usage: " + scriptname + " [OPTION]... BAR", forward<decltype(opts)>(opts));
return std::make_unique<parser>("Usage: " + scriptname + " [OPTION]... BAR", forward<decltype(opts)>(opts));
}
/**
@ -199,6 +200,6 @@ namespace command_line {
throw argument_error("Unrecognized option " + input);
}
}
}
} // namespace command_line
POLYBAR_NS_END

View File

@ -32,7 +32,7 @@ POLYBAR_NS
* Build controller instance
*/
controller::make_type controller::make(unique_ptr<ipc>&& ipc) {
return factory_util::unique<controller>(connection::make(), signal_emitter::make(), logger::make(), config::make(),
return std::make_unique<controller>(connection::make(), signal_emitter::make(), logger::make(), config::make(),
bar::make(), forward<decltype(ipc)>(ipc));
}

View File

@ -24,7 +24,7 @@ static constexpr const char* ipc_action_prefix{"action:"};
* Create instance
*/
ipc::make_type ipc::make() {
return factory_util::unique<ipc>(signal_emitter::make(), logger::make());
return std::make_unique<ipc>(signal_emitter::make(), logger::make());
}
/**

View File

@ -23,7 +23,7 @@ static constexpr double BLOCK_GAP{20.0};
*/
renderer::make_type renderer::make(const bar_settings& bar, tags::action_context& action_ctxt) {
// clang-format off
return factory_util::unique<renderer>(
return std::make_unique<renderer>(
connection::make(),
signal_emitter::make(),
config::make(),

View File

@ -23,7 +23,7 @@ using namespace signals::eventqueue;
* Create instance
*/
screen::make_type screen::make() {
return factory_util::unique<screen>(connection::make(), signal_emitter::make(), logger::make(), config::make());
return std::make_unique<screen>(connection::make(), signal_emitter::make(), logger::make(), config::make());
}
/**

View File

@ -1,12 +1,13 @@
#include "components/taskqueue.hpp"
#include <algorithm>
#include "components/taskqueue.hpp"
#include "utils/factory.hpp"
POLYBAR_NS
taskqueue::make_type taskqueue::make() {
return factory_util::unique<taskqueue>();
return std::make_unique<taskqueue>();
}
taskqueue::taskqueue() {

View File

@ -1,4 +1,5 @@
#include "drawtypes/animation.hpp"
#include "drawtypes/label.hpp"
#include "utils/factory.hpp"
@ -56,7 +57,7 @@ namespace drawtypes {
auto framerate = conf.get(section, name + "-framerate", 1000);
return factory_util::shared<animation>(move(vec), framerate);
return std::make_shared<animation>(move(vec), framerate);
}
} // namespace drawtypes

View File

@ -56,7 +56,7 @@ namespace drawtypes {
std::back_insert_iterator<decltype(tokens)> back_it(tokens);
std::copy(m_tokens.begin(), m_tokens.end(), back_it);
}
return factory_util::shared<label>(m_text, m_foreground, m_background, m_underline, m_overline, m_font, m_padding,
return std::make_shared<label>(m_text, m_foreground, m_background, m_underline, m_overline, m_font, m_padding,
m_margin, m_minlen, m_maxlen, m_alignment, m_ellipsis, move(tokens));
}
@ -278,7 +278,7 @@ namespace drawtypes {
}
// clang-format off
return factory_util::shared<label>(text,
return std::make_shared<label>(text,
conf.get(section, name + "-foreground", rgba{}),
conf.get(section, name + "-background", rgba{}),
conf.get(section, name + "-underline", rgba{}),

View File

@ -11,7 +11,7 @@ POLYBAR_NS
namespace drawtypes {
progressbar::progressbar(const bar_settings& bar, int width, string format)
: m_builder(factory_util::unique<builder>(bar)), m_format(move(format)), m_width(width) {}
: m_builder(std::make_unique<builder>(bar)), m_format(move(format)), m_width(width) {}
void progressbar::set_fill(label_t&& fill) {
m_fill = forward<decltype(fill)>(fill);
@ -97,7 +97,7 @@ namespace drawtypes {
throw application_error("Invalid width defined at [" + section + "." + name + "]");
}
auto pbar = factory_util::shared<progressbar>(bar, width, format);
auto pbar = std::make_shared<progressbar>(bar, width, format);
pbar->set_gradient(conf.get(section, name + "-gradient", true));
pbar->set_colors(conf.get_list(section, name + "-foreground", vector<rgba>{}));

View File

@ -1,4 +1,5 @@
#include "drawtypes/ramp.hpp"
#include "utils/factory.hpp"
#include "utils/math.hpp"
@ -64,8 +65,8 @@ namespace drawtypes {
vec.emplace_back(move(icon));
}
return factory_util::shared<drawtypes::ramp>(move(vec));
return std::make_shared<drawtypes::ramp>(move(vec));
}
}
} // namespace drawtypes
POLYBAR_NS_END

View File

@ -132,13 +132,13 @@ namespace modules {
m_labelseparator = load_optional_label(m_conf, name(), "label-separator", "");
m_icons = factory_util::shared<iconset>();
m_icons->add(DEFAULT_ICON, factory_util::shared<label>(m_conf.get(name(), DEFAULT_ICON, ""s)));
m_icons = std::make_shared<iconset>();
m_icons->add(DEFAULT_ICON, std::make_shared<label>(m_conf.get(name(), DEFAULT_ICON, ""s)));
for (const auto& workspace : m_conf.get_list<string>(name(), "ws-icon", {})) {
auto vec = string_util::tokenize(workspace, ';');
if (vec.size() == 2) {
m_icons->add(vec[0], factory_util::shared<label>(vec[1]));
m_icons->add(vec[0], std::make_shared<label>(vec[1]));
}
}
}
@ -234,7 +234,7 @@ namespace modules {
unsigned int workspace_mask{0U};
if (tag[0] == 'm' || tag[0] == 'M') {
m_monitors.emplace_back(factory_util::unique<bspwm_monitor>());
m_monitors.emplace_back(std::make_unique<bspwm_monitor>());
m_monitors.back()->name = value;
if (m_monitorlabel) {

View File

@ -24,7 +24,7 @@ namespace modules {
throw module_error("Could not find socket: " + (socket_path.empty() ? "<empty>" : socket_path));
}
m_ipc = factory_util::unique<i3ipc::connection>();
m_ipc = std::make_unique<i3ipc::connection>();
// Load configuration values
m_click = m_conf.get(name(), "enable-click", m_click);
@ -59,13 +59,13 @@ namespace modules {
m_labelseparator = load_optional_label(m_conf, name(), "label-separator", "");
m_icons = factory_util::shared<iconset>();
m_icons->add(DEFAULT_WS_ICON, factory_util::shared<label>(m_conf.get(name(), DEFAULT_WS_ICON, ""s)));
m_icons = std::make_shared<iconset>();
m_icons->add(DEFAULT_WS_ICON, std::make_shared<label>(m_conf.get(name(), DEFAULT_WS_ICON, ""s)));
for (const auto& workspace : m_conf.get_list<string>(name(), "ws-icon", {})) {
auto vec = string_util::tokenize(workspace, ';');
if (vec.size() == 2) {
m_icons->add(vec[0], factory_util::shared<label>(vec[1]));
m_icons->add(vec[0], std::make_shared<label>(vec[1]));
}
}
@ -173,7 +173,7 @@ namespace modules {
label->replace_token("%name%", ws_name);
label->replace_token("%icon%", icon->get());
label->replace_token("%index%", to_string(ws->num));
m_workspaces.emplace_back(factory_util::unique<workspace>(ws->name, ws_state, move(label)));
m_workspaces.emplace_back(std::make_unique<workspace>(ws->name, ws_state, move(label)));
}
return true;

View File

@ -49,7 +49,7 @@ namespace modules {
}
m_log.trace("%s: Creating menu level %i", name(), m_levels.size());
m_levels.emplace_back(factory_util::unique<menu_tree>());
m_levels.emplace_back(std::make_unique<menu_tree>());
while (true) {
string item_param{level_param + "-" + to_string(m_levels.back()->items.size())};
@ -59,7 +59,7 @@ namespace modules {
}
m_log.trace("%s: Creating menu level item %i", name(), m_levels.back()->items.size());
auto item = factory_util::unique<menu_tree_item>();
auto item = std::make_unique<menu_tree_item>();
item->label = load_label(m_conf, name(), item_param);
item->exec = m_conf.get(name(), item_param + "-exec", actions_util::get_action_string(*this, EVENT_CLOSE, ""));
m_levels.back()->items.emplace_back(move(item));

View File

@ -67,7 +67,7 @@ namespace modules {
m_formatter->add(FORMAT_OFFLINE, "", {TAG_LABEL_OFFLINE});
m_icons = factory_util::shared<iconset>();
m_icons = std::make_shared<iconset>();
if (m_formatter->has(TAG_ICON_PLAY) || m_formatter->has(TAG_TOGGLE) || m_formatter->has(TAG_TOGGLE_STOP)) {
m_icons->add("play", load_label(m_conf, name(), TAG_ICON_PLAY));
@ -133,7 +133,7 @@ namespace modules {
m_lastsync = chrono::system_clock::now();
try {
m_mpd = factory_util::unique<mpdconnection>(m_log, m_host, m_port, m_pass);
m_mpd = std::make_unique<mpdconnection>(m_log, m_host, m_port, m_pass);
m_mpd->connect();
m_status = m_mpd->get_status();
} catch (const mpd_exception& err) {
@ -170,7 +170,7 @@ namespace modules {
try {
if (!m_mpd) {
m_mpd = factory_util::unique<mpdconnection>(m_log, m_host, m_port, m_pass);
m_mpd = std::make_unique<mpdconnection>(m_log, m_host, m_port, m_pass);
}
if (!connected()) {
m_mpd->connect();
@ -367,9 +367,9 @@ namespace modules {
* We have to create a separate mpd instance because actions run in the
* controller thread and the `m_mpd` pointer is used in the module thread.
*/
#define MPD_CONNECT() \
auto mpd = factory_util::unique<mpdconnection>(m_log, m_host, m_port, m_pass); \
mpd->connect(); \
#define MPD_CONNECT() \
auto mpd = std::make_unique<mpdconnection>(m_log, m_host, m_port, m_pass); \
mpd->connect(); \
auto status = mpd->get_status()
void mpd_module::action_play() {

View File

@ -92,10 +92,10 @@ namespace modules {
// Get an intstance of the network interface
if (net::is_wireless_interface(m_interface)) {
m_wireless = factory_util::unique<net::wireless_network>(m_interface);
m_wireless = std::make_unique<net::wireless_network>(m_interface);
m_wireless->set_unknown_up(m_unknown_up);
} else {
m_wired = factory_util::unique<net::wired_network>(m_interface);
m_wired = std::make_unique<net::wired_network>(m_interface);
m_wired->set_unknown_up(m_unknown_up);
};

View File

@ -28,7 +28,7 @@ namespace modules {
bool m_max_volume = m_conf.get(name(), "use-ui-max", true);
try {
m_pulseaudio = factory_util::unique<pulseaudio>(m_log, move(sink_name), m_max_volume);
m_pulseaudio = std::make_unique<pulseaudio>(m_log, move(sink_name), m_max_volume);
} catch (const pulseaudio_error& err) {
throw module_error(err.what());
}

View File

@ -45,13 +45,13 @@ namespace modules {
m_blacklist = m_conf.get_list(name(), "blacklist", {});
// load layout icons
m_layout_icons = factory_util::shared<iconset>();
m_layout_icons = std::make_shared<iconset>();
m_layout_icons->add(DEFAULT_LAYOUT_ICON, load_optional_label(m_conf, name(), DEFAULT_LAYOUT_ICON, ""s));
for (const auto& it : m_conf.get_list<string>(name(), "layout-icon", {})) {
auto vec = string_util::tokenize(it, ';');
if (vec.size() == 2) {
m_layout_icons->add(vec[0], factory_util::shared<label>(vec[1]));
m_layout_icons->add(vec[0], std::make_shared<label>(vec[1]));
}
}
@ -77,24 +77,24 @@ namespace modules {
}
// load indicator icons
m_indicator_icons_off = factory_util::shared<iconset>();
m_indicator_icons_on = factory_util::shared<iconset>();
m_indicator_icons_off = std::make_shared<iconset>();
m_indicator_icons_on = std::make_shared<iconset>();
auto icon_pair = string_util::tokenize(m_conf.get(name(), DEFAULT_INDICATOR_ICON, ""s), ';');
if (icon_pair.size() == 2) {
m_indicator_icons_off->add(DEFAULT_INDICATOR_ICON, factory_util::shared<label>(icon_pair[0]));
m_indicator_icons_on->add(DEFAULT_INDICATOR_ICON, factory_util::shared<label>(icon_pair[1]));
m_indicator_icons_off->add(DEFAULT_INDICATOR_ICON, std::make_shared<label>(icon_pair[0]));
m_indicator_icons_on->add(DEFAULT_INDICATOR_ICON, std::make_shared<label>(icon_pair[1]));
} else {
m_indicator_icons_off->add(DEFAULT_INDICATOR_ICON, factory_util::shared<label>(""s));
m_indicator_icons_on->add(DEFAULT_INDICATOR_ICON, factory_util::shared<label>(""s));
m_indicator_icons_off->add(DEFAULT_INDICATOR_ICON, std::make_shared<label>(""s));
m_indicator_icons_on->add(DEFAULT_INDICATOR_ICON, std::make_shared<label>(""s));
}
for (const auto& it : m_conf.get_list<string>(name(), "indicator-icon", {})) {
auto icon_triple = string_util::tokenize(it, ';');
if (icon_triple.size() == 3) {
auto const indicator_str = string_util::lower(icon_triple[0]);
m_indicator_icons_off->add(indicator_str, factory_util::shared<label>(icon_triple[1]));
m_indicator_icons_on->add(indicator_str, factory_util::shared<label>(icon_triple[2]));
m_indicator_icons_off->add(indicator_str, std::make_shared<label>(icon_triple[1]));
m_indicator_icons_on->add(indicator_str, std::make_shared<label>(icon_triple[2]));
}
}
@ -232,7 +232,7 @@ namespace modules {
auto layouts = xkb_util::get_layouts(m_connection, XCB_XKB_ID_USE_CORE_KBD);
auto indicators = xkb_util::get_indicators(m_connection, XCB_XKB_ID_USE_CORE_KBD);
auto current_group = xkb_util::get_current_group(m_connection, XCB_XKB_ID_USE_CORE_KBD);
m_keyboard = factory_util::unique<keyboard>(move(layouts), move(indicators), current_group);
m_keyboard = std::make_unique<keyboard>(move(layouts), move(indicators), current_group);
return true;
} catch (const exception& err) {
throw module_error("Failed to query keyboard, err: " + string{err.what()});

View File

@ -80,13 +80,13 @@ namespace modules {
// clang-format on
}
m_icons = factory_util::shared<iconset>();
m_icons->add(DEFAULT_ICON, factory_util::shared<label>(m_conf.get(name(), DEFAULT_ICON, ""s)));
m_icons = std::make_shared<iconset>();
m_icons->add(DEFAULT_ICON, std::make_shared<label>(m_conf.get(name(), DEFAULT_ICON, ""s)));
for (const auto& workspace : m_conf.get_list<string>(name(), "icon", {})) {
auto vec = string_util::tokenize(workspace, ';');
if (vec.size() == 2) {
m_icons->add(vec[0], factory_util::shared<label>(vec[1]));
m_icons->add(vec[0], std::make_shared<label>(vec[1]));
}
}
@ -409,7 +409,6 @@ namespace modules {
for (auto&& viewport : m_viewports) {
for (auto&& desktop : viewport->desktops) {
if (current_desktop == desktop->index) {
current_index = indices.size();
}
@ -423,7 +422,7 @@ namespace modules {
return;
}
int offset = next? 1 : -1;
int offset = next ? 1 : -1;
int new_index = (current_index + offset + indices.size()) % indices.size();
focus_desktop(indices.at(new_index));

View File

@ -16,7 +16,7 @@ namespace tags {
* Create instance
*/
dispatch::make_type dispatch::make(action_context& action_ctxt) {
return factory_util::unique<dispatch>(logger::make(), action_ctxt);
return std::make_unique<dispatch>(logger::make(), action_ctxt);
}
/**

View File

@ -3,6 +3,5 @@
POLYBAR_NS
factory_util::detail::null_deleter factory_util::null_deleter{};
factory_util::detail::fd_deleter factory_util::fd_deleter{};
POLYBAR_NS_END

View File

@ -1,7 +1,8 @@
#include "utils/inotify.hpp"
#include <unistd.h>
#include "errors.hpp"
#include "utils/inotify.hpp"
#include "utils/memory.hpp"
POLYBAR_NS
@ -70,7 +71,7 @@ bool inotify_watch::poll(int wait_ms) const {
* Get the latest inotify event
*/
unique_ptr<inotify_event> inotify_watch::get_event() const {
auto event = factory_util::unique<inotify_event>();
auto event = std::make_unique<inotify_event>();
if (m_fd == -1 || m_wd == -1) {
return event;

View File

@ -48,7 +48,7 @@ POLYBAR_NS
* Create instance
*/
tray_manager::make_type tray_manager::make() {
return factory_util::unique<tray_manager>(
return std::make_unique<tray_manager>(
connection::make(), signal_emitter::make(), logger::make(), background_manager::make());
}
@ -533,10 +533,10 @@ void tray_manager::create_bg(bool realloc) {
m_gc = 0;
}
if(realloc && m_surface) {
if (realloc && m_surface) {
m_surface.reset();
}
if(realloc && m_context) {
if (realloc && m_context) {
m_context.reset();
}
@ -742,7 +742,7 @@ void tray_manager::track_selection_owner(xcb_window_t owner) {
void tray_manager::process_docking_request(xcb_window_t win) {
m_log.info("Processing docking request from '%s' (%s)", ewmh_util::get_wm_name(win), m_connection.id(win));
m_clients.emplace_back(factory_util::shared<tray_client>(m_connection, win, m_opts.width, m_opts.height));
m_clients.emplace_back(std::make_shared<tray_client>(m_connection, win, m_opts.width, m_opts.height));
auto& client = m_clients.back();
try {
@ -759,7 +759,6 @@ void tray_manager::process_docking_request(xcb_window_t win) {
client->get_xembed().get_flags(), client->get_xembed().is_mapped() ? "true" : "false");
}
try {
const unsigned int mask = XCB_CW_EVENT_MASK;
const unsigned int values[]{XCB_EVENT_MASK_PROPERTY_CHANGE | XCB_EVENT_MASK_STRUCTURE_NOTIFY};

View File

@ -1,5 +1,6 @@
#include "common/test.hpp"
#include "drawtypes/ramp.hpp"
#include "common/test.hpp"
#include "utils/factory.hpp"
using namespace polybar::drawtypes;
@ -7,9 +8,9 @@ using namespace polybar;
TEST(Ramp, perc) {
ramp r;
r.add(factory_util::shared<label>("test1", 0));
r.add(factory_util::shared<label>("test2", 0));
r.add(factory_util::shared<label>("test3", 0));
r.add(std::make_shared<label>("test1", 0));
r.add(std::make_shared<label>("test2", 0));
r.add(std::make_shared<label>("test3", 0));
EXPECT_EQ("test1", r.get_by_percentage(33)->get());
EXPECT_EQ("test2", r.get_by_percentage(34)->get());
EXPECT_EQ("test3", r.get_by_percentage(67)->get());
@ -19,7 +20,7 @@ TEST(Ramp, perc) {
EXPECT_EQ("test3", r.get_by_percentage_with_borders(41, 20, 40)->get());
EXPECT_EQ("test1", r.get_by_percentage_with_borders(20, 20, 40)->get());
EXPECT_EQ("test3", r.get_by_percentage_with_borders(40, 20, 40)->get());
r.add(factory_util::shared<label>("test4", 0));
r.add(std::make_shared<label>("test4", 0));
EXPECT_EQ("test2", r.get_by_percentage_with_borders(29, 20, 40)->get());
EXPECT_EQ("test3", r.get_by_percentage_with_borders(31, 20, 40)->get());
}