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  static const char * ev = getenv("DDNAMEID");
16  if (ev) os << '[' << n.id() << ']';
17  return os;
18 }
19 
20 // Implementation of DDName
21 
23  : id_(registerName(std::make_pair(name,ns))->second)
24 { }
25 
26 
28  : id_(0)
29 {
30  std::pair<std::string,std::string> result = DDSplit(name);
31  if (result.second == "") {
32  id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
33  }
34  else {
35  id_ = registerName(result)->second;
36  }
37 }
38 
39 
40 DDName::DDName( const char* name )
41  : id_(0)
42 {
43  std::pair<std::string,std::string> result = DDSplit(name);
44  if (result.second == "") {
45  id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
46  }
47  else {
48  id_ = registerName(result)->second;
49  }
50 }
51 
52 
53 DDName::DDName( const char* name, const char* ns)
54  : id_(registerName(std::make_pair(std::string(name),std::string(ns)))->second)
55 { }
56 
57 
59  : id_(0)
60 { }
61 
63  : id_(id)
64 { }
65 
66 void DDName::defineId(const std::pair<std::string,std::string> & nm, DDName::id_type id)
67 {
69 
70  /*
71  Semantics:
72  If id exists && the registered value matches the given one, do nothing
73  If id exists && the registered value DOES NOT match, throw
74  If id DOES NOT exists, register with the given value
75  */
76  if ( id < id_type(id2n.size()) ) {
77  if(id2n[id]->first != nm) {
78  std::stringstream s;
79  s << id;
80  throw cms::Exception("DDException") << "DDName::DDName(std::pair<std::string,std::string>,id_type): id=" + s.str() + " reg-name=?";
81  }
82  }
83  else {
84  id2n.resize(id+1);
86  id2n[id] = DDI::Singleton<Registry>::instance().find(nm);
87  }
88 }
89 
90 const std::string & DDName::name() const
91 {
92  const static std::string ano_("anonymous");
93  const std::string * result;
94  if (id_ < 0) {
95  result = &ano_;
96  }
97  else {
98  result = &DDI::Singleton<IdToName>::instance()[id_]->first.first;
99  }
100  return *result;
101 }
102 
103 
104 const std::string & DDName::ns() const
105 {
106  const static std::string ano_("anonymous");
107  const std::string * result;
108  if (id_ < 0) {
109  result = &ano_;
110  }
111  else {
112  result = &DDI::Singleton<IdToName>::instance()[id_]->first.second;
113  }
114  return *result;
115 }
116 
117 
118 
120 {
121  const std::pair<std::string,std::string> p(name,ns);
122  Registry::const_iterator it = DDI::Singleton<Registry>::instance().find(p);
123  return it != DDI::Singleton<Registry>::instance().end() ? true : false;
124 }
125 
126 
127 DDName::Registry::iterator DDName::registerName(const std::pair<std::string,std::string> & nm) {
130  Registry::size_type sz = reg_.size();
131  if (!sz) {
132  reg_[std::make_pair(std::string(""),std::string(""))] = 0;
133  idToName.emplace_back(reg_.begin());
134  ++sz;
135  }
136  Registry::value_type val(nm, sz);
137  std::pair<Registry::iterator,bool> result = reg_.insert(val);
138  if (result.second) {
139  idToName.emplace_back(result.first);
140  }
141  return result.first;
142 }
143 
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:104
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
bool ev
static std::string & ns()
DDName()
Definition: DDName.cc:58
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:66
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:119
id_type id_
Definition: DDName.h:63
static Registry::iterator registerName(const std::pair< std::string, std::string > &s)
Definition: DDName.cc:127
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:90