test
CMS 3D CMS Logo

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
DDName.cc
Go to the documentation of this file.
6 
7 #include <sstream>
8 
9 std::ostream & operator<<(std::ostream & os, const DDName & n)
10 {
11  os << n.ns() << ':' << n.name();
12  static const char * ev = getenv("DDNAMEID");
13  if (ev) os << '[' << n.id() << ']';
14  return os;
15 }
16 
17 // Implementation of DDName
18 
20  : id_(registerName(std::make_pair(name,ns))->second)
21 { }
22 
23 
25  : id_(0)
26 {
27  std::pair<std::string,std::string> result = DDSplit(name);
28  if (result.second == "") {
29  id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
30  }
31  else {
32  id_ = registerName(result)->second;
33  }
34 }
35 
36 
37 DDName::DDName( const char* name )
38  : id_(0)
39 {
40  std::pair<std::string,std::string> result = DDSplit(name);
41  if (result.second == "") {
42  id_ = registerName(std::make_pair(result.first,DDCurrentNamespace::ns()))->second;
43  }
44  else {
45  id_ = registerName(result)->second;
46  }
47 }
48 
49 
50 DDName::DDName( const char* name, const char* ns)
51  : id_(registerName(std::make_pair(std::string(name),std::string(ns)))->second)
52 { }
53 
54 
56  : id_(0)
57 { }
58 
60  : id_(id)
61 { }
62 
63 void DDName::defineId(const std::pair<std::string,std::string> & nm, DDName::id_type id)
64 {
66 
67  /*
68  Semantics:
69  If id exists && the registered value matches the given one, do nothing
70  If id exists && the registered value DOES NOT match, throw
71  If id DOES NOT exists, register with the given value
72  */
73  if ( id < id_type(id2n.size()) ) {
74  if(id2n[id]->first != nm) {
75  std::stringstream s;
76  s << id;
77  throw cms::Exception("DDException") << "DDName::DDName(std::pair<std::string,std::string>,id_type): id=" + s.str() + " reg-name=?";
78  }
79  }
80  else {
81  id2n.resize(id+1);
83  id2n[id] = DDI::Singleton<Registry>::instance().find(nm);
84  }
85 }
86 
87 const std::string & DDName::name() const
88 {
89  const static std::string ano_("anonymous");
90  const std::string * result;
91  if (id_ < 0) {
92  result = &ano_;
93  }
94  else {
95  result = &DDI::Singleton<IdToName>::instance()[id_]->first.first;
96  }
97  return *result;
98 }
99 
100 
101 const std::string & DDName::ns() const
102 {
103  const static std::string ano_("anonymous");
104  const std::string * result;
105  if (id_ < 0) {
106  result = &ano_;
107  }
108  else {
109  result = &DDI::Singleton<IdToName>::instance()[id_]->first.second;
110  }
111  return *result;
112 }
113 
114 
115 
116 bool DDName::exists(const std::string & name, const std::string & ns)
117 {
118  const std::pair<std::string,std::string> p(name,ns);
119  Registry::const_iterator it = DDI::Singleton<Registry>::instance().find(p);
120  return it != DDI::Singleton<Registry>::instance().end() ? true : false;
121 }
122 
123 
124 DDName::Registry::iterator DDName::registerName(const std::pair<std::string,std::string> & nm) {
127  Registry::size_type sz = reg_.size();
128  if (!sz) {
129  reg_[std::make_pair(std::string(""),std::string(""))] = 0;
130  idToName.push_back(reg_.begin());
131  ++sz;
132  }
133  Registry::value_type val(nm, sz);
134  std::pair<Registry::iterator,bool> result = reg_.insert(val);
135  if (result.second) {
136  idToName.push_back(result.first);
137  }
138  return result.first;
139 }
140 
id_type id() const
Definition: DDName.h:61
const std::string & ns() const
Returns the namespace.
Definition: DDName.cc:101
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:18
bool ev
static std::string & ns()
std::ostream & operator<<(std::ostream &out, const ALILine &li)
Definition: ALILine.cc:187
DDName()
Definition: DDName.cc:55
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:26
static value_type & instance()
tuple result
Definition: query.py:137
bool first
Definition: L1TdeRCT.cc:75
Container::value_type value_type
static void defineId(const std::pair< std::string, std::string > &, id_type id)
register pre-defined ids
Definition: DDName.cc:63
std::vector< Registry::const_iterator > IdToName
Definition: DDName.h:27
int id_type
Definition: DDName.h:25
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:116
id_type id_
Definition: DDName.h:67
static Registry::iterator registerName(const std::pair< std::string, std::string > &s)
Definition: DDName.cc:124
std::pair< std::string, std::string > DDSplit(const std::string &n)
split into (name,namespace), separator = &#39;:&#39;
Definition: DDSplit.cc:4
const std::string & name() const
Returns the name.
Definition: DDName.cc:87