csutil/plugmgr.h Source File (Crystal Space 2.0 Public API Reference)

| ## Public API Reference

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

- File List
- File Members

csutil/plugmgr.h
Go to the documentation of this file.

    00001 /*
    00002     Copyright (C) 1998-2001 by Jorrit Tyberghein
    00003
    00004     This library is free software; you can redistribute it and/or
    00005     modify it under the terms of the GNU Library General Public
    00006     License as published by the Free Software Foundation; either
    00007     version 2 of the License, or (at your option) any later version.
    00008
    00009     This library is distributed in the hope that it will be useful,
    00010     but WITHOUT ANY WARRANTY; without even the implied warranty of
    00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    00012     Library General Public License for more details.
    00013
    00014     You should have received a copy of the GNU Library General Public
    00015     License along with this library; if not, write to the Free
    00016     Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    00017 */
    00018
    00019 #ifndef __CS_PLUGMGR_H__
    00020 #define __CS_PLUGMGR_H__
    00021
    00026 #include "csextern.h"
    00027 #include "csutil/parray.h"
    00028 #include "csutil/hash.h"
    00029 #include "csutil/scf.h"
    00030 #include "csutil/scf_implementation.h"
    00031 #include "csutil/threading/mutex.h"
    00032 #include "csutil/threadmanager.h"
    00033 #include "iutil/comp.h"
    00034 #include "iutil/plugin.h"
    00035 #include "iutil/pluginconfig.h"
    00036
    00037 struct iObjectRegistry;
    00038
    00043 class CS_CRYSTALSPACE_EXPORT csPluginManager :
    00044   public scfImplementation1<csPluginManager, iPluginManager>
    00045 {
    00046 private:
    00048   CS::Threading::RecursiveMutex mutex;
    00049   bool do_verbose;
    00050
    00052   CS::Threading::Mutex loadingLock;
    00053
    00057   class PluginLoadCondition : public CS::Threading::Condition,
    00058     public CS::Utility::FastRefCount<PluginLoadCondition>
    00059   {
    00060   };
    00061
    00063   typedef csHash<csRef<PluginLoadCondition>, csString> AlreadyLoadingHash;
    00064   AlreadyLoadingHash alreadyLoading;
    00065
    00069   class CS_CRYSTALSPACE_EXPORT csPlugin
    00070   {
    00071   public:
    00073     csRef<iComponent> Plugin;
    00075     csString ClassID;
    00076
    00077     csPlugin ();
    00078
    00080     csPlugin (iComponent *iObject, const char *iClassID);
    00081
    00083     bool operator== (const csPlugin& other) const
    00084     { return Plugin == other.Plugin && ClassID == other.ClassID; }
    00085     bool operator< (const csPlugin& other) const
    00086     { return Plugin < other.Plugin ||
    00087             (Plugin == other.Plugin && ClassID < other.ClassID); }
    00088   };
    00089
    00093   class CS_CRYSTALSPACE_EXPORT csPluginsVector :
    00094     public csArray<csPlugin,
    00095                    csArrayElementHandler<csPlugin>,
    00096                    CS::Container::ArrayAllocDefault,
    00097                    csArrayCapacityFixedGrow<8> >
    00098   {
    00099   public:
    00101     csPluginsVector (int l) : csArray<csPlugin,
    00102       csArrayElementHandler<csPlugin>, CS::Container::ArrayAllocDefault,
    00103       csArrayCapacityFixedGrow<8> > (l) {}
    00105     static int CompareAddress (csPlugin const& Item, iComponent* const& Key)
    00106     { return Item.Plugin == Key ? 0 : 1; }
    00107   };
    00108
    00112   class CS_CRYSTALSPACE_EXPORT csPluginOption
    00113   {
    00114   public:
    00115     char *Name;
    00116     csVariantType Type;
    00117     int ID;
    00118     bool Value;                         // If Type is CSVAR_BOOL
    00119     csRef<iPluginConfig> Config;
    00120
    00121     csPluginOption (const char *iName, csVariantType iType, int iID,
    00122       bool iValue, iPluginConfig* cfg)
    00123     {
    00124       Name = csStrNew (iName);
    00125       Type = iType;
    00126       ID = iID;
    00127       Value = iValue;
    00128       Config = cfg;
    00129     }
    00130     virtual ~csPluginOption ()
    00131     {
    00132       delete [] Name;
    00133     }
    00134   };
    00135
    00137   iObjectRegistry* object_reg;
    00138
    00140   csPluginsVector Plugins;
    00141
    00142   csPlugin* FindPluginByClassID (const char* classID,
    00143     csPlugin* startAfter = 0);
    00144   void WaitForPluginLoad (const char* classID);
    00145   csStringArray GetClassIDTagsLocal (const char* classID);
    00146
    00148   csPDelArray<csPluginOption, CS::Container::ArrayAllocDefault,
    00149     csArrayCapacityFixedGrow<16> > OptionList;
    00150
    00151   typedef csHash<csString, csString> TagToClassHash;
    00152   TagToClassHash tagToClassMap;
    00153
    00154   void Report (int severity, const char* subMsgID,
    00155     const char* message, ...);
    00156   void ReportV (int severity, const char* subMsgID,
    00157     const char* message, va_list args);
    00158   /* Report while the mutex is held:
    00159    * Report() might wait for the main thread; if the mutex is held there
    00160    * b/c a plugin is being loaded a deadlock may occur.
    00161    * This method avoids that. */
    00162   void ReportInLock (int severity, const char* subMsgID,
    00163     const char* message, ...);
    00164 public:
    00166   csPluginManager (iObjectRegistry* object_reg);
    00168   virtual ~csPluginManager ();
    00169
    00170   virtual csPtr<iComponent> LoadPluginInstance (const char* iClassID, uint flags);
    00171
    00172   virtual csPtr<iComponent> QueryPluginInstance (const char *iInterface, int iVersion);
    00173   csPtr<iComponent> QueryPluginInstance (const char* classID);
    00174   virtual csPtr<iComponent> QueryPluginInstance (const char* iClassID,
    00175           const char *iInterface, int iVersion);
    00176   virtual bool UnloadPluginInstance (iComponent *iObject);
    00177   virtual bool RegisterPluginInstance (const char *iClassID,
    00178           iComponent *iObject);
    00179   virtual csPtr<iPluginIterator> GetPluginInstances ();
    00180   virtual void Clear ();
    00181
    00183   virtual void QueryOptions (iComponent *iObject);
    00184
    00185   bool SetTagClassIDMapping (const char* tag, const char* classID)
    00186   {
    00187     CS::Threading::RecursiveMutexScopedLock lock (mutex);
    00188     bool result = tagToClassMap.Contains (tag);
    00189     tagToClassMap.PutUnique (tag, classID);
    00190     return result;
    00191   }
    00192
    00193   bool UnsetTagClassIDMapping (const char* tag)
    00194   {
    00195     CS::Threading::RecursiveMutexScopedLock lock (mutex);
    00196     return tagToClassMap.DeleteAll (tag);
    00197   }
    00198
    00199   const char* GetTagClassIDMapping (const char* tag)
    00200   {
    00201     CS::Threading::RecursiveMutexScopedLock lock (mutex);
    00202     return tagToClassMap.Get (tag, (const char*)0);
    00203   }
    00204
    00205   csPtr<iStringArray> GetClassIDTags (const char* classID);
    00206
    00207   csPtr<iComponent> LoadTagPluginInstance (const char* tag,
    00208     uint loadFlags)
    00209   {
    00210     return LoadTagPluginInstance (GetTagClassIDMapping (tag), loadFlags);
    00211   }
    00212
    00213   csPtr<iComponent> QueryTagPluginInstance (const char* tag)
    00214   {
    00215     return QueryPluginInstance (GetTagClassIDMapping (tag));
    00216   }
    00217 };
    00218
    00219 #endif // __CS_PLUGMGR_H__
    00220