classregistry.cpp
来自「这是VCF框架的代码」· C++ 代码 · 共 966 行 · 第 1/2 页
CPP
966 行
//ClassRegistry.cpp/*Copyright 2000-2004 The VCF Project.Please see License.txt in the top level directorywhere you installed the VCF.*/#include "vcf/FoundationKit/FoundationKit.h"#include "vcf/FoundationKit/VCFRTTIImpl.h"using namespace VCF;ClassRegistry* ClassRegistry::create(){ if ( NULL == ClassRegistry::registryInstance_ ) { ClassRegistry::registryInstance_ = new ClassRegistry(); } return ClassRegistry::registryInstance_;}void ClassRegistry::terminate(){ delete ClassRegistry::registryInstance_;}ClassRegistry::ClassRegistry(){ classContainer_.initContainer( classMap_ ); interfaceContainer_.initContainer( interfaceMap_ );#ifdef VCF_RTTI //register "Object" as default class TypedClass<Object>* objClass = TypedClass<Object>::create("VCF::Object", OBJECT_CLASSID, ""); //new TypedClass<Object>( "VCF::Object", OBJECT_CLASSID, "" ); classMap_["VCF::Object"] = objClass; classIDMap_[OBJECT_CLASSID] = objClass;#endif //objClass->init();}ClassRegistry::~ClassRegistry(){ std::map<String,Class*>::iterator it = classMap_.begin(); while ( it != classMap_.end() ){ Class* clazz = it->second; delete clazz; it ++; } std::map<String,InterfaceClass*>::iterator it2 = interfaceMap_.begin(); while ( it2 != interfaceMap_.end() ){ delete it2->second; it2 ++; } classMap_.clear(); classIDMap_.clear(); interfaceMap_.clear(); interfaceIDMap_.clear(); implementedInterfacesIDMap_.clear();}void ClassRegistry::removeClass( const String& className ){ #ifndef VCF_RTTI return ; #endif if ( !className.empty() ) { String id ; std::map<String,Class*>::iterator found = registryInstance_->classMap_.find( className ); if ( found != registryInstance_->classMap_.end() ){ Class* clazz = found->second; id = clazz->getID(); delete clazz; registryInstance_->classMap_.erase( found ); found = registryInstance_->classIDMap_.find( id ); if ( found != registryInstance_->classIDMap_.end() ) { registryInstance_->classIDMap_.erase( found ); } } }}void ClassRegistry::removeClassByID( const String& classID ){ #ifndef VCF_RTTI return ; #endif if ( !classID.empty() ) { String name ; std::map<String,Class*>::iterator found = registryInstance_->classIDMap_.find( classID ); if ( found != registryInstance_->classIDMap_.end() ){ Class* clazz = found->second; name = clazz->getClassName(); delete clazz; registryInstance_->classIDMap_.erase( found ); found = registryInstance_->classMap_.find( name ); if ( found != registryInstance_->classMap_.end() ) { registryInstance_->classMap_.erase( found ); } } }}void ClassRegistry::removeInterface( const String& interfaceName ){ #ifndef VCF_RTTI return ; #endif if ( !interfaceName.empty() ) { String id ; std::map<String,InterfaceClass*>::iterator found = registryInstance_->interfaceMap_.find( interfaceName ); if ( found != registryInstance_->interfaceMap_.end() ){ InterfaceClass* clazz = found->second; id = clazz->getID(); delete clazz; registryInstance_->interfaceMap_.erase( found ); found = registryInstance_->interfaceIDMap_.find( id ); if ( found != registryInstance_->interfaceIDMap_.end() ) { registryInstance_->interfaceIDMap_.erase( found ); } std::map<String,ImplementedInterfaceClass*>::iterator found2 = registryInstance_->implementedInterfacesIDMap_.find( id ); if ( found2 != registryInstance_->implementedInterfacesIDMap_.end() ) { registryInstance_->implementedInterfacesIDMap_.erase( found2 ); } } }}void ClassRegistry::removeInterfaceByID( const String& interfaceID ){ #ifndef VCF_RTTI return ; #endif if ( !interfaceID.empty() ) { String name ; std::map<String,InterfaceClass*>::iterator found = registryInstance_->interfaceIDMap_.find( interfaceID ); if ( found != registryInstance_->interfaceIDMap_.end() ){ InterfaceClass* clazz = found->second; name = clazz->getInterfaceName(); delete clazz; registryInstance_->interfaceIDMap_.erase( found ); found = registryInstance_->interfaceMap_.find( name ); if ( found != registryInstance_->interfaceMap_.end() ) { registryInstance_->interfaceMap_.erase( found ); } std::map<String,ImplementedInterfaceClass*>::iterator found2 = registryInstance_->implementedInterfacesIDMap_.find( interfaceID ); if ( found2 != registryInstance_->implementedInterfacesIDMap_.end() ) { registryInstance_->implementedInterfacesIDMap_.erase( found2 ); } } }}Class* ClassRegistry::getClass( const String& className ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getClass( className );}Class* ClassRegistry::getClassFromClassID( const String& classID ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getClassFromClassID( classID );}Class* ClassRegistry::getClass( Object* object ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getClass( object );}Object* ClassRegistry::createNewInstance( const String& className ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_createNewInstance( className );}Object* ClassRegistry::createNewInstanceFromClassID( const String& classID ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_createNewInstanceFromClassID( classID );}void* ClassRegistry::createNewInterfaceInstanceFromInterfaceName( const String& interfaceName, const String& implementerClassName ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_createNewInterfaceInstanceFromInterfaceName( interfaceName, implementerClassName );}void* ClassRegistry::createNewInterfaceInstanceFromInterfaceID( const String& interfaceID, const String& implementerClassID ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_createNewInterfaceInstanceFromInterfaceID( interfaceID, implementerClassID );}void ClassRegistry::addClass( const String& className, Class* classToRegister ){#ifndef VCF_RTTI return ;#endif ClassRegistry::getClassRegistry()->internal_addClass( className, classToRegister );}void ClassRegistry::addInterface( const String& interfaceName, InterfaceClass* interfaceToRegister ){#ifndef VCF_RTTI return ;#endif ClassRegistry::getClassRegistry()->internal_addInterface( interfaceName, interfaceToRegister );}Enumerator<Class*>* ClassRegistry::getClasses(){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getClasses();}Enumerator<InterfaceClass*>* ClassRegistry::getInterfaces(){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getInterfaces();}ImplementedInterfaceClass* ClassRegistry::getImplementedInterface( const String& implementingClassName, const String& interfaceID ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getImplementedInterface( implementingClassName, interfaceID );}InterfaceClass* ClassRegistry::getInterface( const String& interfaceName ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getInterface( interfaceName );}InterfaceClass* ClassRegistry::getInterfaceFromInterfaceID( const String& interfaceID ){#ifndef VCF_RTTI return NULL;#endif return ClassRegistry::getClassRegistry()->internal_getInterfaceFromInterfaceID( interfaceID );}void ClassRegistry::addImplementedInterface( ImplementedInterfaceClass* interfaceToRegister, const String& implementerClassID ){#ifndef VCF_RTTI return;#endif ClassRegistry::getClassRegistry()->internal_addImplementedInterface( interfaceToRegister,implementerClassID );}bool ClassRegistry::registerMethod( Method* method, const String& className, const bool& isInterfaceMethod ){#ifndef VCF_RTTI return false;#endif return ClassRegistry::getClassRegistry()->internal_registerMethod( method, className, isInterfaceMethod );}bool ClassRegistry::registerMethodByUUID( Method* method, const String& UUID, const bool& isInterfaceMethod ){ #ifndef VCF_RTTI return false;#endif return ClassRegistry::getClassRegistry()->internal_registerMethodByUUID( method, UUID, isInterfaceMethod );}void ClassRegistry::dump(){#ifndef VCF_RTTI return;#endif ClassRegistry::getClassRegistry()->internal_dump();}ClassRegistry* ClassRegistry::getClassRegistry(){ return ClassRegistry::registryInstance_;}Class* ClassRegistry::internal_getClass( const String& className ){ Class* result = NULL; if ( !className.empty() ) { std::map<String,Class*>::iterator found = classMap_.find( className ); if ( found != classMap_.end() ){ result = found->second; } } return result;}Class* ClassRegistry::internal_getClassFromClassID( const String& classID ){ Class* result = NULL; if ( !classID.empty() ) { std::map<String,Class*>::iterator found = classIDMap_.find( classID ); if ( found != classIDMap_.end() ){ result = found->second; } } return result;}Class* ClassRegistry::internal_getClass( Object* object ){ Class* result = NULL; std::map<String,Class*>::iterator found = classMap_.begin(); Class* clazz = NULL; while ( found != classMap_.end() ){ clazz = found->second; if ( clazz->isEqual( object ) ){ result = clazz; break; } found++; } return result;}Object* ClassRegistry::internal_createNewInstance( const String& className ){ Class* clazz = NULL; Object* result = NULL; std::map<String,Class*>::iterator found = classMap_.find( className ); if ( found != classMap_.end() ){ clazz = found->second; result = clazz->createInstance(); } if ( NULL == result ) { throw CantCreateObjectException( "Unable to create an instance of class\"" + className + "\"" ); } return result;}Object* ClassRegistry::internal_createNewInstanceFromClassID( const String& classID ){ Class* clazz = NULL; Object* result = NULL; std::map<String,Class*>::iterator found = classIDMap_.find( classID ); if ( found != classIDMap_.end() ){ clazz = found->second; result = clazz->createInstance(); } if ( NULL == result ) { throw CantCreateObjectException( "Unable to create an instance of class\"" + classID + "\"" ); } return result;}void ClassRegistry::internal_addClass( const String& className, Class* classToRegister ){ if ( NULL == classToRegister ) return; //throw exception ? ClassRegistry* classRegistry = ClassRegistry::getClassRegistry(); String classID = classToRegister->getID(); std::map<String,Class*>::iterator found; bool classNotFound = true; if ( classID != "" ) { found = classRegistry->classIDMap_.find( classID ); classNotFound = !( found != classIDMap_.end() ); } else { found = classRegistry->classMap_.find( className ); classNotFound = !( found != classMap_.end() ); } if ( true == classNotFound ){ classRegistry->classMap_[className] = classToRegister; if ( classID != "" ) { classRegistry->classIDMap_[classID] = classToRegister; } Class* superClass = classToRegister->getSuperClass(); Class* tmp = NULL; while ( NULL != superClass ){ //copy over properties Enumerator<Property*>* props = superClass->getProperties(); if ( NULL != props ){ while ( props->hasMoreElements() ){ Property* prop = props->nextElement(); if ( NULL != prop ){ if ( false == classToRegister->hasProperty( prop->getName() ) ) { classToRegister->addProperty( prop->clone() ); } } } }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?