## Public API Reference

- Main Page
- Modules
- Namespaces
- Classes
- Files
- Related Pages

- File List
- File Members

# 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,0,0);
    00070   virtual iBase *LoadPlugin (const char *classID,
    00071     bool init = true) = 0;
    00072
    00080   virtual iBase *QueryPlugin (const char *iInterface, int iVersion) = 0;
    00082   virtual iBase *QueryPlugin (const char* classID,
    00083         const char *iInterface, int iVersion) = 0;
    00085   virtual bool UnloadPlugin (iComponent *obj) = 0;
    00087   virtual bool RegisterPlugin (const char *classID, iComponent *obj) = 0;
    00088
    00094   virtual csPtr<iPluginIterator> GetPlugins () = 0;
    00096   virtual void Clear () = 0;
    00097
    00104   virtual void QueryOptions (iComponent* object) = 0;
    00105 };
    00106
    00107
    00116 template<class Interface>
    00117 inline csPtr<Interface> csQueryPluginClass (iPluginManager *mgr,
    00118                                             const char* ClassID)
    00119 {
    00120   iBase* base = mgr->QueryPlugin (ClassID,
    00121     scfInterfaceTraits<Interface>::GetName(),
    00122     scfInterfaceTraits<Interface>::GetVersion());
    00123
    00124   if (base == 0) return csPtr<Interface> (0);
    00125
    00126   Interface *x = (Interface*)base->QueryInterface (
    00127     scfInterfaceTraits<Interface>::GetID (),
    00128     scfInterfaceTraits<Interface>::GetVersion ());
    00129
    00130   base->DecRef (); //release our base interface
    00131
    00132   return csPtr<Interface> (x);
    00133 }
    00134
    00139 #define CS_QUERY_PLUGIN_CLASS(Object,ClassID,Interface)                 \
    00140   csQueryPluginClass<Interface> (Object, ClassID)
    00141
    00148 template<class Interface>
    00149 inline csPtr<Interface> csLoadPlugin (iPluginManager *mgr,
    00150                                       const char* ClassID)
    00151 {
    00152   iBase* base = mgr->LoadPlugin (ClassID);
    00153
    00154   if (base == 0) return csPtr<Interface> (0);
    00155
    00156   Interface *x = (Interface*)base->QueryInterface (
    00157     scfInterfaceTraits<Interface>::GetID (),
    00158     scfInterfaceTraits<Interface>::GetVersion ());
    00159
    00160   base->DecRef (); //release our base interface
    00161
    00162   return csPtr<Interface> (x);
    00163 }
    00164
    00171 template<class Interface>
    00172 inline csPtr<Interface> csLoadPlugin (iObjectRegistry* object_reg,
    00173                                       const char* ClassID)
    00174 {
    00175   csRef<iPluginManager> mgr = csQueryRegistry<iPluginManager> (object_reg);
    00176   if (!mgr) return 0;
    00177   return csLoadPlugin<Interface> (mgr, ClassID);
    00178 }
    00179
    00187 template<class Interface>
    00188 inline csPtr<Interface> csLoadPluginCheck (iPluginManager *mgr,
    00189                                       const char* ClassID)
    00190 {
    00191   csRef<Interface> i = csQueryPluginClass<Interface> (mgr, ClassID);
    00192   if (i) return (csPtr<Interface>) i;
    00193   i = csLoadPlugin<Interface> (mgr, ClassID);
    00194   if (!i) return 0;
    00195   return (csPtr<Interface>) i;
    00196 }
    00197
    00206 template<class Interface>
    00207 inline csPtr<Interface> csLoadPluginCheck (iObjectRegistry* object_reg,
    00208                                       const char* ClassID, bool report = true)
    00209 {
    00210   csRef<iPluginManager> mgr = csQueryRegistry<iPluginManager> (object_reg);
    00211   if (!mgr)
    00212   {
    00213     if (report)
    00214       csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    00215         "crystalspace.plugin.load", "Couldn't find plugin manager!");
    00216     return 0;
    00217   }
    00218   csRef<Interface> i = csLoadPluginCheck<Interface> (mgr, ClassID);
    00219   if (!i)
    00220   {
    00221     if (report)
    00222       csReport (object_reg, CS_REPORTER_SEVERITY_ERROR,
    00223         "crystalspace.plugin.load", "Couldn't load plugin with class '%s'!",
    00224                 ClassID);
    00225     return 0;
    00226   }
    00227   return (csPtr<Interface>) i;
    00228 }
    00229
    00234 #define CS_LOAD_PLUGIN(Object,ClassID,Interface)                        \
    00235   csLoadPlugin<Interface> (Object, ClassID)
    00236
    00241 inline csPtr<iBase> csLoadPluginAlways (iPluginManager *mgr,
    00242                                         const char* ClassID)
    00243 {
    00244   iBase* base = mgr->LoadPlugin (ClassID);
    00245   return csPtr<iBase> (base);
    00246 }
    00247
    00252 #define CS_LOAD_PLUGIN_ALWAYS(Object,ClassID)                           \
    00253   csLoadPluginAlways (Object, ClassID)
    00254
    00276 template<class Interface>
    00277 inline csPtr<Interface> csQueryRegistryOrLoad (iObjectRegistry *Reg,
    00278         const char* classID, bool report = true)
    00279 {
    00280   csRef<Interface> i = csQueryRegistry<Interface> (Reg);
    00281   if (i) return (csPtr<Interface>)i;
    00282   csRef<iPluginManager> plugmgr = csQueryRegistry<iPluginManager> (Reg);
    00283   if (!plugmgr)
    00284   {
    00285     if (report)
    00286       csReport (Reg, CS_REPORTER_SEVERITY_ERROR,
    00287         "crystalspace.plugin.query", "Plugin manager missing!");
    00288     return 0;
    00289   }
    00290   i = csLoadPlugin<Interface> (plugmgr, classID);
    00291   if (!i)
    00292   {
    00293     if (report)
    00294       csReport (Reg, CS_REPORTER_SEVERITY_ERROR,
    00295         "crystalspace.plugin.query",
    00296         "Couldn't load plugin with class '%s'!", classID);
    00297     return 0;
    00298   }
    00299   if (!Reg->Register (i, scfInterfaceTraits<Interface>::GetName ()))
    00300   {
    00301     if (report)
    00302       csReport (Reg, CS_REPORTER_SEVERITY_ERROR,
    00303         "crystalspace.plugin.query",
    00304         "Couldn't register plugin with class '%s'!", classID);
    00305     return 0;
    00306   }
    00307   return (csPtr<Interface>)i;
    00308 }
    00309
    00312 #endif // __CS_IUTIL_PLUGIN_H__