From 1711df33a997744f0a98ef9117f495a899bd0093 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Sat, 15 Sep 2018 00:37:16 -0700 Subject: [PATCH] ircd::conf: Use util::callbacks here. --- include/ircd/conf.h | 2 +- ircd/conf.cc | 7 +++---- modules/s_conf.cc | 14 ++++++++++++-- 3 files changed, 16 insertions(+), 7 deletions(-) diff --git a/include/ircd/conf.h b/include/ircd/conf.h index cb0dfc731..0da4a2fd5 100644 --- a/include/ircd/conf.h +++ b/include/ircd/conf.h @@ -46,7 +46,7 @@ namespace ircd::conf using set_cb = std::function; extern std::map *> items; - extern std::function &)> _init_cb; + extern callbacks &)> on_init; bool exists(const string_view &key); string_view get(const string_view &key, const mutable_buffer &out); diff --git a/ircd/conf.cc b/ircd/conf.cc index 1769fd8b0..ef8ea562e 100644 --- a/ircd/conf.cc +++ b/ircd/conf.cc @@ -12,8 +12,8 @@ decltype(ircd::conf::items) ircd::conf::items {}; -decltype(ircd::conf::_init_cb) -ircd::conf::_init_cb +decltype(ircd::conf::on_init) +ircd::conf::on_init {}; size_t @@ -207,8 +207,7 @@ void ircd::conf::item::call_init() try { - if(_init_cb) - _init_cb(*this); + on_init(*this); } catch(const std::exception &e) { diff --git a/modules/s_conf.cc b/modules/s_conf.cc index 90a41b775..ab4556c59 100644 --- a/modules/s_conf.cc +++ b/modules/s_conf.cc @@ -15,16 +15,26 @@ extern "C" void reload_conf(); extern "C" void refresh_conf(); static void init_conf_item(conf::item<> &); +// This module registers with conf::on_init to be called back +// when a conf item is initialized; when this module is unloaded +// we have to unregister that listener using this state. +decltype(conf::on_init)::const_iterator +conf_on_init_iter +{ + end(conf::on_init) +}; + mapi::header IRCD_MODULE { "Server Configuration", [] { - ircd::conf::_init_cb = init_conf_item; + conf_on_init_iter = conf::on_init.emplace(end(conf::on_init), init_conf_item); reload_conf(); }, [] { - ircd::conf::_init_cb = nullptr; + assert(conf_on_init_iter != end(conf::on_init)); + conf::on_init.erase(conf_on_init_iter); } };