From 7905f3746233adea4575f1209ef0103a101e4ed1 Mon Sep 17 00:00:00 2001 From: Michael Carlberg Date: Tue, 25 Oct 2016 01:55:59 +0200 Subject: [PATCH] refactor(di): Move configure_T() to anonymous ns --- include/components/bar.hpp | 30 +++++++------- include/components/command_line.hpp | 56 +++++++++++++------------- include/components/config.hpp | 18 +++++---- include/components/controller.hpp | 35 +++++++++------- include/components/logger.hpp | 29 +++++-------- include/components/x11/color.hpp | 5 +++ include/components/x11/connection.hpp | 20 ++++----- include/components/x11/fontmanager.hpp | 18 +++++---- include/components/x11/tray.hpp | 30 ++++++-------- include/components/x11/xresources.hpp | 20 ++++----- include/drawtypes/animation.hpp | 2 +- include/modules/xbacklight.hpp | 2 +- include/utils/command.hpp | 2 +- include/utils/socket.hpp | 4 +- include/utils/throttle.hpp | 4 +- src/main.cpp | 8 ++-- 16 files changed, 146 insertions(+), 137 deletions(-) diff --git a/include/components/bar.hpp b/include/components/bar.hpp index 11644035..e40a4c25 100644 --- a/include/components/bar.hpp +++ b/include/components/bar.hpp @@ -67,20 +67,6 @@ class bar : public xpp::event::sink> - static di::injector configure() { - // clang-format off - return di::make_injector( - connection::configure(), - config::configure(), - logger::configure(), - fontmanager::configure()); - // clang-format on - } - /** * Create required components * @@ -1016,4 +1002,20 @@ class bar : public xpp::event::sink> + di::injector configure_bar() { + // clang-format off + return di::make_injector( + configure_connection(), + configure_config(), + configure_logger(), + configure_fontmanager()); + // clang-format on + } +} + LEMONBUDDY_NS_END diff --git a/include/components/command_line.hpp b/include/components/command_line.hpp index f49ded09..8c68d815 100644 --- a/include/components/command_line.hpp +++ b/include/components/command_line.hpp @@ -26,18 +26,18 @@ namespace command_line { string flag_long; string desc; string token; - choices values; + const choices values; /** * Construct option */ explicit option( - string&& flag, string&& flag_long, string&& desc, string&& token = "", choices&& c = {}) - : flag(forward(flag)) - , flag_long(forward(flag_long)) - , desc(forward(desc)) - , token(forward(token)) - , values(forward(c)) {} + string flag, string flag_long, string desc, string token = "", const choices c = {}) + : flag(flag) + , flag_long(flag_long) + , desc(desc) + , token(token) + , values(c) {} }; // }}} @@ -48,8 +48,8 @@ namespace command_line { /** * Construct parser */ - explicit parser(string&& synopsis, const options& opts) - : m_synopsis(forward(synopsis)), m_opts(opts) {} + explicit parser(const string& synopsis, const options& opts) + : m_synopsis(synopsis), m_opts(opts) {} /** * Process input values @@ -65,23 +65,23 @@ namespace command_line { /** * Test if the passed option was provided */ - bool has(string&& option) const { - return m_optvalues.find(forward(option)) != m_optvalues.end(); + bool has(const string& option) const { + return m_optvalues.find(option) != m_optvalues.end(); } /** * Compares the option value with given string */ - bool compare(string&& opt, string val) const { - return get(forward(opt)) == val; + bool compare(string opt, string val) const { + return get(opt) == val; } /** * Gets the value defined for given option */ - string get(string&& opt) const { + string get(string opt) const { if (has(forward(opt))) - return m_optvalues.find(forward(opt))->second; + return m_optvalues.find(opt)->second; return ""; } @@ -130,18 +130,6 @@ namespace command_line { } } - /** - * Configure injection module - */ - template - static di::injector configure(string scriptname, const options& opts) { - // clang-format off - return di::make_injector( - di::bind<>().to("Usage: " + scriptname + " bar_name [OPTION...]"), - di::bind<>().to(opts)); - // clang-format on - } - protected: /** * Compare option with its short version @@ -228,4 +216,18 @@ namespace command_line { // }}} } +namespace { + /** + * Configure injection module + */ + template + di::injector configure_cli_parser(string scriptname, const command_line::options& opts) { + // clang-format off + return di::make_injector( + di::bind<>().to("Usage: " + scriptname + " bar_name [OPTION...]"), + di::bind<>().to(opts)); + // clang-format on + } +} + LEMONBUDDY_NS_END diff --git a/include/components/config.hpp b/include/components/config.hpp index 5c16d66c..d5c82d69 100644 --- a/include/components/config.hpp +++ b/include/components/config.hpp @@ -178,14 +178,6 @@ class config { return vec; } - /** - * Configure injection module - */ - template - static di::injector configure() { - return di::make_injector(logger::configure(), xresource_manager::configure()); - } - protected: /** * Find value of a config parameter defined as a reference @@ -250,4 +242,14 @@ class config { string m_current_bar; }; +namespace { + /** + * Configure injection module + */ + template + di::injector configure_config() { + return di::make_injector(configure_logger(), configure_xresource_manager()); + } +} + LEMONBUDDY_NS_END diff --git a/include/components/controller.hpp b/include/components/controller.hpp index 0a080ee9..6d8e1c2c 100644 --- a/include/components/controller.hpp +++ b/include/components/controller.hpp @@ -19,7 +19,6 @@ #include "utils/throttle.hpp" #include "modules/backlight.hpp" -#include "modules/xbacklight.hpp" #include "modules/battery.hpp" #include "modules/bspwm.hpp" #include "modules/counter.hpp" @@ -30,6 +29,7 @@ #include "modules/script.hpp" #include "modules/text.hpp" #include "modules/unsupported.hpp" +#include "modules/xbacklight.hpp" #if ENABLE_I3 #include "modules/i3.hpp" #endif @@ -271,20 +271,6 @@ class controller { m_reload = (caught_signal == SIGUSR1); } - /** - * Configure injection module - */ - static di::injector> configure(inotify_watch_t& confwatch) { - // clang-format off - return di::make_injector(di::bind().to(), - di::bind<>().to(confwatch), - connection::configure(), - logger::configure(), config::configure(), - bar::configure(), - traymanager::configure()); - // clang-format on - } - protected: /** * Set signal mask for the current and future threads @@ -569,4 +555,23 @@ class controller { throttle_util::strategy::try_once_or_leave_yolo m_throttle_strategy; }; +namespace { + /** + * Configure injection module + */ + template > + di::injector configure_controller(inotify_watch_t& confwatch) { + // clang-format off + return di::make_injector( + di::bind().to(), + di::bind<>().to(confwatch), + configure_connection(), + configure_logger(), + configure_config(), + configure_bar(), + configure_traymanager()); + // clang-format on + } +} + LEMONBUDDY_NS_END diff --git a/include/components/logger.hpp b/include/components/logger.hpp index 2eaceaf6..9092aa5a 100644 --- a/include/components/logger.hpp +++ b/include/components/logger.hpp @@ -112,15 +112,6 @@ class logger { output(loglevel::ERROR, message, args...); } - /** - * Configure injection module - */ - template - static di::injector configure(loglevel level = loglevel::NONE) { - auto instance = factory::generic_singleton(level); - return di::make_injector(di::bind<>().to(instance)); - } - protected: template decltype(auto) convert(T&& arg) const { @@ -147,19 +138,10 @@ class logger { auto suffix = m_suffixes.find(level)->second; // silence the compiler -#if defined(__clang__) #pragma clang diagnostic push #pragma clang diagnostic ignored "-Wformat-security" -#elif defined(__GCC__) -#pragma GCC diagnostic push -#pragma GCC diagnostic ignored "-Wformat-security" -#endif dprintf(m_fd, (prefix + format + suffix + "\n").c_str(), convert(values)...); -#if defined(__clang__) #pragma clang diagnostic pop -#elif defined(__GCC__) -#pragma GCC diagnostic pop -#endif } private: @@ -196,4 +178,15 @@ class logger { // clang-format on }; +namespace { + /** + * Configure injection module + */ + template + di::injector configure_logger(loglevel level = loglevel::NONE) { + auto instance = factory::generic_singleton(level); + return di::make_injector(di::bind<>().to(instance)); + } +} + LEMONBUDDY_NS_END diff --git a/include/components/x11/color.hpp b/include/components/x11/color.hpp index c99a43f7..e88d788f 100644 --- a/include/components/x11/color.hpp +++ b/include/components/x11/color.hpp @@ -7,6 +7,9 @@ LEMONBUDDY_NS +#pragma clang diagnostic push +#pragma clang diagnostic ignored "-Wgnu-anonymous-struct" + union rgba { struct { uint8_t r; @@ -17,6 +20,8 @@ union rgba { uint32_t v; }; +#pragma clang diagnostic pop + static map g_colorstore; class color { diff --git a/include/components/x11/connection.hpp b/include/components/x11/connection.hpp index cb2b0f25..61da6606 100644 --- a/include/components/x11/connection.hpp +++ b/include/components/x11/connection.hpp @@ -180,18 +180,20 @@ class connection : public xpp_connection { m_registry.dispatch(forward(evt)); } - /** - * Configure injection module - */ - template - static di::injector configure() { - return di::make_injector(di::bind<>().to( - factory::generic_singleton(xutils::get_connection()))); - } - protected: registry m_registry{*this}; xcb_screen_t* m_screen = nullptr; }; +namespace { + /** + * Configure injection module + */ + template + di::injector configure_connection() { + return di::make_injector( + di::bind<>().to(factory::generic_singleton(xutils::get_connection()))); + } +} + LEMONBUDDY_NS_END diff --git a/include/components/x11/fontmanager.hpp b/include/components/x11/fontmanager.hpp index 17e586e6..80c6da3a 100644 --- a/include/components/x11/fontmanager.hpp +++ b/include/components/x11/fontmanager.hpp @@ -61,14 +61,6 @@ class fontmanager { m_fonts.clear(); } - /** - * Configure injection module - */ - template > - static di::injector configure() { - return di::make_injector(connection::configure(), logger::configure()); - } - void set_preferred_font(int index) { // {{{ if (index <= 0) { m_fontindex = -1; @@ -247,4 +239,14 @@ class fontmanager { XftColor m_xftcolor; }; +namespace { + /** + * Configure injection module + */ + template > + di::injector configure_fontmanager() { + return di::make_injector(configure_connection(), configure_logger()); + } +} + LEMONBUDDY_NS_END diff --git a/include/components/x11/tray.hpp b/include/components/x11/tray.hpp index ae12d921..8825669b 100644 --- a/include/components/x11/tray.hpp +++ b/include/components/x11/tray.hpp @@ -102,9 +102,8 @@ class trayclient { class traymanager : public xpp::event::sink { + evt::configure_request, evt::resize_request, evt::selection_clear, evt::property_notify, + evt::reparent_notify, evt::destroy_notify, evt::map_notify, evt::unmap_notify> { public: explicit traymanager(connection& conn, const logger& logger) : m_connection(conn), m_logger(logger) { @@ -276,14 +275,6 @@ class traymanager } } - /** - * Configure injection module - */ - template > - static di::injector configure() { - return di::make_injector(logger::configure(), connection::configure()); - } - protected: /** * Signal handler connected to the bar window's visibility change signal. @@ -636,13 +627,6 @@ class traymanager } } - /** - * Event callback : XCB_SELECTION_NOTIFY - */ - void handle(const evt::selection_notify& evt) { - m_logger.trace("tray: Received selection_notify"); - } - /** * Event callback : XCB_PROPERTY_NOTIFY */ @@ -778,4 +762,14 @@ class traymanager // }}} +namespace { + /** + * Configure injection module + */ + template > + di::injector configure_traymanager() { + return di::make_injector(configure_logger(), configure_connection()); + } +} + LEMONBUDDY_NS_END diff --git a/include/components/x11/xresources.hpp b/include/components/x11/xresources.hpp index 47e44b04..62338a56 100644 --- a/include/components/x11/xresources.hpp +++ b/include/components/x11/xresources.hpp @@ -21,15 +21,6 @@ class xresource_manager { return; } - /** - * Configure injection module - */ - template - static di::injector configure() { - auto instance = factory::generic_singleton(); - return di::make_injector(di::bind<>().to(instance)); - } - string get_string(string name) const { return load_value(name, "String", 64); } @@ -63,4 +54,15 @@ class xresource_manager { XrmDatabase m_db; }; +namespace { + /** + * Configure injection module + */ + template + di::injector configure_xresource_manager() { + auto instance = factory::generic_singleton(); + return di::make_injector(di::bind<>().to(instance)); + } +} + LEMONBUDDY_NS_END diff --git a/include/drawtypes/animation.hpp b/include/drawtypes/animation.hpp index 24e68380..e76dabd7 100644 --- a/include/drawtypes/animation.hpp +++ b/include/drawtypes/animation.hpp @@ -70,7 +70,7 @@ namespace drawtypes { else frames = conf.get_list(section, name, {}); - for (int i = 0; i < (int)frames.size(); i++) + for (size_t i = 0; i < frames.size(); i++) vec.emplace_back(forward( get_optional_config_icon(conf, section, name + "-" + to_string(i), frames[i]))); diff --git a/include/modules/xbacklight.hpp b/include/modules/xbacklight.hpp index 6972a5ef..720947fa 100644 --- a/include/modules/xbacklight.hpp +++ b/include/modules/xbacklight.hpp @@ -145,7 +145,7 @@ namespace modules { static constexpr auto TAG_RAMP = ""; throttle_util::throttle_t m_throttler; - connection& m_connection{connection::configure().create()}; + connection& m_connection{configure_connection().create()}; monitor_t m_output; ramp_t m_ramp; diff --git a/include/utils/command.hpp b/include/utils/command.hpp index 9491505a..e6b79678 100644 --- a/include/utils/command.hpp +++ b/include/utils/command.hpp @@ -239,7 +239,7 @@ namespace command_util { template command_t make_command(Args&&... args) { return make_unique( - logger::configure().create(), forward(args)...); + configure_logger().create(), forward(args)...); } } diff --git a/include/utils/socket.hpp b/include/utils/socket.hpp index f6d8e7a8..af83e9a3 100644 --- a/include/utils/socket.hpp +++ b/include/utils/socket.hpp @@ -71,8 +71,8 @@ namespace socket_util { /** * Receive data */ - auto receive(ssize_t receive_bytes, ssize_t& bytes_received_addr, int flags = 0) { - char buffer[receive_bytes + 1]; + auto receive(const ssize_t receive_bytes, ssize_t& bytes_received_addr, int flags = 0) { + char buffer[BUFSIZ]; bytes_received_addr = ::recv(m_fd, buffer, receive_bytes, flags); if (bytes_received_addr == -1) diff --git a/include/utils/throttle.hpp b/include/utils/throttle.hpp index c6248e9c..e0ce7887 100644 --- a/include/utils/throttle.hpp +++ b/include/utils/throttle.hpp @@ -21,7 +21,7 @@ namespace throttle_util { * Only pass events when there are slots available */ struct try_once_or_leave_yolo { - bool operator()(queue& q, limit l, timewindow t) { + bool operator()(queue& q, limit l, timewindow) { if (q.size() >= l) return false; q.emplace_back(timepoint_clock::now()); @@ -35,7 +35,7 @@ namespace throttle_util { * then let the event pass */ struct wait_patiently_by_the_door { - bool operator()(queue& q, limit l, timewindow t) { + bool operator()(queue& q, limit l, timewindow) { auto now = timepoint_clock::now(); q.emplace_back(now); if (q.size() >= l) { diff --git a/src/main.cpp b/src/main.cpp index 10d6b24d..2ca5c541 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -15,7 +15,7 @@ using namespace lemonbuddy; int main(int argc, char** argv) { XInitThreads(); - logger& logger{logger::configure(loglevel::WARNING).create()}; + logger& logger{configure_logger(loglevel::WARNING).create()}; //================================================== // Connect to X server @@ -51,7 +51,7 @@ int main(int argc, char** argv) { // Parse command line arguments //================================================== using cli_parser = command_line::parser; - cli_parser cli{cli_parser::configure(argv[0], opts).create()}; + cli_parser cli{configure_cli_parser(argv[0], opts).create()}; vector args; for (int i = 1; i < argc; i++) { @@ -79,7 +79,7 @@ int main(int argc, char** argv) { //================================================== // Load user configuration //================================================== - config& conf{config::configure().create()}; + config& conf{configure_config().create()}; if (cli.has("config")) conf.load(cli.get("config"), args[0]); @@ -109,7 +109,7 @@ int main(int argc, char** argv) { //================================================== // Create controller and run application //================================================== - auto app = controller::configure(watch).create>(); + auto app = configure_controller(watch).create>(); app->bootstrap(cli.has("stdout"), cli.has("print-wmname"));