]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dynamic.h
4b8c7a4562ab39cbc9b3c9e597c18ee1dd0e362c
[user/henk/code/inspircd.git] / include / dynamic.h
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 /** The DLLManager class is able to load a module file by filename,
18  * and locate its init_module symbol.
19  */
20 class CoreExport DLLManager
21 {
22  protected:
23
24         /** The last error string, or NULL
25          */
26         const char *err;
27
28  public:
29         /** This constructor loads the module using dlopen()
30          * @param ServerInstance The creator class of this object
31          * @param fname The filename to load. This should be within
32          * the modules dir.
33          */
34         DLLManager(InspIRCd* ServerInstance, const char *fname);
35         virtual ~DLLManager();
36
37         /** Get a symbol using dynamic linking.
38          * @param v A function pointer, pointing at an init_module function
39          * @param sym_name The symbol name to find, usually "init_module"
40          * @return true if the symbol can be found, also the symbol will be put into v.
41          */
42         bool GetSymbol(void **v, const char *sym_name);
43
44         /** Get the last error from dlopen() or dlsym().
45          * @return The last error string, or NULL if no error has occured.
46          */
47         const char* LastError()
48         {
49                  return err;
50         }
51
52         /** The module handle.
53          * This is OS dependent, on POSIX platforms it is a pointer to a function
54          * pointer (yes, really!) and on windows it is a library handle.
55          */
56         void *h;
57 };
58
59 class CoreExport LoadModuleException : public CoreException
60 {
61  public:
62         /** This constructor can be used to specify an error message before throwing.
63          */
64         LoadModuleException(const std::string &message)
65         : CoreException(message, "the core")
66         {
67         }
68
69         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
70          * Actually no, it does nothing. Never mind.
71          * @throws Nothing!
72          */
73         virtual ~LoadModuleException() throw() {};
74 };
75
76 class CoreExport FindSymbolException : public CoreException
77 {
78  public:
79         /** This constructor can be used to specify an error message before throwing.
80          */
81         FindSymbolException(const std::string &message)
82         : CoreException(message, "the core")
83         {
84         }
85
86         /** This destructor solves world hunger, cancels the world debt, and causes the world to end.
87          * Actually no, it does nothing. Never mind.
88          * @throws Nothing!
89          */
90         virtual ~FindSymbolException() throw() {};
91 };
92
93 /** This is the highest-level class of the DLLFactory system used to load InspIRCd modules and commands.
94  * All the dirty mucking around with dl*() is done by DLLManager, all this does it put a pretty shell on
95  * it and make it nice to use to load modules and core commands. This class is quite specialised for these
96  * two uses and it may not be useful more generally -- use DLLManager directly for that.
97  */
98 template <typename ReturnType> class CoreExport DLLFactory : public DLLManager
99 {
100  protected:
101         /** This typedef represents the init_* function within each module or command.
102          * The init_module function is the only exported extern "C" declaration
103          * in any module file. In a cmd_*.cpp file the equivilant is init_command
104          */
105         typedef ReturnType * (initfunctype) (InspIRCd*);
106
107         /** Pointer to the init function.
108          */
109         initfunctype* init_func;
110
111         /** Instance pointer to be passed to init_*() when it is called.
112          */
113         InspIRCd* ServerInstance;
114
115  public:
116         /** Default constructor.
117          * This constructor passes its paramerers down through DLLFactoryBase and then DLLManager
118          * to load the module, then calls the factory function to retrieve a pointer to a ModuleFactory
119          * class. It is then down to the core to call the ModuleFactory::CreateModule() method and
120          * receive a Module* which it can insert into its module lists.
121          */
122         DLLFactory(InspIRCd* Instance, const char *fname, const char *func_name)
123         : DLLManager(Instance, fname), init_func(NULL), ServerInstance(Instance)
124         {
125                 const char* error = LastError();
126
127                 if(!error)
128                 {
129                         if(!GetSymbol((void **)&init_func, func_name))
130                         {
131                                 throw FindSymbolException("Missing " + std::string(func_name) + "() entrypoint!");
132                         }
133                 }
134                 else
135                 {
136                         throw LoadModuleException(error);
137                 }
138         }
139
140         /** Calls the 'init_module' C exported function within a module, which
141          * returns a pointer to a Module derived object.
142          */
143         ReturnType* CallInit()
144         {
145                 if(init_func)
146                 {
147                         return init_func(ServerInstance);
148                 }
149                 else
150                 {
151                         return NULL;
152                 }
153         }
154
155         /** The destructor deletes the ModuleFactory pointer.
156          */
157         ~DLLFactory()
158         {
159         }
160 };
161
162 #endif
163