CMS 3D CMS Logo

/data/refman/pasoursint/CMSSW_5_2_9/src/DetectorDescription/Core/src/DDName.cc

Go to the documentation of this file.
00001 #include "DetectorDescription/Core/interface/DDName.h"
00002 #include "DetectorDescription/Core/interface/DDCurrentNamespace.h"
00003 #include "DetectorDescription/Base/interface/DDException.h"
00004 #include "DetectorDescription/Core/interface/DDSplit.h"
00005 #include "DetectorDescription/Base/interface/Singleton.h"
00006 
00007 #include <sstream>
00008 
00009 std::ostream & operator<<(std::ostream & os, const DDName & n)
00010 { 
00011   os << n.ns() << ':' << n.name();
00012   static const char * ev = getenv("DDNAMEID");
00013   if (ev) os << '[' << n.id() << ']';
00014   return os;
00015 }  
00016 
00017 // Implementation of DDName
00018 
00019 DDName::DDName( const std::string & name, const std::string & ns)
00020  : id_(registerName(std::make_pair(name,ns))->second)
00021 { }
00022 
00023 
00024 DDName::DDName( const std::string & name )
00025  : id_(0)
00026 { 
00027   std::pair<std::string,std::string> result = DDSplit(name);
00028   if (result.second == "") {
00029     id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
00030   }  
00031   else {
00032     id_ = registerName(result)->second;
00033   }
00034 }
00035 
00036 
00037 DDName::DDName( const char* name )
00038  : id_(0)
00039 { 
00040   std::pair<std::string,std::string> result = DDSplit(name);
00041   if (result.second == "") {
00042     id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
00043   }  
00044   else {
00045     id_ = registerName(result)->second;
00046   }
00047 }
00048 
00049 
00050 DDName::DDName( const char* name, const char* ns)
00051  : id_(registerName(std::make_pair(std::string(name),std::string(ns)))->second)
00052 { }
00053 
00054 
00055 DDName::DDName()
00056  : id_(0)
00057 { } 
00058 
00059 DDName::DDName(DDName::id_type id)
00060  : id_(id)
00061 { }
00062 
00063 void DDName::defineId(const std::pair<std::string,std::string> & nm, DDName::id_type id)
00064 { 
00065   IdToName & id2n = DDI::Singleton<IdToName>::instance();
00066   
00067   /* 
00068     Semantics:
00069     If id exists && the registered value matches the given one, do nothing
00070     If id exists && the registered value DOES NOT match, throw
00071     If id DOES NOT exists, register with the given value
00072   */
00073   if ( id < id_type(id2n.size()) ) {
00074     if(id2n[id]->first != nm) {
00075       std::stringstream s;
00076       s << id;
00077       throw DDException("DDName::DDName(std::pair<std::string,std::string>,id_type): id=" + s.str() + " reg-name=?" );
00078     }
00079   }
00080   else {
00081     id2n.resize(id+1);
00082     DDI::Singleton<Registry>::instance()[nm]=id;
00083     id2n[id] = DDI::Singleton<Registry>::instance().find(nm);
00084   }
00085 }
00086 
00087 const std::string & DDName::name() const 
00088 {
00089   static std::string ano_("anonymous");
00090   const std::string * result;
00091   if (id_ < 0) {
00092       result = &ano_;
00093   }
00094   else {
00095     result = &DDI::Singleton<IdToName>::instance()[id_]->first.first;
00096   }
00097   return *result; 
00098 }
00099 
00100 
00101 const std::string & DDName::ns() const
00102 {
00103   static std::string ano_("anonymous");
00104   const std::string * result;
00105   if (id_ < 0) {
00106       result = &ano_;
00107   }
00108   else {
00109     result = &DDI::Singleton<IdToName>::instance()[id_]->first.second;
00110   }
00111   return *result;
00112 }
00113 
00114 
00115 
00116 bool DDName::exists(const std::string & name, const std::string & ns)
00117 {
00118    const std::pair<std::string,std::string> p(name,ns);
00119    Registry::const_iterator it = DDI::Singleton<Registry>::instance().find(p);
00120    return it != DDI::Singleton<Registry>::instance().end() ? true : false;
00121 }
00122 
00123 
00124 DDName::Registry::iterator DDName::registerName(const std::pair<std::string,std::string> & nm) {
00125     Registry& reg_ = DDI::Singleton<Registry>::instance();
00126     IdToName & idToName = DDI::Singleton<IdToName>::instance();  
00127     Registry::size_type sz = reg_.size();
00128     if (!sz) {
00129       reg_[std::make_pair(std::string(""),std::string(""))] = 0;
00130       idToName.push_back(reg_.begin());
00131       ++sz;
00132     }
00133     Registry::value_type val(nm, sz);
00134     std::pair<Registry::iterator,bool> result = reg_.insert(val);
00135     if (result.second) {
00136       idToName.push_back(result.first);
00137     }
00138     return result.first;
00139 }
00140