CMS 3D CMS Logo

DDName.cc
Go to the documentation of this file.
5 
6 #include <ext/alloc_traits.h>
7 #include <cstdlib>
8 #include <sstream>
9 
10 std::ostream & operator<<( std::ostream & os, const DDName & n )
11 {
12  os << n.ns() << ':' << n.name();
13  return os;
14 }
15 
17  : id_( registerName( std::make_pair( name, ns ))->second )
18 { }
19 
21  : id_( 0 )
22 {
23  std::pair<std::string, std::string> result = DDSplit( name );
24  if( result.second.empty()) {
25  id_ = registerName( std::make_pair( result.first, DDCurrentNamespace::ns()))->second;
26  }
27  else {
28  id_ = registerName( result )->second;
29  }
30 }
31 
32 DDName::DDName( const char* name )
33  : id_( 0 )
34 {
35  std::pair< std::string, std::string > result = DDSplit( name );
36  if( result.second.empty()) {
37  id_ = registerName( std::make_pair( result.first, DDCurrentNamespace::ns()))->second;
38  }
39  else {
40  id_ = registerName( result )->second;
41  }
42 }
43 
44 DDName::DDName( const char* name, const char* ns )
45  : id_( registerName( std::make_pair( std::string( name ), std::string( ns )))->second )
46 { }
47 
49  : id_(0)
50 { }
51 
52 const std::string &
53 DDName::name() const
54 {
55  const static std::string ano_( "anonymous" );
56  const std::string * result;
57  if( id_ == 0 ) {
58  result = &ano_;
59  }
60  else {
61  result = &DDI::Singleton<IdToName>::instance()[id_]->first.first;
62  }
63  return *result;
64 }
65 
66 const std::string &
67 DDName::ns() const
68 {
69  const static std::string ano_( "anonymous" );
70  const std::string * result;
71  if( id_ == 0 ) {
72  result = &ano_;
73  }
74  else {
75  result = &DDI::Singleton<IdToName>::instance()[id_]->first.second;
76  }
77  return *result;
78 }
79 
80 DDName::Registry::iterator
81 DDName::registerName( const std::pair<std::string, std::string> & nm )
82 {
85  Registry::size_type sz = reg.size();
86  if( !sz )
87  {
88  reg[std::make_pair( std::string(""), std::string(""))] = 0;
89  idToName.emplace_back( reg.begin());
90  ++sz;
91  }
92  Registry::value_type val( nm, sz );
93  std::pair<Registry::iterator, bool> result = reg.insert( val );
94  if( result.second ) {
95  idToName.emplace_back( result.first );
96  }
97  return result.first;
98 }
std::ostream & operator<<(std::ostream &os, const DDName &n)
Definition: DDName.cc:10
std::map< std::pair< std::string, std::string >, id_type > Registry
Definition: DDName.h:20
const std::string & ns() const
Returns the namespace.
Definition: DDName.cc:67
DDName is used to identify DDD entities uniquely.
Definition: DDName.h:15
static std::string & ns()
DDName()
Definition: DDName.cc:48
uint16_t size_type
U second(std::pair< T, U > const &p)
static value_type & instance()
std::vector< Registry::const_iterator > IdToName
Definition: DDName.h:21
id_type id_
Definition: DDName.h:52
static Registry::iterator registerName(const std::pair< std::string, std::string > &s)
Definition: DDName.cc:81
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:53