csutil/scf_interface.h
Go to the documentation of this file.00001 /* 00002 Crystal Space Shared Class Facility (SCF) 00003 This header contains the parts of SCF that is needed for defining 00004 new interfaces. 00005 00006 Copyright (C) 1999 by Andrew Zabolotny 00007 (C) 2005 by Marten Svanfeldt 00008 (C) 2005 by Michael Adams 00009 00010 This library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Library General Public 00012 License as published by the Free Software Foundation; either 00013 version 2 of the License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Library General Public License for more details. 00019 00020 You should have received a copy of the GNU Library General Public 00021 License along with this library; if not, write to the Free 00022 Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 00023 */ 00024 00025 #ifndef __CSUTIL_SCF_INTERFACE_H__ 00026 #define __CSUTIL_SCF_INTERFACE_H__ 00027 00028 #include "csextern.h" 00029 00030 00031 // -- Forward declarations 00032 struct iDocument; 00033 #if defined(CS_DEBUG) || defined(CS_MEMORY_TRACKER) 00034 struct iObjectRegistry; 00035 #endif 00036 template<class T> 00037 class csRef; 00038 struct iStringArray; 00039 00051 typedef unsigned long scfInterfaceID; 00052 00056 typedef int scfInterfaceVersion; 00057 00058 // -- Some helpers needed below 00072 #define SCF_INTERFACE(Name,Major,Minor,Micro) \ 00073 struct InterfaceTraits { \ 00074 typedef Name InterfaceType; \ 00075 CS_FORCEINLINE static scfInterfaceVersion GetVersion() \ 00076 { return SCF_CONSTRUCT_VERSION(Major, Minor, Micro); } \ 00077 CS_FORCEINLINE static char const * GetName() { return #Name; } \ 00078 } 00079 00080 00082 #define SCF_CONSTRUCT_VERSION(Major,Minor,Micro) \ 00083 ((Major << 24) | (Minor << 16) | Micro) 00084 00085 00092 static CS_FORCEINLINE bool scfCompatibleVersion ( 00093 scfInterfaceVersion iVersion, scfInterfaceVersion iItfVersion) 00094 { 00095 return (((iVersion & 0xff000000) == (iItfVersion & 0xff000000)) 00096 && ((iVersion & 0x00ffffff) <= (iItfVersion & 0x00ffffff))) 00097 || iVersion == 0; 00098 } 00099 00103 struct scfInterfaceMetadata 00104 { 00106 const char* interfaceName; 00107 00109 scfInterfaceID interfaceID; 00110 00112 scfInterfaceVersion interfaceVersion; 00113 }; 00114 00118 struct scfInterfaceMetadataList 00119 { 00121 scfInterfaceMetadata* metadata; 00122 00124 size_t metadataCount; 00125 }; 00126 00127 // -- The main two SCF interfaces, iBase and iSCF 00128 00134 struct iBase 00135 { 00136 protected: 00141 virtual ~iBase() {} 00142 public: 00143 SCF_INTERFACE(iBase, 1, 1, 0); 00149 virtual void IncRef () = 0; 00158 virtual void DecRef () = 0; 00165 virtual int GetRefCount () = 0; 00174 virtual void *QueryInterface (scfInterfaceID iInterfaceID, int iVersion) = 0; 00184 virtual void AddRefOwner (void** ref_owner) = 0; 00191 virtual void RemoveRefOwner (void** ref_owner) = 0; 00192 00198 virtual scfInterfaceMetadataList* GetInterfaceMetadata () = 0; 00199 }; 00200 00202 typedef iBase* (*scfFactoryFunc)(iBase*); 00203 00210 struct iSCF : public virtual iBase 00211 { 00212 SCF_INTERFACE(iSCF, 3,0,0); 00224 static CS_CRYSTALSPACE_EXPORT iSCF* SCF; 00225 00226 #if defined(CS_DEBUG) || defined(CS_MEMORY_TRACKER) 00227 // This is EXTREMELY dirty but I see no other solution for now. 00228 // For debugging reasons I must have a global (global over the application 00229 // and all plugins)pointer to the object registry. I have no other 00230 // global object to tag this pointer on that except for iSCF. 00231 // This pointer is only here in debug mode though. That ensures that it 00232 // cannot be misused in real code. 00233 // If you know another solution for this problem? This global pointer 00234 // will be used by csDebuggingGraph in csutil. 00235 iObjectRegistry* object_reg; 00236 #endif 00237 00241 virtual void RegisterClasses (iDocument* metadata, 00242 const char* context = 0) = 0; 00243 00249 virtual void RegisterClasses (char const* xml, 00250 const char* context = 0) = 0; 00251 00255 virtual void RegisterClasses (const char* pluginPath, 00256 iDocument* metadata, const char* context = 0) = 0; 00257 00264 virtual bool ClassRegistered (const char *iClassID) = 0; 00265 00277 virtual iBase *CreateInstance (const char *iClassID) = 0; 00278 00284 virtual const char *GetClassDescription (const char *iClassID) = 0; 00285 00291 virtual const char *GetClassDependencies (const char *iClassID) = 0; 00292 00319 virtual csRef<iDocument> GetPluginMetadata (char const *iClassID) = 0; 00320 00327 virtual void UnloadUnusedModules () = 0; 00328 00339 virtual bool RegisterClass (const char *iClassID, 00340 const char *iLibraryName, const char *iFactoryClass, 00341 const char *Description, const char *Dependencies = 0, 00342 const char* context = 0) = 0; 00343 00350 virtual bool RegisterClass (scfFactoryFunc, const char *iClassID, 00351 const char *Description, const char *Dependencies = 0, 00352 const char* context = 0) = 0; 00353 00361 virtual bool RegisterFactoryFunc (scfFactoryFunc, const char *FactClass) = 0; 00362 00369 virtual bool UnregisterClass (const char *iClassID) = 0; 00370 00375 virtual char const* GetInterfaceName (scfInterfaceID) const = 0; 00376 00382 virtual scfInterfaceID GetInterfaceID (const char *iInterface) = 0; 00383 00390 virtual void Finish () = 0; 00391 00401 virtual csRef<iStringArray> QueryClassList (char const* pattern) = 0; 00402 00406 virtual void ScanPluginsPath (const char* path, bool recursive = false, 00407 const char* context = 0) = 0; 00408 00418 virtual bool RegisterPlugin (const char* path) = 0; 00419 }; 00420 00421 00422 //-- Interface traits 00423 00433 template <typename Interface> 00434 class scfInterfaceTraits 00435 { 00436 public: 00437 typedef typename Interface::InterfaceTraits::InterfaceType 00438 InterfaceType; 00439 00443 CS_FORCEINLINE_TEMPLATEMETHOD static scfInterfaceVersion GetVersion () 00444 { 00445 return Interface::InterfaceTraits::GetVersion (); 00446 } 00447 00455 CS_FORCEINLINE_TEMPLATEMETHOD static scfInterfaceID GetID () 00456 { 00457 scfInterfaceID& ID = GetMyID (); 00458 if (ID == (scfInterfaceID)(-1)) 00459 { 00460 ID = iSCF::SCF->GetInterfaceID (GetName ()); 00461 csStaticVarCleanup (CleanupID); 00462 } 00463 return ID; 00464 } 00465 00469 CS_FORCEINLINE_TEMPLATEMETHOD static char const* GetName () 00470 { 00471 return Interface::InterfaceTraits::GetName (); 00472 } 00473 00474 private: 00475 // This idiom is a Meyers singleton 00476 CS_FORCEINLINE_TEMPLATEMETHOD static scfInterfaceID& GetMyID () 00477 { 00478 static scfInterfaceID ID = (scfInterfaceID)-1; 00479 return ID; 00480 } 00481 static void CleanupID () 00482 { 00483 GetMyID () = (scfInterfaceID)-1; 00484 } 00485 }; 00486 00500 #define SCF_VERSION(Name,Major,Minor,Micro) \ 00501 struct Name; \ 00502 template<> \ 00503 class scfInterfaceTraits<Name> \ 00504 { \ 00505 public: \ 00506 typedef Name InterfaceType; \ 00507 static scfInterfaceVersion GetVersion() \ 00508 { return SCF_CONSTRUCT_VERSION(Major, Minor, Micro); } \ 00509 static char const* GetName () \ 00510 { return #Name; } \ 00511 static scfInterfaceID GetID () \ 00512 { scfInterfaceID& ID = GetMyID (); \ 00513 if (ID == (scfInterfaceID)(-1)) \ 00514 { ID = iSCF::SCF->GetInterfaceID (GetName ()); \ 00515 csStaticVarCleanup (CleanupID); } \ 00516 return ID; \ 00517 } \ 00518 private: \ 00519 static scfInterfaceID& GetMyID () \ 00520 { static scfInterfaceID ID = (scfInterfaceID)-1; return ID; } \ 00521 static void CleanupID () \ 00522 { GetMyID () = (scfInterfaceID)-1; } \ 00523 } 00524 00527 #endif 00528
Generated for Crystal Space 1.4.0 by doxygen 1.5.8