diff options
Diffstat (limited to 'include')
-rw-r--r-- | include/dynamic.h | 63 | ||||
-rw-r--r-- | include/moduledefs.h | 47 | ||||
-rw-r--r-- | include/modules.h | 38 |
3 files changed, 87 insertions, 61 deletions
diff --git a/include/dynamic.h b/include/dynamic.h index 703120fc0..3a312a382 100644 --- a/include/dynamic.h +++ b/include/dynamic.h @@ -30,35 +30,34 @@ */ class CoreExport DLLManager : public classbase { - protected: - /** The last error string - */ + private: + /** The last error string. */ std::string err; - /** Sets the last error string - */ + /** The module library handle. */ +#ifdef _WIN32 + HMODULE lib; +#else + void* lib; +#endif + + /** The filename of the module library. */ + const std::string libname; + + /** Sets the last error string. */ void RetrieveLastError(); public: - /** This constructor loads the module using dlopen() - * @param fname The filename to load. This should be within - * the modules dir. - */ - DLLManager(const char *fname); - virtual ~DLLManager(); - - /** Get the last error from dlopen() or dlsym(). + /** Attempts to load the specified module. + * @param name The name of the library to load. */ - const std::string& LastError() - { - return err; - } + DLLManager(const std::string& name); - /** The module library handle. - */ - void *h; + /** Unloads the module if one was loaded. */ + ~DLLManager(); - /** Return a module by calling the init function + /** 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(); @@ -66,8 +65,24 @@ class CoreExport DLLManager : public classbase * @param name The name of the symbol to retrieve. * @return Either the value of the specified symbol or or NULL if it does not exist. */ - void* GetSymbol(const char* name); + void* GetSymbol(const char* name) const; + + /** Retrieves the value of the specified symbol and casts it to the requested type. + * @param name The name of the symbol to retrieve. + * @return Either the value of the specified symbol or or NULL if it does not exist. + */ + template <typename TReturn> + TReturn* GetSymbol(const char* name) const + { + return static_cast<TReturn*>(GetSymbol(name)); + } + + /** Retrieves the module version from the dynamic library. */ + const char* GetVersion() const { return GetSymbol<const char>(MODULE_STR_VERSION); } + + /** Retrieves the last error which occurred or an empty string if no errors have occurred. */ + const std::string& LastError() const { return err; } - /** Get detailed version information from the module file */ - std::string GetVersion(); + /** Retrieves the filename of the underlying shared library. */ + const std::string& LibraryName() const { return libname; } }; diff --git a/include/moduledefs.h b/include/moduledefs.h new file mode 100644 index 000000000..a2bac63cb --- /dev/null +++ b/include/moduledefs.h @@ -0,0 +1,47 @@ +/* + * InspIRCd -- Internet Relay Chat Daemon + * + * Copyright (C) 2020 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 + +class Module; + +/** The version of the InspIRCd ABI which is presently in use. */ +#define MODULE_ABI 3010 + +/** Stringifies the value of a symbol. */ +#define MODULE_STRINGIFY_SYM1(DEF) MODULE_STRINGIFY_SYM2(DEF) +#define MODULE_STRINGIFY_SYM2(DEF) #DEF + +/** The name of the symbol which contains the ABI that a module was compiled against. */ +#define MODULE_SYM_ABI inspircd_module_abi +#define MODULE_STR_ABI MODULE_STRINGIFY_SYM1(MODULE_SYM_ABI) + +/** The name of the symbol which creates a new Module instance. */ +#define MODULE_SYM_INIT inspircd_module_init +#define MODULE_STR_INIT MODULE_STRINGIFY_SYM1(MODULE_SYM_INIT) + +/** The name of the symbol which contains the version that a module was compiled against. */ +#define MODULE_SYM_VERSION inspircd_module_version +#define MODULE_STR_VERSION MODULE_STRINGIFY_SYM1(MODULE_SYM_VERSION) + +/** Defines the interface that a shared library must expose in order to be a module. */ +#define MODULE_INIT(klass) \ + extern "C" DllExport const uint32_t MODULE_SYM_ABI = MODULE_ABI; \ + extern "C" DllExport const char MODULE_SYM_VERSION[] = INSPIRCD_VERSION; \ + extern "C" DllExport Module* MODULE_SYM_INIT() { return new klass; } diff --git a/include/modules.h b/include/modules.h index 1c1f4a3ae..5e0c9ab07 100644 --- a/include/modules.h +++ b/include/modules.h @@ -30,13 +30,11 @@ #pragma once +#include "moduledefs.h" #include "dynamic.h" #include "base.h" #include "ctables.h" #include "inspsocket.h" -#include <string> -#include <deque> -#include <sstream> #include "timer.h" #include "mode.h" @@ -101,19 +99,6 @@ struct ModResult { } }; -/** InspIRCd major version. - * 1.2 -> 102; 2.1 -> 201; 2.12 -> 212 - */ -#define INSPIRCD_VERSION_MAJ 300 - -/** InspIRCd API version. - * If you change any API elements, increment this value. This counter should be - * reset whenever the major version is changed. Modules can use these two values - * and numerical comparisons in preprocessor macros if they wish to support - * multiple versions of InspIRCd in one file. - */ -#define INSPIRCD_VERSION_API 9 - /** * This #define allows us to call a method in all * loaded modules in a readable simple way, e.g.: @@ -1190,24 +1175,3 @@ class CoreExport ModuleManager : public fakederef<ModuleManager> */ void DelReferent(ServiceProvider* service); }; - -/** Do not mess with these functions unless you know the C preprocessor - * well enough to explain why they are needed. The order is important. - */ -#define MODULE_INIT_STR MODULE_INIT_STR_FN_2(MODULE_INIT_SYM) -#define MODULE_INIT_STR_FN_2(x) MODULE_INIT_STR_FN_1(x) -#define MODULE_INIT_STR_FN_1(x) #x -#define MODULE_INIT_SYM MODULE_INIT_SYM_FN_2(INSPIRCD_VERSION_MAJ, INSPIRCD_VERSION_API) -#define MODULE_INIT_SYM_FN_2(x,y) MODULE_INIT_SYM_FN_1(x,y) -#define MODULE_INIT_SYM_FN_1(x,y) inspircd_module_ ## x ## _ ## y - -/** This definition is used as shorthand for the various classes - * and functions needed to make a module loadable by the OS. - * It defines the class factory and external init_module function. - */ -#define MODULE_INIT(y) \ - extern "C" DllExport Module * MODULE_INIT_SYM() \ - { \ - return new y; \ - } \ - extern "C" DllExport const char inspircd_src_version[] = INSPIRCD_VERSION; |