CMS 3D CMS Logo

DDName.cc
Go to the documentation of this file.
2 
3 #include <ext/alloc_traits.h>
4 #include <cstdlib>
5 #include <sstream>
6 
11 
12 std::ostream & operator<<(std::ostream & os, const DDName & n)
13 {
14  os << n.ns() << ':' << n.name();
15  return os;
16 }
17 
18 // Implementation of DDName
19 
21  : id_(registerName(std::make_pair(name,ns))->second)
22 { }
23 
24 
26  : id_(0)
27 {
28  std::pair<std::string,std::string> result = DDSplit(name);
29  if (result.second.empty()) {
30  id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
31  }
32  else {
33  id_ = registerName(result)->second;
34  }
35 }
36 
37 
38 DDName::DDName( const char* name )
39  : id_(0)
40 {
41  std::pair<std::string,std::string> result = DDSplit(name);
42  if (result.second.empty()) {
43  id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
44  }
45  else {
46  id_ = registerName(result)->second;
47  }
48 }
49 
50 
51 DDName::DDName( const char* name, const char* ns)
52  : id_(registerName(std::make_pair(std::string(name),std::string(ns)))->second)
53 { }
54 
55 
57  : id_(0)
58 { }
59 
61  : id_(id)
62 { }
63 
64 void DDName::defineId(const std::pair<std::string,std::string> & nm, DDName::id_type id)
65 {
67 
68  /*
69  Semantics:
70  If id exists && the registered value matches the given one, do nothing
71  If id exists && the registered value DOES NOT match, throw
72  If id DOES NOT exists, register with the given value
73  */
74  if ( id < id_type(id2n.size()) ) {
75  if(id2n[id]->first != nm) {
76  std::stringstream s;
77  s << id;
78  throw cms::Exception("DDException") << "DDName::DDName(std::pair<std::string,std::string>,id_type): id=" + s.str() + " reg-name=?";
79  }
80  }
81  else {
82  id2n.resize(id+1);
84  id2n[id] = DDI::Singleton<Registry>::instance().find(nm);
85  }
86 }
87 
88 const std::string & DDName::name() const
89 {
90  const static std::string ano_("anonymous");
91  const std::string * result;
92  if (id_ < 0) {
93  result = &ano_;
94  }
95  else {
96  result = &DDI::Singleton<IdToName>::instance()[id_]->first.first;
97  }
98  return *result;
99 }
100 
101 
102 const std::string & DDName::ns() const
103 {
104  const static std::string ano_("anonymous");
105  const std::string * result;
106  if (id_ < 0) {
107  result = &ano_;
108  }
109  else {
110  result = &DDI::Singleton<IdToName>::instance()[id_]->first.second;
111  }
112  return *result;
113 }
114 
115 
116 
118 {
119  const std::pair<std::string,std::string> p(name,ns);
120  Registry::const_iterator it = DDI::Singleton<Registry>::instance().find(p);
121  return it != DDI::Singleton<Registry>::instance().end() ? true : false;
122 }
123 
124 
125 DDName::Registry::iterator DDName::registerName(const std::pair<std::string,std::string> & nm) {
128  Registry::size_type sz = reg_.size();
129  if (!sz) {
130  reg_[std::make_pair(std::string(""),std::string(""))] = 0;
131  idToName.emplace_back(reg_.begin());
132  ++sz;
133  }
134  Registry::value_type val(nm, sz);
135  std::pair<Registry::iterator,bool> result = reg_.insert(val);
136  if (result.second) {
137  idToName.emplace_back(result.first);
138  }
139  return result.first;
140 }
141 
id_type id() const
Definition: DDName.h:57
std::ostream & operator<<(std::ostream &os, const DDName &n)
Definition: DDName.cc:12
const std::string & ns() const
Returns the namespace.
Definition: DDName.cc:102
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
static std::string & ns()
DDName()
Definition: DDName.cc:56
uint16_t size_type
U second(std::pair< T, U > const &p)
std::map< std::pair< std::string, std::string >, id_type > Registry
Definition: DDName.h:23
static value_type & instance()
static void defineId(const std::pair< std::string, std::string > &, id_type id)
register pre-defined ids
Definition: DDName.cc:64
std::vector< Registry::const_iterator > IdToName
Definition: DDName.h:24
int id_type
Definition: DDName.h:22
static bool exists(const std::string &name, const std::string &ns)
true, if a DDName with given name and namespace (ns) already is registerd, otherwise false ...
Definition: DDName.cc:117
id_type id_
Definition: DDName.h:63
static Registry::iterator registerName(const std::pair< std::string, std::string > &s)
Definition: DDName.cc:125
std::pair< std::string, std::string > DDSplit(const std::string &n)
split into (name,namespace), separator = &#39;:&#39;
Definition: DDSplit.cc:3
const std::string & name() const
Returns the name.
Definition: DDName.cc:88