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