]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dynamic.h
Moved all command handler functions into commands.cpp/commands.h
[user/henk/code/inspircd.git] / include / dynamic.h
1 #ifndef __DLL_H
2 #define __DLL_H
3
4 //
5 // class DLLManager is the simple ELF C++ Library manager.
6 //
7 // It tries to dynamically load the specified shared library
8 // when it is construted.
9 //
10 // You should call LastError() before doing anything.  If it 
11 // returns NULL there is no error.
12 //
13
14
15 class DLLManager
16 {
17  public:
18         DLLManager( const char *fname );
19         virtual ~DLLManager();
20
21
22         bool GetSymbol( void **, const char *sym_name );
23
24         const char *LastError() 
25         {
26                  return err;
27         }
28         
29  protected:
30         void *h;
31         const char *err;
32 };
33
34
35 //
36 // class DLLFactoryBase is the base class used for the DLLFactory
37 // template class.  
38 // 
39 // It inherits from the DLLManager class and must be constructed with
40 // the file name of the shared library and the function name within that
41 // library which will create the desired C++ factory class.
42 // If you do not provide func_name to the constructor, it defaults to
43 // the undecorated "C" symbol "factory0"
44 //
45 // factory_func will be set to a pointer to the requested factory creator 
46 // function.  If there was an error linking to the shared library,
47 // factory_func will be 0.
48 //
49 // You can call 'LastError()' to find the error message that occurred.
50 //
51 //
52
53 class DLLFactoryBase : public DLLManager
54 {
55  public:
56         DLLFactoryBase(const char *fname, const char *func_name = 0);
57         virtual ~DLLFactoryBase();
58         void * (*factory_func)(void);   
59 };
60
61
62 //
63 // The DLLFactory template class inherits from DLLFactoryBase.
64 // The constructor takes the file name of the shared library
65 // and the undecorated "C" symbol name of the factory creator
66 // function.  The factory creator function in your shared library
67 // MUST either return a pointer to an object that is a subclass
68 // of 'T' or it must return 0.
69 //
70 // If everything is cool, then 'factory' will point to the
71 // requested factory class.  If not, it will be 0.
72 //
73 // Since the DLLFactory template ultimately inherits DLLManager,
74 // you can call LastError() to get any error code information
75 //
76 // The created factory is OWNED by the DLLFactory class.  
77 // The created factory will get deleted when the DLLFactory class
78 // is deleted, because the DLL will get unloaded as well.
79 //
80
81 template <class T> class DLLFactory : public DLLFactoryBase
82 {
83  public:
84         DLLFactory(const char *fname, const char *func_name=0) : DLLFactoryBase(fname,func_name)
85         {
86                 if (factory_func)
87                         factory = (T*)factory_func();
88                 else
89                         factory = 0;
90         }
91         
92         ~DLLFactory()
93         {
94                 delete factory;
95         }
96
97         T *factory;
98 };
99
100
101
102
103
104
105 #endif