00001
00002
00003
00004
00005
00006
00007
00008 #ifndef PLUGIN_H
00009 #define PLUGIN_H
00010
00011
00012
00013
00014 #include "SugoiTools\exceptions.h"
00015 #define NOMINMAX
00016 #include <windows.h>
00017
00018 namespace SGE{
00019
00020
00021
00022 template <class T>
00023 class CPlugin
00024 {
00025 public :
00026
00027
00028
00029
00030 CPlugin();
00031
00032
00033
00034
00035 ~CPlugin();
00036
00037
00038
00039
00040 T* Load(const std::string& Filename);
00041 FctType* template <typename FctType>Load (const std::string& FunctionName);
00042
00043 private :
00044
00045
00046
00047 typedef T* (*PtrFunc)();
00048
00049
00050
00051
00052 HMODULE m_Library;
00053 std::string LibName;
00054 };
00055
00056 template <class T>
00057 inline CPlugin<T>::CPlugin() :
00058 m_Library(NULL)
00059 {
00060
00061 }
00062
00063 template <class T>
00064 inline CPlugin<T>::~CPlugin()
00065 {
00066 if (m_Library)
00067 FreeLibrary(m_Library);
00068 }
00069
00070 template <class T>
00071 HMODULE CPlugin<T>::Load(const std::string& Filename)
00072 {
00073
00074 m_Library = LoadLibrary(Filename.c_str());
00075 if (!m_Library)
00076 throw CLoadingFailed(Filename, "Impossible de charger la bibliothèque dynamique");
00077 return m_Library;
00078 }
00079
00080 template <class T>
00081 inline T* CPlugin<T>::CreateObj(const std::string& ObjName)
00082 {
00083 SG_Assert(m_Library, "You must load the library first");
00084
00085 PtrFunc Function = reinterpret_cast<PtrFunc>(GetProcAddress(m_Library, "CreatePluginObj"));
00086 if (!Function)
00087 SG_Assert(Function, "Could not find the function");
00088
00089 return Function(ObjName);
00090 }
00091
00092
00093
00094
00095 };
00096 #endif // PLUGIN_H