00001 /*************************************************************************** 00002 DDXMLElementRegistry.cc - description 00003 ------------------- 00004 begin : Wed Mar 27 2002 00005 email : case@ucdhep.ucdavis.edu 00006 ***************************************************************************/ 00007 00008 /*************************************************************************** 00009 * * 00010 * DDDParser sub-component of DDD * 00011 * * 00012 ***************************************************************************/ 00013 00014 00015 00016 // ------------------------------------------------------------------------- 00017 // Includes 00018 // ------------------------------------------------------------------------- 00019 #include "DDXMLElementRegistry.h" 00020 00021 // DDCore dependencies 00022 #include "DetectorDescription/Base/interface/DDdebug.h" 00023 00024 #include <string> 00025 #include <algorithm> 00026 #include <map> 00027 #include <vector> 00028 #include <iostream> 00029 00030 // ------------------------------------------------------------------------- 00031 // Constructor/Destructor 00032 // ------------------------------------------------------------------------- 00033 00034 DDXMLElementRegistry::DDXMLElementRegistry() 00035 { 00036 // registry_ = new RegistryMap; 00037 } 00038 00039 DDXMLElementRegistry::~DDXMLElementRegistry() 00040 { 00041 // Complicated cleanup. I keep track of DDXMLElements that have 00042 // already been deleted using this vector. Then delete them one-by-one. 00043 std::vector<DDXMLElement*> toDelete; 00044 for ( RegistryMap::const_iterator it = registry_.begin(); it != registry_.end(); ++it) { 00045 std::vector<DDXMLElement*>::const_iterator deleteIt = std::find(toDelete.begin(), toDelete.end(), it->second); 00046 if ( deleteIt == toDelete.end() ) { 00047 toDelete.push_back(it->second); 00048 delete it->second; 00049 } 00050 } 00051 } 00052 00053 // ------------------------------------------------------------------------- 00054 // Implementation 00055 // ------------------------------------------------------------------------- 00056 00057 // This allows Elements to register themselves with the static registry 00058 void DDXMLElementRegistry::registerElement(const std::string& name, DDXMLElement* element) 00059 { 00060 DCOUT_V('P',"DDXMLElementRegistry::registerElementBase: " << name << " at " << element); 00061 registry_[name] = element; 00062 } 00063 00064 // THE most important part. Getting the pointer to a given element type. 00065 DDXMLElement* DDXMLElementRegistry::getElement(const std::string& name) 00066 { 00067 DCOUT_V('P', "DDXMLElementRegistry::getElement " << name << std::endl); 00068 RegistryMap::iterator it = registry_.find(name); 00069 DDXMLElement* myret = NULL; 00070 if (it != registry_.end()) 00071 myret = it->second; 00072 DCOUT_V('P', "DDXMLElementRegistry::getElement " << name << std::endl); 00073 return myret; 00074 } 00075 00076 // Get the name given a pointer. This may not be needed... 00077 std::string DDXMLElementRegistry::getElementName(DDXMLElement* theElementBase) 00078 { 00079 std::string ret = ""; 00080 for (RegistryMap::const_iterator it = registry_.begin(); it != registry_.end(); ++it) 00081 if (it->second == theElementBase) 00082 ret = it->first; 00083 return ret; 00084 } 00085 00086 std::ostream & operator<<(std::ostream & os, const DDXMLElementRegistry & element) 00087 { 00088 element.stream(os); 00089 return os; 00090 } 00091 00092 void DDXMLElementRegistry::stream(std::ostream & os) const 00093 { 00094 os << "Output of current Element Registry:" << std::endl; 00095 for (RegistryMap::const_iterator it=registry_.begin(); it != registry_.end(); ++it) 00096 os << it->first << " at address " << it->second << std::endl; 00097 }