Vault
4.1
|
00001 /* 00002 Copyright c1997-2014 Trygve Isaacson. All rights reserved. 00003 This file is part of the Code Vault version 4.1 00004 http://www.bombaydigital.com/ 00005 License: MIT. See LICENSE.md in the Vault top level directory. 00006 */ 00007 00008 #ifndef vclassregistry_h 00009 #define vclassregistry_h 00010 00013 #include "vstring.h" 00014 00015 class VClassFactory; 00016 typedef std::vector<const VClassFactory*> VClassFactoryPtrVector; 00017 00109 class VClassRegistry { 00110 public: 00111 00115 static VClassRegistry* registry(); 00116 00120 VClassRegistry(); 00124 virtual ~VClassRegistry(); 00125 00136 void* instantiateObject(const VString& classID) const; 00141 void registerClass(const VClassFactory* factory); 00150 const VClassFactory* findClassFactory(const VString& classID) const; 00151 00152 private: 00153 00154 VClassFactoryPtrVector mFactories; 00155 00156 static VClassRegistry* gRegistry; 00157 }; 00158 00167 class VClassFactory { 00168 public: 00169 00174 VClassFactory(const VString& classID); 00178 virtual ~VClassFactory() {} 00179 00185 virtual void* instantiateObject() const = 0; 00192 bool matchesClassID(const VString& classID) const; 00197 void getClassID(VString& classID) const; 00198 00199 private: 00200 00201 VString mClassID; 00202 }; 00203 00212 #define DEFINE_CLASSFACTORY(classname, factoryname) \ 00213 class factoryname : public VClassFactory { \ 00214 public: \ 00215 \ 00216 static factoryname gFactory; \ 00217 \ 00218 factoryname(const VString& classID) \ 00219 : VClassFactory(classID) \ 00220 { \ 00221 VClassRegistry::registry()->registerClass(this); \ 00222 } \ 00223 \ 00224 virtual ~factoryname() {} \ 00225 \ 00226 virtual void* instantiateObject() const { return new classname(); } \ 00227 \ 00228 } 00229 00237 #define DECLARE_CLASSFACTORY(classname, factoryname) factoryname factoryname::gFactory(#classname) 00238 00239 #endif /* vclassregistry_h */ 00240