iutil/plugin.h
Go to the documentation of this file.
00001 /* 00002 Copyright (C) 2001,2006 by Jorrit Tyberghein 00003 Copyright (C) 1999 by Andrew Zabolotny <bit@eltech.ru> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public 00016 License along with this library; if not, write to the Free 00017 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00018 */ 00019 00020 #ifndef __CS_IUTIL_PLUGIN_H__ 00021 #define __CS_IUTIL_PLUGIN_H__ 00022 00032 #include "csutil/scf.h" 00033 #include "iutil/objreg.h" 00034 #include "ivaria/reporter.h" 00035 00036 00037 struct iComponent; 00038 00042 struct iPluginIterator : public virtual iBase 00043 { 00044 SCF_INTERFACE(iPluginIterator, 2,0,0); 00046 virtual bool HasNext () = 0; 00048 virtual iBase* Next () = 0; 00049 }; 00050 00062 struct iPluginManager : public virtual iBase 00063 { 00064 SCF_INTERFACE(iPluginManager, 2,1,0); 00074 virtual iBase *LoadPlugin (const char *classID, 00075 bool init = true, bool report = true) = 0; 00076 00084 virtual iBase *QueryPlugin (const char *iInterface, int iVersion) = 0; 00086 virtual iBase *QueryPlugin (const char* classID, 00087 const char *iInterface, int iVersion) = 0; 00089 virtual bool UnloadPlugin (iComponent *obj) = 0; 00091 virtual bool RegisterPlugin (const char *classID, iComponent *obj) = 0; 00092 00098 virtual csPtr<iPluginIterator> GetPlugins () = 0; 00100 virtual void Clear () = 0; 00101 00108 virtual void QueryOptions (iComponent* object) = 0; 00109 }; 00110 00111 00120 template<class Interface> 00121 inline csPtr<Interface> csQueryPluginClass (iPluginManager *mgr, 00122 const char* ClassID) 00123 { 00124 iBase* base = mgr->QueryPlugin (ClassID, 00125 scfInterfaceTraits<Interface>::GetName(), 00126 scfInterfaceTraits<Interface>::GetVersion()); 00127 00128 if (base == 0) return csPtr<Interface> (0); 00129 00130 Interface *x = (Interface*)base->QueryInterface ( 00131 scfInterfaceTraits<Interface>::GetID (), 00132 scfInterfaceTraits<Interface>::GetVersion ()); 00133 00134 base->DecRef (); //release our base interface 00135 00136 return csPtr<Interface> (x); 00137 } 00138 00143 #define CS_QUERY_PLUGIN_CLASS(Object,ClassID,Interface) \ 00144 csQueryPluginClass<Interface> (Object, ClassID) 00145 00152 template<class Interface> 00153 inline csPtr<Interface> csLoadPlugin (iPluginManager *mgr, 00154 const char* ClassID, 00155 bool report = true) 00156 { 00157 iBase* base = mgr->LoadPlugin (ClassID, true, report); 00158 00159 if (base == 0) return csPtr<Interface> (0); 00160 00161 Interface *x = (Interface*)base->QueryInterface ( 00162 scfInterfaceTraits<Interface>::GetID (), 00163 scfInterfaceTraits<Interface>::GetVersion ()); 00164 00165 base->DecRef (); //release our base interface 00166 00167 return csPtr<Interface> (x); 00168 } 00169 00176 template<class Interface> 00177 inline csPtr<Interface> csLoadPlugin (iObjectRegistry* object_reg, 00178 const char* ClassID, 00179 bool report = true) 00180 { 00181 csRef<iPluginManager> mgr = csQueryRegistry<iPluginManager> (object_reg); 00182 if (!mgr) return 0; 00183 return csLoadPlugin<Interface> (mgr, ClassID, report); 00184 } 00185 00193 template<class Interface> 00194 inline csPtr<Interface> csLoadPluginCheck (iPluginManager *mgr, 00195 const char* ClassID, 00196 bool report = true) 00197 { 00198 csRef<Interface> i = csQueryPluginClass<Interface> (mgr, ClassID); 00199 if (i) return (csPtr<Interface>) i; 00200 i = csLoadPlugin<Interface> (mgr, ClassID, report); 00201 if (!i) return 0; 00202 return (csPtr<Interface>) i; 00203 } 00204 00213 template<class Interface> 00214 inline csPtr<Interface> csLoadPluginCheck (iObjectRegistry* object_reg, 00215 const char* ClassID, bool report = true) 00216 { 00217 csRef<iPluginManager> mgr = csQueryRegistry<iPluginManager> (object_reg); 00218 if (!mgr) 00219 { 00220 if (report) 00221 csReport (object_reg, CS_REPORTER_SEVERITY_ERROR, 00222 "crystalspace.plugin.load", "Couldn't find plugin manager!"); 00223 return 0; 00224 } 00225 csRef<Interface> i = csLoadPluginCheck<Interface> (mgr, ClassID, report); 00226 if (!i) 00227 { 00228 if (report) 00229 csReport (object_reg, CS_REPORTER_SEVERITY_ERROR, 00230 "crystalspace.plugin.load", "Couldn't load plugin with class '%s'!", 00231 ClassID); 00232 return 0; 00233 } 00234 return (csPtr<Interface>) i; 00235 } 00236 00241 inline csPtr<iBase> csLoadPluginAlways (iPluginManager *mgr, 00242 const char* ClassID, 00243 bool report = true) 00244 { 00245 iBase* base = mgr->LoadPlugin (ClassID, true, report); 00246 return csPtr<iBase> (base); 00247 } 00248 00270 template<class Interface> 00271 inline csPtr<Interface> csQueryRegistryOrLoad (iObjectRegistry *Reg, 00272 const char* classID, bool report = true) 00273 { 00274 csRef<Interface> i = csQueryRegistry<Interface> (Reg); 00275 if (i) return (csPtr<Interface>)i; 00276 csRef<iPluginManager> plugmgr = csQueryRegistry<iPluginManager> (Reg); 00277 if (!plugmgr) 00278 { 00279 if (report) 00280 csReport (Reg, CS_REPORTER_SEVERITY_ERROR, 00281 "crystalspace.plugin.query", "Plugin manager missing!"); 00282 return 0; 00283 } 00284 i = csLoadPlugin<Interface> (plugmgr, classID, report); 00285 if (!i) 00286 { 00287 if (report) 00288 csReport (Reg, CS_REPORTER_SEVERITY_ERROR, 00289 "crystalspace.plugin.query", 00290 "Couldn't load plugin with class '%s'!", classID); 00291 return 0; 00292 } 00293 if (!Reg->Register (i, scfInterfaceTraits<Interface>::GetName ())) 00294 { 00295 if (report) 00296 csReport (Reg, CS_REPORTER_SEVERITY_ERROR, 00297 "crystalspace.plugin.query", 00298 "Couldn't register plugin with class '%s'!", classID); 00299 return 0; 00300 } 00301 return (csPtr<Interface>)i; 00302 } 00303 00306 #endif // __CS_IUTIL_PLUGIN_H__
Generated for Crystal Space 1.4.1 by doxygen 1.7.1