From 0dcf117910d5766cf09f541bc03efb5845234c62 Mon Sep 17 00:00:00 2001 From: Jason Volk Date: Fri, 23 Mar 2018 18:00:21 -0700 Subject: [PATCH] ircd::mods: Provide a reference to the module's own handle inside the module. --- include/ircd/mapi.h | 21 +++++++++++++++++++++ ircd/mods.cc | 3 +++ 2 files changed, 24 insertions(+) diff --git a/include/ircd/mapi.h b/include/ircd/mapi.h index ad25cd69a..65fb94d58 100644 --- a/include/ircd/mapi.h +++ b/include/ircd/mapi.h @@ -44,11 +44,16 @@ struct ircd::mapi::header init_function init; // Executed after dlopen() fini_function fini; // Executed before dlclose() metadata meta; // Various key-value metadata + mods::mod *self; // Module instance once loaded // get and set metadata auto &operator[](const std::string &s) const; auto &operator[](const std::string &s); + // become self + operator const mods::mod &() const; + operator mods::mod &(); + header(const char *const &desc = "", init_function = {}, fini_function = {}); @@ -69,6 +74,7 @@ ircd::mapi::header::header(const char *const &desc, { { "description", desc } } +,self{nullptr} { } @@ -79,6 +85,21 @@ noexcept static_destruction = true; } +inline ircd::mapi::header::operator +mods::mod &() +{ + assert(self); + return *self; +} + +inline ircd::mapi::header::operator +const mods::mod &() +const +{ + assert(self); + return *self; +} + inline auto & ircd::mapi::header::operator[](const std::string &key) { diff --git a/ircd/mods.cc b/ircd/mods.cc index 8f7594bc7..b0777d929 100644 --- a/ircd/mods.cc +++ b/ircd/mods.cc @@ -106,6 +106,9 @@ try header->magic, mapi::MAGIC); + // Tell the module where to find us. + header->self = this; + // Set some basic metadata auto &meta(header->meta); meta["name"] = name();