dynamic.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014 #ifndef __DLL_H
00015 #define __DLL_H
00016
00020 class CoreExport DLLManager
00021 {
00022 protected:
00023
00026 const char *err;
00027
00028 public:
00034 DLLManager(InspIRCd* ServerInstance, const char *fname);
00035 virtual ~DLLManager();
00036
00042 bool GetSymbol(void **v, const char *sym_name);
00043
00047 const char* LastError()
00048 {
00049 return err;
00050 }
00051
00056 void *h;
00057 };
00058
00059 class CoreExport LoadModuleException : public CoreException
00060 {
00061 public:
00064 LoadModuleException(const std::string &message)
00065 : CoreException(message, "the core")
00066 {
00067 }
00068
00073 virtual ~LoadModuleException() throw() {};
00074 };
00075
00076 class CoreExport FindSymbolException : public CoreException
00077 {
00078 public:
00081 FindSymbolException(const std::string &message)
00082 : CoreException(message, "the core")
00083 {
00084 }
00085
00090 virtual ~FindSymbolException() throw() {};
00091 };
00092
00098 template <typename ReturnType> class CoreExport DLLFactory : public DLLManager
00099 {
00100 protected:
00105 typedef ReturnType * (initfunctype) (InspIRCd*);
00106
00109 initfunctype* init_func;
00110
00113 InspIRCd* ServerInstance;
00114
00115 public:
00122 DLLFactory(InspIRCd* Instance, const char *fname, const char *func_name)
00123 : DLLManager(Instance, fname), init_func(NULL), ServerInstance(Instance)
00124 {
00125 const char* error = LastError();
00126
00127 if(!error)
00128 {
00129 if(!GetSymbol((void **)&init_func, func_name))
00130 {
00131 throw FindSymbolException("Missing " + std::string(func_name) + "() entrypoint!");
00132 }
00133 }
00134 else
00135 {
00136 throw LoadModuleException(error);
00137 }
00138 }
00139
00143 ReturnType* CallInit()
00144 {
00145 if(init_func)
00146 {
00147 return init_func(ServerInstance);
00148 }
00149 else
00150 {
00151 return NULL;
00152 }
00153 }
00154
00157 ~DLLFactory()
00158 {
00159 }
00160 };
00161
00162 #endif
00163