refactor(di): Move configure_T() to anonymous ns
This commit is contained in:
parent
bcf9249dc7
commit
7905f37462
@ -67,20 +67,6 @@ class bar : public xpp::event::sink<evt::button_press, evt::expose, evt::propert
|
||||
m_window.destroy();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = unique_ptr<bar>>
|
||||
static di::injector<T> 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<evt::button_press, evt::expose, evt::propert
|
||||
XftDraw* m_xftdraw;
|
||||
};
|
||||
|
||||
namespace {
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = unique_ptr<bar>>
|
||||
di::injector<T> configure_bar() {
|
||||
// clang-format off
|
||||
return di::make_injector(
|
||||
configure_connection(),
|
||||
configure_config(),
|
||||
configure_logger(),
|
||||
configure_fontmanager());
|
||||
// clang-format on
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -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<string>(flag))
|
||||
, flag_long(forward<string>(flag_long))
|
||||
, desc(forward<string>(desc))
|
||||
, token(forward<string>(token))
|
||||
, values(forward<choices>(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<string>(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<string>(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<string>(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<string>(opt)))
|
||||
return m_optvalues.find(forward<string>(opt))->second;
|
||||
return m_optvalues.find(opt)->second;
|
||||
return "";
|
||||
}
|
||||
|
||||
@ -130,18 +130,6 @@ namespace command_line {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <class T = parser>
|
||||
static di::injector<T> 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 <class T = command_line::parser>
|
||||
di::injector<T> 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
|
||||
|
@ -178,14 +178,6 @@ class config {
|
||||
return vec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = const config&>
|
||||
static di::injector<T> 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 <typename T = const config&>
|
||||
di::injector<T> configure_config() {
|
||||
return di::make_injector(configure_logger(), configure_xresource_manager());
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -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<unique_ptr<controller>> configure(inotify_watch_t& confwatch) {
|
||||
// clang-format off
|
||||
return di::make_injector(di::bind<controller>().to<controller>(),
|
||||
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 <typename T = unique_ptr<controller>>
|
||||
di::injector<T> configure_controller(inotify_watch_t& confwatch) {
|
||||
// clang-format off
|
||||
return di::make_injector(
|
||||
di::bind<controller>().to<controller>(),
|
||||
di::bind<>().to(confwatch),
|
||||
configure_connection(),
|
||||
configure_logger(),
|
||||
configure_config(),
|
||||
configure_bar(),
|
||||
configure_traymanager());
|
||||
// clang-format on
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -112,15 +112,6 @@ class logger {
|
||||
output(loglevel::ERROR, message, args...);
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = const logger&>
|
||||
static di::injector<T> configure(loglevel level = loglevel::NONE) {
|
||||
auto instance = factory::generic_singleton<logger>(level);
|
||||
return di::make_injector(di::bind<>().to(instance));
|
||||
}
|
||||
|
||||
protected:
|
||||
template <typename T>
|
||||
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 <typename T = const logger&>
|
||||
di::injector<T> configure_logger(loglevel level = loglevel::NONE) {
|
||||
auto instance = factory::generic_singleton<logger>(level);
|
||||
return di::make_injector(di::bind<>().to(instance));
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -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<string, class color> g_colorstore;
|
||||
|
||||
class color {
|
||||
|
@ -180,18 +180,20 @@ class connection : public xpp_connection {
|
||||
m_registry.dispatch(forward<decltype(evt)>(evt));
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = connection&>
|
||||
static di::injector<T> configure() {
|
||||
return di::make_injector(di::bind<>().to(
|
||||
factory::generic_singleton<lemonbuddy::connection>(xutils::get_connection())));
|
||||
}
|
||||
|
||||
protected:
|
||||
registry m_registry{*this};
|
||||
xcb_screen_t* m_screen = nullptr;
|
||||
};
|
||||
|
||||
namespace {
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = connection&>
|
||||
di::injector<T> configure_connection() {
|
||||
return di::make_injector(
|
||||
di::bind<>().to(factory::generic_singleton<connection>(xutils::get_connection())));
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -61,14 +61,6 @@ class fontmanager {
|
||||
m_fonts.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = unique_ptr<fontmanager>>
|
||||
static di::injector<T> 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 <typename T = unique_ptr<fontmanager>>
|
||||
di::injector<T> configure_fontmanager() {
|
||||
return di::make_injector(configure_connection(), configure_logger());
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -102,9 +102,8 @@ class trayclient {
|
||||
|
||||
class traymanager
|
||||
: public xpp::event::sink<evt::expose, evt::visibility_notify, evt::client_message,
|
||||
evt::configure_request, evt::resize_request, evt::selection_clear, evt::selection_notify,
|
||||
evt::property_notify, evt::reparent_notify, evt::destroy_notify, evt::map_notify,
|
||||
evt::unmap_notify> {
|
||||
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 <class T = unique_ptr<traymanager>>
|
||||
static di::injector<T> 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 <class T = unique_ptr<traymanager>>
|
||||
di::injector<T> configure_traymanager() {
|
||||
return di::make_injector(configure_logger(), configure_connection());
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -21,15 +21,6 @@ class xresource_manager {
|
||||
return;
|
||||
}
|
||||
|
||||
/**
|
||||
* Configure injection module
|
||||
*/
|
||||
template <typename T = const xresource_manager&>
|
||||
static di::injector<T> configure() {
|
||||
auto instance = factory::generic_singleton<xresource_manager>();
|
||||
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 <typename T = const xresource_manager&>
|
||||
di::injector<T> configure_xresource_manager() {
|
||||
auto instance = factory::generic_singleton<xresource_manager>();
|
||||
return di::make_injector(di::bind<>().to(instance));
|
||||
}
|
||||
}
|
||||
|
||||
LEMONBUDDY_NS_END
|
||||
|
@ -70,7 +70,7 @@ namespace drawtypes {
|
||||
else
|
||||
frames = conf.get_list<string>(section, name, {});
|
||||
|
||||
for (int i = 0; i < (int)frames.size(); i++)
|
||||
for (size_t i = 0; i < frames.size(); i++)
|
||||
vec.emplace_back(forward<icon_t>(
|
||||
get_optional_config_icon(conf, section, name + "-" + to_string(i), frames[i])));
|
||||
|
||||
|
@ -145,7 +145,7 @@ namespace modules {
|
||||
static constexpr auto TAG_RAMP = "<ramp>";
|
||||
|
||||
throttle_util::throttle_t m_throttler;
|
||||
connection& m_connection{connection::configure().create<connection&>()};
|
||||
connection& m_connection{configure_connection().create<connection&>()};
|
||||
monitor_t m_output;
|
||||
|
||||
ramp_t m_ramp;
|
||||
|
@ -239,7 +239,7 @@ namespace command_util {
|
||||
template <typename... Args>
|
||||
command_t make_command(Args&&... args) {
|
||||
return make_unique<command>(
|
||||
logger::configure().create<const logger&>(), forward<Args>(args)...);
|
||||
configure_logger().create<const logger&>(), forward<Args>(args)...);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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)
|
||||
|
@ -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) {
|
||||
|
@ -15,7 +15,7 @@ using namespace lemonbuddy;
|
||||
int main(int argc, char** argv) {
|
||||
XInitThreads();
|
||||
|
||||
logger& logger{logger::configure<decltype(logger)>(loglevel::WARNING).create<decltype(logger)>()};
|
||||
logger& logger{configure_logger<decltype(logger)>(loglevel::WARNING).create<decltype(logger)>()};
|
||||
|
||||
//==================================================
|
||||
// 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<decltype(cli)>(argv[0], opts).create<decltype(cli)>()};
|
||||
cli_parser cli{configure_cli_parser<decltype(cli)>(argv[0], opts).create<decltype(cli)>()};
|
||||
|
||||
vector<string> 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<decltype(conf)>().create<decltype(conf)>()};
|
||||
config& conf{configure_config<decltype(conf)>().create<decltype(conf)>()};
|
||||
|
||||
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<unique_ptr<controller>>();
|
||||
auto app = configure_controller(watch).create<unique_ptr<controller>>();
|
||||
|
||||
app->bootstrap(cli.has("stdout"), cli.has("print-wmname"));
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user