diff options
| author | 2026-03-28 21:32:23 +0000 | |
|---|---|---|
| committer | 2026-03-29 00:42:15 +0000 | |
| commit | cbc5431d62e3fe9166f18395dce3ddf2af0906d3 (patch) | |
| tree | 48a87fc27dc4826ce0caf4071e2060a9ff9e24c5 /include | |
| parent | Move service code from base to its own header. (diff) | |
Switch modules from reference<> to shared_ptr<> and weak_ptr<>.
Diffstat (limited to 'include')
50 files changed, 258 insertions, 180 deletions
diff --git a/include/clientprotocol.h b/include/clientprotocol.h index 7c2ed6365..9dd806f29 100644 --- a/include/clientprotocol.h +++ b/include/clientprotocol.h @@ -549,7 +549,7 @@ class CoreExport ClientProtocol::MessageTagEvent : public Events::ModuleEventProvider { public: - MessageTagEvent(Module* mod) + MessageTagEvent(const WeakModulePtr& mod) : ModuleEventProvider(mod, "messagetag") { } @@ -568,7 +568,7 @@ public: * @param mod Module owning the provider. * @param eventprio The priority to give this event listener. */ - MessageTagProvider(Module* mod, unsigned int eventprio = DefaultPriority) + MessageTagProvider(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : Events::ModuleEventListener(mod, "messagetag", eventprio) { } @@ -624,7 +624,7 @@ public: * @param priority Priority of the hook. Determines the order in which hooks for the same event get called. * Optional. */ - EventHook(Module* mod, const std::string& name, unsigned int priority = Events::ModuleEventListener::DefaultPriority) + EventHook(const WeakModulePtr& mod, const std::string& name, unsigned int priority = Events::ModuleEventListener::DefaultPriority) : Events::ModuleEventListener(mod, GetEventName(name), priority) { } @@ -663,7 +663,7 @@ public: * @param eventname Name of the event this provider is for, e.g. "JOIN", "PART", "NUMERIC". * Should match command name if applicable. */ - EventProvider(Module* Mod, const std::string& eventname) + EventProvider(const WeakModulePtr& Mod, const std::string& eventname) : Events::ModuleEventProvider(Mod, ClientProtocol::EventHook::GetEventName(eventname)) { } @@ -690,20 +690,20 @@ struct CoreExport ClientProtocol::RFCEvents final EventProvider error; RFCEvents() - : numeric(nullptr, "NUMERIC") - , reply(nullptr, "REPLY") - , join(nullptr, "JOIN") - , part(nullptr, "PART") - , kick(nullptr, "KICK") - , quit(nullptr, "QUIT") - , nick(nullptr, "NICK") - , mode(nullptr, "MODE") - , topic(nullptr, "TOPIC") - , privmsg(nullptr, "PRIVMSG") - , invite(nullptr, "INVITE") - , ping(nullptr, "PING") - , pong(nullptr, "PONG") - , error(nullptr, "ERROR") + : numeric({}, "NUMERIC") + , reply({}, "REPLY") + , join({}, "JOIN") + , part({}, "PART") + , kick({}, "KICK") + , quit({}, "QUIT") + , nick({}, "NICK") + , mode({}, "MODE") + , topic({}, "TOPIC") + , privmsg({}, "PRIVMSG") + , invite({}, "INVITE") + , ping({}, "PING") + , pong({}, "PONG") + , error({}, "ERROR") { } }; @@ -729,7 +729,7 @@ public: * @param mod Module owning the serializer. * @param Name Name of the serializer, e.g. "rfc". */ - Serializer(Module* mod, const std::string& Name); + Serializer(const WeakModulePtr& mod, const std::string& Name); /** Handle a tag in a message being parsed. Call this method for each parsed tag. * @param user User sending the tag. diff --git a/include/command.h b/include/command.h index 1c08152ac..313cfba59 100644 --- a/include/command.h +++ b/include/command.h @@ -210,7 +210,7 @@ public: * @param maxpara Maximum number of parameters this command may have - extra parameters * will be tossed into one last space-separated param. */ - CommandBase(Module* me, const std::string& cmd, size_t minpara = 0, size_t maxpara = 0); + CommandBase(const WeakModulePtr& me, const std::string& cmd, size_t minpara = 0, size_t maxpara = 0); virtual RouteDescriptor GetRouting(User* user, const CommandBase::Params& parameters); @@ -239,7 +239,7 @@ protected: * @param minpara The minimum number of parameters that the command accepts. * @param maxpara The maximum number of parameters that the command accepts. */ - Command(Module* me, const std::string& cmd, size_t minpara = 0, size_t maxpara = 0); + Command(const WeakModulePtr& me, const std::string& cmd, size_t minpara = 0, size_t maxpara = 0); public: /** Unregisters this command from the command parser. */ @@ -314,7 +314,7 @@ protected: * @param minpara The minimum number of parameters that the command accepts. * @param maxpara The maximum number of parameters that the command accepts. */ - SplitCommand(Module* me, const std::string& cmd, size_t minpara = 0, size_t maxpara = 0); + SplitCommand(const WeakModulePtr& me, const std::string& cmd, size_t minpara = 0, size_t maxpara = 0); public: /** @copydoc Command::Handle */ diff --git a/include/dynamic.h b/include/dynamic.h index b86bf5e3f..a8277f7ac 100644 --- a/include/dynamic.h +++ b/include/dynamic.h @@ -69,7 +69,7 @@ public: /** Attempts to create a new module instance from this shared library. * @return Either a new instance of the Module class or NULL on error. */ - Module* CallInit(); + ModulePtr CallInit(); /** Retrieves the value of the specified symbol. * @param name The name of the symbol to retrieve. diff --git a/include/dynref.h b/include/dynref.h index 25b297108..1c5ff2969 100644 --- a/include/dynref.h +++ b/include/dynref.h @@ -45,8 +45,8 @@ private: protected: ServiceProvider* value = nullptr; public: - const reference<Module> creator; - dynamic_reference_base(Module* mod, const std::string& stype, const std::string& sname, bool strict); + const WeakModulePtr creator; + dynamic_reference_base(const WeakModulePtr& mod, const std::string& stype, const std::string& sname, bool strict); dynamic_reference_base(const dynamic_reference_base& other); ~dynamic_reference_base(); @@ -86,7 +86,7 @@ class dynamic_reference : public dynamic_reference_base { public: - dynamic_reference(Module* mod, const std::string& stype, const std::string& sname = "", bool strict = false) + dynamic_reference(const WeakModulePtr& mod, const std::string& stype, const std::string& sname = "", bool strict = false) : dynamic_reference_base(mod, stype, sname, strict) { } @@ -118,7 +118,7 @@ class dynamic_reference_nocheck : public dynamic_reference_base { public: - dynamic_reference_nocheck(Module* mod, const std::string& stype, const std::string& sname = "", bool strict = false) + dynamic_reference_nocheck(const WeakModulePtr& mod, const std::string& stype, const std::string& sname = "", bool strict = false) : dynamic_reference_base(mod, stype, sname, strict) { } @@ -149,7 +149,7 @@ class ChanModeReference final : public dynamic_reference_nocheck<ModeHandler> { public: - ChanModeReference(Module* mod, const std::string& modename) + ChanModeReference(const WeakModulePtr& mod, const std::string& modename) : dynamic_reference_nocheck<ModeHandler>(mod, "ModeHandler/C", modename, true) { } @@ -159,7 +159,7 @@ class UserModeReference final : public dynamic_reference_nocheck<ModeHandler> { public: - UserModeReference(Module* mod, const std::string& modename) + UserModeReference(const WeakModulePtr& mod, const std::string& modename) : dynamic_reference_nocheck<ModeHandler>(mod, "ModeHandler/U", modename, true) { } diff --git a/include/event.h b/include/event.h index 2da3e509c..4668f4b1e 100644 --- a/include/event.h +++ b/include/event.h @@ -38,6 +38,26 @@ class Events::ModuleEventProvider : public DataProvider , private dynamic_reference_base::CaptureHook { +private: + /** Determines whether a module is dying or has died. + * @param weakmod The module to check. + * @param modreq Whether a module is required. + */ + inline static bool IsModuleDead(const WeakModulePtr& weakmod, bool modreq) + { + if (!insp::empty_ptr(weakmod)) + { + // We have a module. Is it dead? + auto mod = weakmod.lock(); + if (mod) + return mod->dying; // Module might be dying? + + return true; //Module is dead. + } + + return modreq; // We never had a module. + } + public: struct Comp final { @@ -55,7 +75,7 @@ public: * @param mod Module providing the event(s) * @param eventid Identifier of the event or event group provided, must be unique */ - ModuleEventProvider(Module* mod, const std::string& eventid) + ModuleEventProvider(const WeakModulePtr& mod, const std::string& eventid) : DataProvider(mod, "Events::ModuleEventProvider", eventid) , prov(mod, this->service_type, this->service_name) { @@ -63,7 +83,7 @@ public: } /** Retrieves the module which created this listener. */ - const Module* GetModule() const { return prov.creator; } + const auto& GetModule() const { return prov.creator; } /** Get list of objects subscribed to this event * @return List of subscribed objects @@ -156,7 +176,7 @@ protected: * @param eventid Identifier of the event to subscribe to * @param eventprio The priority to give this event listener */ - ModuleEventListener(Module* mod, const std::string& eventid, unsigned int eventprio) + ModuleEventListener(const WeakModulePtr& mod, const std::string& eventid, unsigned int eventprio) : prov(mod, "Events::ModuleEventProvider", eventid) , eventpriority(eventprio) { @@ -176,7 +196,7 @@ public: } /** Retrieves the module which created this listener. */ - const Module* GetModule() const { return prov.creator; } + const auto& GetModule() const { return prov.creator; } /** Retrieves the priority of this event. */ unsigned int GetPriority() const { return eventpriority; } @@ -199,13 +219,12 @@ inline bool Events::ModuleEventProvider::ElementComp::operator()(Events::ModuleE template<typename Class, typename... FunArgs, typename... FwdArgs> inline void Events::ModuleEventProvider::Call(void (Class::*function)(FunArgs...), FwdArgs&&... args) const { - if (GetModule() && GetModule()->dying) + if (IsModuleDead(GetModule(), false)) return; for (auto* subscriber : GetSubscribers()) { - const Module* mod = subscriber->GetModule(); - if (!mod || mod->dying) + if (IsModuleDead(subscriber->GetModule(), true)) continue; Class* klass = static_cast<Class*>(subscriber); @@ -216,14 +235,13 @@ inline void Events::ModuleEventProvider::Call(void (Class::*function)(FunArgs... template<typename Class, typename... FunArgs, typename... FwdArgs> inline ModResult Events::ModuleEventProvider::FirstResult(ModResult (Class::*function)(FunArgs...), FwdArgs&&... args) const { - if (GetModule() && GetModule()->dying) + if (IsModuleDead(GetModule(), false)) return MOD_RES_PASSTHRU; ModResult result; for (auto* subscriber : GetSubscribers()) { - const Module* mod = subscriber->GetModule(); - if (!mod || mod->dying) + if (IsModuleDead(subscriber->GetModule(), true)) continue; Class* klass = static_cast<Class*>(subscriber); diff --git a/include/exception.h b/include/exception.h index 017bb64df..cb7047d80 100644 --- a/include/exception.h +++ b/include/exception.h @@ -51,17 +51,16 @@ class CoreExport ModuleException { private: /* The module which threw this exception. */ - const Module* module; + const ModulePtr module; public: - /** Creates a new instance of the ModuleException class with the specified module instance and reason. * @param mod The module which threw this exception. * @param format A format string for a message that contains the reason this exception was thrown. * @param args The arguments to format the message. */ template <typename... Args> - ModuleException(const Module* mod, const char* format, Args&&... args) + ModuleException(const WeakModulePtr& mod, const char* format, Args&&... args) : ModuleException(mod, FMT::vformat(format, FMT::make_format_args(args...))) { } @@ -70,12 +69,12 @@ public: * @param mod The module which threw this exception. * @param message A message that contains the reason this exception was thrown. */ - ModuleException(const Module* mod, const std::string& message) + ModuleException(const WeakModulePtr& mod, const std::string& message) : CoreException(message) , module(mod) { } /** Retrieves the module which threw this exception. */ - const Module* GetModule() const noexcept { return module; } + const auto& GetModule() const noexcept { return module; } }; diff --git a/include/extensible.h b/include/extensible.h index 797ec0ca7..ba9ae9e62 100644 --- a/include/extensible.h +++ b/include/extensible.h @@ -93,7 +93,7 @@ public: * @param module The module to unregister extensions for. * @param list The list to add unregistered extensions to. */ - void BeginUnregister(Module* module, std::vector<ExtensionItem*>& list); + void BeginUnregister(const ModulePtr& module, std::vector<ExtensionItem*>& list); /** Retrieves registered extensions keyed by their names. */ const ExtMap& GetExts() const { return types; } diff --git a/include/extension.h b/include/extension.h index b2b37ac66..18f3678f2 100644 --- a/include/extension.h +++ b/include/extension.h @@ -86,7 +86,7 @@ protected: * @param key The name of the extension (e.g. foo-bar). * @param exttype The type of extensible that the extension applies to. */ - ExtensionItem(Module* owner, const std::string& key, ExtensionType exttype); + ExtensionItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype); /** Retrieves the value for this extension of the specified container from the internal map. * @param container The container that this extension is set on. @@ -135,7 +135,7 @@ public: * @param exttype The type of extensible that the extension applies to. * @param sync Whether this extension should be broadcast to other servers. */ - SimpleExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false) + SimpleExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false) : ExtensionItem(owner, key, exttype) , synced(sync) { @@ -281,7 +281,7 @@ public: * @param exttype The type of extensible that the extension applies to. * @param sync Whether this extension should be broadcast to other servers. */ - BoolExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false); + BoolExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false); /** Retrieves the value for this extension of the specified container. * @param container The container that this extension is set on. @@ -335,7 +335,7 @@ public: * @param exttype The type of extensible that the extension applies to. * @param sync Whether this extension should be broadcast to other servers. */ - ListExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false) + ListExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false) : SimpleExtItem<List>(owner, key, exttype, sync) { } @@ -419,7 +419,7 @@ public: * @param exttype The type of extensible that the extension applies to. * @param sync Whether this extension should be broadcast to other servers. */ - IntExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false); + IntExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false); /** Retrieves the value for this extension of the specified container. * @param container The container that this extension is set on. @@ -464,7 +464,7 @@ public: * @param exttype The type of extensible that the extension applies to. * @param sync Whether this extension should be broadcast to other servers. */ - StringExtItem(Module* owner, const std::string& key, ExtensionType exttype, bool sync = false); + StringExtItem(const WeakModulePtr& owner, const std::string& key, ExtensionType exttype, bool sync = false); /** @copydoc ExtensionItem::FromInternal */ void FromInternal(Extensible* container, const std::string& value) noexcept override; diff --git a/include/inspircd.h b/include/inspircd.h index e33480efe..50cf4638e 100644 --- a/include/inspircd.h +++ b/include/inspircd.h @@ -56,6 +56,7 @@ #include "utility/aligned_storage.h" #include "utility/iterator_range.h" +#include "utility/pointer.h" #include "intrusive_list.h" #include "flat_map.h" diff --git a/include/iohook.h b/include/iohook.h index f651eee76..dc0e5cd0f 100644 --- a/include/iohook.h +++ b/include/iohook.h @@ -45,7 +45,7 @@ public: * @param hooktype One of IOHookProvider::Type * @param middle True if the IOHook instances created by this hook are subclasses of IOHookMiddle, false otherwise */ - IOHookProvider(Module* mod, const std::string& Name, Type hooktype = IOH_UNKNOWN, bool middle = false) + IOHookProvider(const WeakModulePtr& mod, const std::string& Name, Type hooktype = IOH_UNKNOWN, bool middle = false) : DataProvider(mod, "IOHookProvider", Name) , middlehook(middle) , type(hooktype) diff --git a/include/listmode.h b/include/listmode.h index cc8d4d03e..a1e04c7e9 100644 --- a/include/listmode.h +++ b/include/listmode.h @@ -122,7 +122,7 @@ public: * @param lnum List numeric * @param eolnum End of list numeric */ - ListModeBase(Module* Creator, const std::string& Name, char modechar, unsigned int lnum, unsigned int eolnum, bool am = false); + ListModeBase(const WeakModulePtr& Creator, const std::string& Name, char modechar, unsigned int lnum, unsigned int eolnum, bool am = false); /** Determines whether some channels have longer lists than others. */ bool HasVariableLength() const { return chanlimits.size() > 1; } diff --git a/include/logging.h b/include/logging.h index 69d13a111..f9c5ea462 100644 --- a/include/logging.h +++ b/include/logging.h @@ -132,7 +132,7 @@ class CoreExport Log::Engine : public DataProvider { protected: - Engine(Module* Creator, const std::string& Name); + Engine(const WeakModulePtr& Creator, const std::string& Name); public: virtual ~Engine() override; @@ -149,7 +149,7 @@ class CoreExport Log::FileEngine final : public Engine { public: - FileEngine(Module* Creator); + FileEngine(const WeakModulePtr& Creator); /** @copydoc Log::Engine::Create */ MethodPtr Create(const std::shared_ptr<ConfigTag>& tag) override; @@ -163,7 +163,7 @@ private: FILE* file; public: - StreamEngine(Module* Creator, const std::string& Name, FILE* fh); + StreamEngine(const WeakModulePtr& Creator, const std::string& Name, FILE* fh); /** @copydoc Log::Engine::Create */ MethodPtr Create(const std::shared_ptr<ConfigTag>& tag) override; diff --git a/include/mode.h b/include/mode.h index d2732629c..55a54b005 100644 --- a/include/mode.h +++ b/include/mode.h @@ -172,7 +172,7 @@ public: * @param type Type of the mode (MODETYPE_USER or MODETYPE_CHANNEL) * @param mclass The object type of this mode handler, one of ModeHandler::Class */ - ModeHandler(Module* me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER); + ModeHandler(const WeakModulePtr& me, const std::string& name, char modeletter, ParamSpec params, ModeType type, Class mclass = MC_OTHER); Cullable::Result Cull() override; /** @copydoc ServiceProvider::RegisterService */ @@ -399,7 +399,7 @@ public: * @param PrefixRank Rank given by this prefix mode, see explanation above * @param PrefixChar Prefix character, or 0 if the mode has no prefix character */ - PrefixMode(Module* Creator, const std::string& Name, char ModeLetter, Rank PrefixRank = 0, char PrefixChar = 0); + PrefixMode(const WeakModulePtr& Creator, const std::string& Name, char ModeLetter, Rank PrefixRank = 0, char PrefixChar = 0); /** @copydoc ModeHandler::AccessCheck */ ModResult AccessCheck(User* source, Channel* channel, Modes::Change& change) override; @@ -450,7 +450,7 @@ class CoreExport SimpleUserMode : public ModeHandler { public: - SimpleUserMode(Module* Creator, const std::string& Name, char modeletter, bool operonly = false, const std::string& old = "") + SimpleUserMode(const WeakModulePtr& Creator, const std::string& Name, char modeletter, bool operonly = false, const std::string& old = "") : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_USER) { this->oldname = old; @@ -471,7 +471,7 @@ class CoreExport SimpleChannelMode { public: - SimpleChannelMode(Module* Creator, const std::string& Name, char modeletter, bool operonly = false, const std::string& old = "") + SimpleChannelMode(const WeakModulePtr& Creator, const std::string& Name, char modeletter, bool operonly = false, const std::string& old = "") : ModeHandler(Creator, Name, modeletter, PARAM_NONE, MODETYPE_CHANNEL) { this->oldname = old; @@ -503,12 +503,12 @@ private: ModeType m_type; public: - const reference<Module> creator; + const WeakModulePtr creator; /** * The constructor initializes the mode and the mode type */ - ModeWatcher(Module* creator, const std::string& modename, ModeType type); + ModeWatcher(const WeakModulePtr& creator, const std::string& modename, ModeType type); /** * The default destructor does nothing. diff --git a/include/modules.h b/include/modules.h index 754b06260..706302080 100644 --- a/include/modules.h +++ b/include/modules.h @@ -210,12 +210,18 @@ enum Implementation /** Base class for all InspIRCd modules * This class is the base class for InspIRCd modules. All modules must inherit from this class, * its methods will be called when irc server events occur. class inherited from module must be - * instantiated by the ModuleFactory class (see relevant section) for the module to be initialised. + * instantiated by MODULE_INIT (see relevant section) for the module to be initialised. */ class CoreExport Module : public Cullable - , public usecountbase + , public std::enable_shared_from_this<Module> { +private: + /** Usually contains nothing. This is part of a hack that allows us to call + * weak_from_this and shared_from_this from the constructor of modules. + */ + std::shared_ptr<Module> pointer; + protected: /** Initializes a new instance of the Module class. * @param mprops The properties of this module. @@ -243,7 +249,7 @@ public: using LinkDataDiff = insp::casemapped_map<std::pair<std::optional<std::string>, std::optional<std::string>>>; /** A list of modules. */ - using List = std::vector<Module*>; + using List = std::vector<ModulePtr>; /** Reference to the dynamic library. */ DLLManager* ModuleDLL = nullptr; @@ -265,6 +271,12 @@ public: /** The version of this module. */ const std::string version; + /** Used internally to convert a module instance to a ModulePtr after + * construction. You should call shared_from_this() to get a shared pointer + * to an already converted module. + */ + ModulePtr Share(); + /** Module setup * \exception ModuleException Throwing this class, or any class derived from ModuleException, causes loading of the module to abort. */ @@ -672,7 +684,7 @@ public: * module). * @param mod A pointer to the new module */ - virtual void OnLoadModule(Module* mod) ATTR_NOT_NULL(2); + virtual void OnLoadModule(const ModulePtr& mod); /** Called whenever a module is unloaded. * mod will contain a pointer to the module, and string will contain its name, @@ -685,7 +697,7 @@ public: * module). * @param mod Pointer to the module being unloaded (still valid) */ - virtual void OnUnloadModule(Module* mod) ATTR_NOT_NULL(2); + virtual void OnUnloadModule(const ModulePtr& mod); /** Called once every five seconds for background processing. * This timer can be used to control timed features. Its period is not accurate @@ -970,7 +982,7 @@ class CoreExport ModuleManager final { public: /** Holds a a list of modules keyed by their module name. */ - using ModuleMap = std::map<std::string, Module*>; + using ModuleMap = std::map<std::string, ModulePtr>; /** Holds one or more services. */ using ServiceList = std::vector<ServiceProvider*>; @@ -1018,7 +1030,7 @@ private: * @param mod Module whose modes to unregister * @param modetype MODETYPE_USER to unregister user modes, MODETYPE_CHANNEL to unregister channel modes */ - void UnregisterModes(Module* mod, ModeType modetype); + void UnregisterModes(const ModulePtr& mod, ModeType modetype); public: /** Event handler hooks. @@ -1069,8 +1081,8 @@ public: * then this contains a the module that your module must be placed before * or after. */ - bool SetPriority(Module* mod, Implementation i, Priority s, const std::string &which); - bool SetPriority(Module* mod, Implementation i, Priority s, Module* which = nullptr); + bool SetPriority(const ModulePtr& mod, Implementation i, Priority s, const std::string &which); + bool SetPriority(const ModulePtr& mod, Implementation i, Priority s, const ModulePtr& which = nullptr); /** Change the priority of all events in a module. * @param mod The module to set the priority of @@ -1080,7 +1092,7 @@ public: * SetPriority method for this, where you may specify other modules to * be prioritized against. */ - void SetPriority(Module* mod, Priority s); + void SetPriority(const ModulePtr& mod, Priority s); /** Attach an event to a module. * You may later detach the event with ModuleManager::Detach(). @@ -1089,19 +1101,19 @@ public: * @param mod Module to attach event to * @return True if the event was attached */ - bool Attach(Implementation i, Module* mod); + bool Attach(Implementation i, const ModulePtr& mod); /** Attach an array of events to a module * @param i Event types (array) to attach * @param mod Module to attach events to * @param sz The size of the implementation array */ - void Attach(const Implementation* i, Module* mod, size_t sz); + void Attach(const Implementation* i, const ModulePtr& mod, size_t sz); /** Attach all events to a module (used on module load) * @param mod Module to attach to all events */ - void AttachAll(Module* mod); + void AttachAll(const ModulePtr& mod); /** Detach an event from a module. * This is not required when your module unloads, as the core will @@ -1110,7 +1122,7 @@ public: * @param mod Module to detach event from * @return True if the event was detached */ - bool Detach(Implementation i, Module* mod); + bool Detach(Implementation i, const ModulePtr& mod); /** Detach an array of events from a module * This is not required when your module unloads, as the core will @@ -1119,12 +1131,12 @@ public: * @param mod Module to detach events from * @param sz The size of the implementation array */ - void Detach(const Implementation* i, Module* mod, size_t sz); + void Detach(const Implementation* i, const ModulePtr& mod, size_t sz); /** Detach all events from a module (used on unload) * @param mod Module to detach from */ - void DetachAll(Module* mod); + void DetachAll(const ModulePtr& mod); /** Returns text describing the last module error * @return The last error message to occur @@ -1143,27 +1155,27 @@ public: * * @return true on success; if false, LastError will give a reason */ - bool Unload(Module* module); + bool Unload(const ModulePtr& module); /** Called by the InspIRCd constructor to load all modules from the config file. */ void LoadAll(); void UnloadAll(); - void DoSafeUnload(Module*); + void DoSafeUnload(const ModulePtr&); /** Check if a module can be unloaded and if yes, prepare it for unload * @param mod Module to be unloaded * @return True if the module is unloadable, false otherwise. * If true the module must be unloaded in the current main loop iteration. */ - bool CanUnload(Module* mod); + bool CanUnload(const ModulePtr& mod); - /** Find a module by name, and return a Module* to it. + /** Find a module by name, and return a ModulePtr to it. * This is preferred over iterating the module lists yourself. * @param name The module name to look up * @return A pointer to the module, or NULL if the module cannot be found */ - Module* Find(const std::string& name); + ModulePtr Find(const std::string& name); /** Register a service provided by a module */ void AddService(ServiceProvider&); @@ -1220,7 +1232,7 @@ public: const Module::List& _handlers = ServerInstance->Modules.EventHandlers[I_ ## EVENT]; \ for (Module::List::const_reverse_iterator _handler = _handlers.rbegin(); _handler != _handlers.rend(); ) \ { \ - Module* _mod = *_handler++; \ + auto& _mod = *_handler++; \ try \ { \ if (!_mod->dying) \ @@ -1244,7 +1256,7 @@ public: const Module::List& _handlers = ServerInstance->Modules.EventHandlers[I_ ## EVENT]; \ for (Module::List::const_reverse_iterator _handler = _handlers.rbegin(); _handler != _handlers.rend(); ) \ { \ - Module* _mod = *_handler++; \ + auto& _mod = *_handler++; \ try \ { \ if (_mod->dying) \ diff --git a/include/modules/account.h b/include/modules/account.h index 3fbdac122..06e4f817e 100644 --- a/include/modules/account.h +++ b/include/modules/account.h @@ -38,7 +38,7 @@ class Account::APIBase : public DataProvider { public: - APIBase(Module* parent) + APIBase(const WeakModulePtr& parent) : DataProvider(parent, "accountapi") { } @@ -73,7 +73,7 @@ class Account::API final : public dynamic_reference<Account::APIBase> { public: - API(Module* parent) + API(const WeakModulePtr& parent) : dynamic_reference<Account::APIBase>(parent, "accountapi") { } @@ -84,7 +84,7 @@ class Account::EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "account", eventprio) { } @@ -101,7 +101,7 @@ class Account::ProviderAPIBase : public DataProvider { public: - ProviderAPIBase(Module* mod) + ProviderAPIBase(const WeakModulePtr& mod) : DataProvider(mod, "accountproviderapi") { } @@ -115,7 +115,7 @@ class Account::ProviderAPI final : public dynamic_reference<Account::ProviderAPIBase> { public: - ProviderAPI(Module* mod) + ProviderAPI(const WeakModulePtr& mod) : dynamic_reference<Account::ProviderAPIBase>(mod, "accountproviderapi") { } diff --git a/include/modules/away.h b/include/modules/away.h index 7ebd3fc76..380a6095f 100644 --- a/include/modules/away.h +++ b/include/modules/away.h @@ -29,7 +29,7 @@ class Away::EventListener : public Events::ModuleEventListener { protected: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "away", eventprio) { } @@ -75,7 +75,7 @@ class Away::EventProvider final : public Events::ModuleEventProvider { public: - EventProvider(Module* mod) + EventProvider(const WeakModulePtr& mod) : ModuleEventProvider(mod, "away") { } diff --git a/include/modules/callerid.h b/include/modules/callerid.h index 4d530f636..475c41cb1 100644 --- a/include/modules/callerid.h +++ b/include/modules/callerid.h @@ -29,7 +29,7 @@ class CallerID::APIBase : public DataProvider { public: - APIBase(Module* parent) + APIBase(const WeakModulePtr& parent) : DataProvider(parent, "m_callerid_api") { } @@ -46,7 +46,7 @@ class CallerID::API final : public dynamic_reference<CallerID::APIBase> { public: - API(Module* parent) + API(const WeakModulePtr& parent) : dynamic_reference<CallerID::APIBase>(parent, "m_callerid_api") { } diff --git a/include/modules/cap.h b/include/modules/cap.h index d665f1509..f349ef1e1 100644 --- a/include/modules/cap.h +++ b/include/modules/cap.h @@ -32,7 +32,7 @@ namespace Cap class ExtItem : public IntExtItem { public: - ExtItem(Module* mod); + ExtItem(const WeakModulePtr& mod); void FromInternal(Extensible* container, const std::string& value) noexcept override; std::string ToHuman(const Extensible* container, const ExtensionPtr& item) const noexcept override; std::string ToInternal(const Extensible* container, const ExtensionPtr& item) const noexcept override; @@ -54,7 +54,7 @@ namespace Cap class EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "cap", eventprio) { } @@ -74,7 +74,7 @@ namespace Cap class Manager : public DataProvider { public: - Manager(Module* mod) + Manager(const WeakModulePtr& mod) : DataProvider(mod, "capmanager") { } @@ -170,7 +170,7 @@ namespace Cap * @param mod Module providing the cap * @param Name Raw name of the cap as used in the protocol (CAP LS, etc.) */ - Capability(Module* mod, const std::string& Name) + Capability(const WeakModulePtr& mod, const std::string& Name) : ServiceProvider(mod, "Cap::Capability", Name) , manager(mod, "capmanager") { @@ -306,7 +306,7 @@ namespace Cap * @param mod Module creating this object * @param Name Raw name of the cap as used in the protocol (CAP LS, etc.) */ - Reference(Module* mod, const std::string& Name) + Reference(const WeakModulePtr& mod, const std::string& Name) : ref(mod, "Cap::Capability", Name) { } diff --git a/include/modules/cloak.h b/include/modules/cloak.h index 38381a25a..b6b76ab90 100644 --- a/include/modules/cloak.h +++ b/include/modules/cloak.h @@ -68,7 +68,7 @@ class Cloak::APIBase : public DataProvider { public: - APIBase(Module* parent) + APIBase(const WeakModulePtr& parent) : DataProvider(parent, "cloakapi") { } @@ -95,7 +95,7 @@ class Cloak::API final : public dynamic_reference<Cloak::APIBase> { public: - API(Module* parent) + API(const WeakModulePtr& parent) : dynamic_reference<Cloak::APIBase>(parent, "cloakapi") { } @@ -106,7 +106,7 @@ class Cloak::Engine : public DataProvider { protected: - Engine(Module* Creator, const std::string& Name) + Engine(const WeakModulePtr& Creator, const std::string& Name) : DataProvider(Creator, "Cloak::Engine", Name) { } diff --git a/include/modules/ctctags.h b/include/modules/ctctags.h index 7d0c94c20..3bc598cfb 100644 --- a/include/modules/ctctags.h +++ b/include/modules/ctctags.h @@ -34,7 +34,7 @@ class CTCTags::CapReference final : public Cap::Reference { public: - CapReference(Module* mod) + CapReference(const WeakModulePtr& mod) : Cap::Reference(mod, "message-tags") { } @@ -120,7 +120,7 @@ class CTCTags::EventListener : public Events::ModuleEventListener { protected: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "tagmsg", eventprio) { } @@ -172,7 +172,7 @@ private: CapReference ctctagcap; public: - TagProvider(Module* mod) + TagProvider(const WeakModulePtr& mod) : ClientProtocol::MessageTagProvider(mod) , ctctagcap(mod) { diff --git a/include/modules/dns.h b/include/modules/dns.h index da700f50d..8bd02a773 100644 --- a/include/modules/dns.h +++ b/include/modules/dns.h @@ -84,7 +84,7 @@ namespace DNS : public ModuleException { public: - Exception(const Module* mod, const std::string& message) + Exception(const WeakModulePtr& mod, const std::string& message) : ModuleException(mod, message) { } @@ -174,7 +174,7 @@ namespace DNS class Manager : public DataProvider { public: - Manager(Module* mod) + Manager(const WeakModulePtr& mod) : DataProvider(mod, "DNS") { } @@ -191,7 +191,7 @@ namespace DNS : public dynamic_reference<DNS::Manager> { public: - ManagerRef(Module* mod) + ManagerRef(const WeakModulePtr& mod) : dynamic_reference<DNS::Manager>(mod, "DNS") { } @@ -210,9 +210,9 @@ namespace DNS /* Request id */ RequestId id = 0; /* Creator of this request */ - Module* const creator; + const WeakModulePtr creator; - Request(Manager* mgr, Module* mod, const std::string& addr, QueryType qt, bool usecache = true, unsigned long timeout = 0) + Request(Manager* mgr, const WeakModulePtr& mod, const std::string& addr, QueryType qt, bool usecache = true, unsigned long timeout = 0) : Timer(timeout ? timeout : mgr->GetDefaultTimeout(), false) , manager(mgr) , question(addr, qt) diff --git a/include/modules/exemption.h b/include/modules/exemption.h index cfa171d68..cdfc7b0cb 100644 --- a/include/modules/exemption.h +++ b/include/modules/exemption.h @@ -29,7 +29,7 @@ class CheckExemption::EventListener : public Events::ModuleEventListener { protected: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "exemption", eventprio) { } @@ -49,7 +49,7 @@ class CheckExemption::EventProvider final : public Events::ModuleEventProvider { public: - EventProvider(Module* mod) + EventProvider(const WeakModulePtr& mod) : ModuleEventProvider(mod, "exemption") { } diff --git a/include/modules/extban.h b/include/modules/extban.h index 4f807d844..3bd768bb2 100644 --- a/include/modules/extban.h +++ b/include/modules/extban.h @@ -121,7 +121,7 @@ protected: /** Initializes an instance of the ExtBan::Base class. * @param mod The module which created this instance. */ - Manager(Module* mod) + Manager(const WeakModulePtr& mod) : DataProvider(mod, "extbanmanager") { } @@ -207,7 +207,7 @@ class ExtBan::ManagerRef final : public dynamic_reference_nocheck<ExtBan::Manager> { public: - ManagerRef(Module* mod) + ManagerRef(const WeakModulePtr& mod) : dynamic_reference_nocheck<ExtBan::Manager>(mod, "extbanmanager") { } @@ -245,7 +245,7 @@ protected: * @param xbletter The character used in bans to signify this extban. * @param xbmatchflags The flags used for matching. */ - Base(Module* mod, const std::string& xbname, ExtBan::Letter xbletter, uint8_t xbmatchflags = ExtBan::MATCH_DEFAULT) + Base(const WeakModulePtr& mod, const std::string& xbname, ExtBan::Letter xbletter, uint8_t xbmatchflags = ExtBan::MATCH_DEFAULT) : ServiceProvider(mod, "ExtBan::Base", xbname) , letter(ServerInstance->Config->ConfValue("extbans")->getCharacter(xbname, xbletter, true)) , manager(mod, "extbanmanager") @@ -345,7 +345,7 @@ protected: * @param xbletter The character used in bans to signify this extban. * @param xbmatchflags The flags used for matching. */ - ActingBase(Module* mod, const std::string& xbname, ExtBan::Letter xbletter, uint8_t xbmatchflags = ExtBan::MATCH_DEFAULT) + ActingBase(const WeakModulePtr& mod, const std::string& xbname, ExtBan::Letter xbletter, uint8_t xbmatchflags = ExtBan::MATCH_DEFAULT) : Base(mod, xbname, xbletter, xbmatchflags | ExtBan::MATCH_REQUIRE_CHANNEL) { } @@ -391,7 +391,7 @@ public: * @param xbname The name used in bans to signify this extban. * @param xbletter The character used in bans to signify this extban. */ - Acting(Module* mod, const std::string& xbname, ExtBan::Letter xbletter) + Acting(const WeakModulePtr& mod, const std::string& xbname, ExtBan::Letter xbletter) : ActingBase(mod, xbname, xbletter) { } @@ -423,7 +423,7 @@ protected: * @param xbletter The character used in bans to signify this extban. * @param xbmatchflags The flags used for matching. */ - MatchingBase(Module* mod, const std::string& xbname, ExtBan::Letter xbletter, uint8_t xbmatchflags = ExtBan::MATCH_DEFAULT) + MatchingBase(const WeakModulePtr& mod, const std::string& xbname, ExtBan::Letter xbletter, uint8_t xbmatchflags = ExtBan::MATCH_DEFAULT) : Base(mod, xbname, xbletter, xbmatchflags) { } @@ -441,7 +441,7 @@ class ExtBan::EventListener : public Events::ModuleEventListener { protected: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "extban", eventprio) { } diff --git a/include/modules/geolocation.h b/include/modules/geolocation.h index fffc8489d..0d267d19c 100644 --- a/include/modules/geolocation.h +++ b/include/modules/geolocation.h @@ -32,7 +32,7 @@ class Geolocation::APIBase : public DataProvider { public: - APIBase(Module* parent) + APIBase(const WeakModulePtr& parent) : DataProvider(parent, "geolocationapi") { } @@ -54,7 +54,7 @@ class Geolocation::API final : public dynamic_reference<Geolocation::APIBase> { public: - API(Module* parent) + API(const WeakModulePtr& parent) : dynamic_reference<Geolocation::APIBase>(parent, "geolocationapi") { } diff --git a/include/modules/hash.h b/include/modules/hash.h index fff5eb602..3f325be03 100644 --- a/include/modules/hash.h +++ b/include/modules/hash.h @@ -88,7 +88,7 @@ public: * @param ds The byte size of the resulting digest or 0 if it is variable length. * @param bs The byte size of the block cipher or 0 if not a block cipher. */ - Provider(Module* mod, const std::string& algorithm, size_t ds = 0, size_t bs = 0) + Provider(const WeakModulePtr& mod, const std::string& algorithm, size_t ds = 0, size_t bs = 0) : DataProvider(mod, "Hash::Provider", algorithm) , block_size(bs) , digest_size(ds) @@ -154,7 +154,7 @@ public: * @param mod The module that created this reference. * @param algorithm The name of the hash algorithm. */ - ProviderRef(Module* mod, const std::string& algorithm) + ProviderRef(const WeakModulePtr& mod, const std::string& algorithm) : dynamic_reference_nocheck<Hash::Provider>(mod, "Hash::Provider", algorithm) { } @@ -229,7 +229,7 @@ public: * @param mod The module that created this provider. * @param algorithm The name of the hash algorithm. */ - HMACProvider(Module* mod, const std::string& algorithm) + HMACProvider(const WeakModulePtr& mod, const std::string& algorithm) : Hash::Provider(mod, FMT::format("hmac-{}", algorithm)) , provider(mod, algorithm) { diff --git a/include/modules/httpd.h b/include/modules/httpd.h index 0f5c62db0..69021f123 100644 --- a/include/modules/httpd.h +++ b/include/modules/httpd.h @@ -248,7 +248,7 @@ class HTTPDocumentResponse final public: /** Module that generated this reply */ - Module* const module; + const WeakModulePtr module; std::stringstream* document; unsigned int responsecode; @@ -266,7 +266,7 @@ public: * @param response A valid HTTP/1.0 or HTTP/1.1 response code. The response text will be determined for you * based upon the response code. */ - HTTPDocumentResponse(Module* mod, HTTPRequest& req, std::stringstream* doc, unsigned int response) + HTTPDocumentResponse(const WeakModulePtr& mod, HTTPRequest& req, std::stringstream* doc, unsigned int response) : module(mod) , document(doc) , responsecode(response) @@ -279,7 +279,7 @@ class HTTPdAPIBase : public DataProvider { public: - HTTPdAPIBase(Module* parent) + HTTPdAPIBase(const WeakModulePtr& parent) : DataProvider(parent, "m_httpd_api") { } @@ -297,7 +297,7 @@ class HTTPdAPI final : public dynamic_reference<HTTPdAPIBase> { public: - HTTPdAPI(Module* parent) + HTTPdAPI(const WeakModulePtr& parent) : dynamic_reference<HTTPdAPIBase>(parent, "m_httpd_api") { } @@ -307,7 +307,7 @@ class HTTPACLEventListener : public Events::ModuleEventListener { public: - HTTPACLEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + HTTPACLEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "http-acl", eventprio) { } @@ -319,7 +319,7 @@ class HTTPRequestEventListener : public Events::ModuleEventListener { public: - HTTPRequestEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + HTTPRequestEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "http-request", eventprio) { } diff --git a/include/modules/invite.h b/include/modules/invite.h index 15fac9c0b..5ac8c3b1f 100644 --- a/include/modules/invite.h +++ b/include/modules/invite.h @@ -50,7 +50,7 @@ class Invite::APIBase : public DataProvider { public: - APIBase(Module* parent); + APIBase(const WeakModulePtr& parent); /** Create or extend an Invite. * When a user is invited to join a channel either a new Invite object is created or @@ -97,7 +97,7 @@ class Invite::API final : public dynamic_reference<APIBase> { public: - API(Module* parent) + API(const WeakModulePtr& parent) : dynamic_reference<APIBase>(parent, "core_channel_invite") { } diff --git a/include/modules/ircv3.h b/include/modules/ircv3.h index 0d10b8b98..3d3e17120 100644 --- a/include/modules/ircv3.h +++ b/include/modules/ircv3.h @@ -131,7 +131,7 @@ public: * See Cap::Capability for more info on client capabilities. * @param Tagname Name of the message tag, to use in the protocol. */ - CapTag(Module* mod, const std::string& capname, const std::string& Tagname) + CapTag(const WeakModulePtr& mod, const std::string& capname, const std::string& Tagname) : ClientProtocol::MessageTagProvider(mod) , cap(mod, capname) , tagname(Tagname) @@ -147,7 +147,7 @@ class IRCv3::ReplyCapReference final : public Cap::Reference { public: - ReplyCapReference(Module* mod) + ReplyCapReference(const WeakModulePtr& mod) : Cap::Reference(mod, "standard-replies") { } diff --git a/include/modules/ircv3_batch.h b/include/modules/ircv3_batch.h index bff4a25f3..6c5589346 100644 --- a/include/modules/ircv3_batch.h +++ b/include/modules/ircv3_batch.h @@ -50,7 +50,7 @@ public: /** Constructor. * @param mod Module that owns the Manager. */ - Manager(Module* mod) + Manager(const WeakModulePtr& mod) : DataProvider(mod, "batchapi") , ClientProtocol::MessageTagProvider(mod) { @@ -176,7 +176,7 @@ class IRCv3::Batch::API final : public dynamic_reference_nocheck<Manager> { public: - API(Module* mod) + API(const WeakModulePtr& mod) : dynamic_reference_nocheck<Manager>(mod, "batchapi") { } @@ -189,7 +189,7 @@ class IRCv3::Batch::CapReference final : public Cap::Reference { public: - CapReference(Module* mod) + CapReference(const WeakModulePtr& mod) : Cap::Reference(mod, "batch") { } diff --git a/include/modules/ircv3_servertime.h b/include/modules/ircv3_servertime.h index 656beb4d7..84de8ac75 100644 --- a/include/modules/ircv3_servertime.h +++ b/include/modules/ircv3_servertime.h @@ -58,7 +58,7 @@ public: /** Constructor. * @param mod Module that owns the Manager. */ - Manager(Module* mod) + Manager(const WeakModulePtr& mod) : DataProvider(mod, "servertimeapi") { } @@ -99,7 +99,7 @@ class IRCv3::ServerTime::API final : public dynamic_reference_nocheck<Manager> { public: - API(Module* mod) + API(const WeakModulePtr& mod) : dynamic_reference_nocheck<Manager>(mod, "servertimeapi") { } diff --git a/include/modules/isupport.h b/include/modules/isupport.h index 40ddacdbe..6b295fbd1 100644 --- a/include/modules/isupport.h +++ b/include/modules/isupport.h @@ -38,7 +38,7 @@ class ISupport::EventListener : public Events::ModuleEventListener { protected: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "isupport", eventprio) { } @@ -53,7 +53,7 @@ class ISupport::EventProvider final : public Events::ModuleEventProvider { public: - EventProvider(Module* mod) + EventProvider(const WeakModulePtr& mod) : Events::ModuleEventProvider(mod, "isupport") { } diff --git a/include/modules/ldap.h b/include/modules/ldap.h index d7399eb03..7a364f64e 100644 --- a/include/modules/ldap.h +++ b/include/modules/ldap.h @@ -127,9 +127,9 @@ struct LDAPResult final class LDAPInterface { public: - const reference<Module> creator; + const WeakModulePtr creator; - LDAPInterface(Module* m) + LDAPInterface(const WeakModulePtr& m) : creator(m) { } @@ -142,7 +142,7 @@ class LDAPProvider : public DataProvider { public: - LDAPProvider(Module* Creator, const std::string& Name) + LDAPProvider(const WeakModulePtr& Creator, const std::string& Name) : DataProvider(Creator, "LDAPProvider", Name) { } diff --git a/include/modules/monitor.h b/include/modules/monitor.h index 7e99e3705..d256a3031 100644 --- a/include/modules/monitor.h +++ b/include/modules/monitor.h @@ -32,7 +32,7 @@ class Monitor::APIBase : public DataProvider { public: - APIBase(Module* parent) + APIBase(const WeakModulePtr& parent) : DataProvider(parent, "monitor") { } @@ -49,7 +49,7 @@ class Monitor::API final : public dynamic_reference<Monitor::APIBase> { public: - API(Module* parent) + API(const WeakModulePtr& parent) : dynamic_reference<Monitor::APIBase>(parent, "monitor") { } diff --git a/include/modules/names.h b/include/modules/names.h index 36fd6a4e2..d1a9d7d08 100644 --- a/include/modules/names.h +++ b/include/modules/names.h @@ -28,7 +28,7 @@ class Names::EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "names", eventprio) { } diff --git a/include/modules/regex.h b/include/modules/regex.h index 94cee3070..0f773a746 100644 --- a/include/modules/regex.h +++ b/include/modules/regex.h @@ -60,7 +60,7 @@ protected: * @param Creator The module which created this instance. * @param Name The name of this regular expression engine. */ - Engine(Module* Creator, const std::string& Name) + Engine(const WeakModulePtr& Creator, const std::string& Name) : DataProvider(Creator, "Regex::Engine", Name) { } @@ -87,7 +87,7 @@ class Regex::SimpleEngine final { public: /** @copydoc Regex::Engine::Engine */ - SimpleEngine(Module* Creator, const std::string& Name) + SimpleEngine(const WeakModulePtr& Creator, const std::string& Name) : Regex::Engine(Creator, Name) { } @@ -108,7 +108,7 @@ public: * @param Creator The module which created this instance. * @param Name The name of the regular expression engine to reference. */ - EngineReference(Module* Creator, const std::string& Name = "") + EngineReference(const WeakModulePtr& Creator, const std::string& Name = "") : dynamic_reference_nocheck<Engine>(Creator, "Regex::Engine", Name) { } @@ -132,7 +132,7 @@ public: * @param regex A regular expression which failed to compile. * @param error The error which occurred whilst compiling the regular expression. */ - Exception(const Module* mod, const std::string& regex, const std::string& error) + Exception(const WeakModulePtr& mod, const std::string& regex, const std::string& error) : ModuleException(mod, FMT::format("Error in regex '{}': {}", regex, error)) { } @@ -143,7 +143,7 @@ public: * @param error The error which occurred whilst compiling the regular expression. * @param offset The offset at which the errror occurred. */ - Exception(const Module* mod, const std::string& regex, const std::string& error, size_t offset) + Exception(const WeakModulePtr& mod, const std::string& regex, const std::string& error, size_t offset) : ModuleException(mod, FMT::format("Error in regex '{}' at offset {}: {}", regex, offset, error)) { } diff --git a/include/modules/reload.h b/include/modules/reload.h index 1670237dd..0076de463 100644 --- a/include/modules/reload.h +++ b/include/modules/reload.h @@ -61,7 +61,7 @@ namespace ReloadModule class EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "reloadmodule", eventprio) { } @@ -71,13 +71,13 @@ namespace ReloadModule * @param mod Module to be reloaded * @param cd CustomData instance that can store your data once. */ - virtual void OnReloadModuleSave(Module* mod, CustomData& cd) = 0; + virtual void OnReloadModuleSave(const ModulePtr& mod, CustomData& cd) = 0; /** Restore data after a reload. Only called if data was added in OnReloadModuleSave(). * @param mod Reloaded module, if NULL the reload failed and the module no longer exists * @param data Pointer that was passed to CustomData::add() in OnReloadModuleSave() at the time when the module's state * was saved */ - virtual void OnReloadModuleRestore(Module* mod, void* data) = 0; + virtual void OnReloadModuleRestore(const ModulePtr& mod, void* data) = 0; }; } diff --git a/include/modules/server.h b/include/modules/server.h index c48885a00..31eda8ee1 100644 --- a/include/modules/server.h +++ b/include/modules/server.h @@ -44,7 +44,7 @@ class ServerProtocol::LinkEventListener : public Events::ModuleEventListener { public: - LinkEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + LinkEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "server-link", eventprio) { } @@ -70,7 +70,7 @@ class ServerProtocol::MessageEventListener : public Events::ModuleEventListener { public: - MessageEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + MessageEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "server-message", eventprio) { } @@ -87,7 +87,7 @@ class ServerProtocol::RouteEventListener : public Events::ModuleEventListener { public: - RouteEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + RouteEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "server-route", eventprio) { } @@ -106,7 +106,7 @@ class ServerProtocol::SyncEventListener : public Events::ModuleEventListener { public: - SyncEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + SyncEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "server-sync", eventprio) { } diff --git a/include/modules/sql.h b/include/modules/sql.h index beb37e233..daac2fd82 100644 --- a/include/modules/sql.h +++ b/include/modules/sql.h @@ -171,13 +171,13 @@ class SQL::Query { protected: /** Creates a new SQL query. */ - Query(Module* Creator) + Query(const WeakModulePtr& Creator) : creator(Creator) { } public: - const reference<Module> creator; + const WeakModulePtr creator; /** Called when an SQL error happens. * @param error The error that occurred. @@ -201,7 +201,7 @@ private: const std::string dbid; public: - Provider(Module* Creator, const std::string& Name) + Provider(const WeakModulePtr& Creator, const std::string& Name) : DataProvider(Creator, "SQL::Provider", Name) , dbid(Name) { diff --git a/include/modules/ssl.h b/include/modules/ssl.h index e1649cc98..19f4156cc 100644 --- a/include/modules/ssl.h +++ b/include/modules/ssl.h @@ -168,7 +168,7 @@ class SSLIOHookProvider : public IOHookProvider { public: - SSLIOHookProvider(Module* mod, const std::string& Name) + SSLIOHookProvider(const WeakModulePtr& mod, const std::string& Name) : IOHookProvider(mod, "ssl/" + Name, IOH_SSL) { } @@ -283,7 +283,7 @@ class UserCertificateAPIBase : public DataProvider { public: - UserCertificateAPIBase(Module* parent) + UserCertificateAPIBase(const WeakModulePtr& parent) : DataProvider(parent, "m_sslinfo_api") { } @@ -340,7 +340,7 @@ class UserCertificateAPI final : public dynamic_reference<UserCertificateAPIBase> { public: - UserCertificateAPI(Module* parent) + UserCertificateAPI(const WeakModulePtr& parent) : dynamic_reference<UserCertificateAPIBase>(parent, "m_sslinfo_api") { } diff --git a/include/modules/stats.h b/include/modules/stats.h index a8d446d64..6653448af 100644 --- a/include/modules/stats.h +++ b/include/modules/stats.h @@ -37,7 +37,7 @@ class Stats::EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "stats", eventprio) { } diff --git a/include/modules/webirc.h b/include/modules/webirc.h index 64c480097..67d4d862e 100644 --- a/include/modules/webirc.h +++ b/include/modules/webirc.h @@ -30,7 +30,7 @@ class WebIRC::EventListener : public Events::ModuleEventListener { protected: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "webirc", eventprio) { } diff --git a/include/modules/who.h b/include/modules/who.h index ab7918fd2..b8bc51e6f 100644 --- a/include/modules/who.h +++ b/include/modules/who.h @@ -32,7 +32,7 @@ class Who::EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "who", eventprio) { } @@ -53,7 +53,7 @@ class Who::MatchEventListener : public Events::ModuleEventListener { public: - MatchEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + MatchEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "who-match", eventprio) { } @@ -72,7 +72,7 @@ class Who::VisibleEventListener : public Events::ModuleEventListener { public: - VisibleEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + VisibleEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "who-visible", eventprio) { } diff --git a/include/modules/whois.h b/include/modules/whois.h index bbd23db71..46c6a3eea 100644 --- a/include/modules/whois.h +++ b/include/modules/whois.h @@ -65,7 +65,7 @@ class Whois::EventListener : public Events::ModuleEventListener { public: - EventListener(Module* mod, unsigned int eventprio = DefaultPriority) + EventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "whois", eventprio) { } @@ -80,7 +80,7 @@ class Whois::LineEventListener : public Events::ModuleEventListener { public: - LineEventListener(Module* mod, unsigned int eventprio = DefaultPriority) + LineEventListener(const WeakModulePtr& mod, unsigned int eventprio = DefaultPriority) : ModuleEventListener(mod, "whoisline", eventprio) { } diff --git a/include/parammode.h b/include/parammode.h index b8e8e21d4..fc7e11f16 100644 --- a/include/parammode.h +++ b/include/parammode.h @@ -27,7 +27,7 @@ private: virtual void OnUnsetInternal(User* source, Channel* chan) = 0; public: - ParamModeBase(Module* Creator, const std::string& Name, char modeletter, ParamSpec ps) + ParamModeBase(const WeakModulePtr& Creator, const std::string& Name, char modeletter, ParamSpec ps) : ModeHandler(Creator, Name, modeletter, ps, MODETYPE_CHANNEL, MC_PARAM) { } /** @copydoc ModeHandler::OnModeChange */ @@ -59,7 +59,7 @@ public: * @param modeletter The mode letter of this mode * @param ps The parameter type of this mode, one of ParamSpec */ - ParamMode(Module* Creator, const std::string& Name, char modeletter, ParamSpec ps = PARAM_SETONLY) + ParamMode(const WeakModulePtr& Creator, const std::string& Name, char modeletter, ParamSpec ps = PARAM_SETONLY) : ParamModeBase(Creator, Name, modeletter, ps) , ext(Creator, "param-mode-" + Name, ExtensionType::CHANNEL) { diff --git a/include/service.h b/include/service.h index 09817537c..c7dacd906 100644 --- a/include/service.h +++ b/include/service.h @@ -25,7 +25,7 @@ class CoreExport ServiceProvider { public: /** Module that created this service */ - const reference<Module> service_creator; + const WeakModulePtr service_creator; /** Name of the service being provided */ const std::string service_name; @@ -33,7 +33,7 @@ public: /** Type of service (must match object type) */ const std::string service_type; - ServiceProvider(Module* mod, const std::string& stype, const std::string& sname); + ServiceProvider(const WeakModulePtr& mod, const std::string& stype, const std::string& sname); /** Register this service in the appropriate registrar. */ virtual void RegisterService(); @@ -53,7 +53,7 @@ class CoreExport DataProvider : public ServiceProvider { public: - DataProvider(Module* mod, const std::string& stype, const std::string& sname = ""); + DataProvider(const WeakModulePtr& mod, const std::string& stype, const std::string& sname = ""); /** @copydoc ServiceProvider::RegisterService */ void RegisterService() override; diff --git a/include/socket.h b/include/socket.h index e869852b1..744b1975e 100644 --- a/include/socket.h +++ b/include/socket.h @@ -205,7 +205,7 @@ public: { public: IOHookProvRef() - : dynamic_reference_nocheck<IOHookProvider>(nullptr, "IOHookProvider", std::string(), true) + : dynamic_reference_nocheck<IOHookProvider>({}, "IOHookProvider", std::string(), true) { } }; diff --git a/include/streamsocket.h b/include/streamsocket.h index 60f21e97e..82c87db1a 100644 --- a/include/streamsocket.h +++ b/include/streamsocket.h @@ -360,7 +360,7 @@ public: * @param mod Module whose IOHook to return * @return IOHook belonging to the module or NULL if the module haven't attached an IOHook to this socket */ - IOHook* GetModHook(Module* mod) const; + IOHook* GetModHook(const ModulePtr& mod) const; /** Get the last IOHook attached to this socket * @return The last IOHook attached to this socket or NULL if no IOHooks are attached diff --git a/include/typedefs.h b/include/typedefs.h index dff831d32..511153b39 100644 --- a/include/typedefs.h +++ b/include/typedefs.h @@ -76,3 +76,9 @@ namespace ClientProtocol /** A bitset of characters which are enabled/set. */ using CharState = std::bitset<UCHAR_MAX + 1>; + +/** A pointer to a module instance. */ +using ModulePtr = std::shared_ptr<Module>; + +/** A weak pointer to a module instance. */ +using WeakModulePtr = std::weak_ptr<Module>; diff --git a/include/users.h b/include/users.h index 6b47c4d50..4eeedfe1d 100644 --- a/include/users.h +++ b/include/users.h @@ -863,7 +863,7 @@ public: * @param mod Module whose I/O hook to return. * @return An I/O hook or nullptr if no hook is attached. */ - virtual IOHook* GetModHook(Module* mod) const { return nullptr; } + virtual IOHook* GetModHook(const ModulePtr& mod) const { return nullptr; } /** Called to write an IRC message to the I/O handler. * @param msg The message to write. diff --git a/include/utility/pointer.h b/include/utility/pointer.h new file mode 100644 index 000000000..2e356ae2b --- /dev/null +++ b/include/utility/pointer.h @@ -0,0 +1,42 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2026 Sadie Powell <sadie@witchery.services> + * + * This file is part of InspIRCd. InspIRCd is free software: you can + * redistribute it and/or modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation, version 2. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS + * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more + * details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + + +#pragma once + +namespace insp +{ + /** Determines if the pointer in \p ptr1 is empty. + * @param ptr Either a shared_ptr<> or a weak_ptr<>. + */ + template <typename Ptr> + bool empty_ptr(const Ptr& ptr) + { + return same_ptr(ptr, {}); + } + + /** Determines if the pointer in \p ptr1 points to the same value as the pointer in \p ptr2. + * @param ptr1 Either a shared_ptr<> or a weak_ptr<>. + * @param ptr2 Either a shared_ptr<> or a weak_ptr<>. + */ + template <typename Ptr1, typename Ptr2 = Ptr1> + bool same_ptr(const Ptr1& ptr1, const Ptr2& ptr2) + { + return !ptr1.owner_before(ptr2) && !ptr2.owner_before(ptr1); + } +} |
