]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - include/dynamic.h
Tons more sanity checks and length checks
[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(
57                        const char *fname,
58                        const char *func_name=0
59                        );
60                 
61         virtual ~DLLFactoryBase();
62         
63         void * (*factory_func)(void);   
64 };
65
66
67 //
68 // The DLLFactory template class inherits from DLLFactoryBase.
69 // The constructor takes the file name of the shared library
70 // and the undecorated "C" symbol name of the factory creator
71 // function.  The factory creator function in your shared library
72 // MUST either return a pointer to an object that is a subclass
73 // of 'T' or it must return 0.
74 //
75 // If everything is cool, then 'factory' will point to the
76 // requested factory class.  If not, it will be 0.
77 //
78 // Since the DLLFactory template ultimately inherits DLLManager,
79 // you can call LastError() to get any error code information
80 //
81 // The created factory is OWNED by the DLLFactory class.  
82 // The created factory will get deleted when the DLLFactory class
83 // is deleted, because the DLL will get unloaded as well.
84 //
85
86 template <class T>
87 class DLLFactory : public DLLFactoryBase
88 {
89  public:
90         DLLFactory(
91                    const char *fname,
92                    const char *func_name=0
93                    ) : DLLFactoryBase( fname, func_name )
94         {
95                 if( factory_func )
96                   factory = (T *)factory_func();
97                 else 
98                   factory = 0;
99         }
100         
101         ~DLLFactory()
102         {
103                 delete factory;
104         }
105
106         T *factory;
107 };
108
109
110
111
112
113
114 #endif