]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dynamic.h
Rename all these so that the names reflect the database server theyre used with more...
[user/henk/code/inspircd.git] / include / dynamic.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #ifndef __DLL_H
15 #define __DLL_H
16
17 /** This typedef represents the init_module function within each module.
18  * The init_module function is the only exported extern "C" declaration
19  * in any module file.
20  */
21 typedef void * (initfunc) (void);
22
23 #include "inspircd_config.h"
24
25 class InspIRCd;
26
27 /** The DLLManager class is able to load a module file by filename,
28  * and locate its init_module symbol.
29  */
30 class DLLManager
31 {
32  public:
33         /** This constructor loads the module using dlopen()
34          * @param ServerInstance The creator class of this object
35          * @param fname The filename to load. This should be within
36          * the modules dir.
37          */
38         DLLManager(InspIRCd* ServerInstance, const char *fname);
39         virtual ~DLLManager();
40
41
42 #ifdef STATIC_LINK
43         /** Get a symbol using static linking.
44          * @param v A static function pointer, pointing at an init_module function
45          * @param sym_name The symbol name to find, usually "init_module"
46          * @return True if the symbol can be found
47          */
48         bool GetSymbol(initfunc* &v, const char *sym_name);
49 #else
50         /** Get a symbol using dynamic linking.
51          * @param v A function pointer, pointing at an init_module function
52          * @param sym_name The symbol name to find, usually "init_module"
53          * @return true if the symbol can be found, also the symbol will be put into v.
54          */
55         bool GetSymbol(void **v, const char *sym_name);
56 #endif
57         /** Get the last error from dlopen() or dlsym().
58          * @return The last error string, or NULL if no error has occured
59          */
60         char* LastError() 
61         {
62                  return err;
63         }
64
65         /** The module handle
66          */
67         void *h;
68
69  protected:
70
71         /** The last error string, or NULL
72          */
73         char *err;
74 #ifdef STATIC_LINK
75
76         /** The module name
77          */
78         char staticname[1024];
79 #endif
80 };
81
82 /** This class is a specialized form of DLLManager designed to load InspIRCd modules.
83  * It's job is to call the init_module function and receive a factory pointer.
84  */
85 class DLLFactoryBase : public DLLManager
86 {
87  public:
88         /** Default constructor.
89          * This constructor loads a module file by calling its DLLManager subclass constructor,
90          * then finds the symbol using DLLManager::GetSymbol(), and calls the symbol,
91          * obtaining a valid pointer to the init_module function
92          */
93         DLLFactoryBase(InspIRCd* Instance, const char *fname, const char *func_name = 0);
94
95         /** Default destructor
96          */
97         virtual ~DLLFactoryBase();
98 #ifdef STATIC_LINK
99         /** A function pointer to the factory function
100          */
101         initfunc *factory_func;
102 #else
103         /** A function pointer to the factory function
104          */
105         void * (*factory_func)(void);   
106 #endif
107 };
108
109 /** This is the highest-level class of the DLLFactory system used to load InspIRCd modules.
110  * Its job is to finally call the init_module function and obtain a pointer to a ModuleFactory.
111  * This template is a container for ModuleFactory itself, so that it may 'plug' into ModuleFactory
112  * and provide module loading capabilities transparently.
113  */
114 template <class T> class DLLFactory : public DLLFactoryBase
115 {
116  public:
117         /** Default constructor.
118          * This constructor passes its paramerers down through DLLFactoryBase and then DLLManager
119          * to load the module, then calls the factory function to retrieve a pointer to a ModuleFactory
120          * class. It is then down to the core to call the ModuleFactory::CreateModule() method and
121          * receive a Module* which it can insert into its module lists.
122          */
123         DLLFactory(InspIRCd* Instance, const char *fname, const char *func_name=0) : DLLFactoryBase(Instance, fname, func_name)
124         {
125                 if (factory_func)
126                         factory = reinterpret_cast<T*>(factory_func());
127                 else
128                         factory = reinterpret_cast<T*>(-1);
129         }
130         
131         /** The destructor deletes the ModuleFactory pointer.
132          */
133         ~DLLFactory()
134         {
135                 if (factory)
136                         delete factory;
137         }
138
139         /** The ModuleFactory pointer.
140          */
141         T *factory;
142 };
143
144 #endif